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.

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