Discuz! X2 核心类源码(class_core.php)分析

来源:互联网 发布:标致雪铁龙 知乎 编辑:程序博客网 时间:2024/04/30 14:17
[php] view plaincopyprint?
  1. <?php  
  2. /** 
  3.  *      [Discuz!] (C)2001-2099 Comsenz Inc. 
  4.  *      This is NOT a freeware, use is subject to license terms 
  5.  * 
  6.  *      $Id: class_core.php 21271 2011-03-22 02:44:58Z congyushuai $ 
  7.  */  
  8. define('IN_DISCUZ', true);  
  9. error_reporting(0);  
  10. class discuz_core {  
  11.  var $db = null;  
  12.  var $mem = null;  
  13.  var $session = null;  
  14.  var $config = array();  
  15.  var $var = array();  
  16.  var $cachelist = array();  
  17.  var $init_setting = true;  
  18.  var $init_user = true;  
  19.  var $init_session = true;  
  20.  var $init_cron = true;  
  21.  var $init_misc = true;  
  22.  var $init_memory = true;  
  23.  var $init_mobile = true;  
  24.  var $initated = false;  //初始化工作未完成标志  
  25.  var $superglobal = array(  
  26.   'GLOBALS' => 1,  
  27.   '_GET' => 1,  
  28.   '_POST' => 1,  
  29.   '_REQUEST' => 1,  
  30.   '_COOKIE' => 1,  
  31.   '_SERVER' => 1,  
  32.   '_ENV' => 1,  
  33.   '_FILES' => 1,  
  34.  );  
  35.  function &instance() {  
  36.   static $object;  
  37.   if(empty($object)) {  
  38.    $object = new discuz_core();  
  39.   }  
  40.   return $object;  
  41.  }  
  42.  function discuz_core() {  
  43.   $this->_init_env();  //初始化环境变量  
  44.   $this->_init_config();  //初始化配置变量  
  45.   $this->_init_input();  //初始化输入  
  46.   $this->_init_output();  //初始化输出  
  47.  }  
  48.  function init() {  
  49.   if(!$this->initated) {  
  50.    $this->_init_db();  //初始化数据库  
  51.    $this->_init_memory();  //初始化memcache  
  52.    $this->_init_user();  //用户信息初始化  
  53.    $this->_init_session();  //session操作初始化  
  54.    $this->_init_setting();  //系统设置初始化  
  55.    $this->_init_mobile();   //手机功能初始化  
  56.    $this->_init_cron();   //计划任务初始化  
  57.    $this->_init_misc();   //其他功能初始化  
  58.   }  
  59.   $this->initated = true;   //设置完成标志  
  60.  }  
  61.  function _init_env() {  //环境变量初始化方法  
  62.   error_reporting(E_ERROR);  //定义错误报告等级  
  63.   if(phpversion() < '5.3.0') {  
  64.    set_magic_quotes_runtime(0);  //设置set_magic_quotes_runtime  
  65.   }  
  66.   define('DISCUZ_ROOT'substr(dirname(__FILE__), 0, -12));  //定义根目录常量  
  67.   define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc());  //定义MAGIC_QUOTES_GPC  
  68.   define('ICONV_ENABLE', function_exists('iconv'));  //定义转码函数常量,如果 iconv函数支持,则为TRUE  
  69.   define('MB_ENABLE', function_exists('mb_convert_encoding')); //转码函数是否支持  
  70.   define('EXT_OBGZIP', function_exists('ob_gzhandler'));  //缓存输出句柄函数   
  71.   define('TIMESTAMP', time());  //定义当前时间戳常量  
  72.   $this->timezone_set();  
  73.   if(!defined('DISCUZ_CORE_FUNCTION') && !@include(DISCUZ_ROOT.'./source/function/function_core.php')) {  
  74.    exit('function_core.php is missing');  //如果系统核心函数库文件未include,则include,如果核心函数库文件缺失,则退出,因为function_core.php是系统本身的函数库,必须加载  
  75.   }  
  76.   if(function_exists('ini_get')) {  
  77.    $memorylimit = @ini_get('memory_limit');  
  78.    if($memorylimit && return_bytes($memorylimit) < 33554432 && function_exists('ini_set')) {  
  79.     ini_set('memory_limit''128m');  
  80.    }  
  81.   }  
  82. //以上6行代码的作用是设置内存使用限制,如果小于32M,则增加为128M,因为memory_limit小于32M,可能会不够用,毕竟X系统比较大,可能内存会使用的多点,如果不支持ini_set函数,就得去php.ini中修改了。  
  83.   define('IS_ROBOT', checkrobot());  //检测机器人  
  84.   foreach ($GLOBALS as $key => $value) {  
  85.    if (!isset($this->superglobal[$key])) {  
  86.     $GLOBALS[$key] = null; unset($GLOBALS[$key]);  
  87.    }  
  88.   }  
  89. //以上几行的意思是,注销所有的超级变量。  
  90.   global $_G;  //$_G大数组是Discuz中自定义的超级变量  
  91.   $_G = array(  
  92.    'uid' => 0,  //uid  
  93.    'username' => ''//用户名  
  94.    'adminid' => 0,  //adminid标识  
  95.    'groupid' => 1,  //用户组ID  
  96.    'sid' => '',  // sessionID  
  97.    'formhash' => '',  //表单验证认证  
  98.    'timestamp' => TIMESTAMP,  //时间戳  
  99.    'starttime' => dmicrotime(),  //开始时间  
  100.    'clientip' => $this->_get_client_ip(),  //客户端IP  
  101.    'referer' => '',  //referer地址  
  102.    'charset' => '',  //字符串编码  
  103.    'gzipcompress' => '',  //gzip  
  104.    'authkey' => '',  //authkey  认证码  
  105.    'timenow' => array(),  //当前时间  
  106.    'PHP_SELF' => ''//PHP_SELF  
  107.    'siteurl' => ''//网站地址  
  108.    'siteroot' => '',  //网站根目录  
  109.    'config' => array(),  //配置变量数组  
  110.    'setting' => array(),  //设置变量数组  
  111.    'member' => array(),  //用户信息数组  
  112.    'group' => array(),  //用户组数组  
  113.    'cookie' => array(),  //cookie数组  
  114.    'style' => array(),  //风格数组  
  115.    'cache' => array(),  //缓存列表数组  
  116.    'session' => array(), //session变量数组  
  117.    'lang' => array(),  //语言包数组  
  118.    'my_app' => array(),  //我的应用数组  
  119.    'my_userapp' => array(),  //用户应用数组  
  120.    'fid' => 0,    
  121.    'tid' => 0,  
  122.    'forum' => array(),  //论坛板块数组  
  123.    'thread' => array(),  //论坛相关帖子数组  
  124.    'rssauth' => '',  //RSS订阅认证  
  125.    'home' => array(),  //home功能相关数组  
  126.    'space' => array(),  //space功能相关数组  
  127.    'block' => array(),  //块信息数组  
  128.    'article' => array(),  
  129.    'action' => array(  
  130.     'action' => APPTYPEID,  
  131.     'fid' => 0,  
  132.     'tid' => 0,  
  133.    ),  
  134.    'mobile' => '',  //手机信息  
  135.   );  
  136.   //以上定义了$_G超级变量的部分内容。都是系统以后要用到的。写在一个大数组中,方便使用  
  137.   $_G['PHP_SELF'] = htmlspecialchars($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PHP_SELF']);  
  138.   //当前脚本地址写入$_G超级变量中  
  139.   $_G['basescript'] = CURSCRIPT; //当前脚本  
  140.   $_G['basefilename'] = basename($_G['PHP_SELF']);  //当前脚本名称  
  141.   $_G['siteurl'] = htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api)?\/*$/i"''substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], '/'))).'/');//网站地址  
  142.   $_G['siteroot'] = substr($_G['PHP_SELF'], 0, -strlen($_G['basefilename']));//网站根地址  
  143.     
  144.   if(defined('SUB_DIR')) {  //二级目录设置  
  145.    $_G['siteurl'] = str_replace(SUB_DIR, '/'$_G['siteurl']);  
  146.    $_G['siteroot'] = str_replace(SUB_DIR, '/'$_G['siteroot']);  
  147.   }  
  148.   $this->var = & $_G;  
  149.  }  
  150.  function _init_input() {   //输入初始化方法  
  151.   if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) ||  isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) {  
  152.    system_error('request_tainting');  
  153.   }  
  154.   if(!MAGIC_QUOTES_GPC) {  
  155.    $_GET = daddslashes($_GET);  
  156.    $_POST = daddslashes($_POST);  
  157.    $_COOKIE = daddslashes($_COOKIE);  
  158.    $_FILES = daddslashes($_FILES);  
  159.   }  
  160. //以上代码未GPC安全机制,进行转义  
  161.   $prelength = strlen($this->config['cookie']['cookiepre']);  
  162.   foreach($_COOKIE as $key => $val) {  
  163.    if(substr($key, 0, $prelength) == $this->config['cookie']['cookiepre']) {  
  164.     $this->var['cookie'][substr($key$prelength)] = $val;  
  165.    }  
  166.   }  
  167. //以上代码意思是,如果cookie的键值等于定义的键值,那么截取cookiepre  
  168.   $_GET['diy'] = empty($_GET['diy']) ? '' : $_GET['diy'];  
  169.   if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {  
  170.    $_GET = array_merge($_GET$_POST);  
  171.   }  
  172. //合并$_POST,$_GET  
  173.   foreach($_GET as $k => $v) {  
  174.    $this->var['gp_'.$k] = $v;  
  175.   }  
  176. //然后把$_POST和$_GET的值都赋予gp变量中,方便使用  
  177.   $this->var['mod'] = empty($this->var['gp_mod']) ? '' : htmlspecialchars($this->var['gp_mod']);  
  178.  //获得$mod变量 ?mod=xxx,则$this->var['mod']为xxx  
  179.   $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));  
  180.   //是否需要ajax方式  
  181.   $this->var['page'] = empty($this->var['gp_page']) ? 1 : max(1, intval($this->var['gp_page']));  
  182.   //页面获取,最小为1  
  183.   $this->var['sid'] = $this->var['cookie']['sid'] = isset($this->var['cookie']['sid']) ? htmlspecialchars($this->var['cookie']['sid']) : '';  
  184.  //sid获取  
  185.  }  
  186.  function _init_config() {  //获得配置文件  
  187.   $_config = array();  
  188.   @include DISCUZ_ROOT.'./config/config_global.php';//加载全局配置文件  
  189.   if(empty($_config)) {  
  190.    if(!file_exists(DISCUZ_ROOT.'./data/install.lock')) {  
  191.     header('location: install');  
  192.     exit;  
  193.    } else {  
  194.     system_error('config_notfound');  
  195.    }  
  196.   }  
  197. //以上代码为:如果config不存在,要么没安装,要么文件不存在,系统报错  
  198.   if(empty($_config['security']['authkey'])) {  
  199.    $_config['security']['authkey'] = md5($_config['cookie']['cookiepre'].$_config['db'][1]['dbname']);  
  200.   }  
  201. //设置安全验证的authkey  
  202.   if(empty($_config['debug']) || !file_exists(libfile('function/debug'))) {  
  203.    define('DISCUZ_DEBUG', false);  
  204.   } elseif($_config['debug'] === 1 || $_config['debug'] === 2 || !empty($_REQUEST['debug']) && $_REQUEST['debug'] === $_config['debug']) {  
  205.    define('DISCUZ_DEBUG', true);  
  206.    if($_config['debug'] == 2) {  
  207.     error_reporting(E_ALL);  
  208.    }  
  209.   } else {  
  210.    define('DISCUZ_DEBUG', false);  
  211.   }  
  212. //以上代码为是否调试模式,  
  213.   define('STATICURL', !empty($_config['output']['staticurl']) ? $_config['output']['staticurl'] : 'static/');  
  214.   //定义静态文件常量,如:css,img等,如果$_config['output']['staticurl']为空,则是默认的static目录,就是默认目录  
  215.   $this->var['staticurl'] = STATICURL;  
  216.     
  217.   $this->config = & $_config;  
  218.   $this->var['config'] = & $_config;  
  219.   //引用$_config变量,改变$this->config,则$_config改变,保持统一  
  220.   if(substr($_config['cookie']['cookiepath'], 0, 1) != '/') {  
  221.    $this->var['config']['cookie']['cookiepath'] = '/'.$this->var['config']['cookie']['cookiepath'];  
  222.   }  
  223.   //设置cookie域,一般是设置目录域。/不存在则加上/,安全性更高  
  224.   $this->var['config']['cookie']['cookiepre'] = $this->var['config']['cookie']['cookiepre'].substr(md5($this->var['config']['cookie']['cookiepath'].'|'.$this->var['config']['cookie']['cookiedomain']), 0, 4).'_';  
  225.   //定义cookie前缀,如定义为xxx_;则为$cookie['xxx_uid]  
  226.   $this->var['authkey'] = md5($_config['security']['authkey'].$_SERVER['HTTP_USER_AGENT']);  
  227.  //得到authkey  
  228.  }  
  229.  function _init_output() {  //输出初始化方法  
  230.   if($this->config['security']['urlxssdefend'] && $_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_SERVER['REQUEST_URI'])) {  
  231.    $this->_xss_check();  
  232.   }  
  233.   if($this->config['security']['attackevasive'] && (!defined('CURSCRIPT') || !in_array($this->var['mod'], array('seccode''secqaa''swfupload')))) {  
  234.    require_once libfile('misc/security''include');  
  235.   }  
  236. //验证码设置,加载include/misc/misc_security.php文件,验证功能;  
  237.   if(!empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) {  
  238.    $this->config['output']['gzip'] = false;  
  239.   }  
  240. //是否开启gzip,如果不支持gzip,则定义为false  
  241.   $allowgzip = $this->config['output']['gzip'] && empty($this->var['inajax']) && $this->var['mod'] != 'attachment' && EXT_OBGZIP;  
  242.   setglobal('gzipcompress'$allowgzip);  
  243.   //把$allowgzip写入全局变量中  
  244.   ob_start($allowgzip ? 'ob_gzhandler' : null);  
  245. //定义输出缓存  
  246.   setglobal('charset'$this->config['output']['charset']);  
  247.   //把配置文件中的字符集赋予全局变量中  
  248.   define('CHARSET'$this->config['output']['charset']);  
  249.   if($this->config['output']['forceheader']) {  
  250.    @header('Content-Type: text/html; charset='.CHARSET);//设置网页编码,强制输出  
  251.   }  
  252.  }  
  253.  function reject_robot() {  //拒绝机器人访问,设置为403错误  
  254.   if(IS_ROBOT) {  
  255.    exit(header("HTTP/1.1 403 Forbidden"));  
  256.   }  
  257.  }  
  258.  function _xss_check() {  //检测xss漏洞,UBB  
  259.   $temp = strtoupper(urldecode(urldecode($_SERVER['REQUEST_URI'])));  
  260.   if(strpos($temp'<') !== false || strpos($temp'"') !== false || strpos($tmp,'CONTENT-TRANSFER-ENCODING')!==false) {  
  261.    system_error('request_tainting');  
  262.   }  
  263.   return true;  
  264.  }  
  265.  function _get_client_ip() {  //得到客户端IP  
  266.   $ip = $_SERVER['REMOTE_ADDR'];  
  267.   if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/'$_SERVER['HTTP_CLIENT_IP'])) {  
  268.    $ip = $_SERVER['HTTP_CLIENT_IP'];  
  269.   } 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)) {  
  270.    foreach ($matches[0] AS $xip) {  
  271.     if (!preg_match('#^(10|172\.16|192\.168)\.#'$xip)) {  
  272.      $ip = $xip;  
  273.      break;  
  274.     }  
  275.    }  
  276.   }  
  277.   return $ip;  
  278.  }  
  279.  function _init_db() {  //初始化数据库  
  280.   $class = 'db_mysql';  
  281.   if(count(getglobal('config/db/slave'))) {  //是否存在从数据连接,存在则初始化  
  282.    require_once libfile('class/mysql_slave');  
  283.    $class = 'db_mysql_slave';  
  284.   }  
  285.   $this->db = & DB::object($class);  
  286.   $this->db->set_config($this->config['db']);  
  287.   $this->db->connect(); //建立数据库连接  
  288.  }  
  289.  function _init_session() { //session初始化方法  
  290.   $this->session = new discuz_session();   //new discuz_session类  
  291.   if($this->init_session)  
  292.   {  
  293.    $this->session->init($this->var['cookie']['sid'], $this->var['clientip'], $this->var['uid']);  
  294.    $this->var['sid'] = $this->session->sid;  //设置sid  
  295.    $this->var['session'] = $this->session->var;  //设置session  
  296.    if($this->var['sid'] != $this->var['cookie']['sid']) {  
  297.     dsetcookie('sid'$this->var['sid'], 86400);  //如果sid不为cookie中的sid,则重写sid到cookie  
  298.    }  
  299.    if($this->session->isnew) {  
  300.     if(ipbanned($this->var['clientip'])) {  
  301.      $this->session->set('groupid', 6);  //如果发现IP在禁止范围里,则设置该客户端用户组为6,则:禁止IP用户组  
  302.     }  
  303.    }  
  304.    if($this->session->get('groupid') == 6) {  
  305.     $this->var['member']['groupid'] = 6;  
  306.     sysmessage('user_banned');  //提示IP禁止  
  307.    }  
  308.   
  309.    if($this->var['uid'] && ($this->session->isnew || ($this->session->get('lastactivity') + 600) < TIMESTAMP)) {  
  310.     $this->session->set('lastactivity', TIMESTAMP);  
  311.     //最近活动检测,600秒  
  312.     if($this->session->isnew) {  
  313.      DB::update('common_member_status'array('lastip' => $this->var['clientip'], 'lastvisit' => TIMESTAMP), "uid='".$this->var['uid']."'");  
  314.     } //如果用户在600秒里不活动,则设置最后访问时间点  
  315.    }  
  316.   }  
  317.  }  
  318.  function _init_user() {  //用户初始化方法  
  319.   if($this->init_user) {  
  320.    if($auth = getglobal('auth''cookie')) {   //得到auth,username\tuid的加密信息  
  321.     $auth = daddslashes(explode("\t", authcode($auth'DECODE')));  //进行解密  
  322.    }  
  323.    list($discuz_pw$discuz_uid) = empty($auth) || count($auth) < 2 ? array('''') : $auth;  
  324.    //得到用户名和用户密码,如果auth为空,或者缺失uid和username中的一个,则为空  
  325.    if($discuz_uid) {  
  326.     $user = getuserbyuid($discuz_uid);  //如果uid存在,则得到该用户信息  
  327.    }  
  328.    if(!empty($user) && $user['password'] == $discuz_pw) {  
  329.     $this->var['member'] = $user;  //如果用户存在,且密码正确,则用户信息写进全局变量中  
  330.    } else {  
  331.     $user = array();  //user定义为空数组  
  332.     $this->_init_guest();  //否则为游客,游客初始化方法  
  333.    }  
  334.    if($user && $user['groupexpiry'] > 0 && $user['groupexpiry'] < TIMESTAMP && getgpc('mod') != 'spacecp' && getgpc('do') != 'expiry' && CURSCRIPT != 'home') {  
  335.     dheader('location: home.php?mod=spacecp&ac=usergroup&do=expiry');  
  336.    }  
  337.     //用户组过期检测  
  338.    $this->cachelist[] = 'usergroup_'.$this->var['member']['groupid'];  
  339.    //用户组数据缓存  
  340.    if($user && $user['adminid'] > 0 && $user['groupid'] != $user['adminid']) {  
  341.     $this->cachelist[] = 'admingroup_'.$this->var['member']['adminid'];  //管理员用户组缓存  
  342.    }  
  343.   } else {  
  344.    $this->_init_guest(); //游客  
  345.   }  
  346.   if(empty($this->var['cookie']['lastvisit'])) {  
  347.    $this->var['member']['lastvisit'] = TIMESTAMP - 3600;  
  348.    dsetcookie('lastvisit', TIMESTAMP - 3600, 86400 * 30); //cookie中如果为记录最后访问时间,则写入  
  349.   } else {  
  350.    $this->var['member']['lastvisit'] = $this->var['cookie']['lastvisit'];//否则,写入全局变量  
  351.   }  
  352.   setglobal('uid', getglobal('uid''member'));    
  353.   setglobal('username'addslashes(getglobal('username''member')));  
  354.   setglobal('adminid', getglobal('adminid''member'));  
  355.   setglobal('groupid', getglobal('groupid''member'));  
  356.   //以上四行是把用户的uid,用户名,管理组id,用户组写入全局变量中  
  357.  }  
  358.  function _init_guest() {  //游客初始化方法  
  359.   setglobal('member'array'uid' => 0, 'username' => '''adminid' => 0, 'groupid' => 7, 'credits' => 0, 'timeoffset' => 9999));  
  360.  }  
  361.  function _init_cron() {  //计划任务初始化  
  362.   if($this->init_cron && $this->init_setting) {  
  363.    if($this->var['cache']['cronnextrun'] <= TIMESTAMP) {  
  364.     require_once libfile('class/cron');   //加载source/class/class_cron.php文件  
  365.     discuz_cron::run();  //运行  
  366.    }  
  367.   }  
  368.  }  
  369.  function _init_misc() {    
  370.   if(!$this->init_misc) {  
  371.    return false;  
  372.   }  
  373.   lang('core');  //加载core语言包  
  374.   if($this->init_setting && $this->init_user) {  
  375.    if(!isset($this->var['member']['timeoffset']) || $this->var['member']['timeoffset'] == 9999 || $this->var['member']['timeoffset'] === '') {  
  376.     $this->var['member']['timeoffset'] = $this->var['setting']['timeoffset'];  
  377.    }  
  378.   }  
  379. //设置用户时区  
  380.   $timeoffset = $this->init_setting ? $this->var['member']['timeoffset'] : $this->var['setting']['timeoffset'];  
  381.   $this->var['timenow'] = array(  
  382.    'time' => dgmdate(TIMESTAMP),  
  383.    'offset' => $timeoffset >= 0 ? ($timeoffset == 0 ? '' : '+'.$timeoffset) : $timeoffset  
  384.   );  
  385.   $this->timezone_set($timeoffset);  
  386.   $this->var['formhash'] = formhash();   //得到FORMHASH  
  387.   define('FORMHASH'$this->var['formhash']);  //定义为常量  
  388.   if($this->init_user) {  
  389.    if($this->var['group'] && isset($this->var['group']['allowvisit']) && !$this->var['group']['allowvisit']) {  
  390.     if($this->var['uid']) {  
  391.      sysmessage('user_banned', null);  //检测是否为禁止访问  
  392.     } elseif((!defined('ALLOWGUEST') || !ALLOWGUEST) && !in_array(CURSCRIPT, array('member''api')) && !$this->var['inajax']) {  
  393.      dheader('location: member.php?mod=logging&action=login&referer='.rawurlencode($_SERVER['REQUEST_URI']));  
  394.     }  
  395.    }  
  396.    if($this->var['member']['status'] == -1) {  
  397.     sysmessage('user_banned', null);  //如果用户状态为-1,则提示禁止访问  
  398.    }  
  399.   }  
  400.   if($this->var['setting']['ipaccess'] && !ipaccess($this->var['clientip'], $this->var['setting']['ipaccess'])) {  
  401.    sysmessage('user_banned', null);  //ip权限检测  
  402.   }  
  403.   if($this->var['setting']['bbclosed']) {  
  404.    if($this->var['uid'] && ($this->var['group']['allowvisit'] == 2 || $this->var['groupid'] == 1)) {  
  405.    } elseif(in_array(CURSCRIPT, array('admin''member''api')) || defined('ALLOWGUEST') && ALLOWGUEST) {  
  406.    } else {  
  407.     $closedreason = DB::result_first("SELECT svalue FROM ".DB::table('common_setting')." WHERE skey='closedreason'");  
  408.     $closedreason = str_replace(':'':'$closedreason);  
  409.     showmessage($closedreason ? $closedreason : 'board_closed', NULL, array(), array('login' => 1));  
  410.    }  
  411.   }  
  412. //以上是论坛如果为关闭,只有管理员可以访问,其他则提示关闭原因  
  413.   if(CURSCRIPT != 'admin' && !(in_array($this->var['mod'], array('logging''seccode')))) {  
  414.    periodscheck('visitbanperiods');  //私密板块访问设置  
  415.   }  
  416.   if(defined('IN_MOBILE')) {  
  417.    $this->var['tpp'] = $this->var['setting']['mobile']['mobiletopicperpage'] ? intval($this->var['setting']['mobile']['mobiletopicperpage']) : 20;  
  418.    $this->var['ppp'] = $this->var['setting']['mobile']['mobilepostperpage'] ? intval($this->var['setting']['mobile']['mobilepostperpage']) : 5;  
  419.   } else {  
  420.    $this->var['tpp'] = $this->var['setting']['topicperpage'] ? intval($this->var['setting']['topicperpage']) : 20;  
  421.    $this->var['ppp'] = $this->var['setting']['postperpage'] ? intval($this->var['setting']['postperpage']) : 10;  
  422.   }  
  423. //wap访问设置  
  424.   if($this->var['setting']['nocacheheaders']) {  
  425.    @header("Expires: -1");  
  426.    @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);  
  427.    @header("Pragma: no-cache");  
  428.   }  
  429. //以上五行作用是header cache状态设置  
  430.   if($this->session->isnew && $this->var['uid']) {  
  431.    updatecreditbyaction('daylogin'$this->var['uid']);  //每日登陆增加积分设置  
  432.    include_once libfile('function/stat');  
  433.    updatestat('login', 1);  
  434.    if(defined('IN_MOBILE')) {  
  435.     updatestat('mobilelogin', 1); //MOBILE  
  436.    }  
  437.    if($this->var['setting']['connect']['allow'] && $this->var['member']['conisbind']) {  
  438.     updatestat('connectlogin', 1);  
  439.    }  
  440.   }  
  441.   if($this->var['member']['conisbind'] && $this->var['setting']['connect']['newbiespan'] !== '') {  
  442.    $this->var['setting']['newbiespan'] = $this->var['setting']['connect']['newbiespan'];  
  443.   }  
  444.   $lastact = TIMESTAMP."\t".htmlspecialchars(basename($this->var['PHP_SELF']))."\t".htmlspecialchars($this->var['mod']);  
  445.   dsetcookie('lastact'$lastact, 86400);  
  446.   setglobal('currenturl_encode'base64_encode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));  
  447. //设置最后动作  
  448.   if($this->var['setting']['magicstatus'] && !$this->var['group']['allowmagics']) {  
  449.    $this->var['setting']['magicstatus'] = false;  
  450.    $this->var['setting']['magics'] = array();  
  451.    unset($this->var['setting']['spacenavs']['magic']);  
  452.   }  
  453.   if((!empty($this->var['gp_fromuid']) || !empty($this->var['gp_fromuser'])) && ($this->var['setting']['creditspolicy']['promotion_visit'] || $this->var['setting']['creditspolicy']['promotion_register'])) {  
  454.    require_once libfile('misc/promotion''include');  
  455.   }  
  456.   $this->var['seokeywords'] = !empty($this->var['setting']['seokeywords'][CURSCRIPT]) ? $this->var['setting']['seokeywords'][CURSCRIPT] : '';  //SEO keywords  
  457.   $this->var['seodescription'] = !empty($this->var['setting']['seodescription'][CURSCRIPT]) ? $this->var['setting']['seodescription'][CURSCRIPT] : '';  //SEO 网站描述  
  458.  }  
  459.  function _init_setting() {  
  460.   if($this->init_setting) {  
  461.    if(empty($this->var['setting'])) {  
  462.     $this->cachelist[] = 'setting';  //缓存设置文件  
  463.    }  
  464.    if(empty($this->var['style'])) {  
  465.     $this->cachelist[] = 'style_default';  //风格缓存设置  
  466.    }  
  467.    if(!isset($this->var['cache']['cronnextrun'])) {  
  468.     $this->cachelist[] = 'cronnextrun';  //缓存计划任务  
  469.    }  
  470.   }  
  471.   !empty($this->cachelist) && loadcache($this->cachelist);  
  472.   if(!is_array($this->var['setting'])) {  
  473.    $this->var['setting'] = array();  
  474.   }  
  475.   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']])) {  
  476.    $this->var['group'] = array_merge($this->var['group'], $this->var['cache']['admingroup_'.$this->var['member']['adminid']]);  
  477.   }  
  478.  }  
  479.  function _init_style() {  //模板初始化方法  
  480.   $styleid = !empty($this->var['cookie']['styleid']) ? $this->var['cookie']['styleid'] : 0;  
  481.   if(intval(!empty($this->var['forum']['styleid']))) {  
  482.    $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['forum']['styleid'];  
  483.   }  
  484.   if(intval(!empty($this->var['category']['styleid']))) {  
  485.    $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['category']['styleid'];  
  486.   }  
  487.   if($styleid && $styleid != $this->var['setting']['styleid']) {  
  488.    loadcache('style_'.$styleid);  
  489.    if($this->var['cache']['style_'.$styleid]) {  
  490.     $this->var['style'] = $this->var['cache']['style_'.$styleid];  
  491.    }  
  492.   }  
  493.   if(is_array($this->var['style'])) {  
  494.    foreach ($this->var['style'as $key => $val) {  
  495.     $key = strtoupper($key);  
  496.     if(!defined($key) && !is_array($val)) {  
  497.      define($key$val);  
  498.     }  
  499.    }  
  500.   }  
  501.  }  
  502.  function _init_memory() {  //缓存设置  
  503.   $this->mem = new discuz_memory();  
  504.   if($this->init_memory) {  
  505.    $this->mem->init($this->config['memory']);  
  506.   }  
  507.   $this->var['memory'] = $this->mem->type;  
  508.  }  
  509.  function _init_mobile() {  //手机访问设置  
  510.   if(!$this->var['setting'] || !$this->init_mobile || !$this->var['setting']['mobile']['allowmobile'] || !is_array($this->var['setting']['mobile']) || IS_ROBOT) {  
  511.    $nomobile = true;  //允许手机访问  
  512.   }  
  513.   if($_GET['mobile'] === 'no') {  
  514.    dsetcookie('mobile''no', 3600);  
  515.    $nomobile = true;  
  516.   } elseif($this->var['cookie']['mobile'] == 'no' && $_GET['mobile'] === 'yes') {  
  517.    dsetcookie('mobile''');  
  518.   } elseif($this->var['cookie']['mobile'] == 'no') {  
  519.    $nomobile = true;  
  520.   }  
  521.   if(!checkmobile()) {  //检测是否为手机访问  
  522.    $nomobile = true;  
  523.   }  
  524.   if((!$this->var['setting']['mobile']['mobileforward'] && $_GET['mobile'] !== 'yes') || $nomobile) {  
  525.    if($_SERVER['HTTP_HOST'] == $this->var['setting']['domain']['app']['mobile'] && $this->var['setting']['domain']['app']['default']) {  
  526.     dheader("Location:http://".$this->var['setting']['domain']['app']['default'].$_SERVER['REQUEST_URI']);  
  527.    } else {  
  528.     return;  
  529.    }  
  530.   }  
  531.   if(strpos($this->var['setting']['domain']['defaultindex'], CURSCRIPT) === false && CURSCRIPT != 'forum' && !$_GET['mod']) {  
  532.    if($this->var['setting']['domain']['app']['mobile']) {  
  533.     $mobileurl = 'http://'.$this->var['setting']['domain']['app']['mobile'];  
  534.    } else {  
  535.     if($this->var['setting']['domain']['app']['forum']) {  
  536.      $mobileurl = 'http://'.$this->var['setting']['domain']['app']['forum'].'?mobile=yes';  
  537.     } else {  
  538.      $mobileurl = $this->var['siteurl'].'forum.php?mobile=yes';  
  539.     }  
  540.    }  
  541.    dheader("location:$mobileurl");  
  542.   }  
  543.   define('IN_MOBILE', true);  
  544.   setglobal('gzipcompress', 0);  
  545.   $query_sting_tmp = preg_replace(array('/&simpletype=\w+/''/simpletype=\w+/''/&mobile=yes/''/mobile=yes/'), array(), $_SERVER['QUERY_STRING']);  
  546.   $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';  
  547.   $this->var['setting']['lazyload'] = 0;  
  548.   if('utf-8' != CHARSET) {  
  549.    if(strtolower($_SERVER['REQUEST_METHOD']) === 'post') {  
  550.     foreach($_POST AS $pk => $pv) {  
  551.      if(!is_numeric($pv)) {  
  552.       $this->var['gp_'.$pk] = $_GET[$pk] = $_POST[$pk] = $this->mobile_iconv_recurrence($pv);  
  553.      }  
  554.     }  
  555.    }  
  556.   }  
  557.   if($_GET['simpletype']) {  
  558.    if($_GET['simpletype'] == 'yes') {  
  559.     $this->var['setting']['mobile']['mobilesimpletype'] = 1;  
  560.     dsetcookie('simpletype', 1, 86400);  
  561.    } else {  
  562.     $this->var['setting']['mobile']['mobilesimpletype'] = 0;  
  563.     dsetcookie('simpletype', 0, 86400);  
  564.    }  
  565.   } elseif($this->var['cookie']['simpletype']) {  
  566.    $this->var['setting']['mobile']['mobilesimpletype'] = $this->var['cookie']['simpletype'] == 1 ? 1 : 0 ;  
  567.   }  
  568.   if(!$this->var['setting']['mobile']['mobilesimpletype']) {  
  569.    $this->var['setting']['imagemaxwidth'] = 224;  
  570.   }  
  571.   $this->var['setting']['regstatus'] = $this->var['setting']['mobile']['mobileregister'] ? $this->var['setting']['regstatus'] : 0 ;  
  572.   if(!$this->var['setting']['mobile']['mobileseccode']) {  
  573.    $this->var['setting']['seccodestatus'] = 0;  
  574.   }  
  575.   $this->var['setting']['seccodedata']['type'] = 99;  
  576.   $this->var['setting']['thumbquality'] = 50;  
  577.   
  578.   $this->var['setting']['mobile']['simpletypeurl'] = array();  
  579.   $this->var['setting']['mobile']['simpletypeurl'][0] = $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'simpletype=no&mobile=yes' ;  
  580.   $this->var['setting']['mobile']['simpletypeurl'][1] =  $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'simpletype=yes&mobile=yes';  
  581.   unset($query_sting_tmp);  
  582.   ob_start();  
  583.  }  
  584.  function timezone_set($timeoffset = 0) {  //时区设置  
  585.   if(function_exists('date_default_timezone_set')) {  
  586.    @date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset)));  
  587.   }  
  588.  }  
  589.  function mobile_iconv_recurrence($value) {  //手机访问再次转码  
  590.   if(is_array($value)) {  
  591.    foreach($value AS $key => $val) {  
  592.     $value[$key] = $this->mobile_iconv_recurrence($val);  
  593.    }  
  594.   } else {  
  595.    $value = daddslashes(diconv(stripslashes($value), 'utf-8', CHARSET));  
  596.   }  
  597.   return $value;  
  598.  }  
  599. }  
  600. class db_mysql   //数据库类  
  601. {  
  602.  var $tablepre;  
  603.  var $version = '';  
  604.  var $querynum = 0;  
  605.  var $slaveid = 0;  
  606.  var $curlink;  
  607.  var $link = array();  
  608.  var $config = array();  
  609.  var $sqldebug = array();  
  610.  var $map = array();  
  611.  function db_mysql($config = array()) {  
  612.   if(!empty($config)) {  
  613.    $this->set_config($config);  
  614.   }  
  615.  }  
  616.  function set_config($config) {  
  617.   $this->config = &$config;  
  618.   $this->tablepre = $config['1']['tablepre'];  
  619.   if(!empty($this->config['map'])) {  
  620.    $this->map = $this->config['map'];  
  621.   }  
  622.  }  
  623.  function connect($serverid = 1) {  
  624.   if(empty($this->config) || empty($this->config[$serverid])) {  
  625.    $this->halt('config_db_not_found');  
  626.   }  
  627.   $this->link[$serverid] = $this->_dbconnect(  
  628.    $this->config[$serverid]['dbhost'],  
  629.    $this->config[$serverid]['dbuser'],  
  630.    $this->config[$serverid]['dbpw'],  
  631.    $this->config[$serverid]['dbcharset'],  
  632.    $this->config[$serverid]['dbname'],  
  633.    $this->config[$serverid]['pconnect']  
  634.    );  
  635.   $this->curlink = $this->link[$serverid];  
  636.  }  
  637.  function _dbconnect($dbhost$dbuser$dbpw$dbcharset$dbname$pconnect) {  
  638.   $link = null;  
  639.   $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';  
  640.   if(!$link = @$func($dbhost$dbuser$dbpw, 1)) {  
  641.    $this->halt('notconnect');  
  642.   } else {  
  643.    $this->curlink = $link;  
  644.    if($this->version() > '4.1') {  
  645.     $dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset'];  
  646.     $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';  
  647.     $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';  
  648.     $serverset && mysql_query("SET $serverset"$link);  
  649.    }  
  650.    $dbname && @mysql_select_db($dbname$link);  
  651.   }  
  652.   return $link;  
  653.  }  
  654.  function table_name($tablename) {  
  655.   if(!empty($this->map) && !empty($this->map[$tablename])) {  
  656.    $id = $this->map[$tablename];  
  657.    if(!$this->link[$id]) {  
  658.     $this->connect($id);  
  659.    }  
  660.    $this->curlink = $this->link[$id];  
  661.   } else {  
  662.    $this->curlink = $this->link[1];  
  663.   }  
  664.   return $this->tablepre.$tablename;  
  665.  }  
  666.  function select_db($dbname) {  
  667.   return mysql_select_db($dbname$this->curlink);  
  668.  }  
  669.  function fetch_array($query$result_type = MYSQL_ASSOC) {  
  670.   return mysql_fetch_array($query$result_type);  
  671.  }  
  672.  function fetch_first($sql) {  
  673.   return $this->fetch_array($this->query($sql));  
  674.  }  
  675.  function result_first($sql) {  
  676.   return $this->result($this->query($sql), 0);  
  677.  }  
  678.  function query($sql$type = '') {  
  679.   if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {  
  680.    $starttime = dmicrotime();  
  681.   }  
  682.   $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?  
  683.   'mysql_unbuffered_query' : 'mysql_query';  
  684.   if(!($query = $func($sql$this->curlink))) {  
  685.    if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {  
  686.     $this->connect();  
  687.     return $this->query($sql'RETRY'.$type);  
  688.    }  
  689.    if($type != 'SILENT' && substr($type, 5) != 'SILENT') {  
  690.     $this->halt('query_error'$sql);  
  691.    }  
  692.   }  
  693.   if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {  
  694.    $this->sqldebug[] = array($sql, number_format((dmicrotime() - $starttime), 6), debug_backtrace());  
  695.   }  
  696.   $this->querynum++;  
  697.   return $query;  
  698.  }  
  699.  function affected_rows() {  
  700.   return mysql_affected_rows($this->curlink);  
  701.  }  
  702.  function error() {  
  703.   return (($this->curlink) ? mysql_error($this->curlink) : mysql_error());  
  704.  }  
  705.  function errno() {  
  706.   return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno());  
  707.  }  
  708.  function result($query$row = 0) {  
  709.   $query = @mysql_result($query$row);  
  710.   return $query;  
  711.  }  
  712.  function num_rows($query) {  
  713.   $query = mysql_num_rows($query);  
  714.   return $query;  
  715.  }  
  716.  function num_fields($query) {  
  717.   return mysql_num_fields($query);  
  718.  }  
  719.  function free_result($query) {  
  720.   return mysql_free_result($query);  
  721.  }  
  722.  function insert_id() {  
  723.   return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);  
  724.  }  
  725.  function fetch_row($query) {  
  726.   $query = mysql_fetch_row($query);  
  727.   return $query;  
  728.  }  
  729.  function fetch_fields($query) {  
  730.   return mysql_fetch_field($query);  
  731.  }  
  732.  function version() {  
  733.   if(empty($this->version)) {  
  734.    $this->version = mysql_get_server_info($this->curlink);  
  735.   }  
  736.   return $this->version;  
  737.  }  
  738.  function close() {  
  739.   return mysql_close($this->curlink);  
  740.  }  
  741.  function halt($message = ''$sql = '') {  
  742.   require_once libfile('class/error');  
  743.   discuz_error::db_error($message$sql);  
  744.  }  
  745. }  
  746. class DB   //数据库操作  
  747. class discuz_session   //session类  
  748. class discuz_memory   //缓存初始化 支持eaccelerator/xcach/memcache   
  749.   
  750. ?>  

原创粉丝点击