Common functions extracted from phpcmsv9 *** return the strings or arrays processed by addslashes * @ param $ string or strings to be processed * @ returnmixed * functionnew_addslashes ($ string) {?? If (! Is_array ($ string) returnaddslashes ($ stri common functions extracted from phpcms v9
/**
* Returns a string or array processed by addslashes.
* @ Param $ string the 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 the string or array processed by stripslashes.
* @ Param $ string the 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 processed by addslashe.
* @ Param $ obj the 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;
}
/**
* Security filter functions
*
* @ Param $ string
* @ Return string
*/
Function safe_replace ($ string ){
? ? $ String = str_replace ('% 20', '', $ string );
? ? $ String = str_replace ('% 27', '', $ string );
? ? $ String = str_replace ('% 100', '', $ 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 control characters from 0 to 28
* @ 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 domain content
*
* @ Param $ string text field content
* @ Return string
*/
Function trim_textarea ($ string ){
? ? $ String = nl2br (str_replace ('','', $ string ));
? ? Return $ string;
}
/**
* Convert the text format into strings suitable for js output
* @ Param string $ string the string to be processed
* @ Param intval $ whether isjs executes string formatting. the default value is execution.
* @ Return string the processed string
*/
Function format_js ($ string, $ isjs = 1)
{
? ? $ String = addslashes (str_replace (array ("\ r", "\ n"), array ('',''), $ string ));
? ? Return $ isjs? 'Document. write ("'. $ string.'"); ': $ string;
}
/**
* Escape javascript code markup
*
* @ 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;
}
/**
* Obtain the complete URL of the current page
*/
Function get_url (){
? ? $ Sys_protocal = isset ($ _ SERVER ['server _ port']) & $ _ SERVER ['server _ port'] = '000000 '? '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;
}
/**
* UTF-8/GBK character truncation is supported.
* @ Param $ string
* @ Param $ length
* @ Param $ dot
*/
Function str_cut ($ string, $ length, $ dot = '...'){
? ? $ Strlen = strlen ($ string );
? ? If ($ strlen <= $ length) return $ string;
? ? $ String = str_replace (array ('','', '&', '"', ''', '-', '<', '> ','·','... '), Array ('region', '',' & ','" ', "'", ''', ','-',' <', '> ','·','... '), $ 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 | (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 );
? ?? ??? $ Strcut = str_replace (array ('hour', '', '&', '"', "'",'-', '<', '> ','·','... '), Array ('','',' & ',' "', ''', ''','-',' <', '> ','·','... '), $ Strcut );
? ? } Else {
? ?? ??? $ Dotlen = strlen ($ dot );
? ?? ??? $ Maxi = $ length-$ dotlen-1;
? ?? ??? $ Current_str = '';
? ?? ??? $ Search_arr = array ('&', '', '"', "'",'-',' <','> ', '·','... ', 'Signature ');
? ?? ??? $ 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;
}
/**
* Obtain the request ip address
*
* @ 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 strings
*
* @ Param? ? Int? ?? ??? $ Length ?? Output length
* @ Param? ? String? ??? $ Chars? ? Optional. the default value is 0123456789.
* @ Return? ? String? ??? String
*/
Function random ($ length, $ chars = '20140901 '){
? ? $ Hash = '';
? ? $ Max = strlen ($ chars)-1;
? ? For ($ I = 0; $ I <$ length; $ I ++ ){
? ?? ??? $ Hash. = $ chars [mt_rand (0, $ max)];
? ? }
? ? Return $ hash;
}
/**
* Convert a string to an array
*
* @ Param? ? String? ? $ Data? ? String
* @ Return? ? Array? ? Returns the array format. if data is null, an empty array is returned.
*/
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 the value is 0, new_stripslashes is not used for processing. optional. the default value is 1.
* @ Return? ? String? ? Returns a string. if data is null, null is returned.
*/
Function array2string ($ data, $ isformdata = 1 ){
? ? If ($ data = '') return '';
? ? If ($ isformdata) $ data = new_stripslashes ($ data );
? ? Return addslashes (var_export ($ data, TRUE ));
}
/**
* The number of converted bytes is in 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 and decryption functions
*
*
* @ Param? ? ? ? String? ? ? ? $ Txt? ? ? ? ? ? ? ? String
* @ Param? ? ? ? String? ? ? ? $ Operation? ? ? ? ENCODE is encrypted, DECODE is decrypted, and an optional parameter. the default value is ENCODE,
* @ Param? ? ? ? String? ? ? ? $ Key? ? ? ? ? ? ? ? Key: number, letter, underline
* @ Return? ? ? ? String
*/
Function sys_auth ($ txt, $ operation = 'encoding', $ key = ''){
? ? ? ? $ Key? ? ? ? = $ Key? $ Key: pc_base: load_config ('system', 'auth _ key ');
? ? ? ? $ Txt? ? ? ? = $ Operation = 'encoding '? (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? ? ? ? Identifier
* @ Param? ? ? ? Array? ? ? ? ? ? ? ? $ Pars? ? ? ? Escape array, two-dimensional array, 'key1' => 'value1', 'key2' => 'value2 ',
* @ Param? ? ? ? String? ? ? ? ? ? ? ? $ Modules multiple modules are separated by commas (,), such as member and 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. 'ages '. DIRECTORY_SEPARATOR. $ lang. DIRECTORY_SEPARATOR. 'system. lang. php ';
? ? ? ? ? ? ? ? If (defined ('in _ admin') require_once PC_PATH. 'ages '. DIRECTORY_SEPARATOR. $ lang. DIRECTORY_SEPARATOR. 'system _ menu. lang. php ';
? ? ? ? ? ? ? ? If (file_exists (PC_PATH. 'ages '. DIRECTORY_SEPARATOR. $ lang. DIRECTORY_SEPARATOR.ROUTE_M. '. lang. php ') require PC_PATH. 'ages '. 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. 'ages '. 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 call
*
* @ 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 ';
? ? ? ? Comment ')){
? ? ? ? ? ? ? ? If (! File_exists ($ compiledtplfile) | ists ')> @ 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) | compre') & gt; filemtime ($ compiledtplfile ))){
? ? ? ? ? ? ? ? ? ? ? ? $ Template_cache-> template_compile ($ module, $ template, 'default ');
? ? ? ? ? ? ? ? } Elseif (! Comment ')){
? ? ? ? ? ? ? ? ? ? ? ? Showmessage ('Template does not exist ');
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? Return $ compiledtplfile;
}
/**
* Custom output error
*
* @ Param $ errno error code
* @ Param $ errstr error description
* @ Param $ errfile error file address
* @ Param $ errline error row number
* @ Return string error message
*/
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. If an array is input for the jump address, multiple addresses are displayed on the page for the user to choose from. the default jump address is the first value of the array, and the time is 5 seconds.
* Showmessage ('logon successfully', array ('default jump address' => 'http: // www.phpcms.cn '));
* @ Param string $ msg prompt 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 $ the character to be searched by needle
* @ Return bool
*/
Function str_exists ($ haystack, $ needle)
{
? ? ? ? Return! (Strpos ($ haystack, $ needle) === FALSE );
}
/**
* Get file extension
*
* @ Param $ filename file name
* @ Return extension
*/
Function fileext ($ filename ){
? ? ? ? Return strtolower (trim (substr (strrchr ($ filename, '.'), 1, 10 )));
}
/**
* Load template label 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. the default value is file cache. cache configuration is not loaded.
* @ 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 );
}
?
?