<?php

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

						///////////////////////////////////////
							
						admin_validate_section("develop",1);
						$action = $GLOBALS["Get"]->val("action");
								
							switch($action){
										
								case '':
									thirdparty_manager();
									break;
									
								case 'edit':
									edit_script();
									break;
									
								case 'save':
									save_script();
									break;
									
								case 'delete':
									delete_script();
									break;
									
							}//switch
							
						///////////////////////////////////////
						
function thirdparty_manager(){
	
	admin_validate();
	
	$data["section"] = "develop";
	$data["subsection"] = 1;
	$data["title_id"] = "1";	
	$data["scripts"] = thirdparty_scripts();
	$data["preset"]  = preset_integration();
	
	$layout[0] = "3rdparty_manager";
	
	admin_output($layout,$data);
	
}//thirdparty_manager

function thirdparty_scripts(){
	
	include(DOC_ROOT."/includes/config/3rdparty.inc.php");
	
	$scripts = array();
	
	while(list($key,$val) = each($thirdparty)){
		$script["name"] = $val["name"];
		$script["edit"] = "index.php?section=3rdparty&action=edit&script=".$key;
		$script["delete"] = "index.php?section=3rdparty&action=delete&script=".$key;
		$scripts[] = $script;
	}//while
	
	return $scripts;
	
}//thirparty_scripts

function preset_integration(){
	
	include(DOC_ROOT."/includes/config/preset_integration.inc.php");
	
	$scripts = array();
	
	while(list($key,$val) = each($thirdparty)){
		$script["name"] = $val["name"];
		$script["install"] = "index.php?section=3rdparty&action=edit&pro=preset&script=".$key;
		$scripts[] = $script;
	}//while
	
	return $scripts;
	
}//preset_integration

function edit_script(){
	
	admin_validate();
	
	$data["section"] = "develop";
	$data["subsection"] = 1;
	$data["title_id"] = "1";	$script = $GLOBALS["Get"]->val("script");
	$data["script"] = $script;
	$pro = $GLOBALS["Get"]->val("pro");
	
	if($pro == "preset"){		
		
		include(DOC_ROOT."/includes/config/preset_integration.inc.php");
		
		$data["values"]["name"] = $thirdparty[$script]["name"];
	
		$events = array("register","login","logout","account","delete","link","init","custom");
	
		foreach($events as $event){
			$code = explode("|",$thirdparty[$script][$event]);
			$data["values"][$event][file] = $code[0];
			$data["values"][$event][func] = $code[1];
		}//foreach
		
		$data["script"] = 0;
		
	}//if
	elseif($script){
		
		include(DOC_ROOT."/includes/config/3rdparty.inc.php");
	
		$data["values"]["name"]     = $thirdparty[$script]["name"];
		$data["values"]["url"]      = $thirdparty[$script]["url"];
		$data["values"]["dbhost"]   = $thirdparty[$script]["dbhost"];
		$data["values"]["dbuser"]   = $thirdparty[$script]["dbuser"];
		$data["values"]["dbname"]   = $thirdparty[$script]["dbname"];
		$data["values"]["dbpass"]   = $thirdparty[$script]["dbpass"];
		$data["values"]["dbprefix"] = $thirdparty[$script]["dbprefix"];
	
		$events = array("register","login","logout","account","delete","link","custom","init");
	
		foreach($events as $event){
			$code = explode("|",$thirdparty[$script][$event]);
			$data["values"][$event]["file"] = $code[0];
			$data["values"][$event]["func"] = $code[1];
		}//foreach
		
	}//if
	else{
		
		$vals = array("name","url","register","login","logout","account","delete","link","custom","init");
		$data["values"] = $GLOBALS["Get"]->arr($vals,1);
		
	}//else
	
	$data["link"] = "index.php?section=3rdparty";
	
	$layout[0] = "edit_script";
	
	admin_output($layout,$data);
	
}//edit_script

function save_script(){
	
	admin_validate();
	
	include(DOC_ROOT."/includes/config/3rdparty.inc.php");	
	
	$vals = array("script","name","url","register","login","logout","account","delete","link","custom","init");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	if(empty($values["name"]) || empty($values["url"])){
		$GLOBALS["aerr_id"] = 10;
		edit_script();
		exit;
	}//if
	
	$values["dbhost"]   = $GLOBALS["Get"]->val("dbhost");
	$values["dbuser"]   = $GLOBALS["Get"]->val("dbuser");
	$values["dbname"]   = $GLOBALS["Get"]->val("dbname");
	$values["dbpass"]   = $GLOBALS["Get"]->val("dbpass");
	$values["dbprefix"] = $GLOBALS["Get"]->val("dbprefix");
	
	if(empty($values["script"]))
		$values["script"] = @max(array_keys($thirdparty)) + 1;
		
	$script["name"]     = $values["name"];
	$script["url"]      = $values["url"];
	$script["dbhost"]   = $values["dbhost"];
	$script["dbuser"]   = $values["dbuser"];
	$script["dbname"]   = $values["dbname"];
	$script["dbpass"]   = $values["dbpass"];
	$script["dbprefix"] = $values["dbprefix"];
	
	$events = array("register","login","logout","account","delete","link","init","custom");
	
	foreach($events as $event){
		if($values[$event]["file"] && $values[$event]["func"])
			$script[$event] = $values[$event]["file"]."|".$values[$event]["func"];
	}//foreach
	
	if(!array_key_exists($values["script"],$thirdparty))
		$init = 1;
	
	$thirdparty[$values["script"]] = $script;
	
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$thirdparty",$thirdparty);
	$write_str.= "\n?>";
	
	$file_handler = fopen(DOC_ROOT."/includes/config/3rdparty.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	if($init)
		load_3rdparty("init",array(),$values["script"]);
	
	load_page("index.php?section=3rdparty&action=edit&script=".$values["script"]);
	
}//save_script

function delete_script(){
	
	admin_validate();
	
	$script = $GLOBALS["Get"]->type_val("script","integer");
	
	include(DOC_ROOT."/includes/config/3rdparty.inc.php");	
	
	unset($thirdparty[$script]);
	
	$write_str = "<?php\n\n";
	$write_str.= go_through_array("\$thirdparty",$thirdparty);
	$write_str.= "\n?>";
	
	$file_handler = fopen(DOC_ROOT."/includes/config/3rdparty.inc.php","w");
	fwrite($file_handler,$write_str);
	fclose($file_handler);
	
	load_page("index.php?section=3rdparty");
	
}//delete_script

?>