<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	$Id: site_desktop_add.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 = $publish = $bar = $desktop = array();
$cdtat_start = '****----****';
$cdtat_end = '####----####';
if ( !empty($_SGLOBAL['supe_uid']) )
{
	if ( isset( $_POST['add_desktop_form_action'] ) && $_POST['add_desktop_form_action'] == 'save' )
	{
		$str = '';
		if ( isset( $_POST['desktop_type'] ) && $_POST['desktop_type'] == 'multi' )
		{
			if ( isset( $_POST['start_content'] ) && $_POST['start_content'] == 'empty' )
			{
				foreach( $_SITE as $key )
				{
					foreach( $key as $val )
					{
						if ( $val == 1 )
						{
							$str .= '0|';
						}
						else
						{
							$str .= $val.'|';
						}
					}
				}
				$str = substr($str,0,-1);
				$setarr = array(
					'uid' => $_SGLOBAL['supe_uid'],
					'content' => $str,
					'active' => 0,
					'type' => strip_tags($_POST['desktop_type']),
					'createtime' => $_SGLOBAL['timestamp']
				);
				$aid = inserttable('site_desktop', $setarr, 1);
				$desktopId = $aid;
				$setbararr = array(
					'desktop_id' => $aid,
					'bar_name' => addslashes(strip_tags($_POST['desktop_name'])),
					'uid' => $_SGLOBAL['supe_uid'],			
					'active' => 0,
					'createtime' => $_SGLOBAL['timestamp']
				);
				inserttable('site_bar', $setbararr, 1);
			}
			elseif ( isset( $_POST['start_content'] ) && intval($_POST['start_content']) == 25 )
			{
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_desktop')." WHERE desktop_id=".intval($_POST['start_content']));
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$list[] = $value;
				}
				$str = $list[0]['content'];
				$setarr = array(
					'uid' => $_SGLOBAL['supe_uid'],
					'content' => $str,
					'active' => 0,
					'type' => strip_tags($_POST['desktop_type']),
					'createtime' => $_SGLOBAL['timestamp']
				);
				$aid = inserttable('site_desktop', $setarr, 1);
				$desktopId = $aid;
				$setbararr = array(
					'desktop_id' => $aid,
					'bar_name' => addslashes(strip_tags($_POST['desktop_name'])),
					'uid' => $_SGLOBAL['supe_uid'],			
					'active' => 0,
					'createtime' => $_SGLOBAL['timestamp']
				);
				inserttable('site_bar', $setbararr, 1);
			}
		}
		elseif ( isset( $_POST['desktop_type'] ) && $_POST['desktop_type'] == 'rss' )
		{
			if ( isset( $_POST['start_content'] ) && $_POST['start_content'] == 'empty' )
			{
				$setarr = array(
					'uid' => $_SGLOBAL['supe_uid'],
					'content' => '||',
					'active' => 0,
					'type' => strip_tags($_POST['desktop_type']),
					'createtime' => $_SGLOBAL['timestamp']
				);
				$aid = inserttable('site_desktop', $setarr, 1);
				$desktopId = $aid;
				$setbararr = array(
					'desktop_id' => $aid,
					'bar_name' => addslashes(strip_tags($_POST['desktop_name'])),
					'uid' => $_SGLOBAL['supe_uid'],			
					'active' => 0,
					'createtime' => $_SGLOBAL['timestamp']
				);
				inserttable('site_bar', $setbararr, 1);
				$display = 'rss_empty';
			}
			elseif ( isset( $_POST['start_content'] ) && $_POST['start_content'] == 'default' )
			{
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_desktop')." WHERE desktop_id=32");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$list[] = $value;
				}
				$str = $list[0]['content'];
				$setarr = array(
					'uid' => $_SGLOBAL['supe_uid'],
					'content' => $str,
					'active' => 0,
					'type' => strip_tags($_POST['desktop_type']),
					'createtime' => $_SGLOBAL['timestamp']
				);
				$aid = inserttable('site_desktop', $setarr, 1);
				$desktopId = $aid;
				$setbararr = array(
					'desktop_id' => $aid,
					'bar_name' => addslashes(strip_tags($_POST['desktop_name'])),
					'uid' => $_SGLOBAL['supe_uid'],			
					'active' => 0,
					'createtime' => $_SGLOBAL['timestamp']
				);
				inserttable('site_bar', $setbararr, 1);
				$display = 'rss_empty';
			}
			elseif ( isset( $_POST['start_content'] ) && $_POST['start_content'] == 32 )
			{
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_desktop')." WHERE desktop_id=32");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$list[] = $value;
				}
				$str = $list[0]['content'];
				$setarr = array(
					'uid' => $_SGLOBAL['supe_uid'],
					'content' => $str,
					'active' => 0,
					'type' => strip_tags($_POST['desktop_type']),
					'createtime' => $_SGLOBAL['timestamp']
				);
				$aid = inserttable('site_desktop', $setarr, 1);
				$desktopId = $aid;
				$setbararr = array(
					'desktop_id' => $aid,
					'bar_name' => addslashes(strip_tags($_POST['desktop_name'])),
					'uid' => $_SGLOBAL['supe_uid'],			
					'active' => 0,
					'createtime' => $_SGLOBAL['timestamp']
				);
				inserttable('site_bar', $setbararr, 1);
				$display = 'rss_empty';
			}
		}
	}
	if ( isset( $_POST['act'] ) && $_POST['act'] == "search" )
	{
		if ( isset( $_POST['desktop_type'] ) && $_POST['desktop_type'] == 'publish' )
		{
			$perpage = 5;
			$page = empty($_POST['page'])?1:intval($_POST['page']);
			if($page<1) $page = 1;
			$start = ($page-1)*$perpage;
			//检查开始数
			ckstart($start, $perpage);
			if($perpage > 100) {
				$count = 1;
			} else {
				$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('site_publish')." WHERE publish_title like '%".addslashes($_POST['search_terms'])."%'"), 0);
			}
			$multi = '';
			if($count) {
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_publish')." WHERE publish_title like '%".addslashes($_POST['search_terms'])."%' LIMIT $start,$perpage");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$publish[] = $value;
				}
				$multi = site_multi($count, $perpage, $page, $mpurl, addslashes($_POST['search_terms']));
			}
		}
	}
	elseif ( isset( $_POST['act'] ) && $_POST['act'] == "show" )
	{
		if ( isset( $_POST['desktop_type'] ) && $_POST['desktop_type'] == 'publish' )
		{
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_publish')." WHERE id=".intval($_POST['id']));
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$publish[] = $value;
			}
		}
	}
	elseif ( isset( $_POST['act'] ) && $_POST['act'] == "browse" )
	{
		if ( isset( $_POST['desktop_type'] ) && $_POST['desktop_type'] == 'publish' )
		{
			$perpage = 5;
			$page = empty($_POST['page'])?1:intval($_POST['page']);
			if($page<1) $page = 1;
			$start = ($page-1)*$perpage;
			//检查开始数
			ckstart($start, $perpage);
			if($perpage > 100) {
				$count = 1;
			} else {
				$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('site_publish')." WHERE category=".intval($_POST['cat'])), 0);
			}
			$multi = '';
			if($count) {
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_publish')." WHERE category=".intval($_POST['cat'])." LIMIT $start,$perpage");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$publish[] = $value;
				}
				$multi = site_multi_browse($count, $perpage, $page, $mpurl, intval($_POST['cat']));
			}
		}
	}
	elseif ( isset( $_POST['act'] ) && $_POST['act'] == "add" )
	{
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_publish')." WHERE id=".intval($_POST['id']));
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			$publish[] = $value;
		}
		$add_id = $publish[0]['desktop_id'];
		if ( $publish )
		{
			if ( $publish[0]['uid'] != $_SGLOBAL['supe_uid'] )
			{
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('site_desktop')." WHERE desktop_id=".$publish[0]['desktop_id']);
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$list[] = $value;
				}
				if ( $list )
				{
					$query = $_SGLOBAL['db']->query("SELECT desktop_id FROM ".tname('site_bar')." WHERE uid=".$_SGLOBAL['supe_uid']);
					while ($value = $_SGLOBAL['db']->fetch_array($query)) {
						$desktop[] = $value;
					}
					foreach( $desktop as $key )
					{
						$bar[] = $key['desktop_id'];
					}
					if ( !in_array($publish[0]['desktop_id'],$bar) )
					{
						$setarr = array(
							'uid' => $_SGLOBAL['supe_uid'],
							'content' => $list[0]['content'],
							'active' => 0,
							'type' => $list[0]['type'],
							'createtime' => $_SGLOBAL['timestamp']
						);
						$aid = inserttable('site_desktop', $setarr, 1);
						$setbararr = array(
							'desktop_id' => $aid,
							'bar_name' => $publish[0]['publish_title'],
							'uid' => $_SGLOBAL['supe_uid'],			
							'active' => 0,
							'publish' => 1,
							'publish_uid' => $publish[0]['uid'],
							'publish_desktop_id' => $publish[0]['desktop_id'],
							'createtime' => $_SGLOBAL['timestamp']
						);
						inserttable('site_bar', $setbararr, 1);
						unset($setarr);
						unset($setbararr);
						$add_id = $aid;
						$setarr = array(
							'num' => $publish[0][num] + 1
						);
						updatetable('site_publish', $setarr, array('id'=>intval($_POST['id'])));
						unset($setarr);
					}
				}
			}
		}
	}
}
else
{
	header("location: /window/login.php");
	exit;
}
?>