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

转自www.discuz.net 作者:郭鑫

if(!defined('IN_DISCUZ')) {
        exit('Access Denied');
}
//防非法引用的
复制内容到剪贴板代码:
/**
* 这一个部分是生成discuz authcode的,用的是base64加密,分别能加密和解密。通过传入$operation = 'ENCODE'|'DECODE'来实现。
* @para string $string 要加/解密的string
* @para string $operation 方法(个人觉得用boolean比较好)
* @para string $key 用来加密的key
*
* @return string
*/

function authcode($string, $operation, $key = '') {

        $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
        $key_length = strlen($key);

        $string = $operation == 'DECODE' ? base64_decode($string) : substr(md5($string.$key), 0, 8).$string;
        $string_length = strlen($string);

        $rndkey = $box = array();
        $result = '';

        for($i = 0; $i <= 255; $i++) {
                $rndkey[$i] = ord($key[$i % $key_length]);
                $box[$i] = $i;
        }

        for($j = $i = 0; $i < 256; $i++) {
                $j = ($j + $box[$i] + $rndkey[$i]) % 256;
                $tmp = $box[$i];
                $box[$i] = $box[$j];
                $box[$j] = $tmp;
        }

        for($a = $j = $i = 0; $i < $string_length; $i++) {
                $a = ($a + 1) % 256;
                $j = ($j + $box[$a]) % 256;
                $tmp = $box[$a];
                $box[$a] = $box[$j];
                $box[$j] = $tmp;
                $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
        }

        if($operation == 'DECODE') {
                if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) {
                        return substr($result, 8);
                } else {
                        return '';
                }
        } else {
                return str_replace('=', '', base64_encode($result));
        }

}
复制内容到剪贴板代码:
/**
* 这个是用来清除所有的cookie的
*/

function clearcookies() {
        global $discuz_uid, $discuz_user, $discuz_pw, $discuz_secques, $adminid, $credits;
        dsetcookie('sid', '', -86400 * 365);
        dsetcookie('auth', '', -86400 * 365);
        dsetcookie('visitedfid', '', -86400 * 365);
        dsetcookie('onlinedetail', '', -86400 * 365, 0);

        $discuz_uid = $adminid = $credits = 0;
        $discuz_user = $discuz_pw = $discuz_secques = '';
}
复制内容到剪贴板代码:
/**
* 用来检查积分的最低要求的
* @para array $creditsarray 传入积分数组
* @para int $coef 单位
*/

function checklowerlimit($creditsarray, $coef = 1) {
        if(is_array($creditsarray)) {
                global $extcredits, $id;
                foreach($creditsarray as $id => $addcredits) {
                        if($addcredits * $coef < 0 && $GLOBALS['extcredits'.$id] - $addcredits < $extcredits[$id]['lowerlimit']) {
                                showmessage('credits_policy_lowerlimit');
                        }
                }
        }
}
复制内容到剪贴板代码:
/**
* 用来完美分词的,也就是把一段中文字只取前面一段,再加一个…
* @para string $string 用来分词的串
* @para int $length 保留的长度
* @para string $dot 最后加点什么
*
* @return string
*/

function cutstr($string, $length, $dot = ' ...') {
        global $charset;

        if(strlen($string) <= $length) {
                return $string;
        }

        $string = str_replace(array('&', '"', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);

        $strcut = '';
        if(strtolower($charset) == 'utf-8') {

                $n = $tn = $noc = 0;
                while($n < strlen($string)) {

                        $t = ord($string[$n]);
                        if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                                $tn = 1; $n++; $noc++;
                        } elseif(194 <= $t && $t <= 223) {
                                $tn = 2; $n += 2; $noc += 2;
                        } elseif(224 <= $t && $t < 239) {
                                $tn = 3; $n += 3; $noc += 2;
                        } elseif(240 <= $t && $t <= 247) {
                                $tn = 4; $n += 4; $noc += 2;
                        } elseif(248 <= $t && $t <= 251) {
                                $tn = 5; $n += 5; $noc += 2;
                        } elseif($t == 252 || $t == 253) {
                                $tn = 6; $n += 6; $noc += 2;
                        } else {
                                $n++;
                        }

                        if($noc >= $length) {
                                break;
                        }

                }
                if($noc > $length) {
                        $n -= $tn;
                }

                $strcut = substr($string, 0, $n);

        } else {
                for($i = 0; $i < $length - strlen($dot) - 1; $i++) {
                        $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
                }
        }

        $strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '&lt;', '&gt;'), $strcut);

        return $strcut.$dot;
}
复制内容到剪贴板代码:
/**
* 用来过滤字串的,之所以要这样一个函数是考虑到是不是打开了magic_quotes_gpc.
* @para string $string 要过滤的字串
* @para int $force 强制过滤
*
* @return string
*/

function daddslashes($string, $force = 0) {
        !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
        if(!MAGIC_QUOTES_GPC || $force) {
                if(is_array($string)) {
                        foreach($string as $key => $val) {
                                $string[$key] = daddslashes($val, $force);
                        }
                } else {
                        $string = addslashes($string);
                }
        }
        return $string;
}
复制内容到剪贴板代码:
/**
* 检查一个日期是否合法
* @para string $ymd 日期字串
* @para string $sep 分隔符
*
* @return boolean
*/

function datecheck($ymd, $sep='-') {
        if(!empty($ymd)) {
                list($year, $month, $day) = explode($sep, $ymd);
                return checkdate($month, $day, $year);
        } else {
                return FALSE;
        }
}
复制内容到剪贴板代码:
/**
* 用来计算程序运行时间的,论坛底部的debug info
*
* @return boolean
*/
function debuginfo() {
        if($GLOBALS['debug']) {
                global $db, $discuz_starttime, $debuginfo;
                $mtime = explode(' ', microtime());
                $debuginfo = array('time' => number_format(($mtime[1] + $mtime[0] - $discuz_starttime), 6), 'queries' => $db->querynum);
                return TRUE;
        } else {
                return FALSE;
        }
}
复制内容到剪贴板代码:
/**
* 强制退出
* @para string $message
*
*/
function dexit($message = '') {
        echo $message;
        output();
        exit();
}
复制内容到剪贴板代码:
/**        
* 过滤HTML代码的
* @para string $string
*
* @return string
*/
function dhtmlspecialchars($string) {
        if(is_array($string)) {
                foreach($string as $key => $val) {
                        $string[$key] = dhtmlspecialchars($val);
                }
        } else {
                $string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
                str_replace(array('&', '"', '<', '>'), array('&', '"', '&lt;', '&gt;'), $string));
        }
        return $string;
}
复制内容到剪贴板代码:
/**
* 用来设置header的
* @para string $string
* @para boolean $replace
* @para int $http_reponse_code
*
*/

function dheader($string, $replace = true, $http_response_code = 0) {
        $string = str_replace(array("\r", "\n"), array('', ''), $string);
        header($string, $replace, $http_response_code); //直接设置header
        if(preg_match('/^\s*location:/is', $string)) { //如果不符合location开头的话就exit了
                exit();
        }
}
复制内容到剪贴板代码:
/**
* 判断是不是文件上传了
* @return boolean
*/

function disuploadedfile($file) {
        return function_exists('is_uploaded_file') && (is_uploaded_file($file) || is_uploaded_file(str_replace('\\\\', '\\', $file)));
}

你可能感兴趣的:(加密,function,解密,include,operation)