<commit_before><?php
/**
 * @version      $Id$
 * @package      Koowa_Template
 * @subpackage    Filter
 * @copyright    Copyright (C) 2007 - 2012 <NAME>. All rights reserved.
 * @license      GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link         http://www.nooku.org
 */

/**
 * Template write filter to handle form html elements
 *
 * For forms that use a post method this filter adds a token to prevent CSRF. For forms
 * that use a get method this filter adds the action url query params as hidden fields
 * to comply with the html form standard.
 *
 * @author      <NAME> <<EMAIL>>
 * @package     Koowa_Template
 * @subpackage  Filter
 * @see         http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.4
 */
class KTemplateFilterForm extends KTemplateFilterAbstract implements KTemplateFilterWrite
{
    /**
     * The form token value
     *
     * @var string
     */
    protected $_token_value;

    /**
     * The form token name
     *
     * @var string
     */
    protected $_token_name;

    /**
     * Constructor.
     *
     * @param   object  An optional KConfig object with configuration options
     */
    public function __construct(KConfig $config)
    {
        parent::__construct($config);

        $this->_token_value = $config->token_value;
        $this->_token_name  = $config->token_name;
    }

    /**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   object  An optional KConfig object with configuration options
     * @return void
     */
    protected function _initialize(KConfig $config)
    {
        $config->append(array(
            'token_value' => '',
            'token_name'  => '_token',
        ));

        parent::_initialize($config);
    }

    /**
     * Get the session token value.
     *
     * If a token isn't set yet one will be generated. Tokens are used to secure forms
     * from spamming attacks. Once a token has been generated the system will check the
     * post request to see if it is present, if not it will invalidate the session.
     *
     * @param boolean If true, force a new token to be created
     * @return string The session token
     */
    protected function _tokenValue($force = false)
    {
        if (empty($this->_token_value) || $force) {
            $this->_token_value = $this->getService('application.session')->getToken($force);
        }

        return $this->_token_value;
    }

    /**
     * Get the session token name
     *
     * Tokens are used to secure forms from spamming attacks. Once a token
     * has been generated the system will check the post request to see if
     * it is present, if not it will invalidate the session.
     *
     * @return string The session token
     */
    protected function _tokenName()
    {
        return $this->_token_name;
    }

    /**
     * Add unique token field
     *
     * @param string
     * @return KTemplateFilterForm
     */
    public function write(&$text)
    {
        // All: Add the action if left empty
        if (preg_match_all('#<\s*form.*?action=""#im', $text, $matches, PREG_SET_ORDER))
        {
            $view = $this->getTemplate()->getView();
            $state = $view->getModel()->getState();
            $action = $view->getRoute(http_build_query($state->getData($state->isUnique())));

            foreach ($matches as $match)
            {
                $str = str_replace('action=""', 'action="' . $action . '"', $match[0]);
                $text = str_replace($match[0], $str, $text);
            }
        }

        // POST : Add token 
        $matches = array();
        preg_match_all('/(<form.*method="post".*>)/i', $text, $matches, PREG_SET_ORDER);

        foreach ($matches as $match)
        {
            $input = PHP_EOL . '<input type="hidden" name="' . $this->_tokenName() . '" value="' . $this->_tokenValue() . '" />';
            $text = str_replace($match[0], $match[0] . $input, $text, $count);
        }

        // GET : Add token to .-koowa-grid forms
        $matches = array();
        preg_match_all('#(<\s*?form\s+?.*?class=(?:\'|")[^\'"]*?-koowa-grid.*?(?:\'|").*?)#im', $text, $matches, PREG_SET_ORDER);

        foreach ($matches as $match)
        {
            $input = ' data-token-name="' . $this->_tokenName() . '" data-token-value="' . $this->_tokenValue() . '"';<commit_msg>re #226 : Replace $this->getService('application.session') by $this->getService('user')->getSession()<commit_after><?php
/**
 * @version      $Id$
 * @package      Koowa_Template
 * @subpackage    Filter
 * @copyright    Copyright (C) 2007 - 2012 <NAME>. All rights reserved.
 * @license      GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link         http://www.nooku.org
 */

/**
 * Template write filter to handle form html elements
 *
 * For forms that use a post method this filter adds a token to prevent CSRF. For forms
 * that use a get method this filter adds the action url query params as hidden fields
 * to comply with the html form standard.
 *
 * @author      <NAME> <<EMAIL>>
 * @package     Koowa_Template
 * @subpackage  Filter
 * @see         http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.4
 */
class KTemplateFilterForm extends KTemplateFilterAbstract implements KTemplateFilterWrite
{
    /**
     * The form token value
     *
     * @var string
     */
    protected $_token_value;

    /**
     * The form token name
     *
     * @var string
     */
    protected $_token_name;

    /**
     * Constructor.
     *
     * @param   object  An optional KConfig object with configuration options
     */
    public function __construct(KConfig $config)
    {
        parent::__construct($config);

        $this->_token_value = $config->token_value;
        $this->_token_name  = $config->token_name;
    }

    /**
     * Initializes the options for the object
     *
     * Called from {@link __construct()} as a first step of object instantiation.
     *
     * @param   object  An optional KConfig object with configuration options
     * @return void
     */
    protected function _initialize(KConfig $config)
    {
        $config->append(array(
            'token_value' => '',
            'token_name'  => '_token',
        ));

        parent::_initialize($config);
    }

    /**
     * Get the session token value.
     *
     * If a token isn't set yet one will be generated. Tokens are used to secure forms
     * from spamming attacks. Once a token has been generated the system will check the
     * post request to see if it is present, if not it will invalidate the session.
     *
     * @param boolean If true, force a new token to be created
     * @return string The session token
     */
    protected function _tokenValue($force = false)
    {
        if (empty($this->_token_value) || $force) {
            $this->_token_value = $this->getService('user')->getSession()->getToken($force);
        }

        return $this->_token_value;
    }

    /**
     * Get the session token name
     *
     * Tokens are used to secure forms from spamming attacks. Once a token
     * has been generated the system will check the post request to see if
     * it is present, if not it will invalidate the session.
     *
     * @return string The session token
     */
    protected function _tokenName()
    {
        return $this->_token_name;
    }

    /**
     * Add unique token field
     *
     * @param string
     * @return KTemplateFilterForm
     */
    public function write(&$text)
    {
        // All: Add the action if left empty
        if (preg_match_all('#<\s*form.*?action=""#im', $text, $matches, PREG_SET_ORDER))
        {
            $view = $this->getTemplate()->getView();
            $state = $view->getModel()->getState();
            $action = $view->getRoute(http_build_query($state->toArray($state->isUnique())));

            foreach ($matches as $match)
            {
                $str = str_replace('action=""', 'action="' . $action . '"', $match[0]);
                $text = str_replace($match[0], $str, $text);
            }
        }

        // POST : Add token 
        $matches = array();
        preg_match_all('/(<form.*method="post".*>)/i', $text, $matches, PREG_SET_ORDER);

        foreach ($matches as $match)
        {
            $input = PHP_EOL . '<input type="hidden" name="' . $this->_tokenName() . '" value="' . $this->_tokenValue() . '" />';
            $text = str_replace($match[0], $match[0] . $input, $text, $count);
        }

        // GET : Add token to .-koowa-grid forms
        $matches = array();
        preg_match_all('#(<\s*?form\s+?.*?class=(?:\'|")[^\'"]*?-koowa-grid.*?(?:\'|").*?)#im', $text, $matches, PREG_SET_ORDER);

        foreach ($matches as $match)
        {
            $input = ' data-token-name="' . $this->_tokenName() . '" data-token-value="' . $this->_tokenValue() . '"';
{'fraction_non_alphanumeric': 0.13974287311347122, 'fraction_numerical': 0.005477920626048072, 'mean_word_length': 2.2145167085878548, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5002727', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2585, 'n_words': 753}