<?php

/**
 * Description of newPHPClass
 *
 * @author stanley-king
 */
class request_helper
{
    static public function fcgi_role()
    {
        return fcgi_getparam('FCGI_ROLE');
    }

    static public function script_file()
    {
        return fcgi_getparam('SCRIPT_FILENAME');
    }

    static public function query_string()
    {
        return fcgi_getparam('QUERY_STRING');
    }

    static public function method()
    {
        return fcgi_getparam('REQUEST_METHOD');
    }

    static public function content_type()
    {
        return fcgi_getparam('REQUEST_METHOD');
    }

    static public function content_length()
    {
        return fcgi_getparam('CONTENT_LENGTH');
    }

    static public function content()
    {
        return fcgi_getcontent();
    }

    static public function script_name()
    {
        return fcgi_getparam('SCRIPT_NAME');
    }

    static public function req_uri()
    {
        return fcgi_getparam('REQUEST_URI');
    }

    static public function doc_uri()
    {
        return fcgi_getparam('DOCUMENT_URI');
    }

    static public function doc_root()
    {
        return fcgi_getparam('DOCUMENT_ROOT');
    }

    static public function srv_protocol()
    {
        return fcgi_getparam('SERVER_PROTOCOL');
    }

    static public function gate_interface()
    {
        return fcgi_getparam('GATEWAY_INTERFACE');
    }

    static public function srv_software()
    {
        return fcgi_getparam('SERVER_SOFTWARE');
    }

    static public function remote_addr()
    {
        return fcgi_getparam('REMOTE_ADDR');
    }

    static public function remote_port()
    {
        return fcgi_getparam('REMOTE_PORT');
    }

    static public function srv_addr()
    {
        return fcgi_getparam('SERVER_ADDR');
    }

    static public function srv_port()
    {
        return fcgi_getparam('SERVER_PORT');
    }

    static public function srv_name()
    {
        return fcgi_getparam('SERVER_NAME');
    }

    static public function redirect_status()
    {
        return fcgi_getparam('REDIRECT_STATUS');
    }

    static public function http_host()
    {
        return fcgi_getparam('HTTP_HOST');
    }

    static public function http_connection()
    {
        return fcgi_getparam('HTTP_CONNECTION');
    }

    static public function http_accept()
    {
        return fcgi_getparam('HTTP_ACCEPT');
    }

    static public function http_upgrade_insecure_requests()
    {
        return fcgi_getparam('HTTP_UPGRADE_INSECURE_REQUESTS');
    }

    static public function http_useragent()
    {
        return fcgi_getparam('HTTP_USER_AGENT');
    }

    static public function http_accept_encoding()
    {
        return fcgi_getparam('HTTP_ACCEPT_ENCODING');
    }

    static public function http_accept_language()
    {
        return fcgi_getparam('HTTP_ACCEPT_LANGUAGE');
    }

    static public function http_cookie()
    {
        return fcgi_getparam('HTTP_COOKIE');
    }

    static public function SRV_PARAMS()
    {
        //            'PHP_SELF','argv','argc','REQUEST_TIME','REQUEST_TIME_FLOAT','SCRIPT_NAME','PATH_TRANSLATED',
        //            'SCRIPT_NAME', 'SCRIPT_FILENAME', 'FCGI_ROLE',
        //            'GATEWAY_INTERFACE',
        //            'SERVER_ADDR',
        //            'SERVER_NAME',
        //            'SERVER_SOFTWARE',
        //            'SERVER_PROTOCOL',
        //            'REQUEST_METHOD',
        //            'QUERY_STRING',
        //            'DOCUMENT_URI',
        //            'DOCUMENT_ROOT',
        static $fields = array(
            "HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET",
            "HTTP_ACCEPT_ENCODING",
            "HTTP_ACCEPT_LANGUAGE", "HTTP_AUTHORIZATION",
            "HTTP_CONNECTION", "CONTENT_TYPE",
            "CONTENT_LENGTH", "HTTP_COOKIE", "HTTP_COOKIE2",
            "HTTP_HOST", "HTTP_PRAGMA",
            "HTTP_REFERER", "HTTP_USER_AGENT",
            "HTTP_CACHE_CONTROL",
            "HTTP_IF_MODIFIED_SINCE", "HTTP_IF_MATCH",
            "HTTP_IF_NONE_MATCH",
            "HTTP_IF_RANGE",
            "HTTP_IF_UNMODIFIED_SINCE",
            "HTTP_KEEP_ALIVE",
            "HTTP_RANGE",
            "HTTP_X_FORWARDED_FOR",
            "HTTP_VIA",
            'HTTPS',

            'REMOTE_ADDR',
            'REMOTE_HOST',
            'REMOTE_PORT',
            'REMOTE_USER',
            'REDIRECT_REMOTE_USER',

            'SERVER_ADMIN',
            'SERVER_PORT',
            'SERVER_SIGNATURE',

            'REQUEST_URI',
            'PHP_AUTH_DIGEST',
            'PHP_AUTH_USER',
            'PHP_AUTH_PW',
            'AUTH_TYPE',
            'PATH_INFO',
            'HTTP_CLIENT_TYPE',
            'HTTP_CLIENT_VERSION',
            'ORIG_PATH_INFO');

        return $fields;
    }

    static public function fill_server()
    {
        $tm = gettimeofday();
        $_SERVER[REQUEST_TIME_FLOAT] = doubleval($tm['sec']) + doubleval($tm['usec']) / 1000000;
        $_SERVER[REQUEST_TIME] = intval($tm['sec']);

        $fields = self::SRV_PARAMS();
        foreach ($fields as $field) {
            $param = fcgi_getparam($field);
            $_SERVER[$field] = $param;
        }
    }

    static public function clear_server()
    {
        $fields = self::SRV_PARAMS();
        foreach ($fields as $field) {
            unset($_SERVER[$field]);
        }
    }

    static public function fill_param()
    {
        $method = strtolower(request_helper::method());
        if ($method == 'get') {
            $squery = request_helper::query_string();
        } else {
            $squery = request_helper::content();
        }

        if ($squery == '') {
            return;
        } else {
            $_SERVER['original_querystring'] = $squery;
        }

        Log::record("content={$squery}",Log::DEBUG);

        $params = preg_split('/&|=/', $squery);
        if ($method == 'get' || $method == 'post')
        {
            for ($i = 0; $i < count($params); ++$i) {
                $key = $params[$i];
                $val = $params[++$i];
                $_GET[$key] =  urldecode($val);
                $_POST[$key] = urldecode($val);
            }
        }
    }
    /**
     * 302  跳转
     */
    static public function url_jump(){
        Log::record("server:" . json_encode($_SERVER));
        if ($_SERVER['HTTP_CLIENT_TYPE'] == 'Android' && $_SERVER['HTTP_CLIENT_VERSION'] == '3') {
             http_header::instance()->header('location:http://a.lrlz.com:8000'.request_helper::req_uri(),false,302);
        }
   }
}

function init_request()
{
    request_helper::fill_server();
    request_helper::fill_param();
    //request_helper::url_jump();
}

class local_request
{
    private $script_file;
    private $query_string;
    private $method;

    public function __construct($uri)
    {
        $param = parse_url($uri);

        $this->script_file = BASE_ROOT_PATH . $param['path'];
        $this->query_string = $param['query'];
        $this->method = 'GET';
    }

    public function script_file()
    {
        return $this->script_file;
    }

    public function query_string()
    {
        return $this->query_string;
    }

    public function  method()
    {
        return $this->method;
    }
}


function init_localreq(local_request $req)
{
    $squery = $req->query_string();
    $params = preg_split('/&|=/', $squery);
    $method = strtolower($req->method());

    if ($method == 'get' || $method == 'post') {
        for ($i = 0; $i < count($params); ++$i) {
            $key = $params[$i];
            $val = $params[++$i];
            $_GET[$key] = $val;
            $_POST[$key] = $val;
        }
    }
}

