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.

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