設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 820|回復: 0
打印 上一主題 下一主題

【discuzX2】/source/class/class_core.php discuz_core类分析

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2015-2-8 23:26:17 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  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 24487 2011-09-21 08:13:57Z monkey $
  7. */

  8. define('IN_DISCUZ', true);//禁止用户直接通过路径访问
  9. error_reporting(0);//设置错误级别

  10. //通用基础类
  11. class discuz_core {

  12.         var $db = null;//数据库db类实例化对象

  13.         var $mem = null;//memcached缓存对象

  14.         var $session = null;//session变量

  15.         var $config = array();//配置信息
  16.         
  17.         //$_G数组的映射
  18.         var $var = array();//变量数组,将超级全局变量$_G的引用赋值给了$var变量

  19.         //加载缓存数组
  20.         var $cachelist = array();//缓存列表

  21.         //是否初始化
  22.         var $init_setting = true;//初始化设置
  23.         var $init_user = true;//初始化用户信息
  24.         var $init_session = true;//初始化session信息
  25.         var $init_cron = true;
  26.         var $init_misc = true;
  27.         var $init_memory = true;//初始化内存情况
  28.         var $init_mobile = true;

  29.         //是否已经初始化
  30.         var $initated = false;//初始化工作为完成标志

  31.         //列举全局变量,为清理做准备
  32.         var $superglobal = array(//自定义超级全局变量
  33.                 'GLOBALS' => 1,
  34.                 '_GET' => 1,
  35.                 '_POST' => 1,
  36.                 '_REQUEST' => 1,
  37.                 '_COOKIE' => 1,
  38.                 '_SERVER' => 1,
  39.                 '_ENV' => 1,
  40.                 '_FILES' => 1,
  41.         );

  42.         //建立唯一的进程
  43.         function &instance() {//单例模式实例化一个discuz_core核心类实例化对象
  44.                 static $object;
  45.                 if(empty($object)) {
  46.                         $object = new discuz_core();//实例化一个discuz_core对象
  47.                 }
  48.                 return $object;//返回唯一的一个discuz_core类实例化对象
  49.         }

  50.         //预处理的调用
  51.         function discuz_core() {//构造函数
  52.                 $this->_init_env();//初始化环境变量
  53.                 $this->_init_config();//初始化配置变量
  54.                 $this->_init_input();//初始化输入
  55.                 $this->_init_output();//初始化输出
  56.         }

  57.         //核心的初始化
  58.         function init() {
  59.                 if(!$this->initated) {
  60.                         $this->_init_db();//数据库操作类实例化对象的初始化
  61.                         $this->_init_memory();//初始化memcache
  62.                         $this->_init_user();//用户信息初始化
  63.                         $this->_init_session();//session操作初始化
  64.                         $this->_init_setting();//系统设置初始化
  65.                         $this->_init_mobile();//手机功能初始化
  66.                         $this->_init_cron();//计划任务初始化
  67.                         $this->_init_misc();//其他功能的初始化
  68.                 }
  69.                 $this->initated = true;//初始化完成的标志
  70.         }

  71.         //定义php环境信息常量和$_G全局变量
  72.         /*
  73.          * 1、主要讲$_G变量的引用赋值给$var数组变量
  74.          * 2、注意:G变量是程序的全局变量,为了让程序更加高效,减少不必要的数据获取,所以程序特将经常需要用到的变量统一到
  75.          *    G变量下,如:用户登录信息、后台设置信息、服务器环境信息、客户端cookies、数据缓存等都存放在G变量里面,在制作
  76.          *    模板文件的时只需将G变量打印出来即可获得需要的信息是否在G变量中
  77.          * 3、自定义变量:自定义变量是以$开头并且首位为字母或下划线的变量,
  78.          *    如:$data、$thread、$post、$forumlist、$threadlist
  79.          * 4、类似$_G['gp_xxx']变量都是get和post过来的数据
  80.          */
  81.         function _init_env() {
  82.                 //设置错误级别
  83.                 error_reporting(E_ERROR);
  84.                 if(PHP_VERSION < '5.3.0') {
  85.                         set_magic_quotes_runtime(0);//设置set_magic_quotes_runtime
  86.                 }

  87.                 define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -12));//定义根目录常量:"d:/wamp/www/discuz/"
  88.                 define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc());//定义MAGIC_QUOTES_GPC常量
  89.                 define('ICONV_ENABLE', function_exists('iconv'));//定义是否支持转码函数常量,如:iconv("gb2312","utf-8","我爱卡");//将gb2312编码转换为utf-8编码
  90.                 define('MB_ENABLE', function_exists('mb_convert_encoding'));//定义是否支持转码函数常量,跟iconv用法差不多,有稍微差异
  91.                 define('EXT_OBGZIP', function_exists('ob_gzhandler'));//缓存输出句柄函数

  92.                 define('TIMESTAMP', time());//定义当前时间戳常量
  93.                 $this->timezone_set();//设置时区
  94.                
  95.                 /*
  96.                  * 1、加载系统核心函数库文件
  97.                  * 2、条件:常量未定义、系统函数库未加载的情况下报"系统核心函数库文件丢失"的错误,否则加载系统核心函数库文件
  98.                  */
  99.                 if(!defined('DISCUZ_CORE_FUNCTION') && !@include(DISCUZ_ROOT.'./source/function/function_core.php')) {
  100.                         exit('function_core.php is missing');//退出,并报"系统核心函数库文件丢失的错误"
  101.                 }

  102.                 if(function_exists('ini_get')) {//获取php.ini配置文件中设置的配置信息
  103.                         $memorylimit = @ini_get('memory_limit');//设置内存使用限制
  104.                         if($memorylimit && return_bytes($memorylimit) < 33554432 && function_exists('ini_set')) {
  105.                                 ini_set('memory_limit', '128m');//如果小于32M,则增加到128M
  106.                         }
  107.                 }

  108.                 define('IS_ROBOT', checkrobot());//检测机器人

  109.                 //$GLOBALS:超全局变量,全局作用域中始终可用的内置变量,在函数和方法中无需使用 global $xxx声明
  110.                 foreach ($GLOBALS as $key => $value) {
  111.                         if (!isset($this->superglobal[$key])) {//注销没有在$superglobal中出现的超全局变量,也就是注销所有的超级全局变量
  112.                                 $GLOBALS[$key] = null; unset($GLOBALS[$key]);//设置为null并销毁
  113.                         }
  114.                 }

  115.                 //超级变量大数组$_G的定义,在模板文件中要使用
  116.                 global $_G;//函数体外可以使用,注意:只能在本文件中,或者include的文件中使用,并不能再整个网站中使用
  117.                 $_G = array(
  118.                         'uid' => 0,//作者UID
  119.                         'username' => '',//用户名
  120.                         'adminid' => 0,//管理组ID
  121.                         'groupid' => 1,//用户组ID
  122.                         'sid' => '',//cookie和session相关的sid
  123.                         'formhash' => '',//表单验证认证
  124.                         'timestamp' => TIMESTAMP,//时间戳
  125.                         'starttime' => dmicrotime(),//开始时间
  126.                         'clientip' => $this->_get_client_ip(),//客户端ip
  127.                         'referer' => '',//来路
  128.                         'charset' => '',//字符编码设置
  129.                         'gzipcompress' => '',//gzip
  130.                         'authkey' => '',//密钥
  131.                         'timenow' => array(),

  132.                         'PHP_SELF' => '',//当前php脚本文件,如:"/discuz/forum.php"
  133.                         'siteurl' => '',//站点url
  134.                         'siteroot' => '',//站点根目录
  135.                         'siteport' => '',//站点端口

  136.                         'config' => array(),//配置变量数组
  137.                         'setting' => array(),//设置变量数组
  138.                         'member' => array(),//用户信息数组
  139.                         'group' => array(),//用户组数组
  140.                         'cookie' => array(),//cookie数组
  141.                         'style' => array(),//风格数组
  142.                         'cache' => array(),//缓存列表数组
  143.                         'session' => array(),//session变量数组
  144.                         'lang' => array(),//语言包数组
  145.                         'my_app' => array(),//我的应用数组
  146.                         'my_userapp' => array(),//用户应用数组

  147.                         'fid' => 0,//版块id
  148.                         'tid' => 0,//帖子id
  149.                         'forum' => array(),//论坛版块数组
  150.                         'thread' => array(),//论坛相关帖子数组
  151.                         'rssauth' => '',//RSS订阅认证

  152.                         'home' => array(),//home功能相关数组
  153.                         'space' => array(),//space功能相关数组

  154.                         'block' => array(),//块信息数组
  155.                         'article' => array(),//文章相关

  156.                         'action' => array(
  157.                                 'action' => APPTYPEID,
  158.                                 'fid' => 0,//版块id
  159.                                 'tid' => 0,//帖子id
  160.                         ),

  161.                         'mobile' => '',//手机信息

  162.                 );
  163.                 $_G['PHP_SELF'] = htmlspecialchars($this->_get_script_url());//将当前脚本地址写入$_G超级变量中;结果:"/discuz/forum.php"
  164.                 $_G['basescript'] = CURSCRIPT;//当前不带扩展名的php脚本,如:"forum"
  165.                 $_G['basefilename'] = basename($_G['PHP_SELF']);//显示带有文件扩展名的php文件名称,如:"forum.php"
  166.                 $sitepath = substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], '/'));//如:"/discuz"
  167.                 if(defined('IN_API')) {
  168.                         $sitepath = preg_replace("/\/api\/?.*?$/i", '', $sitepath);
  169.                 } elseif(defined('IN_ARCHIVER')) {
  170.                         $sitepath = preg_replace("/\/archiver/i", '', $sitepath);
  171.                 }
  172.                 $_G['siteurl'] = htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].$sitepath.'/');//网站地址

  173.                 $url = parse_url($_G['siteurl']);
  174.                 $_G['siteroot'] = isset($url['path']) ? $url['path'] : '';//网站根目录,如:"/discuz"
  175.                 $_G['siteport'] = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];//端口

  176.                 if(defined('SUB_DIR')) {//二级目录设置情况
  177.                         $_G['siteurl'] = str_replace(SUB_DIR, '/', $_G['siteurl']);
  178.                         $_G['siteroot'] = str_replace(SUB_DIR, '/', $_G['siteroot']);
  179.                 }

  180.                 $this->var = & $_G;//$_G变量的引用赋值给$var,以后对$_G变量或$var变量的修改会直接影响到对方

  181.         }

  182.         /*
  183.          * 1、返回PHP_SELF当前脚本文件
  184.          */
  185.         function _get_script_url() {
  186.                 if($this->var['PHP_SELF'] === null){
  187.                         $scriptName = basename($_SERVER['SCRIPT_FILENAME']);
  188.                         if(basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
  189.                                 $this->var['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
  190.                         } else if(basename($_SERVER['PHP_SELF']) === $scriptName) {
  191.                                 $this->var['PHP_SELF'] = $_SERVER['PHP_SELF'];
  192.                         } else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
  193.                                 $this->var['PHP_SELF'] = $_SERVER['ORIG_SCRIPT_NAME'];
  194.                         } else if(($pos = strpos($_SERVER['PHP_SELF'],'/'.$scriptName)) !== false) {
  195.                                 $this->var['PHP_SELF'] = substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
  196.                         } else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT']) === 0) {
  197.                                 $this->var['PHP_SELF'] = str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
  198.                         } else {
  199.                                 system_error('request_tainting');
  200.                         }
  201.                 }
  202.                 return $this->var['PHP_SELF'];
  203.         }

  204.         /*
  205.          * 1、合并$_POST和$_GET,然后将$_POST和$_GET的值都赋予gp变量中,方便使用
  206.          * 2、禁止对全局变量注入
  207.          * 3、slashes处理
  208.          * 4、cookie处理:去掉cookie前缀
  209.          */
  210.         function _init_input() {
  211.                 if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) ||  isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) {
  212.                         system_error('request_tainting');//...请求中...
  213.                 }

  214.                 if(!MAGIC_QUOTES_GPC) {//魔术函数是否开启:仅仅对$_GET、$_POST、$_COOKIE起作用;主要用于在讲数据入库前做一些安全性的转义
  215.                         $_GET = daddslashes($_GET);//对$_GET数据进行转义
  216.                         $_POST = daddslashes($_POST);//对$_POST数据进行转义
  217.                         $_COOKIE = daddslashes($_COOKIE);//对$_COOKIE数据进行转义
  218.                         $_FILES = daddslashes($_FILES);//对$_FILES数据进行转义
  219.                 }

  220.                 /*
  221.                  * 1、如果cookie的键值等于定义的键值,那么截取cookie的前缀cookiepre
  222.                  */
  223.                 $prelength = strlen($this->config['cookie']['cookiepre']);
  224.                 foreach($_COOKIE as $key => $val) {
  225.                         if(substr($key, 0, $prelength) == $this->config['cookie']['cookiepre']) {
  226.                                 $this->var['cookie'][substr($key, $prelength)] = $val;//cookie赋值
  227.                         }
  228.                 }

  229.                 /*
  230.                  * 1、合并$_POST和$_GET,然后将$_POST和$_GET的值都赋予gp变量中,方便使用
  231.                  */
  232.                 if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {
  233.                         $_GET = array_merge($_GET, $_POST);
  234.                 }
  235.                
  236.                 //diy
  237.                 if(isset($_GET['diy'])) {
  238.                         $_GET['diy'] = empty($_GET['diy']) ? '' : $_GET['diy'];
  239.                 }

  240.                 foreach($_GET as $k => $v) {
  241.                         $this->var['gp_'.$k] = $v;//将$_POST和$_GET的值都赋予gp变量中,方便使用
  242.                 }

  243.                 //获取$mod变量,如:/?mod=xxx ,那么$this->var['mod']=xxx;
  244.                 $this->var['mod'] = empty($this->var['gp_mod']) ? '' : htmlspecialchars($this->var['gp_mod']);
  245.                 //是否需要ajax方式
  246.                 $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));
  247.                 //页面获取,最小为1
  248.                 $this->var['page'] = empty($this->var['gp_page']) ? 1 : max(1, intval($this->var['gp_page']));
  249.                 //cookie与session相关的sid的获取
  250.                 $this->var['sid'] = $this->var['cookie']['sid'] = isset($this->var['cookie']['sid']) ? htmlspecialchars($this->var['cookie']['sid']) : '';
  251.                 $this->var['gp_handlekey'] = !empty($this->var['gp_handlekey']) && preg_match('/^\w+$/', $this->var['gp_handlekey']) ? $this->var['gp_handlekey'] : '';

  252.                 if(empty($this->var['cookie']['saltkey'])) {//解密时可能会使用到
  253.                         $this->var['cookie']['saltkey'] = random(8);
  254.                         dsetcookie('saltkey', $this->var['cookie']['saltkey'], 86400 * 30, 1, 1);
  255.                 }
  256.                 //密钥
  257.                 $this->var['authkey'] = md5($this->var['config']['security']['authkey'].$this->var['cookie']['saltkey']);
  258.         }

  259.         /*
  260.          * 1、主要讲$config引用赋值给$config=array()和 $var['config']=$config;
  261.          * 2、加载配置文件
  262.          */
  263.         function _init_config() {

  264.                 $_config = array();//定义$_config配置数组
  265.                 @include DISCUZ_ROOT.'./config/config_global.php';//加载全局配置文件,配置文件中是一个$_config数组
  266.                 if(empty($_config)) {//配置信息为空的情况,安装前为空
  267.                         if(!file_exists(DISCUZ_ROOT.'./data/install.lock')) {//没有安装的情况
  268.                                 header('location: install');//安装discuz论坛系统
  269.                                 exit;
  270.                         } else {
  271.                                 system_error('config_notfound');
  272.                         }
  273.                 }

  274.                 //设置安全认证的authkey
  275.                 if(empty($_config['security']['authkey'])) {
  276.                         $_config['security']['authkey'] = md5($_config['cookie']['cookiepre'].$_config['db'][1]['dbname']);//密钥的组成:cookie前缀+数据库的名称
  277.                 }

  278.                 //是否调试模式
  279.                 if(empty($_config['debug']) || !file_exists(libfile('function/debug'))) {
  280.                         define('DISCUZ_DEBUG', false);//调试模式关闭
  281.                 } elseif($_config['debug'] === 1 || $_config['debug'] === 2 || !empty($_REQUEST['debug']) && $_REQUEST['debug'] === $_config['debug']) {
  282.                         define('DISCUZ_DEBUG', true);//调试模式打开
  283.                         if($_config['debug'] == 2) {
  284.                                 error_reporting(E_ALL);//设置错误级别
  285.                         }
  286.                 } else {
  287.                         define('DISCUZ_DEBUG', false);
  288.                 }

  289.                 //定义静态文件常量:方便找到images、css、js素材文件
  290.                 define('STATICURL', !empty($_config['output']['staticurl']) ? $_config['output']['staticurl'] : 'static/');
  291.                 $this->var['staticurl'] = STATICURL;//素材文件url:主要用于存储images、css、js等素材文件

  292.                 $this->config = & $_config;//配置信息的引用赋值给$config数组变量
  293.                 $this->var['config'] = & $_config;//同时配置信息的引用赋值给$var['config'],即:赋值给了$_G['config']

  294.                 //设置cookie域,一般是设置目录域,"/"不存在则加上"/"
  295.                 if(substr($_config['cookie']['cookiepath'], 0, 1) != '/') {//cookie路径
  296.                         $this->var['config']['cookie']['cookiepath'] = '/'.$this->var['config']['cookie']['cookiepath'];
  297.                 }
  298.                 //定义cookie前缀,如:定义为xxx_,则为$cookie[xxx_uid]
  299.                 $this->var['config']['cookie']['cookiepre'] = $this->var['config']['cookie']['cookiepre'].substr(md5($this->var['config']['cookie']['cookiepath'].'|'.$this->var['config']['cookie']['cookiedomain']), 0, 4).'_';

  300.         }

  301.         /*
  302.          * 1、输出处理
  303.          * 2、get参数跨站检测
  304.          * 3、防页面刷新
  305.          * 4、gzip处理
  306.          * 5、字符集处理
  307.          */
  308.         function _init_output() {

  309.                 if($this->config['security']['urlxssdefend'] && $_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_SERVER['REQUEST_URI'])) {
  310.                         $this->_xss_check();
  311.                 }

  312.                 /*
  313.                  * 1、验证码的设置:加载include/misc/security.php文件,验证功能
  314.                  */
  315.                 if($this->config['security']['attackevasive'] && (!defined('CURSCRIPT') || !in_array($this->var['mod'], array('seccode', 'secqaa', 'swfupload')))) {
  316.                         require_once libfile('misc/security', 'include');
  317.                 }

  318.                 /*
  319.                  * 1、是否开启gzip,如果不支持gzip,则定义为false
  320.                  */
  321.                 if(!empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) {
  322.                         $this->config['output']['gzip'] = false;
  323.                 }

  324.                 //将$allowgzip写入全局变量中
  325.                 $allowgzip = $this->config['output']['gzip'] && empty($this->var['inajax']) && $this->var['mod'] != 'attachment' && EXT_OBGZIP;
  326.                 setglobal('gzipcompress', $allowgzip);
  327.                 ob_start($allowgzip ? 'ob_gzhandler' : null);//定义输出缓存

  328.                 //将配置文件中的字符集写入全局变量中
  329.                 setglobal('charset', $this->config['output']['charset']);
  330.                 define('CHARSET', $this->config['output']['charset']);
  331.                 if($this->config['output']['forceheader']) {
  332.                         @header('Content-Type: text/html; charset='.CHARSET);//设置网页编码,强制输出
  333.                 }

  334.         }

  335.         /*
  336.          * 1、拒绝机器人访问,设置为403错误
  337.          */
  338.         function reject_robot() {
  339.                 if(IS_ROBOT) {
  340.                         exit(header("HTTP/1.1 403 Forbidden"));
  341.                 }
  342.         }

  343.         /*
  344.          * 1、检查xss漏洞、ubb漏洞
  345.          * 2、get参数跨站检测
  346.          */
  347.         function _xss_check() {
  348.                 $temp = strtoupper(urldecode(urldecode($_SERVER['REQUEST_URI'])));
  349.                 if(strpos($temp, '<') !== false || strpos($temp, '"') !== false || strpos($temp, 'CONTENT-TRANSFER-ENCODING') !== false) {
  350.                         system_error('request_tainting');
  351.                 }
  352.                 return true;
  353.         }

  354.         /*
  355.          * 1、获取客户端的ip
  356.          */
  357.         function _get_client_ip() {
  358.                 $ip = $_SERVER['REMOTE_ADDR'];
  359.                 if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
  360.                         $ip = $_SERVER['HTTP_CLIENT_IP'];
  361.                 } 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)) {
  362.                         foreach ($matches[0] AS $xip) {
  363.                                 if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
  364.                                         $ip = $xip;
  365.                                         break;
  366.                                 }
  367.                         }
  368.                 }
  369.                 return $ip;
  370.         }

  371.         /*
  372.          * 1、初始化数据库类、同时选择主从数据库
  373.          */
  374.         function _init_db() {
  375.                 $class = 'db_mysql';
  376.                 if(count(getglobal('config/db/slave'))) {//是否存在从数据连接,存在则初始化
  377.                         require_once libfile('class/mysql_slave');
  378.                         $class = 'db_mysql_slave';
  379.                 }
  380.                 $this->db = & DB::object($class);
  381.                 $this->db->set_config($this->config['db']);
  382.                 $this->db->connect();//建立数据库连接
  383.         }

  384.         /*
  385.          * 1、初始化session信息
  386.          */
  387.         function _init_session() {
  388.                 $this->session = new discuz_session();

  389.                 if($this->init_session)        {
  390.                         $this->session->init($this->var['cookie']['sid'], $this->var['clientip'], $this->var['uid']);
  391.                         $this->var['sid'] = $this->session->sid;//设置sid
  392.                         $this->var['session'] = $this->session->var;//设置session

  393.                         if($this->var['sid'] != $this->var['cookie']['sid']) {
  394.                                 dsetcookie('sid', $this->var['sid'], 86400);//如果sid不为cookie中的sid,则重写sid到cookie
  395.                         }

  396.                         if($this->session->isnew) {
  397.                                 if(ipbanned($this->var['clientip'])) {//如果发现ip在禁止范围内,则设置该客户端用户组为6,即:禁止ip用户组
  398.                                         $this->session->set('groupid', 6);
  399.                                 }
  400.                         }

  401.                         if($this->session->get('groupid') == 6) {
  402.                                 $this->var['member']['groupid'] = 6;
  403.                                 sysmessage('user_banned');//提示ip禁止
  404.                         }

  405.                         /*
  406.                          * 1、最近活动检测,600秒
  407.                          */
  408.                         if($this->var['uid'] && ($this->session->isnew || ($this->session->get('lastactivity') + 600) < TIMESTAMP)) {
  409.                                 $this->session->set('lastactivity', TIMESTAMP);
  410.                                 if($this->session->isnew) {
  411.                                         //如果用户在600秒内不活动,则设置最后访问时间点
  412.                                         DB::update('common_member_status', array('lastip' => $this->var['clientip'], 'lastvisit' => TIMESTAMP), "uid='".$this->var['uid']."'");
  413.                                 }
  414.                         }

  415.                 }
  416.         }

  417.         /*
  418.          * 1、初始化当前用户信息
  419.          */
  420.         function _init_user() {
  421.                 if($this->init_user) {
  422.                         $discuz_uid = '';
  423.                         /*通过COOKIE取得用户信息 start*/
  424.                         /*TODO utf-8 2 GBK change start*/
  425.                         /*$_cookie_userName = $_COOKIE["USERNAME"];*/
  426.                         $username_tmp = $_COOKIE["USERNAME"];
  427.                         $a=urldecode($username_tmp);
  428.                         /*$a=mb_convert_encoding($a, 'GB2312', 'UTF-8'); */
  429.                         $a=diconv($a, 'UTF-8', 'GBK');
  430.                         $_cookie_userName = $a;
  431.                         /*TODO utf-8 2 GBK change start*/
  432.                         $_cookie_userPassWord = $_COOKIE["USERAUTHCODE"];
  433.                         /*通过COOKIE取得用户信息 end*/
  434.                         if(!empty($_cookie_userName) && !empty($_cookie_userPassWord)) {
  435.                                 /* 判断用户是不是在UCENTER中存在,如果不存在则插入一条数据同步到DIS论坛用户 START */
  436.                                 /* 用COOKIE用户名称检索UCENTER */
  437.                                 $query = DB::query("SELECT uid FROM ".DB::table('ucenter_members')." WHERE username = '$_cookie_userName' limit 1");
  438.                                 if(!DB::num_rows($query)) {
  439.                                                 // 插入用户中心数据
  440.                                                 DB::query( "INSERT INTO ".DB::table('ucenter_members')." (uid,username,password,email) VALUES ('$discuz_uid','$_cookie_userName','$_cookie_userPassWord','')");
  441.                                                 DB::query("commit;");
  442.                                                 $query = DB::query("SELECT uid FROM ".DB::table('ucenter_members')." WHERE username = '$_cookie_userName' limit 1");
  443.                                                 $discuz_uid = DB::result($query, 0);
  444.                                                 DB::query( "INSERT INTO ".DB::table('ucenter_memberfields')."(uid) VALUES ('$discuz_uid')");
  445.                                                 DB::query("commit;");
  446.                                                 /* 这个取得公用主键ID  创建UCENTER中用户,在DIS中再检索处理 */
  447.                                                 /* 取得论坛用户UID */
  448.                                                 $query = DB::query("SELECT uid FROM ".DB::table("common_member")." WHERE username = '$_cookie_userName' limit 1");
  449.                                                 if(DB::num_rows($query)) {
  450.                                                         $discuz_uid = DB::result($query, 0);
  451.                                                 } else {
  452.                                                         // 同步插入论坛数据
  453.                                                         DB::query( "INSERT INTO ".DB::table('common_member')." (uid,username,password,adminid,groupid,regdate,email) VALUES ('$discuz_uid','$_cookie_userName','$_cookie_userPassWord','0','10','1307062876','')");
  454.                                                         DB::query( "INSERT INTO ".DB::table('common_member_status')." (uid) VALUES ('$discuz_uid')");
  455.                                                         DB::query( "INSERT INTO ".DB::table('common_member_profile')."(uid)value('$discuz_uid')");
  456.                                                         DB::query( "INSERT INTO ".DB::table('common_member_field_forum')."(uid)value('$discuz_uid')");
  457.                                                         DB::query( "INSERT INTO ".DB::table('common_member_field_home')."(uid)VALUE('$discuz_uid')");
  458.                                                         DB::query( "INSERT INTO ".DB::table('common_member_count')."(uid,extcredits1,extcredits2,extcredits3,extcredits4,extcredits5,extcredits6,extcredits7,extcredits8)VALUE('$discuz_uid','0','0','0','0','0','0','0','0')");
  459.                                                         DB::query("commit;");
  460.                                                         $query = DB::query("SELECT uid FROM ".DB::table("common_member")." WHERE username = '$_cookie_userName' limit 1");
  461.                                                         $discuz_uid = DB::result($query, 0);
  462.                                                 }
  463.                                                 // 取得登录用户信息,写入COOKIE
  464.                                                 $user = getuserbyuid($discuz_uid);
  465.                                                 $ucsynlogin = $this->setting['allowsynlogin'] ? uc_user_synlogin($discuz_uid) : '';
  466.                                                 $this->var['member'] = $user;
  467.                           } else {
  468.                                           // 取得登录用户信息,写入COOKIE
  469.                                                 $discuz_uid = DB::result($query, 0);
  470.                                                 DB::query(" UPDATE ".DB::table('ucenter_members')." SET password='".$_cookie_userPassWord."' WHERE uid=".$discuz_uid);
  471.                                                 DB::query(" UPDATE ".DB::table('common_member')." SET password='".$_cookie_userPassWord."' WHERE uid=".$discuz_uid);
  472.                                                 DB::query("commit;");
  473.                                                 $user = getuserbyuid($discuz_uid);
  474.                                                 //var_dump($discuz_uid);
  475.                                                 $ucsynlogin = $this->setting['allowsynlogin'] ? uc_user_synlogin($discuz_uid) : '';
  476.                                                 //var_dump($ucsynlogin);
  477.                                     $this->var['member'] = $user;
  478.                           }
  479. /*判断用户是不是在UCENTER中存在,如果不存在则插入一条数据同步到DIS论坛用户 END */
  480.                         } else {
  481.                                 /*判断是否UC用户 来源CDB start*/
  482.                                 if($auth = getglobal('auth', 'cookie')) {//得到auth,auth的值:"username\tuid"的加密信息
  483.                                         //进行解密
  484.                                         $auth = daddslashes(explode("\t", authcode($auth, 'DECODE')));
  485.                                 }
  486.                                 //得到用户名和用户密码,如果auth为空,或者确实uid和username中的一个,则为空
  487.                                 list($discuz_pw, $discuz_uid) = empty($auth) || count($auth) < 2 ? array('', '') : $auth;
  488.                                
  489.                                 if($discuz_uid) {
  490.                                         $user = getuserbyuid($discuz_uid);//如果uid存在,则得到该用户信息
  491.                                 }
  492.                                
  493.                                 if(!empty($user) && $user['password'] == $discuz_pw) {
  494.                                         $this->var['member'] = $user;//如果用户存在且密码正确,则将用户信息写入全局变量中
  495.                                 } else {
  496.                                         /*游客判断 START*/
  497.                                                 $user = array();//user定义为空数组
  498.                                                 $this->_init_guest();//是否为游客,游客初始化方法
  499.                                         /*游客判断 END*/
  500.                                 }
  501.                           /*判断是否UC用户 来源CDB end*/
  502.                         }
  503.                     
  504.                         //用户组过期检测
  505.                         if($user && $user['groupexpiry'] > 0 && $user['groupexpiry'] < TIMESTAMP && getgpc('mod') != 'spacecp' && getgpc('do') != 'expiry' && CURSCRIPT != 'home') {
  506.                                 dheader('location: home.php?mod=spacecp&ac=usergroup&do=expiry');
  507.                         }

  508.                         //用户组数据缓存
  509.                         $this->cachelist[] = 'usergroup_'.$this->var['member']['groupid'];
  510.                         if($user && $user['adminid'] > 0 && $user['groupid'] != $user['adminid']) {
  511.                                 $this->cachelist[] = 'admingroup_'.$this->var['member']['adminid'];
  512.                         }

  513.                 } else {
  514.                         $this->_init_guest();//游客
  515.                 }

  516.                 if(empty($this->var['cookie']['lastvisit'])) {//cookie中如果记录有最后一次访问时间,则写入
  517.                         $this->var['member']['lastvisit'] = TIMESTAMP - 3600;
  518.                         dsetcookie('lastvisit', TIMESTAMP - 3600, 86400 * 30);
  519.                 } else {
  520.                         //否则写入全局变量
  521.                         $this->var['member']['lastvisit'] = $this->var['cookie']['lastvisit'];
  522.                 }
  523.                 setglobal('uid', getglobal('uid', 'member'));//用户uid
  524.                 setglobal('username', addslashes(getglobal('username', 'member')));//用户名
  525.                 setglobal('adminid', getglobal('adminid', 'member'));//管理组id
  526.                 setglobal('groupid', getglobal('groupid', 'member'));//用户组id
  527.         }

  528.         /*
  529.          * 1、初始化当前用户信息为游客
  530.          */
  531.         function _init_guest() {
  532.                 setglobal('member', array( 'uid' => 0, 'username' => '', 'adminid' => 0, 'groupid' => 7, 'credits' => 0, 'timeoffset' => 9999));
  533.         }

  534.         /*
  535.          * 1、处理计划任务
  536.          */
  537.         function _init_cron() {
  538.                 $ext = empty($this->config['remote']['on']) || empty($this->config['remote']['cron']) || APPTYPEID == 200;
  539.                 if($this->init_cron && $this->init_setting && $ext) {
  540.                         if($this->var['cache']['cronnextrun'] <= TIMESTAMP) {
  541.                                 require_once libfile('class/cron');//加载"/source/class/class_cron.php"文件
  542.                                 discuz_cron::run();//运行
  543.                         }
  544.                 }
  545.         }

  546.         /*
  547.          * 1、杂项
  548.          * 2、调入核心语言包
  549.          * 3、处理全局时区设置
  550.          * 4、处理被封禁用户
  551.          * 5、站点开关检查
  552.          * 6、手机端控制每页显示主题数和回帖数
  553.          * 7、判断并执行每日登陆奖励积分
  554.          */
  555.         function _init_misc() {
  556.                 if(!$this->init_misc) {
  557.                         return false;
  558.                 }
  559.                 lang('core');//加载core语言包

  560.                 //设置用户时区
  561.                 if($this->init_setting && $this->init_user) {
  562.                         if(!isset($this->var['member']['timeoffset']) || $this->var['member']['timeoffset'] == 9999 || $this->var['member']['timeoffset'] === '') {
  563.                                 $this->var['member']['timeoffset'] = $this->var['setting']['timeoffset'];
  564.                         }
  565.                 }

  566.                 $timeoffset = $this->init_setting ? $this->var['member']['timeoffset'] : $this->var['setting']['timeoffset'];
  567.                 $this->var['timenow'] = array(
  568.                         'time' => dgmdate(TIMESTAMP),
  569.                         'offset' => $timeoffset >= 0 ? ($timeoffset == 0 ? '' : '+'.$timeoffset) : $timeoffset
  570.                 );
  571.                 $this->timezone_set($timeoffset);

  572.                 $this->var['formhash'] = formhash();//得到formhash
  573.                 define('FORMHASH', $this->var['formhash']);//定义为常量

  574.                 if($this->init_user) {
  575.                         if($this->var['group'] && isset($this->var['group']['allowvisit']) && !$this->var['group']['allowvisit']) {
  576.                                 if($this->var['uid']) {
  577.                                         sysmessage('user_banned', null);//检测是否为禁止访问
  578.                                 } elseif((!defined('ALLOWGUEST') || !ALLOWGUEST) && !in_array(CURSCRIPT, array('member', 'api')) && !$this->var['inajax']) {
  579.                                         dheader('location: member.php?mod=logging&action=login&referer='.rawurlencode($_SERVER['REQUEST_URI']));
  580.                                 }
  581.                         }
  582.                         if($this->var['member']['status'] == -1) {
  583.                                 sysmessage('user_banned', null);//如果用户状态为-1,则提示禁止访问
  584.                         }
  585.                 }

  586.                 if($this->var['setting']['ipaccess'] && !ipaccess($this->var['clientip'], $this->var['setting']['ipaccess'])) {
  587.                         sysmessage('user_banned', null);//ip权限检测
  588.                 }

  589.                 //如果论坛为关闭,只有管理员可以访问,其他则提示关闭原因
  590.                 if($this->var['setting']['bbclosed']) {
  591.                         if($this->var['uid'] && ($this->var['group']['allowvisit'] == 2 || $this->var['groupid'] == 1)) {
  592.                         } elseif(in_array(CURSCRIPT, array('admin', 'member', 'api')) || defined('ALLOWGUEST') && ALLOWGUEST) {
  593.                         } else {
  594.                                 $closedreason = DB::result_first("SELECT svalue FROM ".DB::table('common_setting')." WHERE skey='closedreason'");
  595.                                 $closedreason = str_replace(':', ':', $closedreason);
  596.                                 showmessage($closedreason ? $closedreason : 'board_closed', NULL, array('adminemail' => $this->var['setting']['adminemail']), array('login' => 1));
  597.                         }
  598.                 }

  599.                 if(CURSCRIPT != 'admin' && !(in_array($this->var['mod'], array('logging', 'seccode')))) {
  600.                         periodscheck('visitbanperiods');//私密板块访问设置
  601.                 }

  602.                 //wap访问设置
  603.                 if(defined('IN_MOBILE')) {
  604.                         $this->var['tpp'] = $this->var['setting']['mobile']['mobiletopicperpage'] ? intval($this->var['setting']['mobile']['mobiletopicperpage']) : 20;
  605.                         $this->var['ppp'] = $this->var['setting']['mobile']['mobilepostperpage'] ? intval($this->var['setting']['mobile']['mobilepostperpage']) : 5;
  606.                 } else {
  607.                         $this->var['tpp'] = $this->var['setting']['topicperpage'] ? intval($this->var['setting']['topicperpage']) : 20;
  608.                         $this->var['ppp'] = $this->var['setting']['postperpage'] ? intval($this->var['setting']['postperpage']) : 10;
  609.                 }

  610.                 //header头信息设置
  611.                 if($this->var['setting']['nocacheheaders']) {
  612.                         @header("Expires: -1");
  613.                         @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
  614.                         @header("Pragma: no-cache");
  615.                 }

  616.                 if($this->session->isnew && $this->var['uid']) {
  617.                         updatecreditbyaction('daylogin', $this->var['uid']);//每日登陆增加积分设置

  618.                         include_once libfile('function/stat');
  619.                         updatestat('login', 1);
  620.                         if(defined('IN_MOBILE')) {
  621.                                 updatestat('mobilelogin', 1);
  622.                         }
  623.                         if($this->var['setting']['connect']['allow'] && $this->var['member']['conisbind']) {
  624.                                 updatestat('connectlogin', 1);
  625.                         }
  626.                 }
  627.                 if($this->var['member']['conisbind'] && $this->var['setting']['connect']['newbiespan'] !== '') {
  628.                         $this->var['setting']['newbiespan'] = $this->var['setting']['connect']['newbiespan'];
  629.                 }

  630.                 $lastact = TIMESTAMP."\t".htmlspecialchars(basename($this->var['PHP_SELF']))."\t".htmlspecialchars($this->var['mod']);
  631.                 dsetcookie('lastact', $lastact, 86400);
  632.                 setglobal('currenturl_encode', base64_encode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));

  633.                 if((!empty($this->var['gp_fromuid']) || !empty($this->var['gp_fromuser'])) && ($this->var['setting']['creditspolicy']['promotion_visit'] || $this->var['setting']['creditspolicy']['promotion_register'])) {
  634.                         require_once libfile('misc/promotion', 'include');
  635.                 }

  636.                 //SEO关键词、描述
  637.                 $this->var['seokeywords'] = !empty($this->var['setting']['seokeywords'][CURSCRIPT]) ? $this->var['setting']['seokeywords'][CURSCRIPT] : '';
  638.                 $this->var['seodescription'] = !empty($this->var['setting']['seodescription'][CURSCRIPT]) ? $this->var['setting']['seodescription'][CURSCRIPT] : '';

  639.         }

  640.         /*
  641.          * 1、加载全局设置 setting 、风格 style 、 下一个任务 cronnextrun
  642.          */
  643.         function _init_setting() {
  644.                 if($this->init_setting) {
  645.                         if(empty($this->var['setting'])) {
  646.                                 $this->cachelist[] = 'setting';//缓存设置文件
  647.                         }

  648.                         if(empty($this->var['style'])) {
  649.                                 $this->cachelist[] = 'style_default';//风格缓存设置
  650.                         }

  651.                         if(!isset($this->var['cache']['cronnextrun'])) {
  652.                                 $this->cachelist[] = 'cronnextrun';//缓存计划任务
  653.                         }
  654.                 }

  655.                 !empty($this->cachelist) && loadcache($this->cachelist);

  656.                 if(!is_array($this->var['setting'])) {
  657.                         $this->var['setting'] = array();
  658.                 }

  659.                 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']])) {
  660.                         $this->var['group'] = array_merge($this->var['group'], $this->var['cache']['admingroup_'.$this->var['member']['adminid']]);
  661.                 }
  662.         }

  663.         /*
  664.          * 1、处理当前界面风格
  665.          * 2、定义风格常量
  666.          */
  667.         function _init_style() {
  668.                 $styleid = !empty($this->var['cookie']['styleid']) ? $this->var['cookie']['styleid'] : 0;
  669.                 if(intval(!empty($this->var['forum']['styleid']))) {
  670.                         $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['forum']['styleid'];
  671.                 } elseif(intval(!empty($this->var['category']['styleid']))) {
  672.                         $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['category']['styleid'];
  673.                 }

  674.                 $styleid = intval($styleid);

  675.                 if($styleid && $styleid != $this->var['setting']['styleid']) {
  676.                         loadcache('style_'.$styleid);
  677.                         if($this->var['cache']['style_'.$styleid]) {
  678.                                 $this->var['style'] = $this->var['cache']['style_'.$styleid];
  679.                         }
  680.                 }

  681.                 define('IMGDIR', $this->var['style']['imgdir']);
  682.                 define('STYLEID', $this->var['style']['styleid']);
  683.                 define('VERHASH', $this->var['style']['verhash']);
  684.                 define('TPLDIR', $this->var['style']['tpldir']);
  685.                 define('TEMPLATEID', $this->var['style']['templateid']);
  686.         }

  687.         //初始化discuz内存读写引擎
  688.         function _init_memory() {
  689.                 $this->mem = new discuz_memory();
  690.                 if($this->init_memory) {
  691.                         $this->mem->init($this->config['memory']);
  692.                 }
  693.                 $this->var['memory'] = $this->mem->type;
  694.         }
  695.         //手机访问设置
  696.         function _init_mobile() {
  697.                 if(!$this->var['setting'] || !$this->init_mobile || !$this->var['setting']['mobile']['allowmobile'] || !is_array($this->var['setting']['mobile']) || IS_ROBOT) {
  698.                         $nomobile = true;//允许手机访问
  699.                         $unallowmobile = true;
  700.                 }

  701.                 if($_GET['mobile'] === 'no') {
  702.                         dsetcookie('mobile', 'no', 3600);
  703.                         $nomobile = true;
  704.                 } elseif($this->var['cookie']['mobile'] == 'no' && $_GET['mobile'] === 'yes') {
  705.                         dsetcookie('mobile', '');
  706.                 } elseif($this->var['cookie']['mobile'] == 'no') {
  707.                         $nomobile = true;
  708.                 }

  709.                 if(!checkmobile()) {//检测是否为手机访问
  710.                         $nomobile = true;
  711.                 }

  712.                 if($this->var['setting']['mobile']['mobilepreview'] && !$this->var['mobile'] && !$unallowmobile) {
  713.                         if($_GET['mobile'] === 'yes') {
  714.                                 dheader("Location:misc.php?mod=mobile");
  715.                         }
  716.                 }

  717.                 if($nomobile || (!$this->var['setting']['mobile']['mobileforward'] && $_GET['mobile'] !== 'yes')) {
  718.                         if($_SERVER['HTTP_HOST'] == $this->var['setting']['domain']['app']['mobile'] && $this->var['setting']['domain']['app']['default']) {
  719.                                 dheader("Location:http://".$this->var['setting']['domain']['app']['default'].$_SERVER['REQUEST_URI']);
  720.                         } else {
  721.                                 return;
  722.                         }
  723.                 }

  724.                 if(strpos($this->var['setting']['domain']['defaultindex'], CURSCRIPT) !== false && CURSCRIPT != 'forum' && !$_GET['mod']) {
  725.                         if($this->var['setting']['domain']['app']['mobile']) {
  726.                                 $mobileurl = 'http://'.$this->var['setting']['domain']['app']['mobile'];
  727.                         } else {
  728.                                 if($this->var['setting']['domain']['app']['forum']) {
  729.                                         $mobileurl = 'http://'.$this->var['setting']['domain']['app']['forum'].'?mobile=yes';
  730.                                 } else {
  731.                                         $mobileurl = $this->var['siteurl'].'forum.php?mobile=yes';
  732.                                 }
  733.                         }
  734.                         dheader("location:$mobileurl");
  735.                 }
  736.                 define('IN_MOBILE', true);
  737.                 setglobal('gzipcompress', 0);

  738.                 $arr = array(strstr($_SERVER['QUERY_STRING'], '&simpletype'), strstr($_SERVER['QUERY_STRING'], 'simpletype'), '&mobile=yes', 'mobile=yes');
  739.                 $query_sting_tmp = str_replace($arr, '', $_SERVER['QUERY_STRING']);
  740.                 $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';

  741.                 $this->var['setting']['lazyload'] = 0;

  742.                 if('utf-8' != CHARSET) {
  743.                         if(strtolower($_SERVER['REQUEST_METHOD']) === 'post') {
  744.                                 foreach($_POST AS $pk => $pv) {
  745.                                         if(!is_numeric($pv)) {
  746.                                                 $this->var['gp_'.$pk] = $_GET[$pk] = $_POST[$pk] = $this->mobile_iconv_recurrence($pv);
  747.                                         }
  748.                                 }
  749.                         }
  750.                 }

  751.                 if($_GET['simpletype']) {
  752.                         if($_GET['simpletype'] == 'yes') {
  753.                                 $this->var['setting']['mobile']['mobilesimpletype'] = 1;
  754.                                 dsetcookie('simpletype', 1, 86400);
  755.                         } else {
  756.                                 $this->var['setting']['mobile']['mobilesimpletype'] = 0;
  757.                                 dsetcookie('simpletype', 0, 86400);
  758.                         }
  759.                 } elseif($this->var['cookie']['simpletype']) {
  760.                         $this->var['setting']['mobile']['mobilesimpletype'] = $this->var['cookie']['simpletype'] == 1 ? 1 : 0 ;
  761.                 }

  762.                 if(!$this->var['setting']['mobile']['mobilesimpletype']) {
  763.                         $this->var['setting']['imagemaxwidth'] = 224;
  764.                 }

  765.                 $this->var['setting']['regstatus'] = $this->var['setting']['mobile']['mobileregister'] ? $this->var['setting']['regstatus'] : 0 ;
  766.                 if(!$this->var['setting']['mobile']['mobileseccode']) {
  767.                         $this->var['setting']['seccodestatus'] = 0;
  768.                 }

  769.                 $this->var['setting']['seccodedata']['type'] = 99;
  770.                 $this->var['setting']['thumbquality'] = 50;


  771.                 $this->var['setting']['mobile']['simpletypeurl'] = array();
  772.                 $this->var['setting']['mobile']['simpletypeurl'][0] = $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'mobile=yes&simpletype=no';
  773.                 $this->var['setting']['mobile']['simpletypeurl'][1] =  $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'mobile=yes&simpletype=yes';
  774.                 unset($query_sting_tmp);
  775.                 ob_start();
  776.         }

  777.         //时区设置
  778.         function timezone_set($timeoffset = 0) {
  779.                 if(function_exists('date_default_timezone_set')) {
  780.                         @date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset)));
  781.                 }
  782.         }

  783.         //手机访问再次转码
  784.         function mobile_iconv_recurrence($value) {
  785.                 if(is_array($value)) {
  786.                         foreach($value AS $key => $val) {
  787.                                 $value[$key] = $this->mobile_iconv_recurrence($val);
  788.                         }
  789.                 } else {
  790.                         $value = addslashes(diconv(stripslashes($value), 'utf-8', CHARSET));
  791.                 }
  792.                 return $value;
  793.         }
  794. }

  795. /**
  796. * Discuz MySQL 类的支持 程序中不直接使用
  797. *
  798. */
  799. class db_mysql
  800. {
  801.         var $tablepre;
  802.         var $version = '';
  803.         var $querynum = 0;
  804.         var $slaveid = 0;
  805.         var $curlink;
  806.         var $link = array();
  807.         var $config = array();
  808.         var $sqldebug = array();
  809.         var $map = array();

  810.         function db_mysql($config = array()) {
  811.                 if(!empty($config)) {
  812.                         $this->set_config($config);
  813.                 }
  814.         }

  815.         function set_config($config) {
  816.                 $this->config = &$config;
  817.                 $this->tablepre = $config['1']['tablepre'];
  818.                 if(!empty($this->config['map'])) {
  819.                         $this->map = $this->config['map'];
  820.                 }
  821.         }

  822.         function connect($serverid = 1) {

  823.                 if(empty($this->config) || empty($this->config[$serverid])) {
  824.                         $this->halt('config_db_not_found');
  825.                 }

  826.                 $this->link[$serverid] = $this->_dbconnect(
  827.                         $this->config[$serverid]['dbhost'],
  828.                         $this->config[$serverid]['dbuser'],
  829.                         $this->config[$serverid]['dbpw'],
  830.                         $this->config[$serverid]['dbcharset'],
  831.                         $this->config[$serverid]['dbname'],
  832.                         $this->config[$serverid]['pconnect']
  833.                         );
  834.                 $this->curlink = $this->link[$serverid];

  835.         }

  836.         function _dbconnect($dbhost, $dbuser, $dbpw, $dbcharset, $dbname, $pconnect) {
  837.                 $link = null;
  838.                 $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
  839.                 if(!$link = @$func($dbhost, $dbuser, $dbpw, 1)) {
  840.                         $this->halt('notconnect');
  841.                 } else {
  842.                         $this->curlink = $link;
  843.                         if($this->version() > '4.1') {
  844.                                 $dbcharset = $dbcharset ? $dbcharset : $this->config[1]['dbcharset'];
  845.                                 $serverset = $dbcharset ? 'character_set_connection='.$dbcharset.', character_set_results='.$dbcharset.', character_set_client=binary' : '';
  846.                                 $serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',').'sql_mode=\'\'') : '';
  847.                                 $serverset && mysql_query("SET $serverset", $link);
  848.                         }
  849.                         $dbname && @mysql_select_db($dbname, $link);
  850.                 }
  851.                 return $link;
  852.         }

  853.         function table_name($tablename) {
  854.                 if(!empty($this->map) && !empty($this->map[$tablename])) {
  855.                         $id = $this->map[$tablename];
  856.                         if(!$this->link[$id]) {
  857.                                 $this->connect($id);
  858.                         }
  859.                         $this->curlink = $this->link[$id];
  860.                         return $this->config[$id]['tablepre'].$tablename;
  861.                 } else {
  862.                         $this->curlink = $this->link[1];
  863.                 }
  864.                 return $this->tablepre.$tablename;
  865.         }

  866.         function select_db($dbname) {
  867.                 return mysql_select_db($dbname, $this->curlink);
  868.         }

  869.         function fetch_array($query, $result_type = MYSQL_ASSOC) {
  870.                 return mysql_fetch_array($query, $result_type);
  871.         }

  872.         function fetch_first($sql) {
  873.                 return $this->fetch_array($this->query($sql));
  874.         }

  875.         function result_first($sql) {
  876.                 return $this->result($this->query($sql), 0);
  877.         }

  878.         function query($sql, $type = '') {

  879.                 if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  880.                         $starttime = dmicrotime();
  881.                 }
  882.                 $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
  883.                 'mysql_unbuffered_query' : 'mysql_query';
  884.                 if(!($query = $func($sql, $this->curlink))) {
  885.                         if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
  886.                                 $this->connect();
  887.                                 return $this->query($sql, 'RETRY'.$type);
  888.                         }
  889.                         if($type != 'SILENT' && substr($type, 5) != 'SILENT') {
  890.                                 $this->halt('query_error', $sql);
  891.                         }
  892.                 }

  893.                 if(defined('DISCUZ_DEBUG') && DISCUZ_DEBUG) {
  894.                         $this->sqldebug[] = array($sql, number_format((dmicrotime() - $starttime), 6), debug_backtrace());
  895.                 }

  896.                 $this->querynum++;
  897.                 return $query;
  898.         }

  899.         function affected_rows() {
  900.                 return mysql_affected_rows($this->curlink);
  901.         }

  902.         function error() {
  903.                 return (($this->curlink) ? mysql_error($this->curlink) : mysql_error());
  904.         }

  905.         function errno() {
  906.                 return intval(($this->curlink) ? mysql_errno($this->curlink) : mysql_errno());
  907.         }

  908.         function result($query, $row = 0) {
  909.                 $query = @mysql_result($query, $row);
  910.                 return $query;
  911.         }

  912.         function num_rows($query) {
  913.                 $query = mysql_num_rows($query);
  914.                 return $query;
  915.         }

  916.         function num_fields($query) {
  917.                 return mysql_num_fields($query);
  918.         }

  919.         function free_result($query) {
  920.                 return mysql_free_result($query);
  921.         }

  922.         function insert_id() {
  923.                 return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  924.         }

  925.         function fetch_row($query) {
  926.                 $query = mysql_fetch_row($query);
  927.                 return $query;
  928.         }

  929.         function fetch_fields($query) {
  930.                 return mysql_fetch_field($query);
  931.         }

  932.         function version() {
  933.                 if(empty($this->version)) {
  934.                         $this->version = mysql_get_server_info($this->curlink);
  935.                 }
  936.                 return $this->version;
  937.         }

  938.         function close() {
  939.                 return mysql_close($this->curlink);
  940.         }

  941.         function halt($message = '', $sql = '') {
  942.                 require_once libfile('class/error');
  943.                 discuz_error::db_error($message, $sql);
  944.         }

  945. }

  946. /**
  947. * 对Discuz CORE 中 DB Object中的主要方法进行二次封装,方便程序调用
  948. *
  949. */
  950. class DB
  951. {

  952.         /**
  953.          * 返回表名(pre_$table)
  954.          *
  955.          * @param 原始表名 $table
  956.          * @return 增加pre之后的名字
  957.          */
  958.         function table($table) {
  959.                 return DB::_execute('table_name', $table);
  960.         }

  961.         /**
  962.          * 删除一条或者多条记录
  963.          *
  964.          * @param string $table 原始表名
  965.          * @param string $condition 条件语句,不需要写WHERE
  966.          * @param int $limit 删除条目数
  967.          * @param boolean $unbuffered 立即返回?
  968.          */
  969.         function delete($table, $condition, $limit = 0, $unbuffered = true) {
  970.                 if(empty($condition)) {
  971.                         $where = '1';
  972.                 } elseif(is_array($condition)) {
  973.                         $where = DB::implode_field_value($condition, ' AND ');
  974.                 } else {
  975.                         $where = $condition;
  976.                 }
  977.                 $sql = "DELETE FROM ".DB::table($table)." WHERE $where ".($limit ? "LIMIT $limit" : '');
  978.                 return DB::query($sql, ($unbuffered ? 'UNBUFFERED' : ''));
  979.         }

  980.         /**
  981.          * 插入一条记录
  982.          *
  983.          * @param string $table 原始表名
  984.          * @param array $data 数组field->vlaue 对
  985.          * @param boolen $return_insert_id 返回 InsertID?
  986.          * @param boolen $replace 是否是REPLACE模式
  987.          * @param boolen $silent 屏蔽错误?
  988.          * @return InsertID or Result
  989.          */
  990.         function insert($table, $data, $return_insert_id = false, $replace = false, $silent = false) {

  991.                 $sql = DB::implode_field_value($data);

  992.                 $cmd = $replace ? 'REPLACE INTO' : 'INSERT INTO';

  993.                 $table = DB::table($table);
  994.                 $silent = $silent ? 'SILENT' : '';

  995.                 $return = DB::query("$cmd $table SET $sql", $silent);

  996.                 return $return_insert_id ? DB::insert_id() : $return;

  997.         }

  998.         /**
  999.          * 更新一条或者多条数据记录
  1000.          *
  1001.          * @param string $table 原始表名
  1002.          * @param array $data 数据field-value
  1003.          * @param string $condition 条件语句,不需要写WHERE
  1004.          * @param boolean $unbuffered 迅速返回?
  1005.          * @param boolan $low_priority 延迟更新?
  1006.          * @return result
  1007.          */
  1008.         function update($table, $data, $condition, $unbuffered = false, $low_priority = false) {
  1009.                 $sql = DB::implode_field_value($data);
  1010.                 $cmd = "UPDATE ".($low_priority ? 'LOW_PRIORITY' : '');
  1011.                 $table = DB::table($table);
  1012.                 $where = '';
  1013.                 if(empty($condition)) {
  1014.                         $where = '1';
  1015.                 } elseif(is_array($condition)) {
  1016.                         $where = DB::implode_field_value($condition, ' AND ');
  1017.                 } else {
  1018.                         $where = $condition;
  1019.                 }
  1020.                 $res = DB::query("$cmd $table SET $sql WHERE $where", $unbuffered ? 'UNBUFFERED' : '');
  1021.                 return $res;
  1022.         }

  1023.         /**
  1024.          * 格式化field字段和value,并组成一个字符串
  1025.          *
  1026.          * @param array $array 格式为 key=>value 数组
  1027.          * @param 分割符 $glue
  1028.          * @return string
  1029.          */
  1030.         function implode_field_value($array, $glue = ',') {
  1031.                 $sql = $comma = '';
  1032.                 foreach ($array as $k => $v) {
  1033.                         $sql .= $comma."`$k`='$v'";
  1034.                         $comma = $glue;
  1035.                 }
  1036.                 return $sql;
  1037.         }

  1038.         /**
  1039.          * 返回插入的ID
  1040.          *
  1041.          * @return int
  1042.          */
  1043.         function insert_id() {
  1044.                 return DB::_execute('insert_id');
  1045.         }

  1046.         /**
  1047.          * 依据查询结果,返回一行数据
  1048.          *
  1049.          * @param resourceID $resourceid
  1050.          * @return array
  1051.          */
  1052.         function fetch($resourceid, $type = MYSQL_ASSOC) {
  1053.                 return DB::_execute('fetch_array', $resourceid, $type);
  1054.         }

  1055.         /**
  1056.          * 依据SQL语句,返回第一条查询结果
  1057.          *
  1058.          * @param string $query 查询语句
  1059.          * @return array
  1060.          */
  1061.         function fetch_first($sql) {
  1062.                 DB::checkquery($sql);
  1063.                 return DB::_execute('fetch_first', $sql);
  1064.         }

  1065.         /**
  1066.          * 依据查询结果,返回结果数值
  1067.          *
  1068.          * @param resourceid $resourceid
  1069.          * @return string or int
  1070.          */
  1071.         function result($resourceid, $row = 0) {
  1072.                 return DB::_execute('result', $resourceid, $row);
  1073.         }

  1074.         /**
  1075.          * 依据查询语句,返回结果数值
  1076.          *
  1077.          * @param string $query SQL查询语句
  1078.          * @return unknown
  1079.          */
  1080.         function result_first($sql) {
  1081.                 DB::checkquery($sql);
  1082.                 return DB::_execute('result_first', $sql);
  1083.         }

  1084.         /**
  1085.          * 执行查询
  1086.          *
  1087.          * @param string $sql
  1088.          * @param 类型定义 $type UNBUFFERED OR SILENT
  1089.          * @return Resource OR Result
  1090.          */
  1091.         function query($sql, $type = '') {
  1092.                 DB::checkquery($sql);
  1093.                 return DB::_execute('query', $sql, $type);
  1094.         }

  1095.         /**
  1096.          * 返回select的结果行数
  1097.          *
  1098.          * @param resource $resourceid
  1099.          * @return int
  1100.          */
  1101.         function num_rows($resourceid) {
  1102.                 return DB::_execute('num_rows', $resourceid);
  1103.         }

  1104.         /**
  1105.          * 返回sql语句所影响的记录行数
  1106.          *
  1107.          * @return int
  1108.          */
  1109.         function affected_rows() {
  1110.                 return DB::_execute('affected_rows');
  1111.         }

  1112.         function free_result($query) {
  1113.                 return DB::_execute('free_result', $query);
  1114.         }

  1115.         function error() {
  1116.                 return DB::_execute('error');
  1117.         }

  1118.         function errno() {
  1119.                 return DB::_execute('errno');
  1120.         }

  1121.         function _execute($cmd , $arg1 = '', $arg2 = '') {
  1122.                 static $db;
  1123.                 if(empty($db)) $db = & DB::object();
  1124.                 $res = $db->$cmd($arg1, $arg2);
  1125.                 return $res;
  1126.         }

  1127.         /**
  1128.          * 返回 DB object 指针
  1129.          *
  1130.          * @return pointer of db object from discuz core
  1131.          */
  1132.         function &object($dbclass = 'db_mysql') {
  1133.                 static $db;
  1134.                 if(empty($db)) $db = new $dbclass();
  1135.                 return $db;
  1136.         }

  1137.         function checkquery($sql) {
  1138.                 static $status = null, $checkcmd = array('SELECT', 'UPDATE', 'INSERT', 'REPLACE', 'DELETE');
  1139.                 if($status === null) $status = getglobal('config/security/querysafe/status');
  1140.                 if($status) {
  1141.                         $cmd = trim(strtoupper(substr($sql, 0, strpos($sql, ' '))));
  1142.                         if(in_array($cmd, $checkcmd)) {
  1143.                                 $test = DB::_do_query_safe($sql);
  1144.                                 if($test < 1) DB::_execute('halt', 'security_error', $sql);
  1145.                         }
  1146.                 }
  1147.                 return true;
  1148.         }

  1149.         function _do_query_safe($sql) {
  1150.                 static $_CONFIG = null;
  1151.                 if($_CONFIG === null) {
  1152.                         $_CONFIG = getglobal('config/security/querysafe');
  1153.                 }

  1154.                 $sql = str_replace(array('\\\\', '\\\'', '\\"', '\'\''), '', $sql);
  1155.                 $mark = $clean = '';
  1156.                 if(strpos($sql, '/') === false && strpos($sql, '#') === false && strpos($sql, '-- ') === false) {
  1157.                         $clean = preg_replace("/'(.+?)'/s", '', $sql);
  1158.                 } else {
  1159.                         $len = strlen($sql);
  1160.                         $mark = $clean = '';
  1161.                         for ($i = 0; $i <$len; $i++) {
  1162.                                 $str = $sql[$i];
  1163.                                 switch ($str) {
  1164.                                         case '\'':
  1165.                                                 if(!$mark) {
  1166.                                                         $mark = '\'';
  1167.                                                         $clean .= $str;
  1168.                                                 } elseif ($mark == '\'') {
  1169.                                                         $mark = '';
  1170.                                                 }
  1171.                                                 break;
  1172.                                         case '/':
  1173.                                                 if(empty($mark) && $sql[$i+1] == '*') {
  1174.                                                         $mark = '/*';
  1175.                                                         $clean .= $mark;
  1176.                                                         $i++;
  1177.                                                 } elseif($mark == '/*' && $sql[$i -1] == '*') {
  1178.                                                         $mark = '';
  1179.                                                         $clean .= '*';
  1180.                                                 }
  1181.                                                 break;
  1182.                                         case '#':
  1183.                                                 if(empty($mark)) {
  1184.                                                         $mark = $str;
  1185.                                                         $clean .= $str;
  1186.                                                 }
  1187.                                                 break;
  1188.                                         case "\n":
  1189.                                                 if($mark == '#' || $mark == '--') {
  1190.                                                         $mark = '';
  1191.                                                 }
  1192.                                                 break;
  1193.                                         case '-':
  1194.                                                 if(empty($mark)&& substr($sql, $i, 3) == '-- ') {
  1195.                                                         $mark = '-- ';
  1196.                                                         $clean .= $mark;
  1197.                                                 }
  1198.                                                 break;

  1199.                                         default:

  1200.                                                 break;
  1201.                                 }
  1202.                                 $clean .= $mark ? '' : $str;
  1203.                         }
  1204.                 }

  1205.                 $clean = preg_replace("/[^a-z0-9_\-\(\)#\*\/\"]+/is", "", strtolower($clean));

  1206.                 if($_CONFIG['afullnote']) {
  1207.                         $clean = str_replace('/**/','',$clean);
  1208.                 }

  1209.                 if(is_array($_CONFIG['dfunction'])) {
  1210.                         foreach($_CONFIG['dfunction'] as $fun) {
  1211.                                 if(strpos($clean, $fun.'(') !== false) return '-1';
  1212.                         }
  1213.                 }

  1214.                 if(is_array($_CONFIG['daction'])) {
  1215.                         foreach($_CONFIG['daction'] as $action) {
  1216.                                 if(strpos($clean,$action) !== false) return '-3';
  1217.                         }
  1218.                 }

  1219.                 if($_CONFIG['dlikehex'] && strpos($clean, 'like0x')) {
  1220.                         return '-2';
  1221.                 }

  1222.                 if(is_array($_CONFIG['dnote'])) {
  1223.                         foreach($_CONFIG['dnote'] as $note) {
  1224.                                 if(strpos($clean,$note) !== false) return '-4';
  1225.                         }
  1226.                 }

  1227.                 return 1;

  1228.         }

  1229. }

  1230. //session类
  1231. class discuz_session {

  1232.         var $sid = null;
  1233.         var $var;
  1234.         var $isnew = false;
  1235.         //初始化session数组
  1236.         var $newguest = array('sid' => 0, 'ip1' => 0, 'ip2' => 0, 'ip3' => 0, 'ip4' => 0,
  1237.         'uid' => 0, 'username' => '', 'groupid' => 7, 'invisible' => 0, 'action' => 0,
  1238.         'lastactivity' => 0, 'fid' => 0, 'tid' => 0, 'lastolupdate' => 0);

  1239.         var $old =  array('sid' =>  '', 'ip' =>  '', 'uid' =>  0);

  1240.         function discuz_session($sid = '', $ip = '', $uid = 0) {
  1241.                 $this->old = array('sid' =>  $sid, 'ip' =>  $ip, 'uid' =>  $uid);
  1242.                 $this->var = $this->newguest;
  1243.                 if(!empty($ip)) {
  1244.                         $this->init($sid, $ip, $uid);
  1245.                 }
  1246.         }

  1247.         //设置
  1248.         function set($key, $value) {
  1249.                 if(isset($this->newguest[$key])) {
  1250.                         $this->var[$key] = $value;
  1251.                 } elseif ($key == 'ip') {
  1252.                         $ips = explode('.', $value);
  1253.                         $this->set('ip1', $ips[0]);
  1254.                         $this->set('ip2', $ips[1]);
  1255.                         $this->set('ip3', $ips[2]);
  1256.                         $this->set('ip4', $ips[3]);
  1257.                 }
  1258.         }

  1259.         //获取
  1260.         function get($key) {
  1261.                 if(isset($this->newguest[$key])) {
  1262.                         return $this->var[$key];
  1263.                 } elseif ($key == 'ip') {
  1264.                         return $this->get('ip1').'.'.$this->get('ip2').'.'.$this->get('ip3').'.'.$this->get('ip4');
  1265.                 }
  1266.         }

  1267.         //初始化
  1268.         function init($sid, $ip, $uid) {
  1269.                 $this->old = array('sid' =>  $sid, 'ip' =>  $ip, 'uid' =>  $uid);
  1270.                 $session = array();
  1271.                 if($sid) {
  1272.                         $session = DB::fetch_first("SELECT * FROM ".DB::table('common_session').
  1273.                                 " WHERE sid='$sid' AND CONCAT_WS('.', ip1,ip2,ip3,ip4)='$ip'");
  1274.                 }

  1275.                 if(empty($session) || $session['uid'] != $uid) {
  1276.                         $session = $this->create($ip, $uid);
  1277.                 }

  1278.                 $this->var = $session;
  1279.                 $this->sid = $session['sid'];
  1280.         }

  1281.         //创建
  1282.         function create($ip, $uid) {

  1283.                 $this->isnew = true;
  1284.                 $this->var = $this->newguest;
  1285.                 $this->set('sid', random(6));
  1286.                 $this->set('uid', $uid);
  1287.                 $this->set('ip', $ip);
  1288.                 $uid && $this->set('invisible', getuserprofile('invisible'));
  1289.                 $this->set('lastactivity', time());
  1290.                 $this->sid = $this->var['sid'];

  1291.                 return $this->var;
  1292.         }

  1293.         //删除
  1294.         function delete() {

  1295.                 global $_G;
  1296.                 $onlinehold = $_G['setting']['onlinehold'];
  1297.                 $guestspan = 60;

  1298.                 $onlinehold = time() - $onlinehold;
  1299.                 $guestspan = time() - $guestspan;

  1300.                 $condition = " sid='{$this->sid}' ";
  1301.                 $condition .= " OR lastactivity<$onlinehold ";
  1302.                 $condition .= " OR (uid='0' AND ip1='{$this->var['ip1']}' AND ip2='{$this->var['ip2']}' AND ip3='{$this->var['ip3']}' AND ip4='{$this->var['ip4']}' AND lastactivity>$guestspan) ";
  1303.                 $condition .= $this->var['uid'] ? " OR (uid='{$this->var['uid']}') " : '';
  1304.                 DB::delete('common_session', $condition);
  1305.         }

  1306.         //更新数据
  1307.         function update() {
  1308.                 global $_G;
  1309.                 if($this->sid !== null) {

  1310.                         $data = daddslashes($this->var);
  1311.                         if($this->isnew) {
  1312.                                 $this->delete();
  1313.                                 DB::insert('common_session', $data, false, false, true);
  1314.                         } else {
  1315.                                 DB::update('common_session', $data, "sid='$data[sid]'");
  1316.                         }
  1317.                         $_G['session'] = $data;
  1318.                         dsetcookie('sid', $this->sid, 86400);
  1319.                 }
  1320.         }

  1321.         /**
  1322.          * 取在线用户数量
  1323.          *
  1324.          * @param int $type 0=全部 1=会员 2=游客
  1325.          * @return int
  1326.          */
  1327.         function onlinecount($type = 0) {
  1328.                 $condition = $type == 1 ? ' WHERE uid>0 ' : ($type == 2 ? ' WHERE invisible=1 ' : '');
  1329.                 return DB::result_first("SELECT count(*) FROM ".DB::table('common_session').$condition);
  1330.         }

  1331. }


  1332. class discuz_process
  1333. {
  1334.         function islocked($process, $ttl = 0) {
  1335.                 $ttl = $ttl < 1 ? 600 : intval($ttl);
  1336.                 if(discuz_process::_status('get', $process)) {
  1337.                         return true;
  1338.                 } else {
  1339.                         return discuz_process::_find($process, $ttl);
  1340.                 }
  1341.         }

  1342.         function unlock($process) {
  1343.                 discuz_process::_status('rm', $process);
  1344.                 discuz_process::_cmd('rm', $process);
  1345.         }

  1346.         function _status($action, $process) {
  1347.                 static $plist = array();
  1348.                 switch ($action) {
  1349.                         case 'set' : $plist[$process] = true; break;
  1350.                         case 'get' : return !empty($plist[$process]); break;
  1351.                         case 'rm' : $plist[$process] = null; break;
  1352.                         case 'clear' : $plist = array(); break;
  1353.                 }
  1354.                 return true;
  1355.         }

  1356.         function _find($name, $ttl) {

  1357.                 if(!discuz_process::_cmd('get', $name)) {
  1358.                         discuz_process::_cmd('set', $name, $ttl);
  1359.                         $ret = false;
  1360.                 } else {
  1361.                         $ret = true;
  1362.                 }
  1363.                 discuz_process::_status('set', $name);
  1364.                 return $ret;
  1365.         }

  1366.         function _cmd($cmd, $name, $ttl = 0) {
  1367.                 static $allowmem;
  1368.                 if($allowmem === null) {
  1369.                         $allowmem = memory('check') == 'memcache';
  1370.                 }
  1371.                 if($allowmem) {
  1372.                         return discuz_process::_process_cmd_memory($cmd, $name, $ttl);
  1373.                 } else {
  1374.                         return discuz_process::_process_cmd_db($cmd, $name, $ttl);
  1375.                 }
  1376.         }

  1377.         function _process_cmd_memory($cmd, $name, $ttl = 0) {
  1378.                 return memory($cmd, 'process_lock_'.$name, time(), $ttl);
  1379.         }

  1380.         function _process_cmd_db($cmd, $name, $ttl = 0) {
  1381.                 $ret = '';
  1382.                 switch ($cmd) {
  1383.                         case 'set':
  1384.                                 $ret = DB::insert('common_process', array('processid' => $name, 'expiry' => time() + $ttl), false, true);
  1385.                                 break;
  1386.                         case 'get':
  1387.                                 $ret = DB::fetch_first("SELECT * FROM ".DB::table('common_process')." WHERE processid='$name'");
  1388.                                 if(empty($ret) || $ret['expiry'] < time()) {
  1389.                                         $ret = false;
  1390.                                 } else {
  1391.                                         $ret = true;
  1392.                                 }
  1393.                                 break;
  1394.                         case 'rm':
  1395.                                 $ret = DB::delete('common_process', "processid='$name' OR expiry<".time());
  1396.                                 break;
  1397.                 }
  1398.                 return $ret;
  1399.         }
  1400. }

  1401. /**
  1402. * Discuz 内存读写引擎
  1403. * 支持 memcache, eAccelerator, XCache
  1404. *
  1405. * 使用的时候建议直接利用函数 memory()
  1406. */
  1407. class discuz_memory
  1408. {
  1409.         var $config;
  1410.         var $extension = array();
  1411.         var $memory;
  1412.         var $prefix;
  1413.         var $type;
  1414.         var $keys;
  1415.         var $enable = false;

  1416.         /**
  1417.          * 确认当前系统支持的内存读写接口
  1418.          * @return discuz_memory
  1419.          */
  1420.         function discuz_memory() {
  1421.                 $this->extension['eaccelerator'] = function_exists('eaccelerator_get');
  1422.                 $this->extension['apc'] = function_exists('apc_fetch');
  1423.                 $this->extension['xcache'] = function_exists('xcache_get');
  1424.                 $this->extension['memcache'] = extension_loaded('memcache');
  1425.         }

  1426.         /**
  1427.          * 依据config当中设置,初始化内存引擎
  1428.          * @param unknown_type $config
  1429.          */
  1430.         function init($config) {

  1431.                 $this->config = $config;
  1432.                 $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix'];
  1433.                 $this->keys = array();

  1434.                 if($this->extension['memcache'] && !empty($config['memcache']['server'])) {
  1435.                         require_once libfile('class/memcache');
  1436.                         $this->memory = new discuz_memcache();
  1437.                         $this->memory->init($this->config['memcache']);
  1438.                         if(!$this->memory->enable) {
  1439.                                 $this->memory = null;
  1440.                         }
  1441.                 }

  1442.                 if(!is_object($this->memory) && $this->extension['eaccelerator'] && $this->config['eaccelerator']) {
  1443.                         require_once libfile('class/eaccelerator');
  1444.                         $this->memory = new discuz_eaccelerator();
  1445.                         $this->memory->init(null);
  1446.                 }

  1447.                 if(!is_object($this->memory) && $this->extension['xcache'] && $this->config['xcache']) {
  1448.                         require_once libfile('class/xcache');
  1449.                         $this->memory = new discuz_xcache();
  1450.                         $this->memory->init(null);
  1451.                 }

  1452.                 if(!is_object($this->memory) && $this->extension['apc'] && $this->config['apc']) {
  1453.                         require_once libfile('class/apc');
  1454.                         $this->memory = new discuz_apc();
  1455.                         $this->memory->init(null);
  1456.                 }

  1457.                 if(is_object($this->memory)) {
  1458.                         $this->enable = true;
  1459.                         $this->type = str_replace('discuz_', '', get_class($this->memory));
  1460.                         $this->keys = $this->get('memory_system_keys');
  1461.                         $this->keys = !is_array($this->keys) ? array() : $this->keys;
  1462.                 }

  1463.         }

  1464.         /**
  1465.          * 读取内存
  1466.          *
  1467.          * @param string $key
  1468.          * @return mix
  1469.          */
  1470.         function get($key) {
  1471.                 $ret = null;
  1472.                 if($this->enable) {
  1473.                         $ret = $this->memory->get($this->_key($key));
  1474.                         if(!is_array($ret)) {
  1475.                                 $ret = null;
  1476.                                 if(array_key_exists($key, $this->keys)) {
  1477.                                         unset($this->keys[$key]);
  1478.                                         $this->memory->set($this->_key('memory_system_keys'), array($this->keys));
  1479.                                 }
  1480.                         } else {
  1481.                                 return $ret[0];
  1482.                         }
  1483.                 }
  1484.                 return $ret;
  1485.         }

  1486.         /**
  1487.          * 写入内存
  1488.          *
  1489.          * @param string $key
  1490.          * @param array_string_number $value
  1491.          * @param int过期时间 $ttl
  1492.          * @return boolean
  1493.          */
  1494.         function set($key, $value, $ttl = 0) {

  1495.                 $ret = null;
  1496.                 if($this->enable) {
  1497.                         $ret = $this->memory->set($this->_key($key), array($value), $ttl);
  1498.                         if($ret) {
  1499.                                 $this->keys[$key] = true;
  1500.                                 $this->memory->set($this->_key('memory_system_keys'), array($this->keys));
  1501.                         }
  1502.                 }
  1503.                 return $ret;
  1504.         }

  1505.         /**
  1506.          * 删除一个内存单元
  1507.          * @param 键值string $key
  1508.          * @return boolean
  1509.          */
  1510.         function rm($key) {
  1511.                 $ret = null;
  1512.                 if($this->enable) {
  1513.                         $ret = $this->memory->rm($this->_key($key));
  1514.                         unset($this->keys[$key]);
  1515.                         $this->memory->set($this->_key('memory_system_keys'), array($this->keys));
  1516.                 }
  1517.                 return $ret;
  1518.         }

  1519.         /**
  1520.          * 清除当前使用的所有内存
  1521.          */
  1522.         function clear() {
  1523.                 if($this->enable && is_array($this->keys)) {
  1524.                         if(method_exists($this->memory, 'clear')) {
  1525.                                 $this->memory->clear();
  1526.                         } else {
  1527.                                 $this->keys['memory_system_keys'] = true;
  1528.                                 foreach ($this->keys as $k => $v) {
  1529.                                         $this->memory->rm($this->_key($k));
  1530.                                 }
  1531.                         }
  1532.                 }
  1533.                 $this->keys = array();
  1534.                 return true;
  1535.         }

  1536.         /**
  1537.          * 内部函数 追加键值前缀
  1538.          * @param string $str
  1539.          * @return boolean
  1540.          */
  1541.         function _key($str) {
  1542.                 return ($this->prefix).$str;
  1543.         }

  1544. }

  1545. ?>
複製代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-6-10 15:43 , Processed in 0.242723 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表