forked from lincanbin/Carbon-Forum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
836 lines (768 loc) · 23.7 KB
/
common.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
<?php
/*
* Carbon-Forum
* https://github.com/lincanbin/Carbon-Forum
*
* Copyright 2006-2017 Canbin Lin ([email protected])
* http://www.94cb.com/
*
* Licensed under the Apache License, Version 2.0:
* http://www.apache.org/licenses/LICENSE-2.0
*
* A high performance open-source forum software written in PHP.
*/
/*
逐渐替换为帕斯卡命名法
数据库从设计上避免使用Join多表联查
*/
define('CARBON_FORUM_VERSION', '5.9.0');
//Initialize timer
$StartTime = microtime(true);
$TimeStamp = intval($_SERVER['REQUEST_TIME']);
if ((@include __DIR__ . '/config.php') != 1) {
//Bring user to installation
header("Location: install/");
exit(); //No errors
}
require(LanguagePath . 'common.php');
//Initialize PHP Data Object(Database)
require(LibraryPath . 'PDO.class.php');
require(LibraryPath . 'WhiteHTMLFilterConfig.php');
require(LibraryPath . 'WhiteHTMLFilter.php');
$DB = new Db(DBHost, DBPort, DBName, DBUser, DBPassword);
//Initialize MemCache(d) / Redis
$MCache = false;
if (EnableMemcache) {
if (extension_loaded('memcached')) {
//MemCached
$MCache = new Memcached(MemCachePrefix . 'Cache');
//Using persistent memcached connection
if (!count($MCache->getServerList())) {
$MCache->addServer(MemCacheHost, MemCachePort);
}
} elseif (extension_loaded('memcache')) {
//MemCache
require(LibraryPath . "MemcacheMod.class.php");
$MCache = new MemcacheMod(MemCacheHost, MemCachePort);
} elseif (extension_loaded('redis')) {
//Redis
//https://github.com/phpredis/phpredis
$MCache = new Redis();
$MCache->pconnect(MemCacheHost, MemCachePort);
} elseif (extension_loaded('xcache')) {
// XCache
require(LibraryPath . "XCache.class.php");
$MCache = new XCache();
}
}
//Load configuration
$Config = array();
if ($MCache) {
$Config = $MCache->get(MemCachePrefix . 'Config');
}
if (!$Config) {
foreach ($DB->query('SELECT ConfigName,ConfigValue FROM ' . PREFIX . 'config') as $ConfigArray) {
$Config[$ConfigArray['ConfigName']] = $ConfigArray['ConfigValue'];
}
// Update
if ($Config['Version'] != CARBON_FORUM_VERSION) {
header("Location: update/"); // Bring user to installation
exit(); //No errors
}
if ($MCache) {
$MCache->set(MemCachePrefix . 'Config', $Config, 86400);
}
}
// 热门标签列表
$HotTagsArray = json_decode($Config['CacheHotTags'], true);
$HotTagsArray = $HotTagsArray ? $HotTagsArray : array();
$PHPSelf = addslashes(htmlspecialchars($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']));
$UrlPath = '';
//For IIS ISAPI_Rewrite
$RequestURI = str_ireplace('?' . (isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''), '', (isset($_SERVER['HTTP_X_REWRITE_URL']) ? $_SERVER['HTTP_X_REWRITE_URL'] : $_SERVER['REQUEST_URI']));
$IsAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
$CurProtocol = IsSSL() ? 'https://' : 'http://';
$_HEAD = array();
$_PUT = array();
$_DELETE = array();
$_OPTIONS = array();
//消除低版本中魔术引号的影响
if (version_compare(PHP_VERSION, '5.4.0') < 0 && get_magic_quotes_gpc()) {
function StripslashesDeep($var)
{
return is_array($var) ? array_map('StripslashesDeep', $var) : stripslashes($var);
}
$_GET = StripslashesDeep($_GET);
$_POST = StripslashesDeep($_POST);
$_COOKIE = StripslashesDeep($_COOKIE);
$_REQUEST = StripslashesDeep($_REQUEST);
}
// At某人并提醒他,使用时常在其前后加空格或回车,如 “@admin ”
function AddingNotifications($Content, $TopicID, $PostID, $FilterUser = '')
{
/*
Type:
1:新回复
2:@ 到我的
*/
global $DB, $MCache, $TimeStamp, $CurUserName;
$InTransaction = $DB->inTransaction();//是否处于其他事务之中
//例外列表
$ExceptionUser = array(
$CurUserName
);
if ($FilterUser != $CurUserName) {
$ExceptionUser[] = $FilterUser;
}
// 正则跟用户注册、登录保持一致
preg_match_all('/\B\@([a-zA-Z0-9\x80-\xff\-_]{4,20})/', strip_tags($Content, '<br><p>'), $out, PREG_PATTERN_ORDER);
$TemporaryUserList = array_unique($out[1]); //排重
$TemporaryUserList = array_diff($TemporaryUserList, $ExceptionUser);
//对数组重新分配下标
sort($TemporaryUserList);
if ($TemporaryUserList) {
try {
if (!$InTransaction) {
$DB->beginTransaction();
}
$UserList = $DB->row('SELECT ID FROM `' . PREFIX . 'users` WHERE `UserName` IN (?)', $TemporaryUserList);
if ($UserList && count($UserList) <= 20) {
//最多@ 20人,防止骚扰
foreach ($UserList as $UserID) {
$DB->query('INSERT INTO `' . PREFIX . 'notifications`(`ID`,`UserID`, `UserName`, `Type`, `TopicID`, `PostID`, `Time`, `IsRead`) VALUES (NULL,?,?,?,?,?,?,?)', array(
$UserID,
$CurUserName,
2,
$TopicID,
$PostID,
$TimeStamp,
0
));
$DB->query('UPDATE `' . PREFIX . 'users` SET `NewMention` = NewMention+1 WHERE ID = :UserID', array(
'UserID' => $UserID
));
//清理内存缓存
if ($MCache) {
$MCache->delete(MemCachePrefix . 'UserInfo_' . $UserID);
}
}
}
if (!$InTransaction) {
$DB->commit();
}
} catch (Exception $ex) {
if (!$InTransaction) {
$DB->rollBack();
} else {
throw $ex;
}
return false;
}
}
return true;
}
//提示信息
function AlertMsg($PageTitle, $Error, $StatusCode = 200)
{
global $Lang, $CurProtocol, $RequestURI, $UrlPath, $IsAjax, $IsMobile, $IsApp, $DB, $Config, $HotTagsArray, $CurUserID, $CurUserName, $CurUserCode, $CurUserRole, $CurUserInfo, $FormHash, $StartTime, $PageMetaKeyword, $TemplatePath;
$HttpStatuses = [
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
118 => 'Connection timed out',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
208 => 'Already Reported',
210 => 'Content Different',
226 => 'IM Used',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Reserved',
307 => 'Temporary Redirect',
308 => 'Permanent Redirect',
310 => 'Too many Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested range unsatisfiable',
417 => 'Expectation failed',
418 => 'I\'m a teapot',
422 => 'Unprocessable entity',
423 => 'Locked',
424 => 'Method failure',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
431 => 'Request Header Fields Too Large',
449 => 'Retry With',
450 => 'Blocked by Windows Parental Controls',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway or Proxy Error',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
507 => 'Insufficient storage',
508 => 'Loop Detected',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not Extended',
511 => 'Network Authentication Required',
];
$Protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1');
if (!$IsAjax && !empty($HttpStatuses[$StatusCode])) {
// http_response_code($StatusCode);
header($Protocol . ' ' . $StatusCode . ' ' . $HttpStatuses[$StatusCode]);
}
$ContentFile = $TemplatePath . 'alert.php';
include($TemplatePath . 'layout.php');
exit;
}
//获取数组中的某一列
function ArrayColumn($Input, $ColumnKey)
{
if (version_compare(PHP_VERSION, '5.5.0') < 0) {
$Result = array();
if ($Input) {
foreach ($Input as $Value) {
$Result[] = $Value[$ColumnKey];
}
}
return $Result;
} else {
return array_column($Input, $ColumnKey);
}
}
//鉴权
function Auth($MinRoleRequire, $AuthorizedUserID = 0, $StatusRequire = false)
{
global $CurUserRole, $CurUserID, $CurUserInfo, $Lang, $RequestURI;
$error = '';
if ($CurUserRole < $MinRoleRequire) {
$error = str_replace('{{RoleDict}}', $Lang['RolesDict'][$MinRoleRequire], $Lang['Error_Insufficient_Permissions']);
}
if ($CurUserID && $StatusRequire == true && $CurUserInfo['UserAccountStatus'] == 0) {
$error = $Lang['Error_Account_navailable'];
}
if ($AuthorizedUserID && $CurUserID && $CurUserID == $AuthorizedUserID) {
$error = false;
}
if ($error) {
AlertMsg($Lang['Error_Message'], $error, 401);
}
}
//转换字符
function CharCV($string)
{
$string = htmlspecialchars(trim($string));
return $string;
}
// 过滤掉一些非法字符
function CharsFilter($String)
{
$String = str_replace("<", "", $String);
$String = str_replace(">", "", $String);
return trim($String);
}
// 获得IP地址
function CurIP()
{
$IsCDN = false; //未使用CDN时,应直接使用 $_SERVER['REMOTE_ADDR'] 以防止客户端伪造IP
$IP = false;
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$IP = trim($_SERVER["HTTP_CLIENT_IP"]);
}
if ($IsCDN && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$IPs = array_map("trim", explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']));
if ($IP) {
array_unshift($IPs, $IP);//插入头部而不是尾部,提升性能
$IP = FALSE;
}
//支持使用CDN后获取IP,理论上令 $IP = $IPs[0]; 即可,安全起见遍历过滤一次
foreach ($IPs as $Key => $Value) {
/*
Fails validation for the following private IPv4 ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16.
Fails validation for the IPv6 addresses starting with FD or FC.
*/
if (filter_var($Value, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
$IP = $Value;
break;
}
}
}
return htmlspecialchars($IP ? $IP : $_SERVER['REMOTE_ADDR']);
}
//关键词过滤
function Filter($Content)
{
if (is_file(LibraryPath . 'Filtering.words.config.json')) {
$FilteringWords = JsonDecode(file_get_contents(LibraryPath . 'Filtering.words.config.json'));
} else {
$FilteringWords = JsonDecode(file_get_contents(LibraryPath . 'Filtering.words.config.template.json'));
}
$Prohibited = false;
$GagTime = 0;
foreach ($FilteringWords as $SearchRegEx => $Rule) {
if (preg_match_all("/" . $SearchRegEx . "/i", $Content, $SearchWordsList)) {
//var_dump($SearchWordsList);
foreach ($SearchWordsList as $SearchWord) {
if (is_array($Rule)) {
$Content = str_ireplace($SearchWord, $Rule[0], $Content);
$Prohibited |= ($Rule[0] === false);
$GagTime = ($Rule[1] > $GagTime) ? $Rule[1] : $GagTime; //将规则中封禁时间最长的一个赏给用户
} else {
$Content = str_ireplace($SearchWord, $Rule, $Content);
//$Prohibited |= false;
//$GagTime = 0;
}
}
}
}
return array(
'Content' => $Content, //过滤后的内容
'Prohibited' => $Prohibited, //是否包含有禁止发布的词
'GagTime' => $GagTime //赏赐给用户的禁言时间(秒)
);
}
// 获得表单校验散列
function FormHash()
{
global $Config;
if (GetCookie('UserCode'))
return substr(md5($Config['SiteName'] . GetCookie('UserCode') . SALT), 8, 8);
else
return substr(md5($Config['SiteName'] . SALT), 8, 8);
}
//格式化文件大小
function FormatBytes($size, $precision = 2)
{
// https://www.zhihu.com/question/21578998/answer/86401223
// According to Metric prefix, IEEE 1541-2002.
$units = array(
' Bytes',
' KiB',
' MiB',
' GiB',
' TiB'
);
for ($i = 0; $size >= 1024 && $i < 4; $i++)
$size /= 1024;
return round($size, $precision) . $units[$i];
}
// 格式化时间
function FormatTime($UnixTimeStamp)
{
global $Lang;
$Seconds = $_SERVER['REQUEST_TIME'] - $UnixTimeStamp;
if ($Seconds < 2592000) {
// 小于30天如下显示
if ($Seconds >= 86400) {
return round($Seconds / 86400, 0) . ' ' . $Lang['Time_Days_Ago'];
} else if ($Seconds >= 3600) {
return round($Seconds / 3600, 0) . ' ' . $Lang['Time_Hours_Ago'];
} else if ($Seconds >= 60) {
return round($Seconds / 60, 0) . ' ' . $Lang['Time_Minutes_Ago'];
} else if ($Seconds < 0) {
return $Lang['Time_Just_Now'];
} else {
return ($Seconds + 1) . ' ' . $Lang['Time_Seconds_Ago'];
}
} else {
// 大于一月
return date("Y-m-d", $UnixTimeStamp);
//gmdate()可以返回格林威治标准时,date()则为当地时
}
}
//获取头像
function GetAvatar($UserID, $UserName, $Size = 'middle')
{
global $Config;
return '<img src="' . $Config['WebsitePath'] . '/upload/avatar/' . $Size . '/' . $UserID . '.png" alt="' . $UserName . '"/>';
}
//获取Tag标签
function GetTagIcon($TagID, $Icon, $TagName, $Size = 'middle')
{
global $Config;
return '<img src="' . $Config['WebsitePath'] . '/upload/tag/' . $Size . '/' . ($Icon ? $TagID : '0') . '.png" alt="' . $TagName . '"/>';
}
//获取Cookie
function GetCookie($Key, $DefaultValue = false)
{
global $Config, $IsApp;
if (!$IsApp) {
if (!empty($_COOKIE[$Config['CookiePrefix'] . $Key])) {
return $_COOKIE[$Config['CookiePrefix'] . $Key];
} else if ($DefaultValue) {
SetCookies(array(
$Key => $DefaultValue
));
return $DefaultValue;
}
} else {
return Request("Request", "Auth" . $Key, $DefaultValue);
}
return false;
}
//Hash值校验,防止时序攻击法
function HashEquals($KnownString, $UserString)
{
if (version_compare(PHP_VERSION, '5.6.0') < 0) {
return ($KnownString === $UserString);
} else {
return hash_equals($KnownString, $UserString);
}
}
//长整数intval,防止溢出,目前暂未用到
function Int($s)
{
return ($a = preg_replace('/[^\-\d]*(\-?\d*).*/', '$1', $s)) ? $a : '0';
}
//判断是否为邮件地址
function IsEmail($email)
{
return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}
function IsJson($String)
{
JsonDecode($String);
return (json_last_error() === JSON_ERROR_NONE);
}
//判断是否为合法用户名
function IsName($string)
{
return !preg_match('/^[0-9]{4,20}$/', $string) && preg_match('/^[a-zA-Z0-9\x{4e00}-\x{9fa5}\-_]{4,20}$/ui', $string);
}
//判断当前协议
function IsSSL()
{
if (!isset($_SERVER['HTTPS']))
return false;
if ($_SERVER['HTTPS'] === 1) { //Apache
return true;
} elseif ($_SERVER['HTTPS'] === 'on') { //IIS
return true;
} elseif ($_SERVER['SERVER_PORT'] == 443) { //其他
return true;
}
return false;
}
// 去除注释的JsonDecode
function JsonDecode($Json)
{
return json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", $Json), true);
}
//登出
function LogOut()
{
global $CurUserID;
SetCookies(array(
'UserID' => '',
'UserExpirationTime' => '',
'UserCode' => ''
), 1);
$CurUserID = 0;
}
//跳转
function Redirect($URI = '', $ExitCode = 0)
{
global $Config;
header('location: ' . $Config['WebsitePath'] . '/' . $URI);
exit($ExitCode);
}
//来源检查
function ReferCheck($UserHash)
{
global $IsApp;
if (!$IsApp && (empty($_SERVER['HTTP_REFERER']) || $UserHash != FormHash() || preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) !== preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])))
return false;
else
return true;
}
//表单获取
function Request($Type, $Key, $DefaultValue = '')
{
global $_PUT, $_DELETE, $_OPTIONS;
switch ($Type) {
case 'Get':
return isset($_GET[$Key]) ? trim($_GET[$Key]) : $DefaultValue;
break;
case 'Post':
return isset($_POST[$Key]) ? trim($_POST[$Key]) : $DefaultValue;
break;
case 'Put':
return isset($_PUT[$Key]) ? trim($_PUT[$Key]) : $DefaultValue;
break;
case 'Delete':
return isset($_DELETE[$Key]) ? trim($_DELETE[$Key]) : $DefaultValue;
break;
case 'Options':
return isset($_OPTIONS[$Key]) ? trim($_OPTIONS[$Key]) : $DefaultValue;
break;
default:
return isset($_REQUEST[$Key]) ? trim($_REQUEST[$Key]) : $DefaultValue;
break;
}
}
//设置工作模式为API模式(返回Json格式数据)
//SetStyle('api','API');
function SetStyle($PathName, $StyleName)
{
global $IsApp, $TemplatePath, $Style;
if ($StyleName = 'API') {
$IsApp = true;
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=utf-8');
}
$TemplatePath = __DIR__ . '/view/' . $PathName . '/';
$Style = $StyleName;
}
//批量设置Cookie
function SetCookies($CookiesArray, $Expires = 0)
{
global $TimeStamp, $Config;
foreach ($CookiesArray as $key => $value) {
if (!$Expires)
setcookie($Config['CookiePrefix'] . $key, $value, 0, $Config['WebsitePath'] . '/', null, false, true);
else
setcookie($Config['CookiePrefix'] . $key, $value, $TimeStamp + 86400 * $Expires, $Config['WebsitePath'] . '/', null, false, true);
}
}
//大小写不敏感的array_diff
function TagsDiff($Arr1, $Arr2)
{
global $Config;
$Arr2 = array_change_key_case(array_flip($Arr2), CASE_LOWER); //flip,排重,Key有Hash索引,速度更快
foreach ($Arr1 as $Key => $Item) {
if (mb_strlen($Item, "UTF-8") > $Config["MaxTagChars"] || isset($Arr2[strtolower(trim($Item))]) || strpos("|", $Item) || !preg_match('/^[a-zA-Z0-9\x80-\xff\-_\s]{1,' . $Config['MaxTagChars'] . '}$/i', $Item) || $Item != Filter($Item)['Content']) {
unset($Arr1[$Key]);
} else {
$Arr1[$Key] = htmlspecialchars(trim($Arr1[$Key])); //XSS
}
}
sort($Arr1);
return $Arr1;
}
//修改系统设置
function UpdateConfig($NewConfig)
{
global $DB, $Config, $MCache;
if ($NewConfig) {
foreach ($NewConfig as $Key => $Value) {
$DB->query("UPDATE `" . PREFIX . "config` SET ConfigValue=? WHERE ConfigName=?", array(
$Value,
$Key
));
$Config[$Key] = $Value;
}
if ($MCache) {
$MCache->set(MemCachePrefix . 'Config', $Config, 86400);
}
return true;
} else {
return false;
}
}
//修改用户资料
function UpdateUserInfo($NewUserInfo, $UserID = 0)
{
global $DB, $CurUserID, $CurUserInfo, $MCache;
if ($UserID == 0) {
$UserID = $CurUserID;
}
if ($NewUserInfo) {
$StringBindParam = '';
foreach ($NewUserInfo as $Key => $Value) {
$StringBindParam .= $Key . ' = :' . $Key . ',';
}
$StringBindParam = substr($StringBindParam, 0, -1);
$Result = $DB->query('UPDATE `' . PREFIX . 'users` SET ' . $StringBindParam . ' WHERE ID = :UserID', array_merge($NewUserInfo, array(
'UserID' => $UserID
)));
if ($MCache) {
$MCache->set(MemCachePrefix . 'UserInfo_' . $UserID, $DB->row("SELECT *, (NewReply + NewMention + NewMessage) as NewNotification FROM " . PREFIX . "users WHERE ID = :UserID", array(
"UserID" => $UserID
)), 86400);
}
return $Result;
} else {
return false;
}
}
//跨站脚本白名单过滤
function XssEscape($html)
{
$filter = new WhiteHTMLFilter();
$urlFilter = function($url) {
$token = parse_url($url);
if (empty($token['scheme']) || in_array($token['scheme'], array('http', 'https')) === false) {
return '';
}
$hostWhiteList = array(
'www.youtube.com', 'youtube.com', 'www.youtu.be', 'youtu.be',
'player.youku.com', 'v.youku.com',
'video.tudou.com', 'www.tudou.com',
'player.video.qiyi.com', 'open.iqiyi.com',
'imgcache.qq.com', 'v.qq.com',
'static.hdslb.com',
//'www.le.com',
'share.vrs.sohu.com', 'tv.sohu.com',
'player.pptv.com',
'cdn.aixifan.com',
'v.ifeng.com',
'video.sina.com.cn',
'galaxy.bjcathay.com'//CNTV
);
if (empty($token['host']) || in_array($token['host'], $hostWhiteList) === false) {
return '';
}
return $url;
};
$iframeRule = array(
'iframe' => array(
'src' => $urlFilter,
'width',
'height',
'frameborder',
'allowfullscreen'
)
);
$filter->config->modifyTagWhiteList($iframeRule);
$filter->loadHTML($html);
$filter->clean();
return $filter->outputHtml();
}
$UserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
if ($UserAgent) {
$IsSpider = preg_match('/(bot|crawl|spider|slurp|sohu-search|lycos|robozilla|google)/i', $UserAgent);
$IsMobile = preg_match('/(iPod|iPhone|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP)/i', $UserAgent);
} else {
//exit('error: 400 no agent');
$IsSpider = false;
$IsMobile = false;
}
$IsApp = $_SERVER['HTTP_HOST'] == $Config['AppDomainName'] ? true : false;
/* Set current template
* default: PC Version
* mobile: Mobile Version
* api: API
*/
if ($IsApp) {
$TemplatePath = __DIR__ . '/view/api/';
$Style = 'API';
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
//API鉴权
$SignatureKey = Request("Request", "SKey");
$SignatureValue = Request("Request", "SValue");
$SignatureTime = intval(Request("Request", "STime"));
if (!$SignatureTime || !$SignatureKey || !$SignatureValue || empty($APISignature[$SignatureKey]) || abs($SignatureTime - $TimeStamp) > 600 || !HashEquals($SignatureValue, md5($SignatureKey . $APISignature[$SignatureKey] . $SignatureTime))) {
AlertMsg('403', 'Forbidden', 403);
}
} elseif ($_SERVER['HTTP_HOST'] == $Config['MobileDomainName'] || (!$Config['MobileDomainName'] && $IsMobile)) {
$TemplatePath = __DIR__ . '/view/mobile/';
$Style = 'Mobile';
header('X-Frame-Options: SAMEORIGIN');
} else {
$TemplatePath = __DIR__ . '/view/default/';
$Style = 'Default';
header('X-Frame-Options: SAMEORIGIN');
//header('X-XSS-Protection: 1; mode=block');
//X-XSS-Protection may cause some issues in dashboard
}
$CurView = GetCookie('View', $IsMobile ? 'mobile' : 'desktop');
$CurIP = CurIP();
$FormHash = FormHash();
// 限制不能打开.php的网址
if (strpos($RequestURI, '.php')) {
AlertMsg('403', 'Forbidden', 403);
}
$CurrentDate = date('Y-m-d');
if ($Config['DaysDate'] != $CurrentDate) {
if (!strtotime($Config['DaysDate'])) {
$Config['DaysDate'] = $CurrentDate;
}
$DB->query('INSERT INTO `' . PREFIX . 'statistics`
(`DaysUsers`, `DaysPosts`, `DaysTopics`, `TotalUsers`, `TotalPosts`, `TotalTopics`, `DaysDate`, `DateCreated`)
SELECT
:DaysUsers, :DaysPosts, :DaysTopics, :TotalUsers, :TotalPosts, :TotalTopics, :DaysDate, :DateCreated
FROM dual
WHERE NOT EXISTS(
SELECT * FROM `' . PREFIX . 'statistics`
WHERE DaysDate = :DaysDate2
)
', array(
'DaysUsers' => $Config['DaysUsers'],
'DaysPosts' => $Config['DaysPosts'],
'DaysTopics' => $Config['DaysTopics'],
'TotalUsers' => $Config['NumUsers'],
'TotalPosts' => $Config['NumPosts'],
'TotalTopics' => $Config['NumTopics'],
'DaysDate' => $Config['DaysDate'],
'DateCreated' => $TimeStamp,
'DaysDate2' => $Config['DaysDate']
));
UpdateConfig(array(
'DaysDate' => $CurrentDate,
'DaysTopics' => 0,
'DaysPosts' => 0,
'DaysUsers' => 0,
'CacheHotTags' => json_encode($DB->query('SELECT ID,Name,Icon,TotalPosts,Followers FROM ' . PREFIX . 'tags
WHERE IsEnabled=1 AND TotalPosts>0
ORDER BY TotalPosts DESC
LIMIT ' . $Config['TopicsPerPage']))
));
}
// Get the infomation of current user
$CurUserInfo = null; //当前用户信息,Array,以后判断是否登陆使用if($CurUserID)
$CurUserRole = 0;
$CurUserID = intval(GetCookie('UserID'));
$CurUserName = '';
$CurUserExpirationTime = intval(GetCookie('UserExpirationTime'));
$CurUserCode = GetCookie('UserCode');
if ($CurUserExpirationTime > $TimeStamp && $CurUserExpirationTime < ($TimeStamp + 2678400) && $CurUserID && $CurUserCode) {
$TempUserInfo = array();
if ($MCache) {
$TempUserInfo = $MCache->get(MemCachePrefix . 'UserInfo_' . $CurUserID);
}
if (empty($TempUserInfo)) {
$TempUserInfo = $DB->row("SELECT *, (NewReply + NewMention + NewMessage) as NewNotification FROM " . PREFIX . "users WHERE ID = :UserID", array(
"UserID" => $CurUserID
));
if ($MCache && $TempUserInfo) {
$MCache->set(MemCachePrefix . 'UserInfo_' . $CurUserID, $TempUserInfo, 86400);
}
}
if ($TempUserInfo && HashEquals(md5($TempUserInfo['Password'] . $TempUserInfo['Salt'] . $CurUserExpirationTime . SALT), $CurUserCode)) {
$CurUserName = $TempUserInfo['UserName'];
$CurUserRole = $TempUserInfo['UserRoleID'];
$CurUserInfo = $TempUserInfo;
} else {
LogOut();
}
unset($TempUserInfo);
} elseif ($CurUserExpirationTime || $CurUserID || $CurUserCode) {
LogOut();
}