<?php

						/*************************************\

						    Handshakes Professional (3.5)
						            by DZOIC LTD
						       Copyright(c) 2005-2008
						        http://www.dzoic.com

						\*************************************/

						///////////////////////////////////////

						admin_validate_section("setup",4);
						$action = $GLOBALS["Get"]->val("action");

							switch($action){

								case '':
									modules_settings();
									break;

								case 'update_module':
									update_module();
									break;

								case 'categories':
									module_categories();
									break;

								case 'video_categories':
									video_categories();
									break;

								case 'save_categories':
									module_save_categories();
									break;

								case 'save_video_categories':
									save_video_categories();
									break;

								case 'chat_room':
									chat_room();
									break;

								case 'chat_save_room':
									chat_save_room();
									break;

								case 'chat_main_room':
									chat_main_room();
									break;

								case 'chat_delete_room':
									chat_delete_room();
									break;

								case 'music_delete_format':
									music_delete_format();
									break;

								case 'music_add_format':
									music_add_format();
									break;

								case 'videos_delete_format':
									videos_delete_format();
									break;

								case 'videos_add_format':
									videos_add_format();
									break;

								case 'forum_edit':
									forum_edit();
									break;

								case 'forum_save':
									forum_save();
									break;

								case 'forum_delete':
									forum_delete();
									break;

								case 'forum_activate':
									forum_activate();
									break;

								case 'forum_up':
									forum_move("up");
									break;

								case 'forum_down':
									forum_move("down");
									break;

								case 'htaccess':
									htaccess();
									break;

								case 'sb_add':
									sb_add();
									break;

								case 'sb_enable':
									sb_enable();
									break;

								case 'sb_delete':
									sb_delete();
									break;

							}//switch

						///////////////////////////////////////

function modules_settings(){

	include(DOC_ROOT."/includes/config/modules.inc.php");

	admin_validate();

	$data["section"] = "setup";
	$data["subsection"] = 4;
	$data["title_id"] = "38";
	$data["themes"] = admin_get_themes();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];

	$data["theme"] = $theme;

	while(list($key,) = each($modules)){
		if(file_exists(DOC_ROOT."/themes/".$theme."/modules/".$key.".inc.php")){
			include(DOC_ROOT."/themes/".$theme."/modules/".$key.".inc.php");
			$data[$key] = ${$key};
		}//if
		$data[$key]["templates"] = templates_list($key);
	}//while
	$data["forum"]["forums"] = array();
	$sql_query = "SELECT * FROM forums WHERE (clb_id=0 OR clb_id IS NULL) AND frm_id > 100000 ORDER BY position";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$data["forum"]["forums"][] = $res;
	}//foreach

	$data["social_bookmarking"]["engines"] = array();
	$sql_query = "SELECT * FROM social_bookmarking_engines";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$data["social_bookmarking"]["engines"][] = $res;
	}//foreach

	$layout[0] = "modules_settings";

	admin_output($layout,$data);

}//modules_settings

function update_module(){

	admin_validate();

	set_time_limit(0);

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$module = $GLOBALS["Get"]->val("module");

	switch($module){
		case 'blogs':
			$vals = array("url","path","featured_limit","new","updated","mod_rewrite","template","colorize","audio","video","comment_audio","comment_video");
			$module_info["id"] = "blg_id";
			$module_info["table"] = "blogs";
			break;
		case 'classifieds':
			$vals = array("url","path","featured_limit","new","updated","expire","expire_featured","delete","mod_rewrite","template","colorize");
			$module_info["id"] = "cls_id";
			$module_info["table"] = "classifieds";
			break;
		case 'events':
			$vals = array("url","path","featured_limit","new","updated","delete","mod_rewrite","template","colorize");
			$module_info["id"] = "evn_id";
			$module_info["table"] = "events";
			break;
		case 'clubs':
			$vals = array("url","path","featured_limit","new","mod_rewrite","template","colorize");
			$module_info["id"] = "clb_id";
			$module_info["table"] = "clubs";
			break;
		case 'ecards':
			$vals = array("url","path","expire","mod_rewrite","template","colorize");
			$module_info["id"] = "ecrd_id";
			$module_info["table"] = "ecards_office";
			break;
		case 'profiles':
			$vals = array("url","path","new","updated","mod_rewrite","template","colorize","audio","video");
			$module_info["id"] = "mem_id";
			$module_info["table"] = "profiles";
			break;
		case 'hotornot':
			$vals = array("top_count","top_votes");
			break;
		case 'music':
			$vals = array("maxsize","approve","show_unapproved","comment_audio","comment_video");
			break;
		case 'videos':
			$vals = array("maxsize","approve","show_unapproved","screenshot_width","screenshot_height","videos2flv","comment_audio","comment_video");
			break;
		case 'forum':
			$vals = array("topics_per_page","posts_per_page","topics_expire");
			break;
		case 'recording':
			$vals = array("audio_diskspace","audio_limit","audio_bitrate","video_diskspace","video_limit","video_bitrate","video_interval","video_quality","video_width","video_height","video_fps","video_bandwidth","enable_audio_recording","enable_video_recording","show_unapproved_recordings","track_recordings","enable_mailbox_audio","enable_mailbox_video","flash_url");
			break;
		case 'vchat':
			$vals = array("enabled","key","limit","server_url","mic_rate","key_frame_interval","picture_quality","stream_bandwidth","movie_width","movie_height","movie_fps");
			break;
		case 'social_bookmarking':
			$vals = array("enabled");
			break;
		case 'tags':
			$vals = array("enabled","minimum_amount","maximum_amount","minimum_length","cache_lifetime","minimum_font","maximum_font","cache_depth");
			break;
	}//switch

	$values = $GLOBALS["Get"]->arr($vals,1);
	include(DOC_ROOT."/themes/".$theme."/modules/".$module.".inc.php");

	if(${$module}["mod_rewrite"] != $values["mod_rewrite"] || ${$module}["colorize"] != $values["colorize"]){

	if($module != "ecards")
		$sql_query = "SELECT ".$module_info["id"]." as id,folder,template FROM ".$module_info["table"];
	else
		$sql_query = "SELECT ".$module_info["id"]." as id,".$module_info["id"]." as folder,template FROM ".$module_info["table"];

	$result = $GLOBALS["DB"]->execute($sql_query);

	while($get = mysql_fetch_assoc($result)){


		if($values["mod_rewrite"] && !$values["colorize"]){
			$master = (int)($get["id"]/1000);
			$sub    = ereg_replace("^".$master,"",$get["id"]);
			@handle_folder_clean_total(${$module}["path"]."/".$get["folder"]);
			@handle_folder_clean_total(${$module}["path"]."/".$master."/".$sub);
		}//if
		elseif($values["mod_rewrite"] && $values["colorize"]){
			$master = (int)($get["id"]/1000);
			$sub    = ereg_replace("^".$master,"",$get["id"]);
			if(!file_exists(${$module}["path"]."/".$master))
				mkdir(${$module}["path"]."/".$master,PERMISSIONS);
			if(file_exists(${$module}["path"]."/".$get["folder"]))
				rename(${$module}["path"]."/".$get["folder"],${$module}["path"]."/".$master."/".$sub);
			else
				mkdir(${$module}["path"]."/".$master."/".$sub,PERMISSIONS);
			if(!file_exists(${$module}["path"]."/".$master."/".$sub."/settings.xml")){

				if($get["template"])
					$template = $get["template"];
				elseif(!${$module}["template"])
					$template = "default";
				else
					$template = ${$module}["template"];

				copy(${$module}["path"]."/default/index.php",${$module}["path"]."/".$master."/".$sub."/index.php");
				chmod(${$module}["path"]."/".$master."/".$sub."/index.php",PERMISSIONS);

				$folder = DOC_ROOT."/templates/".$module."/".$template;
				$dir = opendir($folder);
				while($file = readdir($dir)){
					if($file!="." && $file!=".." && is_file($folder."/".$file)){
						copy($folder."/".$file,${$module}["path"]."/".$master."/".$sub."/".$file);
					}//if
				}//while
				closedir($dir);
				$sql_query = "UPDATE `".$module_info["table"]."` SET template='".$template."'
							  WHERE `".$module_info["id"]."`=".$get["id"];
				$GLOBALS["DB"]->execute($sql_query);
			}//if
		}//elseif
		elseif(!$values["mod_rewrite"] && !$values["colorize"]){
			$master = (int)($get["id"]/1000);
			$sub    = ereg_replace("^".$master,"",$get["id"]);
			if(file_exists(${$module}["path"]."/".$master."/".$sub))
				rename(${$module}["path"]."/".$master."/".$sub,${$module}["path"]."/".$get["folder"]);
			elseif(!file_exists(${$module}["path"]."/".$get["folder"]))
				mkdir(${$module}["path"]."/".$get["folder"],PERMISSIONS);

			if(file_exists(${$module}["path"]."/".$get["folder"]."/settings.xml")){
				handle_folder_clean(${$module}["path"]."/".$get["folder"]);
			}//if
			copy(${$module}["path"]."/default/index.php",${$module}["path"]."/".$get["folder"]."/index.php");
			chmod(${$module}["path"]."/".$get["folder"]."/index.php",PERMISSIONS);

		}//elseif
		elseif(!$values["mod_rewrite"] && $values["colorize"]){
			$master = (int)($get["id"]/1000);
			$sub    = ereg_replace("^".$master,"",$get["id"]);
			if(file_exists(${$module}["path"]."/".$master."/".$sub))
				rename(${$module}["path"]."/".$master."/".$sub,${$module}["path"]."/".$get["folder"]);
			elseif(!file_exists(${$module}["path"]."/".$get["folder"]))
				mkdir(${$module}["path"]."/".$get["folder"],PERMISSIONS);

			if(!file_exists(${$module}["path"]."/".$get["folder"]."/settings.xml")){
				copy(${$module}["path"]."/default/index.php",${$module}["path"]."/".$get["folder"]."/index.php");
				chmod(${$module}["path"]."/".$get["folder"]."/index.php",PERMISSIONS);

				if($get["template"])
					$template = $get["template"];
				elseif(!${$module}["template"])
					$template = "default";
				else
					$template = ${$module}["template"];

				$folder = DOC_ROOT."/templates/".$module."/".$template;
				$dir = opendir($folder);
				while($file = readdir($dir)){
					if($file!="." && $file!=".." && is_file($folder."/".$file)){
						copy($folder."/".$file,${$module}["path"]."/".$get["folder"]."/".$file);
					}//if
				}//while
				closedir($dir);

				$sql_query = "UPDATE `".$module_info["table"]."` SET template='".$template."'
							  WHERE `".$module_info["id"]."`=".$get["id"];
				$GLOBALS["DB"]->execute($sql_query);
			}//if
		}//elseif


	}//while

	}//if

	if($values["template"] && ${$module}["template"] != $values["template"]){
		$template = $values["template"];
		$sql_query = "SELECT ".$module_info["id"]." as id,folder,template FROM ".$module_info["table"];
		$GLOBALS["DB"]->clean();
		$result = $GLOBALS["DB"]->execute($sql_query);
		while($get = mysql_fetch_assoc($result)){
			if($values["colorize"]){
			$master = (int)($get["id"]/1000);
			$sub    = ereg_replace("^".$master,"",$get["id"]);
			if($values["mod_rewrite"])
				$path = ${$module}["path"]."/".$master."/".$sub;
			else
				$path = ${$module}["path"]."/".$get["folder"];

			$folder = DOC_ROOT."/templates/".$module."/".$template;
			$dir = opendir($folder);
			while($file = readdir($dir)){
				if($file!="." && $file!=".." && is_file($folder."/".$file)){
					copy($folder."/".$file,$path."/".$file);
				}//if
			}//while
			closedir($dir);
			}//if

			$sql_query = "UPDATE `".$module_info["table"]."` SET template='".$template."'
						  WHERE `".$module_info["id"]."`=".$get["id"];
			$GLOBALS["DB"]->execute($sql_query);
		}//while
	}//if

	while(list($key,$val) = each($values)){
		${$module}[$key] = $val;
	}//while

	module_save_settings($theme,$module,${$module});

	load_page("index.php?section=modules&theme=".$theme."#".$module);

}//update_module

function module_categories(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$data["title_id"] = "38";
	$module = $GLOBALS["Get"]->val("module");
	$data["module"] = $module;
	$data["modules"] = admin_get_modules();
	$data["link"] = "index.php?section=modules&theme=".$data["theme"]."#".$module;

	$data["section"] = "setup";
	$data["subsection"] = 4;

	switch($module){
		case 'classifieds':
			include(DOC_ROOT."/themes/".$theme."/system/cls_categories.inc.php");
			break;
		case 'events':
			include(DOC_ROOT."/themes/".$theme."/system/evn_categories.inc.php");
			break;
		case 'clubs':
			include(DOC_ROOT."/themes/".$theme."/system/clb_categories.inc.php");
			break;
		case 'ecards':
			include(DOC_ROOT."/themes/".$theme."/system/ecards_categories.inc.php");
			break;
	}//switch

	$jscode = "";
	while(list($key,$val) = each($categories)){
		$jscode.= "HS_Categories[".$key."] = \"".hs_ereg_replace('"','\"',$val)."\";\n";
	}//while
	while(list($key,$val) = each($subcategories)){
		$jscode.= "HS_SubCategories[".$key."] = new Array;\n";
		while(list($k,$v) = each($val)){
			$jscode.= "HS_SubCategories[".$key."][".$k."] = \"".hs_ereg_replace('"','\"',$v)."\";\n";
		}//while
	}//while

	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	var HS_Categories = new Array;
	var HS_SubCategories = new Array;
	var Category = 0;
	var SubCategory = 0;
	function HS_InitCategories(){
		$jscode
	}//HS_InitCategories
	function HS_PopulateSubCategoriesSimple(){
		var FormHandler = document.classifieds_search;
		Category = FormHandler.category.options[FormHandler.category.selectedIndex].value;
		for(var i=FormHandler.subcategory.options.length-1; i>=0; i--){
			FormHandler.subcategory.options[i] = null;
		}//for
		FormHandler.subcategory.options[0] = new Option("Please, select...","0");
		var k=1;
		for(var Item in HS_SubCategories[Category]){
			FormHandler.subcategory.options[k] = new Option(HS_SubCategories[Category][Item],Item);
			k++;
		}//for
	}//HS_PopulateSubCategoriesSimple
	function HS_PopulateSubCategories(){
		var FormHandler = document.cls_categories;
		Category = FormHandler.categories.options[FormHandler.categories.selectedIndex].value;
		for(var i=FormHandler.subcategories.options.length-1; i>=0; i--){
			FormHandler.subcategories.options[i] = null;
		}//for
		var k=0;
		for(var Item in HS_SubCategories[Category]){
			if(HS_SubCategories[Category][Item] != "EMPTY"){
				FormHandler.subcategories.options[k] = new Option(HS_SubCategories[Category][Item],Item);
				k++;
			}//if
		}//for
		FormHandler.edit_category.value = HS_Categories[Category];
		FormHandler.edit_subcategory.value = "";
		HS_SubCategory = 0;
	}//HS_PopulateSubCategories
	function HS_EditSubCategory(){
		if(Category){
			var FormHandler = document.cls_categories;
			SubCategory = FormHandler.subcategories.options[FormHandler.subcategories.selectedIndex].value;
			FormHandler.edit_subcategory.value = HS_SubCategories[Category][SubCategory];
		}//if
	}//HS_EditSubCategory
	function HS_AddCategory(){
		var FormHandler = document.cls_categories;
		var CategoryName = FormHandler.new_category.value;
		var CatID = HS_Categories.length+1;
		HS_Categories[CatID] = CategoryName;
		HS_SubCategories[CatID] = new Array;
		FormHandler.categories.options[FormHandler.categories.options.length] = new Option(CategoryName,CatID);
		FormHandler.new_category.value = "";
	}//HS_AddCategory
	function HS_AddSubCategory(){
		if(Category){
			var FormHandler = document.cls_categories;
			var SubCategoryName = FormHandler.new_subcategory.value;
			var SubCatID = HS_SubCategories[Category].length+1;
			HS_SubCategories[Category][SubCatID] = SubCategoryName;
			FormHandler.subcategories.options[FormHandler.subcategories.options.length] = new Option(SubCategoryName,SubCatID);
			FormHandler.new_subcategory.value = "";
		}//if
	}//HS_AddSubCategory
	function HS_UpdateCategory(){
		if(Category){
			var FormHandler = document.cls_categories;
			var CategoryName = FormHandler.edit_category.value;
			HS_Categories[Category] = CategoryName;
			var Selected = FormHandler.categories.selectedIndex;
			FormHandler.categories.options[Selected] = new Option(CategoryName,Category);
			FormHandler.categories.selectedIndex = Selected;
		}//if
	}//HS_UpdateCategory
	function HS_UpdateSubCategory(){
		if(Category && SubCategory){
			var FormHandler = document.cls_categories;
			var SubCategoryName = FormHandler.edit_subcategory.value;
			HS_SubCategories[Category][SubCategory] = SubCategoryName;
			var Selected = FormHandler.subcategories.selectedIndex;
			FormHandler.subcategories.options[Selected] = new Option(SubCategoryName,SubCategory);
			FormHandler.subcategories.selectedIndex = Selected;
		}//if
	}//HS_UpdateSubCategory
	function HS_DeleteCategory(){
		if(Category){
			var FormHandler = document.cls_categories;
			HS_Categories[Category] = "EMPTY";
			FormHandler.categories.options[FormHandler.categories.selectedIndex] = null;
			FormHandler.edit_category.value = "";
			FormHandler.edit_subcategory.value = "";
			Category = 0;
			SubCategory = 0;
			for(var i=FormHandler.subcategories.options.length; i>=0; i--){
				FormHandler.subcategories.options[i] = null;
			}//for
		}//if
	}//HS_DeleteCategory
	function HS_DeleteSubCategory(){
		if(Category && SubCategory){
			var FormHandler = document.cls_categories;
			HS_SubCategories[Category][SubCategory] = "EMPTY";
			FormHandler.subcategories.options[FormHandler.subcategories.selectedIndex] = null;
			FormHandler.edit_subcategory.value = "";
			SubCategory = 0;
		}//if
	}//HS_DeleteSubCategory
	function HS_UpdateData(){
		var CatsLine = "";
		var SubCatsLine = "";
		var TotalCats = 0;
		var Rules = new Array();
		var Error = 0;
		var FormHandler = document.cls_categories;
		for(var Item in HS_Categories){
			if(HS_Categories[Item] != "EMPTY"){
				CatsLine+= Item+"|"+HS_Categories[Item]+"==|==";
				Rules[Item] = 0;
				TotalCats++;
				for(var SubItem in HS_SubCategories[Item]){
					SubCatsLine+= Item+"|"+SubItem+"|"+HS_SubCategories[Item][SubItem]+"==|==";
					if(HS_SubCategories[Item][SubItem] != "EMPTY")
						Rules[Item] = 1;
				}//for
			}//if
			else{
				for(var SubItem in HS_SubCategories[Item]){
					SubCatsLine+= Item+"|"+SubItem+"|EMPTY==|==";
				}//for
			}//else
		}//for

		for(var Rule in Rules){
			if(Rules[Rule] == 0){
				Error = 1;
			}//if
		}//for

		if(TotalCats == 0)
			Error = 2;

		if(Error == 1){
			alert("Please, make sure to add at least one subcategory to each category.");
		}//if
		else if(Error == 2){
			alert("Please, make sure to add at least one category.");
		}//else if
		else{
			FormHandler.res_categories.value = CatsLine;
			FormHandler.res_subcategories.value = SubCatsLine;
			FormHandler.submit();
		}//else
	}//HS_UpdateData
//-->
</script>
JAVASCRIPTCODE;

	$data["onload"] = "HS_InitCategories();";

	$data["categories"] = $categories;
	$data["subcategories"] = $subcategories;

	$layout[0] = "module_categories";

	admin_output($layout,$data);

}//module_categories

function video_categories(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$data["title_id"] = "38";
	$data["link"] = "index.php?section=modules&theme=".$data["theme"]."#videos";

	$data["section"] = "setup";
	$data["subsection"] = 4;

	include(DOC_ROOT."/themes/".$theme."/system/video_categories.inc.php");

	$data["categories"] = $categories;

	$jscode = "";
	while(list($key,$val) = each($categories)){
		$jscode.= "HS_Categories[".$key."] = \"".hs_ereg_replace('"','\"',$val)."\";\n";
	}//while

	$data["javascript"] = <<<JAVASCRIPTCODE
<script language="JavaScript">
<!--
	var HS_Categories = new Array;
	var Category = 0;
	function HS_InitCategories(){
		$jscode
	}//HS_InitCategories
	function HS_EditCategory(){
		var FormHandler = document.cls_categories;
		Category = FormHandler.categories.options[FormHandler.categories.selectedIndex].value;
		FormHandler.edit_category.value = HS_Categories[Category];
	}//HS_Edit Category
	function HS_AddCategory(){
		var FormHandler = document.cls_categories;
		var CategoryName = FormHandler.new_category.value;
		var CatID = HS_Categories.length+1;
		HS_Categories[CatID] = CategoryName;
		FormHandler.categories.options[FormHandler.categories.options.length] = new Option(CategoryName,CatID);
		FormHandler.new_category.value = "";
	}//HS_AddCategory
	function HS_UpdateCategory(){
		if(Category){
			var FormHandler = document.cls_categories;
			var CategoryName = FormHandler.edit_category.value;
			HS_Categories[Category] = CategoryName;
			var Selected = FormHandler.categories.selectedIndex;
			FormHandler.categories.options[Selected] = new Option(CategoryName,Category);
			FormHandler.categories.selectedIndex = Selected;
		}//if
	}//HS_UpdateCategory
	function HS_DeleteCategory(){
		if(Category){
			var FormHandler = document.cls_categories;
			HS_Categories[Category] = "EMPTY";
			FormHandler.categories.options[FormHandler.categories.selectedIndex] = null;
			FormHandler.edit_category.value = "";
			FormHandler.edit_subcategory.value = "";
			Category = 0;
		}//if
	}//HS_DeleteCategory
	function HS_UpdateData(){
		var CatsLine = "";
		var TotalCats = 0;
		var FormHandler = document.cls_categories;
		for(var Item in HS_Categories){
			if(HS_Categories[Item] != "EMPTY"){
				CatsLine+= Item+"|"+HS_Categories[Item]+"==|==";
				TotalCats++;
			}//if
		}//for

		if(TotalCats == 0)
			Error = 2;

		if(Error == 2){
			alert("Please, make sure to add at least one category.");
		}//else if
		else{
			FormHandler.res_categories.value = CatsLine;
			FormHandler.submit();
		}//else
	}//HS_UpdateData
//-->
</script>
JAVASCRIPTCODE;

	$data["onload"] = "HS_InitCategories();";

	$layout[0] = "video_categories";

	admin_output($layout,$data);

}//video_categories

function module_save_categories(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$module = $GLOBALS["Get"]->val("module");

	switch($module){
		case 'classifieds':
			$file = DOC_ROOT."/themes/".$theme."/system/cls_categories.inc.php";
			include(DOC_ROOT."/themes/".$theme."/modules/classifieds.inc.php");
			$update_prices = 1;
			break;
		case 'events':
			$file = DOC_ROOT."/themes/".$theme."/system/evn_categories.inc.php";
			include(DOC_ROOT."/themes/".$theme."/modules/events.inc.php");
			$update_prices = 1;
			break;
		case 'clubs':
			$file = DOC_ROOT."/themes/".$theme."/system/clb_categories.inc.php";
			include(DOC_ROOT."/themes/".$theme."/modules/clubs.inc.php");
			$update_prices = 1;
			break;
		case 'ecards':
			$file = DOC_ROOT."/themes/".$theme."/system/ecards_categories.inc.php";
			break;
	}//switch

	$cats = stripslashes(html_entity_decode($GLOBALS["Get"]->val("res_categories")));
	$subcats = stripslashes(html_entity_decode($GLOBALS["Get"]->val("res_subcategories")));

	$cats = hs_ereg_replace(preg_quote("&#124;"),"|",$cats);
	$subcats = hs_ereg_replace(preg_quote("&#124;"),"|",$subcats);

	$cats_exp = handle_empties(explode("==|==",$cats));
	$subcats_exp = handle_empties(explode("==|==",$subcats));

	$e_cats = array();
	$e_subcats = array();

	foreach($cats_exp as $cat){
		$its = explode("|",$cat);
		if($its[1] != "EMPTY")
			$e_cats[$its[0]] = $its[1];
	}//foreach

	foreach($subcats_exp as $subcat){
		$its = explode("|",$subcat);
		if($its[2] == "EMPTY" && $update_prices){
			while(list($key,$val) = each(${$module}[featured])){
				unset(${$module}[featured][$key][$its[0]."_".$its[1]]);
			}//while
		}//if
		elseif($its[2] != "EMPTY")
			$e_subcats[$its[0]][$its[1]] = $its[2];
	}//foreach

	$str = "<?php\n\n";
	while(list($key,$val) = each($e_cats)){
		$str.= "\$categories[".$key."] = \"".hs_ereg_replace('"','\"',$val)."\";\n";
		if(is_array($e_subcats[$key])){
			while(list($k,$v) = each($e_subcats[$key])){
				$str.= "\$subcategories[".$key."][".$k."] = \"".hs_ereg_replace('"','\"',$v)."\";\n";
			}//while
		}//if
	}//while
	$str.= "\n?>";

	$file_handler = fopen($file,"w");
	fwrite($file_handler,$str);
	fclose($file_handler);

	if($update_prices){
		module_save_settings($theme,$module,${$module});
	}//if

	load_page("index.php?section=modules&action=categories&module=".$module."&theme=".$theme);

}//module_save_categories

function save_video_categories(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$file = DOC_ROOT."/themes/".$theme."/system/video_categories.inc.php";

	$cats = stripslashes(html_entity_decode($GLOBALS["Get"]->val("res_categories")));
	$cats = hs_ereg_replace(preg_quote("&#124;"),"|",$cats);
	$cats_exp = handle_empties(explode("==|==",$cats));

	$e_cats = array();
	$e_subcats = array();

	foreach($cats_exp as $cat){
		$its = explode("|",$cat);
		if($its[1] != "EMPTY")
			$e_cats[$its[0]] = $its[1];
	}//foreach

	$str = "<?php\n\n";
	while(list($key,$val) = each($e_cats)){
		$str.= "\$categories[".$key."] = \"".hs_ereg_replace('"','\"',$val)."\";\n";
	}//while
	$str.= "\n?>";

	$file_handler = fopen($file,"w");
	fwrite($file_handler,$str);
	fclose($file_handler);

	load_page("index.php?section=modules&action=video_categories&theme=".$theme);

}//save_video_categories

function chat_room(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$room_id = $GLOBALS["Get"]->type_val("room_id","integer");

	$data["title_id"] = "38";
	$data["section"] = "setup";
	$data["subsection"] = 4;
	$data["link"] = "index.php?section=modules&theme=".$data["theme"]."#chat";

	if($room_id){
		include(DOC_ROOT."/themes/".$theme."/modules/chat.inc.php");
		$data["values"] = $chat["rooms"][$room_id];
		if($chat["main"] == $room_id)
			$data["values"]["main"] = 1;
		$data["room_id"] = $room_id;
	}//if

	$layout[0] = "chat_room";

	admin_output($layout,$data);

}//chat_room

function chat_save_room(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$room_id = $GLOBALS["Get"]->type_val("room_id","integer");
	$name = $GLOBALS["Get"]->val("name");
	$main = $GLOBALS["Get"]->type_val("main","integer");

	if(empty($name)){
		$GLOBALS["aerr_id"] = 10;
		chat_room();
		break;
	}//if

	include(DOC_ROOT."/themes/".$theme."/modules/chat.inc.php");

	if(empty($room_id))
		$room_id = @max(array_keys($chat["rooms"])) + 1;

	$chat["rooms"][$room_id][name] = $name;

	if($main)
		$chat["main"] = $room_id;

	module_save_settings($theme,"chat",$chat);

	load_page("index.php?section=modules&theme=".$data["theme"]."#chat");

}//chat_save_room

function chat_main_room(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$room_id = $GLOBALS["Get"]->type_val("room_id","integer");

	include(DOC_ROOT."/themes/".$theme."/modules/chat.inc.php");

	if($room_id)
	$chat["main"] = $room_id;

	module_save_settings($theme,"chat",$chat);

	load_page("index.php?section=modules&theme=".$data["theme"]."#chat");

}//chat_main_room

function chat_delete_room(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$room_id = $GLOBALS["Get"]->type_val("room_id","integer");

	include(DOC_ROOT."/themes/".$theme."/modules/chat.inc.php");

	if($room_id){
		unset($chat["rooms"][$room_id]);
	}//if

	module_save_settings($theme,"chat",$chat);

	load_page("index.php?section=modules&theme=".$data["theme"]."#chat");

}//chat_delete_room

function music_delete_format(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$format_id = $GLOBALS["Get"]->type_val("format_id","integer");

	include(DOC_ROOT."/themes/".$theme."/modules/music.inc.php");

	if($format_id){
		unset($music["formats"][$format_id]);
	}//if

	module_save_settings($theme,"music",$music);

	load_page("index.php?section=modules&theme=".$data["theme"]."#music");

}//music_delete_format

function music_add_format(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$ext  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ext")));
	$mime = stripslashes(html_entity_decode($GLOBALS["Get"]->val("mime")));

	include(DOC_ROOT."/themes/".$theme."/modules/music.inc.php");

	$format_id = @max(array_keys($music["formats"]))+1;

	$music["formats"][$format_id]["ext"]  = $ext;
	$music["formats"][$format_id]["mime"] = $mime;

	module_save_settings($theme,"music",$music);

	load_page("index.php?section=modules&theme=".$data["theme"]."#music");

}//music_add_format

function videos_delete_format(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$format_id = $GLOBALS["Get"]->type_val("format_id","integer");

	include(DOC_ROOT."/themes/".$theme."/modules/videos.inc.php");

	if($format_id){
		unset($videos["formats"][$format_id]);
	}//if

	module_save_settings($theme,"videos",$videos);

	load_page("index.php?section=modules&theme=".$data["theme"]."#videos");

}//videos_delete_format

function videos_add_format(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$ext  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("ext")));
	$mime = stripslashes(html_entity_decode($GLOBALS["Get"]->val("mime")));

	include(DOC_ROOT."/themes/".$theme."/modules/videos.inc.php");

	$format_id = @max(array_keys($videos["formats"]))+1;

	$videos["formats"][$format_id]["ext"]  = $ext;
	$videos["formats"][$format_id]["mime"] = $mime;

	module_save_settings($theme,"videos",$videos);

	load_page("index.php?section=modules&theme=".$data["theme"]."#videos");

}//videos_add_format

function forum_edit(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$data["frm_id"] = $GLOBALS["Get"]->type_val("frm_id","integer");

	if($data["frm_id"]){
		$sql_query = "SELECT * FROM forums WHERE frm_id=".$data["frm_id"];
		$data["forum"] = $GLOBALS["DB"]->row($sql_query);
	}//if

	if(!$data["forum"]["topics_expire"])
		$data["forum"]["topics_expire"] = 0;

	$data["title_id"] = "38";
	$data["section"] = "setup";
	$data["subsection"] = 4;
	$data["link"] = "index.php?section=modules&theme=".$data["theme"]."#forum";

	$layout[0] = "edit_forum";

	admin_output($layout,$data);

}//forum_edit

function forum_save(){

	admin_validate();

	$vals = array("frm_id","theme","title","description","topics_expire");
	$values = $GLOBALS["Get"]->arr($vals,1);

	if(!$values["title"] || !$values["description"]){
		$GLOBALS["aerr_id"] = 10;
		forum_edit();
		exit;
	}//if

	if(!$values["topics_expire"])
		$values["topics_expire"] = 0;

	if($values["frm_id"]){
		$sql_query = "UPDATE forums SET title=':title:',description=':description:',topics_expire=:topics_expire:
					  WHERE frm_id=:frm_id:";
		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
		load_page("index.php?section=modules&theme=".$values["theme"]."&action=forum_edit&frm_id=".$values["frm_id"]);
	}//if
	else{
		$sql_query = "SELECT MAX(position) FROM forums";
		$values["position"] = $GLOBALS["DB"]->single($sql_query) + 1;
		$sql_query = "INSERT INTO forums (title,description,topics_expire,created,active,position,clb_id)
					  VALUES (':title:',':description:',:topics_expire:,:time:,'1',:position:,0)";
		$values["time"] = time();
		$GLOBALS["DB"]->values = $values;
		$GLOBALS["DB"]->execute($sql_query);
		load_page("index.php?section=modules&theme=".$data["theme"]."#forum");
	}//else

}//forum_save

function forum_delete(){

	admin_validate();
	$theme = $GLOBALS["Get"]->val("theme");
	$frm_id = $GLOBALS["Get"]->type_val("frm_id","integer");

	include_once(DOC_ROOT."/functions/delete_functions.php");

	delete_forum($frm_id);

	load_page("index.php?section=modules&theme=".$theme."#forum");

}//forum_delete

function forum_activate(){

	admin_validate();
	$theme = $GLOBALS["Get"]->val("theme");
	$frm_id = $GLOBALS["Get"]->type_val("frm_id","integer");

	$sql_query = "SELECT active FROM forums WHERE frm_id=".$frm_id;
	$status = $GLOBALS["DB"]->single($sql_query);
	$update_status = abs($status-1);

	$sql_query = "UPDATE forums SET active='".$update_status."' WHERE frm_id=".$frm_id;
	$GLOBALS["DB"]->execute($sql_query);

	load_page("index.php?section=modules&theme=".$theme."#forum");

}//forum_activate

function forum_move($direction){

	admin_validate();
	$theme = $GLOBALS["Get"]->val("theme");
	$frm_id = $GLOBALS["Get"]->type_val("frm_id","integer");

	$sql_query = "SELECT frm_id,position FROM forums WHERE clb_id=0 ORDER BY position ASC";
	$result = $GLOBALS["DB"]->result($sql_query);
	foreach($result as $res){
		$forums[] = $res["frm_id"];
		$positions[] = $res["position"];
	}//foreach

	$ind = array_search($frm_id,$forums);
	$old_position = $positions[$ind];
	if($direction == "up")
		$newind = $ind-1;
	elseif($direction == "down")
		$newind = $ind+1;

	$move_forum = $forums[$newind];
	$new_position = $positions[$newind];

	$sql_query = "UPDATE forums SET position=".$old_position." WHERE frm_id=".$move_forum;
	$GLOBALS["DB"]->execute($sql_query);
	$sql_query = "UPDATE forums SET position=".$new_position." WHERE frm_id=".$frm_id;
	$GLOBALS["DB"]->execute($sql_query);

	load_page("index.php?section=modules&theme=".$theme."#forum");

}//forum_move

function templates_list($type){

	$templates = array();

	$folder = DOC_ROOT."/templates/".$type;
	if(!file_exists($folder))
		return;

	$dir = opendir($folder);
	while($subfolder = readdir($dir)){
		if($subfolder!="." && $subfolder!=".." && file_exists($folder."/".$subfolder."/settings.xml")){
			$template_data = extract_template_data($folder."/".$subfolder."/settings.xml");
			$name = $template_data["name"];
			$path = $template_data["path"];
			$templates[$path] = $name;
		}//if
	}//while
	closedir($dir);

	return $templates;

}//templates_list

function htaccess(){

$content = "RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+) index.php?folder=$1&%{QUERY_STRING} [L]";

$fsize = strlen($content);
$ftime = date("D, d M Y H:i:s T");
header("HTTP/1.1 200 OK");
header("Content-Disposition: attachement; filename=.htaccess");
header("Last-Modified: $ftime");
header("Accept-Ranges: bytes");
header("Content-Length: ".$fsize);
header("Content-Range: bytes -".($fsize -1)."/".$fsize);
header("Content-type: application/octet-stream");
print $content;
exit;


}//htaccess

function sb_add(){

	admin_validate();

	$theme = $GLOBALS["Get"]->val("theme");
	if(empty($theme))
		$theme = $GLOBALS["THEME"];
	$data["theme"] = $theme;

	$name  = stripslashes(html_entity_decode($GLOBALS["Get"]->val("name")));
	$url   = stripslashes(html_entity_decode($GLOBALS["Get"]->val("url")));
	$icon  = $GLOBALS["GetFromForm"]->file("icon");
	if(!empty($icon["tmp"])){

		$ext = substr( $icon["name"], strrpos( $icon["name"], "." ) );

		mt_srand((double) microtime() * 100000);
		$icon_file = hs_substr(md5(time().mt_rand(0,100)),0,10).$ext;

		move_uploaded_file( $icon["tmp"], DOC_ROOT."/includes/sbicons/".$icon_file );

	} else {
		$icon_file = "";
	}

	$sql_query = "INSERT INTO social_bookmarking_engines (name, url, icon, enabled)
				  VALUES (':name:',':url:',':icon:','1')";

	$vals["name"] = $name;
	$vals["url"]  = $url;
	$vals["icon"] = $icon_file;

	$GLOBALS["DB"]->values = $vals;
	$GLOBALS["DB"]->execute($sql_query);

	load_page("index.php?section=modules&theme=".$theme."#social_bookmarking");

}//sb_add

function sb_enable(){

	admin_validate();
	$theme = $GLOBALS["Get"]->val("theme");
	$eng_id = $GLOBALS["Get"]->type_val("eng_id","integer");

	$sql_query = "SELECT enabled FROM social_bookmarking_engines WHERE eng_id=".$eng_id;
	$status = $GLOBALS["DB"]->single($sql_query);
	$update_status = abs($status-1);

	$sql_query = "UPDATE social_bookmarking_engines SET enabled='".$update_status."' WHERE eng_id=".$eng_id;
	$GLOBALS["DB"]->execute($sql_query);

	load_page("index.php?section=modules&theme=".$theme."#social_bookmarking");

}//sb_enable

function sb_delete(){

	admin_validate();
	$theme = $GLOBALS["Get"]->val("theme");
	$eng_id = $GLOBALS["Get"]->type_val("eng_id","integer");

	$sql_query = "SELECT icon FROM social_bookmarking_engines WHERE eng_id=".$eng_id;
	$icon = $GLOBALS["DB"]->single($sql_query);

	if($icon && file_exists( DOC_ROOT."/includes/sbicons/".$icon )) {
		@unlink(DOC_ROOT."/includes/sbicons/".$icon);
	}

	$sql_query = "DELETE FROM social_bookmarking_engines WHERE eng_id=".$eng_id;
	$GLOBALS["DB"]->execute($sql_query);

	load_page("index.php?section=modules&theme=".$theme."#social_bookmarking");

}//sb_delete

?>