123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- <?php
- namespace KIF\Core;
- use KIF\Route;
- use KIF\String\String;
- use KIF\String\Filter;
- use Exception;
- use KIF\Exception\ParamsException;
- use KIF\Core\Config;
- use KIF\Verify;
- class Request {
-
- protected $params;
-
- static protected $instance;
-
-
- private function __construct() {
-
- }
-
- static public function getInstance() {
- if (is_null(self::$instance)) {
- self::$instance = new self();
- }
-
- self::$instance->params = self::params();
-
- return self::$instance;
- }
-
-
- public function ip() {
- if (isset($_SERVER)) {
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
- $realip = $_SERVER['HTTP_CLIENT_IP'];
- } else {
- $realip = $_SERVER['REMOTE_ADDR'];
- }
- } else {
- if (getenv("HTTP_X_FORWARDED_FOR")) {
- $realip = getenv("HTTP_X_FORWARDED_FOR");
- } elseif (getenv("HTTP_CLIENT_IP")) {
- $realip = getenv("HTTP_CLIENT_IP");
- } else {
- $realip = getenv("REMOTE_ADDR");
- }
- }
- $realip = preg_replace('#,.*$#', '', $realip);
- $realip = preg_replace('#[^\d\.]+#', '', $realip);
- return $realip;
- }
-
- public function path($request_uri = null) {
- if (is_null($request_uri)) {
- $request_uri = $_SERVER['REQUEST_URI'];
- }
- if (($iPos = strpos($request_uri, '?')) !== false) {
- $request_uri = substr($request_uri, 0, $iPos);
- }
- $request_uri = str_replace(' ', '/', trim(str_replace('/', ' ', $request_uri)));
- $request_uri = urldecode($request_uri);
- return $request_uri;
- }
-
- public function url() {
- $port = $_SERVER['SERVER_PORT'] == '80' ? '' : ":{$_SERVER['SERVER_PORT']}";
- $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
- return $url;
- }
-
-
- public function pageUrlTpl($pageKey = 'page') {
- $routeParams = Route::getInstance()->getsRouteParams();
- if ($routeParams) {
- $routeParams[$pageKey] = '{page}';
- $path = Route::getInstance()->reverse($routeParams);
- $url = "http://{$_SERVER['HTTP_HOST']}/" . $path;
- return String::jointUrl($url, array_diff_key($_GET, $routeParams));
- } else {
- $request_params = $_GET;
- $request_params[$pageKey] = '{page}';
- $url = "http://{$_SERVER['HTTP_HOST']}/" . $this->path();
- return String::jointUrl($url, $request_params);
- }
- }
-
-
- static public function schemeDomain() {
- $protocol_str = $_SERVER['SERVER_PROTOCOL'];
- if (!$protocol_str) {
- return '';
- }
- list($protocol,) = explode('/', $protocol_str);
- return strtolower($protocol) . "://{$_SERVER['HTTP_HOST']}";
- }
-
-
- public function rootDomain($host = null) {
- if (is_null($host))
- $host = $_SERVER['HTTP_HOST'];
-
- if (empty($host)) {
- return '';
- }
- $host = strtolower($host);
-
-
- if (filter_var($host, FILTER_VALIDATE_IP)) {
- return '';
- }
-
- if (strpos($host, '.') === FALSE) {
- return $host;
- }
-
- $domain_suffs = array(
- '.com', '.cn', '.net', '.com.cn', '.net.cn',
- '.org', '.me', '.biz', '.name', '.org.cn', '.gov.cn',
- '.info', '.so', '.tel', '.mobi', '.asia', '.cc', '.tv', '.co',
- );
-
- foreach ($domain_suffs as $suff) {
- $tmp_stuff_pos = strrpos($host, $suff);
- if ($tmp_stuff_pos === false) {
- continue;
- }
- $tmp_rootdomain_pos = strrpos($host, '.', 0-strlen(substr($host, $tmp_stuff_pos))-1);
- if ($tmp_rootdomain_pos === false) {
- return $host;
- }
-
- return substr($host, $tmp_rootdomain_pos+1);
- }
- }
-
- static public function queryString() {
- return $_SERVER['QUERY_STRING'];
- }
-
- static public function referer() {
- return $_SERVER['HTTP_REFERER'];
- }
-
- static public function isPost() {
- return strtoupper($_SERVER['REQUEST_METHOD']) == 'POST';
- }
-
- static public function isGet() {
- return strtoupper($_SERVER['REQUEST_METHOD']) == 'GET';
- }
-
- public function isSpider() {
- if (self::isCLI()) {
- return false;
- }
- if (!isset($_SERVER['HTTP_USER_AGENT'])) {
- return false;
- }
- $bots = array(
- 'Googebot', 'Baiduspider', 'Yahoo! Slurp', 'Sosospider', 'Sogou', 'Sogou-Test-Spider',
- 'Sogou head spider', 'YoudaoBot', 'qihoobot', 'iaskspider', 'LeapTag', 'MSIECrawler'
- );
- foreach ($bots as $bot) {
- if (strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) {
- return true;
- }
- }
- if (stristr($_SERVER['HTTP_USER_AGENT'], 'Windows') !== false) {
- return false;
- }
- $bots = array(
- 'spider', 'bit', 'crawler', 'slurp', 'subscriber', 'http',
- 'rssreader', 'blogline', 'greatnews', 'feed', 'alexa', 'php'
- );
- foreach ($bots as $bot) {
- if (stristr($_SERVER['HTTP_USER_AGENT'], $bot) !== false) {
- return true;
- }
- }
- return false;
- }
-
- public function isFromSameHost() {
- if (!isset($_SERVER['HTTP_REFERER'])) {
- return true;
- }
- $url_parts = parse_url($_SERVER['HTTP_REFERER']);
- $host = $url_parts['host'];
- if ($_SERVER['HTTP_HOST'] == $host) {
- return true;
- }
- return false;
- }
-
- public function varGetInt($varName, $default = 0) {
- if (!isset($_GET[$varName])) {
- return $default;
- }
- if (!Verify::unsignedInt($_GET[$varName])) {
- return $default;
- }
- return $_GET[$varName];
- }
-
- public function varPostInt($varName, $default = 0) {
- if (!isset($_POST[$varName])) {
- return $default;
- }
- if (!Verify::unsignedInt($_POST[$varName])) {
- return $default;
- }
- return $_POST[$varName];
- }
-
- public function varRequestInt($varName, $default = 0) {
- if (!isset($_REQUEST[$varName])) {
- return $default;
- }
- if (!Verify::unsignedInt($_REQUEST[$varName])) {
- return $default;
- }
- return $_REQUEST[$varName];
- }
-
- public function params() {
- if (self::isCLI()) {
- return self::cliParams();
- } else {
- return $_REQUEST;
- }
- }
-
-
- private function cliParams() {
- $result = array();
- $params = $GLOBALS['argv'];
-
- array_shift($params);
- reset($params);
- do {
- $tmpEachResult = each($params);
- if (!$tmpEachResult) {
- break;
- }
- list($tmp, $p) = $tmpEachResult;
-
- if ($p{0} == '-') {
- $pname = substr($p, 1);
- $value = false;
- if ($pname{0} == '-') {
- $pname = substr($pname, 1);
- if (strpos($p, '=') !== false) {
-
- list($pname, $value) = explode('=', substr($p, 2), 2);
- }
- } else {
- if (strpos($p, '=') !== false) {
-
- list($pname, $value) = explode('=', substr($p, 1), 2);
- } else if (strlen($p) > 1) {
- $pname = substr($p, 1, 1);
- $value = substr($p, 2);
- }
- }
-
- $nextparm = current($params);
- if ($value === false
- && $nextparm !== false
- && $nextparm{0} != '-'
- ) {
- list($tmp, $value) = each($params);
- }
- $result[$pname] = (string) $value;
- } else {
-
-
- }
- } while (true);
-
- return $result;
- }
-
- public function get($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- if (!isset($_GET[$name])) {
- return false;
- }
- $val = $_GET[$name];
- if (is_null($filters)) {
- return $val;
- }
- if (!is_array($filters)) {
- $filters = array($filters);
- }
- return self::filter($val, $filters);
- }
-
- public function g($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- return self::get($name, $filters);
- }
-
- public function post($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- if (!isset($_POST[$name])) {
- return false;
- }
- $val = $_POST[$name];
- if (is_null($filters)) {
- return $val;
- }
- if (!is_array($filters)) {
- $filters = array($filters);
- }
- return self::filter($val, $filters);
- }
-
- public function p($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- return self::post($name, $filters);
- }
-
- public function requestV($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- if (!isset($_REQUEST[$name])) {
- return false;
- }
- $val = $_REQUEST[$name];
- if (is_null($filters)) {
- return $val;
- }
- if (!is_array($filters)) {
- $filters = array($filters);
- }
- return self::filter($val, $filters);
- }
-
- public function r($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- return self::requestV($name, $filters);
- }
-
- public function cookie($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- if (!isset($_COOKIE[$name])) {
- return false;
- }
- $val = $_COOKIE[$name];
- if (is_null($filters)) {
- return $val;
- }
- if (!is_array($filters)) {
- $filters = array($filters);
- }
- return self::filter($val, $filters);
- }
-
- public function c($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- return self::cookie($name, $filters);
- }
-
-
- public function param($name, $filters = array(Filter::HTMLSPECIALCHARS, Filter::TRIM)) {
- if (!isset($this->params[$name])) {
- return false;
- }
- $val = $this->params[$name];
- if (is_null($filters)) {
- return $val;
- }
- if (!is_array($filters)) {
- $filters = array($filters);
- }
- return self::filter($val, $filters);
- }
-
- private function filter($val, array $filters) {
- foreach ($filters as $filter) {
- $params = array($val);
- if (is_array($filter)) {
- list($filter_name, $filter_options) = each($filter);
- } else {
- $filter_name = $filter;
- $filter_options = null;
- }
- if (!Filter::isValid($filter_name)) {
- throw new ParamsException("无效的过滤方法filter");
- }
- $val = call_user_func(array('KIF\String\Filter', $filter_name), $val, $filter_options);
- }
- return $val;
- }
-
- static public function isCLI() {
- if (php_sapi_name() == "cli"
- || empty($_SERVER['PHP_SELF'])
- ) {
- return true;
- } else {
- return false;
- }
- }
-
- static public function isUnixLike() {
- $os = strtolower(PHP_OS);
- if (in_array($os, array('linux', 'freebsd', 'unix', 'netbsd'))) {
- return true;
- }
- return false;
- }
-
-
- public function dispatch() {
- $params = $this->params;
- $controllerName = self::getController($params);
-
- if (!class_exists($controllerName)) {
- throw new Exception("controller: {$controllerName} not exists!");
- }
-
- $action = self::getAction($params);
-
- $controller = new $controllerName();
- $controller->setAction($action);
- $controller->run();
-
-
- if (method_exists($controller, 'display')) {
- $controller->display();
- }
- }
-
- public function getController() {
- $c = self::param('c');
- $c = trim($c, '_');
-
- $arr_class_path = array_map(function ($tmpV) {
- return ucfirst($tmpV);
- }, explode('_', $c));
- $c = join('\\', $arr_class_path);
- $NS = Config::getInstance()->get('Namespace');
- $controller = "{$NS}\Controller\\{$c}";
- return $controller;
- }
-
- public function getAction(array $params = null) {
- $action = self::param('a');
-
- return $action;
- }
- }
|