common.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. use phpmailer\phpmailer;
  13. use phpmailer\SMTP;
  14. use think\Db;
  15. use think\facade\Request;
  16. use app\index\model\Category;
  17. use think\facade\Env;
  18. /*
  19. * 应用公共函数文件,函数不能定义为public类型,
  20. * 如果我们要使用我们定义的公共函数,直接在我们想用的地方直接调用函数即可。
  21. * */
  22. // 公共发送邮件函数
  23. function sendEmail($email,$emailpaswsd,$smtp,$sll,$emname,$title,$content,$toemail){
  24. $mail = new PHPMailer(true); // Passing `true` enables exceptions
  25. try {
  26. // 实例化PHPMailer核心类
  27. $mail = new PHPMailer();
  28. // 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
  29. //$mail->SMTPDebug = 0;
  30. // 使用smtp鉴权方式发送邮件
  31. $mail->isSMTP();
  32. // smtp需要鉴权 这个必须是true
  33. $mail->SMTPAuth = true;
  34. // 链接smtp域名邮箱的服务器地址
  35. $mail->Host = $smtp;
  36. // 设置使用ssl加密方式登录鉴权
  37. $mail->SMTPSecure = 'ssl';
  38. // 设置ssl连接smtp服务器的远程服务器端口号
  39. $mail->Port = $sll;
  40. // 设置发送的邮件的编码
  41. $mail->CharSet = 'UTF-8';
  42. // 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名
  43. $mail->FromName = $emname;
  44. // smtp登录的账号 QQ邮箱即可
  45. $mail->Username = $email;
  46. // smtp登录的密码 使用生成的授权码
  47. $mail->Password = $emailpaswsd;
  48. // 设置发件人邮箱地址 同登录账号
  49. $mail->From = $email;
  50. // 邮件正文是否为html编码 注意此处是一个方法
  51. $mail->isHTML(true);
  52. // 设置收件人邮箱地址
  53. $mail->addAddress($toemail);
  54. // 添加多个收件人 则多次调用方法即可
  55. //$mail->addAddress('87654321@163.com');
  56. // 添加该邮件的主题
  57. $mail->Subject = $title;
  58. // 添加邮件正文
  59. $mail->Body = $content;
  60. // 为该邮件添加附件
  61. //$mail->addAttachment('./example.pdf');
  62. // 发送邮件 返回状态
  63. $status = $mail->send();
  64. return $status;
  65. } catch (Exception $e) {
  66. echo '邮件发送失败: ', $mail->ErrorInfo;
  67. }
  68. }
  69. /**
  70. * 短信宝短信发送
  71. * $user短信宝账号, $passMD5加密的密码, $content短信内容, $phone手机号码
  72. */
  73. function sendSms($user,$pass,$content,$phone)
  74. {
  75. $statusStr = array(
  76. "0" => "短信发送成功",
  77. "-1" => "参数不全",
  78. "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
  79. "30" => "密码错误",
  80. "40" => "账号不存在",
  81. "41" => "余额不足",
  82. "42" => "帐户已过期",
  83. "43" => "IP地址限制",
  84. "50" => "内容含有敏感词"
  85. );
  86. $smsapi = "http://www.smsbao.com/"; //短信网关
  87. $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
  88. $result =file_get_contents($sendurl) ;
  89. return $statusStr[$result];
  90. }
  91. /**
  92. * 订单号生成函数
  93. * */
  94. function trade_no()
  95. {
  96. list($usec, $sec) = explode(" ", microtime());
  97. $usec = substr(str_replace('0.', '', $usec), 0 ,4);
  98. $str = rand(10,99);
  99. return date("YmdHis").$usec.$str;
  100. }
  101. /**
  102. * 循环删除目录和文件
  103. * @param string $dir_name
  104. * @return bool
  105. */
  106. function delete_dir_file($dir_name) {
  107. $result = false;
  108. if(is_dir($dir_name)){
  109. if ($handle = opendir($dir_name)) {
  110. while (false !== ($item = readdir($handle))) {
  111. if ($item != '.' && $item != '..') {
  112. if (is_dir($dir_name . DIRECTORY_SEPARATOR . $item)) {
  113. delete_dir_file($dir_name . DIRECTORY_SEPARATOR . $item);
  114. } else {
  115. unlink($dir_name . DIRECTORY_SEPARATOR . $item);
  116. }
  117. }
  118. }
  119. closedir($handle);
  120. if (rmdir($dir_name)) {
  121. $result = true;
  122. }
  123. }
  124. }
  125. return $result;
  126. }
  127. /*
  128. *递归遍历目录,作自定义文件选择目录
  129. */
  130. function myscandir($pathName){
  131. //将结果保存在result变量中
  132. $result = array();
  133. //判断传入的变量是否是目录
  134. if(!is_dir($pathName) || !is_readable($pathName)) {
  135. return null;
  136. }
  137. //取出目录中的文件和子目录名,使用scandir函数
  138. $allFiles = scandir($pathName);
  139. //遍历
  140. foreach($allFiles as $key=>$fileName) {
  141. if(in_array($fileName, array('.', '..'))) {
  142. continue;
  143. }
  144. //路径加文件名
  145. $fullName = $pathName.'/'.$fileName;
  146. //如果是目录的话就继续遍历这个目录
  147. if(is_dir($fullName)) {
  148. //将这个目录信息存入到数组中
  149. $result[] = ["title"=>$fileName,"id"=>str_replace(Env::get('root_path'),'.',$fullName),"children"=>myscandir($fullName)];
  150. }
  151. }
  152. return array_filter($result);
  153. }
  154. //XSS/sql过滤/
  155. function delete_XSS($val)
  156. {
  157. if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否为打开
  158. {
  159. $post = addslashes ( $val ); // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤
  160. }
  161. $post = str_replace ( "_" , "\_" , $post ); // 把 '_'过滤掉
  162. $post = str_replace ( "%" , "\%" , $post ); // 把' % '过滤掉
  163. $post = nl2br ( $post ); // 回车转换
  164. $post = htmlspecialchars( $post ); // html标记转换
  165. return $post ;
  166. }
  167. /*
  168. *@function 查询所有模型文章 fuck 标签库实在不想拼接 放在这里处理
  169. *
  170. *@param : $limit 查询数量
  171. *
  172. *@param : $order 数据排序条件
  173. *
  174. *@return array_slice($all,$start,$limt) 返回数据
  175. *
  176. */
  177. function setallmat($limt,$order){
  178. $page = input("page") ? input("page") : "1";
  179. if(strpos($limt,",")){
  180. $limt = explode(",",$limt);
  181. }
  182. if(is_array($limt)){
  183. $page = $limt["0"];
  184. $limt = $limt["1"];
  185. }
  186. $table = Db::name("model")->field("tablename")->select();
  187. $all = [];
  188. $allmat = [];
  189. foreach($table as $v){
  190. $mater = Db::name($v["tablename"])
  191. ->alias("a")
  192. ->join($v["tablename"]."_data b","find_in_set(b.aid,a.id)")
  193. ->leftjoin("category c","find_in_set(c.id,a.mid)")
  194. ->leftjoin("member d","find_in_set(d.id,a.uid)")
  195. ->leftjoin("member_asset f","find_in_set(f.memberid,d.id)")
  196. ->leftjoin("type e","find_in_set(e.id,a.type)")
  197. ->fieldRaw("a.*,b.likes,b.browse,comment_t,c.title as catitle,d.name,e.title as tytitle")
  198. ->where(["a.delete_time"=>NULL,"a.status"=>"0"])
  199. ->select()
  200. ->toArray();
  201. if(!empty($mater)){
  202. $all[] = $mater;
  203. }
  204. }
  205. if(!empty($all)){
  206. $all = ary3_ary2($all);
  207. $all = arrorder($all,$order);
  208. $count = count($all);//总条数
  209. $start = ($page - 1)*$limt;
  210. $allmat['allmats'] = array_slice($all,$start,$limt);
  211. $allmat['sum'] = $count;
  212. $allmat['pages'] = round(($count / $limt));
  213. $allmat['thispages'] = $page;
  214. }else{
  215. $allmat['allmats'] = array();
  216. $allmat['sum'] = count($all);
  217. $allmat['pages'] = round((count($all) / $limt));
  218. $allmat['thispages'] = $page;
  219. }
  220. return $allmat;
  221. }
  222. //文章栏目处理
  223. /*
  224. *@function 文章栏目处理 配合文章标签使用
  225. *@param : $mid 需要处理的栏目id
  226. *
  227. *@return : $tab 返回结果数组
  228. *
  229. */
  230. function muyname($mid,$father="true"){
  231. $ids = Db::name("category")->where("id",$mid)->field("pid,modid")->find();
  232. $lmid = $mid;
  233. $tab = ['ftabname'=>'','mid'=>$mid,'tablename'=>''];
  234. if(!empty($ids)){
  235. $lmid = muynamedigui($lmid);
  236. $tab = Db::name("model")->field("tablename")->find($ids['modid']);
  237. $tab['ftabname'] = $tab['tablename'].'_data';
  238. if($father == "true"){
  239. $tab['mid'] = $lmid;
  240. }else{
  241. $tab['mid'] = $mid;
  242. }
  243. }
  244. return $tab;
  245. }
  246. /*
  247. *@function 配合muyname此函数处理栏目所有子孙数据
  248. *
  249. *@param : $pid 需要查子孙数据id的标识
  250. *
  251. **
  252. */
  253. function muynamedigui($pid){
  254. $list = $pid;
  255. if(is_numeric($list)){
  256. $son = Db::name("category")->where("pid",$list)->field("id")->select()->toArray();
  257. if(!empty($son)){
  258. foreach($son as $vs){
  259. $list .= "," . $vs['id'];
  260. $list .= ",".muynamedigui($vs['id']);//继续查询子孙数据的子孙数据;
  261. }
  262. $a = explode(",",$list);
  263. $b = array_unique($a);
  264. $c = implode(",",$b);
  265. $list = trim($c,',');
  266. }
  267. }
  268. return $list;
  269. }
  270. function selectmatinfo($id){
  271. $mod = Db::name("model")->field('tablename')->select()->toArray();
  272. $maturl = "#文章地址丢失";
  273. foreach($mod as $v){
  274. $mat = Db::name($v["tablename"])->find($id);
  275. if(!empty($mat)){
  276. $maturl = $mat['maturl'];
  277. }
  278. }
  279. return $maturl;
  280. }
  281. /*
  282. *@function 递归处理,还需改变,不适宜数据量过大的情况,否则效率大打折扣
  283. *@param : $data 需要递归处理的数据
  284. *
  285. *@param : $pid 子数据匹配条件 默认0
  286. *
  287. *@return $list 返回递归处理完成的数据
  288. */
  289. function alldigui($data,$pid=0,$live=0){
  290. $list = array();//声明新数组,存放新单元
  291. if($live == 0){
  292. foreach($data as $v){
  293. if($v['pid'] == $pid){//匹配子数组
  294. $v['children'] = alldigui($data,$v['id'],$live);//递归
  295. $list[] = $v;//处理好的数据存入新数组
  296. }
  297. }
  298. }else{
  299. foreach($data as $v){
  300. if($v['id'] == $live){
  301. $v['children'] = $data;
  302. $list[] = $v;
  303. }
  304. }
  305. }
  306. return $list;//返回
  307. }
  308. //循环删除文件夹文件
  309. function rmdirr($dir){
  310. if ($handle = opendir("$dir")){
  311. while (false !== ($item = readdir($handle))){
  312. if ($item != '.' && $item != '..'){
  313. if (is_dir("$dir/$item")){
  314. $this->rmdirr("$dir/$item");
  315. }else{
  316. if(unlink("$dir/$item")){
  317. }
  318. }
  319. }
  320. }
  321. closedir($handle);
  322. if(rmdir($dir)){
  323. return true;
  324. }else{
  325. return false;
  326. }
  327. }
  328. }
  329. /*数据大小显示的转换
  330. *目前只应用于数据库表的大小显示转换
  331. */
  332. function format_size($size){
  333. //定义数组
  334. $arr=['B','KB','M','G','T'];
  335. //定义变量i
  336. $i = 0;
  337. //循环
  338. while ($size>=1024){
  339. $size = $size/1024;
  340. $i++;
  341. }
  342. return round($size,2).$arr[$i];
  343. }
  344. /**
  345. * 友链检测
  346. *$url 目标url 必须带http://或https://协议
  347. */
  348. function links_check($url){
  349. $myurl = think\facade\Request::domain();
  350. $key ="http";
  351. if(strpos($url,$key) !== false && strpos($url,$key) == 0){
  352. $data = file_get_contents($url,false);
  353. if(strpos($data,$myurl) !== false){
  354. return "ok";
  355. }else{
  356. return "false";
  357. }
  358. }
  359. return json_encode(array("code"=>0,"msg"=>"请检查url的正确性"),JSON_UNESCAPED_UNICODE);
  360. }
  361. /*
  362. *控制器便捷返回json数据信息
  363. *$code 状态码
  364. *$msg 提示信息
  365. */
  366. function jsonmsg($code,$msg){
  367. return json(['code'=>$code,'msg'=>$msg]);
  368. }
  369. /**
  370. * 获取TDK
  371. *$url 目标url 必须带http://或https://协议
  372. */
  373. function geturltdk($url)
  374. {
  375. $key ="http";
  376. if(strpos($url,$key) !== false && strpos($url,$key) == 0){
  377. $html = file_get_contents($url,false);
  378. if (!$html) {
  379. return "0";
  380. }
  381. $data['title']['length'] = $data['keywords']['length'] = $data['description']['length'] = 0;
  382. $data['title']['content'] = $data['keywords']['content'] = $data['description']['content'] = '';
  383. $html = mb_substr($html, 0, 1000);
  384. preg_match("/<title>(.*)<\/title>/i", $html, $title);
  385. preg_match("/<meta name=\"keywords\" content=(.*)\/>/i", $html, $keywords);
  386. preg_match("/<meta name=\"description\" content=(.*)\/>/i", $html, $description);
  387. if (isset($title[1])) {
  388. $data['title']['content'] = str_replace('"', '', $title[1]);
  389. $data['title']['length'] = mb_strlen($data['title']['content']);
  390. }
  391. if (isset($keywords[1])) {
  392. $data['keywords']['content'] = str_replace('"', '', $keywords[1]);
  393. $data['keywords']['length'] = mb_strlen($data['keywords']['content']);
  394. }
  395. if (isset($description[1])) {
  396. $data['description']['content'] = str_replace('"', '', $description[1]);
  397. $data['description']['length'] = mb_strlen($data['description']['content']);
  398. }
  399. return $data;
  400. }
  401. return json_encode(array("code"=>0,"msg"=>"请检查url的正确性"),JSON_UNESCAPED_UNICODE);
  402. }
  403. /*
  404. *获取路径中文件名方法
  405. *$path 路径
  406. *$postfix 文件后缀
  407. *$fix 是否带当前文件名后缀输出 null输出文件名后缀
  408. */
  409. function gainfile($path,$postfix,$fix=null){
  410. $dile_name=[];
  411. foreach(glob($path . "/*" . $postfix) as $v)
  412. {
  413. if(is_null($fix)){
  414. $dile_name[]=basename($v);
  415. }else{
  416. $dile_name[]=basename($v,$fix);
  417. }
  418. }
  419. return $dile_name;
  420. }
  421. function modsql($tabname){
  422. $sql="CREATE TABLE `$tabname` (
  423. `id` int(11) NOT NULL COMMENT 'id',
  424. `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '标题',
  425. `ftitle` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '副标题',
  426. `titlepic` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '标题图片',
  427. `mid` int(11) DEFAULT NULL COMMENT '所属栏目',
  428. `uid` int(11) NOT NULL COMMENT '关联会员id',
  429. `type` int(11) DEFAULT NULL COMMENT '所属分类',
  430. `keyword` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '关键词',
  431. `abstract` text COLLATE utf8_unicode_ci COMMENT '摘要',
  432. `tag` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '标签',
  433. `author` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '发布者',
  434. `editor` text COLLATE utf8_unicode_ci NOT NULL COMMENT '内容',
  435. `price` tinyint(2) NOT NULL DEFAULT '0' COMMENT '阅读类型 1免费 2付费 3vip免费 4vip折扣',
  436. `moneys` decimal(10,2) NOT NULL DEFAULT '0' COMMENT '付费金额',
  437. `status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '发布状态 0发布 1草稿 2下架 3待审核 4驳回',
  438. `orders` int(11) NOT NULL DEFAULT '1' COMMENT '排序',
  439. `create_time` int(11) NOT NULL COMMENT '创建时间',
  440. `update_time` int(11) NOT NULL COMMENT '更新时间',
  441. `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
  442. `comment` tinyint(2) NOT NULL DEFAULT '0' COMMENT '是否允许评论 0-允许 1不允许',
  443. `refusal` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '驳回原因',
  444. `top` tinyint(2) NOT NULL DEFAULT '0' COMMENT '置顶 0不置顶 1置顶',
  445. `ppts` tinyint(2) NOT NULL DEFAULT '0' COMMENT '标题图片幻灯 0不幻灯 1幻灯',
  446. `isadmin` tinyint(2) NOT NULL DEFAULT '0' COMMENT '是否管理员发布 0否 1是',
  447. `likes_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '当前文章点过赞的ip',
  448. `collects` int(255) NOT NULL DEFAULT '0' COMMENT '当前文章被收藏数',
  449. `matpattern` tinyint(1) NOT NULL DEFAULT '1' COMMENT '文章是否静态',
  450. `maturl` char(100) NOT NULL COMMENT '文章url',
  451. PRIMARY KEY (`id`)
  452. )";
  453. return $sql;
  454. }
  455. function datasql($datatab){
  456. $sql = "CREATE TABLE `$datatab` (
  457. `aid` int(11) NOT NULL COMMENT '关联文章id',
  458. `browse` int(11) NOT NULL DEFAULT '0' COMMENT '文章浏览次数',
  459. `likes` int(11) NOT NULL DEFAULT '0' COMMENT '文章点赞次数',
  460. `comment_t` int(200) NOT NULL DEFAULT '0' COMMENT '文章评论总计',
  461. `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
  462. PRIMARY KEY (`aid`)
  463. )";
  464. return $sql;
  465. }
  466. function fiesql($fietab,$modid){
  467. $sql="INSERT INTO `$fietab` (`modid`, `field`, `title`, `type`, `forms`, `defaults`, `leng`, `adst`, `hmst`, `required`, `chart`, `orders`, `ismuyu`) VALUES
  468. ($modid, 'title', '标题', 'varchar', 'text', NULL, '255', 1, 1, 1, 1, 0, 1),
  469. ($modid, 'titlepic', '标题图片', 'varchar', 'img', NULL, '255', 1, 1, 0, 1, 0, 1),
  470. ($modid, 'ftitle', '副标题', 'varchar', 'text', NULL, '255', 1, 1, 0, 1, 0, 1),
  471. ($modid, 'keyword', '关键词', 'varchar', 'text', NULL, '255', 1, 1, 0, 1, 0, 1),
  472. ($modid, 'abstract', '摘要', 'varchar', 'textarea', NULL, '255', 1, 1, 0, 1, 0, 1),
  473. ($modid, 'author', '发布者', 'varchar', 'text', NULL, '255', 1, 1, 0, 1, 0, 1);";
  474. return $sql;
  475. }
  476. /*
  477. *针对各个模型增加数据id统一自增
  478. */
  479. function setconid(){
  480. $watch = Db::name('model')->field("tablename")->select();
  481. $as=[];
  482. foreach($watch as $key=>$va){
  483. $as[] = Db::name($va['tablename'])->field("id")->order("id","desc")->find();
  484. }
  485. return max($as);
  486. }
  487. /*
  488. *解压缩方法
  489. *$path 需要解压的路径
  490. *需要解压的文件
  491. *
  492. */
  493. function unzip($path,$filename){
  494. //解压缩
  495. $zip = new \ZipArchive;
  496. //要解压的文件
  497. $zipfile = $filename;
  498. $res = $zip->open($zipfile);
  499. if($res != true){
  500. return false;
  501. }
  502. //要解压到的目录
  503. $toDir = $path;
  504. if(!file_exists($toDir)) {
  505. mkdir($toDir,755);
  506. }
  507. $ress = $zip->extractTo($toDir);
  508. $zip->close();
  509. if($ress != true){
  510. return false;
  511. }
  512. return true;
  513. }
  514. /*
  515. *$file 接收需要上传的文件 *必须
  516. *
  517. *$url 上传文件需要保存的路径 *必须
  518. *
  519. *$ext上传类型 file文件上传 image 图片上传 *必须
  520. *
  521. *$id 自定义需要返回的处理值 可不传
  522. */
  523. function allup($file,$url,$ext,$id=NULL){
  524. $set = Db::name('system_upset')->find(1);
  525. $size = '1048576';
  526. if($ext == 'file'){
  527. $ext = $set['fileext'];
  528. $size = $set['filesize'];
  529. }else if($ext == 'image'){
  530. $ext = $set['imageext'];
  531. $size = $set['imagesize'];
  532. }else{
  533. return ['code'=>500,'msg'=>"上传参数配置错误!"];
  534. false;
  535. }
  536. $photo = "";
  537. if(!empty($file)){
  538. //移动到框架指定目录
  539. $info = $file->validate(['ext'=>$ext,'size'=>$size])->rule('uniqid')->move('.'.$url);
  540. if($info){
  541. //获取名称
  542. $imgName = str_replace("\\","/",$info->getSaveName());
  543. $photo = $url.'/'.$imgName;
  544. }else{
  545. $error = $file->getError();
  546. }
  547. }
  548. //判断上传是否成功
  549. if($photo == ""){
  550. $error = $file->getError();
  551. return ['code'=>404,'msg'=>"上传失败,{$error}"];
  552. }else{
  553. return ['code'=>1,'msg'=>'上传成功',"photo"=>$photo,'id'=>$id];
  554. }
  555. }
  556. /*
  557. * @function 三维数组转二维数组
  558. * @Param: $array : 传入参数
  559. * @Return: $tempArr 返回结果数组
  560. */
  561. function ary3_ary2($array){
  562. if(!empty($array)){
  563. if(is_array($array)){
  564. $array = array_filter($array);
  565. $array = array_values($array);
  566. $tempArr = [];
  567. foreach ($array as $orderKey =>$orderVal){
  568. $count = count($orderVal);
  569. if($count > 1){
  570. for ($i = 0;$i < $count;$i++){
  571. $tempArr[] = $orderVal[$i];
  572. }
  573. }else{
  574. $tempArr[] = $orderVal[0];
  575. }
  576. }
  577. return $tempArr;
  578. }
  579. }
  580. return;
  581. }
  582. /*
  583. *@function 对数组排序
  584. *
  585. *@param : $array 需要排序的数组
  586. *
  587. *@param : $order 排序的条件
  588. *
  589. *@return $array 返回排序后数组
  590. *
  591. */
  592. function arrorder($array,$order){
  593. $newArr = array();
  594. $ord = explode(" ",$order);
  595. if($ord['1'] = "desc"){
  596. $orderss = SORT_DESC;
  597. }elseif($ord['1'] = "asc"){
  598. $orderss = SORT_ASC;
  599. }
  600. foreach($array as $key=>$v){
  601. $newArr[$key][$ord["0"]] = $v[$ord["0"]];
  602. }
  603. array_multisort($newArr,$orderss,$array);
  604. return $array;
  605. }
  606. /*
  607. *@function 判断文件$file_name后缀是否符合$allow_type数组里的后缀
  608. *
  609. *@param $file_name 被判定文件后缀的文件
  610. *
  611. *@param $allow_type 判定后缀依据
  612. *
  613. *@return 返回bool值true or false;
  614. */
  615. function get_file_suffix($file_name, $allow_type = array())
  616. {
  617. //分割文件得到当前文件的后缀数组
  618. $fnarray=explode('.', $file_name);
  619. //将后缀转换为小写/删除数组最后一个元素‘.’
  620. $file_suffix = strtolower(array_pop($fnarray));
  621. //如果没有判定依据就直接输出当前文件后缀
  622. if (empty($allow_type))
  623. {
  624. return $file_suffix;
  625. }
  626. else
  627. {
  628. if (in_array($file_suffix, $allow_type))
  629. {
  630. return true;
  631. }
  632. else
  633. {
  634. return false;
  635. }
  636. }
  637. }
  638. /*
  639. *@function 根据标签库获取条件进行查询栏目数据处理
  640. *@param : $limt 查询的数量
  641. *@param : $order 排序方式
  642. *@param : $id 栏目ID 有值则只查此id的父子数据
  643. *@return : $res 返回递归处理数据
  644. */
  645. function hnavs($limt,$sonlimit,$order="id asc",$id=""){
  646. $where[] = ['status','=',1];
  647. $where[] = ['type','=',0];
  648. $live = 0;
  649. if(!empty($id) && is_numeric($id)){
  650. $sonid = $id;
  651. if(!empty($sonlimit)){
  652. $son = foreachpid($sonid,$sonlimit);
  653. }else{
  654. $son = foreachpid($sonid);
  655. }
  656. if(!empty($son)){
  657. $res = array();
  658. $father = Db::name("category")->find($sonid);
  659. $father['children'] = $son;
  660. $res[] = $father;
  661. return $res;
  662. }else{
  663. $one = Db::name("category")->field("pid")->find($sonid);
  664. if(!empty($one)){
  665. if($one['pid'] != 0){
  666. $live = $sonid;
  667. $where[] = ['pid','=',$one['pid']];
  668. $sons = Db::name("category")->where($where)->field("id")->select();
  669. if(!empty($sons)){
  670. foreach($sons as $v){
  671. $sonid .= "," . $v['id'];
  672. }
  673. }
  674. }
  675. }
  676. }
  677. $where[] = ['id','in',$sonid];
  678. }
  679. if(!empty($limt)){
  680. $res = Db::name("category")->where($where)->field("id")->orderRaw($order)->limit($limt)->select()->toArray();
  681. $allids = "";
  682. if(!empty($res)){
  683. foreach($res as $r){
  684. $allids .= $r['id'] . ",";
  685. }
  686. $allids = substr($allids,0,-1);
  687. if($sonlimit){
  688. $soncat = Db::name("category")->field("id")->where("pid","in",$allids)->orderRaw($order)->limit($sonlimit)->select()->toArray();
  689. }else{
  690. $soncat = Db::name("category")->field("id")->where("pid","in",$allids)->orderRaw($order)->select()->toArray();
  691. }
  692. if(!empty($soncat)){
  693. foreach($soncat as $sonv){
  694. $allids .= "," . $sonv['id'];
  695. }
  696. }
  697. $res = Db::name("category")->where("id","in",$allids)->orderRaw($order)->select()->toArray();
  698. }
  699. }else{
  700. $res = Db::name("category")->where($where)->orderRaw($order)->select()->toArray();
  701. }
  702. $res = alldigui($res,0,$live);
  703. // dump($res);
  704. return $res;
  705. }
  706. function foreachpid($id,$i=""){
  707. $array = array();
  708. if(!empty($i)){
  709. $a = Db::name("category")->where("pid",$id)->limit($i)->select();
  710. }else{
  711. $a = Db::name("category")->where("pid",$id)->select();
  712. }
  713. if(!empty($a)){
  714. foreach($a as $as){
  715. $as['children'] = foreachpid($as["id"]);
  716. $array[] = $as;
  717. }
  718. }
  719. return $array;
  720. }
  721. /*
  722. *导航标签高亮在此处理
  723. *也可直接页面使用
  724. *{$id|thisclass=###,"layui-this"}
  725. *
  726. *$thisid 当前栏目id
  727. *$class 高亮样式
  728. */
  729. function thisclass($thisid,$class){
  730. $classid = input("cateid") ? input("cateid") : null;
  731. $aid = input("contid") ? input("contid") : null;
  732. if($aid){
  733. $table = Db::name("model")->field("tablename")->select();
  734. foreach ($table as $t){
  735. $ainfo = Db::name($t['tablename'])->field("mid")->find($aid);
  736. if($ainfo){
  737. $classid = $ainfo["mid"];
  738. }
  739. }
  740. }
  741. if($classid){
  742. $catinfo = Db::name("category")->field("id,pid")->find($classid);
  743. if($thisid == $classid){
  744. return $class;
  745. }elseif($thisid == $catinfo["pid"]){
  746. return $class;
  747. }
  748. }
  749. return;
  750. }
  751. /*
  752. *面包屑函数 snav($mid,$aid,$symbol)
  753. *$mid 栏目id 列表页可省略
  754. *$cid 内容id 内容页可省略
  755. *$symbol 分割符号 默认 /
  756. */
  757. function snavs($mid="",$symbol="/"){
  758. $mid = $mid ? $mid : input("cateid");
  759. $html = '<a href="http://'.$_SERVER["SERVER_NAME"].'">首页</a>';
  760. if(!empty($mid) && is_numeric($mid)){
  761. $cateid = topsnavs($mid);
  762. $cateinfo = Db::name("category")->where('id','in',$cateid)->field("title,href")->select()->toArray();
  763. if(!empty($cateinfo)){
  764. foreach($cateinfo as $val){
  765. $html .= '<span>'.$symbol.'</span><a href="http://'.$_SERVER["SERVER_NAME"].$val['href'].'">'.$val["title"].'</a>';
  766. }
  767. }else{
  768. $html .= '<span>'.$symbol.'</span><a>栏目丢了</a>';
  769. }
  770. }
  771. return $html;
  772. }
  773. function topsnavs($pid){
  774. $list = $pid;
  775. if(is_numeric($list)){
  776. $top = Db::name("category")->where("id",$list)->field("id,pid")->select()->toArray();
  777. if(!empty($top)){
  778. foreach($top as $vs){
  779. $list .= "," . $vs['id'];
  780. if($vs['pid'] != 0){
  781. $list .= ",".topsnavs($vs['pid']);
  782. }
  783. }
  784. $a = explode(",",$list);
  785. $b = array_unique($a);
  786. $c = implode(",",array_reverse($b));
  787. $list = trim($c,',');
  788. }
  789. }
  790. return $list;
  791. }
  792. //处理tags/和key的调用
  793. function tagkey($limt,$field,$order,$catid="",$matid=""){
  794. $list = [];
  795. if(empty($catid) && empty($matid)){
  796. $tab = Db::name("model")->field("tablename")->select();
  797. foreach($tab as $v){
  798. $det = Db::name($v['tablename'])->order($order)->limit($limt)->field($field)->select()->toArray();
  799. if($det){
  800. $list[] = $det;
  801. }
  802. }
  803. if(!empty($list)){
  804. $list = ary3_ary2($list);
  805. $l = array_column($list,$field);
  806. $list = array_filter(array_unique(explode(",",implode(",",$l))));
  807. }else{
  808. $list = array();
  809. }
  810. }
  811. if($catid){
  812. $modid = Db::name("category")->field("modid")->find($catid);
  813. $tab = Db::name("model")->field("tablename")->find($modid['modid']);
  814. $list = Db::name($tab['tablename'])->order($order)->limit($limt)->field($field)->select()->toArray();
  815. $l = array_column($list,$field);
  816. $list = array_unique(explode(",",implode(",",$l)));
  817. }
  818. if($matid){
  819. $tab = Db::name("model")->field("tablename")->select();
  820. foreach($tab as $v){
  821. $det = Db::name($v['tablename'])->field($field)->find($matid);
  822. if($det){
  823. $list = $det;
  824. }
  825. }
  826. $list = array_unique(explode(",",$list[$field]));
  827. }
  828. return $list;
  829. }
  830. //处理幻灯标签查询
  831. function getppt($limt,$order,$operate,$slide="",$top="",$tab=""){
  832. $where[] = ["status","=",0];
  833. $where[] = ["delete_time","=",NULL];
  834. $field = "id,title,ftitle,titlepic,create_time";
  835. $list = "";
  836. if(!empty($slide) && $slide == 'true'){
  837. $where[] = ["ppts","=",1];
  838. }
  839. if(!empty($top) && is_numeric($top) ){
  840. $where[] = ["top","=",$top];
  841. }
  842. if(!in_array($operate,array("0","1","2")) || !is_numeric($operate)){//不在类型内 结束
  843. return;
  844. }
  845. if($operate == "0"){
  846. $tabs = Db::name("model")->field("tablename")->select();
  847. foreach($tabs as $v){
  848. $list = Db::name($v["tablename"])->where($where)->limit($limt)->order($order)->field($field)->select();
  849. }
  850. }elseif($operate == "1"){
  851. if(!is_numeric($tab) || empty($tab)){
  852. return;
  853. }
  854. $where[] = ["mid","=",$tab];
  855. $modid = Db::name("category")->field("modid")->find($tab);
  856. $tabn = Db::name("model")->field("tablename")->find($modid["modid"]);
  857. $list = Db::name($tabn["tablename"])->where($where)->limit($limt)->order($order)->field($field)->select();
  858. }elseif($operate == "2"){
  859. if(empty($tab)){
  860. return;
  861. }
  862. $list = Db::name($tab)->where($where)->limit($limt)->order($order)->field($field)->select();
  863. }
  864. if(empty($list)){
  865. $list =array();
  866. }
  867. return $list;
  868. }
  869. //评论标签获取对应文章ID、标题、标题图片、、
  870. function matsection($aid){
  871. if(!empty($aid)){
  872. $tab = Db::name("model")->field("tablename")->select();
  873. $array= "";
  874. foreach($tab as $v){
  875. $info = Db::name($v["tablename"])->field("id,title,titlepic")->find($aid);
  876. if($info){
  877. $array = $info;
  878. }
  879. }
  880. return $array;
  881. }
  882. return;
  883. }
  884. /*
  885. *muysubstr($str, $start=0, $length, $charset=”utf-8″, $suffix=true)
  886. *$str:要截取的字符串
  887. *$start=0:开始位置,默认从0开始
  888. *$length:截取长度
  889. *$charset=”utf-8″:字符编码,默认UTF-8
  890. *$suffix=true:是否在截取后的字符后面显示符号,可任意的定义
  891. *
  892. *{$val.title|muysubstr=###,0,7,'utf-8'}
  893. *
  894. */
  895. function muysubstr($str, $start=0, $length, $charset="utf-8", $suffix="")
  896. {
  897. if(function_exists("mb_substr")){
  898. return mb_substr($str, $start, $length, $charset).$suffix;
  899. }elseif(function_exists('iconv_substr')){
  900. return iconv_substr($str,$start,$length,$charset).$suffix;
  901. }
  902. $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
  903. $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
  904. $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
  905. $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
  906. preg_match_all($re[$charset], $str, $match);
  907. $slice = join("",array_slice($match[0], $start, $length));
  908. if($suffix) return $slice.$suffix;
  909. return $slice;
  910. }
  911. /**
  912. *filepg 远程file_get_contents方法
  913. * $url 远程地址
  914. * $form 请求方式
  915. * $array 携带参数
  916. * *
  917. **/
  918. function filepg($url,$form,$array){
  919. if(!empty($url) && !empty($form) && !empty($array)){
  920. $postUrl = $url;
  921. $curlPost = $array;
  922. $postdatas = http_build_query($curlPost);//处理请求数据,生成一个经过 URL-encode 的请求字符串
  923. $options = array(
  924. 'http' => array(
  925. 'method' => $form,//请求方式POST
  926. 'header' => 'Content-type:application/x-www-form-urlencoded',
  927. 'content' => $postdatas,
  928. 'timeout' => 5 // 超时时间(单位:s)
  929. )
  930. );
  931. $context = stream_context_create($options);//创建并返回文本数据流
  932. $result = file_get_contents($postUrl, false, $context);//将文本数据流读入字符串
  933. $da = json_decode($result,true);
  934. return $da;
  935. }
  936. return jsonmsg(0,'参数错误!');
  937. }
  938. /**
  939. *严格控制上传文件可上传路径
  940. * url被检测路径
  941. * 单独定义函数是为了方便index和admin模块使用 减少不必要的代码
  942. * *
  943. **/
  944. function upcheckurl($url){
  945. $checkurl = array("/public/upload/userimages","/public/upload/files","/public/upload/ggpic","/public/upload/images","/public/upload/linkpic","/public/upload/menubg");
  946. if(in_array($url,$checkurl)){
  947. return $url;
  948. }else{
  949. return "false";
  950. }
  951. }
  952. /**
  953. * 修改扩展配置文件
  954. * @param array $arr 需要更新或添加的配置
  955. * @param string $file 配置文件名(不需要后辍)
  956. * @return bool
  957. */
  958. function editconfig($arr = [], $file = 'databaseoff')
  959. {
  960. if (is_array($arr)) {
  961. if($file ==="databaseoff"){
  962. $filename = $file.".php";
  963. $filepath = Env::get("config_path") . $filename;
  964. if (!file_exists($filepath)) {
  965. $conf = "<?php return [];";
  966. file_put_contents($filepath, $conf);
  967. }
  968. $conf = include $filepath;
  969. foreach ($arr as $key => $value) {
  970. $conf[$key] = $value;
  971. }
  972. $time = date('Y/m/d H:i:s');
  973. $str = "<?php\r\n/**\r\n * MuYuCMS安全配置文件.\r\n * $time\r\n */\r\nreturn [\r\n";
  974. foreach ($conf as $key => $value) {
  975. $str .= "\t'$key' => '$value',";
  976. $str .= "\r\n";
  977. }
  978. $str .= '];';
  979. file_put_contents($filepath, $str);
  980. return true;
  981. }else{
  982. return false;
  983. }
  984. } else {
  985. return false;
  986. }
  987. }