common.php
24.3 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
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
<?php
/**
* 缩略图方法
* $img 图片的地址及名称
* $width 缩略图宽
* $height 缩略图高
**/
function Thumb($img,$width,$height){
$img=substr($img,1);
$is_img=GetImageSize('./'.$img);
//设置宽高 生成缩略图
if(!empty($width) && !empty($height)){
import('ORG.Util.Image');
$Image = new Image();
$filename ='Uploads/thumb/'.$width.'_'.$height.'/'; //缩略图存储路径
$new_name=strtr($img,array('/'=>'_'));
if (!file_exists($filename)){
@mkdir($filename,0755);
}
if($is_img){
$is_thumb=GetImageSize('./'.$filename.$new_name);
if($is_thumb){
$thumb_img=$filename.$new_name;
}else{
$Image->thumb($img,$filename.$new_name,'',$width,$height);
$thumb_img=$filename.$new_name;
}
}else{
$thumb_img='Public/thumb.jpg';
}
}else{
if($is_img){
$thumb_img=$img;
}else{
$thumb_img='Public/thumb.jpg';
}
}
return '/'.$thumb_img;
}
/**
* 返回正整数,如果非正整数返回0
* @param type $num
* @return type
**/
function numberval($num){
if(!is_numeric($num)) return 0;
$num = (int)$num;
if(is_int($num) && $num>=0){
return $num;
}else{
return 0;
}
}
/**
* 截取字符串长度,过来所有HTML格式
* @param string $str 字符串
* @param int $len 长度
* @param string $tail 尾巴跟的字符
**/
function sub_str($data, $len, $tail='...',$strip=''){
$data = strip_tags($data, $strip);
$str = $data;
for( $i=0; $i<$len; $i++ )
{
$temp_str = substr($str,0,1);
if(ord($temp_str) > 127)
{
$i++;
if( $i<$len )
{
$new_str[] = substr($str,0,3);
$str = substr($str,3);
}
}
else
{
$new_str[] = substr($str,0,1);
$str = substr($str,1);
}
}
$str = join($new_str);
if(utf8_len($data)>$len) $str .= $tail;
return $str;
}
/**
* 截取字符长度
*
**/
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))
$slice = mb_substr($str, $start, $length, $charset);
elseif(function_exists('iconv_substr')) {
$slice = iconv_substr($str,$start,$length,$charset);
if(false === $slice) {
$slice = '';
}
}
else
{
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
}
if($suffix && strlen($str) > ($length*3))
$pp_fix=true;
else
$pp_fix=false;
return $pp_fix ? $slice.'...' : $slice;
}
/**
* 统计字utf-8符串长度,一个汉字算两个字节
* @param type $str 字符串
* @return int 长度
**/
function utf8_len($str)
{
$count = 0;
$str = iconv('utf-8','gbk',$str);
$num = strlen($str);
for($i=0;$i<$num;$i++)
{
if(ord(substr($str,$i+1,1))>127){
$count++;
$i++;
}
}
$str = iconv('gbk','utf-8',$str);
$total = mb_strlen($str,'utf8');
$number = ($total-$count)+($count*2);
return $number;
}
/**
* 改进addslashes
**/
function newaddslashes($string)
{
if(get_magic_quotes_gpc()) return $string;
if(!is_array($string)) return addslashes($string);
foreach($string as $key => $val) $string[$key] = newaddslashes($val);
return $string;
}
/**
* 改进stripslashes
**/
function newstripslashes($string)
{
if(!is_array($string)) return stripslashes($string);
foreach($string as $key => $val) $string[$key] = newstripslashes($val);
return $string;
}
function stripslashes_deep($value)
{
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
/**
* 改进array_filter
* 无返回值
**/
function new_arrayfilter(&$arr)
{
if(is_array($arr))
{
foreach( $arr as $k=>$v){
if(!is_array($v))
{
if(empty($v)) unset($arr[$k]);
}
else
{
new_arrayfilter($arr[$k]);
}
}
}
}
/**
* 二维数组过滤
* 只要有空值就过滤
*/
function ppFilter($arr){
if(is_array($arr)){
foreach($arr as $k => $v){
$count=count($v);
$new_v=array_filter($v);
if($count !== count($new_v)) unset($arr[$k]);
}
}
return $arr;
}
/**
* 汉字首字母
**/
function getfirstchar($s0){
$fchar = ord($s0{0});
if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
$s1 = iconv("UTF-8","gb2312", $s0);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $s0){$s = $s1;}else{$s = $s0;}
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if($asc >= -20319 and $asc <= -20284) return "A";
if($asc >= -20283 and $asc <= -19776) return "B";
if($asc >= -19775 and $asc <= -19219) return "C";
if($asc >= -19218 and $asc <= -18711) return "D";
if($asc >= -18710 and $asc <= -18527) return "E";
if($asc >= -18526 and $asc <= -18240) return "F";
if($asc >= -18239 and $asc <= -17923) return "G";
if($asc >= -17922 and $asc <= -17418) return "I";
if($asc >= -17417 and $asc <= -16475) return "J";
if($asc >= -16474 and $asc <= -16213) return "K";
if($asc >= -16212 and $asc <= -15641) return "L";
if($asc >= -15640 and $asc <= -15166) return "M";
if($asc >= -15165 and $asc <= -14923) return "N";
if($asc >= -14922 and $asc <= -14915) return "O";
if($asc >= -14914 and $asc <= -14631) return "P";
if($asc >= -14630 and $asc <= -14150) return "Q";
if($asc >= -14149 and $asc <= -14091) return "R";
if($asc >= -14090 and $asc <= -13319) return "S";
if($asc >= -13318 and $asc <= -12839) return "T";
if($asc >= -12838 and $asc <= -12557) return "W";
if($asc >= -12556 and $asc <= -11848) return "X";
if($asc >= -11847 and $asc <= -11056) return "Y";
if($asc >= -11055 and $asc <= -10247) return "Z";
return null;
}
function pinyin1($zh){
$ret = "";
$s1 = iconv("UTF-8","gb2312", $zh);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $zh){$zh = $s1;}
$s1 = substr($zh,0,1);
$p = ord($s1);
if($p > 160){
$s2 = substr($zh,$i++,2);
$ret = getfirstchar($s2);
}else{
$ret = $s1;
}
return $ret;
}
/* end */
/**
* 改进htmlspecialchars
* $quotes:ENT_COMPAT、ENT_QUOTES、ENT_NOQUOTES
**/
function newhtmlspecialchars($string,$quotes=ENT_QUOTES)
{
if(!is_array($string)) return htmlspecialchars($string,$quotes);
foreach($string as $key => $val) $string[$key] = newhtmlspecialchars($val,$quotes);
return $string;
}
/**
* 改进htmlspecialchars_decode
* $quotes:ENT_COMPAT、ENT_QUOTES、ENT_NOQUOTES
**/
function newhtmlspecialchars_decode($string,$quotes=ENT_QUOTES)
{
if(!is_array($string)) return htmlspecialchars_decode($string,$quotes);
foreach($string as $key => $val) $string[$key] = newhtmlspecialchars_decode($val,$quotes);
return $string;
}
/**
* 判断是否是安卓
**/
function isandroid(){
if(strstr($_SERVER['HTTP_USER_AGENT'],'Android')) {
return 1;
}
return 0;
}
/**
* 格式化文本域内容
* @param $string 文本域内容
* @return string
**/
function trim_textarea($string) {
$string = nl2br ( str_replace ( ' ', ' ', $string ) );
return $string;
}
/**
* 获取当前页面完整URL地址
**/
function get_url() {
$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
$php_self = $_SERVER['PHP_SELF'] ? safe_replace($_SERVER['PHP_SELF']) : safe_replace($_SERVER['SCRIPT_NAME']);
$path_info = isset($_SERVER['PATH_INFO']) ? safe_replace($_SERVER['PATH_INFO']) : '';
$relate_url = isset($_SERVER['REQUEST_URI']) ? safe_replace($_SERVER['REQUEST_URI']) : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.safe_replace($_SERVER['QUERY_STRING']) : $path_info);
return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
}
/**
* 将字符串转换为数组
* @param string $data 字符串
* @return array 返回数组格式,如果,data为空,则返回空数组
**/
function string2array($data) {
if($data == '') return array();
@eval("\$array = $data;");
return $array;
}
/**
* 将数组转换为字符串
*
* @param array $data 数组
* @param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
* @return string 返回字符串,如果,data为空,则返回空
**/
function array2string($data, $isformdata = 1) {
if($data == '') return '';
if($isformdata) $data = newstripslashes($data);
return addslashes(var_export($data, TRUE));
}
/**
* 多维数组转一维数组
* $tp是否保存键名 true,false
**/
function array2single($array,$tp=false)
{
// static $result_array;
$result_array=array();
foreach ($array as $k => $v)
{
if(is_array($v))
{
$result_array=array_merge($result_array,array2single($v,$tp));
}
else
{
if($tp)
$result_array[$k] = $v;
else
$result_array[] = $v;
}
}
return $result_array;
}
/**
* 数组转成字符串
* @param array $Array 数组
* @param $fg 分隔符
* @return string 返回字符串,如果,data为空,则返回空
**/
function marray2string($Array,$fg=',') {
$Return='';
$NullValue="";
if(!is_array($Array)) return $Array;
foreach ($Array as $Key => $Value) {
if(is_array($Value))
$ReturnValue=marray2string($Value,$fg);
else
$ReturnValue=(strlen($Value)>0)?$Value:$NullValue;
$Return.=$ReturnValue?$fg.$ReturnValue:'';
}
$Return=substr($Return,1);
return $Return;
}
/**
* 判断email格式是否正确
* @param $email
**/
function is_email($email) {
return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}
/**
* 生成随机字符串
* @param string $lenth 长度
* @return string 字符串
**/
function makeRand( $length = 6 )
{
// 密码字符集,可任意添加你需要的字符
$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', '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');
// 在 $chars 中随机取 $length 个数组元素键名
$keys = array_rand($chars, $length);
$password = '';
for($i = 0; $i < $length; $i++)
{
// 将 $length 个数组元素连接成字符串
$password .= $chars[$keys[$i]];
}
return $password;
}
/**
*
* 获取远程内容
* @param $url 接口url地址
* @param $timeout 超时时间
**/
function pc_file_get_contents($url, $timeout=30) {
$stream = stream_context_create(array('http' => array('timeout' => $timeout)));
return @file_get_contents($url, 0, $stream);
}
/**
* 下载文件
* @param string $file
* 被下载文件的路径
* @param string $name
* 用户看到的文件名
**/
function download($file,$name='')
{
$fileName = $name ? $name : pathinfo($file,PATHINFO_FILENAME);
$filePath = realpath($file);
$fp = fopen($filePath,'rb');
if(!$filePath || !$fp){
header('HTTP/1.1 404 Not Found');
echo "Error: 404 Not Found.(server file path error)<!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding --><!-- Padding -->";
exit;
}
$fileName = $fileName .'.'. pathinfo($filePath,PATHINFO_EXTENSION);
// $encoded_filename = urlencode($fileName);
// $encoded_filename = str_replace("+", "%20", $encoded_filename);
$encoded_filename=rawurlencode($fileName);
header('HTTP/1.1 200 OK');
header( "Pragma: public" );
header( "Expires: 0" );
header("Content-type: application/octet-stream");
header("Content-Length: ".filesize($filePath));
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($filePath));
$ua = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
} else {
header('Content-Disposition: attachment; filename="' . $fileName . '"');
}
// ob_end_clean(); <--有些情况可能需要调用此函数
// 输出文件内容
fpassthru($fp);
exit;
}
/**
* 判断是否是手机端
**/
function isMobile2(){
$useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
function CheckSubstrs($substrs,$text)
{
foreach($substrs as $substr)
{
if(false!==strpos($text,$substr)){
return true;
}
}
return false;
}
$mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
$mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
if (CheckSubstrs($mobile_os_list,$useragent_commentsblock) || CheckSubstrs($mobile_token_list,$useragent)){
return true;
}else{
return false;
}
}
function isMobile($url="")
{
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i";
if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
{
if (!empty($url))
{
header("Location: $url\n");
exit;
}
}
}
/**
* 导出数据为excel表格
*@param $data 一个二维数组,结构如同从数据库查出来的数组
*@param $title excel的第一行标题,一个数组,如果为空则没有标题
*@param $filename 下载的文件名
*@examlpe
*$stu = M ('User');
*$arr = $stu -> select();
*exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
*/
function exportexcel($data=array(),$title=array(),$filename='report')
{
header("Content-Type: applicationnd.ms-excel; charset=utf-8");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=".$filename.".xls ");
header("Content-Transfer-Encoding: binary ");
$map .="<table>";
if (!empty($title)){
$map .="<tr>";
foreach ($title as $k => $v) {
//$title[$k]=iconv("UTF-8", "GB2312",$v);
$map .="<td>".$title[$k]."</td>";
}
$map .="</tr>";
}
if (!empty($data)){
foreach($data as $key=>$val){
$map .="<tr>";
foreach ($val as $ck => $cv) {
// $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
$map .="<td>".$data[$key][$ck]."</td>";
}
$map .="</tr>";
}
$map .="</table>";
}
echo $map;
}
/**
* 汉字反转
**/
function str_reverse($string)
{
preg_match_all('/./us', $string, $array);
return implode('', array_reverse($array[0]));
}
/**
* 把一个字符串的中间部分用星号隐藏掉
*/
function replace_with_stars($str,$char='*')
{
if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $str))
{
$len=mb_strlen($str,'UTF-8');
if($len<3)
return $str;
elseif($len<5)
$slen=1;
elseif($len<7)
$slen=2;
elseif($len<9)
$slen=3;
elseif($len<13)
$slen=4;
elseif($len<16)
$slen=5;
elseif($len<19)
$slen=6;
else
$slen=7;
//if($len%2>0)
//$len-=1;
$pos=($len-$slen)%2==0?(($len-$slen)/2):(intval(($len-$slen)/2)+1);
$rt=mb_substr($str,0, $pos,'UTF-8').str_repeat($char, $slen).mb_substr($str, -1*($len-($pos+$slen)),$len,'UTF-8');
return $rt;
}
else
{
$len=strlen($str);
if($len<5)
return $str;
elseif($len<7)
$slen=2;
elseif($len<9)
$slen=3;
elseif($len<13)
$slen=4;
else
$slen=5;
//if($len%2>0)
//$len-=1;
$pos=($len-$slen)%2==0?(($len-$slen)/2):(intval(($len-$slen)/2)+1);
$rt=substr($str,0, $pos).str_repeat($char, $slen).substr($str, -1*($len-($pos+$slen)));
return $rt;
}
//if($len)
}
/**
* 计算字符串长度,汉字算2个字节
**/
function str_length($str)
{
return strlen(preg_replace('/[\x{4e00}-\x{9fa5}]/u', '**',$str));
}
/**
* 过滤脚本代码
**/
function cleanJs($text,$do_filter=false){
$text = trim($text);
$text = stripslashes($text);
//完全过滤动态代码
$text = preg_replace('/<\?|\?>/is','',$text);
//完全过滤js
$text = preg_replace('/<script?.*\/script>/is','',$text);
//过滤多余html
$text = preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/is','',$text);
//过滤on事件lang js
while(preg_match('/(<[^><]+)(lang|onload|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur)[^><]+/is',$text,$mat)){
$text=str_replace($mat[0],$mat[1],$text);
}
while(preg_match('/(onfinish|onload|onmouse[a-z]+|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur|expression)([=\(])/is',$text,$mat)){
$text=str_replace($mat[0],$mat[1].'#'.$mat[2],$text);
}
while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/is',$text,$mat)){
$text=str_replace($mat[0],$mat[1].$mat[3],$text);
}
$text = str_ireplace('script','sc#ipt',$text);
if($do_filter)
return GFW($text);
else
return $text;
}
/**
* 纯文本输出
**/
function ppt($text,$do_filter=false){
$text = cleanJs($text,$do_filter);
$text = strip_tags($text);
return $text;
}
/**
* 数组过滤脚本代码
**/
function tt($src,$do_filter=false)
{
if(!is_array($src))
return t($src,$do_filter);
$data=array();
foreach($src as $key=>$v)
{
if(is_array($v))
$data[$key]=tt($v,$do_filter);
else
$data[$key]=t($v,$do_filter);
}
return $data;
}
//输出安全的html
function h($text){
$text = cleanJs($text);
return $text;
}
//替换非法关键字
function GFW($text) {
$filter=S("InputFilter");
if(empty($filter))
{
$filter=array('find'=>array(),'replace'=>array());
$list=D("Filter")->findAll();
if($list){
foreach($list as $v)
{
$filter['find'][]=$v['find'];
$filter['replace'][]=empty($v['replacement'])?'**':$v['replacement'];
}
}
S('InputFilter',$filter,3600);
}
$text=@str_replace($filter['find'], $filter['replace'], $text);
return $text;
}
/**
* 数组 gb2312 转 utf8
**/
function gb2312ToUtf8(&$input)
{
if (!is_array($input))
{
$input = iconv('GB2312', 'UTF-8//IGNORE', $input);
}
else
{
foreach ($input as $k=>$v)
{
gb2312ToUtf8($input[$k]);
}
}
}
/**
* 字符串清除所有空格
**/
function trimAll($str)
{
$str = trim($str);// 首先去掉头尾空格
$str = str_replace(' ','',$str);
$str = preg_replace('/\s(?=\s)/','',$str);// 接着去掉两个空格以上的
$str = preg_replace('/[\n\r\t]/','',$str);// 最后将非空格替换为一个空格
return $str;
}
/**
* 判断是否中文
*/
function isChinese($s)
{
$allen = preg_match("/^[^\x80-\xff]+$/", $s); //判断是否是英文
$allcn = preg_match("/[\x7f-\xff]/",$s); //判断是否是中文
if($allen){
return false;
}else{
if($allcn){
return true;
}else{
return false;
}
}
}
/**
* 获取目录的结构
* @author 李俊
* @param [string] $path [目录路径]
* @return [array] [目录结构数组]
**/
function dirtree($path)
{
$handle = opendir($path);
$itemArray=array();
while (false !== ($file = readdir($handle)))
{
if (($file=='.')||($file=='..'))
{
}
elseif (is_dir($path.$file))
{
try{
$dirtmparr=dirtree($path.$file.'/');
} catch (Exception $e) {
$dirtmparr=null;
};
$itemArray[$file]=$dirtmparr;
}
else
{
array_push($itemArray, $file);
}
}
closedir($handle);
return $itemArray;
}
/**
* 目录遍历和计算文件个数
* @path 路径,支持相对和绝对
* @absolute 返回的文件数组,是否包含完整路径
*/
function get_files($path, $absolute=1)
{
$files = array();
$_path = realpath($path);
if (!file_exists($_path)) return false;
if (is_dir($_path))
{
$list = scandir($_path);
foreach ($list as $v)
{
if ($v == '.' || $v == '..') continue;
$_paths = $_path.'/'.$v;
//$_paths=str_replace('\\', '/', $_paths);
if (is_dir($_paths))
{
//递归
$files = array_merge($files, get_files($_paths,$absolute));
}
else
{
$files[] = $absolute>0 ? $_paths : $v;
}
}
}
else
{
if (!is_file($_path)) return false;
$files[] = $_path;
}
return $files;
}
/**
* 系统邮件发送函数
* @param string $to 接收邮件者邮箱
* @param string $name 接收邮件者名称
* @param string $subject 邮件主题
* @param string $body 邮件内容
* @param string $attachment 附件列表
* @return boolean
**/
function sendMail($to, $name, $subject = '', $body = '', $attachment = null)
{
$config = C('THINK_EMAIL');
vendor('PHPMailer.class#phpmailer'); //从PHPMailer目录导class.phpmailer.php类文件
$mail = new PHPMailer(); //PHPMailer对象
$mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码
$mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPDebug = 0; // 关闭SMTP调试功能
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
// $mail->SMTPSecure = 'ssl'; // 使用安全协议
$mail->Host = $config['SMTP_HOST']; // SMTP 服务器
$mail->Port = $config['SMTP_PORT']; // SMTP服务器的端口号
$mail->Username = $config['SMTP_USER']; // SMTP服务器用户名
$mail->Password = $config['SMTP_PASS']; // SMTP服务器密码
$mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);
$replyEmail = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];
$replyName = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];
$mail->AddReplyTo($replyEmail, $replyName);
$mail->Subject = $subject;
$mail->MsgHTML($body);
$mail->AddAddress($to, $name);
if(is_array($attachment)){ // 添加附件
foreach ($attachment as $file){
is_file($file) && $mail->AddAttachment($file);
}
}
return $mail->Send() ? true : $mail->ErrorInfo;
}
function Email($to,$subject = '', $body = '')
{
$config = C('THINK_EMAIL');
dump($config);
import('ORG.Util.Smtp');
$smtpserver = $config['SMTP_HOST'];//SMTP服务器
$smtpserverport =$config['SMTP_PORT'];//SMTP服务器端口
$smtpusermail = $config['SMTP_USER'];//SMTP服务器的用户邮箱//163邮箱等
$smtpemailto = $to;//发送给谁
$smtpuser = $config['SMTP_USER'];//SMTP服务器的用户帐号
$smtppass = $config['SMTP_PASS'];//SMTP服务器的用户密码
$mailsubject =$subject;//邮件主题
$mailbody =$body;
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
//$smtp->debug = TRUE;//是否显示发送的调试信息
$data=$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
return $data;
}
?>