PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/core/function/global.func.php

https://github.com/alin40404/FanweShare
PHP | 3171 lines | 2693 code | 180 blank | 298 comment | 199 complexity | 43821957b1725b58aeae0ee93314fec8 MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 方维购物分享网站系统 (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. /**
  8. * global.func
  9. *
  10. * 公共函数
  11. *
  12. * @package function
  13. * @author awfigq <awfigq@qq.com>
  14. */
  15. function getPhpSelf()
  16. {
  17. $php_self = '';
  18. $script_name = basename($_SERVER['SCRIPT_FILENAME']);
  19. if(basename($_SERVER['SCRIPT_NAME']) === $script_name)
  20. $php_self = $_SERVER['SCRIPT_NAME'];
  21. else if(basename($_SERVER['PHP_SELF']) === $script_name)
  22. $php_self = $_SERVER['PHP_SELF'];
  23. else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $script_name)
  24. $php_self = $_SERVER['ORIG_SCRIPT_NAME'];
  25. else if(($pos = strpos($_SERVER['PHP_SELF'],'/'.$script_name)) !== false)
  26. $php_self = substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$script_name;
  27. else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT']) === 0)
  28. $php_self = str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
  29. else
  30. return false;
  31. return $php_self;
  32. }
  33. /**
  34. * 获取引用文件路径
  35. * @param string $file_name 文件名称
  36. * @param string $folder 所在目录(默认为空)
  37. * @return string
  38. */
  39. function fimport($file_name, $folder = '')
  40. {
  41. global $_FANWE;
  42. static $sufix = array(
  43. 'module'=>'.module',
  44. 'service'=>'.service',
  45. 'class'=>'.class',
  46. 'function' => '.func',
  47. 'include' => '.inc',
  48. 'language' => '.lang',
  49. 'cache' => '.cache',
  50. 'dynamic'=>'.dynamic',
  51. );
  52. $file_name = strtolower($file_name);
  53. $file_path = FANWE_ROOT.'./core';
  54. if(strstr($file_name, '/'))
  55. {
  56. list($pre, $name) = explode('/', $file_name);
  57. $insert = '';
  58. if($pre == 'language')
  59. $insert = $_FANWE['config']['default_lang'].'/';
  60. return "{$file_path}/{$pre}/".$insert.(empty($folder) ? "" : $folder . "/")."{$name}".$sufix[$pre].".php";
  61. }
  62. else
  63. {
  64. return "{$file_path}/".(empty($folder) ? "" : $folder . "/")."{$file_name}.php";
  65. }
  66. }
  67. /**
  68. * 获取页面显示操作类
  69. * @param string $module 类名
  70. * @return object
  71. */
  72. function FM($module)
  73. {
  74. static $modules = array();
  75. if($modules[$module] === NULL)
  76. {
  77. require fimport("module/".strtolower($module));
  78. $m = ucfirst($module)."Module";
  79. $modules[$module] = new $m();
  80. unset($m);
  81. }
  82. return $modules[$module];
  83. }
  84. /**
  85. * 获取服务类
  86. * @param string $service 类名
  87. * @return object
  88. */
  89. function FS($service)
  90. {
  91. static $services = array();
  92. if($services[$service] === NULL)
  93. {
  94. require_once fimport("service/".strtolower($service));
  95. $s = ucfirst($service)."Service";
  96. $services[$service] = new $s();
  97. unset($s);
  98. }
  99. return $services[$service];
  100. }
  101. /**
  102. * 页面路径处理
  103. * @param string $type 页面
  104. * @param array $args 参数
  105. * @return string
  106. */
  107. function FU($type,$args,$is_full = false)
  108. {
  109. global $_FANWE;
  110. static $is_rewrite = NULL,$site_url = NULL,$url_lists = array(),$url_flists = array();
  111. if ($is_rewrite === NULL)
  112. $is_rewrite = intval($_FANWE['setting']['url_model']);
  113. if ($site_url === NULL)
  114. $site_url = $_FANWE['site_root'];
  115. $depr = '/';
  116. $url = $site_url;
  117. if($is_full)
  118. {
  119. $url = $_FANWE['site_url'];
  120. $site_url = $_FANWE['site_url'];
  121. }
  122. $apps = explode('/',$type);
  123. $module = $apps[0];
  124. $action = isset($apps[1]) ? $apps[1] : 'index';
  125. $type = $module.'/'.$action;
  126. $url_key = $type.'_'.md5(http_build_query($args));
  127. if($is_full)
  128. {
  129. if(isset($url_flists[$url_key]))
  130. return $url_flists[$url_key];
  131. }
  132. else
  133. {
  134. if(isset($url_lists[$url_key]))
  135. return $url_lists[$url_key];
  136. }
  137. $query = '';
  138. if($is_rewrite == 0)
  139. {
  140. $query = http_build_query($args);
  141. if(!empty($query))
  142. $query = '&'.$query;
  143. }
  144. if($is_rewrite == 0)
  145. {
  146. $url .= $module.'.php?action='.$action.$query;
  147. }
  148. else
  149. {
  150. $params = array();
  151. switch($type)
  152. {
  153. case 'club/index':
  154. case 'ask/index':
  155. case 'event/index':
  156. case 'invite/index':
  157. $search = array('/index');
  158. $replace = array('');
  159. break;
  160. case 'link/index':
  161. $module = "links";
  162. $search = array('/index');
  163. $replace = array('');
  164. break;
  165. case 'daren/index':
  166. $params = array('page' => 0);
  167. $search = array('/index','page/');
  168. $replace = array('','');
  169. break;
  170. case 'daren/all':
  171. $params = array('page' => 0);
  172. $search = array('page/');
  173. $replace = array('');
  174. break;
  175. case 'club/forum':
  176. $params = array('fid' => 0,'sort' => '','page' => 0);
  177. $search = array('forum/','fid/','sort/','page/');
  178. $replace = array('','','','');
  179. break;
  180. case 'club/best':
  181. $params = array('fid' => 0,'sort' => '','page' => 0);
  182. $search = array('fid/','sort/','page/');
  183. $replace = array('','','');
  184. break;
  185. case "club/newtopic":
  186. $search = array('fid/');
  187. $replace = array('');
  188. break;
  189. case "club/detail":
  190. $params = array('tid' => 0,'page' => 0);
  191. $search = array('tid/','page/');
  192. $replace = array('','');
  193. break;
  194. case 'ask/forum':
  195. $params = array('aid' => 0,'type' => '','page' => 0);
  196. $search = array('forum/','aid/','type/','page/');
  197. $replace = array('','','','');
  198. break;
  199. case "ask/newtopic":
  200. $search = array('aid/');
  201. $replace = array('');
  202. break;
  203. case "ask/detail":
  204. $params = array('tid' => 0,'page' => 0);
  205. $search = array('tid/','page/');
  206. $replace = array('','');
  207. break;
  208. case 'book/cate':
  209. case 'book/shopping':
  210. case 'book/search':
  211. $params = array('cate' => '','sid' => 0,'sort'=>'','tag' => '','page' => 0);
  212. $search = array('cate/','sid/','sort/','tag/','page/');
  213. $replace = array('','s','','','');
  214. $url = str_replace($search,$replace,$url);
  215. break;
  216. case 'book/dapei':
  217. case 'book/look':
  218. $params = array('sid' => 0,'sort'=>'','tag' => '','page' => 0);
  219. $search = array('sid/','sort/','tag/','page/');
  220. $replace = array('s','','','');
  221. $url = str_replace($search,$replace,$url);
  222. break;
  223. case 'style/index':
  224. $params = array('sort'=>'','tag' => '','page' => 0);
  225. $search = array('/index','sort/','tag/','page/');
  226. $replace = array('','','','');
  227. $url = str_replace($search,$replace,$url);
  228. break;
  229. case 'event/detail':
  230. $params = array('detail'=>'','id'=>'','page' => 0);
  231. $search = array('/detail','/id','/page');
  232. $replace = array('','','');
  233. $url = str_replace($search,$replace,$url);
  234. break;
  235. case 'event/list':
  236. $params = array('type'=>'','order'=>'','page' => 0);
  237. $search = array('/type','/order','/page');
  238. $replace = array('','','');
  239. $url = str_replace($search,$replace,$url);
  240. break;
  241. case 'adv/show':
  242. $params = array('id'=>'');
  243. $search = array('/show','id/');
  244. $replace = array('','');
  245. $url = str_replace($search,$replace,$url);
  246. break;
  247. case 'second/index':
  248. $params = array('sid'=>0,'cid' => 0,'page' => 0);
  249. $search = array('/index','sid/','cid/','page/');
  250. $replace = array('','s','c','');
  251. $url = str_replace($search,$replace,$url);
  252. break;
  253. case 'album/index':
  254. $params = array('sort'=>'','page' => 0);
  255. $search = array('/index','sort/','page/');
  256. $replace = array('','','');
  257. $url = str_replace($search,$replace,$url);
  258. break;
  259. case 'album/category':
  260. $params = array('id'=>0,'sort'=>'','page' => 0);
  261. $search = array('id/','sort/','page/');
  262. $replace = array('c','','');
  263. $url = str_replace($search,$replace,$url);
  264. break;
  265. case 'album/show':
  266. $args['aid'] = $args['id'];
  267. unset($args['id']);
  268. $params = array('aid'=>0,'sid'=>0,'type' => 0,'page' => 0);
  269. $search = array('aid/','sid/','type/','page/');
  270. $replace = array('a','s','t','');
  271. $url = str_replace($search,$replace,$url);
  272. break;
  273. case 'album/edit':
  274. $params = array('id'=>0);
  275. $search = array('id/');
  276. $replace = array('');
  277. $url = str_replace($search,$replace,$url);
  278. break;
  279. case 'shop/index':
  280. $params = array('cid' => 0,'page' => 0);
  281. $search = array('/index','cid/','page/');
  282. $replace = array('','c','');
  283. $url = str_replace($search,$replace,$url);
  284. break;
  285. case 'shop/show':
  286. $params = array('id' => 0,'page' => 0);
  287. $search = array('id/','page/');
  288. $replace = array('s','');
  289. $url = str_replace($search,$replace,$url);
  290. break;
  291. case 'exchange/index':
  292. $params = array('page' => 0);
  293. $search = array('/index','page/');
  294. $replace = array('','');
  295. $url = str_replace($search,$replace,$url);
  296. break;
  297. }
  298. if(!empty($params))
  299. $args = array_merge($params, $args);
  300. foreach($args as $var=>$val)
  301. {
  302. if($var == 'page' && $val == '0')
  303. $val = '';
  304. if($val != '')
  305. $query .= $depr.$var.$depr.$val;
  306. }
  307. $url .= $module.$depr.$action.$query;
  308. if(!empty($search))
  309. $url = str_replace($search,$replace,$url);
  310. switch($module)
  311. {
  312. case 'index':
  313. $url = $site_url;
  314. break;
  315. case 'u':
  316. if(!isset($args['uid']))
  317. $args['uid'] = $_FANWE['uid'];
  318. if($action == 'all')
  319. $url = $site_url.$module.$depr.$action;
  320. elseif($action == 'msgview')
  321. {
  322. $url = str_replace('/page/','/',$url);
  323. }
  324. else
  325. {
  326. if($action == 'index')
  327. {
  328. if($args['uid'] == $_FANWE['uid'])
  329. $url = $site_url.'me';
  330. else
  331. $url = $site_url.$module.$depr.$args['uid'];
  332. }
  333. else
  334. $url = $site_url.$module.$depr.$args['uid'].$depr.$action;
  335. if($action == 'album')
  336. {
  337. if(isset($args['type']) && $args['type'] != '')
  338. {
  339. $url.= $depr.'t'.$args['type'];
  340. }
  341. }
  342. elseif($action == 'exchange')
  343. {
  344. if(isset($args['status']) && $args['status'] != '')
  345. $url.= $depr.'s'.$args['status'];
  346. }
  347. else
  348. {
  349. if(isset($args['type']) && $args['type'] != '')
  350. $url.= $depr.$args['type'];
  351. if(isset($args['sort']) && $args['sort'] != '')
  352. $url.= $depr.$args['sort'];
  353. }
  354. if(isset($args['page']) && $args['page'] != '0')
  355. $url.= $depr.$args['page'];
  356. }
  357. break;
  358. case 'note':
  359. if($action == 'index')
  360. $url = $site_url.$module.$depr.$args['sid'];
  361. else
  362. $url = $site_url.$module.$depr.$args['sid'].$depr.$action.$depr.$args['id'];
  363. if(isset($args['page']) && $args['page'] != '0')
  364. $url.= $depr.$args['page'];
  365. break;
  366. }
  367. }
  368. if($type == 'tgo/index')
  369. $url = $site_url.'tgo.php?url='.base64_encode($args['url']);
  370. if($is_full)
  371. $url_flists[$url_key] = $url;
  372. else
  373. $url_lists[$url_key] = $url;
  374. return $url;
  375. }
  376. /**
  377. * 页面重写参数处理
  378. * @param array $keys 键值对
  379. * @return void
  380. */
  381. function getRewriteArgs($keys)
  382. {
  383. global $_FANWE;
  384. $args = trim($_FANWE['request']['args']);
  385. foreach($keys as $key)
  386. {
  387. preg_match("/$key-(.+?)(?:$|-)/is",$args,$value);
  388. if(count($value) > 1)
  389. {
  390. $_FANWE['request'][$key] = $value[1];
  391. if($key == 'page')
  392. $_FANWE['page'] = $value[1];
  393. }
  394. }
  395. unset($_FANWE['request']['args']);
  396. }
  397. /**
  398. * 分页处理
  399. * @param string $type 所在页面
  400. * @param array $args 参数
  401. * @param int $total_count 总数
  402. * @param int $page 当前页
  403. * @param int $page_size 分页大小
  404. * @param string $url 自定义路径
  405. * @param int $offset 偏移量
  406. * @return array
  407. */
  408. function buildPage($type,$args,$total_count,$page = 1,$page_size = 0,$url='',$offset = 5)
  409. {
  410. global $_FANWE;
  411. $pager['total_count'] = intval($total_count);
  412. $pager['page'] = $page;
  413. $pager['page_size'] = ($page_size == 0) ? ($_FANWE['setting']['page_listrows'] > 0 ? $_FANWE['setting']['page_listrows'] : 20) : $page_size;
  414. /* page 总数 */
  415. $pager['page_count'] = ($pager['total_count'] > 0) ? ceil($pager['total_count'] / $pager['page_size']) : 1;
  416. /* 边界处理 */
  417. if ($pager['page'] > $pager['page_count'])
  418. $pager['page'] = $pager['page_count'];
  419. $pager['limit'] = ($pager['page'] - 1) * $pager['page_size'] . "," . $pager['page_size'];
  420. $page_prev = ($pager['page'] > 1) ? $pager['page'] - 1 : 1;
  421. $page_next = ($pager['page'] < $pager['page_count']) ? $pager['page'] + 1 : $pager['page_count'];
  422. $pager['prev_page'] = $page_prev;
  423. $pager['next_page'] = $page_next;
  424. if (!empty($url))
  425. {
  426. $pager['page_first'] = $url . 1;
  427. $pager['page_prev'] = $url . $page_prev;
  428. $pager['page_next'] = $url . $page_next;
  429. $pager['page_last'] = $url . $pager['page_count'];
  430. }
  431. else
  432. {
  433. $args['page'] = '_page_';
  434. if(!empty($type))
  435. $page_url = FU($type,$args);
  436. else
  437. $page_url = 'javascript:;';
  438. $pager['page_first'] = str_replace('_page_',1,$page_url);
  439. $pager['page_prev'] = str_replace('_page_',$page_prev,$page_url);
  440. $pager['page_next'] = str_replace('_page_',$page_next,$page_url);
  441. $pager['page_last'] = str_replace('_page_',$pager['page_count'],$page_url);
  442. }
  443. $pager['page_nums'] = array();
  444. if($pager['page_count'] <= $offset * 2)
  445. {
  446. for ($i=1; $i <= $pager['page_count']; $i++)
  447. {
  448. $pager['page_nums'][] = array('name' => $i,'url' => empty($url) ? str_replace('_page_',$i,$page_url) : $url . $i);
  449. }
  450. }
  451. else
  452. {
  453. if($pager['page'] - $offset < 2)
  454. {
  455. $temp = $offset * 2;
  456. for ($i=1; $i<=$temp; $i++)
  457. {
  458. $pager['page_nums'][] = array('name' => $i,'url' => empty($url) ? str_replace('_page_',$i,$page_url) : $url . $i);
  459. }
  460. $pager['page_nums'][] = array('name'=>'...');
  461. $pager['page_nums'][] = array('name' => $pager['page_count'],'url' => empty($url) ? str_replace('_page_',$pager['page_count'],$page_url) : $url . $pager['page_count']);
  462. }
  463. else
  464. {
  465. $pager['page_nums'][] = array('name' => 1,'url' => empty($url) ? str_replace('_page_',1,$page_url) : $url . 1);
  466. $pager['page_nums'][] = array('name'=>'...');
  467. $start = $pager['page'] - $offset + 1;
  468. $end = $pager['page'] + $offset - 1;
  469. if($pager['page_count'] - $end > 1)
  470. {
  471. for ($i=$start;$i<=$end;$i++)
  472. {
  473. $pager['page_nums'][] = array('name' => $i,'url' => empty($url) ? str_replace('_page_',$i,$page_url) : $url . $i);
  474. }
  475. $pager['page_nums'][] = array('name'=>'...');
  476. $pager['page_nums'][] = array('name' => $pager['page_count'],'url' => empty($url) ? str_replace('_page_',$pager['page_count'],$page_url) : $url . $pager['page_count']);
  477. }
  478. else
  479. {
  480. $start = $pager['page_count'] - $offset * 2 + 1;
  481. $end = $pager['page_count'];
  482. for ($i=$start;$i<=$end;$i++)
  483. {
  484. $pager['page_nums'][] = array('name' => $i,'url' => empty($url) ? str_replace('_page_',$i,$page_url) : $url . $i);
  485. }
  486. }
  487. }
  488. }
  489. return $pager;
  490. }
  491. /**
  492. * 分页处理
  493. * @param int $total_count 总数
  494. * @param int $page 当前页
  495. * @param int $page_size 分页大小
  496. * @return array
  497. */
  498. function buildPageMini($total_count,$page = 1,$page_size = 0)
  499. {
  500. $pager['total_count'] = intval($total_count);
  501. $pager['page'] = $page;
  502. $pager['page_size'] = ($page_size == 0) ? ($_FANWE['setting']['page_listrows'] > 0 ? $_FANWE['setting']['page_listrows'] : 20) : $page_size;
  503. /* page 总数 */
  504. $pager['page_count'] = ($pager['total_count'] > 0) ? ceil($pager['total_count'] / $pager['page_size']) : 1;
  505. /* 边界处理 */
  506. if ($pager['page'] > $pager['page_count'])
  507. $pager['page'] = $pager['page_count'];
  508. $pager['limit'] = ($pager['page'] - 1) * $pager['page_size'] . "," . $pager['page_size'];
  509. $page_prev = ($pager['page'] > 1) ? $pager['page'] - 1 : 1;
  510. $page_next = ($pager['page'] < $pager['page_count']) ? $pager['page'] + 1 : $pager['page_count'];
  511. $pager['prev_page'] = $page_prev;
  512. $pager['next_page'] = $page_next;
  513. return $pager;
  514. }
  515. /**
  516. * 用于检测当前用户IP的可操作性,time_span为验证的时间间隔 秒
  517. *
  518. * @param string $ip_str IP地址
  519. * @param string $module 操作的模块 *
  520. * @param integer $time_span 间隔
  521. * @param integer $id 操作的数据
  522. *
  523. * @return boolean
  524. */
  525. function checkIpOperation($module,$time_span = 0,$id = 0)
  526. {
  527. global $_FANWE;
  528. @session_start();
  529. $key = $_FANWE['client_ip'].'_'.$_FANWE['uid'].'_check_'.$module.($id > 0 ? '_'.$id : '');
  530. if(!isset($_SESSION[$key]))
  531. {
  532. $_SESSION[$key] = TIME_UTC;
  533. return true;
  534. }
  535. else
  536. {
  537. $time = (int)$_SESSION[$key];
  538. if(TIME_UTC - $time < $time_span)
  539. {
  540. return false;
  541. }
  542. else
  543. {
  544. $_SESSION[$key] = TIME_UTC;
  545. return true;
  546. }
  547. }
  548. }
  549. /**
  550. * 字符串截断处理
  551. * @param string $string 要处理的字符串
  552. * @param int $length 指定长度
  553. * @param string $dot 超出指定长度时显示
  554. * @return array
  555. */
  556. function cutStr($string, $length, $dot = '...')
  557. {
  558. if(getStrLen($string) <= $length)
  559. return $string;
  560. $pre = '{%';
  561. $end = '%}';
  562. $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);
  563. $strcut = '';
  564. if(strtolower(CHARSET) == 'utf-8')
  565. {
  566. $n = $tn = $noc = 0;
  567. while($n < strlen($string))
  568. {
  569. $t = ord($string[$n]);
  570. if($t == 9 || $t == 10 || (32 <= $t && $t <= 126))
  571. {
  572. $tn = 1; $n++; $noc++;
  573. }
  574. elseif(194 <= $t && $t <= 223)
  575. {
  576. $tn = 2; $n += 2; $noc += 2;
  577. }
  578. elseif(224 <= $t && $t <= 239)
  579. {
  580. $tn = 3; $n += 3; $noc += 2;
  581. }
  582. elseif(240 <= $t && $t <= 247)
  583. {
  584. $tn = 4; $n += 4; $noc += 2;
  585. }
  586. elseif(248 <= $t && $t <= 251)
  587. {
  588. $tn = 5; $n += 5; $noc += 2;
  589. }
  590. elseif($t == 252 || $t == 253)
  591. {
  592. $tn = 6; $n += 6; $noc += 2;
  593. }
  594. else
  595. {
  596. $n++;
  597. }
  598. if($noc >= $length)
  599. break;
  600. }
  601. if($noc > $length)
  602. $n -= $tn;
  603. $strcut = substr($string,0,$n);
  604. }
  605. else
  606. {
  607. for($i = 0; $i < $length; $i++)
  608. {
  609. $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
  610. }
  611. }
  612. $strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);
  613. return $strcut.$dot;
  614. }
  615. /**
  616. * 获取字符串长度
  617. * @param string $str 要获取长度的字符串
  618. * @return int
  619. */
  620. function getStrLen($str)
  621. {
  622. $length = strlen(preg_replace('/[\x00-\x7F]/', '', $str));
  623. if ($length)
  624. {
  625. return strlen($str) - $length + intval($length / 3) * 2;
  626. }
  627. else
  628. {
  629. return strlen($str);
  630. }
  631. }
  632. /**
  633. * 获取字节数
  634. * @param string $val 要获取字节数的字符串
  635. * @return int
  636. */
  637. function getBytes($val)
  638. {
  639. $val = trim($val);
  640. $last = strtolower($val{strlen($val)-1});
  641. switch($last)
  642. {
  643. case 'g': $val *= 1024;
  644. case 'm': $val *= 1024;
  645. case 'k': $val *= 1024;
  646. }
  647. return $val;
  648. }
  649. /**
  650. * 错误处理
  651. * @param string $message 错误信息
  652. * @param bool $show 是否显示
  653. * @param bool $save 是否保存
  654. * @param bool $halt 是否停止
  655. * @return void
  656. */
  657. function systemError($message, $show = true, $save = true, $halt = true)
  658. {
  659. require_once fimport('class/error');
  660. FanweError::systemError($message, $show, $save, $halt);
  661. }
  662. /**
  663. * 显示成功信息
  664. * @param string $title 标题
  665. * @param string $message 成功信息
  666. * @param string $jump_url 跳转地址
  667. * @param int $wait 等待时间
  668. * @return void
  669. */
  670. function showSuccess($title, $message,$jump_url,$wait = 3)
  671. {
  672. global $_FANWE;
  673. include template('page/success');
  674. display();
  675. exit;
  676. }
  677. /**
  678. * 显示错误信息
  679. * @param string $title 标题
  680. * @param string $message 错误信息
  681. * @param string $jump_url 跳转地址
  682. * @param int $wait 等待时间
  683. * @param bool $is_close 是否显示网站关闭
  684. * @return void
  685. */
  686. function showError($title, $message,$jump_url,$wait = 3,$is_close = false)
  687. {
  688. global $_FANWE;
  689. if($is_close)
  690. include template('page/close');
  691. else
  692. include template('page/error');
  693. display();
  694. exit;
  695. }
  696. /**
  697. * 查询字符串是否存在
  698. * @param string $string
  699. * @param string $find
  700. * @return bool
  701. */
  702. function strExists($string, $find)
  703. {
  704. return !(strpos($string, $find) === FALSE);
  705. }
  706. /**
  707. * 获取是否为搜索引擎爬虫
  708. * @param string $userAgent 用户信息
  709. * @return bool
  710. */
  711. function checkRobot($userAgent = '')
  712. {
  713. static $kwSpiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
  714. static $kwBrowsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
  715. $userAgent = empty($userAgent) ? $_SERVER['HTTP_USER_AGENT'] : $userAgent;
  716. if(!strExists($userAgent, 'http://') && preg_match("/($kwBrowsers)/i", $userAgent))
  717. return false;
  718. elseif(preg_match("/($kwSpiders)/i", $userAgent))
  719. return true;
  720. else
  721. return false;
  722. }
  723. /**
  724. * 获取链接格式是否正确
  725. * @param string $url 链接
  726. * @return bool
  727. */
  728. function parseUrl($url)
  729. {
  730. $parse_url = parse_url($url);
  731. return (!empty($parse_url['scheme']) && !empty($parse_url['host']));
  732. }
  733. /**
  734. * 获取客户端IP
  735. * @return string
  736. */
  737. function getFClientIp()
  738. {
  739. $ip = $_SERVER['REMOTE_ADDR'];
  740. if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP']))
  741. {
  742. $ip = $_SERVER['HTTP_CLIENT_IP'];
  743. }
  744. elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches))
  745. {
  746. foreach ($matches[0] AS $xip)
  747. {
  748. if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip))
  749. {
  750. $ip = $xip;
  751. break;
  752. }
  753. }
  754. }
  755. return $ip;
  756. }
  757. /**
  758. * 字符转义
  759. * @return string
  760. */
  761. function fAddslashes($string)
  762. {
  763. if(is_array($string))
  764. {
  765. foreach($string as $key => $val)
  766. {
  767. unset($string[$key]);
  768. $string[addslashes($key)] = fAddslashes($val);
  769. }
  770. }
  771. else
  772. {
  773. $string = addslashes($string);
  774. }
  775. return $string;
  776. }
  777. /**
  778. * 字符转义
  779. * @return string
  780. */
  781. function fStripslashes($string)
  782. {
  783. if(is_array($string))
  784. {
  785. foreach($string as $key => $val)
  786. {
  787. unset($string[$key]);
  788. $string[stripslashes($key)] = fStripslashes($val);
  789. }
  790. }
  791. else
  792. {
  793. $string = stripslashes($string);
  794. }
  795. return $string;
  796. }
  797. /**
  798. * 生成随机数
  799. * @param int $length 随机数长度
  800. * @param int $numeric 是否只生成数字
  801. * @return string
  802. */
  803. function random($length, $numeric = 0)
  804. {
  805. $seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  806. $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
  807. $hash = '';
  808. $max = strlen($seed) - 1;
  809. for($i = 0; $i < $length; $i++)
  810. {
  811. $hash .= $seed{mt_rand(0, $max)};
  812. }
  813. return $hash;
  814. }
  815. /**
  816. * 生成cookie
  817. * @param string $var 键名
  818. * @param string $value 值
  819. * @param int $life 过期时间
  820. * @param bool $prefix 是否加入前缘
  821. * @param bool $http_only
  822. * @return void
  823. */
  824. function fSetCookie($var, $value = '', $life = 0, $prefix = true, $http_only = false)
  825. {
  826. global $_FANWE;
  827. $config = $_FANWE['config']['cookie'];
  828. $_FANWE['cookie'][$var] = $value;
  829. $var = ($prefix ? $config['cookie_pre'] : '').$var;
  830. $_COOKIE[$var] = $value;
  831. if($value == '' || $life < 0)
  832. {
  833. $value = '';
  834. $life = -1;
  835. }
  836. $life = $life > 0 ? TIME_UTC + $life : ($life < 0 ? TIME_UTC - 31536000 : 0);
  837. $path = $http_only && PHP_VERSION < '5.2.0' ? $config['cookie_path'].'; HttpOnly' : $config['cookie_path'];
  838. $secure = $_SERVER['SERVER_PORT'] == 443 ? 1 : 0;
  839. if(PHP_VERSION < '5.2.0')
  840. {
  841. setcookie($var, $value, $life, $path, $config['cookie_domain'], $secure);
  842. }
  843. else
  844. {
  845. setcookie($var, $value, $life, $path, $config['cookie_domain'], $secure, $http_only);
  846. }
  847. }
  848. /**
  849. * 更新session
  850. * @param bool $force 强制更新
  851. * @return bool
  852. */
  853. function updateSession($force = false) {
  854. global $_FANWE;
  855. static $updated = false;
  856. if(!$updated)
  857. {
  858. $fanwe = & FanweService::instance();
  859. foreach($fanwe->session->var as $k => $v)
  860. {
  861. if(isset($_FANWE['user'][$k]) && $k != 'last_activity')
  862. $fanwe->session->set($k, $_FANWE['user'][$k]);
  863. }
  864. $fanwe->session->update();
  865. $updated = true;
  866. }
  867. return $updated;
  868. }
  869. /**
  870. * 获取cookie
  871. * @param string $key 键名
  872. * @return bool
  873. */
  874. function getCookie($key)
  875. {
  876. global $_FANWE;
  877. return isset($_FANWE['cookie'][$key]) ? $_FANWE['cookie'][$key] : '';
  878. }
  879. /**
  880. * 生成表单随机数
  881. * @param string $specialadd 增加文本
  882. * @return string
  883. */
  884. function formHash($specialadd = '')
  885. {
  886. global $_FANWE;
  887. return substr(md5(substr(TIME_UTC, 0, -7).$_FANWE['user_name'].$_FANWE['uid'].$_FANWE['authkey'].$specialadd), 8, 8);
  888. }
  889. /**
  890. * 安全代码处理
  891. * @param string $string 要处理的文本
  892. * @param string $operation 处理方式(DECODE:解码,ENCODE:编码)
  893. * @param string $key 密匙
  894. * @param int $expiry 过期时间
  895. * @return string
  896. */
  897. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
  898. {
  899. global $_FANWE;
  900. $ckey_length = 4;
  901. $key = md5($key != '' ? $key : $_FANWE['authkey']);
  902. $keya = md5(substr($key, 0, 16));
  903. $keyb = md5(substr($key, 16, 16));
  904. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  905. $cryptkey = $keya.md5($keya.$keyc);
  906. $key_length = strlen($cryptkey);
  907. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  908. $string_length = strlen($string);
  909. $result = '';
  910. $box = range(0, 255);
  911. $rndkey = array();
  912. for($i = 0; $i <= 255; $i++)
  913. {
  914. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  915. }
  916. for($j = $i = 0; $i < 256; $i++)
  917. {
  918. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  919. $tmp = $box[$i];
  920. $box[$i] = $box[$j];
  921. $box[$j] = $tmp;
  922. }
  923. for($a = $j = $i = 0; $i < $string_length; $i++)
  924. {
  925. $a = ($a + 1) % 256;
  926. $j = ($j + $box[$a]) % 256;
  927. $tmp = $box[$a];
  928. $box[$a] = $box[$j];
  929. $box[$j] = $tmp;
  930. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  931. }
  932. if($operation == 'DECODE')
  933. {
  934. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16))
  935. {
  936. return substr($result, 26);
  937. } else {
  938. return '';
  939. }
  940. }
  941. else
  942. {
  943. return $keyc.str_replace('=', '', base64_encode($result));
  944. }
  945. }
  946. /**
  947. * 获取语言文本
  948. * @param string $file 所在文件
  949. * @param string $var 键
  950. * @param string $default 默认值
  951. * @return mixed
  952. */
  953. function lang($file, $var = NULL, $default = NULL)
  954. {
  955. global $_FANWE;
  956. $key = $file."_lang";
  957. if(!isset($_FANWE['lang'][$key]))
  958. {
  959. include fimport("language/$file");
  960. $_FANWE['lang'][$key] = $lang;
  961. }
  962. $return = $var !== NULL ? (isset($_FANWE['lang'][$key][$var]) ? $_FANWE['lang'][$key][$var] : NULL) : $_FANWE['lang'][$key];
  963. $return = $return === NULL ? ($default !== NULL ? $default : $var) : $return;
  964. return $return;
  965. }
  966. /**
  967. * 获取IP列表中是否存在指定的IP
  968. * @param string $ip ip
  969. * @param string $access_list ip列表
  970. * @return bool
  971. */
  972. function ipAccess($ip, $access_list)
  973. {
  974. return preg_match("/^(".str_replace(array("\r\n", ' '), array('|', ''), preg_quote($access_list, '/')).")/", $ip);
  975. }
  976. /**
  977. * 获取IP是否充许访问
  978. * @param string $ip ip
  979. * @return bool
  980. */
  981. function ipBanned($ip)
  982. {
  983. global $_FANWE;
  984. if($_FANWE['setting']['ip_access'] && !ipAccess($ip, $_FANWE['setting']['ip_access']))
  985. {
  986. return true;
  987. }
  988. FanweService::instance()->cache->loadCache('ipbanned');
  989. if(empty($_FANWE['cache']['ipbanned']))
  990. return false;
  991. else
  992. {
  993. if($_FANWE['cache']['ipbanned']['expiration'] < TIME_UTC)
  994. {
  995. FanweService::instance()->cache->updateCache('ipbanned');
  996. }
  997. return preg_match("/^(".$_FANWE['cache']['ipbanned']['regexp'].")$/", $ip);
  998. }
  999. }
  1000. /**
  1001. * 获取模板cache文件路径
  1002. * @param string $file 模板文件
  1003. * @param array $args 参数
  1004. * @param int $is_dynamic 是否为动态缓存(动态缓存页面在清空缓存,不删除)
  1005. * @param string $dir 缓存目录
  1006. * @return string
  1007. */
  1008. function getTplCache($file, $args, $is_dynamic = 0,$dir = '')
  1009. {
  1010. global $_FANWE;
  1011. $tpl_dir = './tpl/'.$_FANWE['setting']['site_tmpl'];
  1012. $tpl_file = $tpl_dir.'/'.$file.'.htm';
  1013. if(!empty($dir))
  1014. $dir .= '/';
  1015. switch($is_dynamic)
  1016. {
  1017. case 1:
  1018. $dir = 'dynamic/'.$dir;
  1019. break;
  1020. case 2:
  1021. $dir = 'page/'.$dir;
  1022. break;
  1023. default:
  1024. $dir = 'static/'.$dir;
  1025. break;
  1026. }
  1027. $filename = md5($tpl_file.implode(',',$args));
  1028. return PUBLIC_ROOT.'./data/tpl/caches/'.$dir.str_replace('/', '_', $file).'/'.substr($filename,0,1)."/".substr($filename,1,1)."/".$filename.".htm";
  1029. }
  1030. /**
  1031. * 检测模板是否需要更新
  1032. * @param string $main_tpl
  1033. * @param string $sub_tpl
  1034. * @param int $time_compare
  1035. * @param string $cache_file
  1036. * @param string $tpl_dir
  1037. * @param string $file
  1038. * @return bool
  1039. */
  1040. function checkTplRefresh($main_tpl, $sub_tpl, $time_compare, $cache_file, $tpl_dir, $file)
  1041. {
  1042. global $_FANWE;
  1043. static $tpl_refresh = NULL;
  1044. if($tpl_refresh === NULL)
  1045. {
  1046. $tpl_refresh = $_FANWE['config']['output']['tpl_refresh'];
  1047. }
  1048. if(empty($time_compare) || $tpl_refresh == 1 || ($tpl_refresh > 1 && !(TIMESTAMP % $tpl_refresh)))
  1049. {
  1050. if(empty($time_compare) || @filemtime(FANWE_ROOT.$sub_tpl) > $time_compare)
  1051. {
  1052. require_once fimport('class/template');
  1053. $template = new Template();
  1054. $template->parseTemplate($main_tpl, $tpl_dir, $file, $cache_file);
  1055. return TRUE;
  1056. }
  1057. }
  1058. return FALSE;
  1059. }
  1060. /**
  1061. * 模板处理
  1062. * @param string $file
  1063. * @param string $tpl_dir
  1064. * @param bool $get_tpl_file
  1065. * @return string
  1066. */
  1067. function template($file, $tpl_dir = '', $get_tpl_file = 0)
  1068. {
  1069. global $_FANWE;
  1070. $tpl_dir = $tpl_dir ? $tpl_dir : './tpl/'.$_FANWE['setting']['site_tmpl'];
  1071. $tpl_file = $tpl_dir.'/'.$file.'.htm';
  1072. $cache_file = './data/tpl/compiled/'.str_replace('/', '_', $file).'.tpl.php';
  1073. if($get_tpl_file)
  1074. return $tpl_file;
  1075. checkTplRefresh($tpl_file, $tpl_file, @filemtime(PUBLIC_ROOT.$cache_file), $cache_file, $tpl_dir, $file);
  1076. return PUBLIC_ROOT.$cache_file;
  1077. }
  1078. /**
  1079. * 获取模板编译后内容
  1080. * @param string $file
  1081. * @param array $args
  1082. * @param string $tpl_dir
  1083. * @return string
  1084. */
  1085. function tplFetch($file,&$args = array(), $tpl_dir = '',$cache_file = '')
  1086. {
  1087. global $_FANWE;
  1088. if(!empty($args))
  1089. {
  1090. foreach($args as $key=>$val)
  1091. {
  1092. $$key = &$args[$key];
  1093. }
  1094. }
  1095. ob_start();
  1096. if(!empty($cache_file) && file_exists($cache_file))
  1097. include $cache_file;
  1098. else
  1099. include template($file,$tpl_dir);
  1100. $content = ob_get_contents();
  1101. ob_end_clean();
  1102. express($content);
  1103. if(!empty($cache_file) && !file_exists($cache_file) && diskfreespace(PUBLIC_ROOT.'./data/tpl/caches') > 1000000)
  1104. {
  1105. if(makeDir(preg_replace("/^(.*)\/.*?\.htm$/is", "\\1", $cache_file)))
  1106. writeFile($cache_file,$content);
  1107. }
  1108. require_once fimport('dynamic/common');
  1109. $module_dynamic = '';
  1110. if(defined('MODULE_NAME') && MODULE_NAME != '')
  1111. $module_dynamic = fimport('dynamic/'.MODULE_NAME);
  1112. if(!empty($module_dynamic) && file_exists($module_dynamic))
  1113. require_once $module_dynamic;
  1114. $content = preg_replace('/<!--dynamic\s+(.+?)(?:|\sargs=(.*?))-->/ies', "\\1('\\2');", $content);
  1115. //格式化会员关注
  1116. formatUserFollowTags($content);
  1117. contentParse($content);
  1118. return $content;
  1119. }
  1120. /**
  1121. * 处理模板字符串,并返回编译后内容
  1122. * @param string $string
  1123. * @param string $cache_key
  1124. * @param array $args
  1125. * @return string
  1126. */
  1127. function tplString($string,$cache_key,&$args = array())
  1128. {
  1129. global $_FANWE;
  1130. if(!empty($args))
  1131. {
  1132. foreach($args as $key=>$val)
  1133. {
  1134. $$key = &$args[$key];
  1135. }
  1136. }
  1137. if(empty($cache_key))
  1138. $cache_key = md5($string);
  1139. $cache_file = PUBLIC_ROOT.'./data/tpl/caches/'.$cache_key.'.htm';
  1140. if(!file_exists($cache_file))
  1141. {
  1142. if(makeDir(preg_replace("/^(.*)\/.*?\.htm$/is", "\\1", $cache_file)))
  1143. {
  1144. require_once fimport('class/template');
  1145. $template = new Template();
  1146. $string = $template->parseString($string);
  1147. writeFile($cache_file,$string);
  1148. }
  1149. }
  1150. ob_start();
  1151. include $cache_file;
  1152. $content = ob_get_contents();
  1153. ob_end_clean();
  1154. require_once fimport('dynamic/common');
  1155. $module_dynamic = '';
  1156. if(defined('MODULE_NAME') && MODULE_NAME != '')
  1157. $module_dynamic = fimport('dynamic/'.MODULE_NAME);
  1158. if(!empty($module_dynamic) && file_exists($module_dynamic))
  1159. require_once $module_dynamic;
  1160. $content = preg_replace('/<!--dynamic\s+(.+?)(?:|\sargs=(.*?))-->/ies', "\\1('\\2');", $content);
  1161. //格式化会员关注
  1162. express($content);
  1163. formatUserFollowTags($content);
  1164. contentParse($content);
  1165. return $content;
  1166. }
  1167. /**
  1168. * 显示页面
  1169. * @param string $cache_file 缓存路径
  1170. * @param bool $is_session 是否更新session
  1171. * @param bool $is_return 是否返回页面内容
  1172. * @return mixed
  1173. */
  1174. function display($cache_file,$is_session = true,$is_return = false)
  1175. {
  1176. global $_FANWE;
  1177. $content = NULL;
  1178. if(!empty($cache_file) && !file_exists($cache_file) && diskfreespace(PUBLIC_ROOT.'./data/tpl/caches') > 1000000)
  1179. {
  1180. if(makeDir(preg_replace("/^(.*)\/.*?\.htm$/is", "\\1", $cache_file)))
  1181. {
  1182. $css_script_php = '';
  1183. if(isset($_FANWE['page_parses']))
  1184. $css_script_php = "<?php\n".'$_FANWE[\'CACHE_CSS_SCRIPT_PHP\']'." = ".var_export($_FANWE['page_parses'], true).";\n?>";
  1185. $content = ob_get_contents();
  1186. express($content);
  1187. writeFile($cache_file,$css_script_php.$content);
  1188. }
  1189. }
  1190. require_once fimport('dynamic/common');
  1191. $module_dynamic = '';
  1192. if(defined('MODULE_NAME') && MODULE_NAME != '')
  1193. $module_dynamic = fimport('dynamic/'.MODULE_NAME);
  1194. if(!empty($module_dynamic) && file_exists($module_dynamic))
  1195. require_once $module_dynamic;
  1196. if($content === NULL)
  1197. {
  1198. $content = ob_get_contents();
  1199. express($content);
  1200. }
  1201. ob_end_clean();
  1202. $content = preg_replace('/<!--dynamic\s+(.+?)(?:|\sargs=(.*?))-->/ies', "\\1('\\2');", $content);
  1203. if(isset($_FANWE['CACHE_CSS_SCRIPT_PHP']))
  1204. {
  1205. if(isset($_FANWE['CACHE_CSS_SCRIPT_PHP']['css']))
  1206. {
  1207. foreach($_FANWE['CACHE_CSS_SCRIPT_PHP']['css'] as $url)
  1208. {
  1209. cssParse($url);
  1210. }
  1211. }
  1212. if(isset($_FANWE['CACHE_CSS_SCRIPT_PHP']['script']))
  1213. {
  1214. foreach($_FANWE['CACHE_CSS_SCRIPT_PHP']['script'] as $url)
  1215. {
  1216. scriptParse($url);
  1217. }
  1218. }
  1219. }
  1220. //格式化会员关注
  1221. formatUserFollowTags($content);
  1222. contentParse($content);
  1223. if($is_session)
  1224. updateSession();
  1225. if($is_return)
  1226. return $content;
  1227. $_FANWE['gzip_compress'] ? ob_start('ob_gzhandler') : ob_start();
  1228. echo $content;
  1229. }
  1230. function contentParse(&$content)
  1231. {
  1232. global $_FANWE;
  1233. $patterns = array (
  1234. "/\.\/public\/js\//i",
  1235. "/\.\/public\/upload\//i",
  1236. "/\.\/public\//i",
  1237. //"/\.\/tpl\/(.*?)\/css\//i",
  1238. //"/\.\/tpl\/(.*?)\/js\//i",
  1239. //"/\.\/tpl\/(.*?)\/images\//i",
  1240. "/\.\/tpl\/css\//i",
  1241. "/\.\/tpl\/images\//i",
  1242. "/\.\/tpl\/js\//i",
  1243. "/\.\/tpl\/(.*?)\//i",
  1244. );
  1245. $image_url = !empty($_FANWE['config']['cdn']['image']) ? $_FANWE['config']['cdn']['image'] : $_FANWE['site_root'];
  1246. $css_url = !empty($_FANWE['config']['cdn']['css']) ? $_FANWE['config']['cdn']['css'] : $_FANWE['site_root'];
  1247. $js_url = !empty($_FANWE['config']['cdn']['js']) ? $_FANWE['config']['cdn']['js'] : $_FANWE['site_root'];
  1248. $replace = array (
  1249. $js_url.'public/js/',
  1250. $image_url.'public/upload/',
  1251. $_FANWE['site_root'].'public/',
  1252. //$css_url.'tpl/\\1/css/',
  1253. //$js_url.'tpl/\\1/js/',
  1254. //$image_url.'tpl/\\1/images/',
  1255. $_FANWE['site_root'].'public/data/tpl/css/',
  1256. $_FANWE['site_root'].'tpl/'.TMPL.'/images/',
  1257. $_FANWE['site_root'].'tpl/'.TMPL.'/js/',
  1258. $_FANWE['site_root'].'tpl/\\1/',
  1259. );
  1260. if(FS('Image')->getIsServer())
  1261. {
  1262. if(!isset($_FANWE['cache']['image_servers']))
  1263. FanweService::instance()->cache->loadCache('image_servers');
  1264. foreach($_FANWE['cache']['image_servers']['all'] as $server)
  1265. {
  1266. $patterns[] = "/\.\/".$server['code']."\//i";
  1267. $replace[] = $server['url'];
  1268. }
  1269. }
  1270. $content = preg_replace($patterns,$replace, $content);
  1271. }
  1272. function clearExpress($content)
  1273. {
  1274. return preg_replace("/\[[^\]]+\]/i",'',$content);
  1275. }
  1276. function express(&$content)
  1277. {
  1278. global $_FANWE;
  1279. $express = getCache('emotion_express_cache'); //缓存过的表情hash
  1280. if(!$express)
  1281. {
  1282. $express_rs = FDB::fetchAll("select `emotion`,concat('./public/expression/',`type`,'/',`filename`) as fname from ".FDB::table('expression'));
  1283. foreach($express_rs as $k=>$row)
  1284. {
  1285. $express[0][] = $row['emotion'];
  1286. $express[1][] = "<img src='".$row['fname']."' title='".preg_replace("/[\[\]]/",'',$row['emotion'])."' />";
  1287. }
  1288. setCache('emotion_express_cache',$express);
  1289. }
  1290. preg_match("/(<html.*?>.*?<\/head>)/s",$content,$data);
  1291. $head_html = $data[1];
  1292. $content = str_replace($head_html,'<!--TAG_HEADER-->',$content);
  1293. preg_match_all("/(<textarea.*?>.*?<\/textarea>)/s",$content,$data);
  1294. $textareas = $data[1];
  1295. if(count($textareas) > 0)
  1296. {
  1297. foreach($textareas as $key => $textarea)
  1298. {
  1299. $content = str_replace($textarea,'<!--TAG_TEXTAREA_'.$key.'-->',$content);
  1300. }
  1301. }
  1302. preg_match_all("/@([^\f\n\r\t\v@<> ]{2,20}?)(?:\:| )/",$content,$users);
  1303. if(!empty($users[1]))
  1304. {
  1305. $patterns = array();
  1306. $replace = array();
  1307. $users = array_unique($users[1]);
  1308. $arr = array();
  1309. foreach($users as $user)
  1310. {
  1311. if(!empty($user))
  1312. {
  1313. $arr[] = addslashes($user);
  1314. }
  1315. }
  1316. $res = FDB::query('SELECT uid,user_name
  1317. FROM '.FDB::table('user').'
  1318. WHERE user_name '.FDB::createIN($arr));
  1319. while($data = FDB::fetch($res))
  1320. {
  1321. $patterns[] = '/@'.preg_quote($data['user_name']).'(\:| )/';
  1322. $replace[] = '<a class="u_name GUID" uid="'.$data['uid'].'" href="'.FU('u/index',array('uid'=>$data['uid'])).'">@'.$data['user_name']."</a>\$1";
  1323. }
  1324. $content = preg_replace($patterns,$replace,$content);
  1325. }
  1326. preg_match_all("/#([^\f\n\r\t\v]{1,80}?)#/",$content,$events);
  1327. if(!empty($events[1]))
  1328. {
  1329. $patterns = array();
  1330. $replace = array();
  1331. $events = array_unique($events[1]);
  1332. $arr = array();
  1333. foreach($events as $event)
  1334. {
  1335. if(!empty($event))
  1336. {
  1337. $arr[] = addslashes($event);
  1338. }
  1339. }
  1340. $res = FDB::query('SELECT id,title
  1341. FROM '.FDB::table('event').'
  1342. WHERE title '.FDB::createIN($arr));
  1343. while($data = FDB::fetch($res))
  1344. {
  1345. $patterns[] = '#'.$data['title'].'#';
  1346. $replace[] = '<a href="'.FU("event/detail",array("id"=>$data['id'])).'" target="_blank">#'.$data['title'].'#</a>';
  1347. }
  1348. $content = str_replace($patterns,$replace,$content);
  1349. }
  1350. if(count($_FANWE['tpl_user_formats']) > 0)
  1351. {
  1352. $patterns = array();
  1353. $replace = array();
  1354. $user_ids = array_keys($_FANWE['tpl_user_formats']);
  1355. $user_ids = implode(',',$user_ids);
  1356. $user_ids = str_replace(',,',',',$user_ids);
  1357. if(!empty($user_ids))
  1358. {
  1359. $res = FDB::query("SELECT uid,user_name,server_code,reg_time,credits,is_daren,is_buyer,follows,fans,collects,
  1360. favs,threads,photos,goods,ask,ask_posts,ask_best_posts,shares,forums,forum_posts,
  1361. seconds,albums,referrals FROM ".FDB::table('user').'
  1362. INNER JOIN '.FDB::table('user_count').' AS uc USING(uid)
  1363. WHERE uid IN ('.$user_ids.')');
  1364. while($user = FDB::fetch($res))
  1365. {
  1366. $uid = $user['uid'];
  1367. $user['url'] = FU('u/index',array('uid'=>$uid));
  1368. foreach($_FANWE['tpl_user_formats'][$uid] as $tuf_key => $tuf_val)
  1369. {
  1370. $patterns[] = "<!--USER_".$uid."_".$tuf_key."-->";
  1371. $replace[] = getUserFormatHtml($user,$tuf_val);
  1372. }
  1373. unset($_FANWE['tpl_user_formats'][$uid]);
  1374. }
  1375. $content = str_replace($patterns,$replace,$content);
  1376. }
  1377. }
  1378. $content = str_replace($express[0],$express[1],$content);
  1379. $content = str_replace('<!--TAG_HEADER-->',$head_html,$content);
  1380. if(count($textareas) > 0)
  1381. {
  1382. foreach($textareas as $key => $textarea)
  1383. {
  1384. $content = str_replace('<!--TAG_TEXTAREA_'.$key.'-->',$textarea,$content);
  1385. }
  1386. }
  1387. return $content;
  1388. }
  1389. //加入会员格式化
  1390. function setTplUserFormat($uid,$type,$is_mark,$img_type,$img_size,$link_class,$img_class,$tpl)
  1391. {
  1392. global $_FANWE;
  1393. $uid = (int)$uid;
  1394. $key = md5($type.'_'.$is_mark.'_'.$img_type.'_'.$img_size.'_'.$link_class.'_'.$img_class.'_'.$tpl);
  1395. $_FANWE['tpl_user_formats'][$uid][$key] = array(
  1396. 'type' => $type,
  1397. 'is_mark'=>$is_mark,
  1398. 'img_type' => $img_type,
  1399. 'img_size' => $img_size,
  1400. 'link_class' => $link_class,
  1401. 'img_class' => $img_class,
  1402. 'tpl' => $tpl
  1403. );
  1404. return "<!--USER_".$uid."_".$key."-->";
  1405. }
  1406. //获取会员格式化html
  1407. function getUserFormatHtml($user,$format)
  1408. {
  1409. global $_FANWE;
  1410. static $templates = array(),$daren_name = NULL;
  1411. $html = '';
  1412. if(!empty($format['tpl']))
  1413. {
  1414. if(!isset($templates[$format['tpl']]))
  1415. $templates[$format['tpl']] = template($format['tpl']);
  1416. if($templates[$format['tpl']])
  1417. {
  1418. ob_start();
  1419. include $templates[$format['tpl']];
  1420. $html = ob_get_contents();
  1421. ob_end_clean();
  1422. }
  1423. }
  1424. else
  1425. {
  1426. $uid = $user['uid'];
  1427. $user_name = htmlspecialchars($user['user_name']);
  1428. if($format['type'] == 0)
  1429. $html = '<a class="GUID '.$format['link_class'].'" uid="'.$uid.'" title="'.$user_name.'" href="'.$user['url'].'" target="_blank">'.$user_name.'</a>';
  1430. else
  1431. {
  1432. $width = '';
  1433. if($format['img_size'] > 0)
  1434. $width = 'width="'.$format['img_size'].'" ';
  1435. $is_lazyload = FALSE;
  1436. $img_class = $format['img_class'];
  1437. if(!empty($img_class))
  1438. $is_lazyload = strpos($img_class,'lazyload');
  1439. $link_class = '';
  1440. if(!empty($format['link_class']))
  1441. $link_class = 'class="'.$format['link_class'].'" ';
  1442. if($is_lazyload === FALSE)
  1443. $html = '<a '.$link_class.'title="'.$user_name.'" href="'.$user['url'].'" target="_blank"><img class="GUID '.$img_class.'" uid="'.$uid.'" src="'.avatar($uid,$format['img_type'],$user['server_code'],1).'" '.$width.' alt="'.$user_name.'"/></a>';
  1444. else
  1445. $html = '<a '.$link_class.'title="'.$user_name.'" href="'.$user['url'].'" target="_blank"><img class="GUID '.$img_class.'" uid="'.$uid.'" original="'.avatar($uid,$format['img_type'],$user['server_code'],1).'" src="./tpl/images/lazyload.gif" '.$width.' alt="'.$user_name.'"/></a>';
  1446. }
  1447. if($format['is_mark'] == 1)
  1448. {
  1449. if($user['is_daren'] == 1)
  1450. {
  1451. if($daren_name === NULL)
  1452. $daren_name = sprintf(lang('user','daren_alt'),$_FANWE['setting']['site_name']);
  1453. $html .= '<a href="'.FU('daren/apply').'" class="v" target="_blank"><img title="'.$daren_name.'" src="./tpl/images/daren_icon.png" class="v"></a>';
  1454. }
  1455. elseif($user['is_buyer'] == 1)
  1456. $html .= '<a href="'.FU('settings/buyerverifier').'" class="v" target="_blank"><img title="'.lang('user','buyer_alt').'" src="./tpl/images/buyer_icon.png" class="v"></a>';
  1457. }
  1458. }
  1459. return $html;
  1460. }
  1461. function formatUserFollowTags(&$content)
  1462. {
  1463. global $_FANWE;
  1464. preg_match_all("/<!--getfollow\s(\d+?)\s(.+?)-->/",$content,$follows);
  1465. if(!empty($follows[1]))
  1466. {
  1467. $patterns = array();
  1468. $replace = array();
  1469. $user_ids = array();
  1470. foreach($follows[1] as $key => $uid)
  1471. {
  1472. $uid = (int)$uid;
  1473. $tpl = $follows[2][$key];
  1474. if($_FANWE['uid'] == $uid)
  1475. {
  1476. if(!isset($user_ids[$uid][$tpl]))
  1477. {
  1478. $patterns[] = "<!--getfollow ".$uid." ".$tpl."-->";
  1479. $replace[] = getUserFollowFormatHtml($uid,-1,$tpl);
  1480. $user_ids[$uid][$tpl] = -1;
  1481. }
  1482. }
  1483. else
  1484. {
  1485. $user_ids[$uid]['is_follow'] = 0;
  1486. $user_ids[$uid]['tpls'][$tpl] = 0;
  1487. }
  1488. }
  1489. unset($user_ids[$_FANWE['uid']]);
  1490. if($_FANWE['uid'] > 0)
  1491. {
  1492. $follow_ids = array_keys($user_ids);
  1493. if(count($follow_ids) > 0)
  1494. {
  1495. $follow_ids = implode(',',$follow_ids);
  1496. $follow_ids = str_replace(',,',',',$follow_ids);
  1497. if(!empty($follow_ids))
  1498. {
  1499. $res = FDB::query("SELECT uid FROM ".FDB::table('user_follow').'
  1500. WHERE f_uid = '.$_FANWE['uid'].' AND uid IN ('.$follow_ids.')');
  1501. while($item = FDB::fetch($res))
  1502. {
  1503. $user_ids[$item['uid']]['is_follow'] = 1;
  1504. }
  1505. }
  1506. }
  1507. }
  1508. foreach($user_ids as $uid => $user)
  1509. {
  1510. $is_follow = $user['is_follow'];
  1511. foreach($user['tpls'] as $tpl => $temp)
  1512. {
  1513. $patterns[] = "<!--getfollow ".$uid." ".$tpl."-->";
  1514. $replace[] = getUserFollowFormatHtml($uid,$is_follow,$tpl);
  1515. }
  1516. }
  1517. $content = str_replace($patterns,$replace,$content);
  1518. }
  1519. }
  1520. //获取会员关注格式化html
  1521. function getUserFollowFormatHtml($uid,$is_follow,$tpl)
  1522. {
  1523. static $templates = array();
  1524. $html = '';
  1525. if(!isset($templates[$tpl]))
  1526. $templates[$tpl] = template($tpl);
  1527. if($templates[$tpl])
  1528. {
  1529. ob_start();
  1530. include $templates[$tpl];
  1531. $html = ob_get_contents();
  1532. ob_end_clean();
  1533. }
  1534. return $html;
  1535. }
  1536. /**
  1537. * 清除缓存目录
  1538. * @param string $file 缓存模板目录
  1539. * @param int $is_dynamic 是否为动态缓存
  1540. * @param string $dir 缓存目录
  1541. * @return void
  1542. */
  1543. function clearTplCache($file,$is_dynamic = 0,$dir='')
  1544. {
  1545. if(!empty($dir))
  1546. $dir .= '/';
  1547. $dir = ($is_dynamic == 1 ? 'dynamic/' : 'static/').$dir;
  1548. clearDir(PUBLIC_ROOT.'./data/tpl/caches/'.$dir.str_replace('/', '_', $file));
  1549. }
  1550. /**
  1551. * 检测缓存文件是否需要更新
  1552. * @param string $cache_file 缓存文件路径
  1553. * @param int $time_out 缓存时间(秒)
  1554. * @param int $is_clear 清除缓存
  1555. * @return bool 需要更新返回 true
  1556. */
  1557. function getCacheIsUpdate($cache_file,$time_out,$is_clear = 0)
  1558. {
  1559. if (!file_exists($cache_file))
  1560. return true;
  1561. $time_clear = 0;
  1562. if($is_clear == 1)
  1563. {
  1564. $clear_path = FANWE_ROOT.'./public/data/tpl/caches/page/is_clear.lock';
  1565. if(file_exists($clear_path))
  1566. $time_clear = (int)@file_get_contents($clear_path);
  1567. }
  1568. $mtime = filemtime($cache_file);
  1569. if($time_clear > 0 && $mtime < $time_clear)
  1570. {
  1571. removeFile($cache_file);
  1572. return true;
  1573. }
  1574. elseif(TIMESTAMP - $mtime > $time_out)
  1575. {
  1576. removeFile($cache_file);
  1577. return true;
  1578. }
  1579. else
  1580. return false;
  1581. }
  1582. /**
  1583. * 输出json信息
  1584. * @param mixed $result 要输出的信息
  1585. * @return void
  1586. */
  1587. function outputJson($result,$is_die = true)
  1588. {
  1589. static $json = NULL;
  1590. if($json === NULL)
  1591. {
  1592. require fimport('class/json');
  1593. $json = new Json();
  1594. }
  1595. if($is_die)
  1596. die($json->encode($result));
  1597. else
  1598. return $json->encode($result);
  1599. }
  1600. /**
  1601. * 返回json信息
  1602. * @param mixed $result
  1603. * @return string
  1604. */
  1605. function getJson($result)
  1606. {
  1607. static $json = NULL;
  1608. if($json === NULL)
  1609. {
  1610. require fimport('class/json');
  1611. $json = new Json();
  1612. }
  1613. return $json->encode($result);
  1614. }
  1615. /**
  1616. * 清除指定目录下的文件
  1617. * @param string $dir 目录路径
  1618. * @return void
  1619. */
  1620. function clearDir($dir,$is_del_dir = false)
  1621. {
  1622. if(!file_exists($dir))
  1623. return;
  1624. $directory = dir($dir);
  1625. while($entry = $directory->read())
  1626. {
  1627. if($entry != '.' && $entry != '..')
  1628. {
  1629. $filename = $dir.'/'.$entry;
  1630. if(is_dir($filename))
  1631. clearDir($filename,$is_del_dir);
  1632. if(is_file($filename))
  1633. removeFile($filename);
  1634. }
  1635. }
  1636. $directory->close();
  1637. if($is_del_dir)
  1638. @rmdir($dir);
  1639. }
  1640. /**
  1641. * 检查目标文件夹是否存在,如果不存在则自动创建该目录
  1642. *
  1643. * @access public
  1644. * @param string folder 目录路径。不能使用相对于网站根目录的URL
  1645. *
  1646. * @return bool
  1647. */
  1648. function makeDir($folder)
  1649. {
  1650. $reval = false;
  1651. if (!file_exists($folder))
  1652. {
  1653. $folder = str_replace(FANWE_ROOT,'',$folder);
  1654. /* 如果目录不存在则尝试创建该目录 */
  1655. @umask(0);
  1656. /* 将目录路径拆分成数组 */
  1657. preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);
  1658. /* 如果第一个字符为/则当作物理路径处理 */
  1659. $base = FANWE_ROOT.(($atmp[0][0] == '/') ? '/' : '');
  1660. /* 遍历包含路径信息的数组 */
  1661. foreach ($atmp[1] AS $val)
  1662. {
  1663. if ('' != $val)
  1664. {
  1665. $base .= $val;
  1666. if ('..' == $val || '.' == $val)
  1667. {
  1668. /* 如果目录为.或者..则直接补/继续下一个循环 */
  1669. $base .= '/';
  1670. continue;
  1671. }
  1672. }
  1673. else
  1674. {
  1675. continue;
  1676. }
  1677. $base .= '/';
  1678. if (!file_exists($base))
  1679. {
  1680. /* 尝试创建目录,如果创建失败则继续循环 */
  1681. if (@mkdir(rtrim($base, '/'), 0777))
  1682. {
  1683. @chmod($base, 0777);
  1684. $reval = true;
  1685. }
  1686. }
  1687. }
  1688. }
  1689. else
  1690. {
  1691. /* 路径已经存在。返回该路径是不是一个目录 */
  1692. $reval = is_dir($folder);
  1693. }
  1694. clearstatcache();
  1695. return $reval;
  1696. }
  1697. /**
  1698. * utf8字符串转为GBK字符串
  1699. * @param string $str 要转换的字符串
  1700. * @return void
  1701. */
  1702. function utf8ToGB($str)
  1703. {
  1704. static $chinese = NULL;
  1705. if($chinese === NULL)
  1706. {
  1707. require_once fimport('class/chinese');
  1708. $chinese = new Chinese('UTF-8','GBK');
  1709. }
  1710. return $chinese->convert($str);
  1711. }
  1712. /**
  1713. * GBK字符串转utf8为字符串
  1714. * @param string $str 要转换的字符串
  1715. * @return void
  1716. */
  1717. function gbToUTF8($str)
  1718. {
  1719. static $chinese = NULL;
  1720. if($chinese === NULL)
  1721. {
  1722. require_once fimport('class/chinese');
  1723. $chinese = new Chinese('GBK','UTF-8');
  1724. }
  1725. return $chinese->convert($str);
  1726. }
  1727. /**
  1728. * utf8字符转Unicode字符
  1729. * @param string $char 要转换的单字符
  1730. * @return void
  1731. */
  1732. function utf8ToUnicode($char)
  1733. {
  1734. switch(strlen($char))
  1735. {
  1736. case 1:
  1737. return ord($char);
  1738. case 2:
  1739. $n = (ord($char[0]) & 0x3f) << 6;
  1740. $n += ord($char[1]) & 0x3f;
  1741. return $n;
  1742. case 3:
  1743. $n = (ord($char[0]) & 0x1f) << 12;
  1744. $n += (ord($char[1]) & 0x3f) << 6;
  1745. $n += ord($char[2]) & 0x3f;
  1746. return $n;
  1747. case 4:
  1748. $n = (ord($char[0]) & 0x0f) << 18;
  1749. $n += (ord($char[1]) & 0x3f) << 12;
  1750. $n += (ord($char[2]) & 0x3f) << 6;
  1751. $n += ord($char[3]) & 0x3f;
  1752. return $n;
  1753. }
  1754. }
  1755. /**
  1756. * utf8字符串分隔为unicode字符串
  1757. * @param string $str 要转换的字符串
  1758. * @param string $pre
  1759. * @return string
  1760. */
  1761. function segmentToUnicode($str,$pre = '')
  1762. {
  1763. $arr = array();
  1764. $str_len = mb_strlen($str,'UTF-8');
  1765. for($i = 0;$i < $str_len;$i++)
  1766. {
  1767. $s = mb_substr($str,$i,1,'UTF-8');
  1768. if($s != ' ' && $s != ' ')
  1769. {
  1770. $arr[] = $pre.'ux'.utf8ToUnicode($s);
  1771. }
  1772. }
  1773. $arr = array_unique($arr);
  1774. return implode(' ',$arr);
  1775. }
  1776. /**
  1777. * 将标签数组转换为unicode字符串
  1778. * @param array $tags 要转换的标签
  1779. * @param string $pre
  1780. * @return string
  1781. */
  1782. function tagToUnicode($tags,$pre = '')
  1783. {
  1784. $tags = array_unique($tags);
  1785. $arr = array();
  1786. foreach($tags as $tag)
  1787. {
  1788. $tmp = '';
  1789. $str_len = mb_strlen($tag,'UTF-8');
  1790. for($i = 0;$i < $str_len;$i++)
  1791. {
  1792. $s = mb_substr($tag,$i,1,'UTF-8');
  1793. if($s != ' ' && $s != ' ')
  1794. {
  1795. $tmp.= 'ux'.utf8ToUnicode($s);
  1796. }
  1797. }
  1798. if($tmp != '')
  1799. $arr[] = $pre.$tmp;
  1800. }
  1801. $arr = array_unique($arr);
  1802. return implode(' ',$arr);
  1803. }
  1804. /**
  1805. * 清除符号
  1806. * @param string $str 要清除符号的字符串
  1807. * @return string
  1808. */
  1809. function clearSymbol($str)
  1810. {
  1811. static $symbols = NULL;
  1812. if($symbols === NULL)
  1813. {
  1814. $symbols = file_get_contents(PUBLIC_ROOT.'./table/symbol.table');
  1815. $symbols

Large files files are truncated, but you can click here to view the full file