Discuz!常用函数解析
php, 函数, Discuz, param, Discuz二次开发
<?php
<?php
/*
[Discuz!] (C)2001-2007 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
[Discuz!] (C)2001-2007 Comsenz Inc.
This is NOT a freeware, use is subject to license terms
$Id: global.func.php 13426 2008-04-15 03:37:02Z heyond $
*/
*/
if(!defined(‘IN_DISCUZ‘)) {
exit(‘Access Denied‘);
}
exit(‘Access Denied‘);
}
/**
* 加密或者解密用户信息
* @param $string - 加密或解密的串
* @param $operation - 加密还是解密
* @param 密钥
* @return 返回字符串
* $ckey_length 随机密钥长度 取值 0-32;
* 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
* 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
* 当此值为 0 时,则不产生随机密钥
*/
* 加密或者解密用户信息
* @param $string - 加密或解密的串
* @param $operation - 加密还是解密
* @param 密钥
* @return 返回字符串
* $ckey_length 随机密钥长度 取值 0-32;
* 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
* 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
* 当此值为 0 时,则不产生随机密钥
*/
function authcode($string, $operation = ‘DECODE‘, $key = ‘‘, $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : $GLOBALS[‘discuz_auth_key‘]);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == ‘DECODE‘ ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ‘‘;
$key = md5($key ? $key : $GLOBALS[‘discuz_auth_key‘]);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == ‘DECODE‘ ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ‘‘;
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$key_length = strlen($cryptkey);
$string = $operation == ‘DECODE‘ ? base64_decode(substr($string, $ckey_length)) : sprintf(‘%010d‘, $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$string_length = strlen($string);
$result = ‘‘;
$box = range(0, 255);
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
$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]));
}
$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, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return ‘‘;
}
} else {
return $keyc.str_replace(‘=‘, ‘‘, base64_encode($result));
}
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return ‘‘;
}
} else {
return $keyc.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);
dsetcookie(‘loginuser‘, ‘‘, -86400 * 365);
dsetcookie(‘activationauth‘, ‘‘, -86400 * 365);
* 清理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);
dsetcookie(‘loginuser‘, ‘‘, -86400 * 365);
dsetcookie(‘activationauth‘, ‘‘, -86400 * 365);
$discuz_uid = $adminid = $credits = 0;
$discuz_user = $discuz_pw = $discuz_secques = ‘‘;
}
$discuz_user = $discuz_pw = $discuz_secques = ‘‘;
}
/**
* 检查积分下限
* @param $creditsarray - 积分数组
* @param $coef - 积分
*/
function checklowerlimit($creditsarray, $coef = 1) {
if(is_array($creditsarray)) {
global $extcredits, $id;
foreach($creditsarray as $id => $addcredits) {
$addcredits = $addcredits * $coef;
if($addcredits < 0 && ($GLOBALS[‘extcredits‘.$id] < $extcredits[$id][‘lowerlimit‘] || (($GLOBALS[‘extcredits‘.$id] + $addcredits) < $extcredits[$id][‘lowerlimit‘]))) {
if($coef == 1) {
showmessage(‘credits_policy_lowerlimit‘);
} else {
showmessage(‘credits_policy_num_lowerlimit‘);
}
}
}
}
}
* 检查积分下限
* @param $creditsarray - 积分数组
* @param $coef - 积分
*/
function checklowerlimit($creditsarray, $coef = 1) {
if(is_array($creditsarray)) {
global $extcredits, $id;
foreach($creditsarray as $id => $addcredits) {
$addcredits = $addcredits * $coef;
if($addcredits < 0 && ($GLOBALS[‘extcredits‘.$id] < $extcredits[$id][‘lowerlimit‘] || (($GLOBALS[‘extcredits‘.$id] + $addcredits) < $extcredits[$id][‘lowerlimit‘]))) {
if($coef == 1) {
showmessage(‘credits_policy_lowerlimit‘);
} else {
showmessage(‘credits_policy_num_lowerlimit‘);
}
}
}
}
}
/**
* 密码检测
*
* @param string $md5
* @param string $verified
* @param string $salt
* @return
* 0= Failed
* 1= MD5 with salt, 2= Dual MD5, 3= Normal Md5 4= MD5-16
*/
* 密码检测
*
* @param string $md5
* @param string $verified
* @param string $salt
* @return
* 0= Failed
* 1= MD5 with salt, 2= Dual MD5, 3= Normal Md5 4= MD5-16
*/
function checkmd5($md5, $verified, $salt = ‘‘) {
if(md5($md5.$salt) == $verified) {
$result = !empty($salt) ? 1 : 2;
} elseif(empty($salt)) {
$result = $md5 == $verified ? 3 : ((strlen($verified) == 16 && substr($md5, 8, 16) == $verified) ? 4 : 0);
} else {
$result = 0;
}
return $result;
}
if(md5($md5.$salt) == $verified) {
$result = !empty($salt) ? 1 : 2;
} elseif(empty($salt)) {
$result = $md5 == $verified ? 3 : ((strlen($verified) == 16 && substr($md5, 8, 16) == $verified) ? 4 : 0);
} else {
$result = 0;
}
return $result;
}
/**
* 检查模板源文件是否更新
* 当编译文件不存时强制重新编译
* 当 tplrefresh = 1 时检查文件
* 当 tplrefresh > 1 时,则根据 tplrefresh 取余,无余时则检查更新
*
*/
function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $tpldir) {
global $tplrefresh;
if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($GLOBALS[‘timestamp‘] % $tplrefresh))) {
if(empty($timecompare) || @filemtime($subtpl) > $timecompare) {
require_once DISCUZ_ROOT.‘./include/template.func.php‘;
parse_template($maintpl, $templateid, $tpldir);
return TRUE;
}
}
return FALSE;
}
* 检查模板源文件是否更新
* 当编译文件不存时强制重新编译
* 当 tplrefresh = 1 时检查文件
* 当 tplrefresh > 1 时,则根据 tplrefresh 取余,无余时则检查更新
*
*/
function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $tpldir) {
global $tplrefresh;
if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($GLOBALS[‘timestamp‘] % $tplrefresh))) {
if(empty($timecompare) || @filemtime($subtpl) > $timecompare) {
require_once DISCUZ_ROOT.‘./include/template.func.php‘;
parse_template($maintpl, $templateid, $tpldir);
return TRUE;
}
}
return FALSE;
}
/**
* 根据中文裁减字符串
* @param $string - 字符串
* @param $length - 长度
* @param $doc - 缩略后缀
* @return 返回带省略号被裁减好的字符串
*/
function cutstr($string, $length, $dot = ‘ ...‘) {
global $charset;
* 根据中文裁减字符串
* @param $string - 字符串
* @param $length - 长度
* @param $doc - 缩略后缀
* @return 返回带省略号被裁减好的字符串
*/
function cutstr($string, $length, $dot = ‘ ...‘) {
global $charset;
if(strlen($string) <= $length) {
return $string;
}
return $string;
}
$string = str_replace(array(‘&‘, ‘"‘, ‘<‘, ‘>‘), array(‘&‘, ‘"‘, ‘<‘, ‘>‘), $string);
$strcut = ‘‘;
if(strtolower($charset) == ‘utf-8‘) {
if(strtolower($charset) == ‘utf-8‘) {
$n = $tn = $noc = 0;
while($n < strlen($string)) {
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($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;
}
break;
}
}
if($noc > $length) {
$n -= $tn;
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
$strcut = str_replace(array(‘&‘, ‘"‘, ‘<‘, ‘>‘), array(‘&‘, ‘"‘, ‘<‘, ‘>‘), $strcut);
return $strcut.$dot;
}
}
/**
* 处理转义字符
* @param $string -字符串
* @param $force - 是否强制
* @return 返回整理好的字符串
*/
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;
}
* 处理转义字符
* @param $string -字符串
* @param $force - 是否强制
* @return 返回整理好的字符串
*/
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;
}
/**
* 检测日期的有效性
*/
function datecheck($ymd, $sep=‘-‘) {
if(!empty($ymd)) {
list($year, $month, $day) = explode($sep, $ymd);
return checkdate($month, $day, $year);
} else {
return FALSE;
}
}
* 检测日期的有效性
*/
function datecheck($ymd, $sep=‘-‘) {
if(!empty($ymd)) {
list($year, $month, $day) = explode($sep, $ymd);
return checkdate($month, $day, $year);
} else {
return FALSE;
}
}
/**
* 调试信息
*/
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;
}
}
* 调试信息
*/
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;
}
}
/**
* 退出系统
*/
function dexit($message = ‘‘) {
echo $message;
output();
exit();
}
* 退出系统
*/
function dexit($message = ‘‘) {
echo $message;
output();
exit();
}
function dfopen($url, $limit = 0, $post = ‘‘, $cookie = ‘‘, $bysocket = FALSE, $ip = ‘‘, $timeout = 15, $block = TRUE) {
$return = ‘‘;
$matches = parse_url($url);
$host = $matches[‘host‘];
$path = $matches[‘path‘] ? $matches[‘path‘].‘?‘.$matches[‘query‘].‘#‘.$matches[‘fragment‘] : ‘/‘;
$port = !empty($matches[‘port‘]) ? $matches[‘port‘] : 80;
$return = ‘‘;
$matches = parse_url($url);
$host = $matches[‘host‘];
$path = $matches[‘path‘] ? $matches[‘path‘].‘?‘.$matches[‘query‘].‘#‘.$matches[‘fragment‘] : ‘/‘;
$port = !empty($matches[‘port‘]) ? $matches[‘port‘] : 80;
if($post) {
$out = "POST $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
//$out .= "Referer: $boardurl\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= ‘Content-Length: ‘.strlen($post)."\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cache-Control: no-cache\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
$out .= $post;
} else {
$out = "GET $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
//$out .= "Referer: $boardurl\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
}
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
if(!$fp) {
return ‘‘;//note $errstr : $errno \r\n
} else {
stream_set_blocking($fp, $block);
stream_set_timeout($fp, $timeout);
@fwrite($fp, $out);
$status = stream_get_meta_data($fp);
if(!$status[‘timed_out‘]) {
while (!feof($fp)) {
if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
break;
}
}
$out = "POST $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
//$out .= "Referer: $boardurl\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= ‘Content-Length: ‘.strlen($post)."\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cache-Control: no-cache\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
$out .= $post;
} else {
$out = "GET $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
//$out .= "Referer: $boardurl\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
}
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
if(!$fp) {
return ‘‘;//note $errstr : $errno \r\n
} else {
stream_set_blocking($fp, $block);
stream_set_timeout($fp, $timeout);
@fwrite($fp, $out);
$status = stream_get_meta_data($fp);
if(!$status[‘timed_out‘]) {
while (!feof($fp)) {
if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
break;
}
}
$stop = false;
while(!feof($fp) && !$stop) {
$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
$return .= $data;
if($
while(!feof($fp) && !$stop) {
$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
$return .= $data;
if($
知识推荐
- HttpClient学习记录-系列2(源码学习)
- js生成随机固定长度字符串的简便方法
- 抓取apache2的进程pid
- bootstrap4中自带flex的css类
- 转帖 eclipse Web项目WebContent目录修改
- 漫画网站开发,漫画网站定制,漫画网站建设源码
- CSS边框作图
- Apache Kylin 是什么?
- 【RL-TCPnet网络教程】第2章 ???嵌入式网络协议栈基础知识
- org.apache.ibatis.binding.BindingException: Parameter 'start' not found. Available parameters are [1, 0, param1, param2]
- 把eclipse写好的web项目导入idea 部署到Tomcat
- 使用flume将数据sink到HBase
- php 发送http请求 GET POST
- HTML
- base标签抓取错误:我不再用discuz程序建设门户网站[图]
- vue-webpack3x --> webpack4x
- web基础要点记录
- EXP9 Web安全基础实践