<?php

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

						///////////////////////////////////////
							
						admin_validate_section("design",1);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){								
										
								case '':									
									themes_manager();
									break;
									
								case 'create':
									create_theme();
									break;
									
								case 'switch':
									switch_theme();
									break;
									
								case 'delete':
									delete_theme();
									break;
									
								case 'clean':
									clean_cache();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function themes_manager(){
	
	admin_validate();
	
	$data["themes"] = admin_get_themes();
	$data["theme"] = $GLOBALS["THEME"];
	
	$data["section"] = "design";
	$data["subsection"] = 1;
	$data["title_id"] = "49";	
	$layout[0] = "themes_manager";
	
	admin_output($layout,$data);
	
}//themes_manager

function create_theme(){
	
	admin_validate();
	
	set_time_limit(0);
	
	$name = stripslashes(html_entity_decode($GLOBALS["Get"]->val("name")));
	$folder = ereg_replace("[^A-Za-z|0-9|_]","_",strtolower($name));
	$folder = ereg_replace("[_]+","_",$folder);	
	
	$exists = file_exists(DOC_ROOT."/themes/".$folder);
	
	if($exists || !eregi("^[a-z]",$folder)){
		mt_srand((double) microtime() * 100000);
		$folder = "theme_".hs_substr(md5(time().mt_rand(0,1000)),0,5);
	}//if
	
	if(empty($name)){
		load_page("index.php?section=themes&err_id=24");
	}//if
	
	$sql_query = "SELECT * FROM associations WHERE theme='".$GLOBALS["THEME"]."'";
	$res = $GLOBALS["DB"]->execute($sql_query);
	while($get = mysql_fetch_assoc($res)){
		$sql_query = "INSERT INTO associations (assoc_type,page,association,theme)
					  VALUES ('".$get["assoc_type"]."','".$get["page"]."','".$get["association"]."','".$folder."')";
		$GLOBALS["DB"]->execute($sql_query);
	}//while
	
	$sql_query = "SELECT * FROM javascripts WHERE theme='".$GLOBALS["THEME"]."'";
	$res = $GLOBALS["DB"]->execute($sql_query);
	while($get = mysql_fetch_assoc($res)){
		$get["onload"] = ereg_replace("\\\\","\\\\",$get["onload"]);
		$get["code"]   = ereg_replace("\\\\","\\\\",$get["code"]);
		$get["onload"] = ereg_replace("'","\'",$get["onload"]);
		$get["code"]   = ereg_replace("'","\'",$get["code"]);
		$sql_query = "INSERT INTO javascripts (page,onload,code,theme)
					  VALUES ('".$get["page"]."','".$get["onload"]."','".$get["code"]."','".$folder."')";
		$GLOBALS["DB"]->execute($sql_query);
	}//while
	
	$sql_query = "SELECT * FROM tips WHERE theme='".$GLOBALS["THEME"]."'";
	$res = $GLOBALS["DB"]->execute($sql_query);
	while($get = mysql_fetch_assoc($res)){
		$get["message"] = ereg_replace("\\\\","\\\\",$get["message"]);
		$get["message"] = ereg_replace("'","\'",$get["message"]);
		$sql_query = "INSERT INTO tips (page,message,theme)
					  VALUES ('".$get["page"]."','".$get["message"]."','".$folder."')";
		$GLOBALS["DB"]->execute($sql_query);
	}//while
	
	theme_copy_folder(DOC_ROOT."/themes/".$GLOBALS["THEME"],
				      DOC_ROOT."/themes/".$folder);
				      
	handle_folder_clean(DOC_ROOT."/themes/".$folder."/templates/cache");
	handle_folder_clean(DOC_ROOT."/themes/".$folder."/templates/templates_c");
				      
	include(DOC_ROOT."/includes/themes.inc.php");
	$theme["name"]       = $name;
	$theme["folder"]     = $folder;
	$theme["handshake"]  = "gfx/handshake/handshake.gif";
	$theme["encoding"]   = "utf8";
	$theme["type"]       = $themes[$GLOBALS["THEME"]]["type"];
	
	$new_key = $folder;
	
	$themes[$new_key] = $theme;
				      
	$themes_file = DOC_ROOT."/includes/themes.inc.php";
	$write_str = "<?php\n\n";
	$write_str.= "\$THEME = \"".$THEME."\";\n";
	$write_str.= go_through_array("\$themes",$themes);
	$write_str.= "\n?>";
	
	$file_handler = fopen($themes_file,"w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=themes");
	
}//create_theme

function theme_copy_folder($src,$dst){
	
	if(!file_exists($dst))
		mkdir($dst,0777);
	
	$dir = opendir($src);
	while($file = readdir($dir)){
		if($file!="." && $file!=".."){
			if(is_file($src."/".$file)){
				copy($src."/".$file,$dst."/".$file);
				chmod($dst."/".$file,0777);
			}//if
			else
				theme_copy_folder($src."/".$file,$dst."/".$file);
		}//if
	}//while
	closedir($dir);
	
}//theme_copy_folder

function switch_theme(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	
	include(DOC_ROOT."/includes/themes.inc.php");
	
	$THEME = $theme;
	$GLOBALS["THEME"] = $theme;
	
	$themes_file = DOC_ROOT."/includes/themes.inc.php";
	$write_str = "<?php\n\n";
	$write_str.= "\$THEME = \"".$THEME."\";\n";
	$write_str.= go_through_array("\$themes",$themes);
	$write_str.= "\n?>";
	
	$file_handler = fopen($themes_file,"w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=themes");
	
}//switch_theme

function delete_theme(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	
	include(DOC_ROOT."/includes/themes.inc.php");
	
	unset($themes[$theme]);
	
	$themes_file = DOC_ROOT."/includes/themes.inc.php";
	$write_str = "<?php\n\n";
	$write_str.= "\$THEME = \"".$THEME."\";";
	$write_str.= go_through_array("\$themes",$themes);
	$write_str.= "\n?>";
	
	$file_handler = fopen($themes_file,"w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	handle_folder_clean_total(DOC_ROOT."/themes/".$theme);
	
	$sql_query = "DELETE FROM associations WHERE theme='".$theme."'";
	$GLOBALS["DB"]->execute($sql_query);
	
	$sql_query = "DELETE FROM javascripts WHERE theme='".$theme."'";
	$GLOBALS["DB"]->execute($sql_query);
	
	$sql_query = "DELETE FROM tips WHERE theme='".$theme."'";
	$GLOBALS["DB"]->execute($sql_query);
	
	load_page("index.php?section=themes");
	
}//delete_theme

function clean_cache(){
	
	admin_validate();
	
	$theme = $GLOBALS["Get"]->val("theme");
	
	$cache_dir = DOC_ROOT."/themes/".$theme."/templates/templates_c";
	handle_folder_clean($cache_dir);
	
	load_page("index.php?section=themes");
	
}//clean_cache

?>