<?php
header("Content-type:text/html;charset=utf-8");  
session_start();
$session_admin = isset($_SESSION['admin']) ? $_SESSION['admin'] : ''; 
if ($session_admin == '') {
	die("Can not access");
}
require_once("../common.php");
checkadminrights($session_admin, 'webapps');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" href="../css/base.css" />
<style type="text/css">
.td1 { padding:5px 0 0 0;width:70px;height:20px;vertical-align:top; }
.td2 { width:880px;vertical-align:top; }
</style>
<script type="text/javascript">
String.prototype.trim=function() {
     return this.replace(/(^\s*)(\s*$)/g, '');
}
function chkform() {
	var form = document.getElementById('form');
	if(form.appimage.value.trim() == "") { 
      alert('应用图标不能为空！'); 
      return false; 
    } 
    return   true; 
}//chkform
</script>
</head>
<body>

<?php
function get_extension($file) {
	return pathinfo($file, PATHINFO_EXTENSION);
}

function upload_image($imgfile, $saveto) {
	$name = $imgfile["name"];
	$tmp_name = $imgfile["tmp_name"];
	$type = $imgfile["type"];
	$size = $imgfile["size"];
	$filename = time() . '.' . get_extension($name);
	$uploadfile = $saveto . $filename;
       
    $maxsize = 500*1024;  //最大允许上许文件大小
    if($name == "") //文件名为空
	{
		return '';
	}
    if($type != "image/x-icon")//文件类型不在指定范围
    {
		return '';
	}
    if($size > $maxsize) //超过规定大小
    {
		return '';
	}
     
    if(move_uploaded_file($tmp_name, $uploadfile))
		return $filename;
    else if (copy($tmp_name, $uploadfile))
        return $filename;
     else
        return '';    
}//upload_image

$step = isset($_GET['step']) ? $_GET['step'] : '1'; 

function step1() {
	$appid = isset($_GET['appid']) ? $_GET['appid'] : '0';
	$appname = isset($_GET['appname']) ? urldecode($_GET['appname']) : '';

	//subpageurlparams
	$subpageurlparams = 'appid=' . $appid . '&appname=' . urlencode($appname);

echo<<<EOT
	<div class="rightbar">
		<div class="form_title">修改应用图标</div>
		<div class="form_panel">
			<form action="webapps_changeimage.php?step=2" method="post" enctype="multipart/form-data" id="form" onSubmit="return chkform();">
			<input type="hidden" name="appid" value="{$appid}"/>
			<table border="0">
			<tr>
				<td class="td1">应用图标:</td>
				<td class="td2" style="height:40px;"><input style="width:266px;" type="file" name="appimage" value="">&nbsp;&nbsp;&nbsp;<span style="color:#2fb900;">应用图标只支持ICO格式，推荐尺寸[48*48]+[32*32]+[16*16]</span></td>
			</tr>			
			<tr>
				<td class="td1"></td>
				<td class="td2"><input class="cssbtn" type="submit" value="上传">&nbsp;<input class="cssbtn" type="button" value="取消" onclick="window.location='webapps.php'"></td>
			</tr>
			</table>
			</form>
		</div>
	</div>
EOT;
}//step1

function step2() {
	require_once("../common.php");

	$imgfile=$_FILES['appimage'];

	$appid = isset($_POST['appid']) ? $_POST['appid'] : ''; 
	$appimage = upload_image($imgfile, '..\\..\\webapps\\'); 

	$methodname = 'Admin.ChangeWebAppImage';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<ip>' . getip() . '</ip>' . 
		'<appid>' . $appid . '</appid>' . 
		'<appimage>' . $appimage . '</appimage>' . 
		'</request> ';
	$responseinfo = '';
	$responsexml = null;
	$errorcode = ERRORCODE_UNKNOWN;

	$cmd = new COM("OpenCom.Command");
	$methodname = iconv('UTF-8', 'GBK//IGNORE', $methodname);
	$requestinfo = iconv('UTF-8', 'GBK//IGNORE', $requestinfo);
	if ($cmd->Execute($methodname, $requestinfo, $responseinfo, 5000)) {
		$responseinfo = iconv('GBK', 'UTF-8//IGNORE', $responseinfo);
		$responsexml = simplexml_load_string($responseinfo);
		$errorcode = $responsexml->errorcode;
	}
	$cmd = null;

	switch($errorcode) {
		case ERRORCODE_OK: 
echo<<<EOT
	<div class="msgbox">
		<div class="msgbox_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/ok.gif" />&nbsp;应用图标修改成功！
		</div>
		<div class="msgbox_btns">
			<input class="cssbtn" type="button" value="返回" onclick="window.location='webapps.php'" />
		</div>
	</div>
EOT;
			break;
		default:
echo<<<EOT
	<div class="msgbox">
		<div class="msgbox_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/warning.gif" />&nbsp;应用图标修改失败！
		</div>
		<div class="msgbox_btns">
			<input class="cssbtn" type="button" value="返回" onclick="window.location='webapps.php'" />
		</div>
	</div>
EOT;
	}//switch
}//step2

if ($step == 1) {
	step1();
}
else if ($step == 2) {
	step2();
}
?>

</body>
</html>
