<?php
/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: market_json.php 1000 2011-6-31 15:41:43 by angelrain $
 */
 /**
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
*/

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if($_GET['do']=='checklogin'){
	$uid=intval($_GET['uid']);
	if($uid!=$_G['uid']) $change_login_status=1;
	else $change_login_status=0;
	echo json_encode_gbk(array('needfresh'=>$change_login_status));
}elseif($_GET['do']=='updateview'){
	$type=trim($_GET['type']);
	$id=intval($_GET['id']);
	if($_G['coockie']['view_'.$type.'_'.$id]){
		exit();
	}else{
		switch($type){
			case 'app':
				DB::query("update ".DB::table('dzz_apps')." set hot=hot+1,viewnum=viewnum+1 where appid='{$id}'");
				break;
			case 'image':
				DB::query("update ".DB::table('dzz_image')." set viewnum=viewnum+1 where picid='{$id}'");
				break;
			case 'link':
				DB::query("update ".DB::table('dzz_link')." set viewnum=viewnum+1 where lid='{$id}'");
				break;
			case 'video':
				DB::query("update ".DB::table('dzz_video')." set viewnum=viewnum+1 where vid='{$id}'");
				break;
			case 'music':
				DB::query("update ".DB::table('dzz_music')." set viewnum=viewnum+1 where mid='{$id}'");
				break;
			case 'attach':
				DB::query("update ".DB::table('dzz_attach')." set viewnum=viewnum+1 where qid='{$id}'");
				break;
		}
		dsetcookie('view_'.$type.'_'.$id,'1',86400);
	}
	exit();
	
}elseif($_GET['do']=='restore'){
	
}elseif($_GET['do']=='rename'){
	//判断数据唯一
	$ukey=trim($_GET['ukey']);
	if($ukey!=DB::result_first("select `ukey` from ".DB::table('dzz_userconfig')." where uid='{$uid}'")){
		$arr=array();
		$arr['msg']='refresh';
		echo json_encode_gbk($arr);
		exit();
	}
	$arr=array();
	$icoid=intval($_GET['icoid']);
	$text=diconv(getstr($_POST['text'],80),'UTF-8');
	$arr['text']=$text;
	if(!$space['self']){
		$arr['msg']=dzzlang('message','no_privilege');
		echo json_encode_gbk($arr);
		exit();
	}else{
		if($icoarr=DB::fetch_first("select oid,uid,type from ".DB::table('dzz_icos')." where icoid='{$icoid}'")){
			if($icoarr['uid']==$uid || $space['self']>1){
				switch($icoarr['type']){
					case 'folder':
						if(DB::update('dzz_folder',array('fname'=>addslashes($text)),"fid='{$icoarr[oid]}' and uid='{$uid}'")){
							$arr['dataname']='fname';
						}
						break;
					case 'app':
						DB::update('dzz_apps',array('appname'=>addslashes($text)),"appid='{$icoarr[oid]}' and uid='{$uid}'");
						break;
					case 'blog':
						DB::update('dzz_blog',array('title'=>addslashes($text)),"blogid='{$icoarr[oid]}' and uid='{$uid}'");
						break;
					case 'link':
						DB::update('dzz_link',array('title'=>addslashes($text)),"lid='{$icoarr[oid]}' and uid='{$uid}'");
						break;
					case 'video':
						DB::update('dzz_video',array('title'=>addslashes($text)),"vid='{$icoarr[oid]}' and uid='{$uid}'");
						break;
					case 'music':
						DB::update('dzz_music',array('title'=>addslashes($text)),"mid='{$icoarr[oid]}' and uid='{$uid}'");
						break;
					case 'image':
						DB::update('dzz_image',array('title'=>addslashes($text)),"picid='{$icoarr[oid]}' and uid='{$uid}'");
						break;
					case 'attach':
						DB::update('dzz_attach',array('filename'=>addslashes($text)),"qid='{$icoarr[oid]}' and uid='{$uid}'");
						break;
					
				}
				DB::update('dzz_icos',array('name'=>addslashes($text)),"icoid='{$icoid}'");
				$arr['msg']='success';
				echo json_encode_gbk($arr);
				exit();
			}else{
				$arr['msg']=dzzlang('message','no_privilege');
				echo json_encode_gbk($arr);
				exit();
			}

		}else{
			$arr['msg']='not exist icoid';
			echo json_encode_gbk($arr);
			exit();
		}
	}
}elseif($_GET['do']=='getfolderinfo'){
	$data=array();
	$fid=intval($_GET['fid']);
	if($folder=DB::fetch_first("select * from ".DB::table('dzz_folder')." where fid='{$fid}'")){
		$ids=explode(',',$folder['ids']);
		if($ids){
			$icoids=$icodata=array();
			$folderids=$appids=$linkids=$videoids=$imageids=$blogids=$uids=$musicids=$aids=array();
			$appdata=$linkdata=$folderdata=$imagedata=$videodata=$userdata=$blogdata=$musicdata=$attachdata=array();
			$query=DB::query("select * from ".DB::table('dzz_icos')." where icoid IN(".dimplode($ids).")");
			while($value=DB::fetch($query)){
				if($value['type']=='folder') $folderids[]=$value['oid'];
				
				$icoids[]=$value['icoid'];
				$value['url']=replace_canshu($value['url']);
				$icosdata[$value['icoid']]=$value;
			}
			if($icosdata) $data['sourcedata']['icos']=$icosdata;
			
			if($icoids) $data['sourceids']['icos']=$icoids;
			if($folderids) $data['sourceids']['folder']=$folderids;
			
			
		}
		//判断icoid不存在则删除目录ids中此icoid
		$flag=0;
		foreach($ids as $key =>$icoid){
			if(!in_array($icoid,$icoids)){
				unset($ids[$key]);
				$flag=1;
			}
		}
		if($flag) DB::updata('dzz_folder',array('ids'=>implode(',',$ids)),"fid='{$fid}'");
	
		//目录数据
		$folderdata=array();
		if($folderids){
			$query=DB::query("select * from ".DB::table('dzz_folder')." where fid IN (".dimplode($folderids).")");
			while($value=DB::fetch($query)){
				if($value['ids']) $value['ids']=explode(',',$value['ids']);
				else $value['ids']=array();
				$folderdata[$value['fid']]=$value;
			}
			if($folderdata) $data['sourcedata']['folder']=$folderdata;
		}
		$data['msg']='success';
	}else{
		$data['msg']='folder not exsist';
	}
	//print_r($data);
	echo json_encode_gbk($data);
	exit();
		
}elseif($_GET['do']=='deleteIco'){
	
	//判断数据唯一
	$ukey=trim($_GET['ukey']);
	if($ukey!=DB::result_first("select `ukey` from ".DB::table('dzz_userconfig')." where uid='{$uid}'")){
		$arr=array();
		$arr['msg']='refresh';
		echo json_encode_gbk($arr);
		exit();
	}
	
	$arr=array();
	$icoid=intval($_GET['icoid']);
	$desktop=($_GET['desktop']);
	$pfid=intval($_GET['pfid']);
	if($space['self']){
		if(($msg=dzz_delete_ico($icoid))=='success'){
			//处理数据
			if($pfid){
				if($oids=DB::result_first("select ids from ".DB::table('dzz_folder')." where fid='{$pfid}'")){
					$ids=explode(',',$oids);
					$flag=0;
					foreach($ids as $key=>$value){
						if($icoid==$value){
							$flag=1;
							unset($ids[$key]);
						}
					}
					if($flag) DB::update('dzz_folder',array('ids'=>implode(',',$ids)),"fid='{$pfid}'");
				}
			}elseif($desktop){
				if($config=DB::fetch_first("select * from ".DB::table('dzz_userconfig')." where uid='$uid'")){
					$screenlist=unserialize(stripslashes($config['screenlist']));
					if($screenlist[$desktop]) {
						$ids=$screenlist[$desktop]['icos'];
						foreach($ids as $key=>$value){
							if($icoid==$value){
								$flag=1;
								unset($ids[$key]);
							}
						}
						$screenlist[$desktop]['icos']=array_values($ids);
						DB::update('dzz_userconfig',array('screenlist'=>addslashes(serialize($screenlist))),"uid='{$uid}'");
					}
				}
			}elseif($desktop==-1){
				if($config=DB::fetch_first("select * from ".DB::table('dzz_userconfig')." where uid='$uid'")){
					$docklist=explode($config['docklist']);
						foreach($docklist as $key=>$value){
							if($icoid==$value){
								$flag=1;
								unset($docklist[$key]);
							}
					}
					DB::update('dzz_userconfig',array('docklist'=>implode(',',$docklist)),"uid='{$uid}'");
				}
			}
			$arr['msg']='success';
		}else{
			$arr['msg']=$msg;
		}
	}else{
		$arr['msg']= dzzlang('message','no_privilege');
	}
	echo json_encode_gbk($arr);
	exit();
}elseif($_GET['do']=='newfolder'){
	$desktop=($_GET['desktop']);
	$pfid=intval($_GET['pfid']);
	
	//判断数据唯一
	$ukey=trim($_GET['ukey']);
	if($ukey!=DB::result_first("select `ukey` from ".DB::table('dzz_userconfig')." where uid='{$uid}'")){
		$arr=array();
		$arr['msg']='refresh';
		echo json_encode_gbk($arr);
		exit();
	}
	if($space['self']){
		if($arr=dzz_create_folder($uid,$pfid,$desktop)){
			//处理用户桌面数据
			if($pfid){
				if($folder=DB::fetch_first("select ids,fid from ".DB::table('dzz_folder')." where fid='{$pfid}'")){
					if($folder['ids']) $ids=explode(',',$folder['ids']);
					else $ids=array();
					$ids[]=$arr['icoarr']['icoid'];
					DB::update('dzz_folder',array('ids'=>implode(',',$ids)),"fid='{$folder[fid]}'");
					if($arr['folderarr']['ids']) $arr['folderarr']['ids']=explode(',',$arr['folderarr']['ids']);
					else $arr['folderarr']['ids']=array();
					$arr['msg']='success';
				}else{
					DB::delete('dzz_folder',"fid='{$arr[folderarr][fid]}'");
					DB::delete('dzz_icos',"icoid='{$arr[icoarr][icoid]}'");
					$arr=array();
					$arr['msg']='target folder not exist!';
				}
			}elseif($desktop){
				if(strpos($desktop,'sys_')!==false) $duid=0;
				else $duid=$uid;
				if($space['self']<2 && strpos($desktop,'sys_')!==false){
						DB::delete('dzz_folder',"fid='{$arr[folderarr][fid]}'");
						DB::delete('dzz_icos',"icoid='{$arr[icoarr][icoid]}'");
						$arr=array();
						$arr['msg']=dzzlang('message','no_privilege');
				}else{
					if($config=DB::fetch_first("select * from ".DB::table('dzz_userconfig')." where uid='$duid'")){
						$screenlist=unserialize(stripslashes($config['screenlist']));
						if($screenlist[$desktop]) {
							$screenlist[$desktop]['icos'][]=$arr['icoarr']['icoid'];
							DB::update('dzz_userconfig',array('screenlist'=>addslashes(serialize($screenlist))),"uid='{$duid}'");
							if($arr['folderarr']['ids']) $arr['folderarr']['ids']=explode(',',$arr['folderarr']['ids']);
							else $arr['folderarr']['ids']=array();
							$arr['msg']='success';
						}else{
							DB::delete('dzz_folder',"fid='{$arr[folderarr][fid]}'");
							DB::delete('dzz_icos',"icoid='{$arr[icoarr][icoid]}'");
							$arr=array();
							$arr['msg']='target desktop not exist!';
						}
					}
				}
			}
		}else{
			$arr=array();
			$arr['msg']='failure';
		}
	}else{
		$arr=array();
		$arr['msg']= dzzlang('message','not_login');
	}
	
	echo json_encode_gbk($arr);
	exit();
}elseif($_GET['do']=='newdoc'){
	$container=($_GET['container']);
	$ext=trim($_GET['ext']);
	//判断数据唯一
	$ukey=trim($_GET['ukey']);
	if($ukey!=DB::result_first("select `ukey` from ".DB::table('dzz_userconfig')." where uid='{$uid}'")){
		$arr=array();
		$arr['msg']='refresh';
		echo json_encode_gbk($arr);
		exit();
	}
	if($space['self']){
		$filename_title=dzzlang('message','new_'.$ext).get_newdoc_index($uid,$ext).'.'.$ext;
		if($arr=textsaveToattach($filename_title,$container,' ')){
			if($msg=$arr['error']){
				$arr=array();
				$arr['msg']=$msg;
			}else{
				$arr['msg']='success';
			}
		}else{
			$arr=array();
			$arr['msg']='failure';
		}
	}else{
		$arr=array();
		$arr['msg']= dzzlang('message','not_login');
	}
	echo json_encode_gbk($arr);
	exit();

}elseif($_GET['do']=='save'){
	if(!$space['self']) exit();
	$arr=array('msg'=>'success');
	$ukey=trim($_GET['ukey']);
	if($ukey!=DB::result_first("select `ukey` from ".DB::table('dzz_userconfig')." where uid='{$uid}'")){
		$arr['msg']=dzzlang('message','need_refresh');
	}else{
		//处理目录数据
		if($_GET['folderlist']){
			$folders=unserialize(stripslashes(trim(diconv($_GET['folderlist'],'UTF-8'))));
			
			foreach($folders as $key =>$value){
				unset($value['dataline']);
				$fid=$value['fid'];
				unset($value['fid']);
				unset($value['position']);
				unset($value['opened']);
				$value['ids']=implode(',',$value['ids']);
				$value['pfid']=intval(trim($value['pfid']));
				$value['updatetime']=$_G['timestamp'];
				DB::update('dzz_folder',daddslashes($value),"fid='{$fid}'");
			}
		}
		
		$setarr=array();
		if(isset($_GET['docklist'])) $setarr['docklist']=trim($_GET['docklist']);
		if($_GET['screenlist']){
			$_GET['screenlist']=diconv($_GET['screenlist'],'UTF-8');
			if(unserialize(stripslashes(trim($_GET['screenlist'])))){
				$setarr['screenlist']=$_GET['screenlist'];
			}
		}
		if(isset($_GET['iconpositions_u']) && unserialize(stripslashes(trim($_GET['iconpositions_u'])))){
			 $setarr['iconpositions']=stripslashes(trim($_GET['iconpositions_u']));
		}
		if($_GET['spacename']) $setarr['spacename']=trim($_GET['spacename']);
		if($_GET['friend']!='') $setarr['friend']=intval($_GET['friend']);
		if($_GET['current']){$setarr['current']=trim($_GET['current']);}
		if($_GET['thame']){
			 $setarr['thame']=trim($_GET['thame']);
			 $setarr['custom_backimg']=trim($_GET['custom_backimg']);
			 $setarr['custom_url']=trim($_GET['custom_url']);
			 $setarr['custom_window']=trim($_GET['custom_window']);
			 $setarr['custom_browser']=trim($_GET['custom_browser']);
			 $setarr['custom_topbar']=trim($_GET['custom_topbar']);
			 $setarr['custom_filemanage']=trim($_GET['custom_filemanage']);
			 $setarr['custom_dock']=trim($_GET['custom_dock']);
			 $setarr['custom_btype']=trim($_GET['custom_btype']);
		}
		$setarr['updatetime']=$_G['timestamp'];
		DB::update('dzz_userconfig',daddslashes($setarr),"uid='{$uid}'");
		
		if($space['self']>1 && $_GET['screenlist_0']){
			$_GET['screenlist_0']=diconv($_GET['screenlist_0'],'UTF-8');
			if(unserialize(stripslashes(trim($_GET['screenlist_0'])))){
					DB::update('dzz_userconfig',daddslashes(array('screenlist'=>stripslashes(trim($_GET['screenlist_0'])))),"uid='0'");
			}
		}
		if($space['self']>1 && isset($_GET['iconpositions_0']) && unserialize(stripslashes(trim($_GET['iconpositions_0'])))){
			  DB::update('dzz_userconfig',daddslashes(array('iconpositions'=>stripslashes(trim($_GET['iconpositions_0'])))),"uid='0'");
		}
	}
	echo json_encode_gbk($arr);
	exit();
}else{
	require_once libfile('function/friend');
	$data=array();
	if($_G['setting']['dzz_sysconfig']){
		$sysconfig=dstripslashes(unserialize(stripslashes($_G['setting']['dzz_sysconfig'])));
	}else{
		$sysconfig=DB::fetch_first("select * from ".DB::table('dzz_config')." where id='1'");
	}
	if(intval($sysconfig['userscreennum'])<1){
		$sysconfig['userscreennum']=1;
	}
	$sysconfig['ucenterurl']=$_G['setting']['ucenterurl'];
	unset($sysconfig['siteuniqueid']);
	$data['sysconfig']=$sysconfig;
	if($_G['setting']['dzz_iconview']){
		$iconview=dstripslashes(unserialize(stripslashes($_G['setting']['dzz_iconview'])));
	}else{
		$query=DB::query("select * from ".DB::table('dzz_iconview')." where `avaliable`>0 ORDER BY disp");
		$iconview=array();
		while($value=DB::fetch($query)){
			$iconview[$value['id']]=$value;
		}
	}
	$data['iconview']=$iconview;
	//获取插件信息
	$plugins=array();
	$pluginappids=array();
	$query=DB::query("select * from ".DB::table('dzz_plugin')." where available>0");
	while($value=DB::fetch($query)){
		if($value['datatype']) $value['datatype']=explode(',',$value['datatype']);
		else $value['datatype']=array();
		$plugins[$value['pluginid']]=dstripslashes($value);
	}
	//默认导航信息
	$navids_0=array();
	$docklist_0=$screenlist_0=array();
	if($config_0=DB::fetch_first("select * from ".DB::table('dzz_userconfig')." where uid='0'")){
		$screenlist_0=unserialize(stripslashes($config_0['screenlist']));
		$docklist_0=explode(',',$config_0['docklist']);
		$data['iconpositions_0']=unserialize(stripslashes($config_0['iconpositions']));
	}else{//创建默认导航
		if($_G['setting']['dzz_nav']){
			$navbar=dstripslashes(unserialize(stripslashes($_G['setting']['dzz_nav'])));
		}else{
			$query=DB::query("select * from ".DB::table('dzz_navbar')." where `avaliable`>0 ORDER BY disp");
			while($value=DB::fetch($query)){
				$navbar[$value['navid']]=$value;
			}
		}
		
		foreach($navbar as $value){
			if($value['navicon']) $value['navicon']=$_G['setting']['attachurl'].$value['navicon'];
			$value['navid']='sys_'.$value['navid'];
			$screenlist_0[$value['navid']]['config']=$value;
			$screenlist_0[$value['navid']]['icos']=array();
			$screenlist_0[$value['navid']]['wins']=array();
			$screenlist_0[$value['navid']]['widgets']=array();
		}
		
		$setarr=array(
					'uid'=>0,
					'username'=>'',
					'docklist'=>'',
					'screenlist'=>serialize($screenlist_0),
					'iconpositions'=>serialize(array()),
					'dateline'=>$_G['timestamp'],
					);
		DB::insert('dzz_userconfig',daddslashes($setarr),0,1);
	}
	//重写系统导航
	
	if($_G['setting']['dzz_nav']){
		$navbar=dstripslashes(unserialize(stripslashes($_G['setting']['dzz_nav'])));
	}else{
		$query=DB::query("select * from ".DB::table('dzz_navbar')." where `avaliable`>0 ORDER BY disp");
		while($value=DB::fetch($query)){
			$navbar[$value['navid']]=$value;
		}
	}
	$needsave_screenlist_0=0;
	if(count($screenlist_0)<count($navbar)) $needsave_screenlist_0=1;
	foreach($navbar as $value){
		if($value['navicon']) $value['navicon'] =$_G['setting']['attachurl'].$value['navicon'];
		$value['navid']='sys_'.$value['navid'];
		$screenlist_0[$value['navid']]['config']=$value;
		if(!$screenlist_0[$value['navid']]['icos']) $screenlist_0[$value['navid']]['icos']=array();
		if(!$screenlist_0[$value['navid']]['wins']) $screenlist_0[$value['navid']]['wins']=array();
		if(!$screenlist_0[$value['navid']]['widgets']) $screenlist_0[$value['navid']]['widgets']=array();
		$navids_0[]=$value['navid'];
	}
	if($needsave_screenlist_0){
		DB::update('dzz_userconfig',array('screenlist'=>addslashes(serialize($screenlist_0))),"uid='0'");
	}
	$allicoids_0=array();
	$gids=array();
	
	foreach($screenlist_0 as $key =>$desktop){
		if(!in_array($key,$navids_0)) {
			unset($screenlist_0[$key]);
			continue;
		}
		if($desktop['config']['isdefault']) $data['currentDesktop']=$screenlist_0['current']?$screenlist_0['current']:$key;
		$tempicos=array();
		foreach($desktop['icos'] as $icoid =>$value){
			$allicoids_0[]=$value;
			$tempicos[]=$value;
		}
		$screenlist_0[$key]['icos']=$tempicos;
		foreach($desktop['wins'] as $icoid=>$value){
			$allicoids_0[]=$value['icoid'];
		}
		foreach($desktop['widgets'] as $icoid=>$value){
			$gids[]=$value['gid'];
		}
	}
	if(!$uid)	$allicoids_0=array_merge($allicoids_0,$docklist_0);
	
//	print_r($screenlist_0);
	//获取user导航信息
	$navids_u=array();
	$docklist_u=$screenlist_u=array();
	
		if($uid){
			if($config_u=DB::fetch_first("select * from ".DB::table('dzz_userconfig')." where uid='$uid'")){
					//获取所有桌面的icoids；
				$screenlist_u=unserialize(stripslashes($config_u['screenlist']));
				foreach($screenlist_u as $key=>$value){
					$navids_u[]="$key";
				}
				$needsave_screenlist=0;
				for($i=1;$i<=$sysconfig['userscreennum'];$i++){
					if(!in_array("$i",$navids_u)) {
						
						$navids_u[]="$i";
						$screenlist_u[$i]['config']=array(
															'navid'=>$i,
															'navname'=>$lang['desktop'].$i,
															'type'=>'desktop',
															'autolist'=>$sysconfig['autolist'],
															'marginleft'=>$sysconfig['marginleft'],
															'margintop'=>$sysconfig['margintop'],
															'marginright'=>$sysconfig['marginright'],
															'marginbottom'=>$sysconfig['marginbottom'],
															'iconview'=>$sysconfig['iconview'],
															'iconposition'=>$sysconfig['iconposition'],
															'dockshow'=>$sysconfig['dockshow'],
															'topbarshow'=>$sysconfig['topbarshow']
														);
						$screenlist_u[$i]['icos']=array();
						$screenlist_u[$i]['wins']=array();
						$screenlist_u[$i]['widgets']=array();
						$needsave_screenlist=1;
					}
				}
				foreach($screenlist_u as $key=>$value){
					if($key>$sysconfig['userscreennum']) {
						$screenlist_u[$sysconfig['userscreennum']]=desktop_move_userdata($key,$uid,$sysconfig['userscreennum']);
						unset($screenlist_u[$key]);
						foreach($navids_u as $navid =>$value){
							if($navid==$key) unset($navids_u[$key]);
						}
					}
				}
				foreach($screenlist_u as $key=>$value){
					$screenlist_u[$key]['config']['marginleft']=$sysconfig['marginleft'];
					$screenlist_u[$key]['config']['margintop']=$sysconfig['margintop'];
					$screenlist_u[$key]['config']['marginright']=$sysconfig['marginright'];
					$screenlist_u[$key]['config']['marginbottom']=$sysconfig['marginbottom'];
				}
				$docklist_u=explode(',',$config_u['docklist']);
				$data['iconpositions_u']=unserialize(stripslashes($config_u['iconpositions']));
				if($needsave_screenlist){
					DB::update('dzz_userconfig',array('screenlist'=>addslashes(serialize($screenlist_u))),"uid='{$uid}'");
				}
			}else{//如果用户未创建桌面，则创建桌面
				for($i=1;$i<=$sysconfig['userscreennum'];$i++){
					$navids_u[]="$i";
					$screenlist_u[$i]['config']=array(
														'navid'=>$i,
														'navname'=>$lang['desktop'].$i,
														'type'=>'desktop',
														'autolist'=>$sysconfig['autolist'],
														'marginleft'=>$sysconfig['marginleft'],
														'margintop'=>$sysconfig['margintop'],
														'marginright'=>$sysconfig['marginright'],
														'marginbottom'=>$sysconfig['marginbottom'],
														'iconview'=>$sysconfig['iconview'],
														'iconposition'=>$sysconfig['iconposition'],
														'dockshow'=>$sysconfig['dockshow'],
														'topbarshow'=>$sysconfig['topbarshow']
													);
					$screenlist_u[$i]['icos']=array();
					$screenlist_u[$i]['wins']=array();
					$screenlist_u[$i]['widgets']=array();
					
				}
				$docklist_u=array();
				//添加默认挂件
				$setupwids=array();
				$query=DB::query("select * from ".DB::table('dzz_widget')." where uid='-1'");
				while($value=DB::fetch($query)){
					if(!$value['notdelete']){
						unset($value['gid']);
						$value['uid']=$uid;
						$value['username']=$space['username'];
						$value['gid']=DB::insert('dzz_widget',daddslashes($value),1);
					}
					if($value['desktop']>0 && in_array($value['desktop'],$navids_u)){
						 $gidarr=array('gid'=>$value['gid'],
									   'navid'=>$value['desktop'],
									   'left'=>0,
									   'top'=>0,
									   'zIndex'=>100
									   );
									   //{gid:this.gid,navid:this.desktop,left:this.left,top:this.top,zIndex:this.zIndex};
						 $screenlist_u[$value['desktop']]['widgets'][$value['gid']]= $gidarr;
					}
					$setupwids[]=$value['oid'];
				}
				//添加默认应用
				$query=DB::query("select * from ".DB::table('dzz_icos')." where uid='-1'");
				$setupids=array();
				while($value=DB::fetch($query)){
					
					if(!$value['notdelete']){
						unset($value['icoid']);
						$value['uid']=$uid;
						$value['username']=$space['username'];
						$value['icoid']=DB::insert('dzz_icos',daddslashes($value),1);
					}
					if($value['desktop']<0) $docklist_u[]=$value['icoid'];
					if($value['desktop']>0) $screenlist_u[$value['desktop']]['icos'][]=$value['icoid'];
					$setupids[]=$value['oid'];
				}
				$setarr=array(
					'uid'=>$uid,
					'username'=>$space['username'],
					'docklist'=>implode(',',$docklist_u?$docklist_u:$docklist_0),
					'screenlist'=>serialize($screenlist_u),
					'iconpositions'=>serialize(array()),
					'dateline'=>$_G['timestamp'],
					'current'=>1
					);
				DB::insert('dzz_userconfig',daddslashes($setarr),0,1);
				//统计应用和挂件安装量
				if($setupids) DB::query("update ".DB::table('dzz_apps')." set setupnum=setupnum+1 where appid IN (".dimplode($setupids).")");
				if($setupwids) DB::query("update ".DB::table('dzz_widget_market')." set setupnum=setupnum+1 where wid IN (".dimplode($setupwids).")");
			}
		}
	
	$allicoids_u=array();
	//$screenlist=array_merge($screenlist_0,$screenlist_u);
	foreach($screenlist_u as $key =>$desktop){
		if($desktop['config']['isdefault']) $data['currentDesktop']=$key;
		$tempicos_u=array();
		foreach($desktop['icos'] as $icoid =>$value){
			$allicoids_u[]=$value;
			$tempicos_u[]=$value;
		}
		$screenlist_u[$key]['icos']=$tempicos_u;
		foreach($desktop['wins'] as $icoid=>$value){
			$allicoids_u[]=$value['icoid'];
		}
		foreach($desktop['widgets'] as $icoid=>$value){
			$gids[]=$value['gid'];
		}
	}
	//$docklist=array_merge($docklist_0,$docklist_u);
	$allicoids_u=array_merge($allicoids_u,$docklist_u);
	if($uid){
		//判断默认不能删除的应用是否存在于用户桌面，不存在则添加
		$query=DB::query("select * from ".DB::table('dzz_icos')." where uid='-1' and notdelete=1");
		while($value=DB::fetch($query)){
			if(!in_array($value['icoid'],$allicoids_u)){
				if($value['desktop']>0 && in_array($value['desktop'],$navids_u)){
						$screenlist_u[$value['desktop']]['icos'][]=$value['icoid'];
						$allicoids_u[]=$value['icoid'];
				}elseif($value['desktop']==-1){
					$docklist_u[]=$value['icoid'];
					$allicoids_u[]=$value['icoid'];
				}
			}
		}
		
		//判断默认不能删除的挂件是否存在于用户桌面，不存在则添加
		$query=DB::query("select * from ".DB::table('dzz_widget')." where uid='-1' and notdelete>0");
		while($value=DB::fetch($query)){
			if(!in_array($value['gid'],$gids)){
				if($value['desktop']>0 && in_array($value['desktop'],$navids_u)){
						 $gidarr=array('gid'=>$value['gid'],
					 			   'navid'=>$value['desktop'],
								   'left'=>0,
								   'top'=>0,
								   'zIndex'=>100
								   );
					
						$screenlist_u[$value['desktop']]['widgets'][$value['gid']]=$gidarr;
						$gids[]=$value['gid'];
						
				}
			}
		}
	}
	
	//获取用户所有的ico数据(不包含目录内）；
	$allicoids=array_merge($allicoids_u,$allicoids_0);
	$icosdata=$icoids=$folderids=$appids=$videoids=$linkids=$imageids=$uids=$blogids=$musicids=$aids=array();
	if($allicoids){
		$query=DB::query("select * from ".DB::table('dzz_icos')." where icoid IN(".dimplode($allicoids).") OR (uid='{$space[uid]}' and isshow='0') OR (uid='-1' and notdelete='1')");
		while($value=DB::fetch($query)){
			if($value['type']=='folder') $folderids[]=$value['oid'];
			$value['url']=replace_canshu($value['url']);
			$value['fsize']=formatsize($value['size']);
			$value['ftype']=getFileTypeName($value['type'],$value['ext']);
			$value['fdateline']=dgmdate($value['dateline']);
			if(!$value['ext']) $value['ext']=strtolower(substr(strrchr($value['url'], '.'), 1, 10));
			$icosdata[$value['icoid']]=$value;
			
			//echo $space['isfriend'].'====='. $value['friend'].'<br>';
			/*if($space['self']<2){
				if($value['friend']<1){
					
				}elseif($value['friend']==2){
					if(!friend_check($value['uid']) && $value['uid']!=$_G['uid']) continue;
				}elseif($value['friend']==1){
					if($value['uid']!=$_G['uid']) continue;
				}
			}*/
			// continue;
			
			if($value['isshow']) $icoids[]=$value['icoid'];
			
		}
	}
	
		
		//目录数据
		$folderdata=array();
		if($folderids){
			$query=DB::query("select * from ".DB::table('dzz_folder')." where fid IN (".dimplode($folderids).")");
			$folderids=array();
			while($value=DB::fetch($query)){
				if($value['ids']) $value['ids']=explode(',',$value['ids']);
				else $value['ids']=array();
				$folderdata[$value['fid']]=$value;
				$folderids[]=$value['fid'];
			}
		}
		
	//判断用户桌面icoid是否存在于表dzz_icos中；不存在则删除此icoid
	foreach($screenlist_u as $key =>$desktop){
		if(!in_array($key,$navids_u)) unset($screenlist_u[$key]);
		foreach($desktop['icos'] as $icoid =>$value){
			if(!in_array($value,$icoids)){
				unset($screenlist_u[$key]['icos'][$icoid]);
			}
		}
	}
	foreach($screenlist_0 as $key =>$desktop){
		if(!in_array($key,$navids_0)) unset($screenlist_0[$key]);
		foreach($desktop['icos'] as $icoid =>$value){
			if(!in_array($value,$icoids)){
				unset($screenlist_0[$key]['icos'][$icoid]);
			}
		}
	}
	if(!$uid){
		$docklist=array();
		foreach($docklist_0 as $key =>$value){
			if(in_array($value,$icoids)){
				$docklist[]=$value;
			}
		}
	}else{
		$docklist=array();
		foreach($docklist_u as $key =>$value){
			if(in_array($value,$icoids)){
				$docklist[]=$value;
			}
		}
	}
	//获取所有挂件数据
	$widgetdata=array();
	$gids_all=array();
	if($gids){
		$query=DB::query("select * from ".DB::table('dzz_widget')." where gid IN(".dimplode($gids).")");
		while($value=DB::fetch($query)){
			$gids_all[]=$value['gid'];
			$value['url']=replace_canshu($value['url']);
			$widgetdata[$value['gid']]=$value;
		}
	}
	
	
	//判断如果没有此挂件的数据 则删除桌面上的此挂件的gid
	foreach($screenlist_u as $key =>$desktop){
		foreach($desktop['widgets'] as $icoid =>$value){
			if(!in_array($icoid,$gids_all)){
				unset($screenlist_u[$key]['widgets'][$icoid]);
			}
		}
	}
	foreach($screenlist_0 as $key =>$desktop){
		foreach($desktop['widgets'] as $icoid =>$value){
			if(!in_array($icoid,$gids_all)){
				unset($screenlist_0[$key]['widgets'][$icoid]);
			}
		}
	}
	/*$loginfo=array();
	$loginfo['uid']=$uid;
	$loginfo['username']=$space['username'];
	$loginfo['touxiang']=$_G['setting']['ucenterurl'].'/avatar.php?uid='.$uid.'&size=small';
	$loginfo['formhash']=$_G['formhash'];*/
	//print_r($ooscreenlist);
	$data['formhash']=$_G['formhash'];
	
	$data['navids']=array_merge($navids_0,$navids_u);
	$data['docklist']=$docklist;
	$data['screenlist']=array('screenlist_0'=>$screenlist_0,'screenlist_u'=>$screenlist_u);
	$data['sourceids']=array(
							'icos'=>$icoids,
							'folder'=>$folderids,
							);
	$data['sourcedata']=array(
							'icos'=>$icosdata?$icosdata:array(),
							'folder'=>$folderdata?$folderdata:array(),
							'plugin'=>$plugins,
							'widgets'=>$widgetdata,
							);
	$space['attachextensions']=$space['attachextensions']?explode(',',$space['attachextensions']):array();
	$data['space']=$space;
	
	$arr=array();
	if($uid) $arr=DB::fetch_first("select * from ".DB::table('dzz_thame')." where id='{$config_u[thame]}'");
	if(!$arr){
		$arr=DB::fetch_first("select * from ".DB::table('dzz_thame')." where `default`='1'");
	}
	if(!$arr['backimg']) $arr['backimg']='dzz/styles/thame/'.$arr['folder'].'/back.jpg';
	//print_r($arr);
	$data['thame']['system']=$arr;
	$custom_backimg='';
	

	$data['thame']['custom']=array(
									'custom_backimg'=>$config_u['custom_backimg']?$config_u['custom_backimg']:'',
									'custom_url'=>$config_u['custom_url']?$config_u['custom_url']:'',
									'custom_window'=>$config_u['custom_window']?$config_u['custom_window']:'',
									'custom_browser'=>$config_u['custom_browser']?$config_u['custom_browser']:'',
									'custom_topbar'=>$config_u['custom_topbar']?$config_u['custom_topbar']:'',
									'custom_filemanage'=>$config_u['custom_filemanage']?$config_u['custom_filemanage']:'',
									'custom_dock'=>$config_u['custom_dock']?$config_u['custom_dock']:'',
									'custom_btype'=>$config_u['custom_btype']?$config_u['custom_btype']:''
								);
		
	$data['thame']=dstripslashes($data['thame']);
	if($config_u['current'] && (in_array($config_u['current'],$navids_0) || in_array($config_u['current'],$navids_u))) $data['currentDesktop']=$config_u['current'];
	if(!$data['currentDesktop']) $data['currentDesktop']=$navids_0[0];
	if($space['self']){
		$ukey=random(16);
		if(DB::update('dzz_userconfig',array('ukey'=>$ukey),"uid='{$uid}'")){
			$data['ukey']=$ukey;
		}else{
			$data['ukey']=$config_u['ukey'];
		}
	}
   // print_r($data);
	//print_r($space);

	echo json_encode_gbk($data);
	exit();
}

?>