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.

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