PageRenderTime 33ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/startZine/src/main/core/function/global.func.php

http://ownerpress.googlecode.com/
PHP | 2753 lines | 2277 code | 179 blank | 297 comment | 191 complexity | f8d6a76af62f652deea627aaaa9c15ef MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1

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

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