<commit_before><?php

/**
 * URI.php - Jaxon request URI detector
 *
 * Detect and parse the URI of the Jaxon request being processed.
 *
 * @package jaxon-core
 * @author <NAME> <<EMAIL>>
 * @copyright 2016 <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
 * @link https://github.com/jaxon-php/jaxon-core
 */

namespace Jaxon\Utils\Http;

class URI
{
    /**
     * Get the URL from the $_SERVER var
     *
     * @var array       &$aURL      The URL data
     * @var string      $sKey       The key in the $_SERVER array
     *
     * @return void
     */
    private function getHostFromServer(array &$aURL, $sKey)
    {
        if(empty($aURL['host']) && !empty($_SERVER[$sKey]))
        {
            if(strpos($_SERVER[$sKey], ':') > 0)
            {
                list($aURL['host'], $aURL['port']) = explode(':', $_SERVER[$sKey]);
            }
            else
            {
                $aURL['host'] = $_SERVER[$sKey];
            }
        }
    }

    /**
     * Detect the URI of the current request
     *
     * @return string        The URI
     */
    public function detect()
    {
        $aURL = [];
        // Try to get the request URL
        if(!empty($_SERVER['REQUEST_URI']))
        {
            $_SERVER['REQUEST_URI'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $_SERVER['REQUEST_URI']);
            $aURL = parse_url($_SERVER['REQUEST_URI']);
            if(!is_array($aURL))
            {
                $aURL = [];
            }
        }

        // Fill in the empty values
        if(empty($aURL['scheme']))
        {
            if(!empty($_SERVER['HTTP_SCHEME']))
            {
                $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
            }
            else
            {
                $aURL['scheme'] = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http');
            }
        }

        $this->getHostFromServer($aURL, 'HTTP_X_FORWARDED_HOST');
        $this->getHostFromServer($aURL, 'HTTP_HOST');
        $this->getHostFromServer($aURL, 'SERVER_NAME');
        if(empty($aURL['host']))
        {
            throw new \Jaxon\Exception\URI();
        }

        if(empty($aURL['port']) && !empty($_SERVER['SERVER_PORT']))
        {
            $aURL['port'] = $_SERVER['SERVER_PORT'];
        }

        if(!empty($aURL['path']) && strlen(basename($aURL['path'])) == 0)
        {
            unset($aURL['path']);
        }

        if(empty($aURL['path']))
        {
            if(!empty($_SERVER['PATH_INFO']))
            {
                $sPath = parse_url($_SERVER['PATH_INFO']);
            }
            else
            {
                $sPath = parse_url($_SERVER['PHP_SELF']);
            }
            if(isset($sPath['path']))
            {
                $aURL['path'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $sPath['path']);
            }
            unset($sPath);
        }

        if(empty($aURL['query']) && !empty($_SERVER['QUERY_STRING']))
        {
            $aURL['query'] = $_SERVER['QUERY_STRING'];
        }

        if(!empty($aURL['query']))
        {
            $aURL['query'] = '?' . $aURL['query'];
        }

        // Build the URL: Start with scheme, user and pass
        $sURL = $aURL['scheme'] . '://';
        if(!empty($aURL['user']))
        {
            $sURL .= $aURL['user'];
            if(!empty($aURL['pass']))
            {
                $sURL .= ':' . $aURL['pass'];
            }
            $sURL .= '@';
        }

        // Add the host
        $sURL .= $aURL['host'];

        // Add the port if needed
        if(!empty($aURL['port']) &&
            (($aURL['scheme'] == 'http' && $aURL['port'] != 80) ||
            ($aURL['scheme'] == 'https' && $aURL['port'] != 443)))
        {
            $sURL .= ':' . $aURL['port'];
        }

        // Add the path and the query string
        $sURL .= $aURL['path'] . @$aURL['query'];

        // Clean up
        unset($aURL);

        $aURL = explode("?", $sURL);

        if(1 < count($aURL))
        {
            $aQueries = explode("&", $aURL[1]);

            foreach($aQueries as $sKey => $sQuery)
            {
                if("jxnGenerate" == substr($sQuery, 0, 11))
                {<commit_msg>Bugfix #72<commit_after><?php

/**
 * URI.php - Jaxon request URI detector
 *
 * Detect and parse the URI of the Jaxon request being processed.
 *
 * @package jaxon-core
 * @author <NAME> <<EMAIL>>
 * @copyright 2016 <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
 * @link https://github.com/jaxon-php/jaxon-core
 */

namespace Jaxon\Utils\Http;

class URI
{
    /**
     * Get the URL from the $_SERVER var
     *
     * @var array       &$aURL      The URL data
     * @var string      $sKey       The key in the $_SERVER array
     *
     * @return void
     */
    private function getHostFromServer(array &$aURL, $sKey)
    {
        if(empty($aURL['host']) && !empty($_SERVER[$sKey]))
        {
            if(strpos($_SERVER[$sKey], ':') > 0)
            {
                list($aURL['host'], $aURL['port']) = explode(':', $_SERVER[$sKey]);
            }
            else
            {
                $aURL['host'] = $_SERVER[$sKey];
            }
        }
    }

    /**
     * Detect the URI of the current request
     *
     * @return string        The URI
     */
    public function detect()
    {
        $aURL = [];
        // Try to get the request URL
        if(!empty($_SERVER['REQUEST_URI']))
        {
            $_SERVER['REQUEST_URI'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $_SERVER['REQUEST_URI']);
            $aURL = parse_url($_SERVER['REQUEST_URI']);
            if(!is_array($aURL))
            {
                $aURL = [];
            }
        }

        // Fill in the empty values
        if(empty($aURL['scheme']))
        {
            if(!empty($_SERVER['HTTP_SCHEME']))
            {
                $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
            }
            else
            {
                $aURL['scheme'] = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http');
            }
        }

        $this->getHostFromServer($aURL, 'HTTP_X_FORWARDED_HOST');
        $this->getHostFromServer($aURL, 'HTTP_HOST');
        $this->getHostFromServer($aURL, 'SERVER_NAME');
        if(empty($aURL['host']))
        {
            throw new \Jaxon\Exception\URI();
        }

        if(empty($aURL['port']) && !empty($_SERVER['SERVER_PORT']))
        {
            $aURL['port'] = $_SERVER['SERVER_PORT'];
        }

        if(!empty($aURL['path']) && strlen(basename($aURL['path'])) == 0)
        {
            unset($aURL['path']);
        }

        if(empty($aURL['path']))
        {
            if(!empty($_SERVER['PATH_INFO']))
            {
                $sPath = parse_url($_SERVER['PATH_INFO']);
            }
            else
            {
                $sPath = parse_url($_SERVER['PHP_SELF']);
            }
            if(isset($sPath['path']))
            {
                $aURL['path'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $sPath['path']);
            }
            unset($sPath);
        }

        if(empty($aURL['query']))
        {
            $aURL['query'] = empty($_SERVER['QUERY_STRING']) ? '' : $_SERVER['QUERY_STRING'];
        }

        if(!empty($aURL['query']))
        {
            $aURL['query'] = '?' . $aURL['query'];
        }

        // Build the URL: Start with scheme, user and pass
        $sURL = $aURL['scheme'] . '://';
        if(!empty($aURL['user']))
        {
            $sURL .= $aURL['user'];
            if(!empty($aURL['pass']))
            {
                $sURL .= ':' . $aURL['pass'];
            }
            $sURL .= '@';
        }

        // Add the host
        $sURL .= $aURL['host'];

        // Add the port if needed
        if(!empty($aURL['port']) &&
            (($aURL['scheme'] == 'http' && $aURL['port'] != 80) ||
            ($aURL['scheme'] == 'https' && $aURL['port'] != 443)))
        {
            $sURL .= ':' . $aURL['port'];
        }

        // Add the path and the query string
        $sURL .= $aURL['path'] . $aURL['query'];

        // Clean up
        unset($aURL);

        $aURL = explode("?", $sURL);

        if(1 < count($aURL))
        {
            $aQueries = explode("&", $aURL[1]);

            foreach($aQueries as $sKey => $sQuery)
            {
                if("jxnGenerate" == substr($sQuery, 0, 11))
                {
{'fraction_non_alphanumeric': 0.19809899153819405, 'fraction_numerical': 0.007186739306827402, 'mean_word_length': 1.5693865396069089, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 4, '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': '774907', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2639, 'n_words': 471}