-
Notifications
You must be signed in to change notification settings - Fork 137
/
Tools.php
executable file
·1523 lines (1353 loc) · 33.1 KB
/
Tools.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class Tools {
const FLAG_NUMERIC = 1;
const FLAG_NO_NUMERIC = 2;
const FLAG_ALPHANUMERIC = 3;
/**
* 生成随机密码
*
* @param integer $length Desired length (optional)
* @param string $flag Output type (NUMERIC, ALPHANUMERIC, NO_NUMERIC)
*
* @return string Password
*/
public static function passwdGen($length = 8, $flag = self::FLAG_NO_NUMERIC) {
switch ($flag)
{
case self::FLAG_NUMERIC:
$str = '0123456789';
break;
case self::FLAG_NO_NUMERIC:
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case self::FLAG_ALPHANUMERIC:
default:
$str = 'abcdefghijkmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
}
for ($i = 0, $passwd = ''; $i < $length; $i++)
$passwd .= Tools::substr($str, mt_rand(0, Tools::strlen($str) - 1), 1);
return $passwd;
}
/**
* 替换第一次出现的字符串
*
* @param $search
* @param $replace
* @param $subject
* @param int $cur
*
* @return mixed
*/
public static function strReplaceFirst($search, $replace, $subject, $cur = 0) {
return (strpos($subject, $search, $cur)) ? substr_replace($subject, $replace, (int)strpos($subject, $search, $cur), strlen($search)) : $subject;
}
/**
* 跳转
*
* @param $url
* @param null $headers
*/
public static function redirect($url, $headers = null) {
if (!empty($url))
{
if ($headers)
{
if (!is_array($headers))
$headers = array($headers);
foreach ($headers as $header)
header($header);
}
header('Location: ' . $url);
exit;
}
}
/**
* 清理URL中的http头
*
* @param $url
* @param bool $cleanall
*
* @return mixed|string
*/
public static function cleanUrl($url, $cleanall = true) {
if (strpos($url, 'http://') !== false)
{
if ($cleanall)
{
return '/';
}
else
{
return str_replace('http://', '', $url);
}
}
return $url;
}
/**
* 获取当前域名
*
* @param bool $http
* @param bool $entities
*
* @return string
*/
public static function getHttpHost($http = false, $entities = false) {
$host = (isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST']);
if ($entities)
$host = htmlspecialchars($host, ENT_COMPAT, 'UTF-8');
if ($http)
{
$host = self::getCurrentUrlProtocolPrefix() . $host;
}
return $host;
}
/**
* 获取当前服务器名
*
* @return mixed
*/
public static function getServerName() {
if (isset($_SERVER['HTTP_X_FORWARDED_SERVER']) && $_SERVER['HTTP_X_FORWARDED_SERVER'])
return $_SERVER['HTTP_X_FORWARDED_SERVER'];
return $_SERVER['SERVER_NAME'];
}
/**
* 获取用户IP地址
*
* @return mixed
*/
public static function getRemoteAddr() {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && (!isset($_SERVER['REMOTE_ADDR']) || preg_match('/^127\..*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^172\.16.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^192\.168\.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^10\..*/i', trim($_SERVER['REMOTE_ADDR']))))
{
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ','))
{
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
return $ips[0];
}
else
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $_SERVER['REMOTE_ADDR'];
}
/**
* 获取用户来源地址
*
* @return null
*/
public static function getReferer() {
if (isset($_SERVER['HTTP_REFERER']))
{
return $_SERVER['HTTP_REFERER'];
}
else
{
return null;
}
}
/**
* 判断是否使用了HTTPS
*
* @return bool
*/
public static function usingSecureMode() {
if (isset($_SERVER['HTTPS']))
return ($_SERVER['HTTPS'] == 1 || strtolower($_SERVER['HTTPS']) == 'on');
if (isset($_SERVER['SSL']))
return ($_SERVER['SSL'] == 1 || strtolower($_SERVER['SSL']) == 'on');
return false;
}
/**
* 获取当前URL协议
*
* @return string
*/
public static function getCurrentUrlProtocolPrefix() {
if (Tools::usingSecureMode())
return 'https://';
else
return 'http://';
}
/**
* 判断是否本站链接
*
* @param $referrer
*
* @return string
*/
public static function secureReferrer($referrer) {
if (preg_match('/^http[s]?:\/\/' . Tools::getServerName() . '(:443)?\/.*$/Ui', $referrer))
return $referrer;
return '/';
}
/**
* 获取POST或GET的指定字段内容
*
* @param $key
* @param bool $default_value
*
* @return bool|string
*/
public static function getValue($key, $default_value = false) {
if (!isset($key) || empty($key) || !is_string($key))
return false;
$ret = (isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $default_value));
if (is_string($ret) === true)
$ret = trim(urldecode(preg_replace('/((\%5C0+)|(\%00+))/i', '', urlencode($ret))));
return !is_string($ret) ? $ret : stripslashes($ret);
}
/**
* 判断POST或GET中是否包含指定字段
*
* @param $key
*
* @return bool
*/
public static function getIsset($key) {
if (!isset($key) || empty($key) || !is_string($key))
return false;
return isset($_POST[$key]) ? true : (isset($_GET[$key]) ? true : false);
}
/**
* 判断是否为提交操作
*
* @param $submit
*
* @return bool
*/
public static function isSubmit($submit) {
return (isset($_POST[$submit]) || isset($_POST[$submit . '_x']) || isset($_POST[$submit . '_y']) || isset($_GET[$submit]) || isset($_GET[$submit . '_x']) || isset($_GET[$submit . '_y']));
}
/**
* 过滤HTML内容后返回
*
* @param $string
* @param bool $html
*
* @return array|string
*/
public static function safeOutput($string, $html = false) {
if (!$html)
$string = strip_tags($string);
return @Tools::htmlentitiesUTF8($string, ENT_QUOTES);
}
public static function htmlentitiesUTF8($string, $type = ENT_QUOTES) {
if (is_array($string))
return array_map(array('Tools', 'htmlentitiesUTF8'), $string);
return htmlentities((string)$string, $type, 'utf-8');
}
public static function htmlentitiesDecodeUTF8($string) {
if (is_array($string))
return array_map(array('Tools', 'htmlentitiesDecodeUTF8'), $string);
return html_entity_decode((string)$string, ENT_QUOTES, 'utf-8');
}
/**
* 对POST内容进行处理
*
* @return array
*/
public static function safePostVars() {
if (!is_array($_POST))
return array();
$_POST = array_map(array('Tools', 'htmlentitiesUTF8'), $_POST);
}
/**
* 删除文件夹
*
* @param $dirname
* @param bool $delete_self
*/
public static function deleteDirectory($dirname, $delete_self = true) {
$dirname = rtrim($dirname, '/') . '/';
if (is_dir($dirname))
{
$files = scandir($dirname);
foreach ($files as $file)
if ($file != '.' && $file != '..' && $file != '.svn')
{
if (is_dir($dirname . $file))
Tools::deleteDirectory($dirname . $file, true);
elseif (file_exists($dirname . $file))
unlink($dirname . $file);
}
if ($delete_self)
rmdir($dirname);
}
}
/**
* 显示错误信息
*
* @param string $string
* @param array $error
* @param bool $htmlentities
*
* @return mixed|string
*/
public static function displayError($string = 'Fatal error', $error = array(), $htmlentities = true) {
if (DEBUG_MODE)
{
if (!is_array($error) || empty($error))
return str_replace('"', '"', $string) . ('<pre>' . print_r(debug_backtrace(), true) . '</pre>');
$key = md5(str_replace('\'', '\\\'', $string));
$str = (isset($error) AND is_array($error) AND key_exists($key, $error)) ? ($htmlentities ? htmlentities($error[$key], ENT_COMPAT, 'UTF-8') : $error[$key]) : $string;
return str_replace('"', '"', stripslashes($str));
}
else
{
return str_replace('"', '"', $string);
}
}
/**
* 打印出对象的内容
*
* @param $object
* @param bool $kill
*
* @return mixed
*/
public static function dieObject($object, $kill = true) {
echo '<pre style="text-align: left;">';
print_r($object);
echo '</pre><br />';
if ($kill)
die('END');
return ($object);
}
public static function encrypt($passwd) {
return md5(_COOKIE_KEY_ . $passwd);
}
public static function getToken($string) {
return !empty($string) ? Tools::encrypt($string) : false;
}
/**
* 截取字符串,支持中文
*
* @param $str
* @param $max_length
* @param string $suffix
*
* @return string
*/
public static function truncate($str, $max_length, $suffix = '...') {
if (Tools::strlen($str) <= $max_length)
return $str;
$str = utf8_decode($str);
return (utf8_encode(substr($str, 0, $max_length - Tools::strlen($suffix)) . $suffix));
}
public static function replaceAccentedChars($str) {
$patterns = array( /* Lowercase */
'/[\x{0105}\x{00E0}\x{00E1}\x{00E2}\x{00E3}\x{00E4}\x{00E5}]/u',
'/[\x{00E7}\x{010D}\x{0107}]/u',
'/[\x{010F}]/u',
'/[\x{00E8}\x{00E9}\x{00EA}\x{00EB}\x{011B}\x{0119}]/u',
'/[\x{00EC}\x{00ED}\x{00EE}\x{00EF}]/u',
'/[\x{0142}\x{013E}\x{013A}]/u',
'/[\x{00F1}\x{0148}]/u',
'/[\x{00F2}\x{00F3}\x{00F4}\x{00F5}\x{00F6}\x{00F8}]/u',
'/[\x{0159}\x{0155}]/u',
'/[\x{015B}\x{0161}]/u',
'/[\x{00DF}]/u',
'/[\x{0165}]/u',
'/[\x{00F9}\x{00FA}\x{00FB}\x{00FC}\x{016F}]/u',
'/[\x{00FD}\x{00FF}]/u',
'/[\x{017C}\x{017A}\x{017E}]/u',
'/[\x{00E6}]/u',
'/[\x{0153}]/u',
/* Uppercase */
'/[\x{0104}\x{00C0}\x{00C1}\x{00C2}\x{00C3}\x{00C4}\x{00C5}]/u',
'/[\x{00C7}\x{010C}\x{0106}]/u',
'/[\x{010E}]/u',
'/[\x{00C8}\x{00C9}\x{00CA}\x{00CB}\x{011A}\x{0118}]/u',
'/[\x{0141}\x{013D}\x{0139}]/u',
'/[\x{00D1}\x{0147}]/u',
'/[\x{00D3}]/u',
'/[\x{0158}\x{0154}]/u',
'/[\x{015A}\x{0160}]/u',
'/[\x{0164}]/u',
'/[\x{00D9}\x{00DA}\x{00DB}\x{00DC}\x{016E}]/u',
'/[\x{017B}\x{0179}\x{017D}]/u',
'/[\x{00C6}]/u',
'/[\x{0152}]/u'
);
$replacements = array(
'a',
'c',
'd',
'e',
'i',
'l',
'n',
'o',
'r',
's',
'ss',
't',
'u',
'y',
'z',
'ae',
'oe',
'A',
'C',
'D',
'E',
'L',
'N',
'O',
'R',
'S',
'T',
'U',
'Z',
'AE',
'OE'
);
return preg_replace($patterns, $replacements, $str);
}
public static function cleanNonUnicodeSupport($pattern) {
if (!defined('PREG_BAD_UTF8_OFFSET'))
return $pattern;
return preg_replace('/\\\[px]\{[a-z]\}{1,2}|(\/[a-z]*)u([a-z]*)$/i', "$1$2", $pattern);
}
/**
* 生成年份
*
* @return array
*/
public static function dateYears() {
$tab = array();
for ($i = date('Y') - 10; $i >= 1900; $i--)
$tab[] = $i;
return $tab;
}
/**
* 生成日
*
* @return array
*/
public static function dateDays() {
$tab = array();
for ($i = 1; $i != 32; $i++)
$tab[] = $i;
return $tab;
}
/**
* 生成月
*
* @return array
*/
public static function dateMonths() {
$tab = array();
for ($i = 1; $i != 13; $i++)
$tab[$i] = date('F', mktime(0, 0, 0, $i, date('m'), date('Y')));
return $tab;
}
/**
* 根据时分秒生成时间字符串
*
* @param $hours
* @param $minutes
* @param $seconds
*
* @return string
*/
public static function hourGenerate($hours, $minutes, $seconds) {
return implode(':', array($hours, $minutes, $seconds));
}
/**
* 一日之初
*
* @param $date
*
* @return string
*/
public static function dateFrom($date) {
$tab = explode(' ', $date);
if (!isset($tab[1]))
$date .= ' ' . self::hourGenerate(0, 0, 0);
return $date;
}
/**
* 一日之终
*
* @param $date
*
* @return string
*/
public static function dateTo($date) {
$tab = explode(' ', $date);
if (!isset($tab[1]))
$date .= ' ' . self::hourGenerate(23, 59, 59);
return $date;
}
/**
* 获取精准的时间
*
* @return int
*/
public static function getExactTime() {
return microtime(true);
}
/**
* 转换成小写字符,支持中文
*
* @param $str
*
* @return bool|string
*/
public static function strtolower($str) {
if (is_array($str))
return false;
if (function_exists('mb_strtolower'))
return mb_strtolower($str, 'utf-8');
return strtolower($str);
}
/**
* 转换为int类型
*
* @param $val
*
* @return int
*/
public static function intval($val) {
if (is_int($val))
return $val;
if (is_string($val))
return (int)$val;
return (int)(string)$val;
}
/**
* 计算字符串长度
*
* @param $str
* @param string $encoding
*
* @return bool|int
*/
public static function strlen($str, $encoding = 'UTF-8') {
if (is_array($str) || is_object($str))
return false;
$str = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
if (function_exists('mb_strlen'))
return mb_strlen($str, $encoding);
return strlen($str);
}
public static function stripslashes($string) {
if (get_magic_quotes_gpc())
$string = stripslashes($string);
return $string;
}
/**
* 转换成大写字符串
*
* @param $str
*
* @return bool|string
*/
public static function strtoupper($str) {
if (is_array($str))
return false;
if (function_exists('mb_strtoupper'))
return mb_strtoupper($str, 'utf-8');
return strtoupper($str);
}
/**
* 截取字符串
*
* @param $str
* @param $start
* @param bool $length
* @param string $encoding
*
* @return bool|string
*/
public static function substr($str, $start, $length = false, $encoding = 'utf-8') {
if (is_array($str) || is_object($str))
return false;
if (function_exists('mb_substr'))
return mb_substr($str, intval($start), ($length === false ? self::strlen($str) : intval($length)), $encoding);
return substr($str, $start, ($length === false ? Tools::strlen($str) : intval($length)));
}
/**首字母大写
*
* @param $str
*
* @return string
*/
public static function ucfirst($str) {
return self::strtoupper(self::substr($str, 0, 1)) . self::substr($str, 1);
}
public static function nl2br($str) {
return preg_replace("/((<br ?\/?>)+)/i", "<br />", str_replace(array("\r\n", "\r", "\n"), "<br />", $str));
}
public static function br2nl($str) {
return str_replace("<br />", "\n", $str);
}
/**
* 判断是否真为空
*
* @param $field
*
* @return bool
*/
public static function isEmpty($field) {
return ($field === '' || $field === null);
}
public static function ceilf($value, $precision = 0) {
$precisionFactor = $precision == 0 ? 1 : pow(10, $precision);
$tmp = $value * $precisionFactor;
$tmp2 = (string)$tmp;
// If the current value has already the desired precision
if (strpos($tmp2, '.') === false)
return ($value);
if ($tmp2[strlen($tmp2) - 1] == 0)
return $value;
return ceil($tmp) / $precisionFactor;
}
public static function floorf($value, $precision = 0) {
$precisionFactor = $precision == 0 ? 1 : pow(10, $precision);
$tmp = $value * $precisionFactor;
$tmp2 = (string)$tmp;
// If the current value has already the desired precision
if (strpos($tmp2, '.') === false)
return ($value);
if ($tmp2[strlen($tmp2) - 1] == 0)
return $value;
return floor($tmp) / $precisionFactor;
}
public static function replaceSpace($url) {
return urlencode(strtolower(preg_replace('/[ ]+/', '-', trim($url, ' -/,.?'))));
}
/**
* 获取日期
*
* @param null $timestamp
*
* @return bool|string
*/
public static function getSimpleDate($timestamp = null) {
if ($timestamp == null)
{
return date('Y-m-d');
}
else
{
return date('Y-m-d', $timestamp);
}
}
/**
* 获取完整时间
*
* @param null $timestamp
*
* @return bool|string
*/
public static function getFullDate($timestamp = null) {
if ($timestamp == null)
{
return date('Y-m-d H:i:s');
}
else
{
return date('Y-m-d H:i:s', $timestamp);
}
}
/**
* 判断是否64位架构
*
* @return bool
*/
public static function isX86_64arch() {
return (PHP_INT_MAX == '9223372036854775807');
}
/**
* 获取服务器配置允许最大上传文件大小
*
* @param int $max_size
*
* @return mixed
*/
public static function getMaxUploadSize($max_size = 0) {
$post_max_size = Tools::convertBytes(ini_get('post_max_size'));
$upload_max_filesize = Tools::convertBytes(ini_get('upload_max_filesize'));
if ($max_size > 0)
$result = min($post_max_size, $upload_max_filesize, $max_size);
else
$result = min($post_max_size, $upload_max_filesize);
return $result;
}
public static function convertBytes($value) {
if (is_numeric($value))
return $value;
else
{
$value_length = strlen($value);
$qty = (int)substr($value, 0, $value_length - 1);
$unit = strtolower(substr($value, $value_length - 1));
switch ($unit)
{
case 'k':
$qty *= 1024;
break;
case 'm':
$qty *= 1048576;
break;
case 'g':
$qty *= 1073741824;
break;
}
return $qty;
}
}
/**
* 获取内存限制
*
* @return int
*/
public static function getMemoryLimit() {
$memory_limit = @ini_get('memory_limit');
return Tools::getOctets($memory_limit);
}
public static function getOctets($option) {
if (preg_match('/[0-9]+k/i', $option))
return 1024 * (int)$option;
if (preg_match('/[0-9]+m/i', $option))
return 1024 * 1024 * (int)$option;
if (preg_match('/[0-9]+g/i', $option))
return 1024 * 1024 * 1024 * (int)$option;
return $option;
}
/**
* 从array中取出指定字段
*
* @param $array
* @param $key
*
* @return array|null
*/
public static function simpleArray($array, $key) {
if (!empty($array) && is_array($array))
{
$result = array();
foreach ($array as $k => $item)
{
$result[$k] = $item[$key];
}
return $result;
}
return null;
}
public static function object2array(&$object) {
return json_decode(json_encode($object), true);
}
public static function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return floor($sec + $usec * 1000000);
}
/**
* 根据时间生成图片名
*
* @param string $image_type
*
* @return float|string
*/
public static function getTimeImageName($image_type = "image/jpeg") {
if ($image_type == "image/jpeg" || $image_type == "image/pjpeg")
{
return self::getmicrotime() . ".jpg";
}
elseif ($image_type == "image/gif")
{
return self::getmicrotime() . ".gif";
}
elseif ($image_type == "image/png")
{
return self::getmicrotime() . ".png";
}
else
{
return self::getmicrotime();
}
}
/**
* 日期计算
*
* @param $interval
* @param $step
* @param $date
*
* @return bool|string
*/
public static function dateadd($interval, $step, $date) {
list($year, $month, $day) = explode('-', $date);
if (strtolower($interval) == 'y')
{
return date('Y-m-d', mktime(0, 0, 0, $month, $day, intval($year) + intval($step)));
}
elseif (strtolower($interval) == 'm')
{
return date('Y-m-d', mktime(0, 0, 0, intval($month) + intval($step), $day, $year));
}
elseif (strtolower($interval) == 'd')
{
return date('Y-m-d', mktime(0, 0, 0, $month, intval($day) + intval($step), $year));
}
return date('Y-m-d');
}
public static function echo_microtime($tag) {
list($usec, $sec) = explode(' ', microtime());
echo $tag . ':' . ((float)$usec + (float)$sec) . "\n";
}
public static function redirectTo($link) {
if (strpos($link, 'http') !== false)
{
header('Location: ' . $link);
}
else
{
header('Location: ' . Tools::getHttpHost(true) . '/' . $link);
}
exit;
}
public static function returnAjaxJson($array) {
if (!headers_sent())
{
header("Content-Type: application/json; charset=utf-8");
}
echo(json_encode($array));
ob_end_flush();
exit;
}
public static function cmpWord($a, $b) {
if ($a['word'] > $b['word'])
{
return 1;
}
elseif ($a['word'] == $b['word'])
{
return 0;
}
else
{
return -1;
}
}
/**
* HackNews热度计算公式
*
* @param $time
* @param $viewcount
*
* @return float|int
*/
public static function getGravity($time, $viewcount) {
$timegap = ($_SERVER['REQUEST_TIME'] - strtotime($time)) / 3600;
if ($timegap <= 24)
{
return 999999;
}
return round((pow($viewcount, 0.8) / pow(($timegap + 24), 1.2)), 3) * 1000;
}
public static function getGravityS($stime, $viewcount) {
$timegap = ($_SERVER['REQUEST_TIME'] - $stime) / 3600;
if ($timegap <= 24)
{
return 999999;
}
return round((pow($viewcount, 0.8) / pow(($timegap + 24), 1.2)), 3) * 1000;
}
/**
* 优化的file_get_contents操作,超时关闭
*
* @param $url
* @param bool $use_include_path
* @param null $stream_context
* @param int $curl_timeout
*
* @return bool|mixed|string
*/
public static function file_get_contents($url, $use_include_path = false, $stream_context = null, $curl_timeout = 8) {
if ($stream_context == null && preg_match('/^https?:\/\//', $url))
$stream_context = @stream_context_create(array('http' => array('timeout' => $curl_timeout)));
if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1')) || !preg_match('/^https?:\/\//', $url))
return @file_get_contents($url, $use_include_path, $stream_context);
elseif (function_exists('curl_init'))
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_TIMEOUT, $curl_timeout);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$opts = stream_context_get_options($stream_context);