<?php
/*
公共函数
*/
if(!defined('IN_AACMS')) {
exit('Access Denied');
}
//SQL ADDSLASHES
function daddslashes(&$string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val);
}
} else {
$string = addslashes($string);
}
return $string;
}
function dhtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = dhtmlspecialchars($val);
}
} else {
$string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string);
if(strpos($string, '&#') !== false) {
$string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $string);
}
}
return $string;
}
//字符串解密加密
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
global $_G;
$ckey_length = 4; // 随机密钥长度 取值 0-32;
// 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
// 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
// 当此值为 0 时,则不产生随机密钥
$key = md5($key ? $key : $_G['setting']['sitekey']);
$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);
$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);
$result = '';
$box = range(0, 255);
$rndkey = array();
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;
}
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, 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 dsetcookie($var, $value = '', $life = 0, $prefix = 1, $httponly = false) {
global $_G;
$config = $_G['config']['cookie'];
$_G['cookie'][$var] = $value;
$var = ($prefix ? $config['cookiepre'] : '').$var;
$_COOKIE[$var] = $var;
if($value == '' || $life < 0) {
$value = '';
$life = -1;
}
$life = $life > 0 ? $_G['timestamp'] + $life : ($life < 0 ? $_G['timestamp'] - 31536000 : 0);
$path = $httponly && PHP_VERSION < '5.2.0' ? $config['cookiepath'].'; HttpOnly' : $config['cookiepath'];
$secure = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
if(PHP_VERSION < '5.2.0') {
setcookie($var, $value, $life, $path, $config['cookiedomain'], $secure);
} else {
setcookie($var, $value, $life, $path, $config['cookiedomain'], $secure, $httponly);
}
}
//检查验证码
function ckseccode($seccode) {
$check = true;
$cookie_seccode = empty($_COOKIE['seccode'])?'':authcode($_COOKIE['seccode'], 'DECODE');
if(empty($cookie_seccode) || strtolower($cookie_seccode) != strtolower($seccode)) {
$check = false;
}
return $check;
}
//对话框
function showmessage($msgkey, $url_forward='', $second=1, $values=array()) {
global $_G;
obclean();
$message = lang('message', $msgkey);
$message = "<a href=\"$url_forward\">$message</a><script>setTimeout(\"window.location.href ='$url_forward';\", ".($second*1000).");</script>";
echo $message;
exit;
}
//判断提交是否正确
function submitcheck($var) {
if(!empty($_POST[$var]) && $_SERVER['REQUEST_METHOD'] == 'POST') {
if((empty($_SERVER['HTTP_REFERER']) || preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) == preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) && $_POST['formhash'] == formhash()) {
return true;
} else {
exit("<script>alert('Access Denied');window.history.go(-1)</script>");
}
} else {
return false;
}
}
//ob
function obclean() {
global $_G;
ob_end_clean();
if ($_G['gzipcompress'] && function_exists('ob_gzhandler')) {
ob_start('ob_gzhandler');
} else {
ob_start();
}
}
//模板调用
function template($tpl, $pre='before') {
if(preg_match('/\//', $tpl)) {
$objfile = ROOT.'./data/tpl_cache/'.str_replace('/', '.', $tpl).'.'.$pre.'.php';
} else {
$objfile = ROOT.'./data/tpl_cache/'.$tpl.'.'.$pre.'.php';
}
if(!file_exists($objfile)) {
include_once libfile('function/template');
parse_template($tpl, $pre);
}
return $objfile;
}
//模块
function block($thekey, $param) {
global $_G, $_BLOCK;
$_BLOCK[$thekey] = array();
$havethekey = false;
$needcache = 0;
//缓存key
$cachekey = dmd5($thekey.$param);
$paramarr = parseparameter($param, 0);
if(!empty($paramarr['cachetime'])) {
if($paramarr['perpage'] && $_GET['page']) {
//分页
$cachekey = dmd5($thekey.$param.$_GET['page']);
}
$cacheupdatetime = $paramarr['cachetime'];
} else {
$cacheupdatetime = 0;
$needcache = 3;//DO NOT CACHE
}
if($cacheupdatetime) {
//获取缓存
getcache($cachekey);
if(!isset($_BLOCK[$cachekey])) {
$needcache = 1;//没有缓存
} else {
//创建下次更新时间
if(!empty($_BLOCK[$cachekey]['filemtime'])) $_BLOCK[$cachekey]['updatetime'] = $_BLOCK[$cachekey]['filemtime'] + $cacheupdatetime;
if($_BLOCK[$cachekey]['updatetime'] < $_G['timestamp']) {
$needcache = 2;//需要更新
}
}
}
//BLOCK中没有缓存
if($needcache) {
$theblockarr = array();
$taglibfile = ROOT."./source/function/block/block.$thekey.php"; //定义要包含的文件
if(!file_exists($taglibfile)) {
exit("tag lib file : $taglibfile Not found or have no access!");
} else {
include_once($taglibfile); //BLOCK处理文件
}
//定义要调用的函数
$block_func = 'block_'.$thekey;
$theblockarr = $block_func($paramarr);
$_BLOCK[$cachekey] = $theblockarr;
//保存标签的参数进BLOCK
$havethekey = true;
$_BLOCK[$thekey]['value'] = serialize($theblockarr);
$_BLOCK[$thekey]['updatetime'] = $_G['timestamp'] + $cacheupdatetime;
//没有缓存或缓存过期,则数据保存进BLOCK
if($needcache == 1 || $needcache == 2) {
//INSERT-UPDATE
$_G['tpl_blockvalue'][] = array(
'cachekey' => $cachekey,
'uid' => $uid,
'cachename' => $thekey,
'value' => $_BLOCK[$thekey]['value'],
'updatetime' => $_BLOCK[$thekey]['updatetime']
);
}
}
//如果BLOCK中有缓存
if(!$havethekey) {
if(!empty($_BLOCK[$cachekey]['value'])) {
$_BLOCK[$cachekey] = unserialize($_BLOCK[$cachekey]['value']);
} else {
$_BLOCK[$cachekey] = array();
}
}
if(!empty($paramarr['cachename'])) {
if(empty($_BLOCK[$cachekey]['multipage'])) {
$_BLOCK[$paramarr['cachename'].'_multipage'] = '';
} else {
$_BLOCK[$paramarr['cachename'].'_multipage'] = $_BLOCK[$cachekey]['multipage']; //分页
}
$_BLOCK[$paramarr['cachename']] = $_BLOCK[$cachekey]; //数据数组
unset($_BLOCK[$paramarr['cachename']]['multipage']); //移除分页
}
}
//从数据库或文本中取出cache值,存到BLOCK中
function getcache($cachekey, $tablename='cache') {
global $_G, $_BLOCK;
$cachegrade = 1;
if($_G['setting']['allowcache'] && $cachekey && empty($_BLOCK[$cachekey])) {
if($_G['setting']['cachemode'] == 'file') {
$cachefile = ROOT.'./data/block_cache/'.substr($cachekey, 0, $cachegrade).'/'.$cachekey.'.cache.data';
if(f