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.

1327 lines
31 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 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='',$size=150)
  573. {
  574. return 'data:image/png;base64,'.base64_encode(\QrCode::format('png')->encoding('UTF-8')->size($size)->margin(0)->errorCorrection('H')->generate($url));
  575. }
  576. //根据栏目id获取栏目信息
  577. function typeinfo($typeid)
  578. {
  579. return db("arctype")->where("id=$typeid")->find();
  580. }
  581. //根据栏目id获取该栏目下文章/商品的数量
  582. function catarcnum($typeid, $modelname='article')
  583. {
  584. $map['typeid']=$typeid;
  585. return \DB::table($modelname)->where($map)->count('id');
  586. }
  587. //根据Tag id获取该Tag标签下文章的数量
  588. function tagarcnum($tagid)
  589. {
  590. $taglist = \DB::table("taglist");
  591. if(!empty($tagid)){$map['tid']=$tagid; $taglist = $taglist->where($map);}
  592. return $taglist->count();
  593. }
  594. //判断是否是图片格式,是返回true
  595. function imgmatch($url)
  596. {
  597. $info = pathinfo($url);
  598. if (isset($info['extension']))
  599. {
  600. if (($info['extension'] == 'jpg') || ($info['extension'] == 'jpeg') || ($info['extension'] == 'gif') || ($info['extension'] == 'png'))
  601. {
  602. return true;
  603. }
  604. else
  605. {
  606. return false;
  607. }
  608. }
  609. }
  610. //将栏目列表生成数组
  611. function get_category($modelname, $parent_id=0, $pad=0)
  612. {
  613. $arr = array();
  614. $temp = \DB::table($modelname)->where('pid', $parent_id);
  615. if(get_table_columns($modelname, 'listorder'))
  616. {
  617. $temp = $temp->orderBy('listorder', 'asc');
  618. }
  619. else
  620. {
  621. $temp = $temp->orderBy('id', 'asc');
  622. }
  623. $temp = $temp->get();
  624. $cats = object_to_array($temp);
  625. if($cats)
  626. {
  627. foreach($cats as $row)//循环数组
  628. {
  629. $row['deep'] = $pad;
  630. if($child = get_category($modelname, $row["id"], $pad+1))//如果子级不为空
  631. {
  632. $row['child'] = $child;
  633. }
  634. $arr[] = $row;
  635. }
  636. return $arr;
  637. }
  638. }
  639. function category_tree($list,$pid=0)
  640. {
  641. global $temp;
  642. if(!empty($list))
  643. {
  644. foreach($list as $v)
  645. {
  646. $temp[] = array("id"=>$v['id'],"deep"=>$v['deep'],"name"=>$v['name'],"pid"=>$v['pid']);
  647. //echo $v['id'];
  648. if(array_key_exists("child",$v))
  649. {
  650. category_tree($v['child'],$v['pid']);
  651. }
  652. }
  653. }
  654. return $temp;
  655. }
  656. //递归获取面包屑导航
  657. function get_cat_path($cat,$table='arctype',$type='list')
  658. {
  659. global $temp;
  660. $row = \DB::table($table)->select('name','pid','id')->where('id',$cat)->first();
  661. $temp = '<a href="'.get_front_url(array("catid"=>$row->id,"type"=>$type)).'">'.$row->name."</a> > ".$temp;
  662. if($row->pid<>0)
  663. {
  664. get_cat_path($row->pid, $table, $type);
  665. }
  666. return $temp;
  667. }
  668. //根据文章id获得tag,$id表示文章id,$tagid表示要排除的标签id
  669. function taglist($id,$tagid=0)
  670. {
  671. $tags="";
  672. if($tagid!=0)
  673. {
  674. $Taglist = \DB::table("taglist")->where('aid',$id)->where('tid', '<>', $tagid)->get();
  675. }
  676. else
  677. {
  678. $Taglist = \DB::table("taglist")->where('aid',$id)->get();
  679. }
  680. foreach($Taglist as $row)
  681. {
  682. if($tags==""){$tags='id='.$row->tid;}else{$tags=$tags.' or id='.$row->tid;}
  683. }
  684. if($tags!=""){return object_to_array(\DB::table("tagindex")->whereRaw(\DB::raw($tags))->get());}
  685. }
  686. //读取动态配置
  687. function sysconfig($varname='')
  688. {
  689. $sysconfig = cache('sysconfig');
  690. $res = '';
  691. if(empty($sysconfig))
  692. {
  693. cache()->forget('sysconfig');
  694. $sysconfig = \App\Http\Model\Sysconfig::orderBy('id')->select('varname', 'value')->get()->toArray();
  695. cache(['sysconfig' => $sysconfig], \Carbon\Carbon::now()->addMinutes(86400));
  696. }
  697. if($varname != '')
  698. {
  699. foreach($sysconfig as $row)
  700. {
  701. if($varname == $row['varname'])
  702. {
  703. $res = $row['value'];
  704. }
  705. }
  706. }
  707. else
  708. {
  709. $res = $sysconfig;
  710. }
  711. return $res;
  712. }
  713. //获取https的get请求结果
  714. function curl_post($c_url,$data='')
  715. {
  716. $curl = curl_init(); // 启动一个CURL会话
  717. curl_setopt($curl, CURLOPT_URL, $c_url); // 要访问的地址
  718. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  719. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
  720. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  721. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  722. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  723. if($data)
  724. {
  725. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  726. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  727. }
  728. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  729. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  730. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  731. $tmpInfo = curl_exec($curl); // 执行操作
  732. if (curl_errno($curl))
  733. {
  734. echo 'Errno'.curl_error($curl);//捕抓异常
  735. }
  736. curl_close($curl); // 关闭CURL会话
  737. return $tmpInfo; // 返回数据
  738. }
  739. //通过file_get_content获取远程数据
  740. function http_request_post($url,$data,$type='POST')
  741. {
  742. $content = http_build_query($data);
  743. $content_length = strlen($content);
  744. $options = array(
  745. 'http' => array(
  746. 'method' => $type,
  747. 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-length: $content_length\r\n",
  748. 'content' => $content
  749. )
  750. );
  751. $result = file_get_contents($url,false,stream_context_create($options));
  752. return $result;
  753. }
  754. function json_to_array($json)
  755. {
  756. return json_decode($json,true);
  757. }
  758. function imageResize($url, $width, $height)
  759. {
  760. header('Content-type: image/jpeg');
  761. list($width_orig, $height_orig) = getimagesize($url);
  762. $ratio_orig = $width_orig/$height_orig;
  763. if($width/$height > $ratio_orig)
  764. {
  765. $width = $height*$ratio_orig;
  766. }
  767. else
  768. {
  769. $height = $width/$ratio_orig;
  770. }
  771. // This resamples the image
  772. $image_p = imagecreatetruecolor($width, $height);
  773. $image = imagecreatefromjpeg($url);
  774. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  775. // Output the image
  776. imagejpeg($image_p, null, 100);
  777. }
  778. /**
  779. * 为文章内容添加内敛, 排除alt title <a></a>直接的字符替换
  780. *
  781. * @param string $body
  782. * @return string
  783. */
  784. function ReplaceKeyword($body)
  785. {
  786. $karr = $kaarr = array();
  787. //暂时屏蔽超链接
  788. $body = preg_replace("#(<a(.*))(>)(.*)(<)(\/a>)#isU", '\\1-]-\\4-[-\\6', $body);
  789. if(cache("keywordlist")){$posts=cache("keywordlist");}else{$posts = object_to_array(DB::table("keyword")->get());cache(["keywordlist"=>$posts], \Carbon\Carbon::now()->addMinutes(2592000));}
  790. foreach($posts as $row)
  791. {
  792. $keyword = trim($row['keyword']);
  793. $key_url=trim($row['rpurl']);
  794. $karr[] = $keyword;
  795. $kaarr[] = "<a href='$key_url' target='_blank'><u>$keyword</u></a>";
  796. }
  797. asort($karr);
  798. $body = str_replace('\"', '"', $body);
  799. foreach ($karr as $key => $word)
  800. {
  801. $body = preg_replace("#".preg_quote($word)."#isU", $kaarr[$key], $body, 1);
  802. }
  803. //恢复超链接
  804. return preg_replace("#(<a(.*))-\]-(.*)-\[-(\/a>)#isU", '\\1>\\3<\\4', $body);
  805. }
  806. /**
  807. * 删除非站内链接
  808. *
  809. * @access public
  810. * @param string $body 内容
  811. * @param array $allow_urls 允许的超链接
  812. * @return string
  813. */
  814. function replacelinks($body, $allow_urls=array())
  815. {
  816. $host_rule = join('|', $allow_urls);
  817. $host_rule = preg_replace("#[\n\r]#", '', $host_rule);
  818. $host_rule = str_replace('.', "\\.", $host_rule);
  819. $host_rule = str_replace('/', "\\/", $host_rule);
  820. $arr = '';
  821. preg_match_all("#<a([^>]*)>(.*)<\/a>#iU", $body, $arr);
  822. if( is_array($arr[0]) )
  823. {
  824. $rparr = array();
  825. $tgarr = array();
  826. foreach($arr[0] as $i=>$v)
  827. {
  828. if( $host_rule != '' && preg_match('#'.$host_rule.'#i', $arr[1][$i]) )
  829. {
  830. continue;
  831. }
  832. else
  833. {
  834. $rparr[] = $v;
  835. $tgarr[] = $arr[2][$i];
  836. }
  837. }
  838. if( !empty($rparr) )
  839. {
  840. $body = str_replace($rparr, $tgarr, $body);
  841. }
  842. }
  843. $arr = $rparr = $tgarr = '';
  844. return $body;
  845. }
  846. /**
  847. * 获取文本中首张图片地址
  848. * @param [type] $content
  849. * @return [type]
  850. */
  851. function getfirstpic($content)
  852. {
  853. if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches))
  854. {
  855. $file=$_SERVER['DOCUMENT_ROOT'].$matches[3][0];
  856. if(file_exists($file))
  857. {
  858. return $matches[3][0];
  859. }
  860. }
  861. else
  862. {
  863. return false;
  864. }
  865. }
  866. /**
  867. * 更新配置文件 / 更新系统缓存
  868. */
  869. function updateconfig()
  870. {
  871. $str_tmp="<?php\r\n"; //得到php的起始符。$str_tmp将累加
  872. $str_end="?>"; //php结束符
  873. $str_tmp.="//全站配置文件\r\n";
  874. $param = db("sysconfig")->select();
  875. foreach($param as $row)
  876. {
  877. $str_tmp .= 'define("'.$row['varname'].'","'.$row['value'].'"); // '.$row['info']."\r\n";
  878. }
  879. $str_tmp .= $str_end; //加入结束符
  880. //保存文件
  881. $sf = APP_PATH."common.inc.php"; //文件名
  882. $fp = fopen($sf,"w"); //写方式打开文件
  883. fwrite($fp,$str_tmp); //存入内容
  884. fclose($fp); //关闭文件
  885. }
  886. //清空文件夹
  887. function dir_delete($dir)
  888. {
  889. //$dir = dir_path($dir);
  890. if (!is_dir($dir)) return FALSE;
  891. $handle = opendir($dir); //打开目录
  892. while(($file = readdir($handle)) !== false)
  893. {
  894. if($file == '.' || $file == '..')continue;
  895. $d = $dir.DIRECTORY_SEPARATOR.$file;
  896. is_dir($d) ? dir_delete($d) : @unlink($d);
  897. }
  898. closedir($handle);
  899. return @rmdir($dir);
  900. }
  901. //对象转数组
  902. function object_to_array($object, $get=0)
  903. {
  904. $res = [];
  905. if(empty($object)) {
  906. return $res;
  907. }
  908. if ($get==0) {
  909. foreach ($object as $key=>$value) {
  910. $res[$key] = (array)$value;
  911. }
  912. }
  913. elseif ($get==1) {
  914. $res = (array)$object;
  915. }
  916. return $res;
  917. }
  918. /**
  919. * 操作错误跳转的快捷方法
  920. * @access protected
  921. * @param string $msg 错误信息
  922. * @param string $url 页面跳转地址
  923. * @param mixed $time 当数字时指定跳转时间
  924. * @return void
  925. */
  926. function error_jump($msg='', $url='', $time=3)
  927. {
  928. if ($url=='' && isset($_SERVER["HTTP_REFERER"]))
  929. {
  930. $url = $_SERVER["HTTP_REFERER"];
  931. }
  932. if(!headers_sent())
  933. {
  934. header("Location:".route('admin_jump')."?error=$msg&url=$url&time=$time");
  935. exit();
  936. }
  937. else
  938. {
  939. $str = "<meta http-equiv='Refresh' content='URL=".route('admin_jump')."?error=$msg&url=$url&time=$time"."'>";
  940. exit($str);
  941. }
  942. }
  943. /**
  944. * 操作成功跳转的快捷方法
  945. * @access protected
  946. * @param string $msg 提示信息
  947. * @param string $url 页面跳转地址
  948. * @param mixed $time 当数字时指定跳转时间
  949. * @return void
  950. */
  951. function success_jump($msg='', $url='', $time=1)
  952. {
  953. if ($url=='' && isset($_SERVER["HTTP_REFERER"]))
  954. {
  955. $url = $_SERVER["HTTP_REFERER"];
  956. }
  957. if(!headers_sent())
  958. {
  959. header("Location:".route('admin_jump')."?message=$msg&url=$url&time=$time");
  960. exit();
  961. }
  962. else
  963. {
  964. $str = "<meta http-equiv='Refresh' content='URL=".route('admin_jump')."?message=$msg&url=$url&time=$time"."'>";
  965. exit($str);
  966. }
  967. }
  968. //获取表所有字段
  969. function get_table_columns($table, $field='')
  970. {
  971. $res = \Illuminate\Support\Facades\Schema::getColumnListing($table);
  972. if($field != '')
  973. {
  974. //判断字段是否在表里面
  975. if(in_array($field, $res))
  976. {
  977. return true;
  978. }
  979. else
  980. {
  981. return false;
  982. }
  983. }
  984. return $res;
  985. }
  986. //获取http(s)://+域名
  987. function http_host($flag=true)
  988. {
  989. $res = '';
  990. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  991. if($flag)
  992. {
  993. $res = "$protocol$_SERVER[HTTP_HOST]";
  994. }
  995. else
  996. {
  997. $res = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //完整网址
  998. }
  999. return $res;
  1000. }
  1001. /**
  1002. * 获取数据属性
  1003. * @param $dataModel 数据模型
  1004. * @param $data 数据
  1005. * @return array
  1006. */
  1007. function getDataAttr($dataModel,$data = [])
  1008. {
  1009. if(empty($dataModel) || empty($data))
  1010. {
  1011. return false;
  1012. }
  1013. foreach($data as $k=>$v)
  1014. {
  1015. $_method_str=ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($match) {
  1016. return strtoupper($match[1]);
  1017. }, $k));
  1018. $_method = 'get' . $_method_str . 'Attr';
  1019. if(method_exists($dataModel, $_method))
  1020. {
  1021. $tmp = $k.'_text';
  1022. $data->$tmp = $dataModel->$_method($data);
  1023. }
  1024. }
  1025. return $data;
  1026. }
  1027. /**
  1028. * 调用服务接口
  1029. * @param $name 服务类名称
  1030. * @param array $config 配置
  1031. * @return object
  1032. */
  1033. function service($name = '', $config = [])
  1034. {
  1035. static $instance = [];
  1036. $guid = $name . 'Service';
  1037. if (!isset($instance[$guid]))
  1038. {
  1039. $class = 'App\\Http\\Service\\' . ucfirst($name);
  1040. if (class_exists($class))
  1041. {
  1042. $service = new $class($config);
  1043. $instance[$guid] = $service;
  1044. }
  1045. else
  1046. {
  1047. throw new Exception('class not exists:' . $class);
  1048. }
  1049. }
  1050. return $instance[$guid];
  1051. }
  1052. /**
  1053. * 调用逻辑接口
  1054. * @param $name 逻辑类名称
  1055. * @param array $config 配置
  1056. * @return object
  1057. */
  1058. function logic($name = '', $config = [])
  1059. {
  1060. static $instance = [];
  1061. $guid = $name . 'Logic';
  1062. if (!isset($instance[$guid]))
  1063. {
  1064. $class = 'App\\Http\\Logic\\' . ucfirst($name) . 'Logic';
  1065. if (class_exists($class))
  1066. {
  1067. $logic = new $class($config);
  1068. $instance[$guid] = $logic;
  1069. }
  1070. else
  1071. {
  1072. throw new Exception('class not exists:' . $class);
  1073. }
  1074. }
  1075. return $instance[$guid];
  1076. }
  1077. /**
  1078. * 实例化(分层)模型
  1079. * @param $name 模型类名称
  1080. * @param array $config 配置
  1081. * @return object
  1082. */
  1083. function model($name = '', $config = [])
  1084. {
  1085. static $instance = [];
  1086. $guid = $name . 'Model';
  1087. if (!isset($instance[$guid]))
  1088. {
  1089. $class = '\\App\\Http\\Model\\' . ucfirst($name);
  1090. if (class_exists($class))
  1091. {
  1092. $model = new $class($config);
  1093. $instance[$guid] = $model;
  1094. }
  1095. else
  1096. {
  1097. throw new Exception('class not exists:' . $class);
  1098. }
  1099. }
  1100. return $instance[$guid];
  1101. }
  1102. //判断是否为数字
  1103. function checkIsNumber($data)
  1104. {
  1105. if($data == '' || $data == null)
  1106. {
  1107. return false;
  1108. }
  1109. if(preg_match("/^\d*$/",$data))
  1110. {
  1111. return true;
  1112. }
  1113. return false;
  1114. }
  1115. // 车牌号校验
  1116. function isCarLicense($license)
  1117. {
  1118. if (empty($license)) {
  1119. return false;
  1120. }
  1121. $regular = "/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/u";
  1122. preg_match($regular, $license, $match);
  1123. if (isset($match[0])) {
  1124. return true;
  1125. }
  1126. return false;
  1127. }
  1128. /**
  1129. * 格式化文件大小显示
  1130. *
  1131. * @param int $size
  1132. * @return string
  1133. */
  1134. function format_bytes($size)
  1135. {
  1136. $prec = 3;
  1137. $size = round(abs($size));
  1138. $units = array(
  1139. 0 => " B ",
  1140. 1 => " KB",
  1141. 2 => " MB",
  1142. 3 => " GB",
  1143. 4 => " TB"
  1144. );
  1145. if ($size == 0)
  1146. {
  1147. return str_repeat(" ", $prec) . "0$units[0]";
  1148. }
  1149. $unit = min(4, floor(log($size) / log(2) / 10));
  1150. $size = $size * pow(2, -10 * $unit);
  1151. $digi = $prec - 1 - floor(log($size) / log(10));
  1152. $size = round($size * pow(10, $digi)) * pow(10, -$digi);
  1153. return $size . $units[$unit];
  1154. }