<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace news\public_class;
use \temmoku\db;
use \temmoku\controller;
use \temmoku\Lib\Images_Get_Down;
use \temmoku\lib\custom_model;
use \news\init;
class article extends init{
	
	public function check($data=[]){
		if(!$_POST['post']['title']){
	    	(new Controller)->err('标题不可为空');
	    }
	    !isset($_POST['post']['bold']) && $_POST['post']['bold']=0;
	    $_POST['post']['bold']=intval($_POST['post']['bold']);
	    
	    //添加title之前需要预判缩略图
	    $_POST['post']['is_pic']=0;
	    if($_POST['post']['thumb']){
	    	//如果存在就是修改的
	    	if($data){
	    		if($_POST['post']['thumb'] !== $data['thumb']){
	    			if($data['thumb']){
	    				$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
	    				//删除备份文件
						del_file($oldthumb);
	    			}
	    			$_POST['post']['thumb']=UploadFiles_MOVE($_POST['post']['thumb'],M_MODULE,TRUE);
	    		}
	    		$_POST['post']['is_pic']=1;
	    	}else{
	    		$_POST['post']['thumb']=UploadFiles_MOVE($_POST['post']['thumb'],M_MODULE,TRUE);
	    		$_POST['post']['is_pic']=1;
	    	}	
	    }else{
	    	//如果存在就是修改的
	    	if($data){
	    		$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
				//删除备份文件
				del_file($oldthumb);
	    	}
	    }
	    $cidDB=db::select('*')->from(M_jab."category")->where("cid='{$_POST[post][cid]}'")->row();
	    
	    $_POST['post']['name']=$cidDB['name'];
	    
	    if($_POST['post']['thumb']){
	    	$is_pic='1';
	   	}
	   	
	    if(!$_POST['post']['description']){
	    	$description=htmlspecialchars_decode($_POST['post']['content']);
	    	$_POST['post']['description']=Get_word_strpos($description);
	    }
	    if($data){
	    	$_POST['post']['uid']=$data['uid'];
	   		$_POST['post']['username']=$data['username'];
	    }else{
	    	$_POST['post']['uid']=C('mydb.uid');
	   		$_POST['post']['username']=C('mydb.username');
	    }
		if('3'==C('mydb.groupid')){
			$_POST['post']['state']=1;
		}else{
			$power=unserialize($cidDB['setting'])['power'][C('mydb.groupid')];
			//存在则是修改
			if($data){
				$power['is_edit_state'] ? $_POST['post']['state']=1 : $_POST['post']['state']=0;
			}else{//新发
				$power['is_state'] ? $_POST['post']['state']=1 : $_POST['post']['state']=0;
			}
		}
		if($_POST['post']['state']){
			$_POST['post']['state_time']=NOWTIME;
		}
		if($_POST['post']['posttime']){
			$_POST['post']['posttime']=strtotime($_POST['post']['posttime']);
		}else{
			$_POST['post']['posttime']=NOWTIME;
		}
		//判断加密项
		if($_POST['post']['password']){
			if($data){
				if($_POST['post']['password'] != $data['password']){
					$_POST['post']['password']=md5($_POST['post']['password']);
				}
			}else{
				$_POST['post']['password']=md5($_POST['post']['password']);
			}
		}
		$_POST['post']['is_comment']=intval($_POST['post']['is_comment']);
		//标题替换屏蔽词
		$_POST['post']['title']=Shielding_words($_POST['post']['title']);
		//判断是否删除外链 ,如果是的话就删除白名单内的所有外链
		if('1'==C('Content_Del_link')){
			$_POST['post']['content']= Articles_Del_link(htmlspecialchars_decode($_POST['post']['content']),C('Content_link_White_list'));
		}
		//判断是否图片本地化
		if('1'==C('Images_Get_Down')){
			$pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
			$content=htmlspecialchars_decode($_POST['post']['content']);
			if(preg_match_all($pattern,$content,$match)){
				$_POST['post']['content']= htmlspecialchars((new Images_Get_Down)->Get_img_url($content,$match));
			}
		}
		
		//内容替换屏蔽词
		$_POST['post']['content']=Shielding_words($_POST['post']['content']);
		
		//替换网址
	    $_POST['post']['content']=str_replace  (WEBURL.'/'.C('upfiles_Catalog'), 'WWW_TEMMOKU_CN'   , $_POST['post']['content']);
	}
	
	public function title($post,$step='post')
    {
		
    	$array=[
		    'cid'=>$post['cid'],
		    'name'=>$post['name'],
		    'mid'=>$post['mid'],
		    'uid'=>$post['uid'],
		    'username'=>$post['username'],
		    'state'=>$post['state'],
		    'state_time'=>$post['state_time'],
		    'title'=>$post['title'],
		    'seotitle'=>$post['seotitle'],
		    'color'=>$post['color'],
		    'bold'=>$post['bold'],
		    'keyword'=>$post['keyword'],
		    'description'=>$post['description'],
		    'author'=>$post['author'],
		    'source'=>$post['source'],
		    'source_url'=>$post['source_url'],
		    'thumb'=>$post['thumb'],
		    'is_pic'=>(int)$post['is_pic'],
		    'posttime'=>(int)$post['posttime'],
		    'clicks'=>(int)$post['clicks'],
		    'password'=>$post['password'],
		    'sell_jifen'=>(int)$post['sell_jifen'],
		    'is_comment'=>(int)$post['is_comment'],
		];
		if('post'===$step){
			$array['reorder']=NOWTIME;
			$aid = db::insert(M_jab."article")->cols($array)->query();
		}else{
			db::update(M_jab."article")->cols($array)->where("aid=$post[aid]")->query();
		}
    	
		return $aid;
    }

	public function content($post,$step='post'){
		if('post'===$step){
			$content=[
				'aid'=>$post['aid'],
				'content'=>$post['content']
			];
			db::insert(M_jab."content")->cols($content)->query();	
		}else{
			db::update(M_jab."content")->cols(['content'=>$post['content']])->where("nid='{$post[nid]}'")->query();
		}
	}
	
    public function module($aid,$mid,$data,$step='post'){
		if("post"===$step){
			$data['aid']=$aid;
			db::insert(M_jab."content_$mid")->cols($data)->query();
		}else{
			$module=db::select('*')->from(M_jab."content_$mid")->where("aid=$aid")->row();
			if($module){
				db::update(M_jab."content_$mid")->cols($data)->where("aid=$aid")->query();
			}else{
				$article=db::select('*')->from(M_jab."article")->where("aid=$aid")->row();
				//等于0 说明以前没有发布
				if('0' == $article['mid']){
					$data['aid']=$aid;
					db::insert(M_jab."content_$mid")->cols($data)->query();
				}
			}
		}
	}
	
	public function del($aid){
		
    	$row=db::select('*')->from(M_jab."article")->where("aid= '$aid' ")->row();
    	
    	if($row['thumb']){
    		$oldfile=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$row['thumb'];
    		//删除文件
			del_file($oldfile);
    	}
		db::delete(M_jab."article")->where("aid= '$row[aid]' ")->query();
		//删除专题
		db::delete(M_jab."special_article")->where("aid= '$row[aid]' ")->query();
		//删除属性值
		db::delete(M_jab."attribute_list")->where("aid= '$row[aid]' ")->query();
		$content=db::select('*')->from(M_jab."content")->where("aid= '$aid' ")->query();
		foreach($content as $_row){
			$pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";  
			preg_match_all($pattern, htmlspecialchars_decode($_row['content']), $matches);
			foreach($matches[1] AS $IMG){
				if(stristr($IMG,'WWW_TEMMOKU_CN')){
					$path=Temmoku_PATH.C('upfiles_Catalog');
					$IMG=str_replace  ( 'WWW_TEMMOKU_CN', $path  , $IMG );
					//删除备份文件
					del_file($IMG);
				}
			}
		}
		db::delete(M_jab."content")->where("aid= '$row[aid]' ")->query();

		if($row['mid']){
			//删除附件
			$edit_Custom_Model=db::select('*')->from(M_jab."content_$row[mid]")->where("aid= '$row[aid]' ")->row();
			$_Model=unserialize(C(self::$dir.'|WN_MODELDB.'.$row['mid'])['setting']);
			foreach($_Model AS $KEY=>$VALUE){
				$_ModelDB[$KEY]='';
			}
			(new Custom_Model)->module_post_data($_Model,$_ModelDB,$edit_Custom_Model,true);
			db::delete(M_jab."content_$row[mid]")->where("aid= '$row[aid]' ")->query();
		}
	}
	public function get_special_category($upsid=0,$data=[]){
		static $lists;
		
		$list=db::select('*')->from(M_jab.'special_category')->where("upsid=$upsid")->query();
		
		foreach($list AS $key=>$row){
			
			$nbsp="";
			for($i=0;$i<$row['level'];$i++){
				$nbsp.="&nbsp;&nbsp;&nbsp;";
			}
			$nbsp.="|-";
			if(!$row['upsid']){
				
			}
			$selected='';
			if($data){
				$is=in_array($row['sid'],$data);
				$is ? $selected="selected" : $selected='';
			}
			$lists .= "<option value='$row[sid]' $selected>$nbsp$row[name]</option>\n";
			$this->get_special_category($row['sid'],$data);
		}
		
		return $lists;
	}
	
	public function get_all_special_list(){
		
		$list=db::select('*')->from(M_jab.'special_list')->where("1")->query();
		return $list;
	}
	
	//入库专题
	public function post_article_special($step='post',$aid){
		
		if($step=='post'){
			if($_POST['special']){
				foreach($_POST['special'] AS $id){
					$array=['lid'=>$id,'aid'=>$aid,'uid'=>C('mydb.uid'),'username'=>C('mydb.username'),'posttime'=>NOWTIME,'reorder'=>$aid];
					db::insert(M_jab."special_article")->cols($array)->query();
				}
			}elseif(C(self::$dir.'|setting.special_is_open')==1){
				$special_list=db::select('*')->from(M_jab.'special_list')->where("keyword != ''")->query();
				$title=$_POST['post']['title'];
				foreach($special_list AS $row){
					$row['keyword']=trim($row['keyword']);
					$_keyword=explode(' ',$row['keyword']);
					foreach($_keyword AS $keyword){
						$pattern ="/$keyword/i";
						$match=preg_match($pattern,$title);
						if($match){
							$array=['lid'=>$row[id],'aid'=>$aid,'uid'=>C('mydb.uid'),'username'=>C('mydb.username'),'posttime'=>NOWTIME,'reorder'=>$aid];
							db::insert(M_jab."special_article")->cols($array)->query();
							if(C(self::$dir.'|setting.special_is_fit')==1){
								break;
							}else{
								return;
							}
						}
					}
				}
			}
		}else{
			if($_POST['special']){
				//获取本篇文章的所有所在专题
				$_selected_special_list=db::select('lid')->from(M_jab."special_article")->where("aid = $aid")->query();
				foreach($_selected_special_list AS $row){
					if(!in_array($row['lid'],$_POST['special'])){
						db::delete(M_jab."special_article")->where('lid='.$row['lid'])->query();
					}
					$special[]=$row['lid'];
				}
				foreach($_POST['special'] AS $id){
					if($special && in_array($id,$special)){
						continue;
					}
					$array=['lid'=>$id,'aid'=>$aid,'uid'=>C('mydb.uid'),'username'=>C('mydb.username'),'posttime'=>NOWTIME,'reorder'=>$aid];
					db::insert(M_jab."special_article")->cols($array)->query();
				}
			}else{
				db::delete(M_jab."special_article")->where('aid='.$aid)->query();
			}
		}
	}
	public function allcidDB($cid="0",$table=''){
	   	static $allcidDB=[];
		$table=	$table ? $table : M_jab."category";
		$query=db::select('*')->from($table)->where("upcid = $cid")->orderByDESC(['reorder'])->query();
		if($query){
			foreach($query AS $key=>$row){
				$level="";
				for($i=0;$i<$row['level'];$i++){
					$level.="&nbsp;&nbsp;&nbsp;";
				}
				$level.="|-";
				$row['levels']=$level;
				$allcidDB[] = $row;
				$this->allcidDB($row['cid'],$table);
			}
		}
		return $allcidDB; 
	}
	public function sell_jifen($jifen,$msg=''){
    	return '
	    	<form class="layui-form" method="post" action="">
	    		<table class="layui-table my-tabel">
	    			<tbody>
	    				<tr>
	    					<td colspan="2">本篇文章是付费文章,需要支付“<span class="red">'.$jifen.'</span>”积分</td>
	    				</tr>
	    				<tr>
	    					<td colspan="2">你现在拥有“<span class="red">'.C('mydb.jifen').'</span>”积分</td>
	    				</tr>
	    				<tr>
	    					<td class="my-button center">
	    						<button class="layui-btn" name="step" value="payment">立即支付查看</button>
	    					</td>
	    				</tr>
	    			</tbody>
	    		</table>
	    	</form>
    	';
    }
    public function pass($msg=''){
    	if($msg){
    		$msg='
    			<tr>
	    			<td colspan="2"><span class="center red">'.$msg.'</span></td>
	    		</tr>';
    	}
    	return '
	    	<form class="layui-form" method="post" action="">
	    		<table class="layui-table my-tabel">
	    			<tbody>
	    				<tr>
	    					<td colspan="2">本篇文章是加密文章，需要您输入正确密码才可以查看</td>
	    				</tr>
	    				'.$msg.'
	    				<tr>
	    					<td>
	    						<input name="password" placeholder="请输入查看密码" class="layui-input my-input" type="password">
	    					</td>
	    				</tr>
	    				<tr>
		    				<td class="my-button center">
		    					<button class="layui-btn" name="step" value="pass">立即查看</button>
		    				</td>
		    			</tr>
	    			</tbody>
	    		</table>
	    	</form>
    	';
    }
}
?>