<commit_before><?php

namespace Mouf\Mvc\Splash\Routers;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Mouf\Mvc\Splash\Utils\SplashException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;

/**
 * This router :
 *  - just checks that some PHP settings are not exceeded : max_input_vars, max_post_size
 *  - doesn't actually 'routes' the request. It's more like a filter to me applied and check the request.
 *  - should be placed BEFORE the effective applications router and AFTER the Exceptions handling routers.
 *
 * @author <NAME>
 */
class PhpVarsCheckRouter implements MiddlewareInterface
{
    /**
     * The logger used by Splash.
     *
     * @var LoggerInterface
     */
    private $log;

    /**
     * A simple counter to check requests' length (GET, POST, REQUEST).
     *
     * @var int
     */
    private $count;

    /**
     * @Important
     *
     * @param LoggerInterface $log The logger used by Splash
     */
    public function __construct(LoggerInterface $log = null)
    {
        $this->log = $log;
    }

    /**
     * Get the min in 2 values if there exist.
     *
     * @param int $val1
     * @param int $val2
     *
     * @return int|NULL
     */
    private function getMinInConfiguration($val1, $val2)
    {
        if ($val1 && $val2) {
            return min(array($val1, $val2));
        }
        if ($val1) {
            return $val1;
        }
        if ($val2) {
            return $val2;
        }

        return null;
    }

    /**
     * Returns the number of bytes from php.ini parameter.
     *
     * @param $val
     *
     * @return int|string
     */
    private static function iniGetBytes($val)
    {
        $val = trim(ini_get($val));
        if ($val != '') {
            $last = strtolower(
                    $val{strlen($val) - 1}
            );
        } else {
            $last = '';
        }
        $val = (int) $val;
        switch ($last) {
            // The 'G' modifier is available since PHP 5.1.0
            case 'g':
                $val *= 1024;
            case 'm':
                $val *= 1024;
            case 'k':
                $val *= 1024;
        }

        return $val;
    }

    /**
     * Count number of element in array.
     *
     * @param mixed $item
     * @param mixed $key
     */
    private function countRecursive($item, $key)
    {
        ++$this->count;
    }

    /**
     * Process an incoming server request and return a response, optionally delegating
     * to the next middleware component to create the response.
     *
     * @param Request $request
     * @param DelegateInterface $delegate
     *
     * @return Response
     * @throws \Mouf\Mvc\Splash\Utils\SplashException
     */
    public function process(Request $request, DelegateInterface $delegate)
    {
        // Check if there is a limit of input number in php
        // Throw exception if the limit is reached
        if (ini_get('max_input_vars') || ini_get('suhosin.get.max_vars')) {
            $maxGet = $this->getMinInConfiguration(ini_get('max_input_vars'), ini_get('suhosin.get.max_vars'));
            if ($maxGet !== null) {
                $this->count = 0;
                array_walk_recursive($_GET, array($this, 'countRecursive'));
                if ($this->count === $maxGet) {
                    if ($this->log !== null) {
                        $this->log->error('Max input vars reaches for get parameters ({maxGet}). Check your variable max_input_vars in php.ini or suhosin module suhosin.get.max_vars.', ['maxGet' => $maxGet]);
                    }
                    throw new SplashException('Max input vars reaches for get parameters ('.$maxGet.'). Check your variable max_input_vars in php.ini or suhosin module suhosin.get.max_vars.');
                }
            }
        }
        if (ini_get('max_input_vars') || ini_get('suhosin.post.max_vars')) {
            $maxPost = $this->getMinInConfiguration(ini_get('max_input_vars'), ini_get('suhosin.post.max_vars'));
            if ($maxPost !== null) {
                $this->count = 0;
                array_walk_recursive($_POST, array($this, 'countRecursive'));
                if ($this->count === $maxPost) {
                    if ($this->log !== null) {
                        $this->log->error('Max input vars reaches for post parameters ({maxPost}). Check your variable max_input_vars in php.ini or suhosin module suhosin.post.max_vars.', ['maxPost' => $maxPost]);
                    }
                    throw new SplashException('Max input vars reaches for post parameters ('.$maxPost.'). Check your variable max_input_vars in php.ini or suhosin module suhosin.post.max_vars.');
                }
            }
        }
        if (ini_get('max_input_vars') || ini_get('suhosin.request.max_vars')) {
            $maxRequest = $this->getMinInConfiguration(ini_get('max_input_vars'), ini_get('suhosin.request.max_vars'));
            if ($maxRequest !== null) {
                $this->count = 0;
                array_walk_recursive($_REQUEST, array($this, 'countRecursive'));<commit_msg>Resolving error when more that 1000 parameter are sent to the POST >= max_input_vars<commit_after><?php

namespace Mouf\Mvc\Splash\Routers;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Mouf\Mvc\Splash\Utils\SplashException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;

/**
 * This router :
 *  - just checks that some PHP settings are not exceeded : max_input_vars, max_post_size
 *  - doesn't actually 'routes' the request. It's more like a filter to me applied and check the request.
 *  - should be placed BEFORE the effective applications router and AFTER the Exceptions handling routers.
 *
 * @author <NAME>
 */
class PhpVarsCheckRouter implements MiddlewareInterface
{
    /**
     * The logger used by Splash.
     *
     * @var LoggerInterface
     */
    private $log;

    /**
     * A simple counter to check requests' length (GET, POST, REQUEST).
     *
     * @var int
     */
    private $count;

    /**
     * @Important
     *
     * @param LoggerInterface $log The logger used by Splash
     */
    public function __construct(LoggerInterface $log = null)
    {
        $this->log = $log;
    }

    /**
     * Get the min in 2 values if there exist.
     *
     * @param int $val1
     * @param int $val2
     *
     * @return int|NULL
     */
    private function getMinInConfiguration($val1, $val2)
    {
        if ($val1 && $val2) {
            return min(array($val1, $val2));
        }
        if ($val1) {
            return $val1;
        }
        if ($val2) {
            return $val2;
        }

        return null;
    }

    /**
     * Returns the number of bytes from php.ini parameter.
     *
     * @param $val
     *
     * @return int|string
     */
    private static function iniGetBytes($val)
    {
        $val = trim(ini_get($val));
        if ($val != '') {
            $last = strtolower(
                    $val{strlen($val) - 1}
            );
        } else {
            $last = '';
        }
        $val = (int) $val;
        switch ($last) {
            // The 'G' modifier is available since PHP 5.1.0
            case 'g':
                $val *= 1024;
            case 'm':
                $val *= 1024;
            case 'k':
                $val *= 1024;
        }

        return $val;
    }

    /**
     * Count number of element in array.
     *
     * @param mixed $item
     * @param mixed $key
     */
    private function countRecursive($item, $key)
    {
        ++$this->count;
    }

    /**
     * Process an incoming server request and return a response, optionally delegating
     * to the next middleware component to create the response.
     *
     * @param Request $request
     * @param DelegateInterface $delegate
     *
     * @return Response
     * @throws \Mouf\Mvc\Splash\Utils\SplashException
     */
    public function process(Request $request, DelegateInterface $delegate)
    {
        // Check if there is a limit of input number in php
        // Throw exception if the limit is reached
        if (ini_get('max_input_vars') || ini_get('suhosin.get.max_vars')) {
            $maxGet = $this->getMinInConfiguration(ini_get('max_input_vars'), ini_get('suhosin.get.max_vars'));
            if ($maxGet !== null) {
                $this->count = 0;
                array_walk_recursive($_GET, array($this, 'countRecursive'));
                if ($this->count >= $maxGet) {
                    if ($this->log !== null) {
                        $this->log->error('Max input vars reaches for get parameters ({maxGet}). Check your variable max_input_vars in php.ini or suhosin module suhosin.get.max_vars.', ['maxGet' => $maxGet]);
                    }
                    throw new SplashException('Max input vars reaches for get parameters ('.$maxGet.'). Check your variable max_input_vars in php.ini or suhosin module suhosin.get.max_vars.');
                }
            }
        }
        if (ini_get('max_input_vars') || ini_get('suhosin.post.max_vars')) {
            $maxPost = $this->getMinInConfiguration(ini_get('max_input_vars'), ini_get('suhosin.post.max_vars'));
            if ($maxPost !== null) {
                $this->count = 0;
                array_walk_recursive($_POST, array($this, 'countRecursive'));
                if ($this->count >= $maxPost) {
                    if ($this->log !== null) {
                        $this->log->error('Max input vars reaches for post parameters ({maxPost}). Check your variable max_input_vars in php.ini or suhosin module suhosin.post.max_vars.', ['maxPost' => $maxPost]);
                    }
                    throw new SplashException('Max input vars reaches for post parameters ('.$maxPost.'). Check your variable max_input_vars in php.ini or suhosin module suhosin.post.max_vars.');
                }
            }
        }
        if (ini_get('max_input_vars') || ini_get('suhosin.request.max_vars')) {
            $maxRequest = $this->getMinInConfiguration(ini_get('max_input_vars'), ini_get('suhosin.request.max_vars'));
            if ($maxRequest !== null) {
                $this->count = 0;
                array_walk_recursive($_REQUEST, array($this, 'countRecursive'));
{'fraction_non_alphanumeric': 0.12049795685640977, 'fraction_numerical': 0.006462035541195477, 'mean_word_length': 1.9695259593679457, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '929397', 'n_tokens_mistral': 3160, 'n_tokens_neox': 3016, 'n_words': 817}