Common functions extracted from Phpcms v9
/**
* Returns a string or array that has been processed by addslashes
* @param $string string or array to be processed
* @return Mixed
*/
function New_addslashes ($string) {
? ? if (!is_array ($string)) return addslashes ($string);
? ? foreach ($string as $key = + $val) $string [$key] = new_addslashes ($val);
? ? return $string;
}
/**
* Returns a string or array that has been processed by stripslashes
* @param $string string or array to be processed
* @return Mixed
*/
function New_stripslashes ($string) {
? ? if (!is_array ($string)) return stripslashes ($string);
? ? foreach ($string as $key = + $val) $string [$key] = new_stripslashes ($val);
? ? return $string;
}
/**
* Returns a string or array that has been processed by Addslashe
* @param $obj string or array to be processed
* @return Mixed
*/
function New_html_special_chars ($string) {
? ? if (!is_array ($string)) return Htmlspecialchars ($string);
? ? foreach ($string as $key = + $val) $string [$key] = New_html_special_chars ($val);
? ? return $string;
}
/**
* Safe Filter function
*
* @param $string
* @return String
*/
function Safe_replace ($string) {
? ? $string = Str_replace ('%20 ', ', $string);
? ? $string = Str_replace ('%27 ', ' ', $string);
? ? $string = Str_replace ('%2527 ', ' ', $string);
? ? $string = Str_replace (' * ', ' ', $string);
? ? $string = Str_replace (' "', '" ', $string);
? ? $string = Str_replace ("'", "', $string);
? ? $string = Str_replace (' "', ' ', $string);
? ? $string = Str_replace ('; ', ' ', $string);
? ? $string = Str_replace (' < ', ' < ', $string);
? ? $string = Str_replace (' > ', ' > ', $string);
? ? $string = Str_replace ("{", "', $string);
? ? $string = Str_replace ('} ', ' ', $string);
? ? return $string;
}
/**
* Filter ASCII code from 0-28 control characters
* @return String
*/
function Trim_unsafe_control_chars ($STR) {
? ? $rule = '/['. chr (1). '-' . Chr (8). Chr (11). '-' . Chr (12). Chr (14). '-' . Chr (31). ']*/';
? ? Return Str_replace (Chr (0), ", Preg_replace ($rule,", $str));
}
/**
* Format Text field contents
*
* @param $string Text field content
* @return String
*/
function Trim_textarea ($string) {
? ? $string = NL2BR (Str_replace (",", $string));
? ? return $string;
}
/**
* Format text as a string suitable for JS output
* @param string $string strings to be processed
* @param intval $isjs whether to perform string formatting, default to execute
* @return string processed by string
*/
function Format_js ($string, $isjs = 1)
{
? ? $string = Addslashes (Str_replace ("\ r", "\ n"), Array (', '), $string));
? ? Return $ISJS? ' document.write ('. $string. '); ': $string;
}
/**
* Escape JavaScript code tags
*
* @param $str
* @return Mixed
*/
function Trim_script ($STR) {
? ? $str = Preg_replace ('/\< ([\/]?) Script ([^\>]*?) \>/si ', ' <\\1script\\2> ', $str);
? ? $str = Preg_replace ('/\< ([\/]?) IFRAME ([^\>]*?) \>/si ', ' <\\1iframe\\2> ', $str);
? ? $str = Preg_replace ('/\< ([\/]?) FRAME ([^\>]*?) \>/si ', ' <\\1frame\\2> ', $str);
? ? $str = preg_replace ('/]]\>/si ', ']] > ', $STR);
? ? return $str;
}
/**
* Get the full URL address of the current page
*/
function Get_url () {
? ? $sys _protocal = isset ($_server[' server_port ') && $_server[' server_port '] = = ' 443 '? ' https://': '/HTTP ';
? ? $php _self = $_server[' php_self ')? Safe_replace ($_server[' php_self '): Safe_replace ($_server[' script_name ']);
? ? $path _info = isset ($_server[' path_info ')? Safe_replace ($_server[' path_info '): ';
? ? $relate _url = isset ($_server[' Request_uri ')? Safe_replace ($_server[' Request_uri '): $php _self. (Isset ($_server[' query_string '])? '?'. Safe_replace ($_server[' query_string '): $path _info);
? ? Return $sys _protocal. (Isset ($_server[' http_host '))? $_server[' Http_host ']: '). $relate _url;
}
/**
* Character interception support UTF8/GBK
* @param $string
* @param $length
* @param $dot
*/
function Str_cut ($string, $length, $dot = ' ... ') {
? ? $strlen = strlen ($string);
? ? if ($strlen <= $length) return $string;
? ? $string = Str_replace (', ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ' ', '-', ' < ', ' > ', ' * ', ' ... '), Array (' ∵ ', ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' ' ', ' ' ... '), $string);
? ? $strcut = ";
? ? if (Strtolower (CHARSET) = = ' Utf-8 ') {
? ?? ??? $length = Intval ($length-strlen ($DOT)-$length/3);
? ?? ??? $n = $tn = $noc = 0;
? ?? ??? while ($n < strlen ($string)) {
? ?? ?? ?? ? $t = Ord ($string [$n]);
? ?? ?? ?? if ($t = = 9 | | $t = = 10 | | (<= $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 (<= $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);
? ?? ??? $strcut = Str_replace (' ∵ ', ' ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' * ', ' ... '), Array (', ', ' & ', ' ' ', ' ' ', ' ' ' ', ' ' ' ', '-', ' < ', ' > ', ' ' ', ' ' ... '), $strcut);
? ? } else {
? ?? ??? $dotlen = strlen ($dot);
? ?? ??? $maxi = $length-$dotlen-1;
? ?? ??? $current _str = ";
? ?? ??? $search _arr = Array (' & ', ' ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' * ', ' ... ', ' ∵ ');
? ?? ??? $replace _arr = Array (' & ', ' ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' * ', ' ... ', ');
? ?? ??? $search _flip = Array_flip ($search _arr);
? ?? ??? for ($i = 0; $i < $maxi; $i + +) {
? ?? ?? ?? ? $current _str = Ord ($string [$i]) > 127? $string [$i]. $string [+ + $i]: $string [$i];
? ?? ?? ?? if (In_array ($current _str, $search _arr)) {
? ?? ?? ?? ?? ? $key = $search _flip[$current _str];
? ?? ?? ?? ?? ? $current _str = str_replace ($search _arr[$key], $replace _arr[$key], $current _str);
? ?? ?? ?? ?}
? ?? ?? ?? ? $strcut. = $current _str;
? ?? ???}
? ? }
? ? return $strcut. $dot;
}
/**
* GET request IP
*
* @return IP Address
*/
function IP () {
? ? if (getenv (' http_client_ip ') && strcasecmp (getenv (' http_client_ip '), ' unknown ') {
? ?? ??? $ip = getenv (' http_client_ip ');
? ? } elseif (getenv (' http_x_forwarded_for ') && strcasecmp (getenv (' http_x_forwarded_for '), ' unknown ') {
? ?? ??? $ip = getenv (' http_x_forwarded_for ');
? ? } elseif (getenv (' remote_addr ') && strcasecmp (getenv (' remote_addr '), ' unknown ') {
? ?? ??? $ip = getenv (' remote_addr ');
? ? } elseif (Isset ($_server[' remote_addr ") && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], ' unknown ') {
? ?? ??? $ip = $_server[' remote_addr ');
? ? }
? ? Return Preg_match ('/[\d\.] {7,15}/', $ip, $matches)? $matches [0]: ";
}
function Get_cost_time () {
? ? $microtime = Microtime (TRUE);
? ? return $microtime-sys_start_time;
}
/**
* Program Execution time
*
* @return?? Int?? Unit ms
*/
function Execute_time () {
? ? $stime = Explode (' ', sys_start_time);
? ? $etime = Explode (' ', Microtime ());
? ? Return Number_format (($etime [1] + $etime [0]-$stime [1]-$stime [0]), 6);
}
/**
* Generate random string
*
* @param?? Int??? ??? $length?? Output length
* @param?? String???? $chars?. optional, default is 0123456789
* @return?? string???? 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;
}
/**
* Convert string to array
*
* @param?? String?? $data?? String
* @return?? Array?? Returns an array format that returns an empty array if data is empty
*/
function String2array ($data) {
? ? if ($data = = ") return array ();
? ? Eval ("\ $array = $data;");
? ? return $array;
}
/**
* Convert an array to a string
*
* @param?? Array?? $data??? ??? Array
* @param?? BOOL?? $isformdata?? If 0, does not use new_stripslashes processing, optional parameter, default is 1
* @return?? String?? Returns a string, or null if data is NULL
*/
function array2string ($data, $isformdata = 1) {
? ? if ($data = = ") return";
? ? if ($isformdata) $data = New_stripslashes ($data);
? ? Return Addslashes (Var_export ($data, TRUE));
}
/**
* Convert bytes to other units
*
*
* @param?? String?? $filesize?? BYTE size
* @return?? String?? return size
*/
function Sizecount ($filesize) {
? ? if ($filesize >= 1073741824) {
? ?? ??? $filesize = Round ($filesize/1073741824 * 100)/100. ' GB ';
? ? } elseif ($filesize >= 1048576) {
? ?? ??? $filesize = Round ($filesize/1048576 * 100)/100. ' MB ';
? ? } elseif ($filesize >= 1024) {
? ?? ??? $filesize = Round ($filesize/1024 * 100)/100. ' KB ';
? ? } else {
? ?? ??? $filesize = $filesize. ' Bytes ';
? ? }
? ? return $filesize;
}
?
/**
* String encryption, decryption function
*
*
* @param???? String???? $txt???????? String
* @param???? String???? $operation???? Encode is encrypted, decode is decrypted, optional parameter, default is encode,
* @param???? String???? $key???????? Keys: Numbers, letters, underscores
* @return???? String
*/
function Sys_auth ($txt, $operation = ' ENCODE ', $key = ') {
? ? ? ? $key???? = $key? $key: Pc_base::load_config (' System ', ' Auth_key ');
? ? ? ? $txt???? = $operation = = ' ENCODE '? (string) $txt: Base64_decode ($txt);
? ? ? ? $len???? = strlen ($key);
? ? ? ? $code???? = '';
? ? ? ? for ($i =0; $i ? ? ? ? ? ? ? ? $k???????? = $i% $len;
? ? ? ? ? ? ? ? $code??. = $txt [$i] ^ $key [$k];
? ? ? ? }
? ? ? ? $code = $operation = = = ' DECODE '? $code: Base64_encode ($code);
? ? ? ? return $code;
}
/**
* Language file Processing
*
* @param???? String???????? $language???? Identifiers
* @param???? Array???????? $pars???? An escaped array, a two-dimensional array, ' key1 ' = ' value1 ', ' key2 ' = ' value2 ',
* @param???? String???????? $modules multiple modules separated by commas, such as: Member,guestbook
* @return???? String???????? Language characters
*/
function L ($language = ' no_language ', $pars = Array (), $modules = ') {
? ? ? ? Static $LANG = Array ();
? ? ? ? static $LANG _modules = Array ();
? ? ? ? $lang = pc_base::load_config (' System ', ' Lang ');
? ? ? ? if (! $LANG) {
? ? ? ? ? ? ? ? require_once Pc_path. ' Languages '. Directory_separator. $lang. Directory_separator. ' system.lang.php ';
? ? ? ? ? ? ? ? if (defined (' In_admin ')) require_once Pc_path. ' Languages '. Directory_separator. $lang. Directory_separator. ' system_menu.lang.php ';
? ? ? ? ? ? ? ? if (file_exists (Pc_path. ' Languages '). Directory_separator. $lang. Directory_separator. Route_m. lang.php ')) require pc_path. ' Languages '. Directory_separator. $lang. Directory_separator. Route_m. '. lang.php ';
? ? ? ? }
? ? ? ? if (!empty ($modules)) {
? ? ? ? ? ? ? ? $modules = Explode (', ', $modules);
? ? ? ? ? ? ? ? foreach ($modules as $m) {
? ? ? ? ? ? ? ? ? ? ? ? if (!isset ($LANG _modules[$m])) require pc_path. ' Languages '. Directory_separator. $lang. Directory_separator. $m. '. lang.php ';
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (!array_key_exists ($language, $LANG)) {
? ? ? ? ? ? ? ? return $LANG [' No_language ']. ' ['. $language. '] ';
? ? ? ? } else {
? ? ? ? ? ? ? ? $language = $LANG [$language];
? ? ? ? ? ? ? ? if ($pars) {
? ? ? ? ? ? ? ? ? ? ? ? foreach ($pars as $_k=>$_v) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? $language = Str_replace (' {'. $_k '} ', $_v, $language);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return $language;
? ? ? ? }
}
/**
* Template Invocation
*
* @param $module
* @param $template
* @param $istag
* @return Unknown_type
*/
function template ($module = ' content ', $template = ' index ', $style = ') {
? ? ? ? if (!empty ($style) &&!defined (' style ')) {
? ? ? ? ? ? ? ? Define (' STYLE ', $style);
? ? ? ? } elseif (Empty ($style) &&!defined (' style ')) {
? ? ? ? ? ? ? ? if (defined (' SITEID ')) {
? ? ? ? ? ? ? ? ? ? ? ? $siteid = SiteID;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? $siteid = Param::get_cookie (' WebSiteID ');
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? $sitelist = GetCache (' sitelist ', ' Commons ');
? ? ? ? ? ? ? ? if (!empty ($siteid)) {
? ? ? ? ? ? ? ? ? ? ? ? $style = $sitelist [$siteid] [' Default_style '];
? ? ? ? ? ? ? ? }
? ? ? ? } elseif (Empty ($style) && defined (' style ')) {
? ? ? ? ? ? ? ? $style = style;
? ? ? ? } else {
? ? ? ? ? ? ? ? $style = ' default ';
? ? ? ? }
? ? ? ? if (! $style) $style = ' default ';
? ? ? ? $template _cache = Pc_base::load_sys_class (' Template_cache ');
? ? ? ? $compiledtplfile = Phpcms_path. ' Caches '. Directory_separator. ' Caches_template '. Directory_separator. $style. Directory_separator. $module. Directory_separator. $template. PHP ';
? ? ? ? if (file_exists (pc_path. ' Templates '). Directory_separator. $style. Directory_separator. $module. Directory_separator. $template. HTML ')) {
? ? ? ? ? ? ? ? if (!file_exists ($compiledtplfile) | | (@filemtime (pc_path. ' Templates '). Directory_separator. $style. Directory_separator. $module. Directory_separator. $template. HTML ') > @filemtime ($compiledtplfile))) {????
? ? ? ? ? ? ? ? ? ? ? ? $template _cache->template_compile ($module, $template, $style);
? ? ? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? ? ? $compiledtplfile = Phpcms_path. ' Caches '. Directory_separator. ' Caches_template '. Directory_separator. ' Default '. Directory_separator. $module. Directory_separator. $template. PHP ';
? ? ? ? ? ? ? ? if (!file_exists ($compiledtplfile) | | (File_exists (pc_path. ' Templates '). Directory_separator. ' Default '. Directory_separator. $module. Directory_separator. $template. HTML ') && filemtime (pc_path. ' Templates '). Directory_separator. ' Default '. Directory_separator. $module. Directory_separator. $template. HTML ') > Filemtime ($compiledtplfile)) {
? ? ? ? ? ? ? ? ? ? ? ? $template _cache->template_compile ($module, $template, ' Default ');
? ? ? ? ? ? ? ? } elseif (!file_exists (pc_path. ' Templates '). Directory_separator. ' Default '. Directory_separator. $module. Directory_separator. $template. HTML ')) {
? ? ? ? ? ? ? ? ? ? ? ? ShowMessage (' Template does not exist. ') Directory_separator. $style. Directory_separator. $module. Directory_separator. $template. HTML ');
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return $compiledtplfile;
}
/**
* Output Custom Error
*
* @param $errno Error number
* @param $errstr Error description
* @param $errfile Error file address
* @param $errline Error line number
* @return String Error hint
*/
function My_error_handler ($errno, $errstr, $errfile, $errline) {
? ? ? ? if ($errno ==8) return ';
? ? ? ? $errfile = Str_replace (Phpcms_path, ", $errfile);
? ? ? ? if (pc_base::load_config (' System ', ' errorlog ')) {
? ? ? ? ? ? ? ? Error_log (Date (' m-d h:i:s ', sys_time). ' | '. $errno. ' | '. Str_pad ($errstr, 30). ' | '. $errfile. ' | '. $errline. ' \ r \ n ", 3, Cache_path. ' error_log.php ');
? ? ? ? } else {
? ? ? ? ? ? ? ? $str = ' errorno: '. $errno. ', str: '. $errstr. ', File: '. $errfile. ', line '. $errline. '
Need help?';
? ? ? ? ? ? ? ? Echo $str;
? ? ? ? }
}
/**
* Prompt Information page jump, jump address if an array is passed in, the page prompts multiple addresses for the user to select, and the default jump address is the first value of the array with a time of 5 seconds.
* ShowMessage (' login successful ', array (' default jump address ' = ' http://www.phpcms.cn '));
* @param string $msg tip information
* @param mixed (string/array) $url _forward Jump Address
* @param int $ms Jump Wait Time
*/
function ShowMessage ($msg, $url _forward = ' GoBack ', $ms = 1250, $dialog = ', $returnjs = ') {
? ? ? ? if (defined (' In_admin ')) {
? ? ? ? ? ? ? ? Include (Admin::admin_tpl (' showmessage ', ' admin '));
? ? ? ? } else {
? ? ? ? ? ? ? ? Include (Template (' content ', ' message '));
? ? ? ? }
? ? ? ? Exit
}
/**
* Query whether a character exists in a string
*
* @param $haystack string
* @param $needle characters to look for
* @return BOOL
*/
function str_exists ($haystack, $needle)
{
? ? ? ? Return! (Strpos ($haystack, $needle) = = = = FALSE);
}
/**
* Get file extensions
*
* @param $filename file name
* @return name extension
*/
function Fileext ($filename) {
? ? ? ? Return Strtolower (Trim (substr (STRRCHR ($filename, '. '), 1, 10));
}
/**
* Load Template Tag Cache
* @param string $name cache name
* @param integer $times cache time
*/
function Tpl_cache ($name, $times = 0) {
? ? ? ? $filepath = ' Tpl_data ';
? ? ? ? $info = Getcacheinfo ($name, $filepath);
? ? ? ? if (sys_time-$info [' filemtime '] >= $times) {
? ? ? ? ? ? ? ? return false;
? ? ? ? } else {
? ? ? ? ? ? ? ? Return GetCache ($name, $filepath);
? ? ? ? }
}
/**
* Write cache, default is file cache, do not load cache configuration.
* @param $name Cache Name
* @param $data Cache data
* @param $filepath Data path (module name) caches/cache_$filepath/
* @param $type cache type [FILE,MEMCACHE,APC]
* @param $config Configuration Name
* @param $timeout Expiration Time
*/
function Setcache ($name, $data, $filepath = ', $type = ' file ', $config = ', $timeout = ') {
? ? ? ? Pc_base::load_sys_class (' cache_factory ', ', 0);
? ? ? ? if ($config) {
? ? ? ? ? ? ? ? $cacheconfig = pc_base::load_config (' cache ');
? ? ? ? ? ? ? ? $cache = Cache_factory::get_instance ($cacheconfig)->get_cache ($config);
? ? ? ? } else {
? ? ? ? ? ? ? ? $cache = Cache_factory::get_instance ()->get_cache ($type);
? ? ? ? }
? ? ? ? Return $cache->set ($name, $data, $timeout, ", $filepath);
}
?
?
Transferred from: http://www.v63app.com/thread-2-1-1.html
?