<?php

						/*************************************\
						
						    Handshakes Professional (3.3)
						            by DZOIC LTD
						       Copyright(c) 2005-2007
						        http://www.dzoic.com
						
						\*************************************/

						///////////////////////////////////////
							
						admin_validate_section("setup",2);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':									
									global_settings();
									break;
									
								case 'save':
									update_settings();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function global_settings(){
	
	admin_validate();
	
	$data["section"] = "setup";
	$data["subsection"] = 2;
	$data["title_id"] = "16";	
	$data["themes"] = admin_get_themes();	
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
		
	$data["theme"] = $theme;
	
	include(DOC_ROOT."/themes/".$theme."/config/settings.inc.php");
	$data["settings"] = $settings;
	
	$data["settings"]["fields_order"] = explode("/",$settings["date_fields_order"]);
	
	if($settings["mailer"] == "sendmail")
		$data["settings"]["sendmail"] = $settings["mailsetup"];
		
	if($settings["mailer"] == "smtp"){
		$setup = explode("|",$settings["mailsetup"]);
		$smtp["host"] = $setup[0];
		$smtp["port"] = $setup[1];
		$smtp["username"] = $setup[2];
		$smtp["password"] = $setup[3];
		$data["settings"]["smtp"] = $smtp;
	}//if
		
	if(!$data["settings"]["sendmail"])
		$data["settings"]["sendmail"] = "/usr/sbin/sendmail";
		
	$data["javascript"] = <<<HTMLTEXT
<script language="JavaScript">
<!--
	function HS_ShowBlock(BlockID){
		var El = document.getElementById(BlockID);
		if(El)
			El.style.display = "block";
	}//HS_ShowBlock
	function HS_HideBlocks(Blocks){
		var IDs = new Array;
		IDs = Blocks.split(",");
		for(var i=0;i<IDs.length;i++){
			var Elem = document.getElementById(IDs[i]);
			Elem.style.display = "none";
		}//for
	}//HS_HideBlocks
//-->
</script>
HTMLTEXT;
	
	$layout[0] = "global_settings";
	
	admin_output($layout,$data);
	
}//global_settings

function update_settings(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
		
	$data["theme"] = $theme;
	
	$values["settings"] = $GLOBALS["Get"]->val("settings");
	$date_fields_order = implode("/",$GLOBALS["Get"]->val("fields_order"));
	$values["settings"]["date_fields_order"] = $date_fields_order;
	
	if($values["settings"]["mailer"] == "sendmail"){
		$values["settings"]["mailsetup"] = $GLOBALS["Get"]->val("sendmail");
	}//if
	elseif($values["settings"]["mailer"] == "smtp"){
		$mailsetup = $GLOBALS["Get"]->val("smtp");
		$values["settings"]["mailsetup"] = implode("|",$mailsetup);
	}//elseif
	
	include(DOC_ROOT."/themes/".$theme."/config/settings.inc.php");
	if($values["settings"]["handshakes"] > 1 && $settings["handshakes"] <= 1){
		$sql_query = "UPDATE network SET `update`='1'";
		$GLOBALS["DB"]->execute($sql_query);
		$folder = DOC_ROOT."/themes/".$theme."/options";
		$dir = opendir($folder);
		while($file = readdir($dir)){
			if(eregi("_add_handshakes",$file)){
				$dst = eregi_replace("_add_handshakes","",$file);;
				copy($folder."/".$file,$folder."/".$dst);
			}//if
		}//while
		closedir($dir);
	}//if
	elseif($values["settings"]["handshakes"] <= 1 && $settings["handshakes"] > 1){
		$folder = DOC_ROOT."/themes/".$theme."/options";
		$dir = opendir($folder);
		while($file = readdir($dir)){
			if(eregi("_less_handshakes",$file)){
				$dst = eregi_replace("_less_handshakes","",$file);;
				copy($folder."/".$file,$folder."/".$dst);
			}//if
		}//while
		closedir($dir);
	}//elseif
	
	$new_settings = "<?php\n\n";	
	$new_settings.= go_through_array("\$settings",$values["settings"]);	
	$new_settings.= "\n?>";
	
	$settings_file = fopen(DOC_ROOT."/themes/".$theme."/config/settings.inc.php","w");
	fwrite($settings_file,$new_settings);
	fclose($settings_file);
	
	load_page("index.php?section=global_settings&theme=".$theme);
	
}//update_settings

?>