<?php
/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: market_ajax.php 1000 2011-6-31 15:41:43 by angelrain $
 */
 /**
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
*/
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
define('CURRENT_PATH','dzz/system');
$do = empty($_GET['do'])?'':$_GET['do'];
//判断数据唯一
$refer=dreferer();
$msg='';
	//if(!$space['self']) showmessage('no_privelege',DZZSCRIPT,'',array('showdialog'=>1, 'showmsg' => true, 'closetime' => 3));


if($do=='get_children'){
	$sid=empty($_GET['id'])?0:$_GET['id'];
	$data=array();
	list($prex,$id)=explode('-',$sid);
	
	if(!$prex){
		/*
		//[{"attr":{"id":"node_2","rel":"drive"},"data":"C:","state":"closed"},{"attr":{"id":"node_6","rel":"drive"},"data":"D:","state":"closed"}]
		$data[]=array('attr'=>array('id'=>'type','rel'=>'type'),
					   'data'=>'类型',
					   'state'=>'closed'
					   );*/
		$data[]=array('attr'=>array('id'=>'position','rel'=>'position'),
					   'data'=>dzzlang('message','position'),
					   'state'=>'closed'
					   );
		/*$data[]=array('attr'=>array('id'=>'user','rel'=>'user'),
					   'data'=>'用户',
					   'state'=>'closed'
					   );
		$data[]=array('icosdata'=>'','folderdata'=>'');
	}elseif($prex=='user'){
		$data[]=array('attr'=>array('id'=>'friend','rel'=>'friend'),
					   'data'=>'好友',
					   'state'=>''
					   );
		$data[]=array('attr'=>array('id'=>'alluser','rel'=>'alluser'),
					   'data'=>'所有用户',
					   'state'=>''
					   );*/
	/*}elseif($prex=='type'){
		$arr=array('blog'=>'我的文档','image'=>'我的图片','video'=>'我的视频','music'=>'我的音乐','link'=>'我的网址','app'=>'我的应用','other'=>'其他类型');
		foreach($arr as $key => $val){
			$data[]=array('attr'=>array('id'=>'type_'.$key,'rel'=>$key),
						   'data'=>$val,
						   'state'=>''
						   );
		}
		$data[]=array('icosdata'=>array(),'folderdata'=>array());*/
	}elseif($prex=='position'){
		$arr=array();
		//获取用户桌面信息，判断有目录；
		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){
					$arr[$key]=array('id'=>'d-'.$key.'-'.$uid,'name'=>$value['config']['navname']);
					if($value['icos'] && DB::result_first("select COUNT(*) from ".DB::table('dzz_icos')." where icoid IN (".dimplode($value['icos']).") and type='folder'")) {
						$arr[$key]['state']='closed';
					}else{
							$arr[$key]['state']='';
					}
				}
		}
		foreach($arr as $value){
			$data[]=array('attr'=>array('id'=>$value['id'],'rel'=>'desktop'),
					   'data'=>$value['name'],
					   'state'=>$value['state']
					   );

		}
		$data[]=array('icosdata'=>array(),'folderdata'=>array());
	}elseif($prex=='d'){
		$arr=array();
		$ficos=array();
		$icosdata=array();
		$folderdata=array();
		$folderids=array();
		if($config_u=DB::fetch_first("select * from ".DB::table('dzz_userconfig')." where uid='$uid'")){
				//获取所有桌面的icoids；
				$screenlist_u=unserialize(stripslashes($config_u['screenlist']));
				$icos=$screenlist_u[$id]['icos'];
				if($icos){
					$query=DB::query("select ico.icoid,ico.oid,ico.name,folder.ids,folder.friend from ".DB::table('dzz_icos')." ico
													LEFT JOIN " .DB::table('dzz_folder')." folder ON ico.oid=folder.fid
													where ico.icoid IN (".dimplode($icos).") and ico.type='folder'");
					while($value=DB::fetch($query)){
						$ficos[]=$value['icoid'];
						
						$arr[$value['oid']]=array('id'=>'f-'.$value['oid'].'-'.$uid,'name'=>$value['name'],'icoid'=>$value['icoid']);
						$ids=array();
						if($value['ids']) $ids=explode(',',$value['ids']);
						//判断打开权限
						$open_p=1;
						if($value['friend']==1){
							if(!$space['self']) $open_p=0;
						}elseif($value['friend']==2){
							if(!$space['self'] && !$space['isfriend']) $open_p=0;
						}
						if($open_p && $ids && DB::result_first("select COUNT(*) from ".DB::table('dzz_icos')." where icoid IN (".dimplode($ids).") and type='folder'")) {
							$arr[$value['oid']]['state']='closed';
						}else{
							$arr[$value['oid']]['state']='';
						}
					}
				}
		}
		foreach($arr as $value){
			$data[]=array('attr'=>array('id'=>$value['id'],'rel'=>'folder','icoid'=>$value['icoid']),
					   'data'=>$value['name'],
					   'state'=>$value['state']
					   );

		}
		if($ficos){
			$query=DB::query("select * from ".DB::table('dzz_icos')." where icoid IN (".dimplode($ficos).")");
				while($value=DB::fetch($query)){
					$icosdata[$value['icoid']]=$value;	
					$folderids[]=$value['oid'];
				}
		}
		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;	
				}
		}
		$data[]=array('icosdata'=>$icosdata,'folderdata'=>$folderdata);
		//print_r($data);
	}elseif($prex=='f'){
		$arr=array();
		$icos=array();
		$ficos=array();
		$icosdata=array();
		$folderdata=array();
		$folderids=array();
		if($folder=DB::fetch_first('select * from '.DB::table('dzz_folder')." where fid='{$id}'")){
			$open_p=1;
			if($folder['friend']==1){
				if(!$space['self']) $open_p=0;
			}elseif($folder['friend']==2){
				if(!$space['self'] && !$space['isfriend']) $open_p=0;
			}
			if($open_p){
				$icos=explode(',',$folder['ids']);
			}
		}
		if($icos){
			$query=DB::query("select ico.icoid,ico.oid,ico.name,folder.ids,folder.friend from ".DB::table('dzz_icos')." ico
											LEFT JOIN " .DB::table('dzz_folder')." folder ON ico.oid=folder.fid
											where ico.icoid IN (".dimplode($icos).") and ico.type='folder'");
			while($value=DB::fetch($query)){
				$ficos[]=$value['icoid'];
				
				$arr[$value['oid']]=array('id'=>'f-'.$value['oid'].'-'.$uid,'name'=>$value['name'],'icoid'=>$value['icoid']);
				$ids=array();
				if($value['ids']) $ids=explode(',',$value['ids']);
				//判断打开权限
						$open_p=1;
						if($value['friend']==1){
							if(!$space['self']) $open_p=0;
						}elseif($value['friend']==2){
							if(!$space['self'] && !$space['isfriend']) $open_p=0;
						}
				if($open_p && $ids && DB::result_first("select COUNT(*) from ".DB::table('dzz_icos')." where icoid IN (".dimplode($ids).") and type='folder'")) {
					$arr[$value['oid']]['state']='closed';
				}else{
					$arr[$value['oid']]['state']='';
				}
			}
		}
		
		foreach($arr as $value){
			$data[]=array('attr'=>array('id'=>$value['id'],'rel'=>'folder','icoid'=>$value['icoid']),
					   'data'=>$value['name'],
					   'state'=>$value['state']
					   );

		}	
		if($ficos){
			$query=DB::query("select * from ".DB::table('dzz_icos')." where icoid IN (".dimplode($ficos).")");
				while($value=DB::fetch($query)){
					//判断打开权限
						$open_p=1;
						if($value['friend']==1){
							if(!$space['self']) $open_p=0;
						}elseif($value['friend']==2){
							if(!$space['self'] && !$space['isfriend']) $open_p=0;
						}
						if(!$open_p){
							$value['img']='dzz/images/default_ys/'.$value['type'].'.png';
						}
					$icosdata[$value['icoid']]=$value;
					$folderids[]=$value['oid'];	
				}
		}
		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;	
				}
		}
		$data[]=array('icosdata'=>$icosdata,'folderdata'=>$folderdata);
	}
	echo json_encode_gbk($data);
	exit();
//	$data['zl_user']=array();
	//$data['zl_user']['friend']=array('id=
}elseif($do=='filemanage'){
	$winid=$_GET['winid'];
	$sid=empty($_GET['id'])?0:$_GET['id'];
	$icoid=intval($_GET['icoid']);
	$data=array();
	$data1=array();
	list($prex,$id,$fuid)=explode('-',$sid);
	$folderdata=array();
		$folderids=array();
	if($prex=='f'){
		$container='icosContainer_folder_'.$id;
		$arr=array();
		$icos=array();
		
		if($folder=DB::fetch_first('select * from '.DB::table('dzz_folder')." where fid='{$id}'")){
			$open_p=1;
			if($folder['friend']==1){
				if(!$space['self']) $open_p=0;
			}elseif($folder['friend']==2){
				if(!$space['self'] && !$space['isfriend']) $open_p=0;
			}
			if($open_p){			
				$folder['opened']=1;
				if($folder['ids']){
					$icos=explode(',',$folder['ids']);
				}else{
					$icos=array();
				}
				$folder['ids']=$icos;
				if($icoid) $icos[]=$icoid;
				if($icos){
					
					$query=DB::query("select * from ".DB::table('dzz_icos')." where icoid IN (".dimplode($icos).") ");
					while($value=DB::fetch($query)){
						$value['url']=replace_canshu($value['url']);
						$value['fsize']=formatsize($value['size']);
						$value['ftype']=getFileTypeName($value['type'],$value['ext']);
						$value['fdateline']=dgmdate($value['dateline']);
						$open_p=1;
						if($value['friend']==1){
							if(!$space['self']) $open_p=0;
						}elseif($value['friend']==2){
							if(!$space['self'] && !$space['isfriend']) $open_p=0;
						}
						if(!$open_p){
							$value['img']='dzz/images/default_ys/'.$value['type'].'.png';
						}
						$arr[$value['icoid']]=$value;
						if($value['type']=='folder') $folderids[]=$value['oid'];
					}
					//按桌面目录内顺序排列
					foreach($icos as $value){
						if($value!=$icoid)	$data[$value]=$arr[$value];
						else $data1=$arr[$icoid];
					}
				}
				$folderdata[$folder['fid']]=$folder;
				
				//目录数据
				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;
					}
				}
			}
			$json_folder=json_encode_gbk($folderdata);
			$json_data1=json_encode_gbk($data1);
		}
	}elseif($prex=='d'){
		if($config_u=DB::fetch_first("select * from ".DB::table('dzz_userconfig')." where uid='$fuid'")){
				//获取所有桌面的icoids；
				$screenlist_u=unserialize(stripslashes($config_u['screenlist']));
				$icos=$screenlist_u[$id]['icos'];
		}
		$container='icosContainer_body_'.$id;
		$folderids=array();
		if($icos){
			$query=DB::query("select * from ".DB::table('dzz_icos')." where icoid IN (".dimplode($icos).") ");
			while($value=DB::fetch($query)){
				$value['url']=replace_canshu($value['url']);
				$value['fsize']=formatsize($value['size']);
				$value['ftype']=getFileTypeName($value['type'],$value['ext']);
				$value['fdateline']=dgmdate($value['dateline']);
				$open_p=1;
				if($value['friend']==1){
					if(!$space['self']) $open_p=0;
				}elseif($value['friend']==2){
					if(!$space['self'] && !$space['isfriend']) $open_p=0;
				}
				if(!$open_p){
					$value['img']='dzz/images/default_ys/'.$value['type'].'.png';
				}
				$arr[$value['icoid']]=$value;
				
				if($value['type']=='folder') $folderids[]=$value['oid'];
			}
		}
		//按桌面内顺序排列
		foreach($icos as $icoid){
			$data[$icoid]=$arr[$icoid];
		}
		//目录数据
			$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;
				}
			}
			$json_folder=json_encode_gbk($folderdata);
	}elseif($prex=='type'){
		$container='icosContainer_type_'.$id;
		
	}elseif($prex=='friend'){
		$container='icosContainer_frind_'.$id;
		
	}elseif($prex=='alluser'){
		$container='icosContainer_alluser_'.$id;
	}
	
	$json=json_encode_gbk($data);
	
	include dzztemplate('system/system_filemanage');
}
?>