<?php
/*=================================================
	Project: phpGedView
	File: userexport.php
	Author:
		Boudewijn Sjouke	sjouke@users.sourceforge.net
	Comments:
		Exports users and their data to either SQL queries (Index mode) or authenticate.php and xxxxxx.dat files (MySQL mode).

	Change Log:
		08/04/2004 - File Created

    phpGedView: Genealogy Viewer
    Copyright (C) 2002 to 2003  John Finlay and Others

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

===================================================*/
require "config.php";
require $PGV_BASE_DIRECTORY.$confighelpfile["english"];
if (file_exists($PGV_BASE_DIRECTORY.$confighelpfile[$LANGUAGE])) require $PGV_BASE_DIRECTORY.$confighelpfile[$LANGUAGE];

//-- make sure that they have admin status before they can use this page
//-- otherwise have them login again
if (!userIsAdmin(getUserName())) {
	header("Location: login.php?url=usermigrate.php");
	exit;
}

print_header($pgv_lang["um_header"]);

if (!isset($proceed)) $proceed = "No";

if ($proceed !== "Yes") {
	if ($PGV_DATABASE == "db") {
		print "<br /><br />";
		print $pgv_lang["um_sql_index1"]." ".$INDEX_DIRECTORY."<br />".$pgv_lang["um_sql_index2"];
	}
	else {
		print "<br /><br />";
		print $pgv_lang["um_index_sql1"]." ".$INDEX_DIRECTORY."<br />".$pgv_lang["um_index_sql2"];
	}
	?>
		<br /><br />
		<form action="usermigrate.php" method="post">
			<input type="hidden" name="proceed" value="Yes" />
			<?php print $pgv_lang["um_proceed"]?> <br />
			<input type="submit" class="button" value="<?php print $pgv_lang["yes"];?>" /> <input type="button" class="button" value="<?php print $pgv_lang["no"];?>" onclick="window.location='admin.php';" /><br /><br />
		</form>
	<?php
	print_footer();
	exit;
}

if ($PGV_DATABASE == "db") {

	// Get user array and create authenticate.php
	print $pgv_lang["um_creating"]." \"authenticate.php\"<br /><br />";
	$authtext = "<?php\n\n\$users = array();\n\n";
	$users = GetUsers();
	foreach($users as $key=>$user) {
		$user["fullname"] = addslashes($user["fullname"]);
		$authtext .= "\$user = array();\n";
		foreach($user as $ukey=>$value) {
			if (!is_array($value)) {
				$value = preg_replace('/"/', '\\"', $value);
				$authtext .= "\$user[\"$ukey\"] = '$value';\n";
			}
			else {
				$authtext .= "\$user[\"$ukey\"] = array();\n";
				foreach($value as $subkey=>$subvalue) {
					$subvalue = preg_replace('/"/', '\\"', $subvalue);
					$authtext .= "\$user[\"$ukey\"][\"$subkey\"] = '$subvalue';\n";
				}
			}
		}
		$authtext .= "\$users[\"$key\"] = \$user;\n\n";
	}
	$authtext .= "?>\n";
	if (file_exists($INDEX_DIRECTORY."authenticate.php")) {
		print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."authenticate.php<br /><br />";
	}
	else {
		$fp = fopen($INDEX_DIRECTORY."authenticate.php", "w");
		if ($fp) {
			fwrite($fp, $authtext);
			fclose($fp);
			print $pgv_lang["um_file_create_succ1"]." authenticate.php<br /><br />";
		}
		else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."authenticate.php. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
	}

	// Get messages and create messages.dat 
	print $pgv_lang["um_creating"]." \"messages.dat\"<br /><br />";
	$messages = array();
	$mesid = 1;
	$sql = "SELECT * FROM ".$TBLPREFIX."messages ORDER BY m_id DESC";
	$res =& dbquery($sql);
	while($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)){
		$row = db_cleanup($row);
		$message = array();
		$message["id"] = $mesid;
		$mesid = $mesid + 1;
		$message["to"] = $row["m_to"];
		$message["from"] = $row["m_from"];
		$message["subject"] = stripslashes($row["m_subject"]);
		$message["body"] = stripslashes($row["m_body"]);
		$message["created"] = $row["m_created"];
		$messages[] = $message;
	}
	if ($mesid > 0) {
		$mstring = serialize($messages);

		if (file_exists($INDEX_DIRECTORY."messages.dat")) {
			print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."messages.dat<br /><br />";
		}
		else {
			$fp = fopen($INDEX_DIRECTORY."messages.dat", "wb");
			if ($fp) {
				fwrite($fp, $mstring);
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." messages.dat<br /><br />";
			}
		else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."messages.dat. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
	}
	else print $pgv_lang["um_nomsg"]." ".$pgv_lang["um_file_not_created"]."<br /><br />";

	// Get favorites and create favorites.dat 
	print $pgv_lang["um_creating"]." \"favorites.dat\"<br /><br />";
	$favorites = array();
	$sql = "SELECT * FROM ".$TBLPREFIX."favorites";
	$res =& dbquery($sql);
	$favid = 1;
	while($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)){
		$row = db_cleanup($row);
		$favorite = array();
		$favorite["id"] = $favid;
		$favid = $favid + 1;
		$favorite["username"] = $row["fv_username"];
		$favorite["gid"] = $row["fv_gid"];
		$favorite["type"] = $row["fv_type"];
		$favorite["file"] = $row["fv_file"];
		$favorites[] = $favorite;
	}
	if ($favid > 0) {
		$mstring = serialize($favorites);
		if (file_exists($INDEX_DIRECTORY."favorites.dat")) {
			print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."favorites.dat<br /><br />";
			}
		else {
			$fp = fopen($INDEX_DIRECTORY."favorites.dat", "wb");
			if ($fp) {
				fwrite($fp, $mstring);
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." favorites.dat<br /><br />";
			}
			else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."favorites.dat. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
	}
	else print $pgv_lang["um_nofav"]." ".$pgv_lang["um_file_not_created"]."<br /><br />";

	// Get news and create news.dat 
	print $pgv_lang["um_creating"]." \"news.dat\"<br /><br />";
	$allnews = array();
	$sql = "SELECT * FROM ".$TBLPREFIX."news ORDER BY n_date DESC";
	$res =& dbquery($sql);
	while($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)){
		$row = db_cleanup($row);
		$news = array();
		$news["id"] = $row["n_id"];
		$news["username"] = $row["n_username"];
		$news["date"] = $row["n_date"];
		$news["title"] = stripslashes($row["n_title"]);
		$news["text"] = stripslashes($row["n_text"]);
		$allnews[$row["n_id"]] = $news;
	}
	if (count($news) > 0) {
		$mstring = serialize($allnews);
		if (file_exists($INDEX_DIRECTORY."news.dat")) {
			print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."news.dat<br /><br />";
			}
		else {
			$fp = fopen($INDEX_DIRECTORY."news.dat", "wb");
			if ($fp) {
				fwrite($fp, $mstring);
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." news.dat<br /><br />";
			}
			else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."news.dat. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
	}
	else print $pgv_lang["um_nonews"]." ".$pgv_lang["um_file_not_created"]."<br /><br />";

	// Get blocks and create blocks.dat 
	print $pgv_lang["um_creating"]." \"blocks.dat\"<br /><br />";
	$allblocks = array();
	$blocks["main"] = array();
	$blocks["right"] = array();
	$sql = "SELECT * FROM ".$TBLPREFIX."blocks ORDER BY b_location, b_order";
	$res =& dbquery($sql);
	while($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)){
		$row = db_cleanup($row);
		$blocks = array();
		$blocks["username"] = $row["b_username"];
		$blocks["location"] = $row["b_location"];
		$blocks["order"] = $row["b_order"];
		$blocks["name"] = $row["b_name"];
		$allblocks[] = $blocks;
	}
	if (count($allblocks) > 0) {
		$mstring = serialize($allblocks);
		if (file_exists($INDEX_DIRECTORY."blocks.dat")) {
			print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."blocks.dat<br /><br />";
			}
		else {
			$fp = fopen($INDEX_DIRECTORY."blocks.dat", "wb");
			if ($fp) {
				fwrite($fp, $mstring);
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." blocks.dat<br /><br />";
			}
			else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."blocks.dat. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
	}
	else print $pgv_lang["UM_NOBLOCK"]." ".$pgv_lang["um_file_not_created"]."<br /><br />";
}
else {

	// Get user array and create authenticate.sql
	print $pgv_lang["um_creating"]." \"authenticate.sql\"<br /><br />";
	if (file_exists($INDEX_DIRECTORY."authenticate.sql")) {
		print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."authenticate.sql<br /><br />";
	}
	else {
		function db_prep($item) {
			if (is_array($item)) {
				foreach($item as $key=>$value) {
					$item[$key]=db_prep($value);
				}
				return $item;
			}
			else {
				return addslashes($item);
				//-- use the following commented line to convert between character sets
				//return addslashes(iconv("iso-8859-1", "UTF-8", $item));
			}
		}
		$fp = fopen($INDEX_DIRECTORY."authenticate.sql", "w");
		if ($fp) {
			foreach($users as $username=>$user) {
				$newuser = db_prep($user);
				$newuser["fullname"] = preg_replace("/\//", "", $newuser["fullname"]);
				$sql = "INSERT INTO ".$TBLPREFIX."users VALUES('".$newuser["username"]."','".$newuser["password"]."','".addslashes($newuser["fullname"])."','".serialize($newuser["gedcomid"])."','".serialize($newuser["rootid"])."'";
				if ($newuser["canadmin"]) $sql .= ",'Y'";
				else $sql .= ",'N'";
				$sql .= ",'".serialize($newuser["canedit"])."'";
				$sql .= ",'".$newuser["email"]."'";
				$sql .= ",'".$newuser["verified"]."'";
				$sql .= ",'".$newuser["verified_by_admin"]."'";
				$sql .= ",'".$newuser["language"]."'";
				$sql .= ",'".$newuser["pwrequested"]."'";
				$sql .= ",'".$newuser["reg_timestamp"]."'";
				$sql .= ",'".$newuser["reg_hashcode"]."'";
				$sql .= ",'".$newuser["theme"]."'";
				$sql .= ",'".$newuser["loggedin"]."'";
				$sql .= ",'".$newuser["sessiontime"]."'";
				$sql .= ",'".$newuser["contactmethod"]."'";
				$sql .= ",'".$newuser["visibleonline"]."'";
				$sql .= ",'".$newuser["editaccount"]."'";
				$sql .= ");\r\n";
				fwrite($fp, $sql);
			}
			fclose($fp);
			print $pgv_lang["um_file_create_succ1"]." authenticate.sql<br /><br />";
		}
		else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."authenticate.sql. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
	}
	
	// Get messages and create message.sql
	print $pgv_lang["um_creating"]." \"messages.sql\"<br /><br />";
		if (file_exists($INDEX_DIRECTORY."messages.sql")) {
		print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."messages.sql<br /><br />";
	}
	else {
		$messages = array();
		if (file_exists($INDEX_DIRECTORY."messages.dat")) {
			$fp = fopen($INDEX_DIRECTORY."messages.dat", "rb");
			$mstring = fread($fp, filesize($INDEX_DIRECTORY."messages.dat"));
			fclose($fp);
			$messages = unserialize($mstring);
			$fp = fopen($INDEX_DIRECTORY."messages.sql", "w");
			if ($fp) {
				foreach($messages as $m => $message) {
					$sql = "INSERT INTO ".$TBLPREFIX."messages VALUES (NULL, '".addslashes($message["from"])."','".addslashes($message["to"])."','".addslashes($message["subject"])."','".addslashes($message["body"])."','".addslashes($message["created"])."');\r\n";
					fwrite($fp, $sql);
				}
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." messages.sql<br /><br />";

			}
			else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."messages.sql. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
		else $pgv_lang["um_nomsg"]." ".$pgv_lang["um_query_not_created"]."<br /><br />";
	}
	
	// Get favorites and create favorites.sql
	print $pgv_lang["um_creating"]." \"favorites.sql\"<br /><br />";
	if (file_exists($INDEX_DIRECTORY."favorites.sql")) {
		print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."favorites.sql<br /><br />";
	}
	else {
		$favorites = array();
		if (file_exists($INDEX_DIRECTORY."favorites.dat")) {
			$fp = fopen($INDEX_DIRECTORY."favorites.dat", "rb");
			$mstring = fread($fp, filesize($INDEX_DIRECTORY."favorites.dat"));
			fclose($fp);
			$favorites = unserialize($mstring);
			$fp = fopen($INDEX_DIRECTORY."favorites.sql", "w");
			if ($fp) {
				foreach($favorites as $m => $favorite) {
					$sql = "INSERT INTO ".$TBLPREFIX."favorites VALUES (NULL, '".addslashes($favorite["username"])."','".addslashes($favorite["gid"])."','".addslashes($favorite["type"])."','".addslashes($favorite["file"])."');\r\n";
					fwrite($fp, $sql);
				}
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." favorites.sql<br /><br />";
			}
			else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."favorites.sql. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
		else $pgv_lang["um_nofav"]." ".$pgv_lang["um_query_not_created"]."<br /><br />";
	}

	// Get news and create news.sql
	print $pgv_lang["um_creating"]." \"news.sql\"<br /><br />";
	if (file_exists($INDEX_DIRECTORY."news.sql")) {
		print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."news.sql<br /><br />";
	}
	else {
		$allnews = array();
		if (file_exists($INDEX_DIRECTORY."news.dat")) {
			$fp = fopen($INDEX_DIRECTORY."news.dat", "rb");
			$mstring = fread($fp, filesize($INDEX_DIRECTORY."news.dat"));
			fclose($fp);
			$allnews = unserialize($mstring);
			$fp = fopen($INDEX_DIRECTORY."news.sql", "w");
			if ($fp) {
				foreach($allnews as $m => $news) {
					$sql = "INSERT INTO ".$TBLPREFIX."news VALUES (NULL, '".addslashes($news["username"])."','".addslashes($news["date"])."','".addslashes($news["title"])."','".addslashes($news["text"])."');\r\n";
					fwrite($fp, $sql);
				}
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." news.sql<br /><br />";
			}
			else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."news.sql. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
		else $pgv_lang["um_nonews"]." ".$pgv_lang["um_query_not_created"]."<br /><br />";
	}

	// Get blocks and create blocks.sql
	print $pgv_lang["um_creating"]." \"blocks.sql\"<br /><br />";
	if (file_exists($INDEX_DIRECTORY."blocks.sql")) {
		print $pgv_lang["um_file_create_fail1"]." ".$INDEX_DIRECTORY."blocks.sql<br /><br />";
	}
	else {
		$allblocks = array();
		if (file_exists($INDEX_DIRECTORY."blocks.dat")) {
			$fp = fopen($INDEX_DIRECTORY."blocks.dat", "rb");
			$mstring = fread($fp, filesize($INDEX_DIRECTORY."blocks.dat"));
			fclose($fp);
			$allblocks = unserialize($mstring);
			$fp = fopen($INDEX_DIRECTORY."blocks.sql", "w");
			if ($fp) {
				foreach($allblocks as $m => $blocks) {
					$sql = "INSERT INTO ".$TBLPREFIX."blocks VALUES (NULL, '".addslashes($blocks["username"])."','".addslashes($blocks["location"])."','".addslashes($blocks["order"])."','".addslashes($blocks["name"])."');\r\n";
						fwrite($fp, $sql);
				}
				fclose($fp);
				print $pgv_lang["um_file_create_succ1"]." blocks.sql<br /><br />";
			}
			else print $pgv_lang["um_file_create_fail2"]." ".$INDEX_DIRECTORY."blocks.sql. ".$pgv_lang["um_file_create_fail3"]."<br /><br />";
		}
		else $pgv_lang["um_noblocks"]." ".$pgv_lang["um_query_not_created"]."<br /><br />";
	}
}
print  "<a href=\"admin.php\">" . $pgv_lang["lang_back_admin"] . "</a><br /><br />";

print_footer();
?>