<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: site_icon_add_window_custom_upload_image.php 10953 2009-01-12 02:55:37Z liguode $
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}
include_once('img.php');
include_once(S_ROOT.'./source/function_cp.php');
if ( isset( $_FILES['add_icon_image_file']['name'] ) && $_FILES['add_icon_image_file']['name'] != '' )
{
	$FILE = $_FILES['add_icon_image_file'];
	$msg = custom_upload($FILE);
	if ( empty($msg) )
	{
		$fileext = fileext($FILE['name']);
		$img_file = '../icon/tmp/'.$_SC['site_key'].'.'.$fileext;
		$corner = '../images/template.png';
		$resizeimage = new resizeimage($img_file, $corner, "78", "78", "1", "2", "0");
	}
}
function custom_upload($FILE)
{
	global $_SGLOBAL, $_SC;
	//允许上传类型
	$allowpictype = array('jpg','gif','png');
	//检查
	$FILE['size'] = intval($FILE['size']);
	if(empty($FILE['size']) || empty($FILE['tmp_name']) || !empty($FILE['error'])) {
		return $msg = '<error>无法获取上传文件大小</error>';
	}
	if ( $FILE['size'] > 204800 )
	{
		return $msg = '<error>发生延时。上传的图片不能超过200kb</error>';
	}

	//判断后缀
	$fileext = fileext($FILE['name']);
	if(!in_array($fileext, $allowpictype)) {
		return $msg = '<error>只允许上传jpg、gif、png标准格式的图片</error>';
	}
	//$filepath = get_custom_file_path($fileext, true);
	//本地上传
	//$new_name = '../icon/custom/upload/'.$filepath;
	$new_name = '../icon/tmp/'.$_SC['site_key'].'.'.$fileext;
	$tmp_name = $FILE['tmp_name'];
	if(@copy($tmp_name, $new_name)) {
		@unlink($tmp_name);
	} elseif((function_exists('move_uploaded_file') && @move_uploaded_file($tmp_name, $new_name))) {
	} elseif(@rename($tmp_name, $new_name)) {
	} else {
		return $msg = '<error>无法转移临时图片到服务器指定目录</error>';
	}
	
	//检查是否图片
	if(function_exists('getimagesize') && !@getimagesize($new_name)) {
		@unlink($new_name);
		return $msg = '<error>只允许上传jpg、gif、png标准格式的图片</error>';
	}
}

//获取上传路径
function get_custom_file_path($fileext, $mkdir=false) {
	global $_SGLOBAL;

	$filepath = "{$_SGLOBAL['supe_uid']}_{$_SGLOBAL['timestamp']}".random(4).".$fileext";
	$name1 = gmdate('Ym');
	$name2 = gmdate('j');

	if($mkdir) {
		$newfilename = '../icon/custom/upload/'.$name1;
		if(!is_dir($newfilename)) {
			if(!@mkdir($newfilename)) {
				return $filepath;
			}
		}
		$newfilename .= '/'.$name2;
		if(!is_dir($newfilename)) {
			if(!@mkdir($newfilename)) {
				return $name1.'/'.$filepath;
			}
		}
	}
	return $name1.'/'.$name2.'/'.$filepath;
}
?>