12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 流年 <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // 应用公共文件
- use phpmailer\phpmailer;
- use phpmailer\SMTP;
- use think\Db;
- use think\facade\Request;
- use app\index\model\Category;
- use think\facade\Env;
- /*
- * 应用公共函数文件,函数不能定义为public类型,
- * 如果我们要使用我们定义的公共函数,直接在我们想用的地方直接调用函数即可。
- * */
- // 公共发送邮件函数
- function sendEmail($email,$emailpaswsd,$smtp,$sll,$emname,$title,$content,$toemail){
- $mail = new PHPMailer(true); // Passing `true` enables exceptions
- try {
- // 实例化PHPMailer核心类
- $mail = new PHPMailer();
- // 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
- //$mail->SMTPDebug = 0;
- // 使用smtp鉴权方式发送邮件
- $mail->isSMTP();
- // smtp需要鉴权 这个必须是true
- $mail->SMTPAuth = true;
- // 链接smtp域名邮箱的服务器地址
- $mail->Host = $smtp;
- // 设置使用ssl加密方式登录鉴权
- $mail->SMTPSecure = 'ssl';
- // 设置ssl连接smtp服务器的远程服务器端口号
- $mail->Port = $sll;
- // 设置发送的邮件的编码
- $mail->CharSet = 'UTF-8';
- // 设置发件人昵称 显示在收件人邮件的发件人邮箱地址前的发件人姓名
- $mail->FromName = $emname;
- // smtp登录的账号 QQ邮箱即可
- $mail->Username = $email;
- // smtp登录的密码 使用生成的授权码
- $mail->Password = $emailpaswsd;
- // 设置发件人邮箱地址 同登录账号
- $mail->From = $email;
- // 邮件正文是否为html编码 注意此处是一个方法
- $mail->isHTML(true);
- // 设置收件人邮箱地址
- $mail->addAddress($toemail);
- // 添加多个收件人 则多次调用方法即可
- //$mail->addAddress('87654321@163.com');
- // 添加该邮件的主题
- $mail->Subject = $title;
- // 添加邮件正文
- $mail->Body = $content;
- // 为该邮件添加附件
- //$mail->addAttachment('./example.pdf');
- // 发送邮件 返回状态
- $status = $mail->send();
- return $status;
-
- } catch (Exception $e) {
- echo '邮件发送失败: ', $mail->ErrorInfo;
- }
- }
- /**
- * 短信宝短信发送
- * $user短信宝账号, $passMD5加密的密码, $content短信内容, $phone手机号码
- */
- function sendSms($user,$pass,$content,$phone)
- {
- $statusStr = array(
- "0" => "短信发送成功",
- "-1" => "参数不全",
- "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
- "30" => "密码错误",
- "40" => "账号不存在",
- "41" => "余额不足",
- "42" => "帐户已过期",
- "43" => "IP地址限制",
- "50" => "内容含有敏感词"
- );
- $smsapi = "http://www.smsbao.com/"; //短信网关
- $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
- $result =file_get_contents($sendurl) ;
- return $statusStr[$result];
- }
- /**
- * 订单号生成函数
- * */
- function trade_no()
- {
- list($usec, $sec) = explode(" ", microtime());
- $usec = substr(str_replace('0.', '', $usec), 0 ,4);
- $str = rand(10,99);
- return date("YmdHis").$usec.$str;
- }
-
- /**
- * 循环删除目录和文件
- * @param string $dir_name
- * @return bool
- */
- function delete_dir_file($dir_name) {
- $result = false;
- if(is_dir($dir_name)){
- if ($handle = opendir($dir_name)) {
- while (false !== ($item = readdir($handle))) {
- if ($item != '.' && $item != '..') {
- if (is_dir($dir_name . DIRECTORY_SEPARATOR . $item)) {
- delete_dir_file($dir_name . DIRECTORY_SEPARATOR . $item);
- } else {
- unlink($dir_name . DIRECTORY_SEPARATOR . $item);
- }
- }
- }
- closedir($handle);
- if (rmdir($dir_name)) {
- $result = true;
- }
- }
- }
- return $result;
- }
- /*
- *递归遍历目录,作自定义文件选择目录
- */
- function myscandir($pathName){
- //将结果保存在result变量中
- $result = array();
- //判断传入的变量是否是目录
- if(!is_dir($pathName) || !is_readable($pathName)) {
- return null;
- }
- //取出目录中的文件和子目录名,使用scandir函数
- $allFiles = scandir($pathName);
- //遍历
- foreach($allFiles as $key=>$fileName) {
- if(in_array($fileName, array('.', '..'))) {
- continue;
- }
- //路径加文件名
- $fullName = $pathName.'/'.$fileName;
- //如果是目录的话就继续遍历这个目录
- if(is_dir($fullName)) {
- //将这个目录信息存入到数组中
- $result[] = ["title"=>$fileName,"id"=>str_replace(Env::get('root_path'),'.',$fullName),"children"=>myscandir($fullName)];
- }
- }
- return array_filter($result);
- }
-
-
- //XSS/sql过滤/
- function delete_XSS($val)
- {
- if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否为打开
- {
- $post = addslashes ( $val ); // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤
- }
- $post = str_replace ( "_" , "\_" , $post ); // 把 '_'过滤掉
- $post = str_replace ( "%" , "\%" , $post ); // 把' % '过滤掉
- $post = nl2br ( $post ); // 回车转换
- $post = htmlspecialchars( $post ); // html标记转换
- return $post ;
- }
- /*
- *@function 查询所有模型文章 fuck 标签库实在不想拼接 放在这里处理
- *
- *@param : $limit 查询数量
- *
- *@param : $order 数据排序条件
- *
- *@return array_slice($all,$start,$limt) 返回数据
- *
- */
- function setallmat($limt,$order){
- $page = input("page") ? input("page") : "1";
- if(strpos($limt,",")){
- $limt = explode(",",$limt);
- }
- if(is_array($limt)){
- $page = $limt["0"];
- $limt = $limt["1"];
- }
- $table = Db::name("model")->field("tablename")->select();
- $all = [];
- $allmat = [];
- foreach($table as $v){
- $mater = Db::name($v["tablename"])
- ->alias("a")
- ->join($v["tablename"]."_data b","find_in_set(b.aid,a.id)")
- ->leftjoin("category c","find_in_set(c.id,a.mid)")
- ->leftjoin("member d","find_in_set(d.id,a.uid)")
- ->leftjoin("member_asset f","find_in_set(f.memberid,d.id)")
- ->leftjoin("type e","find_in_set(e.id,a.type)")
- ->fieldRaw("a.*,b.likes,b.browse,comment_t,c.title as catitle,d.name,e.title as tytitle")
- ->where(["a.delete_time"=>NULL,"a.status"=>"0"])
- ->select()
- ->toArray();
- if(!empty($mater)){
- $all[] = $mater;
- }
- }
- if(!empty($all)){
- $all = ary3_ary2($all);
- $all = arrorder($all,$order);
- $count = count($all);//总条数
- $start = ($page - 1)*$limt;
- $allmat['allmats'] = array_slice($all,$start,$limt);
- $allmat['sum'] = $count;
- $allmat['pages'] = round(($count / $limt));
- $allmat['thispages'] = $page;
- }else{
- $allmat['allmats'] = array();
- $allmat['sum'] = count($all);
- $allmat['pages'] = round((count($all) / $limt));
- $allmat['thispages'] = $page;
- }
- return $allmat;
- }
-
- //文章栏目处理
- /*
- *@function 文章栏目处理 配合文章标签使用
- *@param : $mid 需要处理的栏目id
- *
- *@return : $tab 返回结果数组
- *
- */
- function muyname($mid,$father="true"){
- $ids = Db::name("category")->where("id",$mid)->field("pid,modid")->find();
- $lmid = $mid;
- $tab = ['ftabname'=>'','mid'=>$mid,'tablename'=>''];
- if(!empty($ids)){
- $lmid = muynamedigui($lmid);
- $tab = Db::name("model")->field("tablename")->find($ids['modid']);
- $tab['ftabname'] = $tab['tablename'].'_data';
- if($father == "true"){
- $tab['mid'] = $lmid;
- }else{
- $tab['mid'] = $mid;
- }
- }
- return $tab;
- }
- /*
- *@function 配合muyname此函数处理栏目所有子孙数据
- *
- *@param : $pid 需要查子孙数据id的标识
- *
- **
- */
- function muynamedigui($pid){
- $list = $pid;
- if(is_numeric($list)){
- $son = Db::name("category")->where("pid",$list)->field("id")->select()->toArray();
- if(!empty($son)){
- foreach($son as $vs){
- $list .= "," . $vs['id'];
- $list .= ",".muynamedigui($vs['id']);//继续查询子孙数据的子孙数据;
- }
- $a = explode(",",$list);
- $b = array_unique($a);
- $c = implode(",",$b);
- $list = trim($c,',');
- }
- }
- return $list;
- }
-
- function selectmatinfo($id){
- $mod = Db::name("model")->field('tablename')->select()->toArray();
- $maturl = "#文章地址丢失";
- foreach($mod as $v){
- $mat = Db::name($v["tablename"])->find($id);
- if(!empty($mat)){
- $maturl = $mat['maturl'];
- }
- }
- return $maturl;
- }
-
-
- /*
- *@function 递归处理,还需改变,不适宜数据量过大的情况,否则效率大打折扣
- *@param : $data 需要递归处理的数据
- *
- *@param : $pid 子数据匹配条件 默认0
- *
- *@return $list 返回递归处理完成的数据
- */
- function alldigui($data,$pid=0,$live=0){
- $list = array();//声明新数组,存放新单元
- if($live == 0){
- foreach($data as $v){
- if($v['pid'] == $pid){//匹配子数组
- $v['children'] = alldigui($data,$v['id'],$live);//递归
- $list[] = $v;//处理好的数据存入新数组
- }
- }
- }else{
- foreach($data as $v){
- if($v['id'] == $live){
- $v['children'] = $data;
- $list[] = $v;
- }
- }
- }
- return $list;//返回
- }
- //循环删除文件夹文件
- function rmdirr($dir){
-
- if ($handle = opendir("$dir")){
- while (false !== ($item = readdir($handle))){
- if ($item != '.' && $item != '..'){
- if (is_dir("$dir/$item")){
- $this->rmdirr("$dir/$item");
- }else{
- if(unlink("$dir/$item")){
- }
- }
- }
- }
- closedir($handle);
- if(rmdir($dir)){
- return true;
- }else{
- return false;
- }
- }
- }
-
- /*数据大小显示的转换
- *目前只应用于数据库表的大小显示转换
- */
- function format_size($size){
- //定义数组
- $arr=['B','KB','M','G','T'];
- //定义变量i
- $i = 0;
- //循环
- while ($size>=1024){
- $size = $size/1024;
- $i++;
- }
- return round($size,2).$arr[$i];
- }
-
-
- /**
- * 友链检测
- *$url 目标url 必须带http://或https://协议
- */
- function links_check($url){
- $myurl = think\facade\Request::domain();
- $key ="http";
- if(strpos($url,$key) !== false && strpos($url,$key) == 0){
- $data = file_get_contents($url,false);
- if(strpos($data,$myurl) !== false){
- return "ok";
- }else{
- return "false";
- }
- }
- return json_encode(array("code"=>0,"msg"=>"请检查url的正确性"),JSON_UNESCAPED_UNICODE);
- }
- /*
- *控制器便捷返回json数据信息
- *$code 状态码
- *$msg 提示信息
- */
- function jsonmsg($code,$msg){
- return json(['code'=>$code,'msg'=>$msg]);
- }
- /**
- * 获取TDK
- *$url 目标url 必须带http://或https://协议
- */
- function geturltdk($url)
- {
- $key ="http";
- if(strpos($url,$key) !== false && strpos($url,$key) == 0){
- $html = file_get_contents($url,false);
- if (!$html) {
- return "0";
- }
- $data['title']['length'] = $data['keywords']['length'] = $data['description']['length'] = 0;
- $data['title']['content'] = $data['keywords']['content'] = $data['description']['content'] = '';
- $html = mb_substr($html, 0, 1000);
- preg_match("/<title>(.*)<\/title>/i", $html, $title);
- preg_match("/<meta name=\"keywords\" content=(.*)\/>/i", $html, $keywords);
- preg_match("/<meta name=\"description\" content=(.*)\/>/i", $html, $description);
- if (isset($title[1])) {
- $data['title']['content'] = str_replace('"', '', $title[1]);
- $data['title']['length'] = mb_strlen($data['title']['content']);
- }
- if (isset($keywords[1])) {
- $data['keywords']['content'] = str_replace('"', '', $keywords[1]);
- $data['keywords']['length'] = mb_strlen($data['keywords']['content']);
- }
- if (isset($description[1])) {
- $data['description']['content'] = str_replace('"', '', $description[1]);
- $data['description']['length'] = mb_strlen($data['description']['content']);
- }
- return $data;
- }
- return json_encode(array("code"=>0,"msg"=>"请检查url的正确性"),JSON_UNESCAPED_UNICODE);
- }
- /*
- *获取路径中文件名方法
- *$path 路径
- *$postfix 文件后缀
- *$fix 是否带当前文件名后缀输出 null输出文件名后缀
- */
- function gainfile($path,$postfix,$fix=null){
- $dile_name=[];
- foreach(glob($path . "/*" . $postfix) as $v)
- {
- if(is_null($fix)){
- $dile_name[]=basename($v);
- }else{
- $dile_name[]=basename($v,$fix);
- }
- }
- return $dile_name;
- }
-
- function modsql($tabname){
- $sql="CREATE TABLE `$tabname` (
- `id` int(11) NOT NULL COMMENT 'id',
- `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '标题',
- `ftitle` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '副标题',
- `titlepic` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '标题图片',
- `mid` int(11) DEFAULT NULL COMMENT '所属栏目',
- `uid` int(11) NOT NULL COMMENT '关联会员id',
- `type` int(11) DEFAULT NULL COMMENT '所属分类',
- `keyword` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '关键词',
- `abstract` text COLLATE utf8_unicode_ci COMMENT '摘要',
- `tag` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '标签',
- `author` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '发布者',
- `editor` text COLLATE utf8_unicode_ci NOT NULL COMMENT '内容',
- `price` tinyint(2) NOT NULL DEFAULT '0' COMMENT '阅读类型 1免费 2付费 3vip免费 4vip折扣',
- `moneys` decimal(10,2) NOT NULL DEFAULT '0' COMMENT '付费金额',
- `status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '发布状态 0发布 1草稿 2下架 3待审核 4驳回',
- `orders` int(11) NOT NULL DEFAULT '1' COMMENT '排序',
- `create_time` int(11) NOT NULL COMMENT '创建时间',
- `update_time` int(11) NOT NULL COMMENT '更新时间',
- `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
- `comment` tinyint(2) NOT NULL DEFAULT '0' COMMENT '是否允许评论 0-允许 1不允许',
- `refusal` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '驳回原因',
- `top` tinyint(2) NOT NULL DEFAULT '0' COMMENT '置顶 0不置顶 1置顶',
- `ppts` tinyint(2) NOT NULL DEFAULT '0' COMMENT '标题图片幻灯 0不幻灯 1幻灯',
- `isadmin` tinyint(2) NOT NULL DEFAULT '0' COMMENT '是否管理员发布 0否 1是',
- `likes_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '当前文章点过赞的ip',
- `collects` int(255) NOT NULL DEFAULT '0' COMMENT '当前文章被收藏数',
- `matpattern` tinyint(1) NOT NULL DEFAULT '1' COMMENT '文章是否静态',
- `maturl` char(100) NOT NULL COMMENT '文章url',
- PRIMARY KEY (`id`)
- )";
- return $sql;
- }
- function datasql($datatab){
- $sql = "CREATE TABLE `$datatab` (
- `aid` int(11) NOT NULL COMMENT '关联文章id',
- `browse` int(11) NOT NULL DEFAULT '0' COMMENT '文章浏览次数',
- `likes` int(11) NOT NULL DEFAULT '0' COMMENT '文章点赞次数',
- `comment_t` int(200) NOT NULL DEFAULT '0' COMMENT '文章评论总计',
- `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
- PRIMARY KEY (`aid`)
- )";
- return $sql;
- }
- function fiesql($fietab,$modid){
- $sql="INSERT INTO `$fietab` (`modid`, `field`, `title`, `type`, `forms`, `defaults`, `leng`, `adst`, `hmst`, `required`, `chart`, `orders`, `ismuyu`) VALUES
- ($modid, 'title', '标题', 'varchar', 'text', NULL, '255', 1, 1, 1, 1, 0, 1),
- ($modid, 'titlepic', '标题图片', 'varchar', 'img', NULL, '255', 1, 1, 0, 1, 0, 1),
- ($modid, 'ftitle', '副标题', 'varchar', 'text', NULL, '255', 1, 1, 0, 1, 0, 1),
- ($modid, 'keyword', '关键词', 'varchar', 'text', NULL, '255', 1, 1, 0, 1, 0, 1),
- ($modid, 'abstract', '摘要', 'varchar', 'textarea', NULL, '255', 1, 1, 0, 1, 0, 1),
- ($modid, 'author', '发布者', 'varchar', 'text', NULL, '255', 1, 1, 0, 1, 0, 1);";
- return $sql;
- }
- /*
- *针对各个模型增加数据id统一自增
- */
- function setconid(){
- $watch = Db::name('model')->field("tablename")->select();
- $as=[];
- foreach($watch as $key=>$va){
- $as[] = Db::name($va['tablename'])->field("id")->order("id","desc")->find();
- }
- return max($as);
- }
- /*
- *解压缩方法
- *$path 需要解压的路径
- *需要解压的文件
- *
- */
- function unzip($path,$filename){
- //解压缩
- $zip = new \ZipArchive;
- //要解压的文件
- $zipfile = $filename;
- $res = $zip->open($zipfile);
- if($res != true){
- return false;
- }
- //要解压到的目录
- $toDir = $path;
- if(!file_exists($toDir)) {
- mkdir($toDir,755);
- }
- $ress = $zip->extractTo($toDir);
- $zip->close();
- if($ress != true){
- return false;
- }
- return true;
- }
- /*
- *$file 接收需要上传的文件 *必须
- *
- *$url 上传文件需要保存的路径 *必须
- *
- *$ext上传类型 file文件上传 image 图片上传 *必须
- *
- *$id 自定义需要返回的处理值 可不传
- */
- function allup($file,$url,$ext,$id=NULL){
- $set = Db::name('system_upset')->find(1);
- $size = '1048576';
- if($ext == 'file'){
- $ext = $set['fileext'];
- $size = $set['filesize'];
- }else if($ext == 'image'){
- $ext = $set['imageext'];
- $size = $set['imagesize'];
- }else{
- return ['code'=>500,'msg'=>"上传参数配置错误!"];
- false;
- }
- $photo = "";
- if(!empty($file)){
- //移动到框架指定目录
- $info = $file->validate(['ext'=>$ext,'size'=>$size])->rule('uniqid')->move('.'.$url);
- if($info){
- //获取名称
- $imgName = str_replace("\\","/",$info->getSaveName());
- $photo = $url.'/'.$imgName;
- }else{
- $error = $file->getError();
- }
- }
- //判断上传是否成功
- if($photo == ""){
- $error = $file->getError();
- return ['code'=>404,'msg'=>"上传失败,{$error}"];
- }else{
- return ['code'=>1,'msg'=>'上传成功',"photo"=>$photo,'id'=>$id];
- }
- }
-
- /*
- * @function 三维数组转二维数组
- * @Param: $array : 传入参数
- * @Return: $tempArr 返回结果数组
- */
- function ary3_ary2($array){
- if(!empty($array)){
- if(is_array($array)){
- $array = array_filter($array);
- $array = array_values($array);
- $tempArr = [];
- foreach ($array as $orderKey =>$orderVal){
- $count = count($orderVal);
- if($count > 1){
- for ($i = 0;$i < $count;$i++){
- $tempArr[] = $orderVal[$i];
- }
- }else{
- $tempArr[] = $orderVal[0];
- }
- }
- return $tempArr;
- }
- }
- return;
- }
- /*
- *@function 对数组排序
- *
- *@param : $array 需要排序的数组
- *
- *@param : $order 排序的条件
- *
- *@return $array 返回排序后数组
- *
- */
- function arrorder($array,$order){
- $newArr = array();
- $ord = explode(" ",$order);
- if($ord['1'] = "desc"){
- $orderss = SORT_DESC;
- }elseif($ord['1'] = "asc"){
- $orderss = SORT_ASC;
- }
- foreach($array as $key=>$v){
- $newArr[$key][$ord["0"]] = $v[$ord["0"]];
- }
- array_multisort($newArr,$orderss,$array);
- return $array;
- }
- /*
- *@function 判断文件$file_name后缀是否符合$allow_type数组里的后缀
- *
- *@param $file_name 被判定文件后缀的文件
- *
- *@param $allow_type 判定后缀依据
- *
- *@return 返回bool值true or false;
- */
- function get_file_suffix($file_name, $allow_type = array())
- {
- //分割文件得到当前文件的后缀数组
- $fnarray=explode('.', $file_name);
- //将后缀转换为小写/删除数组最后一个元素‘.’
- $file_suffix = strtolower(array_pop($fnarray));
- //如果没有判定依据就直接输出当前文件后缀
- if (empty($allow_type))
- {
- return $file_suffix;
- }
- else
- {
- if (in_array($file_suffix, $allow_type))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
-
- /*
- *@function 根据标签库获取条件进行查询栏目数据处理
- *@param : $limt 查询的数量
- *@param : $order 排序方式
- *@param : $id 栏目ID 有值则只查此id的父子数据
- *@return : $res 返回递归处理数据
- */
- function hnavs($limt,$sonlimit,$order="id asc",$id=""){
- $where[] = ['status','=',1];
- $where[] = ['type','=',0];
- $live = 0;
- if(!empty($id) && is_numeric($id)){
- $sonid = $id;
- if(!empty($sonlimit)){
- $son = foreachpid($sonid,$sonlimit);
- }else{
- $son = foreachpid($sonid);
- }
- if(!empty($son)){
- $res = array();
- $father = Db::name("category")->find($sonid);
- $father['children'] = $son;
- $res[] = $father;
- return $res;
- }else{
- $one = Db::name("category")->field("pid")->find($sonid);
- if(!empty($one)){
- if($one['pid'] != 0){
- $live = $sonid;
- $where[] = ['pid','=',$one['pid']];
- $sons = Db::name("category")->where($where)->field("id")->select();
- if(!empty($sons)){
- foreach($sons as $v){
- $sonid .= "," . $v['id'];
- }
- }
- }
- }
- }
- $where[] = ['id','in',$sonid];
- }
- if(!empty($limt)){
- $res = Db::name("category")->where($where)->field("id")->orderRaw($order)->limit($limt)->select()->toArray();
- $allids = "";
- if(!empty($res)){
- foreach($res as $r){
- $allids .= $r['id'] . ",";
- }
- $allids = substr($allids,0,-1);
- if($sonlimit){
- $soncat = Db::name("category")->field("id")->where("pid","in",$allids)->orderRaw($order)->limit($sonlimit)->select()->toArray();
- }else{
- $soncat = Db::name("category")->field("id")->where("pid","in",$allids)->orderRaw($order)->select()->toArray();
- }
- if(!empty($soncat)){
- foreach($soncat as $sonv){
- $allids .= "," . $sonv['id'];
- }
- }
- $res = Db::name("category")->where("id","in",$allids)->orderRaw($order)->select()->toArray();
- }
- }else{
- $res = Db::name("category")->where($where)->orderRaw($order)->select()->toArray();
- }
- $res = alldigui($res,0,$live);
- // dump($res);
- return $res;
- }
-
- function foreachpid($id,$i=""){
- $array = array();
- if(!empty($i)){
- $a = Db::name("category")->where("pid",$id)->limit($i)->select();
- }else{
- $a = Db::name("category")->where("pid",$id)->select();
- }
- if(!empty($a)){
- foreach($a as $as){
- $as['children'] = foreachpid($as["id"]);
- $array[] = $as;
- }
- }
- return $array;
- }
-
- /*
- *导航标签高亮在此处理
- *也可直接页面使用
- *{$id|thisclass=###,"layui-this"}
- *
- *$thisid 当前栏目id
- *$class 高亮样式
- */
- function thisclass($thisid,$class){
- $classid = input("cateid") ? input("cateid") : null;
- $aid = input("contid") ? input("contid") : null;
- if($aid){
- $table = Db::name("model")->field("tablename")->select();
- foreach ($table as $t){
- $ainfo = Db::name($t['tablename'])->field("mid")->find($aid);
- if($ainfo){
- $classid = $ainfo["mid"];
- }
- }
- }
- if($classid){
- $catinfo = Db::name("category")->field("id,pid")->find($classid);
- if($thisid == $classid){
- return $class;
- }elseif($thisid == $catinfo["pid"]){
- return $class;
- }
- }
- return;
- }
-
- /*
- *面包屑函数 snav($mid,$aid,$symbol)
- *$mid 栏目id 列表页可省略
- *$cid 内容id 内容页可省略
- *$symbol 分割符号 默认 /
- */
- function snavs($mid="",$symbol="/"){
- $mid = $mid ? $mid : input("cateid");
- $html = '<a href="http://'.$_SERVER["SERVER_NAME"].'">首页</a>';
- if(!empty($mid) && is_numeric($mid)){
- $cateid = topsnavs($mid);
- $cateinfo = Db::name("category")->where('id','in',$cateid)->field("title,href")->select()->toArray();
- if(!empty($cateinfo)){
- foreach($cateinfo as $val){
- $html .= '<span>'.$symbol.'</span><a href="http://'.$_SERVER["SERVER_NAME"].$val['href'].'">'.$val["title"].'</a>';
- }
- }else{
- $html .= '<span>'.$symbol.'</span><a>栏目丢了</a>';
- }
- }
- return $html;
- }
- function topsnavs($pid){
- $list = $pid;
- if(is_numeric($list)){
- $top = Db::name("category")->where("id",$list)->field("id,pid")->select()->toArray();
- if(!empty($top)){
- foreach($top as $vs){
- $list .= "," . $vs['id'];
- if($vs['pid'] != 0){
- $list .= ",".topsnavs($vs['pid']);
- }
- }
- $a = explode(",",$list);
- $b = array_unique($a);
- $c = implode(",",array_reverse($b));
- $list = trim($c,',');
- }
- }
- return $list;
- }
- //处理tags/和key的调用
- function tagkey($limt,$field,$order,$catid="",$matid=""){
- $list = [];
- if(empty($catid) && empty($matid)){
- $tab = Db::name("model")->field("tablename")->select();
- foreach($tab as $v){
- $det = Db::name($v['tablename'])->order($order)->limit($limt)->field($field)->select()->toArray();
- if($det){
- $list[] = $det;
- }
- }
- if(!empty($list)){
- $list = ary3_ary2($list);
- $l = array_column($list,$field);
- $list = array_filter(array_unique(explode(",",implode(",",$l))));
- }else{
- $list = array();
- }
- }
- if($catid){
- $modid = Db::name("category")->field("modid")->find($catid);
- $tab = Db::name("model")->field("tablename")->find($modid['modid']);
- $list = Db::name($tab['tablename'])->order($order)->limit($limt)->field($field)->select()->toArray();
- $l = array_column($list,$field);
- $list = array_unique(explode(",",implode(",",$l)));
- }
- if($matid){
- $tab = Db::name("model")->field("tablename")->select();
- foreach($tab as $v){
- $det = Db::name($v['tablename'])->field($field)->find($matid);
- if($det){
- $list = $det;
- }
- }
- $list = array_unique(explode(",",$list[$field]));
- }
- return $list;
- }
- //处理幻灯标签查询
- function getppt($limt,$order,$operate,$slide="",$top="",$tab=""){
- $where[] = ["status","=",0];
- $where[] = ["delete_time","=",NULL];
- $field = "id,title,ftitle,titlepic,create_time";
- $list = "";
- if(!empty($slide) && $slide == 'true'){
- $where[] = ["ppts","=",1];
- }
- if(!empty($top) && is_numeric($top) ){
- $where[] = ["top","=",$top];
- }
- if(!in_array($operate,array("0","1","2")) || !is_numeric($operate)){//不在类型内 结束
- return;
- }
- if($operate == "0"){
- $tabs = Db::name("model")->field("tablename")->select();
- foreach($tabs as $v){
- $list = Db::name($v["tablename"])->where($where)->limit($limt)->order($order)->field($field)->select();
- }
- }elseif($operate == "1"){
- if(!is_numeric($tab) || empty($tab)){
- return;
- }
- $where[] = ["mid","=",$tab];
- $modid = Db::name("category")->field("modid")->find($tab);
- $tabn = Db::name("model")->field("tablename")->find($modid["modid"]);
- $list = Db::name($tabn["tablename"])->where($where)->limit($limt)->order($order)->field($field)->select();
- }elseif($operate == "2"){
- if(empty($tab)){
- return;
- }
- $list = Db::name($tab)->where($where)->limit($limt)->order($order)->field($field)->select();
- }
- if(empty($list)){
- $list =array();
- }
- return $list;
- }
- //评论标签获取对应文章ID、标题、标题图片、、
- function matsection($aid){
- if(!empty($aid)){
- $tab = Db::name("model")->field("tablename")->select();
- $array= "";
- foreach($tab as $v){
- $info = Db::name($v["tablename"])->field("id,title,titlepic")->find($aid);
- if($info){
- $array = $info;
- }
- }
- return $array;
- }
- return;
- }
-
- /*
- *muysubstr($str, $start=0, $length, $charset=”utf-8″, $suffix=true)
- *$str:要截取的字符串
- *$start=0:开始位置,默认从0开始
- *$length:截取长度
- *$charset=”utf-8″:字符编码,默认UTF-8
- *$suffix=true:是否在截取后的字符后面显示符号,可任意的定义
- *
- *{$val.title|muysubstr=###,0,7,'utf-8'}
- *
- */
- function muysubstr($str, $start=0, $length, $charset="utf-8", $suffix="")
- {
- if(function_exists("mb_substr")){
- return mb_substr($str, $start, $length, $charset).$suffix;
- }elseif(function_exists('iconv_substr')){
- return iconv_substr($str,$start,$length,$charset).$suffix;
- }
- $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
- $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
- $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
- $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
- preg_match_all($re[$charset], $str, $match);
- $slice = join("",array_slice($match[0], $start, $length));
- if($suffix) return $slice.$suffix;
- return $slice;
- }
- /**
- *filepg 远程file_get_contents方法
- * $url 远程地址
- * $form 请求方式
- * $array 携带参数
- * *
- **/
- function filepg($url,$form,$array){
- if(!empty($url) && !empty($form) && !empty($array)){
- $postUrl = $url;
- $curlPost = $array;
- $postdatas = http_build_query($curlPost);//处理请求数据,生成一个经过 URL-encode 的请求字符串
- $options = array(
- 'http' => array(
- 'method' => $form,//请求方式POST
- 'header' => 'Content-type:application/x-www-form-urlencoded',
- 'content' => $postdatas,
- 'timeout' => 5 // 超时时间(单位:s)
- )
- );
- $context = stream_context_create($options);//创建并返回文本数据流
- $result = file_get_contents($postUrl, false, $context);//将文本数据流读入字符串
- $da = json_decode($result,true);
- return $da;
- }
- return jsonmsg(0,'参数错误!');
- }
- /**
- *严格控制上传文件可上传路径
- * url被检测路径
- * 单独定义函数是为了方便index和admin模块使用 减少不必要的代码
- * *
- **/
- function upcheckurl($url){
- $checkurl = array("/public/upload/userimages","/public/upload/files","/public/upload/ggpic","/public/upload/images","/public/upload/linkpic","/public/upload/menubg");
- if(in_array($url,$checkurl)){
- return $url;
- }else{
- return "false";
- }
- }
- /**
- * 修改扩展配置文件
- * @param array $arr 需要更新或添加的配置
- * @param string $file 配置文件名(不需要后辍)
- * @return bool
- */
- function editconfig($arr = [], $file = 'databaseoff')
- {
- if (is_array($arr)) {
- if($file ==="databaseoff"){
- $filename = $file.".php";
- $filepath = Env::get("config_path") . $filename;
- if (!file_exists($filepath)) {
- $conf = "<?php return [];";
- file_put_contents($filepath, $conf);
- }
- $conf = include $filepath;
- foreach ($arr as $key => $value) {
- $conf[$key] = $value;
- }
- $time = date('Y/m/d H:i:s');
- $str = "<?php\r\n/**\r\n * MuYuCMS安全配置文件.\r\n * $time\r\n */\r\nreturn [\r\n";
- foreach ($conf as $key => $value) {
- $str .= "\t'$key' => '$value',";
- $str .= "\r\n";
- }
- $str .= '];';
- file_put_contents($filepath, $str);
- return true;
- }else{
- return false;
- }
- } else {
- return false;
- }
- }
|