forked from hetao29/slightphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSUtil.php
More file actions
100 lines (96 loc) · 3.18 KB
/
SUtil.php
File metadata and controls
100 lines (96 loc) · 3.18 KB
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
<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation. You should have received a copy of the |
| GNU General Public License along with this program. If not, see |
| http://www.gnu.org/licenses/. |
| Copyright (C) 2008-2009. All Rights Reserved. |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com |
+-----------------------------------------------------------------------+
}}}*/
/**
* @package SlightPHP
*/
class SUtil{
/**
* Get Request Value
* @param array $data ($_GET,$_POST)
* @param string $key
* @param bool $isnum true|false
*/
static function getRequestValue(array $data,$key,$isnum=false,$default=null,$minLength=0,$maxLength=0){
if(!isset($data[$key]) || empty($data[$key]) || ($isnum && !is_numeric($data[$key])) ||
strlen($data[$key])<$minLength || ($maxLength!=0 && strlen($data[$key])>$maxLength)) {
return $default;
}
return $data[$key];
}
/**
*
*/
static function log($logFile,$data){
error_log("[".date("Y-m-d H:i:s")."]$data\r\n",3,$logFile);
}
/**
*
*/
static function getIP($long=false) {
$cip = getenv('HTTP_CLIENT_IP');
$xip = getenv('HTTP_X_FORWARDED_FOR');
$rip = getenv('REMOTE_ADDR');
$srip = @$_SERVER['REMOTE_ADDR'];
if($cip && strcasecmp($cip, 'unknown')) {
$ip = $cip;
} elseif($xip && strcasecmp($xip, 'unknown')) {
$ip = $xip;
} elseif($rip && strcasecmp($rip, 'unknown')) {
$ip = $rip;
} elseif($srip && strcasecmp($srip, 'unknown')) {
$ip = $srip;
}
preg_match("/[\d\.]{7,15}/", $ip, $match);
$ip = $match[0] ? $match[0] : 'unknown';
if($long){
return sprintf("%u",ip2long($ip));
}
return $ip;
}
static function validEmail($email){
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email);
}
/**
* get substr support chinese
* return $str
*/
static function getSubStr($str,$length,$postfix='...',$encoding='UTF-8') {
$realLen = mb_strwidth($str,$encoding);
if(!is_numeric($length) or $length*2>=$realLen) {
return htmlspecialchars($str, ENT_QUOTES,$encoding);
}
$str = mb_strimwidth($str,0,$length*2,$postfix,$encoding);
return htmlspecialchars($str, ENT_QUOTES,$encoding);
}
/**
* get rand string
*/
static function getRandString($len) {
$chars = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
);
$charsLen = count($chars) - 1;
shuffle($chars);
$output = '';
for ($i=0; $i<$len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
}
?>