<?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 = 1;
$fck = 0;
$bts = 1;
$file_type = 'image';
$keepName = 0;
$thumb = 0;
$javascript = $_GET['javascript'];
$sAllowExt = $ext['IMAGE'];

$close_thumb = 1;

@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'];	

$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/module.php?mod=pic&action=upload&channelid={$channelid}&file_type={$file_type}&bts={$bts}";

if ($_FILES["Filedata"] && $_SERVER['REQUEST_METHOD'] == 'POST') {
	InitUpload();
	DoCreateNewDir();
	require_once(R_P . '/include/class.qupload.php');	
	$upload = new qupload($sSaveDir, $allowUploadExt, $max_file_size);
	$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];

	require(R_P . "/include/class.resizeimage.php");
		
	$pic_mod_set = array();
	$cache_file = C_P . "/config/pic_mod_set.php";
	if(!is_file($cache_file)){
		$data = getPicModCache(0);
		$s = var_export($data, true);
		qbase::writetofile($cache_file, "<?php\n \$pic_mod_set = {$s}; \n?>");
		clearstatcache();
	}
	include_once($cache_file);
	$resize = new resizeimage($real_file_name);
	$rs = $resize->init();
	if($rs != 0) exitupload(1);

	foreach($pic_mod_set[$channelid] as $val){
		$dstimg = getSmallImageFile($real_file_name, $val['name']);
		$resize->run($val['w'], $val['h'], $val['type'], $dstimg);
		$s_picurl = getSmallImageFile($web_url_name, $val['name']);
	}
	clearstatcache();
	$resize->close();
		
	$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');
} 
?>