<?php
/*******************************************************************************
* Ourphp - CMS建站系统
* Copyright (C) 2023 www.ourphp.net
* 开发者：哈尔滨伟成科技有限公司
*******************************************************************************/
include '../../../config/ourphp_code.php';
include '../../../config/ourphp_config.php';


if($_POST)
{
	
	if($_POST['token'] == $ourphp['safecode'])
	{
		
		$ourphp_rs = $db -> select("
		a.OP_Watermarkimg,
		a.OP_Watermarkfont,
		a.OP_Watermarkcolor,
		a.OP_Watermarksize,
		a.OP_Watermarkposition,
		a.OP_Watermarkoff,
		a.OP_Imgcompress,
		b.OP_Extfile
		","`ourphp_watermark` a, `ourphp_webdeploy` b","where a.id = 1 && b.id = 1");

		$OP_Watermarkimg = $ourphp_rs[0];
		$OP_Watermarkfont = $ourphp_rs[1];
		$OP_Watermarkcolor = $ourphp_rs[2];
		$OP_Watermarksize = $ourphp_rs[3];
		$OP_Watermarkposition = $ourphp_rs[4];
		$OP_Watermarkoff = $ourphp_rs[5];
		$OP_Imgcompress = $ourphp_rs[6];
		$OP_Extfile = explode("@",$ourphp_rs[7]);
		
		function ext($file){
			$file = explode(".",$file);
			$file = end($file);
			if(strpos($file,"?") !== false)
			{
				$index = strpos($file,"?");
				$file = substr($file,0,$index);	
			}
			return $file;
		}
		
		$ext_arr = array(
			'image' => explode(",",$OP_Extfile[0]),
			'flash' => explode(",",$OP_Extfile[1]),
			'media' => explode(",",$OP_Extfile[2]),
			'file' => explode(",",$OP_Extfile[3]),
			'mp4' => explode(",",$OP_Extfile[4]),
		);

		$file = urldecode($_POST['localUrl']);
		$filelist = str_replace("%7C","|",$file);
		$filelist = explode("|",rtrim($filelist,"|"));
		
		//创建文件夹
		$save_path .= "../../uploadfile/".date("Ymd")."/";
		if (!file_exists($save_path)) {
			mkdir($save_path,0777);
		}
		
		//生成index.htm防止遍历漏洞
		$index_file = fopen($save_path."index.htm",'w');
		fwrite($index_file,"ourphp！");
		fclose($index_file);
		
		$newimgurl = '';
		
		if ($OP_Watermarkoff == 1){
			include '../../ourphp_watermark.php';
		}
		if($OP_Imgcompress != 'ourphp')
		{
			include '../../ourphp_imagecompress.php';
		}
		foreach($filelist as $op)
		{
			$file_ext = ext($op);
			if (in_array($file_ext, $ext_arr['image'], true)) {
				
				//新文件名
				$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
				$file_s = $save_path.$new_file_name;
				@chmod($file_s, 0644);
				copy($op,$file_s);
				
				
				//OSS
				$file_url = $file_s;
				if($_POST['wateradd'] == 3)
				{
					$ossapi = $db -> select("OP_Key","ourphp_api","where OP_Keyid = 'oss'");
					$ossapi = explode("|",$ossapi[0]);
					if($ossapi[1] == 1)
					{
						include '../../../client/plus/oss/'.$ossapi[6].'/autoload.php';
					}else{
						alert("请先在API中配置好OSS参数。");
					}
					
				}
					
				$newimgurl .= str_replace("../../uploadfile",$ourphp['webpath']."function/uploadfile",$file_url)."|";
					
				
				/*
					水印需要
				*/
				if ($OP_Watermarkoff == 2){
					
				echo '';
				
				}elseif ($OP_Watermarkoff == 1){
					
					if($_POST['wateradd'] == 0){
						
						echo '';
						
							}elseif ($_POST['wateradd'] == 1){
								/*
									图片水印
								*/
								echo imageWaterMark($file_s,$OP_Watermarkposition,'../../../skin/'.$OP_Watermarkimg,'',$OP_Watermarksize,$OP_Watermarkcolor);
							}elseif ($_POST['wateradd'] == 2){
								/*
									文字水印
								*/
								echo imageWaterMark($file_s,$OP_Watermarkposition,'',$OP_Watermarkfont,$OP_Watermarksize,$OP_Watermarkcolor);
					}
				}
				/*
					图像压缩
				*/
				if($OP_Imgcompress != 'ourphp')
				{
					if(version_compare(PHP_VERSION,'5.6.0','>')){
						$img = new imagecompress;
						$img -> compress($file_s,$file_s,$OP_Imgcompress);
					}
				}

				
			}else{
				
				$array = array(
					"error" => 0,
					"msg" => '上传文件扩展名是不允许的扩展名！只允许' . implode(",", $ext_arr['image']) . '格式。',
				);
				echo json_encode($array);
				exit;
			}			
		}
		
		$array = array(
			"error" => 1,
			"msg" => '上传成功！',
			"imglist" => rtrim($newimgurl,"|"),
		);
		echo json_encode($array);
		exit;
		
	}
	
}
?>