forked from loveyu/php-framework-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.php
204 lines (183 loc) · 4.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Core;
if(!defined('_CorePath_')){
exit;
}
/**
* 请求处理类
*/
class Request{
/**
* 请求类的模式标记
* @var int
*/
private $_mode;
private static $_htmlspecialchars_mode = ENT_QUOTES;
/**
* more为0时为明文模式
* @param int $mode
*/
function __construct($mode = 0){
$this->_mode = $mode;
}
public function set_htmlspecialchars_mode($mode = ENT_QUOTES){
switch($mode){
case ENT_COMPAT:
case ENT_QUOTES:
case ENT_NOQUOTES:
case ENT_IGNORE:
case ENT_SUBSTITUTE:
case ENT_DISALLOWED:
case ENT_HTML401:
case ENT_XML1:
case ENT_XHTML:
case ENT_HTML5:
self::$_htmlspecialchars_mode = $mode;
return;
}
self::$_htmlspecialchars_mode = ENT_QUOTES;
}
/**
* 转义模式,使用addslashes()效果
* @return Request
*/
public function _escape(){
return new Request(1);
}
/**
* 明文模式,将所有Html标签转义
* @return Request
*/
public function _plain(){
return new Request(2);
}
/**
* 对数据进行转换或明文处理
* @param mixed $data 输入默认数据null
* @return mixed 根据相应模式进行转换
*/
private function data_convert($data = ''){
if(NULL === $data){
return NULL;
}
if(MAGIC_QUOTES_GPC){
switch($this->_mode){
case 0:
$data = self::stripslashes_deep($data);
break;
case 2:
//先转换为非转义字符,然后进行HTML标签替换
$data = self::htmlspecialchars_deep(self::stripslashes_deep($data));
break;
}
} else{
switch($this->_mode){
case 1:
$data = self::addslashes_deep($data);
break;
case 2:
$data = self::htmlspecialchars_deep($data);
break;
}
}
return $data;
}
/**
* 对数组进行递归反转义操作
* @param string|array $value
* @return array|string
*/
public static function stripslashes_deep($value){
return is_array($value) ? array_map(array(
'Core\\Request',
'stripslashes_deep'
), $value) : stripslashes($value);
}
/**
* 对数组进行递归HTML标签转换为HTML符号
* @param string|array $value
* @return array|string
*/
public static function htmlspecialchars_deep($value){
return is_array($value) ? array_map(array(
'Core\\Request',
'htmlspecialchars_deep'
), $value) : htmlspecialchars($value, self::$_htmlspecialchars_mode);
}
/**
* 对数组进行递归转义
* @param string|array $value
* @return array|string
*/
public static function addslashes_deep($value){
return is_array($value) ? array_map(array(
'Core\\Request',
'addslashes_deep'
), $value) : addslashes($value);
}
/**
* 获取$_GET变量中的内容,参数为连续参数
* @return string|array|null
*/
public function get(){
return hook()->apply('Request_get', $this->data_convert(self::_get($_GET, func_get_args())));
}
/**
* 获取$_POST变量中的内容,参数为连续参数
* @return string|array|null
*/
public function post(){
return hook()->apply('Request_post', $this->data_convert(self::_get($_POST, func_get_args())));
}
/**
* 获取$_COOKIE变量中的内容,参数为连续参数
* @return string|array|null
*/
public function cookie(){
$p = func_get_args();
if(isset($p[0])){
$p[0] = COOKIE_PREFIX . $p[0];
}
return hook()->apply('Request_cookie', $this->data_convert(self::_get($_COOKIE, $p)));
}
/**
* 获取$_REQUEST变量中的内容,参数为连续参数
* @return string|array|null
*/
public function req(){
return hook()->apply('Request_req', $this->data_convert(self::_get($_REQUEST, func_get_args())));
}
/**
* 获取某一数组中的对应层次的元素
* @param array $data 原始数据
* @param array $list 键值列表
* @return mixed 对应的数据
*/
public static function _get($data, $list){
foreach($list as $v){
if(isset($data[$v])){
$data = $data[$v];
} else{
return NULL;
}
}
return $data;
}
/**
* 是否为GET请求方式
* @return bool
*/
public function is_get(){
return strtolower($_SERVER['REQUEST_METHOD']) === 'get';
}
/**
* 是否为POST请求方式
* @return bool
*/
public function is_post(){
return strtolower($_SERVER['REQUEST_METHOD']) === 'post';
}
function is_ajax(){
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
}