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.

1073 lines
25 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
8 years ago
8 years ago
8 years ago
8 years ago
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);
  549. if(get_table_columns($modelname, 'listorder'))
  550. {
  551. $temp = $temp->orderBy('listorder', 'asc');
  552. }
  553. else
  554. {
  555. $temp = $temp->orderBy('id', 'asc');
  556. }
  557. $temp = $temp->get();
  558. $cats = object_to_array($temp);
  559. if($cats)
  560. {
  561. foreach($cats as $row)//循环数组
  562. {
  563. $row['deep'] = $pad;
  564. if($child = get_category($modelname, $row["id"], $pad+1))//如果子级不为空
  565. {
  566. $row['child'] = $child;
  567. }
  568. $arr[] = $row;
  569. }
  570. return $arr;
  571. }
  572. }
  573. function category_tree($list,$pid=0)
  574. {
  575. global $temp;
  576. if(!empty($list))
  577. {
  578. foreach($list as $v)
  579. {
  580. $temp[] = array("id"=>$v['id'],"deep"=>$v['deep'],"name"=>$v['name'],"pid"=>$v['pid']);
  581. //echo $v['id'];
  582. if(array_key_exists("child",$v))
  583. {
  584. category_tree($v['child'],$v['pid']);
  585. }
  586. }
  587. }
  588. return $temp;
  589. }
  590. //递归获取面包屑导航
  591. function get_cat_path($cat)
  592. {
  593. global $temp;
  594. $row = db("arctype")->field('typename,reid,id')->where("id=$cat")->find();
  595. $temp = '<a href="'.cms_basehost.'/cat'.$row["id"].'.html">'.$row["typename"]."</a> > ".$temp;
  596. if($row["reid"]<>0)
  597. {
  598. get_cat_path($row["reid"]);
  599. }
  600. return $temp;
  601. }
  602. //根据文章id获得tag,$id表示文章id,$tagid表示要排除的标签id
  603. function taglist($id,$tagid=0)
  604. {
  605. $tags="";
  606. if($tagid!=0)
  607. {
  608. $Taglist = db("taglist")->where("aid=$id and tid<>$tagid")->select();
  609. }
  610. else
  611. {
  612. $Taglist = db("taglist")->where("aid=$id")->select();
  613. }
  614. foreach($Taglist as $row)
  615. {
  616. if($tags==""){$tags='id='.$row['tid'];}else{$tags=$tags.' or id='.$row['tid'];}
  617. }
  618. if($tags!=""){return db("tagindex")->where($tags)->select();}
  619. }
  620. //读取动态配置
  621. function sysconfig($varname='')
  622. {
  623. $sysconfig = cache('sysconfig');
  624. $res = '';
  625. if(empty($sysconfig))
  626. {
  627. cache()->forget('sysconfig');
  628. $sysconfig = \App\Http\Model\Sysconfig::orderBy('id')->select('varname', 'value')->get()->toArray();
  629. cache(['sysconfig' => $sysconfig], \Carbon\Carbon::now()->addMinutes(86400));
  630. }
  631. if($varname != '')
  632. {
  633. foreach($sysconfig as $row)
  634. {
  635. if($varname == $row['varname'])
  636. {
  637. $res = $row['value'];
  638. }
  639. }
  640. }
  641. else
  642. {
  643. $res = $sysconfig;
  644. }
  645. return $res;
  646. }
  647. //获取https的get请求结果
  648. function get_curl_data($c_url,$data='')
  649. {
  650. $curl = curl_init(); // 启动一个CURL会话
  651. curl_setopt($curl, CURLOPT_URL, $c_url); // 要访问的地址
  652. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
  653. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
  654. curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
  655. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
  656. curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
  657. if($data)
  658. {
  659. curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
  660. curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
  661. }
  662. curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
  663. curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
  664. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
  665. $tmpInfo = curl_exec($curl); // 执行操作
  666. if (curl_errno($curl))
  667. {
  668. echo 'Errno'.curl_error($curl);//捕抓异常
  669. }
  670. curl_close($curl); // 关闭CURL会话
  671. return $tmpInfo; // 返回数据
  672. }
  673. //通过file_get_content获取远程数据
  674. function http_request_post($url,$data,$type='POST')
  675. {
  676. $content = http_build_query($data);
  677. $content_length = strlen($content);
  678. $options = array(
  679. 'http' => array(
  680. 'method' => $type,
  681. 'header' =>
  682. "Content-type: application/x-www-form-urlencoded\r\n" .
  683. "Content-length: $content_length\r\n",
  684. 'content' => $content
  685. )
  686. );
  687. $result = file_get_contents($url,false,stream_context_create($options));
  688. return $result;
  689. }
  690. function imageResize($url, $width, $height)
  691. {
  692. header('Content-type: image/jpeg');
  693. list($width_orig, $height_orig) = getimagesize($url);
  694. $ratio_orig = $width_orig/$height_orig;
  695. if($width/$height > $ratio_orig)
  696. {
  697. $width = $height*$ratio_orig;
  698. }
  699. else
  700. {
  701. $height = $width/$ratio_orig;
  702. }
  703. // This resamples the image
  704. $image_p = imagecreatetruecolor($width, $height);
  705. $image = imagecreatefromjpeg($url);
  706. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  707. // Output the image
  708. imagejpeg($image_p, null, 100);
  709. }
  710. /**
  711. * 为文章内容添加内敛, 排除alt title <a></a>直接的字符替换
  712. *
  713. * @param string $body
  714. * @return string
  715. */
  716. function ReplaceKeyword($body)
  717. {
  718. $karr = $kaarr = array();
  719. //暂时屏蔽超链接
  720. $body = preg_replace("#(<a(.*))(>)(.*)(<)(\/a>)#isU", '\\1-]-\\4-[-\\6', $body);
  721. if(cache("keywordlist")){$posts=cache("keywordlist");}else{$posts = object_to_array(DB::table("keyword")->get());cache(["keywordlist"=>$posts], \Carbon\Carbon::now()->addMinutes(2592000));}
  722. foreach($posts as $row)
  723. {
  724. $keyword = trim($row['keyword']);
  725. $key_url=trim($row['rpurl']);
  726. $karr[] = $keyword;
  727. $kaarr[] = "<a href='$key_url' target='_blank'><u>$keyword</u></a>";
  728. }
  729. asort($karr);
  730. $body = str_replace('\"', '"', $body);
  731. foreach ($karr as $key => $word)
  732. {
  733. $body = preg_replace("#".preg_quote($word)."#isU", $kaarr[$key], $body, 1);
  734. }
  735. //恢复超链接
  736. return preg_replace("#(<a(.*))-\]-(.*)-\[-(\/a>)#isU", '\\1>\\3<\\4', $body);
  737. }
  738. /**
  739. * 删除非站内链接
  740. *
  741. * @access public
  742. * @param string $body 内容
  743. * @param array $allow_urls 允许的超链接
  744. * @return string
  745. */
  746. function replacelinks($body, $allow_urls=array())
  747. {
  748. $host_rule = join('|', $allow_urls);
  749. $host_rule = preg_replace("#[\n\r]#", '', $host_rule);
  750. $host_rule = str_replace('.', "\\.", $host_rule);
  751. $host_rule = str_replace('/', "\\/", $host_rule);
  752. $arr = '';
  753. preg_match_all("#<a([^>]*)>(.*)<\/a>#iU", $body, $arr);
  754. if( is_array($arr[0]) )
  755. {
  756. $rparr = array();
  757. $tgarr = array();
  758. foreach($arr[0] as $i=>$v)
  759. {
  760. if( $host_rule != '' && preg_match('#'.$host_rule.'#i', $arr[1][$i]) )
  761. {
  762. continue;
  763. }
  764. else
  765. {
  766. $rparr[] = $v;
  767. $tgarr[] = $arr[2][$i];
  768. }
  769. }
  770. if( !empty($rparr) )
  771. {
  772. $body = str_replace($rparr, $tgarr, $body);
  773. }
  774. }
  775. $arr = $rparr = $tgarr = '';
  776. return $body;
  777. }
  778. /**
  779. * 获取文本中首张图片地址
  780. * @param [type] $content
  781. * @return [type]
  782. */
  783. function getfirstpic($content)
  784. {
  785. if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches))
  786. {
  787. $file=$_SERVER['DOCUMENT_ROOT'].$matches[3][0];
  788. if(file_exists($file))
  789. {
  790. return $matches[3][0];
  791. }
  792. }
  793. else
  794. {
  795. return false;
  796. }
  797. }
  798. /**
  799. * 更新配置文件 / 更新系统缓存
  800. */
  801. function updateconfig()
  802. {
  803. $str_tmp="<?php\r\n"; //得到php的起始符。$str_tmp将累加
  804. $str_end="?>"; //php结束符
  805. $str_tmp.="//全站配置文件\r\n";
  806. $param = db("sysconfig")->select();
  807. foreach($param as $row)
  808. {
  809. $str_tmp .= 'define("'.$row['varname'].'","'.$row['value'].'"); // '.$row['info']."\r\n";
  810. }
  811. $str_tmp .= $str_end; //加入结束符
  812. //保存文件
  813. $sf = APP_PATH."common.inc.php"; //文件名
  814. $fp = fopen($sf,"w"); //写方式打开文件
  815. fwrite($fp,$str_tmp); //存入内容
  816. fclose($fp); //关闭文件
  817. }
  818. //清空文件夹
  819. function dir_delete($dir)
  820. {
  821. //$dir = dir_path($dir);
  822. if (!is_dir($dir)) return FALSE;
  823. $handle = opendir($dir); //打开目录
  824. while(($file = readdir($handle)) !== false)
  825. {
  826. if($file == '.' || $file == '..')continue;
  827. $d = $dir.DIRECTORY_SEPARATOR.$file;
  828. is_dir($d) ? dir_delete($d) : @unlink($d);
  829. }
  830. closedir($handle);
  831. return @rmdir($dir);
  832. }
  833. //对象转数组
  834. function object_to_array($object, $get=0)
  835. {
  836. $res = '';
  837. if(!empty($object))
  838. {
  839. if($get==0)
  840. {
  841. foreach($object as $key=>$value)
  842. {
  843. $res[$key] = (array)$value;
  844. }
  845. }
  846. elseif($get==1)
  847. {
  848. $res = (array)$object;
  849. }
  850. }
  851. return $res;
  852. }
  853. /**
  854. * 操作错误跳转的快捷方法
  855. * @access protected
  856. * @param string $msg 错误信息
  857. * @param string $url 页面跳转地址
  858. * @param mixed $time 当数字时指定跳转时间
  859. * @return void
  860. */
  861. function error_jump($msg='', $url='', $time=3)
  862. {
  863. if ($url=='' && isset($_SERVER["HTTP_REFERER"]))
  864. {
  865. $url = $_SERVER["HTTP_REFERER"];
  866. }
  867. if(!headers_sent())
  868. {
  869. header("Location:".route('admin_jump')."?error=$msg&url=$url&time=$time");
  870. exit();
  871. }
  872. else
  873. {
  874. $str = "<meta http-equiv='Refresh' content='URL=".route('admin_jump')."?error=$msg&url=$url&time=$time"."'>";
  875. exit($str);
  876. }
  877. }
  878. /**
  879. * 操作成功跳转的快捷方法
  880. * @access protected
  881. * @param string $msg 提示信息
  882. * @param string $url 页面跳转地址
  883. * @param mixed $time 当数字时指定跳转时间
  884. * @return void
  885. */
  886. function success_jump($msg='', $url='', $time=1)
  887. {
  888. if ($url=='' && isset($_SERVER["HTTP_REFERER"]))
  889. {
  890. $url = $_SERVER["HTTP_REFERER"];
  891. }
  892. if(!headers_sent())
  893. {
  894. header("Location:".route('admin_jump')."?message=$msg&url=$url&time=$time");
  895. exit();
  896. }
  897. else
  898. {
  899. $str = "<meta http-equiv='Refresh' content='URL=".route('admin_jump')."?message=$msg&url=$url&time=$time"."'>";
  900. exit($str);
  901. }
  902. }
  903. //获取表所有字段
  904. function get_table_columns($table, $field='')
  905. {
  906. $res = \Illuminate\Support\Facades\Schema::getColumnListing($table);
  907. if($field != '')
  908. {
  909. //判断字段是否在表里面
  910. if(in_array($field, $res))
  911. {
  912. return true;
  913. }
  914. else
  915. {
  916. return false;
  917. }
  918. }
  919. return $res;
  920. }