<commit_before><?php

/**
 * @author <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
 */

namespace MaglLegacyApplication\Controller;

use MaglLegacyApplication\Application\MaglLegacy;
use MaglLegacyApplication\Options\LegacyControllerOptions;
use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;

class LegacyController extends AbstractActionController
{

    /**
     *
     * @var LegacyControllerOptions
     */
    private $options;

    /**
     *
     * @var MaglLegacy
     */
    private $legacy;

    public function __construct(LegacyControllerOptions $options, MaglLegacy $legacy)
    {
        $this->options = $options;
        $this->legacy = $legacy;
    }

    public function indexAction()
    {
        $docroot = getcwd() . '/' . $this->options->getDocRoot();
        $docroot = rtrim($docroot, '/');
        $scriptUri = '/' . ltrim($this->params('script'), '/'); // force leading '/'
        $legacyScriptFilename = $docroot . $scriptUri;

        if (!file_exists($legacyScriptFilename)) {
            // if we're here, the file doesn't really exist and we do not know what to do
            $response = $this->getResponse();

            /* @var $response Response */ //<-- this one for netbeans (WHY, NetBeans, WHY??)
            /** @var Response $response */ // <-- this one for other IDEs and code analyzers :)
            $response->setStatusCode(404);

            return;
        }

        //inform the application about the used script
        $this->legacy->setLegacyScriptFilename($legacyScriptFilename);
        $this->legacy->setLegacyScriptName($scriptUri);

        //inject get and request variables
        $this->setGetVariables();

        ob_start();
        include $legacyScriptFilename;
        $output = ob_get_clean();
        $this->getResponse()->setContent($output);

        return $this->getResponse();
    }

    private function setGetVariables()
    {
        $globals_options = $this->options->getGlobals();

        // if we should not set any global vars, we can return safely
        if (!$globals_options['get'] && !$globals_options['request']) {
            return;
        }

        // check if $_GET is used at all (ini - variables_order ?)
        // check if $_GET is written to $_REQUEST (ini - variables_order / request_order)
        // depending on request_order, check if $_REQUEST is already written and decide if we are allowed to override

        $request_order = ini_get('request_order');

        if ($request_order === false) {
            $request_order = ini_get('variables_order');
        }

        $get_prio = stripos($request_order, 'g');
        $post_prio = stripos($request_order, 'p');

        $forceOverrideRequest = $get_prio > $post_prio;

        $routeParams = $this->getEvent()->getRouteMatch()->getParams();

        foreach ($routeParams as $paramName => $paramValue) {

            if ($globals_options['get']) {
                $_GET[$paramName] = $paramValue;
            }

            if ($globals_options['request'] && ($forceOverrideRequest || !isset($_REQUEST[$paramName]))) {
                $_REQUEST[$paramName] = $paramValue;
            }
        }
    }
}<commit_msg>don't override $_GET variable if already set be request<commit_after><?php

/**
 * @author <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
 */

namespace MaglLegacyApplication\Controller;

use MaglLegacyApplication\Application\MaglLegacy;
use MaglLegacyApplication\Options\LegacyControllerOptions;
use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;

class LegacyController extends AbstractActionController
{

    /**
     *
     * @var LegacyControllerOptions
     */
    private $options;

    /**
     *
     * @var MaglLegacy
     */
    private $legacy;

    public function __construct(LegacyControllerOptions $options, MaglLegacy $legacy)
    {
        $this->options = $options;
        $this->legacy = $legacy;
    }

    public function indexAction()
    {
        $docroot = getcwd() . '/' . $this->options->getDocRoot();
        $docroot = rtrim($docroot, '/');
        $scriptUri = '/' . ltrim($this->params('script'), '/'); // force leading '/'
        $legacyScriptFilename = $docroot . $scriptUri;

        if (!file_exists($legacyScriptFilename)) {
            // if we're here, the file doesn't really exist and we do not know what to do
            $response = $this->getResponse();

            /* @var $response Response */ //<-- this one for netbeans (WHY, NetBeans, WHY??)
            /** @var Response $response */ // <-- this one for other IDEs and code analyzers :)
            $response->setStatusCode(404);

            return;
        }

        //inform the application about the used script
        $this->legacy->setLegacyScriptFilename($legacyScriptFilename);
        $this->legacy->setLegacyScriptName($scriptUri);

        //inject get and request variables
        $this->setGetVariables();

        ob_start();
        include $legacyScriptFilename;
        $output = ob_get_clean();
        $this->getResponse()->setContent($output);

        return $this->getResponse();
    }

    private function setGetVariables()
    {
        $globals_options = $this->options->getGlobals();

        // if we should not set any global vars, we can return safely
        if (!$globals_options['get'] && !$globals_options['request']) {
            return;
        }

        // check if $_GET is used at all (ini - variables_order ?)
        // check if $_GET is written to $_REQUEST (ini - variables_order / request_order)
        // depending on request_order, check if $_REQUEST is already written and decide if we are allowed to override

        $request_order = ini_get('request_order');

        if ($request_order === false) {
            $request_order = ini_get('variables_order');
        }

        $get_prio = stripos($request_order, 'g');
        $post_prio = stripos($request_order, 'p');

        $forceOverrideRequest = $get_prio > $post_prio;

        $routeParams = $this->getEvent()->getRouteMatch()->getParams();

        foreach ($routeParams as $paramName => $paramValue) {

            if ($globals_options['get'] && !isset($_GET[$paramName])) {
                $_GET[$paramName] = $paramValue;
            }

            if ($globals_options['request'] && ($forceOverrideRequest || !isset($_REQUEST[$paramName]))) {
                $_REQUEST[$paramName] = $paramValue;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.13417798809342085, 'fraction_numerical': 0.0015264845061822622, 'mean_word_length': 2.4832535885167464, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 53, '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': '1214840', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1812, 'n_words': 429}