Discuz!源代码分析系列:./include/global.func.php(5)

转自www.discuz.net 做者:郭鑫 /** * 更新版主工做统计的 * @para string $modaction 进行的何种操做(高亮,关闭,提高…) * @para int $posts 一次进行了的个数 * */ function updatemodworks($modaction, $posts = 1) {         global $modworkstatus, $db, $tablepre, $discuz_uid, $timestamp, $_DCACHE;         $today = gmdate('Y-m-d', $timestamp + $_DCACHE['settings']['timeoffset'] * 3600);         if($modworkstatus && $modaction && $posts) {                 $db->query("UPDATE {$tablepre}modworks SET count=count+1, posts=posts+'$posts' WHERE uid='$discuz_uid' AND modaction='$modaction' AND dateline='$today'");                 if(!$db->affected_rows()) {                         $db->query("INSERT INTO {$tablepre}modworks (uid, modaction, dateline, count, posts) VALUES ('$discuz_uid', '$modaction', '$today', 1, '$posts')");                 }         } } 复制内容到剪贴板代码: /** * 写入系统日志 * @para string $file 写入的日志文件 * @para string $log 要写入的内容提要 * */ function writelog($file, $log) {         global $timestamp, $_DCACHE;         $yearmonth = gmdate('Ym', $timestamp + $_DCACHE['settings']['timeoffset'] * 3600);         $logdir = DISCUZ_ROOT.'./forumdata/logs/';         $logfile = $logdir.$yearmonth.'_'.$file.'.php';         if(@filesize($logfile) > 2048000) {                 $dir = opendir($logdir);                 $length = strlen($file);                 $maxid = $id = 0;                 while($entry = readdir($dir)) {                         if(strexists($entry, $yearmonth.'_'.$file)) {                                 $id = intval(substr($entry, $length + 8, -4));                                 $id > $maxid && $maxid = $id;                         }                 }                 closedir($dir);                 $logfilebak = $logdir.$yearmonth.'_'.$file.'_'.($maxid + 1).'.php';                 @rename($logfile, $logfilebak);         }         if($fp = @fopen($logfile, 'a')) {                 @flock($fp, 2);                 $log = is_array($log) ? $log : array($log);                 foreach($log as $tmp) {                         fwrite($fp, "<?PHP exit;?>\t".str_replace(array('<?', '?>'), '', $tmp)."\n");                 }                 fclose($fp);         } } 复制内容到剪贴板代码: /** * 当作函数的重载,即把implode这个函数重载成在两边加上'(单引号)的函数 * @para array $array * * @return string */ function implodeids($array) {         if(!empty($array)) {                 return "'".implode("','", is_array($array) ? $array : array($array))."'";         } else {                 return '';         } } 复制内容到剪贴板代码: /** * 把这三个函数放一块儿,由于它们三个是一块儿实现的,即传说中的ajax… * 这里说一下ajax的实现方式,一种方法是用XMLHttpRequest异步提交给服务器,服务器返回一个带有数据结点的xml文件 * javascript或者php等解析这个xml文档,从而获得数据;另一种是这个xml文件中含一个CDATA,把已经用HTML格式化好的 * 内容返回过来直接用,显然第二种好用,Discuz用的就是这样一个方式。其余用iframe等等就不在这里介绍了,那个通常 * 在提交数据检查、异步文件上传的时候用。 */ //ajaxshowheader生成一个xml头和一个<root>根以及一个CDATA function ajaxshowheader() {         global $charset;         @header("Expires: -1");         @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);         @header("Pragma: no-cache");         header("Content-type: application/xml");         echo "<?xml version=\"1.0\" encoding=\"$charset\"?>\n<root><![CDATA["; } //闭合CDATA和root function ajaxshowfooter() {         echo ']]></root>'; } //ajaxtemplate组合了ajaxshowheader + 模板 + ajaxshowfooter,使之完整。 function ajaxtemplate($tplname) {         if(!empty($_GET['inajax'])) {                 extract($GLOBALS);                 updatesession();                 ajaxshowheader();                 include template($tplname);                 ajaxshowfooter();                 die();         } } 复制内容到剪贴板代码: /** * 过滤.., \n, \r 用的 * @para string $str 要过滤的string * * @return string */ function wipespecial($str) {         return str_replace(array('..', "\n", "\r"), array('', '', ''), $str); } 复制内容到剪贴板代码: /** * supser site 数据库链接 * 经过$super['db']来用,而Discuz自己的是$db */ function supe_dbconnect() {         global $supe, $db;         if(empty($supe['dbmode'])) {                 $supe['db'] = $db;         } elseif(empty($supe['db'])) {                 $supe['db'] = new dbstuff;                 $supe['db']->connect($supe['dbhost'], $supe['dbuser'], $supe['dbpw'], $supe['dbname'], $supe['pconnect']);         } }