<?
include_once('../common.php');
include_once('background.php');
include_once(S_ROOT.'./source/function_cp.php');
if ( $_SGLOBAL['supe_uid'] )
{
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_bar')." WHERE desktop_id=".intval($_POST['desktop_id'])." and uid=".$_SGLOBAL['supe_uid']);
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$check[] = $value;
	}
	if ( $check )
	{
		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, "800", "480");
				for ( $i=0; $i < 60; $i++ )
				{
				   $m = $i%10;
				   if (  $m == 0 )
				   {
					   $j = 0;
					   if ( $i != 0 )
					   {
							$k++;
					   }
				   }
				   $x = $j*80;
				   $y = $k*80;
				   $img_file = '../icon/tmp/'.$_SC['site_key'].'.png';
				   $tmp = $i+1;
				   $resizeimage = new resizeimagetwo($img_file, $x, $y, $tmp);
				   $file = '../icon/tmp/'.$_SC['site_key'].'_'.($i+1).'.png';
				   $resizeimage = new resizeimagethree($file, $corner, "78", "78", "2", $tmp);
				   $j++;
				}
				if ( $resizeimage )
				{
					$desktop = array();
					$query = $_SGLOBAL['db']->query("SELECT b.content as content, b.type as type, a.bar_name as bar_name, b.custom as custom FROM ".tname('site_bar')." as a,".tname('site_desktop')." as b  where a.desktop_id=b.desktop_id and a.desktop_id=".intval($_POST['desktop_id'])." order by a.bar_id desc");
					while ($value = $_SGLOBAL['db']->fetch_array($query)) {
						$desktop[] = $value;
					}
					if ( $desktop )
					{
						if ( $desktop[0]['custom'] == 0 )
						{
							$setarr = array(
								'uid' => $_SGLOBAL['supe_uid'],
								'content' => $desktop[0]['content'],
								'active' => 0,
								'type' => $desktop[0]['type'],
								'createtime' => $_SGLOBAL['timestamp']
							);
							$newaid = inserttable('site_desktop', $setarr, 1);
							$setbararr = array(
								'desktop_id' => $newaid,
								'bar_name' => $desktop[0]['bar_name'].'备份',
								'uid' => $_SGLOBAL['supe_uid'],			
								'active' => 0,
								'createtime' => $_SGLOBAL['timestamp']
							);
							inserttable('site_bar', $setbararr, 1);
							unset($setarr);
							unset($setbararr);
							$str = explode("|", $desktop[0]['content']);
							$img = 1;
							$content = '';
							foreach( $str as $key )
							{
								if( is_numeric( $key ) )
								{
									if ( $key > 1 )
									{
										$tmp_path = mkdir_custom_dir(true);
										$oldpath = '../icon/tmp/'.$_SC['site_key'].'_'.$img.'.png';
										$newpath = '../icon/custom/upload/'.$tmp_path.$_SGLOBAL['supe_uid'].'_'.intval($_POST['desktop_id']).'_'.$img.'.png';
										@rename($oldpath,$newpath);
										$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site')." WHERE id=".$key);
										while ($value = $_SGLOBAL['db']->fetch_array($query)) {
											$list[] = $value;
										}
										if ( $list[0]['type'] == 'links' )
										{
											$setarr = array(
												'title' => $list[0]['title'],
												'url' => $list[0]['url'],
												'icon' => $newpath,
												'color' => '',
												'titlecolor' => addslashes($_POST['add_custom_icon_text_color']),
												'type' => 'links',
												'visible' => 1,
												'uid' => $_SGLOBAL['supe_uid'],
												'createtime' => $_SGLOBAL['timestamp']
											);
											$aid = inserttable('site', $setarr, 1);
											$content .= $aid.'|';
										}
										else
										{
											$setarr = array(
												'title' => '',
												'url' => '',
												'icon' => $newpath,
												'color' => '',
												'titlecolor' => addslashes($_POST['add_custom_icon_text_color']),
												'type' => 'links',
												'visible' => 1,
												'uid' => $_SGLOBAL['supe_uid'],
												'createtime' => $_SGLOBAL['timestamp']
											);
											$aid = inserttable('site', $setarr, 1);
											$content .= $aid.'|';
										}
										unset($list);
									}
									else
									{
										$tmp_path = mkdir_custom_dir(true);
										$oldpath = '../icon/tmp/'.$_SC['site_key'].'_'.$img.'.png';
										$newpath = '../icon/custom/upload/'.$tmp_path.$_SGLOBAL['supe_uid'].'_'.intval($_POST['desktop_id']).'_'.$img.'.png';
										@rename($oldpath,$newpath);
										$setarr = array(
											'title' => '',
											'url' => '',
											'icon' => $newpath,
											'color' => '',
											'titlecolor' => addslashes($_POST['add_custom_icon_text_color']),
											'type' => 'links',
											'visible' => 1,
											'uid' => $_SGLOBAL['supe_uid'],
											'createtime' => $_SGLOBAL['timestamp']
										);
										$aid = inserttable('site', $setarr, 1);
										$content .= $aid.'|';

									}
								}
								else
								{
									$content .= $key.'|';
								}
								unset($setarr);
								unset($setarripdate);
								$img++;
							}
							$content = substr($content,0,-1);
							$arr = array(
								'content' => $content,
								'custom' => 1
							);
							updatetable('site_desktop', $arr, array('desktop_id'=>intval($_POST['desktop_id'])));
							$desktop_id = intval($_POST['desktop_id']);
						}
						else
						{
							$str = explode("|", $desktop[0]['content']);
							$img = 1;
							$content = '';
							foreach( $str as $key )
							{
								if( is_numeric( $key ) )
								{
									if ( $key > 1 )
									{
										$tmp_path = mkdir_custom_dir(true);
										$oldpath = '../icon/tmp/'.$_SC['site_key'].'_'.$img.'.png';
										$newpath = '../icon/custom/upload/'.$tmp_path.$_SGLOBAL['supe_uid'].'_'.intval($_POST['desktop_id']).'_'.$img.'.png';
										@unlink($newpath);
										@rename($oldpath,$newpath);
										$setarripdate = array(
											'icon' => $newpath
										);
										updatetable('site', $setarripdate, array('id'=>$key));
										$content .= $key.'|';
									}
									else
									{
										$tmp_path = mkdir_custom_dir(true);
										$oldpath = '../icon/tmp/'.$_SC['site_key'].'_'.$img.'.png';
										$newpath = '../icon/custom/upload/'.$tmp_path.$_SGLOBAL['supe_uid'].'_'.intval($_POST['desktop_id']).'_'.$img.'.png';
										@rename($oldpath,$newpath);
										$setarr = array(
											'title' => '',
											'url' => '',
											'icon' => $newpath,
											'color' => '',
											'titlecolor' => addslashes($_POST['add_custom_icon_text_color']),
											'type' => 'links',
											'visible' => 1,
											'uid' => $_SGLOBAL['supe_uid'],
											'createtime' => $_SGLOBAL['timestamp']
										);
										$aid = inserttable('site', $setarr, 1);
										$content .= $aid.'|';

									}
								}
								else
								{
									$content .= $key.'|';
								}
								unset($setarr);
								unset($setarripdate);
								$img++;
							}
							$content = substr($content,0,-1);
							$arr = array(
								'content' => $content
							);
							updatetable('site_desktop', $arr, array('desktop_id'=>intval($_POST['desktop_id'])));
							$desktop_id = intval($_POST['desktop_id']);
						}
					}
					else
					{
						$msg = '<error>不能修改别人的桌面</error>';
					}
				}
			}
		}
	}
	else
	{
		$msg = '<error>不能修改别人的桌面</error>';
	}
}
else
{
	$msg = '<error>请登陆后操作</error>';
}
global $_SGLOBAL;
$_SGLOBAL['inajax'] = 2;
$cdtat_start = '****----****';
$cdtat_end = '####----####';
$desktop_id = intval($_POST['desktop_id']);
include_once template("site_test");
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'] > 804800 )
	{
		return $msg = '<error>发生延时。上传的图片不能超过200kb</error>';
	}

	//判断后缀
	$fileext = fileext($FILE['name']);
	if(!in_array($fileext, $allowpictype)) {
		return $msg = '<error>只允许上传jpg、gif、png标准格式的图片</error>';
	}
	//本地上传
	$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') && !$ss = @getimagesize($new_name)) {
		@unlink($new_name);
		return $msg = '<error>只允许上传jpg、gif、png标准格式的图片</error>';
	}
	else
	{
		if ( $ss[0] < 800 && $ss[1] < 480 )
		{
			return $msg = '<error>图片的高宽不合适, 推荐800*480（宽*高）</error>';
		}
	}
}
function mkdir_custom_dir($mkdir)
{
	$name1 = gmdate('Ym');
	$name2 = gmdate('j');
	if($mkdir) {
		$newfilename = '../icon/custom/upload/'.$name1;
		if(!is_dir($newfilename)) {
			@mkdir($newfilename);
		}
		$newfilename .= '/'.$name2;
		if(!is_dir($newfilename)) {
			@mkdir($newfilename);
		}
	}
	return $name1.'/'.$name2.'/';
}
?>