<commit_before><?php

/**
 * CGI support.
 *
 * @author <NAME>
 *
 * @version $Id$
 *
 * @copyright Genee, 2014-02-07
 **/

/**
 * Define DocBlock.
 **/

namespace Gini;

class CGI
{
    public static function stripslashes(&$value)
    {
        return is_array($value) ?
                array_map([__CLASS__, __FUNCTION__], $value) :
                stripslashes($value);
    }

    public static function main($argv)
    {
        //从末端开始尝试
        /*
            home/page/edit/1/2

            home/page/index.php Controller\Page\Index::edit(1)
            home/page/index.php Controller\Index::index('edit', 1)
            home/page.php        Controller\Page::edit(1)
            home/page.php        Controller\Page::index('edit', 1)
        */

        $response = static::request(static::route())->execute();
        if ($response) {
            $response->output();
        }
    }

    /**
     * Get default environment variables: $_GET, $_POST, $_FILES, $_SERVER, $_COOKIE, method, route
     *
     * @return void
     */
    public static function defaultEnv()
    {
        // use Gini-HTTP-Method to replace uncommon methods like DELETE/PUT to get through firewall
        if ($_SERVER['HTTP_X_GINI_HTTP_METHOD']) {
            $_SERVER['REQUEST_METHOD'] = $_SERVER['HTTP_X_GINI_HTTP_METHOD'];
        }
        return [
            'get' => $_GET, 'post' => $_POST,
            'files' => $_FILES, 'server' => $_SERVER, 'cookie' => $_COOKIE,
            'route' => static::$route, 'method' => $_SERVER['REQUEST_METHOD'],
        ];
    }

    public static function request($route, $env = null)
    {
        if (is_null($env)) {
            $env = static::defaultEnv();
        }

        $router = static::router();
        $controller = $router->dispatch($env['method'], $route);
        \Gini\Config::set('runtime.controller_class', get_class($controller));
        $controller->env = $env;
        return $controller;
    }

    public static function exception($e)
    {
        $message = $e->getMessage();
        if ($message) {
            $file = $e->getFile();
            foreach (\Gini\Core::$MODULE_INFO as $info) {
                if (0 == strncmp($file, $info->path, strlen($info->path))) {
                    $file = "[$info->id] ".\Gini\File::relativePath($file, $info->path);
                    break;
                }
            }
            $line = $e->getLine();
            error_log(sprintf('ERROR %s', $message));
            $trace = array_slice($e->getTrace(), 1, 5);
            foreach ($trace as $n => $t) {
                $file = $t['file'];
                foreach (\Gini\Core::$MODULE_INFO as $info) {
                    if (0 == strncmp($file, $info->path, strlen($info->path))) {
                        $file = "[$info->id] ".\Gini\File::relativePath($file, $info->path);
                        break;
                    }
                }
                error_log(sprintf(
                    '    %d) %s%s() in %s on line %d',
                    $n + 1,
                    $t['class'] ? $t['class'].'::' : '',
                    $t['function'],
                    $file,
                    $t['line']
                ));
            }
        }

        if (PHP_SAPI != 'cli') {
            while (@ob_end_clean()) {
                //清空之前的所有显示
            }
            header('HTTP/1.1 500 Internal Server Error');
        }
    }

    public static function functionArguments($action, $params, $form = [])
    {
        $args = [];
        $r = is_string($action) ?
            new \ReflectionFunction($action) :
            new \ReflectionMethod($action[0], $action[1]);
        $rps = $r->getParameters();
        if (count($params) > 0 && is_numeric(key($params))) {
            // 需要考虑默认值以及无参数传入后使用func_get_args获取变量的情况
            $max = max(count($params), count($rps));
            for ($idx = 0; $idx < $max; $idx ++) {
                $param = $params[$idx];
                $rp = $rps[$idx];
                $args[] = $param ?: (
                        $rp ? (
                            $rp->isDefaultValueAvailable() ? $rp->getDefaultValue() : null
                        ) : null
                    );
            }
        } elseif (count($rps) > 0) {
            // 如果是有字符串键值的, 尝试通过反射对应变量
            // 可以把form数据合并进去
            $params = array_merge((array)$params, (array)$form);
            // 修正变量名以配合驼峰式命名
            // user_id, user-id, userId
            $newParams = [];<commit_msg>CGI 参数合并覆盖问题修复<commit_after><?php

/**
 * CGI support.
 *
 * @author <NAME>
 *
 * @version $Id$
 *
 * @copyright Genee, 2014-02-07
 **/

/**
 * Define DocBlock.
 **/

namespace Gini;

class CGI
{
    public static function stripslashes(&$value)
    {
        return is_array($value) ?
                array_map([__CLASS__, __FUNCTION__], $value) :
                stripslashes($value);
    }

    public static function main($argv)
    {
        //从末端开始尝试
        /*
            home/page/edit/1/2

            home/page/index.php Controller\Page\Index::edit(1)
            home/page/index.php Controller\Index::index('edit', 1)
            home/page.php        Controller\Page::edit(1)
            home/page.php        Controller\Page::index('edit', 1)
        */

        $response = static::request(static::route())->execute();
        if ($response) {
            $response->output();
        }
    }

    /**
     * Get default environment variables: $_GET, $_POST, $_FILES, $_SERVER, $_COOKIE, method, route
     *
     * @return void
     */
    public static function defaultEnv()
    {
        // use Gini-HTTP-Method to replace uncommon methods like DELETE/PUT to get through firewall
        if ($_SERVER['HTTP_X_GINI_HTTP_METHOD']) {
            $_SERVER['REQUEST_METHOD'] = $_SERVER['HTTP_X_GINI_HTTP_METHOD'];
        }
        return [
            'get' => $_GET, 'post' => $_POST,
            'files' => $_FILES, 'server' => $_SERVER, 'cookie' => $_COOKIE,
            'route' => static::$route, 'method' => $_SERVER['REQUEST_METHOD'],
        ];
    }

    public static function request($route, $env = null)
    {
        if (is_null($env)) {
            $env = static::defaultEnv();
        }

        $router = static::router();
        $controller = $router->dispatch($env['method'], $route);
        \Gini\Config::set('runtime.controller_class', get_class($controller));
        $controller->env = $env;
        return $controller;
    }

    public static function exception($e)
    {
        $message = $e->getMessage();
        if ($message) {
            $file = $e->getFile();
            foreach (\Gini\Core::$MODULE_INFO as $info) {
                if (0 == strncmp($file, $info->path, strlen($info->path))) {
                    $file = "[$info->id] ".\Gini\File::relativePath($file, $info->path);
                    break;
                }
            }
            $line = $e->getLine();
            error_log(sprintf('ERROR %s', $message));
            $trace = array_slice($e->getTrace(), 1, 5);
            foreach ($trace as $n => $t) {
                $file = $t['file'];
                foreach (\Gini\Core::$MODULE_INFO as $info) {
                    if (0 == strncmp($file, $info->path, strlen($info->path))) {
                        $file = "[$info->id] ".\Gini\File::relativePath($file, $info->path);
                        break;
                    }
                }
                error_log(sprintf(
                    '    %d) %s%s() in %s on line %d',
                    $n + 1,
                    $t['class'] ? $t['class'].'::' : '',
                    $t['function'],
                    $file,
                    $t['line']
                ));
            }
        }

        if (PHP_SAPI != 'cli') {
            while (@ob_end_clean()) {
                //清空之前的所有显示
            }
            header('HTTP/1.1 500 Internal Server Error');
        }
    }

    public static function functionArguments($action, $params, $form = [])
    {
        $args = [];
        $r = is_string($action) ?
            new \ReflectionFunction($action) :
            new \ReflectionMethod($action[0], $action[1]);
        $rps = $r->getParameters();
        if (count($params) > 0 && is_numeric(key($params))) {
            // 需要考虑默认值以及无参数传入后使用func_get_args获取变量的情况
            $max = max(count($params), count($rps));
            for ($idx = 0; $idx < $max; $idx ++) {
                $param = $params[$idx];
                $rp = $rps[$idx];
                $args[] = $param ?: (
                        $rp ? (
                            $rp->isDefaultValueAvailable() ? $rp->getDefaultValue() : null
                        ) : null
                    );
            }
        } elseif (count($rps) > 0) {
            // 如果是有字符串键值的, 尝试通过反射对应变量
            // 可以把form数据合并进去
            $params = array_merge((array)$form, (array)$params);
            // 修正变量名以配合驼峰式命名
            // user_id, user-id, userId
            $newParams = [];
{'fraction_non_alphanumeric': 0.16181878970244065, 'fraction_numerical': 0.006463835952301348, 'mean_word_length': 1.50390625, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 63, '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': '7513035', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2739, 'n_words': 503}