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.

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