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.

1154 lines
27 KiB

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