-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
290 lines (269 loc) · 6.87 KB
/
functions.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
<?php
function ajaxReturn($data, $success = true)
{
$return = array();
$return['ret'] = $success ? 1 : 0;
if($success){
if(is_string($data))
$return['msg'] = $data;
else if(is_array($data))
$return['data'] = $data;
} else {
$return['msg'] = $data;
}
exit(json_encode($return));
}
function unescape($str)
{
$ret = '';
$len = strlen($str);
for ($i = 0; $i < $len; $i ++)
{
if ($str[$i] == '%' && $str[$i + 1] == 'u')
{
$val = hexdec(substr($str, $i + 2, 4));
if ($val < 0x7f)
$ret .= chr($val);
else
if ($val < 0x800)
$ret .= chr(0xc0 | ($val >> 6)) .
chr(0x80 | ($val & 0x3f));
else
$ret .= chr(0xe0 | ($val >> 12)) .
chr(0x80 | (($val >> 6) & 0x3f)) .
chr(0x80 | ($val & 0x3f));
$i += 5;
} else
if ($str[$i] == '%')
{
$ret .= urldecode(substr($str, $i, 3));
$i += 2;
} else
$ret .= $str[$i];
}
return $ret;
}
function isImg($filename)
{
return in_array(strtolower(pathinfo($filename, PATHINFO_EXTENSION)), ['jpg', 'gif', 'png', 'jpeg']);
}
function formatMoney($money, $decimals = 2, $separator = ',', $decimalpoint = '.')
{
return number_format($money, $decimals, $decimalpoint, $separator);
}
function getDawnByDate($date = null)
{
if ($date === 0)
return null;
$today = strtotime(date('Y-m-d 00:00:00'));
switch ($date) {
case 1:
return strtotime('-1 day', $today);
break;
case 3:
return strtotime('-3 day', $today);
break;
case 7:
return strtotime('-7 day', $today);
break;
case 'm':
return strtotime('-1 month', $today);
break;
default:
return null;
break;
}
}
function ip()
{
$ip = null;
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown'))
$ip = getenv('HTTP_CLIENT_IP');
elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown'))
$ip = getenv('HTTP_X_FORWARDED_FOR');
elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'),'unknown'))
$ip = getenv('REMOTE_ADDR');
elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],'unknown'))
$ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
function getUserAgent()
{
return $_SERVER['HTTP_USER_AGENT'];
}
function getClientOs()
{
$agent = getUserAgent();
$os = null;
if(stristr($agent, 'win 9x') && stripos($agent,"4.90"))
$os = 'Windows ME';
else if (stristr($agent, 'win')) {
if (stristr($agent, '95'))
$os = 'Windows 95';
else if(stristr($agent, 98))
$os = 'Windows 98';
else if(stristr($agent, 'nt 5.1'))
$os = 'Windows XP';
else if(stristr($agent, 'nt 5'))
$os = 'Windows 2000';
else if(stristr($agent, 'nt'))
$os = 'Windows NT';
else if(stristr($agent, '32'))
$os = 'Windows 32';
else
$os = 'Windows';
} else if(stristr($agent, 'linux'))
$os = 'Linux';
else if(stristr($agent, 'unix'))
$os = 'Unix';
else if(stristr($agent, 'sun') && stristr($agent, 'os'))
$os = 'SunOS';
else if(stristr($agent, 'ibm') && stristr($agent, 'os'))
$os = 'IBM OS/2';
else if(stristr($agent, 'mac'))
$os = 'Macintosh';
else if(stristr($agent, 'powerpc'))
$os = 'PowerPC';
else if(stristr($agent, 'aix'))
$os = 'AIX';
else if(stristr($agent, 'HPUX'))
$os = 'HPUX';
else if(stristr($agent, 'netbsd'))
$os = 'NetBSD';
else if(stristr($agent, 'bsd'))
$os = 'BSD';
else if(stristr($agent, 'OSF1'))
$os = 'OSF1';
else if(stristr($agent, 'IRIX'))
$os = 'IRIX';
else if(stristr($agent, 'FreeBSD'))
$os = 'FreeBSD';
else if(stristr($agent, 'teleport'))
$os = 'teleport';
else if(stristr($agent, 'flashget'))
$os = 'flashget';
else if(stristr($agent, 'webzip'))
$os = 'webzip';
else if(stristr($agent, 'offline'))
$os = 'offline';
else
$os = 'Unknown';
return $os;
}
function getBrowser()
{
$agent = getUserAgent();
$browser = 'unknown';
$version = 'unknown';
if ($agent) {
if (strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
$browser = 'ie';
else if(strpos($agent,'Firefox')!==false)
$browser = 'firefox';
else if(strpos($agent,'Chrome')!==false)
$browser = 'chrome';
else if(strpos($agent,'Opera')!==false)
$browser = 'opera';
else if((strpos($agent,'Chrome')==false) && strpos($agent,'Safari')!==false)
$browser = 'safari';
if (preg_match('/MSIE\s(\d+)\..*/i', $agent, $regs))
$version = $regs[1];
elseif (preg_match('/FireFox\/(\d+)\..*/i', $agent, $regs))
$version = $regs[1];
elseif (preg_match('/Opera[\s|\/](\d+)\..*/i', $agent, $regs))
$version = $regs[1];
elseif (preg_match('/Chrome\/(\d+)\..*/i', $agent, $regs))
$version = $regs[1];
elseif ((strpos($agent,'Chrome')==false) && preg_match('/Safari\/(\d+)\..*$/i', $agent, $regs))
$version = $regs[1];
}
return [
'browser' => ucfirst($browser),
'version' => $version
];
}
function getIpArea($ip = null)
{
$ip or $ip = getIp();
$ch = curl_init('http://ipapi.sinaapp.com/api.php?f=json&ip=' . $ip);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if (!$res)
return curl_error($ch);
curl_close($ch);
$res = json_decode($res);
$f = fopen('data/ip.txt', 'a+');
fwrite($f, $ip . '#' . $res->area1 . "\n");
fclose($f);
return [
'ip' => $ip,
'area' => $res->area1
];
}
function getGenderByIDCard($idcard)
{
if (!preg_match('/^[\dxX]+$/', $idcard))
return 0;
$strlen = strlen($idcard);
if ($strlen == 15) {
return substr($idcard, 14)%2 == 0 ? 2 : 1;
}
if ($strlen == 18) {
return substr($idcard, 16, 1)%2 == 0 ? 2 : 1;
}
return 0;
}
function getAgeByIDCard($idcard)
{
$strlen = strlen($idcard);
if ($strlen != 15 and $strlen != 18)
return 0;
if ($strlen == 15)
$birthday = substr($idcard, 6, 6);
else
$birthday = substr($idcard, 6, 8);
$age = floor((time() - strtotime($birthday))/31104000);
return $age > 0 ? $age : 0;
}
function getProvinceByIDCard($idcard)
{
$provinces = array(
'11' => '北京市',
'12' => '天津市',
'13' => '河北省',
'14' => '山西省',
'15' => '内蒙古自治区',
'21' => '辽宁省',
'22' => '吉林省',
'23' => '黑龙江省',
'31' => '上海市',
'32' => '江苏省',
'33' => '浙江省',
'34' => '安徽省',
'35' => '福建省',
'36' => '江西省',
'37' => '山东省',
'41' => '河南省',
'42' => '湖北省',
'43' => '湖南省',
'44' => '广东省',
'45' => '广西壮族自治区',
'46' => '海南省',
'50' => '重庆市',
'51' => '四川省',
'52' => '贵州省',
'53' => '云南省',
'54' => '西藏自治区',
'61' => '陕西省',
'62' => '甘肃省',
'63' => '青海省',
'64' => '宁夏回族自治区',
'65' => '新疆维吾尔族自治区',
'71' => '台湾省',
'81' => '香港特别行政区',
'82' => '澳门特别行政区',
);
$id = substr($idcard, 0, 2);
return array_key_exists($id, $provinces) ? $provinces[$id] : 0;
}