CrossPHP
  • Namespace
  • Class
  • Download

Namespaces

  • Cross
    • Auth
    • Cache
      • Driver
      • Request
    • Core
    • DB
      • Connecter
      • Drivers
      • SQLAssembler
    • Exception
    • Http
    • I
    • Lib
      • Document
    • Module
    • MVC
    • Runtime
  • None

Classes

  • Cross\Auth\CookieAuth
  • Cross\Auth\SessionAuth
  • Cross\Cache\Driver\FileCacheDriver
  • Cross\Cache\Driver\MemcacheDriver
  • Cross\Cache\Driver\RedisDriver
  • Cross\Cache\Request\FileCache
  • Cross\Cache\Request\Memcache
  • Cross\Cache\Request\RedisCache
  • Cross\Cache\RequestCache
  • Cross\Core\Annotate
  • Cross\Core\Application
  • Cross\Core\ArrayMap
  • Cross\Core\Config
  • Cross\Core\CrossArray
  • Cross\Core\Delegate
  • Cross\Core\FrameBase
  • Cross\Core\Helper
  • Cross\Core\HttpAuth
  • Cross\Core\Loader
  • Cross\Core\Rest
  • Cross\Core\Router
  • Cross\DB\Connecter\BaseConnecter
  • Cross\DB\Connecter\MySQLConnecter
  • Cross\DB\Connecter\PgSQLConnecter
  • Cross\DB\Connecter\SQLiteConnecter
  • Cross\DB\DBFactory
  • Cross\DB\Drivers\CouchDriver
  • Cross\DB\Drivers\MongoDriver
  • Cross\DB\Drivers\PDOSqlDriver
  • Cross\DB\SQLAssembler\MySQLAssembler
  • Cross\DB\SQLAssembler\PgSQLAssembler
  • Cross\DB\SQLAssembler\SQLAssembler
  • Cross\DB\SQLAssembler\SQLiteAssembler
  • Cross\Http\Request
  • Cross\Http\Response
  • Cross\Lib\Array2XML
  • Cross\Lib\Document\CallTree
  • Cross\Lib\Document\CallTreeToHTML
  • Cross\Lib\Document\HTML
  • Cross\Lib\StringToPHPStream
  • Cross\Module\SQLModule
  • Cross\MVC\Controller
  • Cross\MVC\Module
  • Cross\MVC\View
  • Cross\Runtime\ClosureContainer

Interfaces

  • Cross\I\CacheInterface
  • Cross\I\HttpAuthInterface
  • Cross\I\PDOConnecter
  • Cross\I\RequestCacheInterface
  • Cross\I\RouterInterface
  • Cross\I\SqlInterface

Exceptions

  • Cross\Exception\CacheException
  • Cross\Exception\CoreException
  • Cross\Exception\CrossException
  • Cross\Exception\FrontException

Functions

  • ascLogo
  • line
  • tBody
  • th
  • tHead
  1 <?php
  2 /**
  3  * Cross - a micro PHP 5 framework
  4  *
  5  * @link        http://www.crossphp.com
  6  * @license     MIT License
  7  */
  8 
  9 namespace Cross\Http;
 10 
 11 use Cross\Exception\FrontException;
 12 
 13 /**
 14  * @author wonli <wonli@live.com>
 15  * Class Request
 16  * @package Cross\Core
 17  */
 18 class Request
 19 {
 20     private $baseUrl;
 21     private $hostInfo;
 22     private $scriptUrl;
 23     private static $instance;
 24 
 25     /**
 26      * 实例化类
 27      *
 28      * @return Request
 29      */
 30     public static function getInstance()
 31     {
 32         if (!self::$instance) {
 33             self::$instance = new self();
 34         }
 35 
 36         return self::$instance;
 37     }
 38 
 39     /**
 40      * script url
 41      *
 42      * @return mixed
 43      * @throws FrontException
 44      */
 45     function getScriptUrl()
 46     {
 47         if (!$this->scriptUrl) {
 48             $this->initScriptUrl();
 49         }
 50 
 51         return $this->scriptUrl;
 52     }
 53 
 54     /**
 55      * 设置基础路径
 56      *
 57      * @param  string $url 设置基础路径
 58      */
 59     function setBaseUrl($url)
 60     {
 61         $this->baseUrl = $url;
 62     }
 63 
 64     /**
 65      * 返回当前URL绝对路径
 66      *
 67      * @param  boolean $absolute 是否返回带HOST的绝对路径
 68      * @return string 当前请求的url
 69      * @throws FrontException
 70      */
 71     function getBaseUrl($absolute = false)
 72     {
 73         if ($this->baseUrl === null) {
 74             $this->baseUrl = rtrim(dirname($this->getScriptUrl()), '\\/.');
 75         }
 76         return $absolute ? $this->getHostInfo() . $this->baseUrl : $this->baseUrl;
 77     }
 78 
 79     /**
 80      * 当前执行的脚本名
 81      *
 82      * @return string
 83      */
 84     function getIndexName()
 85     {
 86         return basename($this->getScriptName());
 87     }
 88 
 89     /**
 90      * get host
 91      *
 92      * @return mixed
 93      */
 94     function getHostInfo()
 95     {
 96         if (!$this->hostInfo) {
 97             $this->initHostInfo();
 98         }
 99         return $this->hostInfo;
100     }
101 
102     /**
103      * 获取当前页面URL
104      *
105      * @param bool $absolute
106      * @return string
107      */
108     function getCurrentUrl($absolute = true)
109     {
110         if ($absolute) {
111             return $this->getHostInfo() . $this->SERVER('REQUEST_URI');
112         }
113 
114         return $this->SERVER('REQUEST_URI');
115     }
116 
117     /**
118      * 取得服务器端口
119      *
120      * @return int 当前服务器端口号
121      */
122     function getServerPort()
123     {
124         return $this->SERVER('SERVER_PORT');
125     }
126 
127     /**
128      * 当前scriptFile的路径
129      *
130      * @throws FrontException
131      * @return string
132      */
133     function getScriptFilePath()
134     {
135         if (($scriptName = $this->SERVER('SCRIPT_FILENAME')) == null) {
136             throw new FrontException('determine the entry script URL failed!!!');
137         }
138 
139         return dirname($scriptName);
140     }
141 
142     /**
143      * @return string
144      */
145     function getUserHost()
146     {
147         return $this->SERVER('REMOTE_HOST');
148     }
149 
150     /**
151      * @return string
152      */
153     function getRequestURI()
154     {
155         return $this->SERVER('REQUEST_URI');
156     }
157 
158     /**
159      * @return string
160      */
161     function getRequestType()
162     {
163         return $this->SERVER('REQUEST_METHOD');
164     }
165 
166     /**
167      * @return string
168      */
169     function getPathInfo()
170     {
171         return $this->SERVER('PATH_INFO');
172     }
173 
174     /**
175      * @return string
176      */
177     function getQueryString()
178     {
179         return $this->SERVER('QUERY_STRING');
180     }
181 
182     /**
183      * @return string
184      */
185     function getScriptName()
186     {
187         return $this->SERVER('SCRIPT_NAME');
188     }
189 
190     /**
191      * HTTP_REFERER;
192      *
193      * @return string
194      */
195     function getUrlReferrer()
196     {
197         return $this->SERVER('HTTP_REFERER');
198     }
199 
200     /**
201      * @return string userAgent
202      */
203     function getUserAgent()
204     {
205         return $this->SERVER('HTTP_USER_AGENT');
206     }
207 
208     /**
209      * @return string ACCEPT TYPE
210      */
211     function getAcceptTypes()
212     {
213         return $this->SERVER('HTTP_ACCEPT');
214     }
215 
216     /**
217      * 是否是PUT请求
218      *
219      * @return bool
220      */
221     function isPutRequest()
222     {
223         return ($this->SERVER('REQUEST_METHOD')
224                 && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'PUT')) || $this->isPutViaPostRequest();
225     }
226 
227     /**
228      * 判断一个链接是否为post请求
229      *
230      * @return boolean
231      */
232     function isPostRequest()
233     {
234         return $this->SERVER('REQUEST_METHOD') && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'POST');
235     }
236 
237     /**
238      * 判断请求类型是否为get
239      *
240      * @return bool
241      */
242     function isGetRequest()
243     {
244         return $this->SERVER('REQUEST_METHOD') && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'GET');
245     }
246 
247     /**
248      * 判断请求类型是否为delete
249      *
250      * @return bool
251      */
252     function isDeleteRequest()
253     {
254         return $this->SERVER('REQUEST_METHOD') && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'DELETE');
255     }
256 
257     /**
258      * 是否是ajax请求
259      *
260      * @return bool
261      */
262     function isAjaxRequest()
263     {
264         return 0 === strcasecmp($this->SERVER('HTTP_X_REQUESTED_WITH'), 'XMLHttpRequest');
265     }
266 
267     /**
268      * 是否是flash请求
269      *
270      * @return bool
271      */
272     function isFlashRequest()
273     {
274         return stripos($this->SERVER('HTTP_USER_AGENT'), 'Shockwave') !== false
275             || stripos($this->SERVER('HTTP_USER_AGENT'), 'Flash') !== false;
276     }
277 
278     /**
279      * 获取客户端IP地址
280      *
281      * @param array $env_keys
282      * @return string userIP
283      */
284     function getClientIPAddress(array $env_keys = array())
285     {
286         static $ip = null;
287         if (null === $ip) {
288             if (empty($env_keys)) {
289                 $env_keys = array(
290                     'HTTP_CLIENT_IP',
291                     'HTTP_CF_CONNECTING_IP',
292                     'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
293                     'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
294                     'REMOTE_ADDR'
295                 );
296             }
297 
298             $ip = '0.0.0.0';
299             foreach ($env_keys as $env) {
300                 $env_info = $this->SERVER($env);
301                 if (!empty($env_info) && 0 !== strcasecmp($env_info, 'unknown')) {
302                     $ips = explode(',', $env_info);
303                     foreach ($ips as $ip) {
304                         $ip = trim($ip);
305                         if (false !== ip2long($ip)) {
306                             break 2;
307                         }
308                     }
309                 }
310             }
311         }
312 
313         return $ip;
314     }
315 
316     /**
317      * 取得$_SERVER全局变量的值
318      *
319      * @param string $name $_SERVER的名称
320      * @return string
321      */
322     function SERVER($name)
323     {
324         return isset($_SERVER[$name]) ? $_SERVER[$name] : '';
325     }
326 
327     /**
328      * 是否是通过POST的PUT请求
329      *
330      * @return bool
331      */
332     protected function isPutViaPostRequest()
333     {
334         return isset($_POST['_method']) && !strcasecmp($_POST['_method'], 'PUT');
335     }
336 
337     /**
338      * 初始化URL
339      *
340      * @throws FrontException
341      */
342     private function initScriptUrl()
343     {
344         if (($scriptName = $this->SERVER('SCRIPT_FILENAME')) == null) {
345             throw new FrontException('determine the entry script URL failed!!!');
346         }
347         $scriptName = basename($scriptName);
348         if (($_scriptName = $this->SERVER('SCRIPT_NAME')) != null && basename($_scriptName) === $scriptName) {
349             $this->scriptUrl = $_scriptName;
350         } elseif (($_scriptName = $this->SERVER('PHP_SELF')) != null && basename($_scriptName) === $scriptName) {
351             $this->scriptUrl = $_scriptName;
352         } elseif (($_scriptName = $this->SERVER('ORIG_SCRIPT_NAME')) != null && basename(
353                 $_scriptName
354             ) === $scriptName
355         ) {
356             $this->scriptUrl = $_scriptName;
357         } elseif (($pos = strpos($this->SERVER('PHP_SELF'), '/' . $scriptName)) !== false) {
358             $this->scriptUrl = substr($this->SERVER('SCRIPT_NAME'), 0, $pos) . '/' . $scriptName;
359         } elseif (($_documentRoot = $this->SERVER('DOCUMENT_ROOT')) != null && ($_scriptName = $this->SERVER(
360                 'SCRIPT_FILENAME'
361             )) != null && strpos($_scriptName, $_documentRoot) === 0
362         ) {
363             $this->scriptUrl = str_replace('\\', '/', str_replace($_documentRoot, '', $_scriptName));
364         } else {
365             throw new FrontException('determine the entry script URL failed!!');
366         }
367     }
368 
369     /**
370      * 设置Host信息
371      *
372      * @return null|string
373      */
374     private function initHostInfo()
375     {
376         if (PHP_SAPI === 'cli') {
377             return '';
378         }
379 
380         $protocol = 'http';
381         if (strcasecmp($this->SERVER('HTTPS'), 'on') === 0) {
382             $protocol = 'https';
383         }
384 
385         if (($host = $this->SERVER('HTTP_HOST')) != null) {
386             $this->hostInfo = $protocol . '://' . $host;
387         } elseif (($host = $this->SERVER('SERVER_NAME')) != null) {
388             $this->hostInfo = $protocol . '://' . $host;
389             $port = $this->getServerPort();
390             if (($protocol == 'http' && $port != 80) || ($protocol == 'https' && $port != 443)) {
391                 $this->hostInfo .= ':' . $port;
392             }
393         }
394 
395         return '';
396     }
397 }
398 
399 
CrossPHP API documentation generated by ApiGen