// +---------------------------------------------------------------------- namespace app\admin\controller; use think\Db; use think\Validate; class Plug extends Base { protected $files_res_exists; protected $files_res_used; protected $files_unused; /* * 留言列表 * @author rainfer <81818832@qq.com> */ public function plug_sug_list() { $plug_sug=Db::name('plug_sug')->order('plug_sug_open,plug_sug_addtime desc')->paginate(config('paginate.list_rows')); $show=$plug_sug->render(); $show=preg_replace("(]*page[=|/](\d+).+?>(.+?)<\/a>)","$2",$show); $this->assign('plug_sug',$plug_sug); $this->assign('page',$show); if(request()->isAjax()){ return $this->fetch('ajax_plug_sug_list'); }else{ return $this->fetch(); } } /* * 留言回复返回数据 * @author rainfer <81818832@qq.com> */ public function plug_sug_reply() { $plug_sug_id=input('plug_sug_id'); $plug_sug=Db::name('plug_sug')->where(array('plug_sug_id'=>$plug_sug_id))->find(); $rule = [ ['plug_sug_email','require|email','留言邮箱为空|留言邮箱格式不正确'], ]; $validate = new Validate($rule); $rst = $validate->check(array( 'plug_sug_email'=>$plug_sug['plug_sug_email'] )); if(true !==$rst){ $sl_data['code']=0; $sl_data['msg']=$validate->getError(); }else{ $sl_data['plug_sug_email']=$plug_sug['plug_sug_email']; $sl_data['plug_sug_name']=$plug_sug['plug_sug_name']; $sl_data['plug_sug_id']=$plug_sug['plug_sug_id']; $sl_data['code']=1; } return json($sl_data); } /* * 留言回复 * @author rainfer <81818832@qq.com> */ public function plug_sug_runreply() { $email=input('plug_sug_toemail'); $name=input('plug_sug_toname'); $plug_sug_id=input('plug_sug_id'); $content=htmlspecialchars_decode(input('plug_sug_replycontent')); $send_result=sendMail($email,"Reply:". $name, $content); if($send_result['error']){ $this->error('邮箱设置不正确或对方邮箱地址不存在',url('admin/Plug/plug_sug_list')); }else{ $rst=Db::name('plug_sug')->where('plug_sug_id',$plug_sug_id)->setField('plug_sug_open',1); if($rst!==false){ $this->success('回复留言成功',url('admin/Plug/plug_sug_list')); }else{ $this->error('回复留言失败',url('admin/Plug/plug_sug_list')); } } } /* * 留言删除 * @author rainfer <81818832@qq.com> */ public function plug_sug_del() { $p = input('p'); $plug_sug_id=input('plug_sug_id'); $rst=Db::name('plug_sug')->where(array('plug_sug_id'=>$plug_sug_id))->delete(); if($rst!==false){ $this->success('留言删除成功',url('admin/Plug/plug_sug_list',array('p'=>$p))); }else{ $this->error('留言删除失败',url('admin/Plug/plug_sug_list',array('p'=>$p))); } } /* * 留言删除(全选) * @author rainfer <81818832@qq.com> */ public function plug_sug_alldel() { $p = input('p'); $ids = input('sug_id/a'); if(empty($ids)){ $this -> error("请选择删除留言",url('admin/Plug/plug_sug_list',array('p'=>$p))); } if(is_array($ids)){ $where = 'plug_sug_id in('.implode(',',$ids).')'; }else{ $where = 'plug_sug_id='.$ids; } $rst=Db::name('plug_sug')->where($where)->delete(); if($rst!==false){ $this->success("留言删除成功!",url('admin/Plug/plug_sug_list',array('p'=>$p))); }else{ $this -> error("删除留言失败!",url('admin/Plug/plug_sug_list',array('p' => $p))); } } /* * 本地文件列表 * @author rainfer <81818832@qq.com> */ public function plug_file_list() { $map=array(); //查询:时间格式过滤 $sldate=input('reservation','');//获取格式 2015-11-12 - 2015-11-18 $arr = explode(" - ",$sldate);//转换成数组 if(count($arr)==2){ $arrdateone=strtotime($arr[0]); $arrdatetwo=strtotime($arr[1].' 23:55:55'); $map['uptime'] = array(array('egt',$arrdateone),array('elt',$arrdatetwo),'AND'); } //查询文件路径 $val=input('val'); if(!empty($val)){ $map['path']= array('like',"%".$val."%"); } $plug_files=Db::name('plug_files')->where($map)->order('id desc')->paginate(config('paginate.list_rows'),false,['query'=>get_query()]); $show = $plug_files->render(); $show=preg_replace("(]*page[=|/](\d+).+?>(.+?)<\/a>)","$2",$show); $this->assign('page',$show); $this->assign('plug_files',$plug_files); $this->assign('sldate',$sldate); $this->assign('val',$val); if(request()->isAjax()){ return $this->fetch('ajax_plug_file_list'); }else{ return $this->fetch(); } } /** * 文件过滤 */ public function plug_file_filter() { //获取本地文件数组,'./data/upload/2016-01-21/56a03ff96b6ff.jpg' => int 224138 $file_list=list_file('data/upload'); $path="/data/upload/"; $this->files_res_exists=array(); foreach ($file_list as $a){ if ($a ['isDir']) { foreach (list_file($a ['pathname'] . '/') as $d) { if (!$d ['isDir']) { //文件 if($d['ext']!='html' && $d['ext']!='lock'){ $this->files_res_exists [$path . $a ['filename'] . '/' . $d ['filename']] = $d ['size']; } } } } } //获取数据表datafile已存记录,并删除资源数组里的成员,完毕后得到未存入数据表datafile的资源文件 $datas = Db::name('plug_files')->select(); if (is_array($datas)) { foreach ($datas as &$d) { $f = $d ['path']; if (isset ($this->files_res_exists [$f])) { unset ($this->files_res_exists [$f]); } } } //未存入数据表的数据写入数据表 $time=time(); foreach ($this->files_res_exists as $d => $v) { Db::name('plug_files')->insert(array( 'path' => $d, 'uptime' => $time, 'filesize' => $v )); } //获取利用到的资源文件 $this->files_res_used=array(); //avatar,涉及表admin里字段admin_avatar,member_list里member_list_headpic,头像只保存头像图片名 $datas = Db::name('admin')->select(); if (is_array($datas)) { foreach ($datas as &$d) { if($d['admin_avatar']){ if(stripos($d['admin_avatar'],'http')===false){ //本地头像 $this->files_res_used['/data/upload/avatar/' . $d['admin_avatar']]=true; } } } } $datas = Db::name('member_list')->select(); if (is_array($datas)) { foreach ($datas as &$d) { if($d['member_list_headpic']){ if(stripos($d['member_list_headpic'],'http')===false){ //本地头像 $this->files_res_used['/data/upload/avatar/' . $d['member_list_headpic']]=true; } } } } //news里的news_img,news_pic_allurl,news_content $datas = Db::name('news')->select(); if (is_array($datas)) { foreach ($datas as &$d) { //字段保存'/data/....' if($d['news_img']){ if(stripos($d['news_img'],'http')===false){ $this->files_res_used[$d['news_img']]=true; } } //字段保存'/data/....' if($d['news_pic_allurl']){ $imgs=array_filter(explode(",",$d['news_pic_allurl'])); foreach ($imgs as &$f) { if(stripos($f,'http')===false && !empty($f)){ $this->files_res_used[$f]=true; } } } if($d['news_content']){ //匹配'/网站目录/data/....' $preg_match=__ROOT__.'\/data\/upload\/([0-9]{4}[-][0-9]{2}[-][0-9]{2}\/[a-z0-9]{13}\.[a-z0-9]+)/i'; @preg_match_all($preg_match, $d['news_content'], $mat); if(!empty($mat [1])){ foreach ($mat [1] as &$f) { $this->files_res_used['/data/upload/'.$f]=true; } } //匹配'./data/....' $preg_match='/\.\/data\/upload\/([0-9]{4}[-][0-9]{2}[-][0-9]{2}\/[a-z0-9]{13}\.[a-z0-9]+)/i'; @preg_match_all($preg_match, $d['news_content'], $mat); if(!empty($mat [1])){ foreach ($mat [1] as &$f) { $this->files_res_used['/data/upload/'.$f]=true; } } } } } //options里'option_name'=>'site_options'的site_logo、site_qr,字段保存'/data/....' $datas = Db::name('options')->where(array('option_name'=>'site_options'))->select(); if (is_array($datas)) { foreach ($datas as &$d) { if($d['option_value']){ $smeta=json_decode($d['option_value'],true); if($smeta['site_logo'] && stripos($smeta['site_logo'],'http')===false){ $this->files_res_used[$smeta['site_logo']]=true; } if(!empty($smeta['site_qr']) && stripos($smeta['site_qr'],'http')===false){ $this->files_res_used[$smeta['site_qr']]=true; } } } } //plug_ad里plug_ad_pic,字段保存'/data/....' $datas = Db::name('plug_ad')->select(); if (is_array($datas)) { foreach ($datas as &$d) { if($d['plug_ad_pic']){ if(stripos($d['plug_ad_pic'],'http')===false){ //本地图片 $this->files_res_used[$d['plug_ad_pic']]=true; } } } } //menu里menu_img,字段保存'/data/....' $datas = Db::name('menu')->select(); if (is_array($datas)) { foreach ($datas as &$d) { if($d['menu_img']){ if(stripos($d['menu_img'],'http')===false){ //本地图片 $this->files_res_used[$d['menu_img']]=true; } } } } //model自定义模型 $table_arr=Db::name('model')->column('model_name,model_fields','model_name'); foreach ($table_arr as $table=>$fields){ $fields=json_decode($fields,true); foreach ($fields as $field){ //文件或图片或富文本字段 if(in_array($field['type'],['imagefile','images','file','files','richtext'])){ $datas=Db::name($table)->field($field['name'])->select(); foreach ($datas as $d) { if($d[$field['name']]){ if($field['type']=='richtext'){ //匹配'/网站目录/data/....' $preg_match=__ROOT__.'\/data\/upload\/([0-9]{4}[-][0-9]{2}[-][0-9]{2}\/[a-z0-9]{13}\.[a-z0-9]+)/i'; @preg_match_all($preg_match, $d[$field['name']], $mat); if(!empty($mat [1])){ foreach ($mat [1] as &$f) { $this->files_res_used['/data/upload/'.$f]=true; } } //匹配'./data/....' $preg_match='/\.\/data\/upload\/([0-9]{4}[-][0-9]{2}[-][0-9]{2}\/[a-z0-9]{13}\.[a-z0-9]+)/i'; @preg_match_all($preg_match, $d[$field['name']], $mat); if(!empty($mat [1])){ foreach ($mat [1] as &$f) { $this->files_res_used['/data/upload/'.$f]=true; } } }elseif($field['type']=='imagefile' || $field['type']=='file'){ if(stripos($d[$field['name']],'http')===false){ //本地图片 $this->files_res_used[$d[$field['name']]]=true; } }else{ //字段保存'/data/....' if($d[$field['name']]){ $imgs=array_filter(explode(",",$d[$field['name']])); foreach ($imgs as &$f) { if(stripos($f,'http')===false && !empty($f)){ $this->files_res_used[$f]=true; } } } } } } } } } //找出未使用的资源文件 $this->files_unused=array(); $ids=array(); $datas = Db::name('plug_files')->select(); if (is_array($datas)) { foreach ($datas as &$d) { $f = $d ['path']; if (isset ($this->files_res_used[$f])) { unset ($this->files_res_used[$f]); } else { $ids[]=$d ['id']; $this->files_unused [] = array( 'id' => $d ['id'], 'filesize' =>$d['filesize'], 'path' => $f, 'uptime' => $d ['uptime'] ); } } } //数据库 $where=array(); $plug_files=array(); $show=''; if(!empty($ids)){ $where['id']=array('in',$ids); $plug_files=Db::name('plug_files')->where($where)->order('id desc')->paginate(config('paginate.list_rows')); $show = $plug_files->render(); $show=preg_replace("(]*page[=|/](\d+).+?>(.+?)<\/a>)","$2",$show); } $this->assign('plug_files',$plug_files); $this->assign('page',$show); if(request()->isAjax()){ return $this->fetch('ajax_plug_file_filter'); }else{ return $this->fetch(); } } /** * 文件删除(全选) */ public function plug_file_alldel(){ $p = input('p'); $ids = input('id/a'); if(empty($ids)){ $this -> error("请选择要删除的文件",url('admin/Plug/plug_file_filter',array('p'=>$p))); } if(is_array($ids)){ $where = 'id in('.implode(',',$ids).')'; foreach (Db::name('plug_files')->field('path')->where($where)->select() as $r) { $file = $r ['path']; if(stripos($file, "/")===0){ $file=substr($file,1); } if (file_exists($file)) { unlink($file); } } if (Db::name('plug_files')->where($where)->delete()!==false) { $this->success("删除文件成功!",url('admin/Plug/plug_file_filter',array('p'=>$p))); } else { $this->error("删除文件失败!",url('admin/Plug/plug_file_filter',array('p'=>$p))); } }else{ $r=Db::name('plug_files')->find($ids); if($r){ $file=$r['path']; if(stripos($file, "/")===0){ $file=substr($file,1); } if (file_exists($file)) { unlink($file); } if (Db::name('plug_files')->delete($ids)!==false) { $this->success("删除文件成功!",url('admin/Plug/plug_file_filter',array('p'=>$p))); }else{ $this->error("删除文件失败!",url('admin/Plug/plug_file_filter',array('p'=>$p))); } }else{ $this->error("删除文件失败!",url('admin/Plug/plug_file_filter',array('p'=>$p))); } } } /** * 文件删除 */ public function plug_file_del() { $id=input('id'); $p = input('p'); if (empty($id)){ $this->error('参数错误',url('admin/Plug/plug_file_filter',array('p'=>$p))); }else{ $r=Db::name('plug_files')->find($id); if($r){ $file=$r['path']; if(stripos($file, "/")===0){ $file=substr($file,1); } if (file_exists($file)) { unlink($file); } if (Db::name('plug_files')->delete($id)!==false) { $this->success("删除文件成功!",url('admin/Plug/plug_file_filter',array('p'=>$p))); }else{ $this->error("删除文件失败!",url('admin/Plug/plug_file_filter',array('p'=>$p))); } }else{ $this->error("删除文件失败!",url('admin/Plug/plug_file_filter',array('p'=>$p))); } } } }