<?php

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

						///////////////////////////////////////
						
						admin_validate_section("tools",5);
						$action = $GLOBALS["Get"]->val("action");
						
							switch($action){
									
								case '':
									impex_options();
									break;
									
								case 'import':
									import_data();
									break;
									
								case 'do_import':
									do_import_data();
									break;
								
							}//switch
							
						///////////////////////////////////////

function impex_options(){
	
	admin_validate();
	
	$data["section"] = "tools";
	$data["subsection"] = 5;
	$data["title_id"] = "59";	
	$vals = array("doc_root","dbhost","dbname","dbuser","dbpass","script");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	$data["values"] = $values;
	
	$data["scripts"] = array(
		"phpfox" => "phpFoX"
	);
	
	$layout[0] = "impex_options";
	
	admin_output($layout,$data);
	
}//impex_options

function import_data(){
	
	admin_validate();
	
	$data["section"] = "tools";
	$data["subsection"] = 5;
	$data["title_id"] = "59";	
	$vals = array("doc_root","dbhost","dbname","dbuser","dbpass","script");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	foreach($values as $val){
		if(empty($val)){
			$GLOBALS["aerr_id"] = 10;
			impex_options();
			exit;
		}//if
	}//foreach
	
	$conn = mysql_connect($values["dbhost"],$values["dbuser"],$values["dbpass"],1);
	if(!$conn){
		$GLOBALS["aerr_id"] = 39;
		impex_options();
		exit;
	}//if
	
	mysql_select_db($values["dbname"],$conn);
	$sql_query = "SELECT VERSION()";
	$result = mysql_query($sql_query,$conn);
	if(!$result){
		$GLOBALS["aerr_id"] = 39;
		impex_options();
		exit;
	}//if
	
	mysql_close($conn);
	
	if(!file_exists($values["doc_root"]."/file/pic")){
		$GLOBALS["aerr_id"] = 40;
		impex_options();
		exit;
	}//if
	
	$data["link"] = "index.php?section=impex&action=do_import";
	
	reset($values);
	while(list($key,$val) = each($values)){
		if(is_array($val)){
			while(list($k,$v) = each($val)){
				$data["link"].= "&".$key."[".$k."]=".urlencode($v);
			}//while
		}//if
		else
			$data["link"].= "&".$key."=".urlencode($val);
	}//while
	
	$data["javascript"] = "
<script language=\"JavaScript\">
	function HS_PullReport( status ){
		MessageHandler = document.getElementById('status');
		MessageHandler.innerHTML = status;
	}//HS_PullReport
	function HS_StartImport(){
		ImportHandler = document.getElementById('import_processor');
		ImportHandler.src = \"".$data["link"]."\";
	}//HS_StartImport
</script>
";

	$data["onload"] = "HS_StartImport();HS_PullReport('Import in process...');";
	
	$layout[0] = "import_data";
	
	admin_output($layout,$data);
	
}//import_data

function do_import_data(){
	
	set_time_limit(0);
	
	admin_validate();
	
	$vals = array("doc_root","dbhost","dbname","dbuser","dbpass","script");
	$values = $GLOBALS["Get"]->arr($vals,1);
	
	extract($values);
	
	$conn = mysql_connect($dbhost,$dbuser,$dbpass,1);
	if(!$conn){
		pull_report("Incorrect MySQL connect info provided!");
	}//if
	
	mysql_select_db($dbname,$conn);
	$sql_query = "SELECT VERSION()";
	$result = mysql_query($sql_query,$conn);
	if(!$result){
		pull_report("Incorrect MySQL connect info provided!");
	}//if
	
	include(DOC_ROOT."/administrator/impex/".$script.".php");
	
	pull_report("Import Complete!<br>".$report);
	
	mysql_close($conn);
	
}//do_import_members

function pull_report($report){
	
	Header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    Header("Cache-Control: no-cache, must-revalidate"); 
	Header("Pragma: no-cache");
	
	?><script language="JavaScript">
	<!--
		parent.HS_PullReport("<?php echo $report; ?>");
	//-->
	</script><?php
	exit;
	
}//pull_report

function names2geo($country,$state,$city,$zip){
	
	$sql_query = "SELECT con_id FROM geo_countries WHERE name='".mysql_escape_string($country)."'";
	$con_id = $GLOBALS["DB"]->single($sql_query);
	
	$sql_query = "SELECT sta_id FROM geo_states WHERE con_id='".$con_id."'
				  AND name='".mysql_escape_string($state)."'";
	$sta_id = $GLOBALS["DB"]->single($sql_query);
	
	$sql_query = "SELECT cty_id FROM geo_cities WHERE con_id='".$con_id."' AND sta_id='".$sta_id."'
				  AND name='".mysql_escape_string($city)."'";
	$cty_id = $GLOBALS["DB"]->single($sql_query);
	
	return build_geo_position($con_id,$sta_id,$cty_id,$zip);
	
}//names2geo

function name2id($name){
	
	$sql_query = "SELECT mem_id FROM members WHERE username=':0:'";
	$GLOBALS["DB"]->values = array($name);
	$mem_id = $GLOBALS["DB"]->single($sql_query);
	
	return $mem_id;
	
}//name2id

?>