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

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}
@include_once(S_ROOT.'./data/site_config.php');
global $_SC;
$list = $search = $desktop = $edit_arr =array();
$cdtat_start = '****----****';
$cdtat_end = '####----####';
$previous_step = $step = 10;
$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 0;
if ( !empty($_SGLOBAL['supe_uid']) )
{
	if ( $_POST['refresh_desktop'] == 1 )
	{
		$refresh_desktop = '$("#cache_add_icon_alternative_images").html("");';
	}
	$display = true;
	if ( isset( $_POST['action'] ) && $_POST['action'] == 'add' && isset( $_POST['content'] ) && $_POST['content'] == 'links' )
	{
		if ( isset( $_POST['add_icon_url'] ) )
		{
			if (  $_POST['add_icon_url'] == 'http://' )
			{
				$add_icon_url = 'empty';
				$previous_step = $step = 10;
			}
			else
			{
				if ( isset( $_POST['step'] ) && $_POST['step'] == 40 )
				{
					$previous_step = $step = 40;
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site')." WHERE url like '%".addslashes($_POST['add_icon_url'])."%' and hot=1 and type='".addslashes($_POST['content'])."' limit 1");
					while ($value = $_SGLOBAL['db']->fetch_array($query)) {
						$search[] = $value;
					}
					$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;
					$_POST['add_icon_label'] = str_replace('http://','',$_POST['add_icon_url']);
				}
				elseif( isset( $_POST['step'] ) && $_POST['step'] == 50 )
				{
					if ( isset( $_POST['add_icon_image_uri'] ) && $_POST['add_icon_image_uri'] != '' )
					{
						$previous_step = $step = 60;
						if ( $_POST['action'] == 'add' )
						{
							if ( $_POST['add_icon_image_type'] == 3 )
							{
								$tmp_path = mkdir_custom_dir(true);
								$oldpath = '../icon/tmp/'.$_SC['site_key'].'.png';
								$newpath = '../icon/custom/upload/'.$tmp_path.$_SGLOBAL['supe_uid'].'_'.$_SGLOBAL['timestamp'].'.png';
								@rename($oldpath,$newpath);
								$icon_path = $newpath;
							}
							else
							{
								$icon_path = addslashes($_POST['add_icon_image_default_uri']);
							}
							$setarr = array(
								'title' => addslashes($_POST['add_icon_label']),
								'url' => addslashes($_POST['add_icon_url']),
								'icon' => $icon_path,
								'color' => addslashes($_POST['add_icon_image_default_color']),
								'titlecolor' => '#'.addslashes($_POST['add_icon_label_color']),
								'type' => addslashes($_POST['content']),
								'visible' => intval($_POST['add_icon_label_visible']),
								'uid' => $_SGLOBAL['supe_uid'],
								'createtime' => $_SGLOBAL['timestamp']
							);
							$aid = inserttable('site', $setarr, 1);
							$step_js = "hideWindow(); iconAdd('c$aid', 'links');";
						}
						elseif ( $_POST['action'] == 'edit' )
						{
							$aid = intval($_POST['icon_id']);
							$step_js = 'windowContentShow("links", function() { addIconLoadTab("links", "custom"); });onWindowClose = function(){loadDesktop(desktopId);};';
							$setarr = array(
								'title' => addslashes($_POST['add_icon_label']),
								'url' => addslashes($_POST['add_icon_url']),
								'icon' => addslashes($_POST['add_icon_image_default_uri']),
								'color' => addslashes($_POST['add_icon_image_default_color']),
								'titlecolor' => addslashes($_POST['add_icon_label_color']),
								'type' => addslashes($_POST['content']),
								'visible' => intval($_POST['add_icon_label_visible']),
								'createtime' => $_SGLOBAL['timestamp']
							);
							updatetable('site', $setarr, array('id'=>$aid));
						}
					}
					else
					{
						$previous_step = $step = 40;
						$msg = 1;
					}
				}
				else
				{
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site')." WHERE url like '%".addslashes($_POST['add_icon_url'])."%' and hot=1 and type='".addslashes($_POST['content'])."' limit 1");
					while ($value = $_SGLOBAL['db']->fetch_array($query)) {
						$search[] = $value;
					}
					if ( !empty( $search ) )
					{
						if ( $_POST['step'] == 0 )
						{
							$previous_step = $step = 10;
						}
						else
						{
							$previous_step = $step = 20;
							$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;				
						}
					}
					else
					{
						if ( $_POST['step'] == 0 )
						{
							$previous_step = $step = 10;
							$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;
						}
						else
						{
							$previous_step = $step = 40;
							$_POST['add_icon_label'] = str_replace('http://','',$_POST['add_icon_url']);
							//$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;					
						}
					}
				}
			}
		}
		else
		{
			$previous_step = $step = 10;
		}
	}
	elseif ( isset( $_POST['action'] ) && $_POST['action'] == 'add' && isset( $_POST['content'] ) && $_POST['content'] == 'feeds' )
	{
		if ( isset( $_POST['add_icon_url'] ) )
		{
			if (  $_POST['add_icon_url'] == 'http://' )
			{
				$add_icon_url = 'empty';
				$previous_step = $step = 10;
			}
			else
			{
				if ( isset( $_POST['step'] ) && $_POST['step'] == 40 )
				{
					$previous_step = $step = 40;
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site')." WHERE url like '%".addslashes($_POST['add_icon_url'])."%' and hot=1 and type='".addslashes($_POST['content'])."' limit 1");
					while ($value = $_SGLOBAL['db']->fetch_array($query)) {
						$search[] = $value;
					}
					$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;
					$_POST['add_icon_label'] = str_replace('http://','',$_POST['add_icon_url']);
				}
				elseif( isset( $_POST['step'] ) && $_POST['step'] == 50 )
				{
					if ( isset( $_POST['add_icon_image_uri'] ) && $_POST['add_icon_image_uri'] != '' )
					{
						$previous_step = $step = 60;
						if ( $_POST['action'] == 'add' )
						{
							if ( $_POST['add_icon_image_type'] == 3 )
							{
								$tmp_path = mkdir_custom_dir(true);
								$oldpath = '../icon/tmp/'.$_SC['site_key'].'.png';
								$newpath = '../icon/custom/upload/'.$tmp_path.$_SGLOBAL['supe_uid'].'_'.$_SGLOBAL['timestamp'].'.png';
								@rename($oldpath,$newpath);
								$icon_path = $newpath;
							}
							else
							{
								$icon_path = addslashes($_POST['add_icon_image_default_uri']);
							}
							$setarr = array(
								'title' => addslashes($_POST['add_icon_label']),
								'url' => addslashes($_POST['add_icon_url']),
								'icon' => $icon_path,
								'color' => addslashes($_POST['add_icon_image_default_color']),
								'titlecolor' => '#'.addslashes($_POST['add_icon_label_color']),
								'type' => addslashes($_POST['content']),
								'visible' => intval($_POST['add_icon_label_visible']),
								'uid' => $_SGLOBAL['supe_uid'],
								'createtime' => $_SGLOBAL['timestamp']
							);
							$aid = inserttable('site', $setarr, 1);
							$step_js = "hideWindow(); iconAdd('c$aid', 'feeds');";
						}
					}
					else
					{
						$previous_step = $step = 40;
						$msg = 1;
					}
				}
				else
				{
					$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site')." WHERE url like '%".addslashes($_POST['add_icon_url'])."%' and hot=1 and type='".addslashes($_POST['content'])."' limit 1");
					while ($value = $_SGLOBAL['db']->fetch_array($query)) {
						$search[] = $value;
					}
					if ( !empty( $search ) )
					{
						if ( $_POST['step'] == 0 )
						{
							$previous_step = $step = 10;
						}
						else
						{
							$previous_step = $step = 20;
							$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;				
						}
					}
					else
					{
						if ( $_POST['step'] == 0 )
						{
							$previous_step = $step = 10;
							$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;
						}
						else
						{
							$previous_step = $step = 40;
							$_POST['add_icon_label'] = str_replace('http://','',$_POST['add_icon_url']);
							//$add_icon_alternative_icons_size = $add_icon_alternative_images_size = 1;					
						}
					}
				}
			}
		}
		else
		{
			$previous_step = $step = 10;
		}
	}
	elseif ( isset( $_POST['action'] ) &&  $_POST['action'] == 'edit' )
	{
		if ( isset( $_POST['step'] ) && $_POST['step'] == 10 )
		{
			$previous_step = $step = 40;
			$_POST['add_icon_label'] = addslashes($_POST['add_icon_label']);
		}
		elseif( isset( $_POST['step'] ) && $_POST['step'] == 50 )
		{
			$previous_step = $step = 60;
			$aid = intval($_POST['icon_id']);
			$query = $_SGLOBAL['db']->query("SELECT hot,uid FROM ".tname('site')." WHERE id=".$aid." limit 1");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$check_user[] = $value;
			}
			if ( $check_user[0]['hot'] != 1 && $_SGLOBAL['supe_uid'] == $check_user[0]['uid'] )
			{
				$step_js = 'windowContentShow("links", function() { addIconLoadTab("links", "custom"); });onWindowClose = function(){loadDesktop(desktopId);};';
				if ( $_POST['add_icon_image_type'] == 3 )
				{
					$tmp_path = mkdir_custom_dir(true);
					$oldpath = '../icon/tmp/'.$_SC['site_key'].'.png';
					$newpath = '../icon/custom/upload/'.$tmp_path.$_SGLOBAL['supe_uid'].'_'.$_SGLOBAL['timestamp'].'.png';
					@rename($oldpath,$newpath);
					$icon_path = $newpath;
				}
				else
				{
					$icon_path = addslashes($_POST['add_icon_image_default_uri']);
				}
				$setarr = array(
					'title' => addslashes($_POST['add_icon_label']),
					'url' => addslashes($_POST['add_icon_url']),
					'icon' => $icon_path,
					'color' => addslashes($_POST['add_icon_image_default_color']),
					'titlecolor' => '#'.addslashes($_POST['add_icon_label_color']),
					'type' => addslashes($_POST['content']),
					'visible' => intval($_POST['add_icon_label_visible']),
					'createtime' => $_SGLOBAL['timestamp']
				);
				updatetable('site', $setarr, array('id'=>$aid));
			}
		}
		else
		{
			if ( isset( $_POST['icon_id'] ) &&  intval($_POST['icon_id']) > 0 )
			{
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site')." WHERE url like hot=0 and id=".intval($_POST['icon_id'])." limit 1");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$edit_arr[] = $value;
				}
				if ( isset( $_POST['add_icon_url'] ) )
				{

				}
				else
				{
					$previous_step = $step = 10;
				}
				if ( $edit_arr[0]['type'] == 'links' )
				{
					$addTab = 'addIconLoadTab("links", "add", true);';
				}
				else
				{
					$addTab = 'addIconLoadTab("feeds", "add", true);';
				}
			}
		}
	}
}
else
{
	$display = false;
}

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.'/';
}
?>