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.

1017 lines
24 KiB

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