-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
167 lines (135 loc) · 3 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* {header_doc}
*/
class Ryus_Request
{
public function __construct()
{
}
public function isPost()
{
return ( $_SERVER['REQUEST_METHOD'] === 'POST' );
}
public function isGet()
{
return ( $_SERVER['REQUEST_METHOD'] === 'GET' );
}
public function isSSL()
{
return ( isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] === 'on' );
}
/**
*
* @notice The X-Requested-With is sent by the Ajax functions of most major Frameworks but not all.
* @see http://stackoverflow.com/questions/2579254/php-does-serverhttp-x-requested-with-exist-or-not
*/
public function isXHR()
{
return ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) === true and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest' );
}
public function isCLI()
{
return ( PHP_SAPI === 'cli' );
}
public function get($name, $default = null)
{
if ( isset($_GET[$name]) )
{
return $_GET[$name];
}
return $default;
}
public function post($name, $default = null)
{
if ( isset($_POST[$name]) )
{
return $_POST[$name];
}
return $default;
}
public function cookie($name, $default = null)
{
if ( isset($_COOKIE[$name]) )
{
return $_COOKIE[$name];
}
return $default;
}
public function getScheme()
{
if ( $this->isSSL() === true )
{
return 'https';
}
return 'http';
}
public function getUrl()
{
return $this->getScheme().'://'.$this->getHost().$this->getRequestUri();
}
public function getRequestUri()
{
return $_SERVER['REQUEST_URI'];
}
public function getScriptName()
{
return $_SERVER['SCRIPT_NAME'];
}
public function getRemoteAddr()
{
if ( empty($_SERVER['HTTP_X_FORWARDED_FOR']) === false )
{
return $_SERVER['HTTP_X_FORWARDED_FOR']; // for reverse proxy
}
return $_SERVER['REMOTE_ADDR'];
}
public function getHost()
{
if ( empty($_SERVER['HTTP_X_FORWARDED_HOST']) === false )
{
return $_SERVER['HTTP_X_FORWARDED_HOST']; // for reverse proxy
}
return $_SERVER['HTTP_HOST'];
}
public function getServerName()
{
if ( empty($_SERVER['HTTP_X_FORWARDED_SERVER']) === false )
{
return $_SERVER['HTTP_X_FORWARDED_SERVER']; // for reverse proxy
}
return $_SERVER['SERVER_NAME'];
}
public function getSiteUrl()
{
return $this->getScheme().'://'.$this->getHost().$this->getBaseUrl();
}
public function getBaseUrl()
{
$scriptName = $this->getScriptName();
$requestUri = $this->getRequestUri();
if ( strpos($requestUri, $scriptName) === 0 )
{
return $scriptName;
}
elseif ( strpos($requestUri, dirname($scriptName)) === 0 )
{
return rtrim(dirname($scriptName), '/');
}
return '';
}
public function getPathInfo()
{
$baseUrl = $this->getBaseUrl();
$requestUri = $this->getRequestUri();
$queryStringPosition = strpos($requestUri, '?');
if ( $queryStringPosition !== false )
{
$requestUri = substr($requestUri, 0, $queryStringPosition);
}
$baseUrlLength = strlen($baseUrl);
$pathInfo = substr($requestUri, $baseUrlLength);
$pathInfo = strval($pathInfo);
return $pathInfo;
}
}