discuz代码分析一 从common.inc.php开始
// <?php 是php的开始标记;$是php变量开头标记,所有变量都以$开头.
<?php
/*
[Discuz!] (C)2001-2006 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$RCSfile: common.inc.php,v $
$Revision: 1.61.2.1 $
$Date: 2006/09/01 06:15:00 $
*/
// 系统关于文件错误的处理设置,定义错误报告级别
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//关闭魔法引用
set_magic_quotes_runtime(0);
//取当前时间,用于时间变量的赋值
$mtime = explode(' ', microtime());
$discuz_starttime = $mtime[1] + $mtime[0];
//定义一个常量,防止非法引用
define('IN_DISCUZ', TRUE);
//定义论坛根目录
define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -7));
//定义天下秀
define('DISCUZ_AVATARSHOW', '3560626219401401200');
//兼容低php版本
if(PHP_VERSION < '4.1.0') {
$_GET = &$HTTP_GET_VARS;
$_POST = &$HTTP_POST_VARS;
$_COOKIE = &$HTTP_COOKIE_VARS;
$_SERVER = &$HTTP_SERVER_VARS;
$_ENV = &$HTTP_ENV_VARS;
$_FILES = &$HTTP_POST_FILES;
}
//包含全局函数定义文件
require_once DISCUZ_ROOT.'./include/global.func.php';
//取魔法引用的设置值
$magic_quotes_gpc = get_magic_quotes_gpc();
//转义输入变量
@extract(daddslashes($_POST));
@extract(daddslashes($_GET));
if(!$magic_quotes_gpc) {
$_FILES = daddslashes($_FILES);
}
//初始论坛配置变量
$charset = $dbcharset = $forumfounders = '';
$plugins = $hooks = array();
//包含配置文件
require_once DISCUZ_ROOT.'./config.inc.php';
//包含数据库操作类
require_once DISCUZ_ROOT.'./include/db_'.$database.'.class.php';
//判断是否开启防护大量正常请求造成的拒绝服务攻击功能
if($attackevasive) {
require_once DISCUZ_ROOT.'./include/security.inc.php';
}
//取当前时间
$timestamp = time();
//构造安全论坛url
$PHP_SELF = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$SCRIPT_FILENAME = str_replace('\\\\', '/', (isset($_SERVER['PATH_TRANSLATED']) ? $_SERVER['PATH_TRANSLATED'] : $_SERVER['SCRIPT_FILENAME']));
$boardurl = 'http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api|archiver|wap)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/';
//取用户IP
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
//不知道匹配什么
preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
$onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
unset($onlineipmatches);
//初始用户信息变量
$extrahead = '';
$_DCOOKIE = $_DSESSION = $_DCACHE = $_DPLUGIN = array();
//取用户cookie
//表前缀长度
$prelength = strlen($tablepre);
foreach($_COOKIE as $key => $val) {
if(substr($key, 0, $prelength) == $tablepre) {
$_DCOOKIE[(substr($key, $prelength))] = daddslashes($val);
}
}
unset($prelength);
//取缓存
$cachelost = (@include DISCUZ_ROOT.'./forumdata/cache/cache_settings.php') ? '' : 'settings';
@extract($_DCACHE['settings']);
//设置输出缓冲
if($gzipcompress && function_exists('ob_gzhandler') && CURSCRIPT != 'wap') {
ob_start('ob_gzhandler');
} else {
$gzipcompress = 0;
ob_start();
}
//不知道干什么的
$jsmenu = array();
$jsmenustatus = sprintf('%b', $jsmenustatus);
for($i = 1; $i <= strlen($jsmenustatus); $i++) {
if(substr($jsmenustatus, -$i, 1)) $jsmenu[$i] = TRUE;
}
//天下秀
/* Avatar Show Data */
$avatarshow_license = strval(empty($avatarshow_license) ? DISCUZ_AVATARSHOW : $avatarshow_license);
if($loadctrl && (!defined('CURSCRIPT') || CURSCRIPT != 'wap') && substr(PHP_OS, 0, 3) != 'WIN') {
if($fp = @fopen('/proc/loadavg', 'r')) {
list($loadaverage) = explode(' ', fread($fp, 6));
fclose($fp);
if($loadaverage > $loadctrl) {
header("HTTP/1.0 503 Service Unavailable");
include DISCUZ_ROOT.'./include/serverbusy.htm';
exit();
}
}
}
//缓存
if(defined('CURSCRIPT') && in_array(CURSCRIPT, array('index', 'forumdisplay', 'viewthread', 'post', 'blog', 'pm'))) {
$cachelost .= (@include DISCUZ_ROOT.'./forumdata/cache/cache_'.CURSCRIPT.'.php') ? '' : ' '.CURSCRIPT;
}
//连接数据库
$db = new dbstuff;
$db->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
//处理SID
$sid = daddslashes(($transsidstatus || (defined('CURSCRIPT') && CURSCRIPT == 'wap'))&& (isset($_GET['sid']) || isset($_POST['sid'])) ?
(isset($_GET['sid']) ? $_GET['sid'] : $_POST['sid']) :
(isset($_DCOOKIE['sid']) ? $_DCOOKIE['sid'] : ''));
//生成加密KEY
$discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);
//解密COOKIE
list($discuz_pw, $discuz_secques, $discuz_uid) = isset($_DCOOKIE['auth']) ? explode("\t", authcode($_DCOOKIE['auth'], 'DECODE')) : array('', '', 0);
//转义输入变量
if(isset($_DCOOKIE['auth']) && !$discuz_uid) {
clearcookies();
}
//初始用户变量
$newpm = $newpmexists = $sessionexists = $seccode = $bloguid = 0;
//判断SID存在
if($sid) {
if($discuz_uid) {
//查询现登陆用户存在SESSION表中的SESSION
$query = $db->query("SELECT s.sid, s.styleid, s.groupid='6' AS ipbanned, s.pageviews AS spageviews, s.lastolupdate, s.seccode, m.uid AS discuz_uid,
m.username AS discuz_user, m.password AS discuz_pw, m.secques AS discuz_secques, m.adminid, m.groupid, m.groupexpiry,
m.extgroupids, m.email, m.timeoffset, m.tpp, m.ppp, m.posts, m.digestposts, m.oltime, m.pageviews, m.credits, m.extcredits1, m.extcredits2, m.extcredits3,
m.extcredits4, m.extcredits5, m.extcredits6, m.extcredits7, m.extcredits8, m.timeformat, m.dateformat, m.pmsound,
m.sigstatus, m.invisible, m.lastvisit, m.lastactivity, m.lastpost, m.newpm, m.accessmasks,m.xspacestatus, m.editormode, m.customshow
FROM {$tablepre}sessions s, {$tablepre}members m
WHERE m.uid=s.uid AND s.sid='$sid' AND CONCAT_WS('.',s.ip1,s.ip2,s.ip3,s.ip4)='$onlineip' AND m.uid='$discuz_uid'
AND m.password='$discuz_pw' AND m.secques='$discuz_secques'");
} else {
//查询当前游客存在SESSION表中的SESSION
$query = $db->query("SELECT sid, uid AS sessionuid, groupid, groupid='6' AS ipbanned, pageviews AS spageviews, styleid, lastolupdate, seccode
FROM {$tablepre}sessions WHERE sid='$sid' AND CONCAT_WS('.',ip1,ip2,ip3,ip4)='$onlineip'");
}
if($_DSESSION = $db->fetch_array($query)) {
//当前用户SESSION存在
$sessionexists = 1;
//判断是否为游客
if(!empty($_DSESSION['sessionuid'])) {
//登陆用户
//完善登陆用户SESSION(更新和添加)
$query = $db->query("SELECT m.uid AS discuz_uid, m.username AS discuz_user, m.password AS discuz_pw,
m.secques AS discuz_secques, m.adminid, m.groupid, m.groupexpiry, m.extgroupids, m.email, m.timeoffset,
m.tpp, m.ppp, m.posts, m.digestposts, m.oltime, m.pageviews, m.credits, m.extcredits1, m.extcredits2, m.extcredits3, m.extcredits4, m.extcredits5,
m.extcredits6, m.extcredits7, m.extcredits8, m.timeformat, m.dateformat, m.pmsound, m.sigstatus, m.invisible,
m.lastvisit, m.lastactivity, m.lastpost, m.newpm, m.accessmasks,m.xspacestatus, m.editormode, m.customshow
FROM {$tablepre}members m WHERE uid='$_DSESSION[sessionuid]'");
$_DSESSION = array_merge($_DSESSION, $db->fetch_array($query));
}
} else {
//游客
//清除游客本地的COOKIE
$query = $db->query("SELECT sid, groupid, groupid='6' AS ipbanned, pageviews AS spageviews, styleid, lastolupdate, seccode
FROM {$tablepre}sessions WHERE sid='$sid' AND CONCAT_WS('.',ip1,ip2,ip3,ip4)='$onlineip'");
if($_DSESSION = $db->fetch_array($query)) {
clearcookies();
$sessionexists = 1;
}
}
}
if(!$sessionexists) {
//SESSION不存在
if($discuz_uid) {
//登陆用户
//取登陆用户信息
$query = $db->query("SELECT uid AS discuz_uid, username AS discuz_user, password AS discuz_pw, secques AS discuz_secques,
adminid, groupid, groupexpiry, extgroupids, email, timeoffset, styleid, tpp, ppp, posts, digestposts, oltime, pageviews, credits,
extcredits1, extcredits2, extcredits3, extcredits4, extcredits5, extcredits6, extcredits7, extcredits8, timeformat,
dateformat, pmsound, sigstatus, invisible, lastvisit, lastactivity, lastpost, newpm, accessmasks, xspacestatus, editormode, customshow
FROM {$tablepre}members WHERE uid='$discuz_uid' AND password='$discuz_pw' AND secques='$discuz_secques'");
if(!($_DSESSION = $db->fetch_array($query))) {
clearcookies();
}
}
//判断当前用户IP是否受限
if(ipbanned($onlineip)) {
$_DSESSION['ipbanned'] = 1;
}
//生成SID和验证码
$_DSESSION['sid'] = random(6);
$_DSESSION['seccode'] = random(4, 1);
}
$_DSESSION['dateformat'] = empty($_DSESSION['dateformat']) ? $_DCACHE['settings']['dateformat'] : $_DSESSION['dateformat'];
$_DSESSION['timeformat'] = empty($_DSESSION['timeformat']) ? $_DCACHE['settings']['timeformat'] : ($_DSESSION['timeformat'] == 1 ? 'h:i A' : 'H:i');
$_DSESSION['timeoffset'] = isset($_DSESSION['timeoffset']) && $_DSESSION['timeoffset'] != 9999 ? $_DSESSION['timeoffset'] : $_DCACHE['settings']['timeoffset'];
@extract($_DSESSION);
//上次访问
$lastvisit = empty($lastvisit) ? $timestamp - 86400 : $lastvisit;
//当前时间,格式为用户定义的格式(游客为默认设置)
$timenow = array('time' => gmdate("$dateformat $timeformat", $timestamp + 3600 * $timeoffset),
'offset' => ($timeoffset >= 0 ? ($timeoffset == 0 ? '' : '+'.$timeoffset) : $timeoffset));
//判断是否为登陆用户
//游客扩展信息设为默认
if(empty($discuz_uid) || empty($discuz_user)) {
$discuz_user = $extgroupids = '';
$discuz_uid = $adminid = $posts = $digestposts = $pageviews = $oltime = $invisible
= $credits = $extcredits1 = $extcredits2 = $extcredits3 = $extcredits4
= $extcredits5 = $extcredits6 = $extcredits7 = $extcredits8 = 0;
$groupid = empty($groupid) || $groupid != 6 ? 7 : 6;
$avatarshowid = 0;
} else {
$discuz_userss = $discuz_user;
$discuz_user = addslashes($discuz_user);
}
//定义论坛哈希值
define('FORMHASH', formhash());
//构造正确附件目录
$attachdir = substr($attachdir, 0, 2) == './' ? DISCUZ_ROOT.$attachdir : $attachdir;
//包含统计页面
$statstatus && require_once DISCUZ_ROOT.'./include/counter.inc.php';
$rssauth = $rssstatus && $discuz_uid ? authcode("$discuz_uid\t$fid\t".substr(md5($discuz_pw.$discuz_secques), 0, 8), 'ENCODE', md5($_DCACHE['settings']['authkey'])) : '0';
$navtitle = $navigation = '';
$extra = isset($extra) && preg_match("/^[&=;a-z0-9]+$/i", $extra) ? $extra : '';
$tpp = intval(empty($_DSESSION['tpp']) ? $topicperpage : $_DSESSION['tpp']);
$ppp = intval(empty($_DSESSION['ppp']) ? $postperpage : $_DSESSION['ppp']);
//note 用户访问权限
if($discuz_uid && $accessmasks) {
$accessadd1 = ', a.allowview, a.allowpost, a.allowreply, a.allowgetattach, a.allowpostattach';
$accessadd2 = "LEFT JOIN {$tablepre}access a ON a.uid='$discuz_uid' AND a.fid=f.fid";
} else {
$accessadd1 = $accessadd2 = '';
}
if($discuz_uid && $adminid == 3) {
$modadd1 = ', m.uid AS ismoderator';
$modadd2 = "LEFT JOIN {$tablepre}moderators m ON m.uid='$discuz_uid' AND m.fid=f.fid";
} else {
$modadd1 = $modadd2 = '';
}
//载入缓存
$_DSESSION['groupid'] = $groupid = empty($ipbanned) ? (empty($groupid) ? 7 : intval($groupid)) : 6;
if(!@include DISCUZ_ROOT.'./forumdata/cache/usergroup_'.$groupid.'.php') {
$query = $db->query("SELECT type FROM {$tablepre}usergroups WHERE groupid='$groupid'");
$grouptype = $db->result($query, 0);
if(!empty($grouptype)) {
$cachelost .= ' usergroup_'.$groupid;
} else {
$grouptype = 'member';
}
}
//登陆,登出,注册的URL设置
//判断通行证设置
if($passport_status) {
$passport_forward = rawurlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$link_login = $passport_url.$passport_login_url.(strpos($passport_login_url, '?') === FALSE ? '?' : '&').'forward='.$passport_forward;
$link_logout = $passport_url.$passport_logout_url.(strpos($passport_logout_url, '?') === FALSE ? '?' : '&').'forward='.$passport_forward;
$link_register = $passport_url.$passport_register_url.(strpos($passport_register_url, '?') === FALSE ? '?' : '&').'forward='.$passport_forward;
} else {
$link_login = 'logging.php?action=login';
$link_logout = 'logging.php?action=logout&formhash='.FORMHASH;
$link_register = 'register.php';
}
//用户组初始化
if($discuz_uid && $_DSESSION) {
if(!empty($groupexpiry) && $groupexpiry < $timestamp && (!defined('CURSCRIPT') || (CURSCRIPT != 'wap' && CURSCRIPT != 'member'))) {
header("Location: {$boardurl}member.php?action=groupexpiry");
dexit();
} elseif($grouptype && $groupid != getgroupid($discuz_uid, array
(
'type' => $grouptype,
'creditshigher' => $groupcreditshigher,
'creditslower' => $groupcreditslower
), $_DSESSION)) {
@extract($_DSESSION);
$cachelost .= (@include DISCUZ_ROOT.'./forumdata/cache/usergroup_'.intval($groupid).'.php') ? '' : ' usergroup_'.$groupid;
}
}
//管理权限初始化
if(!in_array($adminid, array(1, 2, 3))) {
if(!$errorreport) {
error_reporting(E_ALL);
}
$alloweditpost = $alloweditpoll = $allowstickthread = $allowmodpost = $allowdelpost = $allowmassprune
= $allowrefund = $allowcensorword = $allowviewip = $allowbanip = $allowedituser = $allowmoduser
= $allowbanuser = $allowpostannounce = $allowviewlog = $disablepostctrl = $supe_allowpushthread = 0;
} elseif(isset($radminid) && $adminid != $radminid && $adminid != $groupid) {
$cachelost .= (@include DISCUZ_ROOT.'./forumdata/cache/admingroup_'.intval($adminid).'.php') ? '' : ' admingroup_'.$groupid;
}
//论坛版快初始化
$forum = array();
$auditstatuson = !empty($mod) && $mod == 'edit' && in_array($adminid, array(1, 2, 3)) && $allowmodpost ? true : false;
if(!empty($tid) || !empty($fid)) {
if(empty($tid)) {
$query = $db->query("SELECT f.fid, f.*, ff.* $accessadd1 $modadd1, f.fid AS fid
FROM {$tablepre}forums f
LEFT JOIN {$tablepre}forumfields ff ON ff.fid=f.fid $accessadd2 $modadd2
WHERE f.fid='$fid'");
$forum = $db->fetch_array($query);
} else {
$query = $db->query("SELECT t.tid, t.closed, f.*, ff.* $accessadd1 $modadd1, f.fid AS fid
FROM {$tablepre}threads t
INNER JOIN {$tablepre}forums f ON f.fid=t.fid
LEFT JOIN {$tablepre}forumfields ff ON ff.fid=f.fid $accessadd2 $modadd2
WHERE t.tid='$tid'".($auditstatuson ? '' : " AND t.displayorder>='0'")." LIMIT 1");
$forum = $db->fetch_array($query);
$tid = $forum['tid'];
}
$fid = $forum['fid'];
$typeid = isset($typeid) ? intval($typeid) : 0;
$forum['ismoderator'] = !empty($forum['ismoderator']) || $adminid == 1 || $adminid == 2 ? 1 : 0;
$forum['postcredits'] = $forum['postcredits'] ? unserialize($forum['postcredits']) : array();
$forum['replycredits'] = $forum['replycredits'] ? unserialize($forum['replycredits']) : array();
$forum['threadtypes'] = $forum['threadtypes'] ? unserialize($forum['threadtypes']) : array();
$forum['digestcredits'] = $forum['digestcredits'] ? unserialize($forum['digestcredits']) : array();
$forum['postattachcredits'] = $forum['postattachcredits'] ? unserialize($forum['postattachcredits']) : array();
$forum['getattachcredits'] = $forum['getattachcredits'] ? unserialize($forum['getattachcredits']) : array();
if($supe_status) {
$forum['supe_pushsetting'] = unserialize($forum['supe_pushsetting']);
}
} else {
$fid = $tid = 0;
}
//页面样式初始化
$styleid = intval(!empty($_GET['styleid']) ? $_GET['styleid'] :
(!empty($_POST['styleid']) ? $_POST['styleid'] :
(!empty($_DSESSION['styleid']) ? $_DSESSION['styleid'] :
$_DCACHE['settings']['styleid'])));
$styleid = intval(isset($stylejump[$styleid]) ? $styleid : $_DCACHE['settings']['styleid']);
if(@!include DISCUZ_ROOT.'./forumdata/cache/style_'.intval(!empty($forum['styleid']) ? $forum['styleid'] : $styleid).'.php') {
$cachelost .= (@include DISCUZ_ROOT.'./forumdata/cache/style_'.($styleid = $_DCACHE['settings']['styleid']).'.php') ? '' : ' style_'.$styleid;
}
if($cachelost) {
require_once DISCUZ_ROOT.'./include/cache.func.php';
updatecache();
dexit('Cache List: '.$cachelost.'<br>Caches successfully created, please refresh.');
}
if(!defined('CURSCRIPT') || CURSCRIPT != 'wap') {
if($nocacheheaders) {
@header("Expires: 0");
@header("Cache-Control: private, post-check=0, pre-check=0, max-age=0", FALSE);
@header("Pragma: no-cache");
}
if($headercharset) {
@header('Content-Type: text/html; charset='.$charset);
}
if(empty($_DCOOKIE['sid']) || $sid != $_DCOOKIE['sid']) {
dsetcookie('sid', $sid, 604800);
}
}
//与后台批处理有关
if($cronnextrun && $cronnextrun <= $timestamp) {
require_once DISCUZ_ROOT.'./include/cron.func.php';
runcron();
}
//插件初始化
if(isset($plugins['include']) && is_array($plugins['include'])) {
foreach($plugins['include'] as $include) {
if(!$include['adminid'] || ($include['adminid'] && $include['adminid'] >= $adminid)) {
@include_once DISCUZ_ROOT.'./plugins/'.$include['script'].'.inc.php';
}
}
}
//是否允许访问
if(isset($allowvisit) && $allowvisit == 0 && !(defined('CURSCRIPT') && CURSCRIPT == 'member' && $action == 'groupexpiry')) {
showmessage('user_banned', NULL, 'HALTED');
} elseif(!((defined('CURSCRIPT') && in_array(CURSCRIPT, array('logging', 'wap', 'seccode'))) || $adminid == 1)) {
if($bbclosed) {
clearcookies();
showmessage($closedreason ? $closedreason : 'board_closed', NULL, 'NOPERM');
}
periodscheck('visitbanperiods');
}
$advarray = $advlist = array();
if($advertisements) {
require_once DISCUZ_ROOT.'./include/advertisements.inc.php';
}
if((!empty($advertisements['lateststarttime']) && $advertisements['lateststarttime'] <= $timestamp) ||
(!empty($advertisements['latestendtime']) && $advertisements['latestendtime'] <= $timestamp)) {
require_once DISCUZ_ROOT.'./include/cache.func.php';
updatecache('settings');
}
if((!empty($fromuid) || !empty($fromuser)) && ($creditspolicy['promotion_visit'] || $creditspolicy['promotion_register'])) {
require_once DISCUZ_ROOT.'/include/promotion.inc.php';
}
?>