1
|
|
|
<?php |
2
|
|
|
use puck\Container; |
3
|
|
|
use puck\tools\Str; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* 获取客户端IP地址 |
7
|
|
|
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 |
8
|
|
|
* @param boolean $adv 是否进行高级模式获取(有可能被伪装) |
9
|
|
|
* @return mixed |
10
|
|
|
*/ |
11
|
|
|
function get_client_ip($type=0, $adv=true) |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
$type=$type ? 1 : 0; |
14
|
|
|
static $ip=null; |
15
|
|
|
if (null !== $ip) { |
16
|
|
|
return $ip[$type]; |
17
|
|
|
} |
18
|
|
|
if ($adv) { |
19
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
20
|
|
|
$arr=explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); |
21
|
|
|
$pos=array_search('unknown', $arr); |
22
|
|
|
if (false !== $pos) { |
23
|
|
|
unset($arr[$pos]); |
24
|
|
|
} |
25
|
|
|
$ip=trim($arr[0]); |
26
|
|
|
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { |
27
|
|
|
$ip=$_SERVER['HTTP_CLIENT_IP']; |
28
|
|
|
} elseif (isset($_SERVER['REMOTE_ADDR'])) { |
29
|
|
|
$ip=$_SERVER['REMOTE_ADDR']; |
30
|
|
|
} |
31
|
|
|
} elseif (isset($_SERVER['REMOTE_ADDR'])) { |
32
|
|
|
$ip=$_SERVER['REMOTE_ADDR']; |
33
|
|
|
} |
34
|
|
|
// IP地址合法验证 |
35
|
|
|
$long=sprintf("%u", ip2long($ip)); |
36
|
|
|
$ip=$long ? array($ip, $long) : array('0.0.0.0', 0); |
37
|
|
|
return $ip[$type]; |
38
|
|
|
} |
39
|
|
|
//2为直接输出数组 |
40
|
|
|
function show_json(array $arr, $type=2) |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
if (isset($arr['status']) && $type == 2) { |
44
|
|
|
$ret=$arr; |
45
|
|
|
} |
46
|
|
|
else { |
47
|
|
|
$ret['status']=$type; |
|
|
|
|
48
|
|
|
$ret['data']=$arr; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$obj=json_encode($ret); |
52
|
|
|
header('Content-Type: application/json'); |
53
|
|
|
echo $obj; |
54
|
|
|
exit(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function success($arr) |
58
|
|
|
{ |
59
|
|
|
show_json($arr, 1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
function error($arr) |
63
|
|
|
{ |
64
|
|
|
show_json($arr, 0); |
65
|
|
|
} |
66
|
|
|
function not_found($str='page not found,that is all we know!') { |
67
|
|
|
header('HTTP/1.1 404 Not Found'); |
68
|
|
|
header("status: 404 Not Found"); |
69
|
|
|
exit($str); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* 数据签名认证 |
75
|
|
|
* @param array $data 被认证的数据 |
76
|
|
|
* @return string 签名 |
77
|
|
|
* @author 麦当苗儿 <[email protected]> |
78
|
|
|
*/ |
79
|
|
|
function data_auth_sign($data) { |
80
|
|
|
//数据类型检测 |
81
|
|
|
|
82
|
|
|
if (!is_array($data)) { |
83
|
|
|
|
84
|
|
|
$data=(array) $data; |
85
|
|
|
} |
86
|
|
|
ksort($data); //排序 |
87
|
|
|
$code=http_build_query($data); //url编码并生成query字符串 |
88
|
|
|
$sign=sha1($code); //生成签名 |
89
|
|
|
return $sign; |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* session管理函数 |
93
|
|
|
* @param string $name session名称 如果为数组则表示进行session设置 |
94
|
|
|
* @param mixed $value session值 |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
function session($name='', $value='') { |
|
|
|
|
98
|
|
|
if (is_array($name)) { |
99
|
|
|
|
100
|
|
|
if (isset($name['id'])) { |
101
|
|
|
session_id($name['id']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($name['name'])) { |
105
|
|
|
session_name($name['name']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (isset($name['path'])) { |
109
|
|
|
session_save_path($name['path']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($name['domain'])) { |
113
|
|
|
ini_set('session.cookie_domain', $name['domain']); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (isset($name['expire'])) { |
117
|
|
|
ini_set('session.gc_maxlifetime', $name['expire']); |
118
|
|
|
ini_set('session.cookie_lifetime', $name['expire']); |
119
|
|
|
} |
120
|
|
|
if (isset($name['use_trans_sid'])) { |
121
|
|
|
ini_set('session.use_trans_sid', $name['use_trans_sid'] ? 1 : 0); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (isset($name['use_cookies'])) { |
125
|
|
|
ini_set('session.use_cookies', $name['use_cookies'] ? 1 : 0); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (isset($name['cache_limiter'])) { |
129
|
|
|
session_cache_limiter($name['cache_limiter']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (isset($name['cache_expire'])) { |
133
|
|
|
session_cache_expire($name['cache_expire']); |
134
|
|
|
} |
135
|
|
|
session_start(); |
136
|
|
|
} elseif ('' === $value) { |
137
|
|
|
if ('' === $name) { |
138
|
|
|
// 获取全部的session |
139
|
|
|
return $_SESSION; |
140
|
|
|
} elseif (0 === strpos($name, '[')) { |
141
|
|
|
// session 操作 |
142
|
|
|
if ('[pause]' == $name) {// 暂停session |
143
|
|
|
session_write_close(); |
144
|
|
|
} elseif ('[start]' == $name) { |
145
|
|
|
// 启动session |
146
|
|
|
session_start(); |
147
|
|
|
} elseif ('[destroy]' == $name) { |
148
|
|
|
// 销毁session |
149
|
|
|
$_SESSION=array(); |
150
|
|
|
session_unset(); |
151
|
|
|
session_destroy(); |
152
|
|
|
} elseif ('[regenerate]' == $name) { |
153
|
|
|
// 重新生成id |
154
|
|
|
session_regenerate_id(); |
155
|
|
|
} |
156
|
|
|
} else { |
157
|
|
|
if (strpos($name, '.')) { |
158
|
|
|
list($name1, $name2)=explode('.', $name); |
159
|
|
|
return isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null; |
160
|
|
|
} else { |
161
|
|
|
return isset($_SESSION[$name]) ? $_SESSION[$name] : null; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} elseif (is_null($value)) { |
165
|
|
|
// 删除session |
166
|
|
|
if (strpos($name, '.')) { |
167
|
|
|
list($name1, $name2)=explode('.', $name); |
168
|
|
|
unset($_SESSION[$name1][$name2]); |
169
|
|
|
} else { |
170
|
|
|
unset($_SESSION[$name]); |
171
|
|
|
} |
172
|
|
|
} else { |
173
|
|
|
// 设置session |
174
|
|
|
if (strpos($name, '.')) { |
175
|
|
|
list($name1, $name2)=explode('.', $name); |
176
|
|
|
$_SESSION[$name1][$name2]=$value; |
177
|
|
|
} else { |
178
|
|
|
$_SESSION[$name]=$value; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
function admin_is_login() { |
186
|
|
|
$user=session('admin_user_auth'); |
187
|
|
|
if (empty($user)) { |
188
|
|
|
return 0; |
189
|
|
|
} else { |
190
|
|
|
$auth_sign=session('admin_user_auth_sign'); |
191
|
|
|
if (data_auth_sign($user) != $auth_sign) { |
192
|
|
|
return 0; |
193
|
|
|
} |
194
|
|
|
return $user['uid']; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
function json($str) { |
198
|
|
|
$obj=json_encode($str, JSON_UNESCAPED_UNICODE); |
199
|
|
|
header('Content-Type: application/json'); |
200
|
|
|
echo $obj; |
201
|
|
|
} |
202
|
|
|
/** |
203
|
|
|
* 浏览器友好的变量输出 |
204
|
|
|
* @param mixed $var 变量 |
205
|
|
|
* @param boolean $echo 是否输出 默认为true 如果为false 则返回输出字符串 |
206
|
|
|
* @param string $label 标签 默认为空 |
207
|
|
|
* @param integer $flags htmlspecialchars flags |
208
|
|
|
* @return void|string |
209
|
|
|
*/ |
210
|
|
|
function dump($var, $echo=true, $label=null, $flags=ENT_SUBSTITUTE) |
211
|
|
|
{ |
212
|
|
|
$label=(null === $label) ? '' : rtrim($label).':'; |
213
|
|
|
ob_start(); |
214
|
|
|
var_dump($var); |
|
|
|
|
215
|
|
|
$output=ob_get_clean(); |
216
|
|
|
$output=preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output); |
217
|
|
|
if (IS_CLI) { |
218
|
|
|
$output=PHP_EOL.$label.$output.PHP_EOL; |
219
|
|
|
} else { |
220
|
|
|
if (!extension_loaded('xdebug')) { |
221
|
|
|
$output=htmlspecialchars($output, $flags); |
222
|
|
|
} |
223
|
|
|
$output='<pre>'.$label.$output.'</pre>'; |
224
|
|
|
} |
225
|
|
|
if ($echo) { |
226
|
|
|
echo($output); |
227
|
|
|
return; |
228
|
|
|
} else { |
229
|
|
|
return $output; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* 获取输入参数 支持过滤和默认值 |
236
|
|
|
* 使用方法: |
237
|
|
|
* <code> |
238
|
|
|
* I('id',0); 获取id参数 自动判断get或者post |
239
|
|
|
* I('post.name','','htmlspecialchars'); 获取$_POST['name'] |
240
|
|
|
* I('get.'); 获取$_GET |
241
|
|
|
* </code> |
242
|
|
|
* @param string $name 变量的名称 支持指定类型 |
243
|
|
|
* @param mixed $default 不存在的时候默认值 |
244
|
|
|
* @param mixed $filter 参数过滤方法 |
245
|
|
|
* @param mixed $datas 要获取的额外数据源 |
246
|
|
|
* @return mixed |
247
|
|
|
*/ |
248
|
|
|
function I($name, $default='', $filter=null, $datas=null) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
static $_PUT=null; |
251
|
|
|
if (strpos($name, '/')) { |
252
|
|
|
// 指定修饰符 |
253
|
|
|
list($name, $type)=explode('/', $name, 2); |
254
|
|
|
} else { |
255
|
|
|
// 默认强制转换为字符串 |
256
|
|
|
$type='s'; |
257
|
|
|
} |
258
|
|
|
if (strpos($name, '.')) { |
259
|
|
|
// 指定参数来源 |
260
|
|
|
list($method, $name)=explode('.', $name, 2); |
261
|
|
|
} else { |
262
|
|
|
// 默认为自动判断 |
263
|
|
|
$method='param'; |
264
|
|
|
} |
265
|
|
|
switch (strtolower($method)) { |
266
|
|
|
case 'get': |
267
|
|
|
$input=&$_GET; |
268
|
|
|
break; |
269
|
|
|
case 'post': |
270
|
|
|
$input=&$_POST; |
271
|
|
|
break; |
272
|
|
|
case 'put': |
273
|
|
|
if (is_null($_PUT)) { |
274
|
|
|
parse_str(file_get_contents('php://input'), $_PUT); |
275
|
|
|
} |
276
|
|
|
$input=$_PUT; |
277
|
|
|
break; |
278
|
|
|
case 'param': |
279
|
|
|
switch ($_SERVER['REQUEST_METHOD']) { |
280
|
|
|
case 'POST': |
281
|
|
|
$input=$_POST; |
282
|
|
|
break; |
283
|
|
|
case 'PUT': |
284
|
|
|
if (is_null($_PUT)) { |
285
|
|
|
parse_str(file_get_contents('php://input'), $_PUT); |
286
|
|
|
} |
287
|
|
|
$input=$_PUT; |
288
|
|
|
break; |
289
|
|
|
default: |
290
|
|
|
$input=$_GET; |
291
|
|
|
} |
292
|
|
|
break; |
293
|
|
|
case 'path': |
294
|
|
|
$input=array(); |
295
|
|
|
if (!empty($_SERVER['PATH_INFO'])) { |
296
|
|
|
$depr=C('URL_PATHINFO_DEPR'); |
297
|
|
|
$input=explode($depr, trim($_SERVER['PATH_INFO'], $depr)); |
298
|
|
|
} |
299
|
|
|
break; |
300
|
|
|
case 'request': |
301
|
|
|
$input=&$_REQUEST; |
302
|
|
|
break; |
303
|
|
|
case 'session': |
304
|
|
|
$input=&$_SESSION; |
305
|
|
|
break; |
306
|
|
|
case 'cookie': |
307
|
|
|
$input=&$_COOKIE; |
308
|
|
|
break; |
309
|
|
|
case 'server': |
310
|
|
|
$input=&$_SERVER; |
311
|
|
|
break; |
312
|
|
|
case 'globals': |
313
|
|
|
$input=&$GLOBALS; |
314
|
|
|
break; |
315
|
|
|
case 'data': |
316
|
|
|
$input=&$datas; |
317
|
|
|
break; |
318
|
|
|
default: |
319
|
|
|
return null; |
320
|
|
|
} |
321
|
|
|
if ('' == $name) { |
322
|
|
|
// 获取全部变量 |
323
|
|
|
$data=$input; |
324
|
|
|
$filters=isset($filter) ? $filter : 'htmlspecialchars'; |
325
|
|
|
if ($filters) { |
326
|
|
|
if (is_string($filters)) { |
327
|
|
|
$filters=explode(',', $filters); |
|
|
|
|
328
|
|
|
} |
329
|
|
|
else if (is_array($filters)) { |
330
|
|
|
foreach ($filters as $filter) { |
331
|
|
|
$data=array_map_recursive($filter, $data); // 参数过滤 |
332
|
|
|
} |
333
|
|
|
} else { |
334
|
|
|
throw new \RuntimeException('$filters must be an array or string.'); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} elseif (isset($input[$name])) { |
338
|
|
|
// 取值操作 |
339
|
|
|
$data=$input[$name]; |
340
|
|
|
$filters=isset($filter) ? $filter : 'htmlspecialchars'; |
341
|
|
|
if ($filters) { |
342
|
|
|
if (is_string($filters)) { |
343
|
|
|
if (0 === strpos($filters, '/')) { |
344
|
|
|
if (1 !== preg_match($filters, (string) $data)) { |
345
|
|
|
// 支持正则验证 |
346
|
|
|
return isset($default) ? $default : null; |
347
|
|
|
} |
348
|
|
|
} else { |
349
|
|
|
$filters=explode(',', $filters); |
350
|
|
|
} |
351
|
|
|
} elseif (is_int($filters)) { |
352
|
|
|
$filters=array($filters); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
if (is_array($filters)) { |
356
|
|
|
foreach ($filters as $filter) { |
357
|
|
|
$filter=trim($filter); |
358
|
|
|
if (function_exists($filter)) { |
359
|
|
|
$data=is_array($data) ? array_map_recursive($filter, $data) : $filter($data); // 参数过滤 |
360
|
|
|
} else { |
361
|
|
|
$data=filter_var($data, is_int($filter) ? $filter : filter_id($filter)); |
362
|
|
|
if (false === $data) { |
363
|
|
|
return isset($default) ? $default : null; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
if (!empty($type)) { |
370
|
|
|
switch (strtolower($type)) { |
371
|
|
|
case 'a': // 数组 |
372
|
|
|
$data=(array) $data; |
373
|
|
|
break; |
374
|
|
|
case 'd': // 数字 |
375
|
|
|
$data=(int) $data; |
376
|
|
|
break; |
377
|
|
|
case 'f': // 浮点 |
378
|
|
|
$data=(float) $data; |
379
|
|
|
break; |
380
|
|
|
case 'b': // 布尔 |
381
|
|
|
$data=(boolean) $data; |
382
|
|
|
break; |
383
|
|
|
case 's':// 字符串 |
384
|
|
|
default: |
385
|
|
|
$data=(string) $data; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
} else { |
389
|
|
|
// 变量默认值 |
390
|
|
|
$data=isset($default) ? $default : null; |
391
|
|
|
} |
392
|
|
|
is_array($data) && array_walk_recursive($data, 'think_filter'); |
393
|
|
|
return $data; |
394
|
|
|
} |
395
|
|
|
function array_map_recursive($filter, $data) |
396
|
|
|
{ |
397
|
|
|
$result=array(); |
398
|
|
|
foreach ($data as $key => $val) { |
399
|
|
|
$result[$key]=is_array($val) |
400
|
|
|
? array_map_recursive($filter, $val) |
401
|
|
|
: call_user_func($filter, $val); |
402
|
|
|
} |
403
|
|
|
return $result; |
404
|
|
|
} |
405
|
|
|
function think_filter(&$value) |
406
|
|
|
{ |
407
|
|
|
// TODO 其他安全过滤 |
408
|
|
|
|
409
|
|
|
// 过滤查询特殊字符 |
410
|
|
|
if (preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i', $value)) { |
411
|
|
|
$value .= ' '; |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
/** |
415
|
|
|
* @param string $name |
416
|
|
|
* |
417
|
|
|
* @return string|null |
418
|
|
|
*/ |
419
|
|
|
function config($name=null,$value=null,$default=null){ |
420
|
|
|
$config=\puck\Conf::load(); |
421
|
|
|
if ($name===null){ |
422
|
|
|
return $config->all(); |
423
|
|
|
} |
424
|
|
|
if ($value===null){ |
425
|
|
|
return $config->get($name,$default); |
426
|
|
|
} |
427
|
|
|
$config->set($name,$value); |
428
|
|
|
} |
429
|
|
|
/** |
430
|
|
|
* 字符串命名风格转换 |
431
|
|
|
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格 |
432
|
|
|
* @param string $name 字符串 |
433
|
|
|
* @param integer $type 转换类型 |
434
|
|
|
* @return string |
435
|
|
|
*/ |
436
|
|
|
function parse_name($name, $type=0) { |
437
|
|
|
if ($type) { |
438
|
|
|
return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function($match) {return strtoupper($match[1]); }, $name)); |
439
|
|
|
} else { |
440
|
|
|
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_")); |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
if (! function_exists('app')) { |
445
|
|
|
/** |
446
|
|
|
* 获取容器实例 |
447
|
|
|
* |
448
|
|
|
* @param string $make |
449
|
|
|
* @return mixed|\puck\App |
450
|
|
|
*/ |
451
|
|
|
function app($make = null) |
452
|
|
|
{ |
453
|
|
|
if (is_null($make)) { |
454
|
|
|
return Container::getInstance(); |
455
|
|
|
} |
456
|
|
|
return Container::getInstance()->make($make); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
if (! function_exists('env')) { |
461
|
|
|
/** |
462
|
|
|
* 获取环境变量 |
463
|
|
|
* |
464
|
|
|
* @param string $key |
465
|
|
|
* @param mixed $default |
466
|
|
|
* @return mixed |
467
|
|
|
*/ |
468
|
|
|
function env($key, $default = null) |
469
|
|
|
{ |
470
|
|
|
$value = getenv($key); |
471
|
|
|
if ($value === false) { |
472
|
|
|
return value($default); |
473
|
|
|
} |
474
|
|
|
switch (strtolower($value)) { |
475
|
|
|
case 'true': |
476
|
|
|
case '(true)': |
477
|
|
|
return true; |
478
|
|
|
case 'false': |
479
|
|
|
case '(false)': |
480
|
|
|
return false; |
481
|
|
|
case 'empty': |
482
|
|
|
case '(empty)': |
483
|
|
|
return ''; |
484
|
|
|
case 'null': |
485
|
|
|
case '(null)': |
486
|
|
|
return; |
487
|
|
|
} |
488
|
|
|
if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) { |
489
|
|
|
return substr($value, 1, -1); |
490
|
|
|
} |
491
|
|
|
return $value; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
if (! function_exists('value')) { |
495
|
|
|
/** |
496
|
|
|
* 返回给定的表达式的结果 |
497
|
|
|
* |
498
|
|
|
* @param mixed $value |
499
|
|
|
* @return mixed |
500
|
|
|
*/ |
501
|
|
|
function value($value) |
502
|
|
|
{ |
503
|
|
|
return $value instanceof Closure ? $value() : $value; |
504
|
|
|
} |
505
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: