/
Global.func.php
391 lines (351 loc) · 9.64 KB
/
Global.func.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
// +---------------------------------------------------------------------------
// | Mini Framework
// +---------------------------------------------------------------------------
// | Copyright (c) 2015-2018 http://www.sunbloger.com
// +---------------------------------------------------------------------------
// | Licensed under the Apache License, Version 2.0 (the "License");
// | you may not use this file except in compliance with the License.
// | You may obtain a copy of the License at
// |
// | http://www.apache.org/licenses/LICENSE-2.0
// |
// | Unless required by applicable law or agreed to in writing, software
// | distributed under the License is distributed on an "AS IS" BASIS,
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// | See the License for the specific language governing permissions and
// | limitations under the License.
// +---------------------------------------------------------------------------
// | Source: https://github.com/jasonweicn/MiniFramework
// +---------------------------------------------------------------------------
// | Author: Jason Wei <jasonwei06@hotmail.com>
// +---------------------------------------------------------------------------
// | Website: http://www.sunbloger.com/miniframework
// +---------------------------------------------------------------------------
/**
* 获取客户端IP地址
*
* @return NULL|string|unknown
*/
function getClientIp()
{
$ip = null;
if ($ip !== null) {
return $ip;
}
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($arr as $ip) {
$ip = trim($ip);
if ($ip != 'unknown') {
$ip = $ip;
break;
}
}
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_CDN_SRC_IP'])) {
$ip = $_SERVER['HTTP_CDN_SRC_IP'];
} else {
if (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = '0.0.0.0';
}
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_CDN_SRC_IP')) {
$ip = getenv('HTTP_CDN_SRC_IP');
} else {
$ip = getenv('REMOTE_ADDR');
}
}
preg_match("/[\d\.]{7,15}/", $ip, $matches);
$ip = ! empty($matches[0]) ? $matches[0] : '0.0.0.0';
return $ip;
}
/**
* 改变数组KEY
*
* @param array $array
* @param mixed $field
* @return array
*/
function chgArrayKey($array, $field)
{
$tmp = array();
if (is_array($array)) {
foreach ($array as $value) {
$tmp[$value[$field]] = $value;
}
} else {
return false;
}
return $tmp;
}
/**
* 获取一个指定长度的随机字符串
*
* @param int $len
* @return string
*/
function getRandomString($len = 8)
{
$str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$strLen = strlen($str);
$randomString = '';
if (! is_int($len) || $len <= 0)
$len = 8;
for ($i = 0; $i < $len; $i ++) {
$randomString .= substr($str, rand(0, $strLen - 1), 1);
}
return $randomString;
}
/**
* 对图片进行base64编码转换
*
* @param string $image_file
* @return string
*/
function base64EncodeImage($image_file)
{
$base64_image = '';
if (is_file($image_file)) {
$image_info = getimagesize($image_file);
$image_data = fread(fopen($image_file, 'r'), filesize($image_file));
$base64_image = 'data:' . $image_info['mime'] . ';base64,';
$base64_image .= chunk_split(base64_encode($image_data));
} else {
return false;
}
return $base64_image;
}
/**
* 输出JSON
*
* @param mixed $data
* @param bool $push
* (true: echo & die | false: return)
*/
function pushJson($data, $push = true)
{
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
} else {
$json = json_encode($data);
}
if ($push === true) {
echo $json;
die();
}
return $json;
}
/**
* 校验日期格式是否正确
*
* @param string $date
* 日期
* @param string $formats
* 需要检验的格式数组
* @return boolean
*/
function isDate($date, $formats = array('Y-m-d', 'Y/m/d'))
{
$timestamp = strtotime($date);
if (! $timestamp) {
return false;
}
foreach ($formats as $format) {
if (date($format, $timestamp) == $date) {
return true;
}
}
return false;
}
/**
* 变量输出
*
* @param mixed $var
* @param string $label
* @param bool $echo
*/
function dump($var, $label = null, $echo = true)
{
ob_start();
var_dump($var);
$output = ob_get_clean();
$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
$cli = preg_match("/cli/i", PHP_SAPI) ? true : false;
if ($cli === true) {
$output = PHP_EOL . $label . PHP_EOL . $output . PHP_EOL;
} else {
$output = '<pre>' . PHP_EOL . $label . PHP_EOL . $output . '</pre>' . PHP_EOL;
}
if ($echo) {
echo $output;
}
return $output;
}
/**
* 输出XML
*
* @param mixed $data
* 数据
* @param bool $push
* (true: echo & die | false: return) 是否立即显示并终止程序
* @param bool $indent
* 是否格式化缩进
* @param string $root
* 根标签名称
* @param array $attr
* 根标签属性数组
* @param string $item
* 项目标签名称
* @param string $id
* 当数据为索引数组时,项目标签属性的名称
* @param string $encoding
* 编码
* @return string
*/
function pushXml($data, $push = true, $indent = false, $root = 'data', $attr = array(), $item = 'item', $id = 'id', $encoding = 'utf-8')
{
$eol = ($indent === true) ? PHP_EOL : '';
$space = ($indent === true) ? ' ' : '';
$_attr = '';
foreach ($attr as $key => $val) {
$_attr .= ' ' . $key . '="' . $val . '"';
}
$xml = '<?xml version="1.0" encoding="' . $encoding . '"?>' . $eol;
$xml .= '<' . $root . $_attr . '>' . $eol;
$xml .= parseDataToXml($data, $item, $id, $indent);
$xml .= '</' . $root . '>';
if ($push === true) {
echo $xml;
die();
}
return $xml;
}
/**
* 数据转换XML
*
* @param mixed $data
* @param string $item
* @param string $id
* @param string $indent
* @param int $level
* @return string
*/
function parseDataToXml($data, $item = 'item', $id = 'id', $indent = false, $level = 1)
{
$eol = ($indent === true) ? PHP_EOL : '';
$space = ($indent === true) ? str_repeat(' ', $level) : '';
$xml = $attr = '';
if (empty($data)) {
return $xml;
}
foreach ($data as $key => $val) {
if (is_int($key) && $key >= 0) {
if (! empty($id)) {
$attr = ' ' . $id . '="' . $key . '"';
}
$key = $item;
}
$xml .= $space . '<' . $key . $attr . '>';
if (is_array($val) || is_object($val)) {
$level ++;
$xml .= $eol . parseDataToXml($val, $item, $id, $indent, $level);
$level --;
$xml .= $space . '</' . $key . '>' . $eol;
} else {
$xml .= $val;
$xml .= '</' . $key . '>' . $eol;
}
}
return $xml;
}
/**
* 校验图片是否有效
*
* @param string $file
* @return boolean
*/
function isImage($file)
{
$tmp = getimagesize($file);
switch ($tmp['mime']) {
case 'image/jpeg':
$img = imagecreatefromjpeg($file);
break;
case 'image/gif':
$img = imagecreatefromgif($file);
break;
case 'image/png':
$img = imagecreatefrompng($file);
break;
default:
return false;
break;
}
if ($img == false) {
return false;
} else {
return true;
}
}
/**
* 获取字符串长度
*
* @param string $string
* @return int
*/
function getStringLen($string)
{
return (strlen($string) + mb_strlen($string, 'UTF8')) / 2;
}
/**
* 让浏览器下载文件
*
* @param string $file 文件路径
* @param string $customName 自定义文件名
* @return output | bool
*/
function browserDownload($file, $customName = null)
{
if (file_exists($file)) {
$filename = empty($customName) ? basename($file) : $customName;
header('Content-length: ' . filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename. '"');
@readfile($file);
} else {
return false;
}
}
/**
* 获取文件扩展名
*
* @param string $filename
* @return string
*/
function getFileExtName($filename)
{
$tmp = explode('.', $filename);
return $tmp[count($tmp) - 1];
}
/**
* 获取一个指定长度INT类型HASH值
*
* @param string $s
* @param number $len
* @return number
*/
function getHash($s, $len = 4)
{
$h = sprintf('%u', crc32($s));
return intval(fmod($h, $len));
}