[二次开发]Discuz! X2 核心类(class_core.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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 |
<?php /** * [Discuz!] (C)2001-2099 Comsenz Inc. * This is NOT a freeware, use is subject to license terms * * $Id: class_core.php 21271 2011-03-22 02:44:58Z congyushuai $ */ define('IN_DISCUZ', true); error_reporting(0); class discuz_core { var $db = null; var $mem = null; var $session = null; var $config = array(); var $var = array(); var $cachelist = array(); var $init_setting = true; var $init_user = true; var $init_session = true; var $init_cron = true; var $init_misc = true; var $init_memory = true; var $init_mobile = true; var $initated = false; //初始化工作未完成标志 var $superglobal = array( 'GLOBALS' => 1, '_GET' => 1, '_POST' => 1, '_REQUEST' => 1, '_COOKIE' => 1, '_SERVER' => 1, '_ENV' => 1, '_FILES' => 1, ); function &instance() { static $object; if(empty($object)) { $object = new discuz_core(); } return $object; } function discuz_core() { $this->_init_env(); //初始化环境变量 $this->_init_config(); //初始化配置变量 $this->_init_input(); //初始化输入 $this->_init_output(); //初始化输出 } function init() { if(!$this->initated) { $this->_init_db(); //初始化数据库 $this->_init_memory(); //初始化memcache $this->_init_user(); //用户信息初始化 $this->_init_session(); //session操作初始化 $this->_init_setting(); //系统设置初始化 $this->_init_mobile(); //手机功能初始化 $this->_init_cron(); //计划任务初始化 $this->_init_misc(); //其他功能初始化 } $this->initated = true; //设置完成标志 }<span id="more-664"></span> function _init_env() { //环境变量初始化方法 error_reporting(E_ERROR); //定义错误报告等级 if(phpversion() < '5.3.0') { set_magic_quotes_runtime(0); //设置set_magic_quotes_runtime } define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -12)); //定义根目录常量 define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()); //定义MAGIC_QUOTES_GPC define('ICONV_ENABLE', function_exists('iconv')); //定义转码函数常量,如果 iconv函数支持,则为TRUE define('MB_ENABLE', function_exists('mb_convert_encoding')); //转码函数是否支持 define('EXT_OBGZIP', function_exists('ob_gzhandler')); //缓存输出句柄函数 define('TIMESTAMP', time()); //定义当前时间戳常量 $this->timezone_set(); if(!defined('DISCUZ_CORE_FUNCTION') && !@include(DISCUZ_ROOT.'./source/function/function_core.php')) { exit('function_core.php is missing'); //如果系统核心函数库文件未include,则include,如果核心函数库文件缺失,则退出,因为function_core.php是系统本身的函数库,必须加载 } if(function_exists('ini_get')) { $memorylimit = @ini_get('memory_limit'); if($memorylimit && return_bytes($memorylimit) < 33554432 && function_exists('ini_set')) { ini_set('memory_limit', '128m'); } } //以上6行代码的作用是设置内存使用限制,如果小于32M,则增加为128M,因为memory_limit小于32M,可能会不够用,毕竟X系统比较大,可能内存会使用的多点,如果不支持ini_set函数,就得去php.ini中修改了。 define('IS_ROBOT', checkrobot()); //检测机器人 foreach ($GLOBALS as $key => $value) { if (!isset($this->superglobal[$key])) { $GLOBALS[$key] = null; unset($GLOBALS[$key]); } } //以上几行的意思是,注销所有的超级变量。 global $_G; //$_G大数组是Discuz中自定义的超级变量 $_G = array( 'uid' => 0, //uid 'username' => '', //用户名 'adminid' => 0, //adminid标识 'groupid' => 1, //用户组ID 'sid' => '', // sessionID 'formhash' => '', //表单验证认证 'timestamp' => TIMESTAMP, //时间戳 'starttime' => dmicrotime(), //开始时间 'clientip' => $this->_get_client_ip(), //客户端IP 'referer' => '', //referer地址 'charset' => '', //字符串编码 'gzipcompress' => '', //gzip 'authkey' => '', //authkey 认证码 'timenow' => array(), //当前时间 'PHP_SELF' => '', //PHP_SELF 'siteurl' => '', //网站地址 'siteroot' => '', //网站根目录 'config' => array(), //配置变量数组 'setting' => array(), //设置变量数组 'member' => array(), //用户信息数组 'group' => array(), //用户组数组 'cookie' => array(), //cookie数组 'style' => array(), //风格数组 'cache' => array(), //缓存列表数组 'session' => array(), //session变量数组 'lang' => array(), //语言包数组 'my_app' => array(), //我的应用数组 'my_userapp' => array(), //用户应用数组 'fid' => 0, 'tid' => 0, 'forum' => array(), //论坛板块数组 'thread' => array(), //论坛相关帖子数组 'rssauth' => '', //RSS订阅认证 'home' => array(), //home功能相关数组 'space' => array(), //space功能相关数组 'block' => array(), //块信息数组 'article' => array(), 'action' => array( 'action' => APPTYPEID, 'fid' => 0, 'tid' => 0, ), 'mobile' => '', //手机信息 ); //以上定义了$_G超级变量的部分内容。都是系统以后要用到的。写在一个大数组中,方便使用 $_G['PHP_SELF'] = htmlspecialchars($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF']); //当前脚本地址写入$_G超级变量中 $_G['basescript'] = CURSCRIPT; //当前脚本 $_G['basefilename'] = basename($_G['PHP_SELF']); //当前脚本名称 $_G['siteurl'] = htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api)?\/*$/i", '', substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], '/'))).'/');//网站地址 $_G['siteroot'] = substr($_G['PHP_SELF'], 0, -strlen($_G['basefilename']));//网站根地址 if(defined('SUB_DIR')) { //二级目录设置 $_G['siteurl'] = str_replace(SUB_DIR, '/', $_G['siteurl']); $_G['siteroot'] = str_replace(SUB_DIR, '/', $_G['siteroot']); } $this->var = & $_G; } function _init_input() { //输入初始化方法 if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) || isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) { system_error('request_tainting'); } if(!MAGIC_QUOTES_GPC) { $_GET = daddslashes($_GET); $_POST = daddslashes($_POST); $_COOKIE = daddslashes($_COOKIE); $_FILES = daddslashes($_FILES); } //以上代码未GPC安全机制,进行转义 $prelength = strlen($this->config['cookie']['cookiepre']); foreach($_COOKIE as $key => $val) { if(substr($key, 0, $prelength) == $this->config['cookie']['cookiepre']) { $this->var['cookie'][substr($key, $prelength)] = $val; } } //以上代码意思是,如果cookie的键值等于定义的键值,那么截取cookiepre $_GET['diy'] = empty($_GET['diy']) ? '' : $_GET['diy']; if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) { $_GET = array_merge($_GET, $_POST); } //合并$_POST,$_GET foreach($_GET as $k => $v) { $this->var['gp_'.$k] = $v; } //然后把$_POST和$_GET的值都赋予gp变量中,方便使用 $this->var['mod'] = empty($this->var['gp_mod']) ? '' : htmlspecialchars($this->var['gp_mod']); //获得$mod变量 ?mod=xxx,则$this->var['mod']为xxx $this->var['inajax'] = empty($this->var['gp_inajax']) ? 0 : (empty($this->var['config']['output']['ajaxvalidate']) ? 1 : ($_SERVER['REQUEST_METHOD'] == 'GET' && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || $_SERVER['REQUEST_METHOD'] == 'POST' ? 1 : 0)); //是否需要ajax方式 $this->var['page'] = empty($this->var['gp_page']) ? 1 : max(1, intval($this->var['gp_page'])); //页面获取,最小为1 $this->var['sid'] = $this->var['cookie']['sid'] = isset($this->var['cookie']['sid']) ? htmlspecialchars($this->var['cookie']['sid']) : ''; //sid获取 } function _init_config() { //获得配置文件 $_config = array(); @include DISCUZ_ROOT.'./config/config_global.php';//加载全局配置文件 if(empty($_config)) { if(!file_exists(DISCUZ_ROOT.'./data/install.lock')) { header('location: install'); exit; } else { system_error('config_notfound'); } } //以上代码为:如果config不存在,要么没安装,要么文件不存在,系统报错 if(empty($_config['security']['authkey'])) { $_config['security']['authkey'] = md5($_config['cookie']['cookiepre'].$_config['db'][1]['dbname']); } //设置安全验证的authkey if(empty($_config['debug']) || !file_exists(libfile('function/debug'))) { define('DISCUZ_DEBUG', false); } elseif($_config['debug'] === 1 || $_config['debug'] === 2 || !empty($_REQUEST['debug']) && $_REQUEST['debug'] === $_config['debug']) { define('DISCUZ_DEBUG', true); if($_config['debug'] == 2) { error_reporting(E_ALL); } } else { define('DISCUZ_DEBUG', false); } //以上代码为是否调试模式, define('STATICURL', !empty($_config['output']['staticurl']) ? $_config['output']['staticurl'] : 'static/'); //定义静态文件常量,如:css,img等,如果$_config['output']['staticurl']为空,则是默认的static目录,就是默认目录 $this->var['staticurl'] = STATICURL; $this->config = & $_config; $this->var['config'] = & $_config; //引用$_config变量,改变$this->config,则$_config改变,保持统一 if(substr($_config['cookie']['cookiepath'], 0, 1) != '/') { $this->var['config']['cookie']['cookiepath'] = '/'.$this->var['config']['cookie']['cookiepath']; } //设置cookie域,一般是设置目录域。/不存在则加上/,安全性更高 $this->var['config']['cookie']['cookiepre'] = $this->var['config']['cookie']['cookiepre'].substr(md5($this->var['config']['cookie']['cookiepath'].'|'.$this->var['config']['cookie']['cookiedomain']), 0, 4).'_'; //定义cookie前缀,如定义为xxx_;则为$cookie['xxx_uid] $this->var['authkey'] = md5($_config['security']['authkey'].$_SERVER['HTTP_USER_AGENT']); //得到authkey } function _init_output() { //输出初始化方法 if($this->config['security']['urlxssdefend'] && $_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_SERVER['REQUEST_URI'])) { $this->_xss_check(); } if($this->config['security']['attackevasive'] && (!defined('CURSCRIPT') || !in_array($this->var['mod'], array('seccode', 'secqaa', 'swfupload')))) { require_once libfile('misc/security', 'include'); } //验证码设置,加载include/misc/misc_security.php文件,验证功能; if(!empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) { $this->config['output']['gzip'] = false; } //是否开启gzip,如果不支持gzip,则定义为false $allowgzip = $this->config['output']['gzip'] && empty($this->var['inajax']) && $this->var['mod'] != 'attachment' && EXT_OBGZIP; setglobal('gzipcompress', $allowgzip); //把$allowgzip写入全局变量中 ob_start($allowgzip ? 'ob_gzhandler' : null); //定义输出缓存 setglobal('charset', $this->config['output']['charset']); //把配置文件中的字符集赋予全局变量中 define('CHARSET', $this->config['output']['charset']); if($this->config['output']['forceheader']) { @header('Content-Type: text/html; charset='.CHARSET);//设置网页编码,强制输出 } } function reject_robot() { //拒绝机器人访问,设置为403错误 if(IS_ROBOT) { exit(header("HTTP/1.1 403 Forbidden")); } } function _xss_check() { //检测xss漏洞,UBB $temp = strtoupper(urldecode(urldecode($_SERVER['REQUEST_URI']))); if(strpos($temp, '<') !== false || strpos($temp, '"') !== false || strpos($tmp,'CONTENT-TRANSFER-ENCODING')!==false) { system_error('request_tainting'); } return true; } function _get_client_ip() { //得到客户端IP $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) { foreach ($matches[0] AS $xip) { if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) { $ip = $xip; break; } } } return $ip; } function _init_db() { //初始化数据库 $class = 'db_mysql'; if(count(getglobal('config/db/slave'))) { //是否存在从数据连接,存在则初始化 require_once libfile('class/mysql_slave'); $class = 'db_mysql_slave'; } $this->db = & DB::object($class); $this->db->set_config($this->config['db']); $this->db->connect(); //建立数据库连接 } function _init_session() { //session初始化方法 $this->session = new discuz_session(); //new discuz_session类 if($this->init_session) { $this->session->init($this->var['cookie']['sid'], $this->var['clientip'], $this->var['uid']); $this->var['sid'] = $this->session->sid; //设置sid $this->var['session'] = $this->session->var; //设置session if($this->var['sid'] != $this->var['cookie']['sid']) { dsetcookie('sid', $this->var['sid'], 86400); //如果sid不为cookie中的sid,则重写sid到cookie } if($this->session->isnew) { if(ipbanned($this->var['clientip'])) { $this->session->set('groupid', 6); //如果发现IP在禁止范围里,则设置该客户端用户组为6,则:禁止IP用户组 } } if($this->session->get('groupid') == 6) { $this->var['member']['groupid'] = 6; sysmessage('user_banned'); //提示IP禁止 } if($this->var['uid'] && ($this->session->isnew || ($this->session->get('lastactivity') + 600) < TIMESTAMP)) { $this->session->set('lastactivity', TIMESTAMP); //最近活动检测,600秒 if($this->session->isnew) { DB::update('common_member_status', array('lastip' => $this->var['clientip'], 'lastvisit' => TIMESTAMP), "uid='".$this->var['uid']."'"); } //如果用户在600秒里不活动,则设置最后访问时间点 } } } function _init_user() { //用户初始化方法 if($this->init_user) { if($auth = getglobal('auth', 'cookie')) { //得到auth,username\tuid的加密信息 $auth = daddslashes(explode("\t", authcode($auth, 'DECODE'))); //进行解密 } list($discuz_pw, $discuz_uid) = empty($auth) || count($auth) < 2 ? array('', '') : $auth; //得到用户名和用户密码,如果auth为空,或者缺失uid和username中的一个,则为空 if($discuz_uid) { $user = getuserbyuid($discuz_uid); //如果uid存在,则得到该用户信息 } if(!empty($user) && $user['password'] == $discuz_pw) { $this->var['member'] = $user; //如果用户存在,且密码正确,则用户信息写进全局变量中 } else { $user = array(); //user定义为空数组 $this->_init_guest(); //否则为游客,游客初始化方法 } if($user && $user['groupexpiry'] > 0 && $user['groupexpiry'] < TIMESTAMP && getgpc('mod') != 'spacecp' && getgpc('do') != 'expiry' && CURSCRIPT != 'home') { dheader('location: home.php?mod=spacecp&ac=usergroup&do=expiry'); } //用户组过期检测 $this->cachelist[] = 'usergroup_'.$this->var['member']['groupid']; //用户组数据缓存 if($user && $user['adminid'] > 0 && $user['groupid'] != $user['adminid']) { $this->cachelist[] = 'admingroup_'.$this->var['member']['adminid']; //管理员用户组缓存 } } else { $this->_init_guest(); //游客 } if(empty($this->var['cookie']['lastvisit'])) { $this->var['member']['lastvisit'] = TIMESTAMP - 3600; dsetcookie('lastvisit', TIMESTAMP - 3600, 86400 * 30); //cookie中如果为记录最后访问时间,则写入 } else { $this->var['member']['lastvisit'] = $this->var['cookie']['lastvisit'];//否则,写入全局变量 } setglobal('uid', getglobal('uid', 'member')); setglobal('username', addslashes(getglobal('username', 'member'))); setglobal('adminid', getglobal('adminid', 'member')); setglobal('groupid', getglobal('groupid', 'member')); //以上四行是把用户的uid,用户名,管理组id,用户组写入全局变量中 } function _init_guest() { //游客初始化方法 setglobal('member', array( 'uid' => 0, 'username' => '', 'adminid' => 0, 'groupid' => 7, 'credits' => 0, 'timeoffset' => 9999)); } function _init_cron() { //计划任务初始化 if($this->init_cron && $this->init_setting) { if($this->var['cache']['cronnextrun'] <= TIMESTAMP) { require_once libfile('class/cron'); //加载source/class/class_cron.php文件 discuz_cron::run(); //运行 } } } function _init_misc() { if(!$this->init_misc) { return false; } lang('core'); //加载core语言包 if($this->init_setting && $this->init_user) { if(!isset($this->var['member']['timeoffset']) || $this->var['member']['timeoffset'] == 9999 || $this->var['member']['timeoffset'] === '') { $this->var['member']['timeoffset'] = $this->var['setting']['timeoffset']; } } //设置用户时区 $timeoffset = $this->init_setting ? $this->var['member']['timeoffset'] : $this->var['setting']['timeoffset']; $this->var['timenow'] = array( 'time' => dgmdate(TIMESTAMP), 'offset' => $timeoffset >= 0 ? ($timeoffset == 0 ? '' : '+'.$timeoffset) : $timeoffset ); $this->timezone_set($timeoffset); $this->var['formhash'] = formhash(); //得到FORMHASH define('FORMHASH', $this->var['formhash']); //定义为常量 if($this->init_user) { if($this->var['group'] && isset($this->var['group']['allowvisit']) && !$this->var['group']['allowvisit']) { if($this->var['uid']) { sysmessage('user_banned', null); //检测是否为禁止访问 } elseif((!defined('ALLOWGUEST') || !ALLOWGUEST) && !in_array(CURSCRIPT, array('member', 'api')) && !$this->var['inajax']) { dheader('location: member.php?mod=logging&action=login&referer='.rawurlencode($_SERVER['REQUEST_URI'])); } } if($this->var['member']['status'] == -1) { sysmessage('user_banned', null); //如果用户状态为-1,则提示禁止访问 } } if($this->var['setting']['ipaccess'] && !ipaccess($this->var['clientip'], $this->var['setting']['ipaccess'])) { sysmessage('user_banned', null); //ip权限检测 } if($this->var['setting']['bbclosed']) { if($this->var['uid'] && ($this->var['group']['allowvisit'] == 2 || $this->var['groupid'] == 1)) { } elseif(in_array(CURSCRIPT, array('admin', 'member', 'api')) || defined('ALLOWGUEST') && ALLOWGUEST) { } else { $closedreason = DB::result_first("SELECT svalue FROM ".DB::table('common_setting')." WHERE skey='closedreason'"); $closedreason = str_replace(':', ':', $closedreason); showmessage($closedreason ? $closedreason : 'board_closed', NULL, array(), array('login' => 1)); } } //以上是论坛如果为关闭,只有管理员可以访问,其他则提示关闭原因 if(CURSCRIPT != 'admin' && !(in_array($this->var['mod'], array('logging', 'seccode')))) { periodscheck('visitbanperiods'); //私密板块访问设置 } if(defined('IN_MOBILE')) { $this->var['tpp'] = $this->var['setting']['mobile']['mobiletopicperpage'] ? intval($this->var['setting']['mobile']['mobiletopicperpage']) : 20; $this->var['ppp'] = $this->var['setting']['mobile']['mobilepostperpage'] ? intval($this->var['setting']['mobile']['mobilepostperpage']) : 5; } else { $this->var['tpp'] = $this->var['setting']['topicperpage'] ? intval($this->var['setting']['topicperpage']) : 20; $this->var['ppp'] = $this->var['setting']['postperpage'] ? intval($this->var['setting']['postperpage']) : 10; } //wap访问设置 if($this->var['setting']['nocacheheaders']) { @header("Expires: -1"); @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE); @header("Pragma: no-cache"); } //以上五行作用是header cache状态设置 if($this->session->isnew && $this->var['uid']) { updatecreditbyaction('daylogin', $this->var['uid']); //每日登陆增加积分设置 include_once libfile('function/stat'); updatestat('login', 1); if(defined('IN_MOBILE')) { updatestat('mobilelogin', 1); //MOBILE } if($this->var['setting']['connect']['allow'] && $this->var['member']['conisbind']) { updatestat('connectlogin', 1); } } if($this->var['member']['conisbind'] && $this->var['setting']['connect']['newbiespan'] !== '') { $this->var['setting']['newbiespan'] = $this->var['setting']['connect']['newbiespan']; } $lastact = TIMESTAMP."\t".htmlspecialchars(basename($this->var['PHP_SELF']))."\t".htmlspecialchars($this->var['mod']); dsetcookie('lastact', $lastact, 86400); setglobal('currenturl_encode', base64_encode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])); //设置最后动作 if($this->var['setting']['magicstatus'] && !$this->var['group']['allowmagics']) { $this->var['setting']['magicstatus'] = false; $this->var['setting']['magics'] = array(); unset($this->var['setting']['spacenavs']['magic']); } if((!empty($this->var['gp_fromuid']) || !empty($this->var['gp_fromuser'])) && ($this->var['setting']['creditspolicy']['promotion_visit'] || $this->var['setting']['creditspolicy']['promotion_register'])) { require_once libfile('misc/promotion', 'include'); } $this->var['seokeywords'] = !empty($this->var['setting']['seokeywords'][CURSCRIPT]) ? $this->var['setting']['seokeywords'][CURSCRIPT] : ''; //SEO keywords $this->var['seodescription'] = !empty($this->var['setting']['seodescription'][CURSCRIPT]) ? $this->var['setting']['seodescription'][CURSCRIPT] : ''; //SEO 网站描述 } function _init_setting() { if($this->init_setting) { if(empty($this->var['setting'])) { $this->cachelist[] = 'setting'; //缓存设置文件 } if(empty($this->var['style'])) { $this->cachelist[] = 'style_default'; //风格缓存设置 } if(!isset($this->var['cache']['cronnextrun'])) { $this->cachelist[] = 'cronnextrun'; //缓存计划任务 } } !empty($this->cachelist) && loadcache($this->cachelist); if(!is_array($this->var['setting'])) { $this->var['setting'] = array(); } if($this->var['member'] && $this->var['group']['radminid'] == 0 && $this->var['member']['adminid'] > 0 && $this->var['member']['groupid'] != $this->var['member']['adminid'] && !empty($this->var['cache']['admingroup_'.$this->var['member']['adminid']])) { $this->var['group'] = array_merge($this->var['group'], $this->var['cache']['admingroup_'.$this->var['member']['adminid']]); } } function _init_style() { //模板初始化方法 $styleid = !empty($this->var['cookie']['styleid']) ? $this->var['cookie']['styleid'] : 0; if(intval(!empty($this->var['forum']['styleid']))) { $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['forum']['styleid']; } if(intval(!empty($this->var['category']['styleid']))) { $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['category']['styleid']; } if($styleid && $styleid != $this->var['setting']['styleid']) { loadcache('style_'.$styleid); if($this->var['cache']['style_'.$styleid]) { $this->var['style'] = $this->var['cache']['style_'.$styleid]; } } if(is_array($this->var['style'])) { foreach ($this->var['style'] as $key => $val) { $key = strtoupper($key); if(!defined($key) && !is_array($val)) { define($key, $val); } } } } function _init_memory() { //缓存设置 $this->mem = new discuz_memory(); if($this->init_memory) { $this->mem->init($this->config['memory']); } $this->var['memory'] = $this->mem->type; } function _init_mobile() { //手机访问设置 if(!$this->var['setting'] || !$this->init_mobile || !$this->var['setting']['mobile']['allowmobile'] || !is_array($this->var['setting']['mobile']) || IS_ROBOT) { $nomobile = true; //允许手机访问 } if($_GET['mobile'] === 'no') { dsetcookie('mobile', 'no', 3600); $nomobile = true; } elseif($this->var['cookie']['mobile'] == 'no' && $_GET['mobile'] === 'yes') { dsetcookie('mobile', ''); } elseif($this->var['cookie']['mobile'] == 'no') { $nomobile = true; } if(!checkmobile()) { //检测是否为手机访问 $nomobile = true; } if((!$this->var['setting']['mobile']['mobileforward'] && $_GET['mobile'] !== 'yes') || $nomobile) { if($_SERVER['HTTP_HOST'] == $this->var['setting']['domain']['app']['mobile'] && $this->var['setting']['domain']['app']['default']) { dheader("Location:http://".$this->var['setting']['domain']['app']['default'].$_SERVER['REQUEST_URI']); } else { return; } } if(strpos($this->var['setting']['domain']['defaultindex'], CURSCRIPT) === false && CURSCRIPT != 'forum' && !$_GET['mod']) { if($this->var['setting']['domain']['app']['mobile']) { $mobileurl = 'http://'.$this->var['setting']['domain']['app']['mobile']; } else { if($this->var['setting']['domain']['app']['forum']) { $mobileurl = 'http://'.$this->var['setting']['domain']['app']['forum'].'?mobile=yes'; } else { $mobileurl = $this->var['siteurl'].'forum.php?mobile=yes'; } } dheader("location:$mobileurl"); } define('IN_MOBILE', true); setglobal('gzipcompress', 0); $query_sting_tmp = preg_replace(array('/&simpletype=\w+/', '/simpletype=\w+/', '/&mobile=yes/', '/mobile=yes/'), array(), $_SERVER['QUERY_STRING']); $this->var['setting']['mobile']['nomobileurl'] = ($this->var['setting']['domain']['app']['forum'] ? 'http://'.$this->var['setting']['domain']['app']['forum'].'/' : $this->var['siteurl']).$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'mobile=no'; $this->var['setting']['lazyload'] = 0; if('utf-8' != CHARSET) { if(strtolower($_SERVER['REQUEST_METHOD']) === 'post') { foreach($_POST AS $pk => $pv) { if(!is_numeric($pv)) { $this->var['gp_'.$pk] = $_GET[$pk] = $_POST[$pk] = $this->mobile_iconv_recurrence($pv); } } } } if($_GET['simpletype']) { if($_GET['simpletype'] == 'yes') { $this->var['setting']['mobile']['mobilesimpletype'] = 1; dsetcookie('simpletype', 1, 86400); } else { $this->var['setting']['mobile']['mobilesimpletype'] = 0; dsetcookie('simpletype', 0, 86400); } } elseif($this->var['cookie']['simpletype']) { $this->var['setting']['mobile']['mobilesimpletype'] = $this->var['cookie']['simpletype'] == 1 ? 1 : 0 ; } if(!$this->var['setting']['mobile']['mobilesimpletype']) { $this->var['setting']['imagemaxwidth'] = 224; } $this->var['setting']['regstatus'] = $this->var['setting']['mobile']['mobileregister'] ? $this->var['setting']['regstatus'] : 0 ; if(!$this->var['setting']['mobile']['mobileseccode']) { $this->var['setting']['seccodestatus'] = 0; } $this->var['setting']['seccodedata']['type'] = 99; $this->var['setting']['thumbquality'] = 50; $this->var['setting']['mobile']['simpletypeurl'] = array(); $this->var['setting']['mobile']['simpletypeurl'][0] = $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'simpletype=no&mobile=yes' ; $this->var['setting']['mobile']['simpletypeurl'][1] = $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'simpletype=yes&mobile=yes'; unset($query_sting_tmp); ob_start(); } function timezone_set($timeoffset = 0) { //时区设置 if(function_exists('date_default_timezone_set')) { @date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset))); } } function mobile_iconv_recurrence($value) { //手机访问再次转码 if(is_array($value)) { foreach($value AS $key => $val) { $value[$key] = $this->mobile_iconv_recurrence($val); } } else { $value = daddslashes(diconv(stripslashes($value), 'utf-8', CHARSET)); } return $value; } } class db_mysql //数据库类 { var $tablepre; var $version = ''; var $querynum = 0; var $slaveid = 0; var $curlink; var $link = array(); var $config = array(); var $sqldebug = array(); var $map = array(); function db_mysql($config = array()) { if(!empty($config)) { $this->set_config($config); } } function set_config($config) { $this->config = &$config; $this->tablepre = $config['1']['tablepre']; if(!empty($this->config['map'])) { $this->map = $this->config['map']; } } function connect($serverid = 1) { if(empty($this->config) || empty($this->config[$serverid])) { $this->halt('config_db_not_found'); } $this->link[$serverid] = $this->_dbconnect( $this->config[$serverid]['dbhost'], $this->config[$serverid]['dbuser'], $this->config[$serverid]['dbpw'], $this->config[$serverid]['dbcharset'], $this->config[$serverid]['dbname'], $this->config[$serverid]['pconnect'] ); $this->curlink = $this->link[$serverid]; } function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect) { $link = null; $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect'; if(!$link = @$func($dbhost, $dbuser, $dbpw, 1)) { $this->halt('notconnect'); } else { $this->curlink = $link; if($this->version() > '4.1') { $dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset']; $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : ''; $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : ''; $serverset && mysql_query("SET $serverset", $link); } $dbname && @mysql_select_db($dbname, $link); } return $link; } function table_name($tablename) { if(!empty($this->map) && !empty($this->map[$tablename])) { $id = $this->map[$tablename]; if(!$this->link[$id]) { $this->connect($id); } $this->curlink = $this->link[$id]; } else { $this->curlink = $this->link[1]; } return $this->tablepre.$tablename; } function select_db($dbname) { return mysql_select_db($dbname, $this->curlink); } function fetch_array($query, $result_type = MYSQL_ASSOC) { return mysql_fetch_array($query, $result_type); } function fetch_first($sql) { return $this->fetch_array($this->query($sql)); } function result_first($sql) { return $this->result($this->query($sql), 0); } function query($sql, $type = '') { if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) { $starttime = dmicrotime(); } $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query'; if(!($query = $func($sql, $this->curlink))) { if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') { $this->connect(); return $this->query($sql, 'RETRY'.$type); } if($type != 'SILENT' && substr($type, 5) != 'SILENT') { $this->halt('query_error', $sql); } } if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) { $this->sqldebug[] = array($sql, number_format((dmicrotime() - $starttime), 6), debug_backtrace()); } $this->querynum++; return $query; } function affected_rows() { return mysql_affected_rows($this->curlink); } function error() { return (($this->curlink) ? mysql_error($this->curlink) : mysql_error()); } function errno() { return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno()); } function result($query, $row = 0) { $query = @mysql_result($query, $row); return $query; } function num_rows($query) { $query = mysql_num_rows($query); return $query; } function num_fields($query) { return mysql_num_fields($query); } function free_result($query) { return mysql_free_result($query); } function insert_id() { return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0); } function fetch_row($query) { $query = mysql_fetch_row($query); return $query; } function fetch_fields($query) { return mysql_fetch_field($query); } function version() { if(empty($this->version)) { $this->version = mysql_get_server_info($this->curlink); } return $this->version; } function close() { return mysql_close($this->curlink); } function halt($message = '', $sql = '') { require_once libfile('class/error'); discuz_error::db_error($message, $sql); } } class DB //数据库操作 class discuz_session //session类 class discuz_memory //缓存初始化 支持eaccelerator/xcach/memcache ?> |