📄 global.func.php
字号:
<?php
defined('IN_PHPCMS') or exit('Access Denied');
function showmessage($msg, $url_forward = 'goback', $ms=1250)
{
global $CONFIG,$PHPCMS,$debuginfo;
$templatefile = defined('IN_ADMIN') ? PHPCMS_ROOT.'/admin/templates/showmessage.tpl.php' : template('phpcms','showmessage');
include $templatefile;
exit;
}
function new_htmlspecialchars($string)
{
return is_array($string) ? array_map('new_htmlspecialchars', $string) : htmlspecialchars($string,ENT_QUOTES);
}
function new_addslashes($string)
{
if(!is_array($string)) return addslashes($string);
foreach($string as $key => $val) $string[$key] = new_addslashes($val);
return $string;
}
function new_stripslashes($string)
{
if(!is_array($string)) return stripslashes($string);
foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
return $string;
}
function filter_xss($string, $allowedtags = '', $disabledattributes = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'))
{
if(is_array($string))
{
foreach($string as $key => $val) $string[$key] = filter_xss($val, ALLOWED_HTMLTAGS);
}
else
{
$string = preg_replace('/\s('.implode('|', $disabledattributes).').*?([\s\>])/', '\\2', preg_replace('/<(.*?)>/ie', "'<'.preg_replace(array('/javascript:[^\"\']*/i', '/(".implode('|', $disabledattributes).")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", strip_tags($string, $allowedtags)));
}
return $string;
}
function strip_sql($string)
{
global $search_arr,$replace_arr;
return is_array($string) ? array_map('strip_sql', $string) : preg_replace($search_arr, $replace_arr, $string);
}
function strip_textarea($string)
{
return nl2br(str_replace(' ', ' ', htmlspecialchars($string, ENT_QUOTES)));
}
function strip_js($string, $js = 1)
{
$string = str_replace(array("\n","\r","\""),array('','',"\\\""),$string);
return $js==1 ? "document.write(\"".$string."\");\n" : $string;
}
function str_safe($string)
{
$searcharr = array("/(javascript|jscript|js|vbscript|vbs|about):/i","/on(mouse|exit|error|click|dblclick|key|load|unload|change|move|submit|reset|cut|copy|select|start|stop)/i","/<script([^>]*)>/i","/<iframe([^>]*)>/i","/<frame([^>]*)>/i","/<link([^>]*)>/i","/@import/i");
$replacearr = array("\\1\n:","on\n\\1","<script\\1>","<iframe\\1>","<frame\\1>","<link\\1>","@\nimport");
$string = preg_replace($searcharr,$replacearr,$string);
$string = str_replace("&#","&\n#",$string);
return $string;
}
function random($length, $chars = '0123456789')
{
$hash = '';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function mkcookie($var, $value = '', $time = 0)
{
global $CONFIG,$PHP_TIME;
$time = $time > 0 ? $time : (empty($value) ? $PHP_TIME - 3600 : 0);
$s = $_SERVER['SERVER_PORT'] == '443' ? 1 : 0;
$var = $CONFIG['cookiepre'].$var;
return setcookie($var, $value, $time, $CONFIG['cookiepath'], $CONFIG['cookiedomain'], $s);
}
function getcookie($var)
{
global $CONFIG;
$var = $CONFIG['cookiepre'].$var;
return isset($_COOKIE[$var]) ? $_COOKIE[$var] : FALSE;
}
if(!function_exists('file_put_contents'))
{
define('FILE_APPEND', 8);
function file_put_contents($file, $string, $append = '')
{
$mode = $append == '' ? 'wb' : 'ab';
$fp = @fopen($file, $mode) or exit("Can not open file $file !");
flock($fp, LOCK_EX);
$stringlen = @fwrite($fp, $string);
flock($fp, LOCK_UN);
@fclose($fp);
return $stringlen;
}
}
function file_down($file, $filename = '')
{
global $PHP_TIME;
if(!file_exists($file)) showmessage("The file $file is not exists !");
$filename = $filename ? $filename : basename($file);
$filetype = fileext($filename);
$filesize = filesize($file);
ob_end_clean();
@set_time_limit(900);
header('Cache-control: max-age=31536000');
header('Expires: '.gmdate('D, d M Y H:i:s', $PHP_TIME + 31536000).' GMT');
header('Content-Encoding: none');
header('Content-Length: '.$filesize);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Type: '.$filetype);
readfile($file);
exit;
}
function phpcache($is_js = 0)
{
global $CONFIG,$cachefiledir,$cachefile;
if(!$is_js && $CONFIG['phpcache'] != '2') return FALSE;
$contents = ob_get_clean();
if($is_js) $contents = strip_js($contents);
if($CONFIG['phpcache'] == '2' && $cachefiledir && $cachefile)
{
dir_create($cachefiledir);
file_put_contents($cachefile, $contents);
@chmod($cachefile, 0777);
}
header('Expires: Mon, 26 Jul 2000 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
echo $contents;
}
function cache_read($file, $mode = 'i')
{
$cachefile = PHPCMS_CACHEDIR.$file;
if(!file_exists($cachefile)) return array();
return $mode == 'i' ? include $cachefile : file_get_contents($cachefile);
}
function cache_write($file, $string, $type = 'array')
{
if(is_array($string))
{
$type = strtolower($type);
if($type == 'array')
{
$string = "<?php\n return ".var_export($string,TRUE).";\n?>";
}
elseif($type == 'constant')
{
$data='';
foreach($string as $key => $value) $data .= "define('".strtoupper($key)."','".addslashes($value)."');\n";
$string = "<?php\n".$data."\n?>";
}
}
$strlen = file_put_contents(PHPCMS_CACHEDIR.$file, $string);
chmod(PHPCMS_CACHEDIR.$file, 0777);
return $strlen;
}
function cache_delete($file)
{
return @unlink(PHPCMS_CACHEDIR.$file);
}
function phpcms_auth($txt, $operation = 'ENCODE', $key = '')
{
$key = $key ? $key : $GLOBALS['phpcms_auth_key'];
require_once PHPCMS_ROOT.'/include/auth.func.php';
return $operation=='ENCODE' ? phpcms_encode($txt, $key) : phpcms_decode($txt, $key);
}
function createhtml($filename, $mod_root = '')
{
global $TEMP;
if(!defined('CREATEHTML')) define('CREATEHTML', 1);
@extract($GLOBALS, EXTR_SKIP);
if(!$mod_root) $mod_root = defined('MOD_ROOT') ? MOD_ROOT : PHPCMS_ROOT;
include $mod_root.'/include/createhtml/'.$filename.'.php';
}
function template($module = 'phpcms', $template = 'index')
{
global $CONFIG;
$compiledtplfile = $CONFIG['templatescachedir'].$module.'_'.$template.'.tpl.php';
if($CONFIG['templaterefresh'])
{
$tplfile = PHPCMS_ROOT.'/templates/'.$CONFIG['defaulttemplate'].'/'.$module.'/'.$template.'.html';
if(!file_exists($compiledtplfile) || @filemtime($tplfile) > @filemtime($compiledtplfile))
{
require_once PHPCMS_ROOT.'/include/template.func.php';
template_refresh($tplfile, $compiledtplfile);
}
}
return $compiledtplfile;
}
function str_cut($string, $length, $dot = ' ...')
{
global $CONFIG;
$strlen = strlen($string);
if($strlen <= $length) return $string;
$string = str_replace(array(' ', '&', '"', ''', '“', '”', '—', '<', '>'), array(' ', '&', '"', "'", '鈥
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -