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

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}
@include_once(S_ROOT.'./data/site_config.php');
$list = $empty = array();
$cdtat_start = '****----****';
$cdtat_end = '####----####';
@header('Content-Type: text/html; charset=utf-8');
if ( !empty($_SGLOBAL['supe_uid']) )
{
	if ( !empty($_POST['add_desk_id']) && intval($_POST['add_desk_id']) > 0 )
	{
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_desktop')." where uid=".$_SGLOBAL['supe_uid']." and desktop_id=".intval($_POST['add_desk_id']));
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$list[] = $value;
		}
		if ( $list )
		{
			$str = explode("|", $list[0]['content']);
			$action = 0;
			foreach( $str as $key )
			{
				if ( is_numeric($key) )
				{
					$empty[] = $key;
				}
			}
			if ( !in_array(0,$empty) )
			{
				$action = 1;
			}
			$_POST['add_icon_id'] = str_replace('c','',$_POST['add_icon_id']);
			if ( in_array(intval($_POST['add_icon_id']),$empty) )
			{
				$action = 2;
			}
			if ( $action == 1 )
			{
				$msg = 1;
			}
			elseif ( $action == 2 )
			{
				$msg = 2;
			}
			else
			{
				$html = '';
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site')." where id=".intval($_POST['add_icon_id']));
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$site[] = $value;
				}
				$setarr = array(
					'title' => $site[0]['title'],
					'url' => $site[0]['url'],
					'icon' => $site[0]['icon'],
					'color' => $site[0]['color'],
					'titlecolor' => $site[0]['titlecolor'],
					'hot' => 0,
					'type' => $site[0]['type'],
					'visible' => $site[0]['visible'],
					'uid' => $_SGLOBAL['supe_uid'],
					'createtime' => $_SGLOBAL['timestamp']
				);
				$aid = inserttable('site', $setarr, 1);
				foreach( $str as $key => $val )
				{
					if ( is_numeric($val) && $val == 0 )
					{
						$str[$key]= $aid;
						break;
					}
				}
				$content = implode('|',$str);
				$setarredit_desktop = array(
					'content' => $content
				);
				updatetable('site_desktop', $setarredit_desktop, array('desktop_id'=>intval($_POST['add_desk_id']),'uid'=>$_SGLOBAL['supe_uid']));
				$msg = 3;
			}
		}
	}
}
else
{
	header("location: /window/login.php");
}
?>