<?php

/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
 */
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$refer=dreferer();
dzz_cpheader();

$config=dstripslashes(DB::fetch_first("select * from ".DB::table('dzz_config')." where id='1'"));


if(!submitcheck('settingsubmit')) {
	
	if($operation=='basic'){
		dzz_shownav($lang['menu_global'],$lang['menu_global_siteinfo']);
	}elseif($operation=='address'){
		dzz_shownav($lang['menu_global'],$lang['menu_global_generalAddress']);
	}elseif($operation=='canshu'){	
		dzz_shownav($lang['menu_global'],$lang['menu_global_desktopParameter']);
		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;
			}
		}
	}elseif($operation=='navbar'){
		dzz_shownav($lang['menu_global'],$lang['menu_global_navbar']);
		if($_GET['do']=='edit' || $_GET['do']=='add'){
			$nav=array();
			$navid=intval($_GET['navid']);
			$nav=dstripslashes(DB::fetch_first("select * from ".DB::table("dzz_navbar")." where navid='{$navid}'"));
			if($nav['navicon']) $nav['navicon']=$_G['setting']['attachurl'].$nav['navicon'];
			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;
				}
			}
			
		}elseif($_GET['do']=='delete'){
			$navid=intval($_GET['navid']);
			$nav=DB::fetch_first("select * from ".DB::table("dzz_navbar")." where navid='{$navid}'");
			if($nav['type']=='desktop') $navarr=dstripslashes(unserialize($_G['setting']['dzz_nav']));
		}else{
			$list=array();
			$query=DB::query("select a.*,view.name from ".DB::table('dzz_navbar')." a
							LEFT JOIN ".DB::table("dzz_iconview")." view ON view.id=a.iconview
							order by disp");
			while($value=DB::fetch($query)){
				if($value['navicon']) $value['navicon']=$_G['setting']['attachurl'].$value['navicon'];
				$list[]=$value;
			}
		}
	}elseif($operation=='iconview'){
		dzz_shownav($lang['menu_global'],$lang['menu_global_iconview']);
		if($_GET['do']=='edit' || $_GET['do']=='add'){
			$iconview=array();
			$id=intval($_GET['id']);
			$iconview=dstripslashes(DB::fetch_first("select * from ".DB::table("dzz_iconview")." where id='{$id}'"));
		}else{
			$list=array();
			$query=DB::query("select * from ".DB::table('dzz_iconview')." order by disp");
			while($value=dstripslashes(DB::fetch($query))){
				$list[]=$value;
			}
		}
	}
	include dzztemplate('admin/dzz_setting');
}else{
	
	if($operation=='basic'){
					
					$config['sitename']=getstr($_POST['sitename']);
					$config['keywords']=getstr($_POST['keywords']);
					$config['description']=getstr($_POST['description']);
					$config['tongji']=($_POST['tongji']);
					$config['copyright']=$_POST['copyright'];
					$config['leavealert']=intval($_POST['leavealert']);
				DB::update('dzz_config',daddslashes($config),"id='1'");
				DB::insert('common_setting', array(
				'skey' => 'dzz_sysconfig',
				'svalue' => addslashes(serialize(dstripslashes($config))),
			), false, true);
			updatecache(array('setting', 'dzz_sysconfig'));
			cpmsg($lang['do_success'],$refer,'succeed');		
	}elseif($operation=='address'){
	
					$config['dataurl']=trim($_POST['dataurl']);
					$config['marketurl']=trim($_POST['marketurl']);
					$config['widgeturl']=trim($_POST['widgeturl']);
					$config['addappurl']=trim($_POST['addappurl']);
					$config['systhameurl']=trim($_POST['systhameurl']);
					$config['sysbrowserurl']=trim($_POST['sysbrowserurl']);
					$config['loginurl']=trim($_POST['loginurl']);
					$config['logouturl']=trim($_POST['logouturl']);
				DB::update('dzz_config',daddslashes($config),"id='1'");
				DB::insert('common_setting', array(
				'skey' => 'dzz_sysconfig',
				'svalue' => addslashes(serialize(dstripslashes($config))),
			), false, true);
			updatecache(array('setting', 'dzz_sysconfig'));
			cpmsg($lang['do_success'],$refer,'succeed');		
	}elseif($operation=='canshu'){
	
					$config['userscreennum']=intval($_POST['userscreennum']);;
					$config['marginleft']=intval($_POST['marginleft']);
					$config['marginright']=intval($_POST['marginright']);
					$config['marginbottom']=intval($_POST['marginbottom']);
					$config['margintop']=intval($_POST['margintop']);
					$config['dockshow']=intval($_POST['dockshow']);
					$config['topbarshow']=intval($_POST['topbarshow']);
					$config['iconposition']=intval($_POST['iconposition']);
					$config['iconview']=intval($_POST['iconview']);
					$config['autolist']=intval($_POST['autolist']);
			DB::update('dzz_config',daddslashes($config),"id='1'");
			DB::insert('common_setting', array(
				'skey' => 'dzz_sysconfig',
				'svalue' => addslashes(serialize(dstripslashes($config))),
			), false, true);
			updatecache(array('setting', 'dzz_sysconfig'));
			cpmsg($lang['do_success'],$refer,'succeed');	
	}elseif($operation=='navbar'){	
		if($_POST['do']=='add'||$_POST['do']=='edit' ) {
			$navid=intval($_POST['navid']);
			if(!$_POST['navname']) cpmsg($lang['navname_empty'],$refer,'error');
			if($navid) $nav=dstripslashes(DB::fetch_first("select * from ".DB::table('dzz_navbar')." where navid='{$navid}'"));
			$setarr=array();
			if($_FILES['iconnew']) {
				//print_r($_FILES['iconnew']);
				//cpmsg('dfdsfd');
				if($_FILES['iconnew']['tmp_name']){
					if($image=uploadtolocal($_FILES['iconnew'],'appimg',$nav['navicon'])){
						$img=$image;
					}
				}
			} else {
			
				if($_POST['iconnew'] && $nav['navicon']!=$_POST['iconnew']){
					if($image=imagetolocal($_POST['iconnew'],'appimg',$nav['navicon'])){
						$img=$image;
					}
				}
			}
			
			$setarr['navname']=getstr($_POST['navname']);
			$setarr['allow']=intval($_POST['allow']);
			$setarr['backimg']=trim($_POST['backimg']);
			$setarr['dockshow']=intval($_POST['dockshow']);
			$setarr['topbarshow']=intval($_POST['topbarshow']);
			$setarr['type']=$_POST['type'];
			if($setarr['type']=='desktop'){
				$setarr['iconposition']=intval($_POST['iconposition']);
				$setarr['iconview']=intval($_POST['iconview']);
				$setarr['marginleft']=intval($_POST['marginleft']);
				$setarr['marginright']=intval($_POST['marginright']);
				$setarr['marginbottom']=intval($_POST['marginbottom']);
				$setarr['margintop']=intval($_POST['margintop']);
				$setarr['autolist']=intval($_POST['autolist']);
			}else{
				$setarr['navurl']=$_POST['navurl'];
				$setarr['target']=trim($_POST['target']);
			}
			
			if($img) {
				$setarr['navicon']=$img;
			}else{
				if($nav['avaliable']==3) $setarr['avaliable']=1;
				elseif($nav['avaliable']==2) $setarr['avaliable']=1;
			}
			if($navid) {
				DB::update('dzz_navbar',daddslashes($setarr),"navid='{$navid}'");
			}else{
				DB::insert('dzz_navbar',daddslashes($setarr));
			}
			
			$query=DB::query("select * from ".DB::table('dzz_navbar')." where `avaliable`>0 ORDER BY disp");
			$nav=array();
			while($value=DB::fetch($query)){
				$nav[$value['navid']]=$value;
			}
			DB::insert('common_setting', array(
					'skey' => 'dzz_nav',
					'svalue' => addslashes(serialize(dstripslashes($nav))),
				), false, true);
			updatecache(array('setting', 'dzz_nav'));
			cpmsg($lang['do_success'], 'action=setting&operation=navbar', 'succeed');
		}elseif($_POST['do']=='delete'){
				$navid=intval($_POST['navid']);
				$nav=DB::fetch_first("select * from ".DB::table('dzz_navbar')." where navid='{$navid}'");
				if(!$nav) cpmsg($lang['navbar_delete_not_exist'],$refer,'error');
				//删除图片
				
				//删除导航桌面的数据
				desktop_delete_userdata('sys_'.$navid,0);
				@unlink($_G['setting']['attachdir'].$nav['navicon']);
				DB::delete('dzz_navbar',"navid='{$navid}'");
				$query=DB::query("select * from ".DB::table('dzz_navbar')." where `avaliable`>0 ORDER BY disp");
				$nav=array();
				while($value=DB::fetch($query)){
					$nav[$value['navid']]=$value;
				}
				DB::insert('common_setting', array(
						'skey' => 'dzz_nav',
						'svalue' => addslashes(serialize(dstripslashes($nav))),
					), false, true);
				updatecache(array('setting', 'dzz_nav'));
				cpmsg($lang['do_success'], 'action=setting&operation=navbar', 'succeed');
		}else{
			$isdefault=array();
			$isdefault[intval($_POST['isdefault'])]=1;
			
			$ids=$_POST['navid'];
			foreach($ids as $navid){
				DB::update('dzz_navbar',array('disp'=>intval($_POST['disp'][$navid]),'avaliable'=>intval($_POST['avaliable_1'][$navid])+intval($_POST['avaliable_2'][$navid]),'isdefault'=>intval($isdefault[$navid])),"navid='{$navid}'");
				
			}
			$query=DB::query("select * from ".DB::table('dzz_navbar')." where `avaliable`>0 ORDER BY disp");
			$nav=array();
			while($value=DB::fetch($query)){
				$nav[$value['navid']]=$value;
			}
			DB::insert('common_setting', array(
					'skey' => 'dzz_nav',
					'svalue' => addslashes(serialize(dstripslashes($nav))),
				), false, true);
			updatecache(array('setting', 'dzz_nav'));
			cpmsg($lang['do_success'], $refer, 'succeed');
		}
		
	}elseif($operation=='iconview'){	
		if($_POST['do']=='add'||$_POST['do']=='edit' ) {
			$id=intval($_POST['id']);
			if(!$_POST['name']) cpmsg($lang['iconview_name_empty'],$refer,'error');
			
				$setarr=array();
				$setarr['width']=intval($_POST['width']);
				$setarr['height']=intval($_POST['height']);
				$setarr['divwidth']=intval($_POST['divwidth']);
				$setarr['divheight']=intval($_POST['divheight']);
				$setarr['paddingleft']=intval($_POST['paddingleft']);
				$setarr['paddingtop']=intval($_POST['paddingleft']);
				$setarr['textlength']=intval($_POST['textlength']);
				$setarr['name']=getstr($_POST['name']);
				$setarr['align']=$_POST['align'];
				$setarr['avaliable']=$_POST['avaliable'];
			if($id) {
				DB::update('dzz_iconview',daddslashes($setarr),"id='{$id}'");
			}else{
				DB::insert('dzz_iconview',daddslashes($setarr));
			}
			$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;
			}
			DB::insert('common_setting', array(
					'skey' => 'dzz_iconview',
					'svalue' => addslashes(serialize(dstripslashes($iconview))),
				), false, true);
			updatecache(array('setting', 'dzz_iconview'));
			cpmsg($lang['do_success'], 'action=setting&operation=iconview', 'succeed');
		}else{
			
			$delete=$_POST['delete'];
			if($delete){
				DB::delete('dzz_iconview',"navid IN(".dimplode($delete).")");
			}
			$ids=$_POST['id'];
			foreach($ids as $id){
				if(!in_array($id,$delete))	DB::update('dzz_iconview',array('disp'=>intval($_POST['disp'][$id]),'avaliable'=>intval($_POST['avaliable'][$id])),"id='{$id}'");
			}
			$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;
			}
			DB::insert('common_setting', array(
					'skey' => 'dzz_iconview',
					'svalue' => addslashes(serialize(dstripslashes($iconview))),
				), false, true);
			updatecache(array('setting', 'dzz_iconview'));
			cpmsg($lang['do_success'], $refer, 'succeed');
		}
		
	}
}
?>