<?php 
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
include '../../include/config.inc.php';
include '../../include/config.php';
include '../admin_loginstate.php';

$folder;
if(isset($_POST['module'])){
	switch($_POST['module']){
		case 'vod':
			$folder = S_Vimgfolder;
		break;
		case 'art':
			$folder = S_Aimgfolder;
		break;
		case 'zt':
			$folder = S_Timgfolder;
		break;
	}
} else {
		$return = array(
			'error'=>1,
			'message'=>'没有接收到module指令',
		);
		$return['message'] = auto_charset($return['message'],'gb2312','utf-8');
		exit(json_encode($return));
}

if (!empty($_FILES)) {
	$return = array('error'=>0);
	$mime = array('image/png','image/jpeg','image/bmp','image/gif','image/jpg');
	$file = $_FILES['imgFile'];
	if($file['error']!=0){
		$return['error'] = 1;
         switch($file['error']) {
            case 1:
                $return['message'] = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
                break;
            case 2:
                $return['message'] = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
                break;
            case 3:
                $return['message'] = '文件只有部分被上传';
                break;
            case 4:
                $return['message'] = '没有文件被上传';
                break;
            case 6:
                $return['message'] = '找不到临时文件夹';
                break;
            case 7:
                $return['message'] = '文件写入失败';
                break;
            default:
                $return['message'] = '未知上传错误！';
        }
        $return['message'] = auto_charset($return['message'],'gb2312','utf-8');
		exit(json_encode($return));
	}
	$imageInfo = getimagesize($file['tmp_name']);
	if(!in_array($file['type'], $mime) || $imageInfo==false || !in_array($imageInfo['mime'], $mime)){
		$return = array(
			'error'=>1,
			'message'=>'只接受图片上传',
		);
		$return['message'] = auto_charset($return['message'],'gb2312','utf-8');
		exit(json_encode($return));
	}
	$sha = md5_file($file['tmp_name']);
	$filename = _SYC_ROOT_.'images/'.$folder.'/'.$sha.'.jpg';
	if (is_file($filename) || move_uploaded_file($file['tmp_name'], $filename)) {
		$path = trim(S_SitePath,'/');
		if(!empty($path)){
			$path = '/'.$path;
		}
		if($_POST['type']=='logo'){
			$return['url'] = $sha.'.jpg';
		}else{
			$return['url'] = $path.'/images/'.$folder.'/'.$sha.'.jpg';
		}
		exit(json_encode($return));
	} else {
		$return = array(
			'error'=>1,
			'message'=>'上传到'.$folder.'目录失败'
		);
		$return['message'] = auto_charset($return['message'],'gb2312','utf-8');
		exit(json_encode($return));
	}
}

/**
 * 自动转换字符集 支持数组转换
 * @param $fContents
 * @param $from
 * @param $to
 */
function auto_charset(&$content,$from='utf-8',$to='gb2312'){
    $from   =  strtoupper($from)=='UTF8'? 'utf-8':$from;
    $to       =  strtoupper($to)=='UTF8'? 'utf-8':$to;
    if( strtoupper($from) === strtoupper($to) || empty($content) || (is_scalar($content) && !is_string($content)) ){
        return $content;
    }
    if(is_string($content) ) {
        if(function_exists('mb_convert_encoding')){
            return mb_convert_encoding ($content, $to, $from);
        }elseif(function_exists('iconv')){
            return iconv($from,$to,$content);
        }else{
            return $content;
        }
    }
    elseif(is_array($content)){
        foreach ( $content as $key => $val ) {
            $_key =     auto_charset($key,$from,$to);
            $content[$_key] = auto_charset($val,$from,$to);
            if($key != $_key )
                unset($content[$key]);
        }
        return $content;
    }
    else{
        return $content;
    }
}
?>