<?php
define("IN_COLNET",true);
define("IN_QKWEB",true);
$rootPath = str_replace("plugins/editor/ueditor/php/action_upload.php","",str_replace("\\","/",__FILE__));
require($rootPath."includes/init.php");
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
    case 'uploadimage':
        $config = array(
            "pathFormat" => $CONFIG['imagePathFormat'],
            "maxSize" => $CONFIG['imageMaxSize'],
            "allowFiles" => $CONFIG['imageAllowFiles']
        );
        $fieldName = $CONFIG['imageFieldName'];
		$title = htmlspecialchars( isset($_POST[ 'pictitle' ])?$_POST[ 'pictitle' ]:'' , ENT_QUOTES );
		$save_path =  ROOT_PATH."/data/attachment/images/";
		$year = date("Y",time());
		$save_path .=$year."/";
		if(!file_exists($save_path)){if(!@mkdir($save_path)){return '{"url":"","title":"' . $title . '","original":"","state":"创建图片目录失败"}';}}
		$day = date("md",time());
		$save_path .=$day."/";
		if(!file_exists($save_path)){if(!@mkdir($save_path)){return '{"url":"","title":"' . $title . '","original":"","state":"创建图片目录失败"}';}}
		$url_path = "data/attachment/images/".$year."/".$day."/";
		$config['savePath'] = ROOT_PATH.$url_path;
        break;
    case 'uploadscrawl':
        $config = array(
            "pathFormat" => $CONFIG['scrawlPathFormat'],
            "maxSize" => $CONFIG['scrawlMaxSize'],
            "allowFiles" => $CONFIG['scrawlAllowFiles'],
            "oriName" => "scrawl.png"
        );
        $fieldName = $CONFIG['scrawlFieldName'];
        $base64 = "base64";
        break;
    case 'uploadvideo':
        $config = array(
            "pathFormat" => $CONFIG['videoPathFormat'],
            "maxSize" => $CONFIG['videoMaxSize'],
            "allowFiles" => $CONFIG['videoAllowFiles']
        );
        $fieldName = $CONFIG['videoFieldName'];
        break;
    case 'uploadfile':
    default:
        $config = array(
            "pathFormat" => $CONFIG['filePathFormat'],
            "maxSize" => $CONFIG['fileMaxSize'],
            "allowFiles" => $CONFIG['fileAllowFiles']
        );
        $fieldName = $CONFIG['fileFieldName'];
        break;
}

/* 生成上传实例对象并完成上传 */
$up = new Uploader($fieldName, $config, $base64);

/**
 * 得到上传文件所对应的各个参数,数组结构
 * array(
 *     "state" => "",          //上传状态，上传成功时必须返回"SUCCESS"
 *     "url" => "",            //返回的地址
 *     "title" => "",          //新文件名
 *     "original" => "",       //原始文件名
 *     "type" => ""            //文件类型
 *     "size" => "",           //文件大小
 * )
 */

    $info = $up->getFileInfo();
	 $info["url"] = "/".APP_DIR.$url_path.$info["name"];
    return json_encode($info);
	//"{'url':'" . $info["url"] . "','title':'" . $title . "','original':'" . $info["originalName"] . "','state':'" . $info["state"] . "'}";
//return json_encode($up->getFileInfo());

/* 上传配置 */
