<?php
if (!defined('MOD')) die("access die");
require_once(R_P . '/config/admin_options.php');
require_once(R_P . '/include/inc.editor.php');
$channelid = intval($_GET['channelid']);
$type = intval($_GET['type']);
$fck = intval($_GET['fck']);
$bts = intval($_GET['bts']);
$javascript = $_GET['javascript'];
$file_id = $_GET['id'] ? $_GET['id'] : 'pic';
$dir = urldecode($_GET['path']);
$file_type = $_GET['file_type'] ? $_GET['file_type'] : 'image';
$keepName = intval($_GET['keepName']);
$thumb = intval($_GET['thumb']);
switch (strtoupper($file_type)) {
	case "FILE":
		$sAllowExt = $ext['FILE'];
		break;
	case "MEDIA":
		$sAllowExt = $ext['MEDIA'];
		break;
	case "FLASH":
		$sAllowExt = $ext['FLASH'];
		break;
	case "ALL":
		$sAllowExt =  implode('|', $ext);
		break;
	default:
		$sAllowExt = $ext['IMAGE'];
		break;
} 

if ($channelid > 0) {
	@extract($channel[$channelid]);
	if ($allow_upload == 0) exitupload(6);
	$uptypes = explode("|", $upload_type);
	$max_file_size = $upload_size;
	
	$pic_width = $channel[$channelid]['pic_width'];
	$pic_height = $channel[$channelid]['pic_height'];
	$pic_cut = $channel[$channelid]['pic_cut'];	
} else {
	$uptypes = explode("|", $sAllowExt);
	$max_file_size = 1024 ;
	$pic_width = 0;
	$pic_height = 0;
	$pic_cut = 0;
} 
$max_file_size = min($systemMaxUpload, $max_file_size);
$allowUploadExt = $sAllowExt ? array_intersect($uptypes, explode("|", $sAllowExt)):$uptypes;
$forbidden_upload = explode("|", $_SET['forbidden_upload']);
$allowUploadExtFlash = array();
foreach($allowUploadExt as $val) {
	if(in_array(trim($val), $forbidden_upload)) continue;
	$allowUploadExtFlash[] = "*." . trim($val);
} 
$allowUploadFileType = count($allowUploadExtFlash);

$action_url = W_P . "admin/system.php?sys=upload&channelid={$channelid}&file_type={$file_type}&bts={$bts}";

if ($_FILES["Filedata"] && $_SERVER['REQUEST_METHOD'] == 'POST') {
	InitUpload();
	DoCreateNewDir();
	if($_POST['targetDir']){
		$sSaveDir = preg_replace("/\"|\'|\\|\<|\>|\*|\?|\:|\|\.\.\/|/", "", urldecode(trim($_POST['targetDir']))) . '/';
		qbase::createdir(R_P .'/'. $sSaveDir);
	}

	require_once(R_P . '/include/class.qupload.php');	

	$upload = new qupload($sSaveDir, $allowUploadExt, $max_file_size);
	if($keepName > 0) $upload->setKeepName(true);
	$upload_file = $upload->move();
	if($upload_file[0] > 0) exitupload($upload_file[0]);

	$real_file_name = R_P . '/' . $upload_file[1];
	$s_picurl = $web_url_name = $upload_file[1];

	if (isPic(qbase::fileext($upload_file[1])) && $channelid > 0) {
		require(R_P . "/include/class.resizeimage.php");
		
		if($thumb){
			if(intval($_GET['width']) > 0 && intval($_GET['height']) > 0){
				$pic_width = intval($_GET['width']);
				$pic_height = intval($_GET['height']);
			}
			$resize = new resizeimage($real_file_name);
			$rs = $resize->init();
			if($rs != 0) exitupload(7);
			$resize->run($pic_width, $pic_height, $pic_cut, $real_file_name);
			$resize->close();
			clearstatcache();
		}
		
		$info = @getimagesize($real_file_name);
		if($channel[$channelid]['water'] && $info[0] > 300 && $info[1] > 300){
			require(R_P . '/include/class.water.mask.php');
			$logoPath = R_P . "/images/water.png";
			$t = new WaterMask();
			$t -> setSrcImg($real_file_name);
			$t -> setDstImg($real_file_name);
			if($channel[$channelid]['water'] == 1){
				if (is_file(R_P . "/include/table/simsun.ttc")) $t->setMaskFont(R_P . "/include/table/simsun.ttc");
				$t->setMaskFontSize($channel[$channelid]['water_size']);
				$t->setMaskFontColor($channel[$channelid]['water_color'] ? $channel[$channelid]['water_color'] : '#000000');
				$t->setMaskWord(qbase::iconvs($channel[$channelid]['water_word'], "utf-8"));
			}else{
				$t -> setMaskImg($logoPath);
			}
			$t -> setMaskPosition($channel[$channelid]['water_position']);
			$t -> setMaskImgPct(100);
			$t -> setMaskTxtPct(0);
			$t -> setImgCreateQuality(75);
			$t -> createImg(100);
		}
	} 
	$arr = array('status'=>200, "msg"=>array(W_P . $web_url_name, $s_picurl));
	die(qbase::jsonencode($arr));

} else {
	include qbase::template($path . 'upload');
} 
?>