PageRenderTime 58ms CodeModel.GetById 12ms 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
  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 = explode("\r\n",$symbols);
  1816. }
  1817. return str_replace($symbols,"",$str);
  1818. }
  1819. /**
  1820. * 对 MYSQL LIKE 的内容进行转义
  1821. *
  1822. * @access public
  1823. * @param string string 内容
  1824. * @return string
  1825. */
  1826. function fMysqlLikeQuote($str)
  1827. {
  1828. return strtr($str, array("\\\\" => "\\\\\\\\", '_' => '\_', '%' => '\%', "\'" => "\\\\\'"));
  1829. }
  1830. /**
  1831. * 页面跳转
  1832. * @param string $string
  1833. * @param bool $replace
  1834. * @param int $http_response_code
  1835. * @return void
  1836. */
  1837. function fHeader($string, $replace = true, $http_response_code = 0)
  1838. {
  1839. global $_FANWE;
  1840. $string = str_replace(array("\r", "\n"), array('', ''), $string);
  1841. $reg = '/location:\s?'.preg_quote($_FANWE['site_root'], '/').'/i';
  1842. $string = preg_replace($reg,'Location: '.$_FANWE['site_url'], $string);
  1843. if(strpos($string,$_FANWE['site_url']))
  1844. {
  1845. fSetCookie('from_heade',authcode(TIME_UTC,'ENCODE'));
  1846. }
  1847. if(empty($http_response_code) || PHP_VERSION < '4.3' )
  1848. {
  1849. @header($string, $replace);
  1850. }
  1851. else
  1852. {
  1853. @header($string, $replace, $http_response_code);
  1854. }
  1855. if(preg_match('/^\s*location:/is', $string))
  1856. {
  1857. exit();
  1858. }
  1859. }
  1860. /**
  1861. * 显示指定名称的广告位布局
  1862. * @param string $id 布局编号
  1863. * @param int $count 显示数量
  1864. * @param string $target 关键字
  1865. * @return array
  1866. */
  1867. function getAdvLayout($id,$count = '',$target='')
  1868. {
  1869. global $_FANWE;
  1870. $layout = FDB::fetchFirst('SELECT rec_id AS pid,item_limit AS acount,target_id AS target FROM '.FDB::table('layout')." WHERE layout_id ='$id' AND tmpl = '".$_FANWE['setting']['site_tmpl']."' AND rec_module = 'AdvPosition'");
  1871. if(!$layout)
  1872. return '';
  1873. if($count != '')
  1874. $layout['acount'] = intval($count);
  1875. if($target != '')
  1876. $layout['target'] = explode(',',$target);
  1877. return getAdvPosition($layout['pid'],$layout['acount'],$layout['target']);
  1878. }
  1879. /**
  1880. * 显示指定ID的广告位ID
  1881. * @param string $id 广告位
  1882. * @param int $count 显示数量
  1883. * @param string $target 关键字
  1884. * @return array
  1885. */
  1886. function getAdvPosition($id,$count = '',$target='')
  1887. {
  1888. global $_FANWE;
  1889. $ap = FDB::fetchFirst('SELECT * FROM '.FDB::table('adv_position').' WHERE id ='.$id);
  1890. if(!$ap)
  1891. return '';
  1892. if($target != '')
  1893. $target = explode(',',$target);
  1894. $where = "status = 1 AND position_id = '$id'";
  1895. if($target != '')
  1896. $where .= ' AND target_key'.FDB::createIN($target);
  1897. $sql = 'SELECT * FROM '.FDB::table('adv').' WHERE '.$where.' ORDER BY sort ASC,id DESC';
  1898. if($count > 0)
  1899. $sql .= ' LIMIT 0,'.$count;
  1900. $adv_res = FDB::query($sql);
  1901. $adv_list = array();
  1902. while($adv = FDB::fetch($adv_res))
  1903. {
  1904. $adv['html'] = getAdvHTML($adv,$ap);
  1905. $adv['durl'] = FU("adv/show",array("id"=>$adv['id']));
  1906. $adv['turl'] = $adv['url'];
  1907. $adv['url'] = urlencode(FU("adv/show",array("id"=>$adv['id'])));
  1908. $adv_list[] = $adv;
  1909. }
  1910. $ap['adv_list'] = $adv_list;
  1911. return $ap;
  1912. }
  1913. /**
  1914. * 获取广告的html代码
  1915. * @param array $adv 广告
  1916. * @param array $ap 广告位
  1917. * @return string
  1918. */
  1919. function getAdvHTML($adv,$ap)
  1920. {
  1921. if($ap['width'] == 0)
  1922. $ap['width']="";
  1923. else
  1924. $ap['width']=" width='".$ap['width']."'";
  1925. if($ap['height'] == 0)
  1926. $ap['height']="";
  1927. else
  1928. $ap['height']=" height='".$ap['height']."'";
  1929. switch($adv['type'])
  1930. {
  1931. case '1':
  1932. if($adv['url']=='')
  1933. $adv_str = "<img src='".$adv['code']."'".$ap['width'].$ap['height']."/>";
  1934. else
  1935. $adv_str = "<a href='".FU("adv/show",array("id"=>$adv['id']))."' target='_blank'><img src='".$adv['code']."'".$ap['width'].$ap['height']."/></a>";
  1936. break;
  1937. case '2':
  1938. $adv_str = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'".$ap['width'].$ap['height'].">".
  1939. "<param name='movie' value='".$adv['code']."' />".
  1940. "<param name='quality' value='high' />".
  1941. "<param name='menu' value='false' />".
  1942. "<embed src='".$adv['code']."' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'".$ap['width'].$ap['height']."></embed>".
  1943. "</object>";
  1944. break;
  1945. case '3':
  1946. $adv_str = $adv['code'];
  1947. break;
  1948. }
  1949. return $adv_str;
  1950. }
  1951. /**
  1952. * 写入文件内容
  1953. * @param string $filepat 文件路径
  1954. * @param string $content 写入内容
  1955. * @param string $type 写入方式 w:将文件指针指向文件头并将文件大小截为零 a:将文件指针指向文件末尾
  1956. * @return string
  1957. */
  1958. function writeFile($filepath,$content,$type='w')
  1959. {
  1960. $is_success = false;
  1961. if($fp = fopen($filepath,$type))
  1962. {
  1963. /*$start_time = microtime();
  1964. do
  1965. {
  1966. $is_write = flock($fp, LOCK_EX);
  1967. if(!$is_write)
  1968. usleep(round(rand(0,100) * 1000));
  1969. }
  1970. while(!$is_write && ((microtime() - $start_time) < 10000));
  1971. if ($is_write && fwrite($fp, $content))
  1972. $is_success = true;*/
  1973. @flock($fp, LOCK_EX);
  1974. if (fwrite($fp, $content))
  1975. $is_success = true;
  1976. @flock($fp,LOCK_UN);
  1977. @fclose($fp);
  1978. @chmod($filepath, 0777);
  1979. }
  1980. return $is_success;
  1981. }
  1982. /**
  1983. * 删除文件
  1984. * @param string $filepat 文件路径
  1985. * @return bool
  1986. */
  1987. function removeFile($filepath)
  1988. {
  1989. $is_success = false;
  1990. /*do
  1991. {
  1992. @unlink($filepath);
  1993. $is_exists = file_exists($filepath);
  1994. if($is_exists)
  1995. usleep(round(rand(0,100) * 1000));
  1996. else
  1997. $is_success = true;
  1998. }
  1999. while($is_exists && ((microtime() - $start_time) < 10000));*/
  2000. @unlink($filepath);
  2001. if(!file_exists($filepath))
  2002. $is_success = true;
  2003. return $is_success;
  2004. }
  2005. /**
  2006. * 获取缓存的数据
  2007. * @param string $key 缓存键名 如果有目录 格式为 目录1/目录2/.../键名
  2008. * @return mixed
  2009. */
  2010. function getCache($key)
  2011. {
  2012. static $caches = array();
  2013. if(!isset($caches[$key]))
  2014. {
  2015. if(!file_exists(PUBLIC_ROOT.'./data/caches/custom/'.$key.'.cache.php'))
  2016. return NULL;
  2017. else
  2018. {
  2019. include(PUBLIC_ROOT.'./data/caches/custom/'.$key.'.cache.php');
  2020. $list = explode('/',$key);
  2021. $key = end($list);
  2022. $caches[$key] = $data[$key];
  2023. }
  2024. }
  2025. return $caches[$key];
  2026. }
  2027. /**
  2028. * 设置缓存数据
  2029. * @param string $key 缓存键名 可设置所在目录 格式为 目录1/目录2/.../键名
  2030. * @param string $data 缓存的数据
  2031. * @return bool
  2032. */
  2033. function setCache($key,$data)
  2034. {
  2035. $cache_path = PUBLIC_ROOT.'./data/caches/custom/'.$key.'.cache.php';
  2036. $phth = dirname($cache_path);
  2037. makeDir($phth);
  2038. $list = explode('/',$key);
  2039. $key = end($list);
  2040. $cache_data = "<?php\n".'$data[\''.$key."'] = ".var_export($data, true).";\n\n?>";
  2041. return writeFile($cache_path,$cache_data);
  2042. }
  2043. /**
  2044. * 删除缓存
  2045. * @param string $key 缓存键名 如果有目录 格式为 目录1/目录2/.../键名
  2046. * @return bool
  2047. */
  2048. function deleteCache($key)
  2049. {
  2050. return removeFile(PUBLIC_ROOT.'./data/caches/custom/'.$key.'.cache.php');
  2051. }
  2052. /**
  2053. * 清空缓存目录
  2054. * @param string $dir 缓存目录
  2055. * @return void
  2056. */
  2057. function clearCacheDir($dir)
  2058. {
  2059. clearDir(PUBLIC_ROOT.'./data/caches/custom/'.$dir,true);
  2060. }
  2061. function avatar($uid, $type = 'm',$code = '', $is_src = 0,$is_full = false)
  2062. {
  2063. static $avatars = array();
  2064. static $types = array(
  2065. 's'=>'small',
  2066. 'm'=>'middle',
  2067. 'b'=>'big',
  2068. );
  2069. $size = 'small';
  2070. if(array_key_exists($type,$types))
  2071. $size = $types[$type];
  2072. if($avatars[$uid][$size] === NULL)
  2073. {
  2074. global $_FANWE;
  2075. $uid = sprintf("%09d", $uid);
  2076. $dir1 = substr($uid, 0, 3);
  2077. $dir2 = substr($uid, 3, 2);
  2078. $dir3 = substr($uid, 5, 2);
  2079. if(empty($code))
  2080. {
  2081. $file_path = PUBLIC_ROOT.'./upload/avatar/'.$dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).'_'.$size.'.jpg';
  2082. $file = './public/upload/avatar/'.$dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).'_'.$size.'.jpg';
  2083. if(!file_exists($file_path))
  2084. $file = './public/upload/avatar/noavatar_'.$size.'.jpg';
  2085. }
  2086. else
  2087. {
  2088. $file = './'.$code.'/public/upload/avatar/'.$dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).'_'.$size.'.jpg';
  2089. }
  2090. $avatars[$uid][$size] = $file;
  2091. }
  2092. $src = $avatars[$uid][$size];
  2093. if($is_full)
  2094. $src = FS("Image")->getImageUrl($src,2);
  2095. return $is_src ? $src : '<img src="'.$src.'" />';
  2096. }
  2097. /**
  2098. * 根据图片原图地址。 获取规格图片的地址,如果没有该规则动态生成。
  2099. * by fzmatthew
  2100. */
  2101. function getImgName($img_url,$width=0,$height=0,$gen = 0,$is_full = false)
  2102. {
  2103. static $imagec = NULL;
  2104. if($width>0&&$height>0)
  2105. {
  2106. if(strpos($img_url,'./public/') === FALSE)
  2107. {
  2108. $img_url_arr[0] = substr($img_url,0,-4);
  2109. $img_url_arr[1] = substr($img_url,-3,3);
  2110. $img_url = $img_url_arr[0]."_".$width."x".$height.".".$img_url_arr[1];
  2111. if($is_full)
  2112. $img_url = FS("Image")->getImageUrl($img_url);
  2113. $img_url .= '?gen='.$gen;
  2114. }
  2115. else
  2116. {
  2117. if(!file_exists(FANWE_ROOT.$img_url))
  2118. return;
  2119. if($imagec === NULL)
  2120. {
  2121. include_once fimport('class/image');
  2122. $imagec = new Image();
  2123. }
  2124. $org_img = $img_url;
  2125. $img_url_arr[0] = substr($img_url,0,-4);
  2126. $img_url_arr[1] = substr($img_url,-3,3);
  2127. $img_url = $img_url_arr[0]."_".$width."x".$height.".".$img_url_arr[1];
  2128. if(!file_exists(FANWE_ROOT.$img_url))
  2129. {
  2130. $imagec->thumb(FANWE_ROOT.$org_img,$width,$height,$gen);
  2131. }
  2132. if($is_full)
  2133. $img_url = FS("Image")->getImageUrl($img_url,2);
  2134. }
  2135. }
  2136. return $img_url;
  2137. }
  2138. /**
  2139. *
  2140. * @param $origin_path 原始物理图片地址
  2141. * @param $path 存储的路径
  2142. * @param $file_name 保存的文件名
  2143. * @param $del_temp 是否删除临时文件
  2144. * @param $id 关联编号,将根据编号生成目录
  2145. *
  2146. * 返回 复制成功的信息,如为false则复制失败
  2147. * array(
  2148. * 'path' => xxx //物理路径
  2149. * 'url' => xxx //相对路径
  2150. * );
  2151. *
  2152. * by fzmatthew
  2153. */
  2154. function copyFile($origin_path, $path = 'share',$del_temp = true,$id = 0)
  2155. {
  2156. static $image = NULL;
  2157. if($image === NULL)
  2158. {
  2159. include_once fimport('class/image');
  2160. $image = new Image();
  2161. }
  2162. if($path == 'temp')
  2163. $dir = './public/upload/temp/'.fToDate(NULL,'Y/m/d/H');
  2164. else
  2165. {
  2166. if($id > 0)
  2167. $dir = './public/upload/'.$path.'/'.getDirsById($id);
  2168. else
  2169. $dir = './public/upload/'.$path.'/'.fToDate(NULL,'Y/m/d');
  2170. }
  2171. $file_name = md5(microtime(true)).random('6').'.jpg';
  2172. makeDir(FANWE_ROOT.$dir);
  2173. $file_path = FANWE_ROOT.$dir."/".$file_name;
  2174. if(file_exists($origin_path) && @copy($origin_path,$file_path))
  2175. {
  2176. if($del_temp)
  2177. @unlink($origin_path);
  2178. }
  2179. else
  2180. {
  2181. $data = getUrlContent($origin_path);
  2182. if(!empty($data) && @file_put_contents($file_path,$data) > 0)
  2183. {
  2184. if($del_temp)
  2185. @unlink($origin_path);
  2186. }
  2187. else
  2188. return false;
  2189. }
  2190. $info = $image->getImageInfo($file_path);
  2191. if($info['type'] != 'jpg' && $info['type'] != 'jpeg')
  2192. {
  2193. if(!$image->convertType($file_path,$file_path))
  2194. return false;
  2195. }
  2196. return array(
  2197. 'path' => $file_path,
  2198. 'url' => $dir."/".$file_name,
  2199. 'width' => $info[0],
  2200. 'height' => $info[1],
  2201. );
  2202. }
  2203. /**
  2204. *
  2205. * @param $origin_path 原始物理图片地址
  2206. * @param array $sizes 缩略图大小信息 为空则取后台设置
  2207. 可生成多个缩略图
  2208. 数组 参数1 为宽度,
  2209. 参数2为高度,
  2210. 参数3为处理方式:0(缩放,默认),1(剪裁),
  2211. 参数4为是否水印 默认为 0(不生成水印)
  2212. array(
  2213. array(300,300,0,0),
  2214. array(100,100,0,0),
  2215. ...
  2216. ),
  2217. * @param $path 存储的路径
  2218. * @param $file_name 保存的文件名
  2219. * @param $del_temp 是否删除临时文件
  2220. * @param $id 关联编号,将根据编号生成目录
  2221. *
  2222. * 返回 复制成功的信息,如为false则复制失败
  2223. * array(
  2224. * 'path' => xxx //物理路径
  2225. * 'url' => xxx //相对路径
  2226. * );
  2227. *
  2228. * by fzmatthew
  2229. */
  2230. function copyImage($origin_path,$sizes = array(),$path = 'share', $del_temp = true,$id = 0)
  2231. {
  2232. global $_FANWE;
  2233. static $size_setting = NULL,$image = NULL;
  2234. if($image === NULL)
  2235. {
  2236. include_once fimport('class/image');
  2237. $image = new Image();
  2238. }
  2239. if($path == 'temp')
  2240. $dir = './public/upload/temp/'.fToDate(NULL,'Y/m/d/H');
  2241. else
  2242. {
  2243. if($id > 0)
  2244. $dir = './public/upload/'.$path.'/'.getDirsById($id);
  2245. else
  2246. $dir = './public/upload/'.$path.'/'.fToDate(NULL,'Y/m/d');
  2247. }
  2248. makeDir(FANWE_ROOT.$dir);
  2249. $file_name = md5(microtime(true)).random('6').'.jpg';
  2250. $file_path = FANWE_ROOT.$dir."/".$file_name;
  2251. $bln = false;
  2252. if(file_exists($origin_path) && @copy($origin_path,$file_path))
  2253. $bln = true;
  2254. else
  2255. {
  2256. $data = getUrlContent($origin_path);
  2257. if(!empty($data) && @file_put_contents($file_path,$data) > 0)
  2258. $bln = true;
  2259. }
  2260. if($bln)
  2261. {
  2262. $info = $image->getImageInfo($file_path);
  2263. if($info['type'] != 'jpg' && $info['type'] != 'jpeg')
  2264. {
  2265. if(!$image->convertType($file_path,$file_path))
  2266. return false;
  2267. }
  2268. $water_image = $_FANWE['setting']['water_image'];
  2269. if(!empty($water_image))
  2270. $water_image = FANWE_ROOT.$water_image;
  2271. $water_mark = intval($_FANWE['setting']['water_mark']);
  2272. $alpha = intval($_FANWE['setting']['water_alpha']);
  2273. $place = intval($_FANWE['setting']['water_position']);
  2274. if($sizes !== false && empty($sizes))
  2275. {
  2276. if($size_setting === NULL)
  2277. {
  2278. if(!isset($_FANWE['cache']['image_sizes']))
  2279. FanweService::instance()->cache->loadCache('image_sizes');
  2280. $size_setting = $_FANWE['cache']['image_sizes'];
  2281. }
  2282. $sizes = $size_setting;
  2283. }
  2284. foreach($sizes as $size)
  2285. {
  2286. if($size[0] > 0 || $size[1] > 0)
  2287. {
  2288. $thumb_bln = false;
  2289. $thumb_type = isset($size[2]) ? intval($size[2]) : 0;
  2290. if($thumb = $image->thumb($file_path,$size[0],$size[1],$thumb_type))
  2291. {
  2292. if(isset($size[3]) && intval($size[3]) > 0)
  2293. $image->water($thumb['path'],$water_image,$alpha, $place);
  2294. }
  2295. }
  2296. }
  2297. if($del_temp)
  2298. @unlink($origin_path);
  2299. return array(
  2300. 'path' => $file_path,
  2301. 'url' => $dir."/".$file_name,
  2302. 'width' => $info[0],
  2303. 'height' => $info[1],
  2304. );
  2305. }
  2306. else
  2307. return false;
  2308. }
  2309. //获取url的内容
  2310. function getUrlContent($url)
  2311. {
  2312. $content = '';
  2313. if(!parseUrl($url))
  2314. {
  2315. $content = @file_get_contents($url);
  2316. }
  2317. else
  2318. {
  2319. if(function_exists('curl_init'))
  2320. {
  2321. $ch = curl_init();
  2322. curl_setopt($ch, CURLOPT_URL,$url);
  2323. curl_setopt($ch, CURLOPT_TIMEOUT,30);
  2324. curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
  2325. curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
  2326. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  2327. $content = curl_exec($ch);
  2328. curl_close($ch);
  2329. }
  2330. else
  2331. {
  2332. $content = @file_get_contents($url);
  2333. }
  2334. }
  2335. return $content;
  2336. }
  2337. /**
  2338. * 搜索Club的分类
  2339. *
  2340. * @param 父类ID $pid
  2341. * @return 数组
  2342. */
  2343. function getForumClass($pid = 0)
  2344. {
  2345. global $_FANWE;
  2346. FanweService::instance()->cache->loadCache('forums');
  2347. $list = $_FANWE['cache']['forums'];
  2348. if($pid > 0){
  2349. foreach($list as $k => $v)
  2350. {
  2351. if(intval($pid)==$v['fid'])
  2352. return $v;
  2353. }
  2354. }
  2355. else {
  2356. return $list;
  2357. }
  2358. }
  2359. function getAskClass($pid = 0)
  2360. {
  2361. global $_FANWE;
  2362. FanweService::instance()->cache->loadCache('asks');
  2363. $list = $_FANWE['cache']['asks'];
  2364. if($pid > 0){
  2365. foreach($list as $k => $v)
  2366. {
  2367. if(intval($pid)==$v['aid'])
  2368. return $v;
  2369. }
  2370. }
  2371. else {
  2372. return $list;
  2373. }
  2374. }
  2375. /**
  2376. * 获取CDN链接
  2377. * @param string $url 链接
  2378. * @param string $type (image:图片,css:样式,js:脚本),为空则根据后缀获取
  2379. * @return string
  2380. */
  2381. function getCDNUrl($url,$type='')
  2382. {
  2383. global $_FANWE;
  2384. static $img_exts = array('jpg', 'jpeg', 'png', 'bmp','gif','giff'),
  2385. $types = array('image','css','js');
  2386. if(empty($type))
  2387. {
  2388. $ext = fileExt($url);
  2389. if(in_array($ext,$img_exts))
  2390. $type = 'image';
  2391. elseif($ext == 'css')
  2392. $type = 'css';
  2393. elseif($ext == 'js')
  2394. $type = 'js';
  2395. }
  2396. $url_pre = $_FANWE['site_root'];
  2397. if(in_array($type,$types))
  2398. {
  2399. switch($type)
  2400. {
  2401. case 'image':
  2402. if(!empty($_FANWE['config']['cdn']['image']))
  2403. $url_pre = $_FANWE['config']['cdn']['image'];
  2404. break;
  2405. case 'css':
  2406. if(!empty($_FANWE['config']['cdn']['css']))
  2407. $url_pre = $_FANWE['config']['cdn']['css'];
  2408. break;
  2409. case 'js':
  2410. if(!empty($_FANWE['config']['cdn']['js']))
  2411. $url_pre = $_FANWE['config']['cdn']['js'];
  2412. break;
  2413. }
  2414. }
  2415. return $url_pre.$url;
  2416. }
  2417. /**
  2418. * 获取文件扩展名
  2419. * @return string
  2420. */
  2421. function fileExt($file)
  2422. {
  2423. return addslashes(strtolower(substr(strrchr($file, '.'), 1, 10)));
  2424. }
  2425. function priceFormat($price)
  2426. {
  2427. return '¥'.number_format(round($price,2),2);
  2428. }
  2429. /**
  2430. * 根据ID划分目录
  2431. * @return string
  2432. */
  2433. function getDirsById($id)
  2434. {
  2435. $id = sprintf("%011d", $id);
  2436. $dir1 = substr($id, 0, 3);
  2437. $dir2 = substr($id, 3, 3);
  2438. $dir3 = substr($id, 6, 3);
  2439. $dir4 = substr($id, -2);
  2440. return $dir1.'/'.$dir2.'/'.$dir3.'/'.$dir4;
  2441. }
  2442. function cssParse($urls)
  2443. {
  2444. global $_FANWE;
  2445. if(!empty($urls))
  2446. $_FANWE['page_parses']['css'][] = $urls;
  2447. if(is_array($urls))
  2448. {
  2449. $url = md5(implode(',',$urls));
  2450. $css_url = './public/data/tpl/css/'.$url.'.css';
  2451. $url_path = FANWE_ROOT.$css_url;
  2452. if(!file_exists($url_path))
  2453. {
  2454. $css_content = '';
  2455. foreach($urls as $url)
  2456. {
  2457. $url = str_replace('./tpl/css/','./public/data/tpl/css/',$url);
  2458. $css_content .= @file_get_contents(FANWE_ROOT.$url);
  2459. }
  2460. $css_content = preg_replace("/[\r\n]/",'',$css_content);
  2461. @file_put_contents($url_path,$css_content);
  2462. }
  2463. return $css_url;
  2464. }
  2465. else
  2466. {
  2467. return $urls;
  2468. }
  2469. }
  2470. function scriptParse($urls)
  2471. {
  2472. global $_FANWE;
  2473. if(!empty($urls))
  2474. $_FANWE['page_parses']['script'][] = $urls;
  2475. if(is_array($urls))
  2476. {
  2477. $url = md5(implode(',',$urls));
  2478. $js_url = './public/data/tpl/js/'.$url.'.js';
  2479. $url_path = FANWE_ROOT.$js_url;
  2480. if(!file_exists($url_path))
  2481. {
  2482. $js_content = '';
  2483. foreach($urls as $url)
  2484. {
  2485. $url = str_replace('./tpl/js/','./tpl/'.TMPL.'/js/',$url);
  2486. $js_content .= @file_get_contents(FANWE_ROOT.$url)."\r\n";
  2487. }
  2488. @file_put_contents($url_path,$js_content);
  2489. }
  2490. return $js_url;
  2491. }
  2492. else
  2493. {
  2494. return $urls;
  2495. }
  2496. }
  2497. /**
  2498. * 获取是否显示前台管理
  2499. * @return bool
  2500. */
  2501. function getIsManage($module)
  2502. {
  2503. global $_FANWE;
  2504. if($_FANWE['uid'] == 0)
  2505. return false;
  2506. $module = strtolower($module);
  2507. if(isset($_FANWE['authoritys'][$module]))
  2508. return true;
  2509. else
  2510. return false;
  2511. }
  2512. /**
  2513. * 检测是否具有指定的前台管理权限
  2514. * @return bool
  2515. */
  2516. function checkAuthority($module,$action)
  2517. {
  2518. global $_FANWE;
  2519. if($_FANWE['uid'] == 0)
  2520. return false;
  2521. $module = strtolower($module);
  2522. $action = strtolower($action);
  2523. if(isset($_FANWE['authoritys'][$module]) && isset($_FANWE['authoritys'][$module][$action]))
  2524. return true;
  2525. else
  2526. return false;
  2527. return true;
  2528. }
  2529. /**
  2530. * 检测所管理的对像是否锁定,已锁定返回锁定数据array,未锁定返回false
  2531. 如果锁定30分钟以上,还未解锁,将设为未锁定
  2532. * @return
  2533. */
  2534. function checkIsManageLock($module,$id)
  2535. {
  2536. global $_FANWE;
  2537. $module = strtolower($module);
  2538. $lock_file = PUBLIC_ROOT.'./manage/'.$module.'/'.$id.'.lock';
  2539. if(file_exists($lock_file))
  2540. {
  2541. include $lock_file;
  2542. if(TIME_UTC - $lock['time'] > 1800)
  2543. {
  2544. removeFile($lock_file);
  2545. return false;
  2546. }
  2547. if($lock['uid'] == $_FANWE['uid'])
  2548. return false;
  2549. else
  2550. return $lock;
  2551. }
  2552. else
  2553. return false;
  2554. }
  2555. /**
  2556. * 获取前台管理锁定
  2557. * @return void
  2558. */
  2559. function getManageLock($module,$id)
  2560. {
  2561. $module = strtolower($module);
  2562. $lock_file = PUBLIC_ROOT.'./manage/'.$module.'/'.$id.'.lock';
  2563. if(file_exists($lock_file))
  2564. {
  2565. include $lock_file;
  2566. return $lock;
  2567. }
  2568. else
  2569. return false;
  2570. }
  2571. /**
  2572. * 创建前台管理锁定
  2573. * @return void
  2574. */
  2575. function createManageLock($module,$id)
  2576. {
  2577. global $_FANWE;
  2578. $module = strtolower($module);
  2579. $phth = PUBLIC_ROOT.'./manage/'.$module;
  2580. makeDir($phth);
  2581. $lock_file = $phth.'/'.$id.'.lock';
  2582. $data = array(
  2583. 'uid'=>$_FANWE['uid'],
  2584. 'user_name'=>$_FANWE['user_name'],
  2585. 'time'=>TIME_UTC,
  2586. );
  2587. $data = "<?php\n".'$lock = '.var_export($data, true).";\n?>";
  2588. return writeFile($lock_file,$data);
  2589. }
  2590. /**
  2591. * 删除前台管理锁定
  2592. * @return void
  2593. */
  2594. function deleteManageLock($module,$id)
  2595. {
  2596. $module = strtolower($module);
  2597. $lock_file = PUBLIC_ROOT.'./manage/'.$module.'/'.$id.'.lock';
  2598. removeFile($lock_file);
  2599. }
  2600. /**
  2601. * 前台管理日志
  2602. * @return void
  2603. */
  2604. function createManageLog($module,$action,$id,$content = '')
  2605. {
  2606. global $_FANWE;
  2607. $log = array(
  2608. 'rec_id'=>$id,
  2609. 'module'=>$module,
  2610. 'action'=>$action,
  2611. 'uid'=>$_FANWE['uid'],
  2612. 'user_name'=>$_FANWE['user_name'],
  2613. 'content'=>$content,
  2614. 'create_time'=>TIME_UTC
  2615. );
  2616. FDB::insert('manage_log',$log);
  2617. }
  2618. /**
  2619. * 获取分享链接
  2620. * @return string
  2621. */
  2622. function getSnsLink($types,$title,$url,$content,$pic)
  2623. {
  2624. global $_FANWE;
  2625. $links = array();
  2626. $title = urlencode($title);
  2627. $content_url = urlencode($content.' '.$url);
  2628. $content = urlencode($content);
  2629. $url = urlencode($url);
  2630. $site_url = urlencode($_FANWE['site_url']);
  2631. $pic = empty($pic) ? $pic : urlencode($pic);
  2632. foreach($types as $type)
  2633. {
  2634. switch($type)
  2635. {
  2636. case 'kaixin':
  2637. $links[$type] = "http://www.kaixin001.com/diary/write.php?classid=0&title=$title&content=$content_url";
  2638. break;
  2639. case 'renren':
  2640. $links[$type] = "http://share.renren.com/share/buttonshare.do?link=$url&title=$title";
  2641. break;
  2642. case 'sina':
  2643. $links[$type] = "http://v.t.sina.com.cn/share/share.php?sourceUrl=$site_url&content=utf8&url=$url&title=$content";
  2644. if(!empty($pic))
  2645. $links[$type] .= "&pic=$pic";
  2646. break;
  2647. case 'tqq':
  2648. $links[$type] = "http://v.t.qq.com/share/share.php?url=$url&title=$content";
  2649. if(!empty($pic))
  2650. $links[$type] .= "&pic=$pic";
  2651. break;
  2652. case 'douban':
  2653. $links[$type] = "http://www.douban.com/recommend/?url=$url&title=$title";
  2654. break;
  2655. case 'qzone':
  2656. $links[$type] = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=$url&title=$title&summary=$content";
  2657. if(!empty($pic))
  2658. $links[$type] .= "&pics=$pic";
  2659. break;
  2660. case 'baidu':
  2661. $links[$type] = "http://apps.hi.baidu.com/share/?url=$url&title=$title&content=$content";
  2662. break;
  2663. }
  2664. }
  2665. return $links;
  2666. }
  2667. function getLoginModule($class_name)
  2668. {
  2669. global $_FANWE;
  2670. if(!isset($_FANWE['cache']['logins']))
  2671. FanweService::instance()->cache->loadCache('logins');
  2672. if(file_exists(FANWE_ROOT."login/".$class_name.".php"))
  2673. {
  2674. require_once FANWE_ROOT."login/".$class_name.".php";
  2675. if(class_exists($class_name))
  2676. {
  2677. $module = new $class_name;
  2678. return $module->getInfo();
  2679. }
  2680. }
  2681. else
  2682. return false;
  2683. }
  2684. function getLoginModuleList()
  2685. {
  2686. global $_FANWE;
  2687. if(!isset($_FANWE['cache']['logins']))
  2688. FanweService::instance()->cache->loadCache('logins');
  2689. $list = array();
  2690. foreach($_FANWE['cache']['logins'] as $class_name => $val)
  2691. {
  2692. if(file_exists(FANWE_ROOT."login/".$class_name.".php"))
  2693. {
  2694. require_once FANWE_ROOT."login/".$class_name.".php";
  2695. if(class_exists($class_name))
  2696. {
  2697. $module = new $class_name;
  2698. $list[$class_name] = $module->getInfo();
  2699. }
  2700. }
  2701. }
  2702. return $list;
  2703. }
  2704. function deleteShareImg($img_path,$server_code = '')
  2705. {
  2706. if(empty($server_code))
  2707. {
  2708. $img_path = FANWE_ROOT.str_replace('./','',$img_path);
  2709. $paths = pathinfo($img_path);
  2710. @unlink($img_path);
  2711. $old_img = explode('.',$img_path);
  2712. $old_img = $old_img[0];
  2713. if($dirhandle = opendir($paths['dirname']))
  2714. {
  2715. while(($file = readdir($dirhandle)) !== FALSE)
  2716. {
  2717. if(($file!=".") && ($file!=".."))
  2718. {
  2719. $filename = $paths['dirname'].'/'.$file;
  2720. if(strpos($filename,$old_img) !== FALSE)
  2721. {
  2722. @unlink($filename);
  2723. }
  2724. }
  2725. }
  2726. @closedir($dirhandle);
  2727. }
  2728. }
  2729. else
  2730. {
  2731. $server = FS("Image")->getServer($server_code);
  2732. if($server)
  2733. {
  2734. $args = array();
  2735. $args['img_path'] = $img_path;
  2736. $server = FS("Image")->getImageUrlToken($args,$server,1);
  2737. FS("Image")->sendRequest($server,'deleteshareimg');
  2738. }
  2739. }
  2740. }
  2741. function deleteImg($img_path,$server_code = '')
  2742. {
  2743. if(empty($server_code))
  2744. {
  2745. $img_path = FANWE_ROOT.str_replace('./','',$img_path);
  2746. @unlink($img_path);
  2747. }
  2748. else
  2749. {
  2750. $server = FS("Image")->getServer($server_code);
  2751. if($server)
  2752. {
  2753. $args = array();
  2754. $args['img_path'] = $img_path;
  2755. $server = FS("Image")->getImageUrlToken($args,$server,1);
  2756. FS("Image")->sendRequest($server,'deleteimg');
  2757. }
  2758. }
  2759. }
  2760. /**
  2761. * 清除当前2小时以前的临时图片
  2762. * @param string $dir 目录路径
  2763. * @return void
  2764. */
  2765. function clearTempImage($path = '')
  2766. {
  2767. global $_FANWE;
  2768. if($path == '')
  2769. $_FANWE['clear_image_count'] = 0;
  2770. $max_time = mktime(date('H')-2,0,0,date('m'),date('d'),date('Y')) - date('Z');
  2771. $basepath = FANWE_ROOT.'public/upload/temp/';
  2772. $paths = array();
  2773. if($path != '')
  2774. {
  2775. $currentpath = str_replace($basepath,'',$path);
  2776. $paths = explode('/',$currentpath);
  2777. $year = (int)$paths[0];
  2778. $month = isset($paths[1]) ? (int)$paths[1] : 1;
  2779. $day = isset($paths[2]) ? (int)$paths[2] : 1;
  2780. $hours = isset($paths[3]) ? (int)$paths[3] : 0;
  2781. $day_time = mktime($hours,0,0,$month,$day,$year) - date('Z');
  2782. if($max_time <= $day_time)
  2783. return;
  2784. $currentpath .= '/';
  2785. }
  2786. else
  2787. $currentpath = '';
  2788. $dir = $basepath.$currentpath;
  2789. $directory = dir($dir);
  2790. while($entry = @$directory->read())
  2791. {
  2792. if($_FANWE['clear_image_count'] >= 50)
  2793. break;
  2794. if($entry != '.' && $entry != '..')
  2795. {
  2796. $filename = $dir.$entry;
  2797. if(is_dir($filename))
  2798. {
  2799. clearTempImage($filename);
  2800. @rmdir($filename);
  2801. }
  2802. if(is_file($filename))
  2803. {
  2804. removeFile($filename);
  2805. $_FANWE['clear_image_count']++;
  2806. }
  2807. }
  2808. }
  2809. $directory->close();
  2810. }
  2811. ?>