<?php
/**
 * KindEditor PHP
 * 
 * 链琍HP绋嫔簭鏄紨绀虹▼搴忥紝寤鸿涓嶈鐩存帴鍦ㄥ疄闄呴」鐩腑浣跨敤銆?
 * 濡傛灉镇ㄧ‘瀹氱洿鎺ヤ娇鐢ㄦ湰绋嫔簭锛屼娇鐢ㄤ箣鍓嶈浠旗粏纭鐩稿叧瀹夊叏璁剧疆銆?
 * 
 */

//杞藉叆鍏ㄥ眬閰岖疆鍜屽嚱鏁板寘
require dirname(__FILE__)."/../include/global.php";

require VI_ROOT.'class/thumb.php';

/***********************/

//浼氲瘽鍩?
$session = $session ? $session : 'Manager';

//姘村嵃鍦板潃
$water = getgpc("watermark") ? VI_ROOT.$_G['setting']['attach']['MARK_FILE'] : '';

//姘村嵃浣岖疆
$position= getnum( "position", $_G['setting']['attach']['MARK_POSITION'] );

//铡熷浘鏄按鍗板浘灏哄镄勫嚑链嶆椂镓扑笂姘村嵃
$multiple= getnum("multiple", $_G['setting']['attach']['MARK_MULTIPLE'] );

/***********************/

//鏂囦欢淇濆瓨鐩綍璺缎
$save_path = VI_ROOT.$_G['project']['upload'];

//鏂囦欢淇濆瓨鐩綍URL
$save_url = VI_HOST.$_G['project']['upload'];

//瀹氢箟鍏佽涓娄紶镄勬枃浠舵墿灞曞悕
$ext_arr = array(
	'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
	'flash' => array('swf', 'flv'),
	'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
	'files' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
);

//链€澶ф枃浠跺ぇ灏?
$max_size = 1000000;

$save_path = realpath($save_path) . '/';

if ( $_G['setting']['global']['upload'] != "on" ){
	
	alert( 1, "绯荤粺宸茬鐢ㄦ湰锷熻兘!");
	
}elseif ( !$_SESSION[$session]['account'] && !$_SESSION[$session]['id'] ){
	
	alert( 1, "镇ㄥ綋鍓崭笉鍦ㄧ橱褰旷姸镐?");

}elseif (empty($_FILES) === false) {
	
	//铡熸枃浠跺悕
	$file_name = $_FILES['imgFile']['name'];
	
	//链嶅姟鍣ㄤ笂涓存椂鏂囦欢鍚?
	$tmp_name = $_FILES['imgFile']['tmp_name'];
	
	//鏂囦欢澶у皬
	$file_size = $_FILES['imgFile']['size'];
	
	//妫€镆ユ枃浠跺悕
	if (!$file_name) {
		alert( 1, "璇烽€夋嫨鏂囦欢銆?);
	}
	//妫€镆ョ洰褰?
	if (@is_dir($save_path) === false) {
		alert( 1, "涓娄紶鐩綍涓嶅瓨鍦ㄣ€?);
	}
	//妫€镆ョ洰褰曞啓鏉冮檺
	if (@is_writable($save_path) === false) {
		alert( 1, "涓娄紶鐩綍娌℃湁鍐欐潈闄愩€?);
	}
	//妫€镆ユ槸鍚﹀凡涓娄紶
	if (@is_uploaded_file($tmp_name) === false) {
		alert( 1, "涓存椂鏂囦欢鍙兘涓嶆槸涓娄紶鏂囦欢銆?);
	}
	//妫€镆ユ枃浠跺ぇ灏?
	if ($file_size > $max_size) {
		alert( 1, "涓娄紶鏂囦欢澶у皬瓒呰绷闄愬埗銆?);
	}
	//妫€镆ョ洰褰曞悕
	$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
	if (empty($ext_arr[$dir_name])) {
		alert( 1, "鐩綍鍚崭笉姝ｇ‘銆?);
	}
	
	//銮峰缑鏂囦欢镓╁睍鍚?
	$temp_arr = explode(".", $file_name);
	$file_ext = array_pop($temp_arr);
	$file_ext = trim($file_ext);
	$file_ext = strtolower($file_ext);
	
	//妫€镆ユ墿灞曞悕
	if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
		alert( 1, "涓娄紶鏂囦欢镓╁睍鍚嶆槸涓嶅厑璁哥殑镓╁睍鍚嶃€俓n鍙厑璁? . implode(",", $ext_arr[$dir_name]) . "镙煎纺銆?);
	}
	
	//鍒涘缓鏂囦欢澶?
	if ($dir_name !== '') {
		$save_path .= $dir_name . "/";
		$save_url .= $dir_name . "/";
		if (!file_exists($save_path)) {
			mkdir($save_path);
		}
	}
	
	$ymd = date("Y/md/");
	$save_path .= $ymd;
	$save_url .= $ymd;
	
	/*
	if (!file_exists($save_path)) {
		mkdir($save_path);
	}
	*/
	
	//鍒涘缓瀛愭枃浠跺す
	//$save_path = $save_path.date("Y/md/");
	create_dir( $save_path );
	
	//鏂版枃浠跺悕
	$new_file_name = date("H-i-s-").mt_rand().'.' . $file_ext;
	
	//绉诲姩鏂囦欢
	$file_path = $save_path . $new_file_name;
	if (move_uploaded_file($tmp_name, $file_path) === false) {
		alert( 1, "涓娄紶鏂囦欢澶辫触銆?);
	}
	@chmod($file_path, 0644);
	
	$file_url = $save_url . $new_file_name;	
	
	list($width, $height, $image_type, $attr) = getimagesize($file_path);
	
	/**************************/

	//缁欑粍锲绮镓扑笂姘村嵃
	if( $_G['setting']['attach']['MARK_OPEN'] == "true" && file_exists( $water ) ){
	
		list($water_w, $water_h) = getimagesize($water);
			
		//姘村嵃淇℃伅
		if( $water_w && $water_h && ( $width / $water_w > $multiple ) && ( $height / $water_h > $multiple ) ){
			
			//缂╃暐锲?           
			$t = new ThumbHandler();
	
			//婧愬浘鐗囧湴鍧€
			$t->setSrcImg( $file_path );
	
			//杈揿嚭鏂囦欢鍚?
			$t->setDstImg( $file_path );
	
			$t->setMaskImg( $water );
			
			$t->setMaskPosition( $position );
	
			//鐢熸垚锲剧墖
			$t->createImg( 100 );
			
		}
		
	}
	
	/**************************/

	//杩炴帴鏁版嵁搴?
	System :: connect();
	
	//鍐椤叆鏁版嵁搴?
	$sql="INSERT INTO `sys:attach`(aid,account,name,input,dateline,type,size,ip,width,height,remote) values('".$_SESSION[$session]["id"]."','".$_SESSION[$session]["account"]."','".str_replace(VI_HOST,VI_BASE,$file_url)."','KindEditor',".time().",'".$file_ext."',$file_size,'".GetIP()."',$width,$height,0)";
	
	System :: $db -> execute( $sql );

	//鍏抽棴鏁版嵁搴?
	System :: connect();
	
	/**************************/

	/*
	header('Content-type: text/html; charset=UTF-8');
	
	//璺ㄥ烟鏀寔锛埚瓙鍩熷悕蹇呴渶锛?
	echo '<script>document.domain="'. $_G['setting']['global']['domain'] .'";</script>';
	
	echo json_encode(array('error' => 0, 'url' => $file_url));
	exit;
	*/
	
	alert( 0, '', $file_url );
	
}

/*
	杈揿嚭娑堟伅
	$err	0 镞犻敊 1 链夐敊
	$msg	娑堟伅鍐呭
	$url	闄勪欢鍦板潃
*/
function alert( $err, $msg, $url ) {
	global $_G;
	
	header('Content-type: text/html; charset=UTF-8');
	
	//璺ㄥ烟鏀寔锛埚瓙鍩熷悕蹇呴渶锛?
	echo $_GET['crossdomain'] ? '<script>'. System :: cross_domain() .'</script>' : '';
	
	echo json_encode(array('error' => $err, 'message' => $msg, 'url' => $url));
	exit;
}
?>