You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

790 lines
20 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. // 公共函数文件
  3. //定义常量
  4. define('FLADMIN', '/fladmin'); // 后台模块,首字母最好大写
  5. define('CMS_VERSION', '1.1.0'); // 版本号
  6. function dataList($modelname, $map = '', $orderby = '', $field = '*', $listRows = 15)
  7. {
  8. return db($modelname)->where($map)->field($field)->order($orderby)->limit($limit)->select();
  9. }
  10. //pc前台栏目、标签、内容页面地址生成
  11. function get_front_url($param='')
  12. {
  13. $url = '';
  14. if($param['type'] == 'list')
  15. {
  16. //列表页
  17. $url .= '/cat'.$param['catid'].'.html';
  18. }
  19. else if($param['type'] == 'content')
  20. {
  21. //内容页
  22. $url .= '/cat'.$param['catid'].'/id'.$param['id'].'.html';
  23. }
  24. else if($param['type'] == 'tags')
  25. {
  26. //tags页面
  27. $url .= '/tag'.$param['tagid'].'.html';
  28. }
  29. else if($param['type'] == 'page')
  30. {
  31. //单页面
  32. $url .= '/'.$param['pagename'].'.html';
  33. }
  34. return $url;
  35. }
  36. //wap前台栏目、标签、内容页面地址生成
  37. function murl(array $param)
  38. {
  39. $url = '';
  40. if($param['type'] == 'list')
  41. {
  42. //列表页
  43. $url .= '/cat'.$param['catid'].'.html';
  44. }
  45. else if($param['type'] == 'content')
  46. {
  47. //内容页
  48. $url .= '/cat'.$param['catid'].'/id'.$param['id'].'.html';
  49. }
  50. else if($param['type'] == 'tags')
  51. {
  52. //tags页面
  53. $url .= '/tag'.$param['tagid'].'.html';
  54. }
  55. else if($param['type'] == 'page')
  56. {
  57. //单页面
  58. $url .= '/'.$param['pagename'].'.html';
  59. }
  60. return $url;
  61. }
  62. /**
  63. * 获取文章列表
  64. * @param int $tuijian=0 推荐等级
  65. * @param int $typeid=0 分类
  66. * @param int $image=1 是否存在图片
  67. * @param int $row=10 需要返回的数量
  68. * @param string $orderby='id desc' 排序,默认id降序,随机rand()
  69. * @param string $limit='0,10' 如果存在$row,$limit就无效
  70. * @return string
  71. */
  72. function arclist(array $param)
  73. {
  74. $map=array();
  75. if(isset($param['tuijian'])){$map['tuijian']=$param['tuijian'];}
  76. if(isset($param['typeid'])){$map['typeid']=$param['typeid'];}
  77. if(isset($param['image'])){$map['litpic']=array('NEQ','');}
  78. if(isset($param['limit'])){$limit=$param['limit'];}else{if(isset($param['row'])){$limit="0,".$param['row'];}else{$limit='0,'.cms_pagesize;}}
  79. if(isset($param['orderby'])){$orderby=$param['orderby'];}else{$orderby='id desc';}
  80. if(isset($param['sql']))
  81. {
  82. $Artlist = db("article")->field('body',true)->where($param['sql'])->order($orderby)->limit($limit)->select();
  83. }
  84. else
  85. {
  86. $Artlist = db("article")->field('body',true)->where($map)->order($orderby)->limit($limit)->select();
  87. }
  88. return $Artlist;
  89. }
  90. /**
  91. * 获取tag标签列表
  92. * @param int $row=10 需要返回的数量,如果存在$limit,$row就无效
  93. * @param string $orderby='id desc' 排序,默认id降序,随机rand()
  94. * @param string $limit='0,10'
  95. * @return string
  96. */
  97. function tagslist($param="")
  98. {
  99. $orderby=$limit="";
  100. if(isset($param['limit'])){$limit=$param['limit'];}else{if(isset($param['row'])){$limit=$param['row'];}}
  101. if(isset($param['orderby'])){$orderby=$param['orderby'];}else{$orderby='id desc';}
  102. return db("tagindex")->field('content',true)->select();
  103. }
  104. /**
  105. * 获取友情链接
  106. * @param string $orderby='id desc' 排序,默认id降序,随机rand()
  107. * @param int||string $limit='0,10'
  108. * @return string
  109. */
  110. function flinklist($param="")
  111. {
  112. if(isset($param['row'])){$limit=$param['row'];}else{$limit="";}
  113. if(isset($param['orderby'])){$orderby=$param['orderby'];}else{$orderby='id desc';}
  114. return db("friendlink")->order($orderby)->limit($limit)->select();
  115. }
  116. /**
  117. * 获取文章上一篇,下一篇id
  118. * @param $param['aid'] 当前文章id
  119. * @param $param['typeid'] 当前文章typeid
  120. * @param string $type 获取类型
  121. * pre:上一篇 next:下一篇
  122. * @return array
  123. */
  124. function get_article_prenext(array $param)
  125. {
  126. $sql = $typeid = $res = '';
  127. $sql='id='.$param["aid"];
  128. if(!empty($param["typeid"]))
  129. {
  130. $typeid = $param["typeid"];
  131. }
  132. else
  133. {
  134. $Article = db("article")->field('typeid')->where($sql)->find();
  135. $typeid = $Article["typeid"];
  136. }
  137. if($param["type"]=='pre')
  138. {
  139. $sql='id<'.$param['aid'].' and typeid='.$typeid;
  140. $res = db("article")->field('id,typeid,title')->where($sql)->order('id desc')->find();
  141. }
  142. else if($param["type"]=='next')
  143. {
  144. $sql='id>'.$param['aid'].' and typeid='.$typeid;
  145. $res = db("article")->field('id,typeid,title')->where($sql)->order('id asc')->find();
  146. }
  147. return $res;
  148. }
  149. /**
  150. * 获取列表分页
  151. * @param $param['pagenow'] 当前第几页
  152. * @param $param['counts'] 总条数
  153. * @param $param['pagesize'] 每页显示数量
  154. * @param $param['catid'] 栏目id
  155. * @param $param['offset'] 偏移量
  156. * @return array
  157. */
  158. function get_listnav(array $param)
  159. {
  160. $catid=$param["catid"];
  161. $pagenow=$param["pagenow"];
  162. $prepage = $nextpage = '';
  163. $prepagenum = $pagenow-1;
  164. $nextpagenum = $pagenow+1;
  165. $counts=$param["counts"];
  166. $totalpage=get_totalpage(array("counts"=>$counts,"pagesize"=>$param["pagesize"]));
  167. if($totalpage<=1 && $counts>0)
  168. {
  169. return "<li><span class=\"pageinfo\">共1页/".$counts."条记录</span></li>";
  170. }
  171. if($counts == 0)
  172. {
  173. return "<li><span class=\"pageinfo\">共0页/".$counts."条记录</span></li>";
  174. }
  175. $maininfo = "<li><span class=\"pageinfo\">共".$totalpage."".$counts."条</span></li>";
  176. if(!empty($param["urltype"]))
  177. {
  178. $urltype = $param["urltype"];
  179. }
  180. else
  181. {
  182. $urltype = 'cat';
  183. }
  184. //获得上一页和下一页的链接
  185. if($pagenow != 1)
  186. {
  187. if($pagenow == 2)
  188. {
  189. $prepage.="<li><a href='/".$urltype.$catid.".html'>上一页</a></li>";
  190. }
  191. else
  192. {
  193. $prepage.="<li><a href='/".$urltype.$catid."/$prepagenum.html'>上一页</a></li>";
  194. }
  195. $indexpage="<li><a href='/".$urltype.$catid.".html'>首页</a></li>";
  196. }
  197. else
  198. {
  199. $indexpage="<li><a>首页</a></li>";
  200. }
  201. if($pagenow!=$totalpage && $totalpage>1)
  202. {
  203. $nextpage.="<li><a href='/".$urltype.$catid."/$nextpagenum.html'>下一页</a></li>";
  204. $endpage="<li><a href='/".$urltype.$catid."/$totalpage.html'>末页</a></li>";
  205. }
  206. else
  207. {
  208. $endpage="<li><a>末页</a></li>";
  209. }
  210. //获得数字链接
  211. $listdd="";
  212. if(!empty($param["offset"])){$offset=$param["offset"];}else{$offset=2;}
  213. $minnum=$pagenow-$offset;
  214. $maxnum=$pagenow+$offset;
  215. if($minnum<1){$minnum=1;}
  216. if($maxnum>$totalpage){$maxnum=$totalpage;}
  217. for($minnum;$minnum<=$maxnum;$minnum++)
  218. {
  219. if($minnum==$pagenow)
  220. {
  221. $listdd.= "<li class=\"thisclass\"><a>$minnum</a></li>";
  222. }
  223. else
  224. {
  225. if($minnum==1)
  226. {
  227. $listdd.="<li><a href='/".$urltype.$catid.".html'>$minnum</a></li>";
  228. }
  229. else
  230. {
  231. $listdd.="<li><a href='/".$urltype.$catid."/$minnum.html'>$minnum</a></li>";
  232. }
  233. }
  234. }
  235. $plist = '';
  236. $plist .= $indexpage; //首页链接
  237. $plist .= $prepage; //上一页链接
  238. $plist .= $listdd; //数字链接
  239. $plist .= $nextpage; //下一页链接
  240. $plist .= $endpage; //末页链接
  241. $plist .= $maininfo;
  242. return $plist;
  243. }
  244. /**
  245. * 获取列表上一页、下一页
  246. * @param $param['pagenow'] 当前第几页
  247. * @param $param['counts'] 总条数
  248. * @param $param['pagesize'] 每页显示数量
  249. * @param $param['catid'] 栏目id
  250. * @return array
  251. */
  252. function get_prenext(array $param)
  253. {
  254. $counts=$param['counts'];
  255. $pagenow=$param["pagenow"];
  256. $prepage = $nextpage = '';
  257. $prepagenum = $pagenow-1;
  258. $nextpagenum = $pagenow+1;
  259. $cat=$param['catid'];
  260. if(!empty($param["urltype"]))
  261. {
  262. $urltype = $param["urltype"];
  263. }
  264. else
  265. {
  266. $urltype = 'cat';
  267. }
  268. $totalpage=get_totalpage(array("counts"=>$counts,"pagesize"=>$param["pagesize"]));
  269. //获取上一页
  270. if($pagenow == 1)
  271. {
  272. }
  273. elseif($pagenow==2)
  274. {
  275. $prepage='<a class="prep" href="/'.$urltype.$cat.'.html">上一页</a> &nbsp; ';
  276. }
  277. else
  278. {
  279. $prepage='<a class="prep" href="/'.$urltype.$cat.'/'.$prepagenum.'.html">上一页</a> &nbsp; ';
  280. }
  281. //获取下一页
  282. if($pagenow<$totalpage && $totalpage>1)
  283. {
  284. $nextpage='<a class="nextp" href="/'.$urltype.$cat.'/'.$nextpagenum.'.html">下一页</a>';
  285. }
  286. $plist = '';
  287. $plist .= $indexpage; //首页链接
  288. $plist .= $prepage; //上一页链接
  289. $plist .= $nextpage; //下一页链接
  290. return $plist;
  291. }
  292. /**
  293. * 获取分页列表
  294. * @access public
  295. * @param string $list_len 列表宽度
  296. * @param string $list_len 列表样式
  297. * @return string
  298. */
  299. function pagenav(array $param)
  300. {
  301. $prepage = $nextpage = '';
  302. $prepagenum = $param["pagenow"]-1;
  303. $nextpagenum = $param["pagenow"]+1;
  304. if(!empty($param['tuijian'])){$map['tuijian']=$param['tuijian'];}
  305. if(!empty($param['typeid'])){$map['typeid']=$param['typeid'];}
  306. if(!empty($param['image'])){$map['litpic']=array('NEQ','');}
  307. if(!empty($param['row'])){$limit="0,".$param['row'];}else{if(!empty($param['limit'])){$limit=$param['limit'];}else{$limit='0,8';}}
  308. if(!empty($param['orderby'])){$orderby=$param['orderby'];}else{$orderby='id desc';}
  309. return db("article")->field('body',true)->where($map)->order($orderby)->limit($limit)->select();
  310. }
  311. //根据总数与每页条数,获取总页数
  312. function get_totalpage(array $param)
  313. {
  314. if(!empty($param['pagesize'] || $param['pagesize']==0)){$pagesize=$param["pagesize"];}else{$pagesize=CMS_PAGESIZE;}
  315. $counts=$param["counts"];
  316. //取总数据量除以每页数的余数
  317. if($counts % $pagesize)
  318. {
  319. $totalpage = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  320. }
  321. else
  322. {
  323. $totalpage = $counts/$pagesize;
  324. }
  325. return $totalpage;
  326. }
  327. /**
  328. * 获得当前的页面文件的url
  329. * @access public
  330. * @return string
  331. */
  332. function GetCurUrl()
  333. {
  334. if(!empty($_SERVER['REQUEST_URI']))
  335. {
  336. $nowurl = $_SERVER['REQUEST_URI'];
  337. $nowurls = explode('?', $nowurl);
  338. $nowurl = $nowurls[0];
  339. }
  340. else
  341. {
  342. $nowurl = $_SERVER['PHP_SELF'];
  343. }
  344. return $nowurl;
  345. }
  346. /**
  347. * 获取单页列表
  348. * @param int $row=8 需要返回的数量
  349. * @param string $orderby='id desc' 排序,默认id降序,随机rand()
  350. * @param string $limit='0,8' 如果存在$row,$limit就无效
  351. * @return string
  352. */
  353. function pagelist($param="")
  354. {
  355. if(!empty($param['row'])){$limit="0,".$param['row'];}else{if(!empty($param['limit'])){$limit=$param['limit'];}else{$limit='0,8';}}
  356. if(!empty($param['orderby'])){$orderby=$param['orderby'];}else{$orderby='id desc';}
  357. return db("page")->field('body',true)->order($orderby)->limit($limit)->select();
  358. }
  359. /**
  360. * 截取中文字符串
  361. * @param string $string 中文字符串
  362. * @param int $sublen 截取长度
  363. * @param int $start 开始长度 默认0
  364. * @param string $code 编码方式 默认UTF-8
  365. * @param string $omitted 末尾省略符 默认...
  366. * @return string
  367. */
  368. function cut_str($string, $sublen=250, $omitted = '', $start=0, $code='UTF-8')
  369. {
  370. $string = strip_tags($string);
  371. $string = str_replace(" ","",$string);
  372. $string = mb_strcut($string,$start,$sublen,$code);
  373. $string.= $omitted;
  374. return $string;
  375. }
  376. //PhpAnalysis获取中文分词
  377. function get_keywords($keyword)
  378. {
  379. Vendor('phpAnalysis.phpAnalysis');
  380. //import("Vendor.phpAnalysis.phpAnalysis");
  381. //初始化类
  382. PhpAnalysis::$loadInit = false;
  383. $pa = new PhpAnalysis('utf-8', 'utf-8', false);
  384. //载入词典
  385. $pa->LoadDict();
  386. //执行分词
  387. $pa->SetSource($keyword);
  388. $pa->StartAnalysis( false );
  389. $keywords = $pa->GetFinallyResult(',');
  390. return ltrim($keywords, ",");
  391. }
  392. //获取二维码
  393. function get_erweima($url="")
  394. {
  395. Vendor('phpqrcode.qrlib');
  396. $url = str_replace("%26","&",$url);
  397. $url = str_replace("%3F","?",$url);
  398. $url = str_replace("%3D","=",$url);
  399. return QRcode::png($url, false, "H", 6);
  400. }
  401. //根据栏目id获取栏目信息
  402. function typeinfo($typeid)
  403. {
  404. return db("arctype")->where("id=$typeid")->find();
  405. }
  406. //根据栏目id获取该栏目下文章/商品的数量
  407. function catarcnum($typeid,$modelname='article')
  408. {
  409. $map['typeid']=$typeid;
  410. return db($modelname)->where($map)->count('id');
  411. }
  412. //根据Tag id获取该Tag标签下文章的数量
  413. function tagarcnum($tagid)
  414. {
  415. if(!empty($tagid)){$map['tid']=$tagid;}
  416. return db("taglist")->where($map)->count();
  417. }
  418. //判断是否是图片格式,是返回true
  419. function imgmatch($url)
  420. {
  421. $info = pathinfo($url);
  422. if (isset($info['extension']))
  423. {
  424. if (($info['extension'] == 'jpg') || ($info['extension'] == 'jpeg') || ($info['extension'] == 'gif') || ($info['extension'] == 'png'))
  425. {
  426. return true;
  427. }
  428. else
  429. {
  430. return false;
  431. }
  432. }
  433. }
  434. //将栏目列表生成数组
  435. function get_category($modelname,$parent_id=0,$pad=0)
  436. {
  437. $arr=array();
  438. $cats = db($modelname)->where("reid=$parent_id")->order('id asc')->select();
  439. if($cats)
  440. {
  441. foreach($cats as $row)//循环数组
  442. {
  443. $row['deep'] = $pad;
  444. if(get_category($modelname,$row["id"]))//如果子级不为空
  445. {
  446. $row['child'] = get_category($modelname,$row["id"],$pad+1);
  447. }
  448. $arr[] = $row;
  449. }
  450. return $arr;
  451. }
  452. }
  453. function tree($list,$pid=0)
  454. {
  455. global $temp;
  456. if(!empty($list))
  457. {
  458. foreach($list as $v)
  459. {
  460. $temp[] = array("id"=>$v['id'],"deep"=>$v['deep'],"typename"=>$v['typename'],"reid"=>$v['reid'],"typedir"=>$v['typedir'],"addtime"=>$v['addtime']);
  461. //echo $v['id'];
  462. if(array_key_exists("child",$v))
  463. {
  464. tree($v['child'],$v['reid']);
  465. }
  466. }
  467. }
  468. return $temp;
  469. }
  470. //递归获取面包屑导航
  471. function get_cat_path($cat)
  472. {
  473. global $temp;
  474. $row = db("arctype")->field('typename,reid,id')->where("id=$cat")->find();
  475. $temp = '<a href="'.cms_basehost.'/cat'.$row["id"].'.html">'.$row["typename"]."</a> > ".$temp;
  476. if($row["reid"]<>0)
  477. {
  478. get_cat_path($row["reid"]);
  479. }
  480. return $temp;
  481. }
  482. //根据文章id获得tag,$id表示文章id,$tagid表示要排除的标签id
  483. function taglist($id,$tagid=0)
  484. {
  485. $tags="";
  486. if($tagid!=0)
  487. {
  488. $Taglist = db("taglist")->where("aid=$id and tid<>$tagid")->select();
  489. }
  490. else
  491. {
  492. $Taglist = db("taglist")->where("aid=$id")->select();
  493. }
  494. foreach($Taglist as $row)
  495. {
  496. if($tags==""){$tags='id='.$row['tid'];}else{$tags=$tags.' or id='.$row['tid'];}
  497. }
  498. if($tags!=""){return db("tagindex")->where($tags)->select();}
  499. }
  500. //获取https的get请求结果
  501. function get_curl_data($c_url,$data='')
  502. {
  503. $curl = curl_init(); // 启动一个CURL会话
  504. curl_setopt($curl, CURLOPT_URL, $c_url); // 要访问的地址
  505. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  506. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
  507. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  508. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  509. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  510. if($data)
  511. {
  512. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  513. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  514. }
  515. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  516. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  517. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  518. $tmpInfo = curl_exec($curl); // 执行操作
  519. if (curl_errno($curl))
  520. {
  521. echo 'Errno'.curl_error($curl);//捕抓异常
  522. }
  523. curl_close($curl); // 关闭CURL会话
  524. return $tmpInfo; // 返回数据
  525. }
  526. //通过file_get_content获取远程数据
  527. function http_request_post($url,$data,$type='POST')
  528. {
  529. $content = http_build_query($data);
  530. $content_length = strlen($content);
  531. $options = array(
  532. 'http' => array(
  533. 'method' => $type,
  534. 'header' =>
  535. "Content-type: application/x-www-form-urlencoded\r\n" .
  536. "Content-length: $content_length\r\n",
  537. 'content' => $content
  538. )
  539. );
  540. $result = file_get_contents($url,false,stream_context_create($options));
  541. return $result;
  542. }
  543. function imageResize($url, $width, $height)
  544. {
  545. header('Content-type: image/jpeg');
  546. list($width_orig, $height_orig) = getimagesize($url);
  547. $ratio_orig = $width_orig/$height_orig;
  548. if($width/$height > $ratio_orig)
  549. {
  550. $width = $height*$ratio_orig;
  551. }
  552. else
  553. {
  554. $height = $width/$ratio_orig;
  555. }
  556. // This resamples the image
  557. $image_p = imagecreatetruecolor($width, $height);
  558. $image = imagecreatefromjpeg($url);
  559. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  560. // Output the image
  561. imagejpeg($image_p, null, 100);
  562. }
  563. /**
  564. * 为文章内容添加内敛, 排除alt title <a></a>直接的字符替换
  565. *
  566. * @param string $body
  567. * @return string
  568. */
  569. function ReplaceKeyword($body)
  570. {
  571. $karr = $kaarr = array();
  572. //暂时屏蔽超链接
  573. $body = preg_replace("#(<a(.*))(>)(.*)(<)(\/a>)#isU", '\\1-]-\\4-[-\\6', $body);
  574. if(cache("keywordlist")){$posts=cache("keywordlist");}else{$posts = db("Keyword")->select();cache("keywordlist",$posts,2592000);}
  575. foreach($posts as $row)
  576. {
  577. $keyword = trim($row['keyword']);
  578. $key_url=trim($row['rpurl']);
  579. $karr[] = $keyword;
  580. $kaarr[] = "<a href='$key_url' target='_blank'><u>$keyword</u></a>";
  581. }
  582. asort($karr);
  583. $body = str_replace('\"', '"', $body);
  584. foreach ($karr as $key => $word)
  585. {
  586. $body = preg_replace("#".preg_quote($word)."#isU", $kaarr[$key], $body, 1);
  587. }
  588. //恢复超链接
  589. return preg_replace("#(<a(.*))-\]-(.*)-\[-(\/a>)#isU", '\\1>\\3<\\4', $body);
  590. }
  591. /**
  592. * 删除非站内链接
  593. *
  594. * @access public
  595. * @param string $body 内容
  596. * @param array $allow_urls 允许的超链接
  597. * @return string
  598. */
  599. function replacelinks($body, $allow_urls=array())
  600. {
  601. $host_rule = join('|', $allow_urls);
  602. $host_rule = preg_replace("#[\n\r]#", '', $host_rule);
  603. $host_rule = str_replace('.', "\\.", $host_rule);
  604. $host_rule = str_replace('/', "\\/", $host_rule);
  605. $arr = '';
  606. preg_match_all("#<a([^>]*)>(.*)<\/a>#iU", $body, $arr);
  607. if( is_array($arr[0]) )
  608. {
  609. $rparr = array();
  610. $tgarr = array();
  611. foreach($arr[0] as $i=>$v)
  612. {
  613. if( $host_rule != '' && preg_match('#'.$host_rule.'#i', $arr[1][$i]) )
  614. {
  615. continue;
  616. }
  617. else
  618. {
  619. $rparr[] = $v;
  620. $tgarr[] = $arr[2][$i];
  621. }
  622. }
  623. if( !empty($rparr) )
  624. {
  625. $body = str_replace($rparr, $tgarr, $body);
  626. }
  627. }
  628. $arr = $rparr = $tgarr = '';
  629. return $body;
  630. }
  631. /**
  632. * 获取文本中首张图片地址
  633. * @param [type] $content
  634. * @return [type]
  635. */
  636. function getfirstpic($content)
  637. {
  638. if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches))
  639. {
  640. $file=$_SERVER['DOCUMENT_ROOT'].$matches[3][0];
  641. if(file_exists($file))
  642. {
  643. return $matches[3][0];
  644. }
  645. }
  646. else
  647. {
  648. return false;
  649. }
  650. }
  651. /**
  652. * 更新配置文件 / 更新系统缓存
  653. */
  654. function updateconfig()
  655. {
  656. $str_tmp="<?php\r\n"; //得到php的起始符。$str_tmp将累加
  657. $str_end="?>"; //php结束符
  658. $str_tmp.="//全站配置文件\r\n";
  659. $param = db("sysconfig")->select();
  660. foreach($param as $row)
  661. {
  662. $str_tmp .= 'define("'.$row['varname'].'","'.$row['value'].'"); // '.$row['info']."\r\n";
  663. }
  664. $str_tmp .= $str_end; //加入结束符
  665. //保存文件
  666. $sf = APP_PATH."common.inc.php"; //文件名
  667. $fp = fopen($sf,"w"); //写方式打开文件
  668. fwrite($fp,$str_tmp); //存入内容
  669. fclose($fp); //关闭文件
  670. }
  671. //清空文件夹
  672. function dir_delete($dir)
  673. {
  674. //$dir = dir_path($dir);
  675. if (!is_dir($dir)) return FALSE;
  676. $handle = opendir($dir); //打开目录
  677. while(($file = readdir($handle)) !== false)
  678. {
  679. if($file == '.' || $file == '..')continue;
  680. $d = $dir.DIRECTORY_SEPARATOR.$file;
  681. is_dir($d) ? dir_delete($d) : @unlink($d);
  682. }
  683. closedir($handle);
  684. return @rmdir($dir);
  685. }