<commit_before><?php
class Kwf_Util_Setup
{
    private static function _getZendPath()
    {
        return VENDOR_PATH.'/koala-framework/zendframework1/library';
    }

    public static function minimalBootstrapAndGenerateFile()
    {
        if (!defined('VENDOR_PATH')) define('VENDOR_PATH', 'vendor');
        if (VENDOR_PATH == '../vendor') {
            $kwfPath = '..';
        } else {
            $kwfPath = VENDOR_PATH.'/koala-framework/koala-framework';
        }
        if (!defined('KWF_PATH')) define('KWF_PATH', $kwfPath);

        //reset include path, don't use anything from php.ini
        set_include_path('.' . PATH_SEPARATOR . $kwfPath . PATH_SEPARATOR . self::_getZendPath());

        require_once $kwfPath.'/Kwf/Loader.php';
        Kwf_Loader::registerAutoload();

        Zend_Registry::setClassName('Kwf_Registry');

        error_reporting(E_ALL^E_STRICT);

        class_exists('Kwf_Trl'); //trigger autoload

        umask(000); //nicht 002 weil wwwrun und kwcms in unterschiedlichen gruppen

        file_put_contents('cache/setup'.Kwf_Setup::CACHE_SETUP_VERSION.'.php', self::generateCode());

        Zend_Registry::_unsetInstance(); //cache/setup?.php will call setClassName again
    }

    private static function _generatePreloadClassesCode($preloadClasses, $ip)
    {
        $ret = '';
        foreach ($preloadClasses as $cls) {
            foreach ($ip as $path) {
                $file = $path.'/'.str_replace('_', '/', $cls).'.php';
                if (file_exists($file)) {
                    if (substr($file, 0, strlen(getcwd())) != getcwd()) {
                        throw new Kwf_Exception("'$file' not in cwd");
                    }
                    $ret .= "require(\$cwd.'/".substr($file, strlen(getcwd())+1)."');\n";
                    break;
                }
            }
        }
        return $ret;
    }

    public static function generateCode()
    {
        $preloadIncludePaths = array();
        $namespaces = require VENDOR_PATH.'/composer/autoload_namespaces.php';
        $ret = array();
        foreach ($namespaces as $ns=>$dirs) {
            $preloadIncludePaths = array_merge($preloadIncludePaths, $dirs);
        }

        $ret = "<?php\n";

        $ret .= "\$cwd = getcwd();\n";

        $preloadClasses = array(
            'Kwf_Benchmark',
            'Kwf_Loader',
            'Kwf_Debug',
        );
        $ret .= "if (!class_exists('Kwf_Loader', false)) {\n";
        $ret .= self::_generatePreloadClassesCode($preloadClasses, $preloadIncludePaths);
        $ret .= "}\n";

        $ret .= "Kwf_Benchmark::\$startTime = microtime(true);\n";
        $ret .= "\n";

        //override the default cache (/tmp) dir used by Zend_Cache_Backend to have the cache per web
        $ret .= "\$_SERVER['TMP'] = 'cache/zend';\n";

        //only replace configured value to avoid spoofing
        //required eg. behind load balancers
        if (Kwf_Config::getValueArray('server.replaceVars.remoteAddr')) {
            $a = Kwf_Config::getValueArray('server.replaceVars.remoteAddr');
            $ret .= "\nif (isset(\$_SERVER['REMOTE_ADDR']) && \$_SERVER['REMOTE_ADDR'] == '$a[if]' && isset(\$_SERVER['$a[replace]'])) {\n";
            $ret .= "    \$_SERVER['REMOTE_ADDR'] = \$_SERVER['$a[replace]'];\n";
            $ret .= "}\n";
        }

        //try different values, if one spoofs this this is no security issue
        $ret .= "if (isset(\$_SERVER['HTTP_SSL_SESSION_ID'])) \$_SERVER['HTTPS'] = 'on';\n";
        $ret .= "if (isset(\$_SERVER['HTTP_SESSION_ID_TAG'])) \$_SERVER['HTTPS'] = 'on';\n";
        $ret .= "if (isset(\$_SERVER['HTTP_X_FORWARDED_PROTO']) && \$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {\n";
        $ret .= "    \$_SERVER['HTTPS'] = 'on';\n";
        $ret .= "}\n";

        if (VENDOR_PATH == '../vendor') {
            $kwfPath = '..';
        } else {<commit_msg>Allow pattern with * for server.replaceVars.remoteAddr<commit_after><?php
class Kwf_Util_Setup
{
    private static function _getZendPath()
    {
        return VENDOR_PATH.'/koala-framework/zendframework1/library';
    }

    public static function minimalBootstrapAndGenerateFile()
    {
        if (!defined('VENDOR_PATH')) define('VENDOR_PATH', 'vendor');
        if (VENDOR_PATH == '../vendor') {
            $kwfPath = '..';
        } else {
            $kwfPath = VENDOR_PATH.'/koala-framework/koala-framework';
        }
        if (!defined('KWF_PATH')) define('KWF_PATH', $kwfPath);

        //reset include path, don't use anything from php.ini
        set_include_path('.' . PATH_SEPARATOR . $kwfPath . PATH_SEPARATOR . self::_getZendPath());

        require_once $kwfPath.'/Kwf/Loader.php';
        Kwf_Loader::registerAutoload();

        Zend_Registry::setClassName('Kwf_Registry');

        error_reporting(E_ALL^E_STRICT);

        class_exists('Kwf_Trl'); //trigger autoload

        umask(000); //nicht 002 weil wwwrun und kwcms in unterschiedlichen gruppen

        file_put_contents('cache/setup'.Kwf_Setup::CACHE_SETUP_VERSION.'.php', self::generateCode());

        Zend_Registry::_unsetInstance(); //cache/setup?.php will call setClassName again
    }

    private static function _generatePreloadClassesCode($preloadClasses, $ip)
    {
        $ret = '';
        foreach ($preloadClasses as $cls) {
            foreach ($ip as $path) {
                $file = $path.'/'.str_replace('_', '/', $cls).'.php';
                if (file_exists($file)) {
                    if (substr($file, 0, strlen(getcwd())) != getcwd()) {
                        throw new Kwf_Exception("'$file' not in cwd");
                    }
                    $ret .= "require(\$cwd.'/".substr($file, strlen(getcwd())+1)."');\n";
                    break;
                }
            }
        }
        return $ret;
    }

    public static function generateCode()
    {
        $preloadIncludePaths = array();
        $namespaces = require VENDOR_PATH.'/composer/autoload_namespaces.php';
        $ret = array();
        foreach ($namespaces as $ns=>$dirs) {
            $preloadIncludePaths = array_merge($preloadIncludePaths, $dirs);
        }

        $ret = "<?php\n";

        $ret .= "\$cwd = getcwd();\n";

        $preloadClasses = array(
            'Kwf_Benchmark',
            'Kwf_Loader',
            'Kwf_Debug',
        );
        $ret .= "if (!class_exists('Kwf_Loader', false)) {\n";
        $ret .= self::_generatePreloadClassesCode($preloadClasses, $preloadIncludePaths);
        $ret .= "}\n";

        $ret .= "Kwf_Benchmark::\$startTime = microtime(true);\n";
        $ret .= "\n";

        //override the default cache (/tmp) dir used by Zend_Cache_Backend to have the cache per web
        $ret .= "\$_SERVER['TMP'] = 'cache/zend';\n";

        //only replace configured value to avoid spoofing
        //required eg. behind load balancers
        if (Kwf_Config::getValueArray('server.replaceVars.remoteAddr')) {
            $a = Kwf_Config::getValueArray('server.replaceVars.remoteAddr');
            if (substr($a['if'], -2) == '.*') {
                $comparison = "substr(\$_SERVER['REMOTE_ADDR'], 0, ".(strlen($a['if'])-1).") == '".substr($a['if'], 0, -1)."'";
            } else {
                $comparison = "\$_SERVER['REMOTE_ADDR'] == '$a[if]'";
            }
            $ret .= "\nif (isset(\$_SERVER['REMOTE_ADDR']) && $comparison && isset(\$_SERVER['$a[replace]'])) {\n";
            $ret .= "    \$_SERVER['REMOTE_ADDR'] = \$_SERVER['$a[replace]'];\n";
            $ret .= "}\n";
        }

        //try different values, if one spoofs this this is no security issue
        $ret .= "if (isset(\$_SERVER['HTTP_SSL_SESSION_ID'])) \$_SERVER['HTTPS'] = 'on';\n";
        $ret .= "if (isset(\$_SERVER['HTTP_SESSION_ID_TAG'])) \$_SERVER['HTTPS'] = 'on';\n";
        $ret .= "if (isset(\$_SERVER['HTTP_X_FORWARDED_PROTO']) && \$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {\n";
        $ret .= "    \$_SERVER['HTTPS'] = 'on';\n";
        $ret .= "}\n";

        if (VENDOR_PATH == '../vendor') {
            $kwfPath = '..';
        } else {
{'fraction_non_alphanumeric': 0.1750278706800446, 'fraction_numerical': 0.002849002849002849, 'mean_word_length': 2.238668271159246, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7175735', 'n_tokens_mistral': 2608, 'n_tokens_neox': 2517, 'n_words': 477}