轉自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('&', '"', '<', '>'), 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('&', '"', '<', '>'), $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('&', '"', '<', '>'), $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))); }