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.

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