<?php
/**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
class UploadAction extends BaseAction{
	function index(){
		$config = array("maxSize" => 2048,"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"));
		$callback=getgpc('callback');
		$an=getgpc('an');
		$upurl=getgpc('upurl');
		$name=getgpc('name');
		$typeId=getgpc('typeId');
		$cloud=getgpc('cloud');
		if(!$cloud || $cloud!='1'){$cloud=0;}
		if($upurl){
			$state='SUCCESS';
			$size = @getimagesize($upurl);
			if(!$size){$state='图片链接无法访问';}
			$ret=array(
					'url'=>$upurl,
					'title'=>'',
					'originalName'=>'',
					'state'=>$state
			);
			$info['url']=$upurl;
		}else{
			$config['savePath'] = ROOT_PATH.'data/attachment';
			$yearPath=$config['savePath'].'/'.date('Y');
			$c=true;
			if(!file_exists($yearPath)){if(!@mkdir($yearPath)){ $info=array('state'=>'创建图片目录失败');$c=false;}}
			$config['savePath']=$yearPath;
			$monthPath=$config['savePath'].'/'.date('md');
			if(!file_exists($monthPath)){if(!@mkdir($monthPath)){ $info=array('state'=>'创建图片目录失败');$c=false;}}
			$config['savePath']=$monthPath;

			if($c){
				$title = htmlspecialchars( $_POST[ 'title' ] , ENT_QUOTES );
				$up = new Uploader("upfile" , $config );
				$info = $up->getFileInfo();
				if(is_null($info['url'])){
						 $info['url']='';
				}else{
					$info['url']=str_replace('//','/',$info['url']);
					$file=$info['url'];
					if($cloud=='1'){
						$url='http://app.ueidc.com/upload';
						 if(class_exists('CURLFile')){
							 $http = new http($url);
							 $_GET['an']='false';
							 $param=array( 'an'=>getgpc('an'),'name'=>getgpc('name'),'upfile'=>new CURLFile($info['url']) );
							 $http->setPost($param);
							 $http->createCurl();
							 $ret=$http->responseText();
						 }else{
								$ch=curl_init($url);
								curl_setopt($ch, CURLOPT_HEADER, 0);
								curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
								curl_setopt($ch, CURLOPT_POST, 1);
								curl_setopt($ch, CURLOPT_POSTFIELDS, 
								array('an'=>getgpc('an'),'name'=>getgpc('name'),'upfile'=>'@'.$info['url'])
							);
							$ret=curl_exec($ch);
							curl_close($ch);
						 }
						$serverRetArr=json_decode($ret,true);
						if($serverRetArr && $serverRetArr['state']=='SUCCESS'){
							unlink($info['url']);
							$info=$serverRetArr;
						}
						if($info['url']){
							 if(substr($info['url'],0,7)!="http://" && substr($info['url'],0,8)!="https://" && substr($info['url'],0,1)!="/"){
								$info['url']=$GLOBALS['baseUrl'].'data/attachment/'.date('Y').'/'.date('md').'/'.$info['name'];
							}
						 }
					}else{
						$info['url']=$GLOBALS['baseUrl'].'data/attachment/'.date('Y').'/'.date('md').'/'.$info['name'];
					}
				}
				 if(is_null($info['originalName'])){$info['originalName']='';}
				 $info=array(
					'url'=>$info['url'],
					'title'=>$title,
					'originalName'=>$info['originalName'],
					'state'=>$info['state']
				  );
			}
		}
		$id=0;
		if($info['state']=='SUCCESS'){
			if(!$typeId || !is_numeric($typeId)){$typeId=0;}
			$sql = "insert into qk_attachment set name='".addslashes($name)."',url='".$info['url']."',iStatus=1,siteId='".SITE_ID."',typeId='$typeId',posttime=UNIX_TIMESTAMP()";
			$GLOBALS['db']->query($sql);
			$id=$GLOBALS['db']->insert_id();
		}
		$str = '{"url":"'.$info['url'].'","title":"'.$info['title'].'","originalName":"'.$info['originalName'].'","state":"'.$info['state'].'","id":'.$id.'}';
		if(preg_match("/^([A-Za-z\.]+)$/",$callback)){
			echo '<script>'.$callback.'('.$str.');</script>';
		}else{
			echo $str;
		}
		//echo '<script>'.getgpc('callback').'({"url":"http://cache.ordns.com/images/2014/0817/upload_padding.png","state":"SUCCESS","id":'.$id.'});</script>';
	}

	function data(){
		$inajax=getgpc('inajax','P');
		if($inajax=='1'){
			$ret=array('success'=>false,'message'=>'非法访问');
			$id=getgpc('id','P');
			if($id && is_numeric($id)){
				$sql = "select id,url from qk_attachment where id='$id'";
				$info=$GLOBALS['db']->getRow($sql);
				if($info){
					if(substr($info['url'],0,7)!="http://" && substr($info['url'],0,8)!="https://" && substr($info['url'],0,1)!="/"){
						$info['url']=$GLOBALS['baseUrl'].$info['url'];
					}
					$ret=array('success'=>true,'message'=>'Success','data'=>$info);
				}
			}
			echo json_encode($ret);
		}
	}
}
?>