-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.php
328 lines (295 loc) · 10.2 KB
/
Util.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* 便利工具
*
* @package comm
* @author [email protected]
*/
class Comm_Util {
/**
* 控制台颜色值
*/
const CONSOLE_COLOR_NORMAL = '30';
const CONSOLE_COLOR_BLACK = '30';
const CONSOLE_COLOR_ERROR = '31';
const CONSOLE_COLOR_OK = '32';
const CONSOLE_COLOR_WARNING = '33';
const CONSOLE_COLOR_INFO = '34';
/**
* 判断php宿主环境是否是64bit
*
* ps: 在64bit下,php有诸多行为与32bit不一致,诸如mod、integer、json_encode/decode等,具体请自行google。
*
* @return bool
*/
public static function is64bit() {
return ( int ) 0xFFFFFFFF !== -1;
}
/**
* 修正过的ip2long
*
* 可去除ip地址中的前导0。32位php兼容,若超出127.255.255.255,则会返回一个float
*
* for example: 02.168.010.010 => 2.168.10.10
*
* 处理方法有很多种,目前先采用这种分段取绝对值取整的方法吧……
* @param string $ip
* @return float 使用unsigned int表示的ip。如果ip地址转换失败,则会返回0
*/
public static function ip2long($ip) {
$ip_chunks = explode('.', $ip, 4);
foreach ($ip_chunks as $i => $v) {
$ip_chunks[$i] = abs(intval($v));
}
return sprintf('%u', ip2long(implode('.', $ip_chunks)));
}
/**
* 判断是否是内网ip
* @param string $ip
* @return boolean
*/
public static function isPrivateIp($ip) {
$ip_value = self::ip2long($ip);
return ($ip_value & 0xFF000000) === 0x0A000000 || //10.0.0.0-10.255.255.255
($ip_value & 0xFFF00000) === 0xAC100000 || //172.16.0.0-172.31.255.255
($ip_value & 0xFFFF0000) === 0xC0A80000; //192.168.0.0-192.168.255.255
}
/**
* 使json_decode能处理32bit机器上溢出的数值类型
*
* @param string $response
* @param string $field_name
* @param boolean $assoc
* @return array|object
*/
public static function jsonDecode($value, $assoc = true) {
//PHP5.3以下版本不支持
//TODO 获取机器CPU位数
if (version_compare(PHP_VERSION, '5.3.0', '>') && defined('JSON_BIGINT_AS_STRING')) {
return json_decode($value, $assoc, 512, JSON_BIGINT_AS_STRING);
} else {
$value = preg_replace("/\"(\w+)\":(\d+[\.\d+[e\+\d+]*]*)/", "\"\$1\":\"\$2\"", $value);
return json_decode($value, $assoc);
}
}
/**
* To get ip belonged region according to ip
* @param <string> $ip ip address, heard that can be ip strings, split by "," ,but i found it not used
* @param <int> $type 地域名及ISP的显示格式 0 默认文本格式;
1 regions.xml中的id;
2 regions.xml中的code,即ISO-3166的地区代码;
3 regions.xml中的fips,即FIPS的地区代码。
* @param <string> $encoding 编码类, gbk或utf-8, 默认为gbk
* @return <int or array>
*/
static function getIpSource($ip, $type = 1, $encoding = 'utf-8') {
if (!function_exists('lookup_ip_source'))
return 0;
$code = lookup_ip_source($ip, $type, $encoding);
switch ($code) {
case "-1" :
case "-2" :
case "-3" :
return 0;
break;
default :
return $code;
break;
}
}
/**
* 获取真实的客户端ip地址
*
* This function is copied from login.sina.com.cn/module/libmisc.php/get_ip()
*
* @param boolean $to_long 可选。是否返回一个unsigned int表示的ip地址
* @return string|float 客户端ip。如果to_long为真,则返回一个unsigned int表示的ip地址;否则,返回字符串表示。
*/
public static function getRealClientIp($to_long = false) {
$forwarded = self::getServer('HTTP_X_FORWARDED_FOR');
if ($forwarded) {
$ip_chains = explode(',', $forwarded);
$proxied_client_ip = $ip_chains ? trim(array_pop($ip_chains)) : '';
}
if (Comm_Util::isPrivateIp(self::getServer('REMOTE_ADDR')) && isset($proxied_client_ip)) {
$real_ip = $proxied_client_ip;
} else {
$real_ip = self::getServer('REMOTE_ADDR');
}
return $to_long ? self::ip2long($real_ip) : $real_ip;
}
/**
* 根据实际场景,获取客户端IP
* @param boolean $to_long 是否变为整型IP
* @return string
*/
public static function getClientIp($to_long = false) {
static $ip = null;
if ($ip === null) {
$module = Yaf_Dispatcher::getInstance()->getRequest()->getModuleName();
switch ($module) {
case 'Internal' :
isset($_GET['cip']) && $ip = $_GET['cip'];
break;
case 'Openapi' :
$headers = array();
if(function_exists('getallheaders')) {
foreach( getallheaders() as $name => $value ) {
$headers[strtolower($name)] = $value;
}
} else {
foreach($_SERVER as $name => $value) {
if(substr($name, 0, 5) == 'HTTP_') {
$headers[strtolower(str_replace(' ', '-', str_replace('_', ' ', substr($name, 5))))] = $value;
}
}
}
isset($headers['cip']) && $ip = $headers['cip'];
break;
case 'Cli' :
$ip = '0.0.0.0';
// $ip = `/sbin/ifconfig | grep 'inet addr' | awk '{ print $2 }' | awk -F ':' '{ print $2}' | head -1`;
break;
}
empty($ip) && $ip = self::getRealClientIp();
}
return $to_long ? self::ip2long($ip) : $ip;
}
/**
* 获取当前Referer
*
* @return string
*/
public static function getReferer() {
return self::getServer('HTTP_REFERER');
}
/**
* 获取当前域名
*
* @return string
*/
public static function getDomain() {
return self::getServer('SERVER_NAME');
}
/**
* 得到当前请求的环境变量
*
* @param string $name
* @return string|null 当$name指定的环境变量不存在时,返回null
*/
public static function getServer($name) {
return isset($_SERVER[$name]) ? $_SERVER[$name] : null;
}
/**
* 返回当前url
*
* @param bool $urlencode 是否urlencode后返回,默认true
*/
public static function getCurrentUrl($urlencode = true) {
$req_uri = self::getServer('REQUEST_URI');
if (null === $req_uri) {
$req_uri = self::getServer('PHP_SELF');
}
$https = self::getServer('HTTPS');
$s = null === $https ? '' : ('on' == $https ? 's' : '');
$protocol = self::getServer('SERVER_PROTOCOL');
$protocol = strtolower(substr($protocol, 0, strpos($protocol, '/'))) . $s;
$port = self::getServer('SERVER_PORT');
$port = ($port == '80') ? '' : (':' . $port);
$server_name = self::getServer('SERVER_NAME');
$current_url = $protocol . '://' . $server_name . $port . $req_uri;
return $urlencode ? rawurlencode($current_url) : $current_url;
}
/**
* 执行系统shell脚本,并返回输出
* @param $cmd
* @return string
*/
static public function execute($cmd) {
return shell_exec($cmd);
}
/**
* 循环写入网络包
*
* @param resource $fp 网络资源
* @param string $content 内容
*
* @return int
*
* @author chengxuan <[email protected]>
*/
static public function netWrite($fp, $content) {
$length = strlen($content);
$write_length = fwrite($fp, $content);
if($write_length < $length) {
for($i = 0; $write_length < $length && $i < 10; ++$i) {
$write_length += fwrite($fp, substr($content, $write_length));
}
}
return $write_length;
}
/**
* 判断是否为cli方式运行
* @return bool
*/
static public function isCli() {
return php_sapi_name()=='cli';
}
/**
* 返回控制台输出时高亮颜色
* @param string $txt
* @param string $color CONSOLE_COLOR
* @return string
*/
static public function colorText($txt, $color=self::CONSOLE_COLOR_INFO) {
if(!self::isCli() || $color==self::CONSOLE_COLOR_NORMAL)
return $txt;
return "\033[0;31;".$color."m".$txt."\033[0m";
}
/**
* 返回控制台输出时错误字符串颜色
* @param string $txt
* @param string $color CONSOLE_COLOR
* @return string
*/
static public function errorText($txt) {
return self::colorText($txt, self::CONSOLE_COLOR_ERROR);
}
/**
* 返回控制台输出时错误字符串颜色
* @param string $txt
* @param string $color CONSOLE_COLOR
* @return string
*/
static public function infoText($txt) {
return self::colorText($txt, self::CONSOLE_COLOR_INFO);
}
/**
* 返回控制台输出时错误字符串颜色
* @param string $txt
* @param string $color CONSOLE_COLOR
* @return string
*/
static public function warningText($txt) {
return self::colorText($txt, self::CONSOLE_COLOR_WARNING);
}
/**
* 判断一个给定的UA是否为主流的搜索引擎 http://www.useragentstring.com/pages/Crawlerlist/
* @param $ua user agent
* @return bool
*/
static function isSpiderUA($ua='') {
if(!$ua) {
$ua = $_SERVER["HTTP_USER_AGENT"];
}
//搜狗: 'Sogou web spider'
//搜搜; 'Sosospider'
$ua_list = array('Baiduspider', 'bingbot', 'Googlebot', 'msnbot', 'YoudaoBot', 'spider', 'Sosospider', 'Yahoo! Slurp');
foreach ($ua_list as $item) {
if(strpos($ua, $item) !== false) {
return true;
}
}
return false;
}
}