<?PHP
date_default_timezone_set('PRC'); 
$mb = $_POST['mb'];//模板
$b = $_POST['b'];//操作标识

//织梦
if($mb == 'dede_'){
	require_once 'data/common.inc.php';
	require_once ('data/safe/inc_safe_config.php');
	require_once ('data/config.cache.inc.php');
	$myconn=mysqli_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
	mysqli_set_charset($myconn,$cfg_db_language);
	mysqli_select_db($myconn,$cfg_dbname);
	$_dbpre = $cfg_dbprefix; //数据库表头
}

//wp
if($mb == 'wp_'){
	$db = require_once 'wp-config.php';
	date_default_timezone_set('PRC');
	$myconn=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD);
	mysqli_set_charset($myconn,DB_CHARSET);
	mysqli_select_db($myconn,DB_NAME);
	$_dbpre = $table_prefix; //数据库表头
}

//获取栏目
if($b == "1"){
	$s1 = str_replace(' ', '+',$_POST['s1']);
	$s1 = base64_decode($s1);
	$s1 = mb_convert_encoding($s1,"UTF-8", "GBK");
	$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$s1));
	$max_num = mysqli_num_rows($result);
	$i = 0;$str = "";
	while($row=mysqli_fetch_array($result)){
		$i = $i +1 ;
		if($i < $max_num){$str = $str.$row["typename"]."|".$row["id"].",";
		}else{$str = $str.$row["typename"]."|".$row["id"];}
	}
	echo "<lm>".$str."</lm>";
}

//新增,WP专用
if($b == "2"){
	$tlsql = str_replace(' ', '+',$_POST['tlsql']);//标题查询sql
	if($tlsql !=''){
		$tlsql = base64_decode($tlsql);
		$tlsql = mb_convert_encoding($tlsql,"UTF-8", "GBK");
		$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$tlsql));
		$row = mysqli_fetch_array($result); 
		$id = $row["id"];
	}else{
		$id = '';
	}
	
	if($id!=""){
		echo '[fail]';
	}else{
		//下载缩略图
		$s1 = str_replace(' ', '+',$_POST['s1']);
		$s1 = base64_decode($s1);
		$s1 = mb_convert_encoding($s1,"UTF-8", "GBK");
		$s1= str_replace($mb,$_dbpre,$s1);
		$s1= str_replace("\\","",$s1);
		$s2 = str_replace(' ', '+',$_POST['s2']);
		$s2 = base64_decode($s2);
		$s2 = mb_convert_encoding($s2,"UTF-8", "GBK");
		$s2= str_replace($mb,$_dbpre,$s2);

		$s2= str_replace("\\","",$s2);
		$mrimg="http://www.henxisoft.com/img/".$_POST['mrtpk']."/".rand(1,50).".jpg";

		//下载远程图片
		$s1 = getTu($s1,$mrimg,$mb);
		$s2 = getTu($s2,$mrimg,$mb);

		$id = $_POST['id'];
		$fenlei = $_POST['fenlei'];
		$s3="INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) VALUES ('".$id."', '".$fenlei."', '0')";
		$s3= str_replace($mb,$_dbpre,$s3);
		echo 's3:'.$s3;

		echo 'a:'.mysqli_query($myconn,$s1); 
		echo 'b:'.mysqli_query($myconn,$s2); 
		echo 'c:'.mysqli_query($myconn,$s3); 
	}
	mysqli_close($myconn);
}

//新增、修改文章信息
if($b == "3"){
	$tlsql = str_replace(' ', '+',$_POST['tlsql']);//标题查询sql
	if($tlsql !=''){
		$tlsql = base64_decode($tlsql);
		//$tlsql = mb_convert_encoding($tlsql,"UTF-8", "GBK");
		$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$tlsql));
		$row = mysqli_fetch_array($result); 
		$id = $row["id"];
	}else{
		$id = '';
	}
	//echo '文章id:'.$id;
	if($id!=""){
		echo '[fail]';
	}else{
		//下载缩略图
		/*
		$imgname = $_POST['mrtpk'];
		if($imgname == ''){
			$mrimg="http://www.henxisoft.com/img/img0/".rand(1,50).".jpg";
		}else{
			$mrimg="http://www.henxisoft.com/img/".$_POST['mrtpk']."/".rand(1,50).".jpg";
		}
		*/
		$mrimg="http://www.henxisoft.com/img/".rand(1,50).".jpg";
		$s1 = str_replace(' ', '+',$_POST['s1']);
		$s2 = str_replace(' ', '+',$_POST['s2']);
		$s3 = str_replace(' ', '+',$_POST['s3']);
		if($s1!=null){
			$s1 = base64_decode($s1);
			$s1 = mb_convert_encoding($s1,"UTF-8", "GBK");
			$s1 = str_replace($mb,$_dbpre,$s1);
			$s1 = setslt($s1,$mrimg,$mb);
			if($s2 == null){
				$s1 = getTu($s1,$mrimg,$mb);
			}
			//echo 's1:'.$s1;
		}
		
		if($s2!=null){
			$s2 = base64_decode($s2);
			$s2 = mb_convert_encoding($s2,"UTF-8", "GBK");
			$s2 = str_replace($mb,$_dbpre,$s2);
			$s2 = getTu($s2,$mrimg,$mb);
			
		}
		
		if($s3!=null){
			$s3 = base64_decode($s3);
			$s3 = mb_convert_encoding($s3,"UTF-8", "GBK");
			$s3 = str_replace($mb,$_dbpre,$s3);
			echo 's3:'.$s3;
		}
		
		if($s1!=null){echo 'a:'.mysqli_query($myconn,$s1);}else{echo 'a:1';}
		if($s2!=null){echo 'b:'.mysqli_query($myconn,$s2);}else{echo 'b:1';}
		if($s3!=null){echo 'c:'.mysqli_query($myconn,$s3);}else{echo 'c:1'; }
	}
	mysqli_close($myconn);
}

//获取文章最大id
if($b == "4"){
	$s1 = str_replace(' ', '+',$_POST['s1']);
	$s1 = base64_decode($s1);
	$s1 = mb_convert_encoding($s1,"UTF-8", "GBK");
	$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$s1));
	$row = mysqli_fetch_array($result); 
	$id = $row["id"] + 1;
	echo '<id>'.$id.'</id>';
	mysqli_close($myconn);
}

//获取栏目路径名称
if($b == "5"){
	$s1 = str_replace(' ', '+',$_POST['s1']);
	$s1 = base64_decode($s1);
	$s1 = mb_convert_encoding($s1,"UTF-8", "GBK");
	$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$s1));
	$row = mysqli_fetch_array($result); 
	echo '<info>'.$row["info"].'</info>';
	mysqli_close($myconn);
}

//删除
if($b == "6"){
	$s1 = str_replace(' ', '+',$_POST['s1']);
	$s1 = base64_decode($s1);
	$s1 = mb_convert_encoding($s1,"UTF-8", "GBK");
	echo mysqli_query($myconn,str_replace($mb,$_dbpre,$s1)); 

	$s2 = str_replace(' ', '+',$_POST['s2']);
	$s2 = base64_decode($s2);
	$s2 = mb_convert_encoding($s2,"UTF-8", "GBK");
	echo mysqli_query($myconn,str_replace($mb,$_dbpre,$s2)); 

	$s3 = str_replace(' ', '+',$_POST['s3']);
	$s3 = base64_decode($s3);
	$s3 = mb_convert_encoding($s3,"UTF-8", "GBK");
	echo mysqli_query($myconn,str_replace($mb,$_dbpre,$s3)); 
	mysqli_close($myconn);
}

//织梦专用
if($b == "7"){
	$enkey = substr(md5(substr($cfg_cookie_encode,0,5)),0,10);
	$sessSavePath = "sessions_{$enkey}";
	if ( !is_dir($sessSavePath) ) mkdir($sessSavePath);
	if(is_writeable($sessSavePath) && is_readable($sessSavePath)){ session_save_path($sessSavePath); }
	if(!empty($cfg_domain_cookie)) session_set_cookie_params(0,'/',$cfg_domain_cookie);
	@session_start();
	$_SESSION['securimage_code_value'] = strtolower($_POST['md5']);
}

//修改验证码
if($b == "8"){
	$file = file_get_contents("data/safe/inc_safe_config.php");
	$file = str_replace("1,2,3,4,5,6,7", "1,2,3,4,5,7", $file);
	$file1=fopen("data/safe/inc_safe_config.php","w");
	fwrite($file1, $file);
	fclose($file1);
}

//还原验证码
if($b == "9"){
	$file = file_get_contents("data/safe/inc_safe_config.php");
	$file = str_replace("1,2,3,4,5,7", "1,2,3,4,5,6,7", $file);
	$file1=fopen("data/safe/inc_safe_config.php","w");
	fwrite($file1, $file);
	fclose($file1);
}

//织梦插入标签
if($b == "10"){
	$id = $_POST['id'];
	$gjc = str_replace(' ', '+',$_POST['gjc']);
	$gjc = base64_decode($gjc);
	$gjc = mb_convert_encoding($gjc,"UTF-8", "GBK");
	$gjc1 = explode(",",$gjc);
	for ($i = 0; $i < count($gjc1); $i++){
		$s1 = "SELECT max(tid) as tid from dede_taglist";
		$s1 = str_replace($mb,$_dbpre,$s1);
		$result = mysqli_query($myconn,$s1);
		$row = mysqli_fetch_array($result); 
		$tid = $row["tid"] + 1;

		$s2 = "INSERT INTO dede_taglist (`tid`, `aid`, `arcrank`, `typeid`, `tag`) VALUES ('".$tid."', '".$id."', '0', '1', '".$gjc1[$i]."');";
		echo $s2;
		$s2 = str_replace($mb,$_dbpre,$s2);
		echo 'tag:'.mysqli_query($myconn,$s2); 
	}
	mysqli_close($myconn);
}

//过滤%D，%A，%9字符
function delDA9($str){
	$str=str_replace("%D","",$str);
	$str=str_replace("%A","",$str);
	$str=str_replace("%9","",$str);
	return $str;
}


//新增友情链接
if($b == "11"){
	//$name = $_POST['name'];
	//$url = $_POST['url'];
	$name = str_replace(' ', '+',$_POST['name']);
	$name = base64_decode($name);
	$name = mb_convert_encoding($name,"UTF-8", "GBK");
	$url = str_replace(' ', '+',$_POST['url']);
	$url = base64_decode($url);
	$url = mb_convert_encoding($url,"UTF-8", "GBK");

	$tlsql = "SELECT * from dede_flink where url='".$url."'";
	echo $tlsql;
	if($tlsql !=''){
		$tlsql = mb_convert_encoding($tlsql,"UTF-8", "GBK");
		$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$tlsql));
		$row = mysqli_fetch_array($result); 
		$id = $row["id"];
	}else{
		$id = '';
	}
	//echo '文章id:'.$id;
	if($id!=""){
		echo '[fail]';
	}else{
		$s1 = "insert into dede_flink(sortrank,url,webname,dtime,typeid,ischeck) VALUES(1,'".$url."','".$name."',".time().",1,2)";
		$s1 = str_replace($mb,$_dbpre,$s1);
		echo $s1;
		if($s1!=null){echo 'a:'.mysqli_query($myconn,$s1);}else{echo 'a:1';}
	}
	mysqli_close($myconn);
}
//删除友情链接
if($b == "12"){
	$url = str_replace(' ', '+',$_POST['url']);
	$url = base64_decode($url);
	$url = mb_convert_encoding($url,"UTF-8", "GBK");
	//$url = $_POST['url'];	
	$s1 = "DELETE from dede_flink where url ='".$url."'";
	$s1 = str_replace($mb,$_dbpre,$s1);
	if($s1!=null){echo 'a:'.mysqli_query($myconn,$s1);}else{echo 'a:1';}
	mysqli_close($myconn);
}

//修改友情链接
if($b == "13"){
	$name = str_replace(' ', '+',$_POST['name']);
	$name = base64_decode($name);
	$name = mb_convert_encoding($name,"UTF-8", "GBK");
	$url = str_replace(' ', '+',$_POST['url']);
	$url = base64_decode($url);
	$url = mb_convert_encoding($url,"UTF-8", "GBK");
	$id = str_replace(' ', '+',$_POST['id']);
	$id = base64_decode($id);
	$id = mb_convert_encoding($id,"UTF-8", "GBK");
	//$name = $_POST['name'];
	//$url = $_POST['url'];
	//$id = $_POST['id'];
	$s1 = "UPDATE dede_flink SET url='".$url."', webname='".$name."'  WHERE id=".$id;
	$s1 = str_replace($mb,$_dbpre,$s1);
	if($s1!=null){echo 'a:'.mysqli_query($myconn,$s1);}else{echo 'a:1';}
	mysqli_close($myconn);
}

//查询友情链接
if($b == "14"){
	$s1 = "SELECT id,url,webname from dede_flink ";
	$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$s1));
	$str = "";
	while($row=mysqli_fetch_array($result)){
		$str = $str."<id>".$row["id"]."</id><webname>".$row["webname"]."</webname><url>".$row["url"]."</url>";	
	}
	echo $str;
	mysqli_close($myconn);
}

//获取友情数
if($b == "15"){
	$s1 = "SELECT count(id) as cn from dede_flink ";
	$result = mysqli_query($myconn,str_replace($mb,$_dbpre,$s1));
	$row = mysqli_fetch_array($result); 
	$cn = $row["cn"];
	echo $cn;
	mysqli_close($myconn);
}

/** 
*
*函数：调整图片尺寸或生成缩略图 
*返回：True/False 
*参数：
*   $Image   需要调整的图片(含路径) 
*   $Dw=450  调整时最大宽度;缩略图时的绝对宽度 
*   $Dh=450  调整时最大高度;缩略图时的绝对高度 
*   $Type=1  1,调整尺寸; 2,生成缩略图 
*/ 
$phtypes=array('img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png'); 

function compressImg($Image,$Dw,$Dh,$Type){  
    IF(!file_exists($Image)){  
        return false;  
    }  
    // 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)  
    // 当Type==1的时候，将不拷贝原图像文件，而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)  
    IF($Type!=1){  
        copy($Image,str_replace(".","_x.",$Image));  
        $Image=str_replace(".","_x.",$Image);  
    }  
    // 取得文件的类型,根据不同的类型建立不同的对象  
    $ImgInfo=getimagesize($Image);  
    Switch($ImgInfo[2]){  
        case 1:  
            $Img =@imagecreatefromgif($Image);  
            break;  
        case 2:  
            $Img =@imagecreatefromjpeg($Image);  
            Break;  
        case 3:  
            $Img =@imagecreatefrompng($Image);  
            break;  
    }  
    // 如果对象没有创建成功,则说明非图片文件  
    IF(Empty($Img)){  
        // 如果是生成缩略图的时候出错,则需要删掉已经复制的文件  
        IF($Type!=1){  
            unlink($Image);  
        }  
        return false;  
    }  
    // 如果是执行调整尺寸操作则  
    IF($Type==1){  
        $w=ImagesX($Img);  
        $h=ImagesY($Img);  
        $width = $w;  
        $height = $h;  
        IF($width>$Dw){  
            $Par=$Dw/$width;  
            $width=$Dw;  
            $height=$height*$Par;  
            IF($height>$Dh){  
                $Par=$Dh/$height;  
                $height=$Dh;  
                $width=$width*$Par;  
            }  
        } ElseIF($height>$Dh) {  
            $Par=$Dh/$height;  
            $height=$Dh;  
            $width=$width*$Par;  
            IF($width>$Dw){  
                $Par=$Dw/$width;  
                $width=$Dw;  
                $height=$height*$Par;  
            }  
        } Else {  
            $width=$width;  
            $height=$height;  
        }  
        $nImg =ImageCreateTrueColor($width,$height);// 新建一个真彩色画布  
        ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小  
        ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件  
        return true;  
    } Else {// 如果是执行生成缩略图操作则  
        $w=ImagesX($Img);  
        $h=ImagesY($Img);  
        $width = $w;  
        $height = $h;  
        $nImg =ImageCreateTrueColor($Dw,$Dh);  
        IF($h/$w>$Dh/$Dw){// 高比较大  
            $width=$Dw;  
            $height=$h*$Dw/$w;  
            $IntNH=$height-$Dh;  
            ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);  
        } Else {// 宽比较大  
            $height=$Dh;  
            $width=$w*$Dh/$h;  
            $IntNW=$width-$Dw;  
            ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);  
        }  
        ImageJpeg($nImg,$Image);  
        return true;  
    }  
};  
  
/** 
 *根据url获取服务器上的图片 
 *$url服务器上图片路径 $filename文件名 
*/  
function GrabImage($url,$filename="") {  
    if($url=="") return false;  
    if($filename=="") {  
        $ext=strrchr($url,".");  
        if($ext!=".gif" && $ext!=".jpg" && $ext!=".png" && $ext!=".jpeg" && $ext!=".bmp")  
            return false;  
        $filename=strtotime('now').rand(10,99).$ext;  
    }  
 
	ob_start();   
	readfile($url);
	$img = ob_get_contents();  
	ob_end_clean();  
	$size = strlen($img);   
  
	$fp2=@fopen($filename, "a");  
	fwrite($fp2,$img);  
	fclose($fp2);  
	return $filename; 

}  

/**
*判断url是否正确
*/
function isUrlTure($url){
	echo 'panduan:'.$url;
	$array = get_headers($url,1);
	if(preg_match('/200/',$array[0])){
		//print_r($array);
		return true;
	}else{
		echo "无效url资源！";
		return false;
	}
}

function setslt($s1,$mrimg,$mb){
	$dtml = date("Ymd"); //当天目录
	$imgurl=$_POST['imgurl'];
	$isslt=$_POST['isslt'];//是否下载缩略图
	echo 'mrimg:'.$mrimg;
	echo 'imgurl:'.$imgurl;
	if($isslt==''){$isslt='1';}
	if($isslt=='1'){
		if (!file_exists('img')){mkdir ('img',0777,true);} 
		if (!file_exists('img/'.$dtml)){mkdir ('img/'.$dtml,0777,true);} 
		$dir = 'img/'.$dtml.'/slt'.strtotime('now').rand(10,99).'.jpg';

		if($imgurl != '' and isUrlTure($imgurl)==true){
			$iurl = GrabImage($imgurl, $dir);//获取服务器上图片路径
			if($iurl != ''){
				var_dump(compressImg($iurl,240,165,1));
				$s1 = str_replace("[litpic]",'http://'.$_SERVER['HTTP_HOST'].'/'.$dir,$s1);
			}
		}else{
			if(isUrlTure($mrimg)==true){
				$iurl = GrabImage($mrimg, $dir);
				if($iurl != ''){
					var_dump(compressImg($iurl,240,165,1));
					$s1 = str_replace("[litpic]",'http://'.$_SERVER['HTTP_HOST'].'/'.$dir,$s1);
				}
			}
		}
	}	
	return $s1;
}



function getTu($s2,$mrimg,$mb){
	$isimg=$_POST['isimg'];
	if($isimg==''){$isimg='1';}
	$HTTPHOST = 'http://'.$_SERVER['HTTP_HOST'];
	$dtml = date("Ymd"); //当天目录
	if($isimg=='1'){
		if (!file_exists('img')){mkdir ('img',0777,true);} 
		if (!file_exists('img/'.$dtml)){mkdir ('img/'.$dtml,0777,true);} 
		$imgurl2=$_POST['imgurl2'];
		if($imgurl2!=''){
			$img = explode("--",$imgurl2);
			if(count($img)==0){
				$imgname = 'img/'.$dtml.'/'.strtotime('now').rand(10,99).'.jpg';
				if(isUrlTure($imgurl2)==true){
					if(GrabImage($imgurl2, $imgname) != ''){
						$s2 = str_replace($imgurl2,$HTTPHOST.'/'.$imgname,$s2);
						$s2 = str_replace("[img]","",$s2);
					}
				}
			}else{
				if(isUrlTure($img[0])==true){
					for ($i = 0; $i < count($img); $i++){
						if(isUrlTure($img[$i])==true){
							$imgname = 'img/'.$dtml.'/'.strtotime('now').rand(10,99).'.jpg';
							if(GrabImage($img[$i], $imgname) != ''){
								$s2 = str_replace($img[$i],$HTTPHOST.'/'.$imgname,$s2);
								$s2 = str_replace("[img]","",$s2);
							}
						}
					}
				}
			}
		}else{
			$imgname = 'img/'.$dtml.'/'.strtotime('now').rand(10,99).'.jpg';
			if(isUrlTure($mrimg)==true){
				if(GrabImage($mrimg, $imgname) != ''){
					$s2 = str_replace("[img]","<p><img src=".$HTTPHOST.'/'.$imgname."></p><br>",$s2);
				}
			}
		}
	}else{
		$s2 = str_replace("[img]","",$s2);
	}
	return $s2;
}

/**
* 请求接口返回内容
* @param  string $url [请求的URL地址]
* @param  string $params [请求的参数]
* @param  int $ipost [是否采用POST形式]
* @return  string
*/


function _base_juhecurl($url,$params = false,$ispost = 0)
{
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    if ( $ispost ) {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else {
        if ($params) {
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else {
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}

?>