<?php
/**
 * UI for online updating of the config file.
 *
 * 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
 *
 * @package PhpGedView
 * @subpackage Admin
 * @see index/gedcoms.php
 * @version $Id: editgedcoms.php,v 1.47 2004/12/04 11:27:41 eikland Exp $
 */

require "config.php";
require $PGV_BASE_DIRECTORY.$confighelpfile["english"];
global $TEXT_DIRECTION;
if (file_exists($PGV_BASE_DIRECTORY.$confighelpfile[$LANGUAGE])) require $PGV_BASE_DIRECTORY.$confighelpfile[$LANGUAGE];

if (!isset($action)) $action="";
if (!isset($ged)) $ged = "";

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

print_header($pgv_lang["gedcom_adm_head"]);
print "<center>\n";

if ($action=="delete") {
	unset($GEDCOMS[$ged]);
	store_gedcoms();
	delete_gedcom($ged);
	print "<br />".str_replace("#GED#", $ged, $pgv_lang["gedcom_deleted"])."<br />\n";
}

if ($action=="setdefault") {
	$DEFAULT_GEDCOM = $default_ged;
	$configtext = implode('', file($INDEX_DIRECTORY."gedcoms.php"));
	print $pgv_lang["config_file_read"]."<br />\n";
	$configtext = preg_replace('/\$DEFAULT_GEDCOM\s*=\s*".*";/', "\$DEFAULT_GEDCOM = \"".$_POST["default_ged"]."\";", $configtext);
	$fp = fopen($INDEX_DIRECTORY."gedcoms.php", "wb");
	if (!$fp) {
		print "<span class=\"error\">".$pgv_lang["gedcom_config_write_error"]."<br /></span>\n";
	}
	else {
		fwrite($fp, $configtext);
		fclose($fp);
	}
}

print "<br /><br />";
?>
<span class="subheaders"><?php print $pgv_lang["current_gedcoms"]; ?></span><br />
<?php if (!empty($ged)) print "<br />".$pgv_lang["now_import"]."<br /><br />";?>
<form name="defaultform" method="post" action="editgedcoms.php">
<input type="hidden" name="action" value="setdefault" />
<?php
// Default gedcom choice
print "<br />";
if (count($GEDCOMS)>0) {
	if (userIsAdmin($username)) {
		print $pgv_lang["DEFAULT_GEDCOM"]." ";
		print_help_link("default_gedcom_help", "qm");
		print "<select name=\"default_ged\" class=\"header_select\" onchange=\"document.defaultform.submit();\">";
		foreach($GEDCOMS as $gedc=>$gedarray) {
			if (empty($DEFAULT_GEDCOM)) $DEFAULT_GEDCOM = $gedc;
			print "<option value=\"".urlencode($gedc)."\"";
			if ($DEFAULT_GEDCOM==$gedc) print " selected=\"selected\"";
			print " onclick=\"document.defaultform.submit();\">";
			print PrintReady($gedarray["title"])."</option>";
		}
		print "</select><br /><br />";
	}
	print "<table class=\"gedcom_table\">";
	if (userIsAdmin($username)) {
		print "<tr><td class=\"list_label\">";
		print "<a href=\"uploadgedcom.php?action=add_form\">".$pgv_lang["add_gedcom"]."</a>";
		print_help_link("help_addgedcom.php", "qm");
		print "</td>";
	}
	print "<td class=\"list_label\"><a href=\"uploadgedcom.php\">".$pgv_lang["upload_gedcom"]."</a>";
	print_help_link("help_uploadgedcom.php", "qm");
	print "</td>";
	if (userIsAdmin($username)) {
		print "<td class=\"list_label\">";
		print "<a href=\"addnewgedcom.php\">".$pgv_lang["add_new_gedcom"]."</a>";
		print_help_link("help_addnewgedcom.php", "qm");
		print "</td>";
	}
	print  "<td class=\"list_label\"><a href=\"admin.php\">" . $pgv_lang["lang_back_admin"] . "</a></td></tr>";
	print "</table>";
	print "<table class=\"gedcom_table\">";
	foreach($GEDCOMS as $gedc=>$gedarray) {
		if (userGedcomAdmin($username, $gedc)) {
			if (empty($DEFAULT_GEDCOM)) $DEFAULT_GEDCOM = $gedc;
			
			// Heading
			print "<tr><td colspan=\"1\" class=\"list_label\">".$pgv_lang["ged_title"]."</td>";
			print "<td colspan=\"5\" class=\"list_value_wrap\">";
			if ($DEFAULT_GEDCOM==$gedc) print "<span class=\"label\">".PrintReady($gedarray["title"])."</span></td>";
			else print PrintReady($gedarray["title"])."</td>";
			
			// First column
			print "\t<tr><td valign=\"top\">".$pgv_lang["ged_gedcom"]."<br />\n";
			print "\t".$pgv_lang["ged_config"]."<br />\n";
			print "\t".$pgv_lang["ged_privacy"]."<br />\n";
		
			print "</td>";
			
			// Second column
			print "<td>";
			if (file_exists($gedarray["path"])) {
				if ($TEXT_DIRECTION=="ltr") print "$gedc (";
				else print "$gedc &rlm;(";
				printf("%.2fKb", (filesize($gedarray["path"])/1024));
				print ")";
			}
			else print "<span class=\"error\">".$pgv_lang["file_not_found"]."</span>";
			print "<br />\n";
			print "\t &lrm;".$gedarray["config"]."\n<br />";
			print "\t &lrm;".$gedarray["privacy"]."\n";
			check_gedcom_downloadable($gedarray["path"]);
			print "</td>";
			
			// Third column
			print "\t<td valign=\"top\"><a href=\"validategedcom.php?ged=$gedc\">".$pgv_lang["ged_import"]."</a>";
			if (!check_for_import($gedc)) {
				print "<br /><span class=\"error\">".$pgv_lang["gedcom_not_imported"]."</span>";
			}
			print "<br />\n";
			print "\t<a href=\"editconfig_gedcom.php?ged=".urlencode($gedc)."\">".$pgv_lang["edit"]."</a><br />";
			print "\t<a href=\"edit_privacy.php?action=edit&amp;ged=".urlencode($gedc)."\">".$pgv_lang["edit"]."</a></td>";
			
			// Fourth column
			print "\t<td valign=\"top\"><a href=\"editgedcoms.php?action=delete&ged=".urlencode($gedc)."\" onclick=\"return confirm('".$pgv_lang["confirm_gedcom_delete"]." ".preg_replace("/'/", "\'", $gedc)."?');\">".$pgv_lang["delete"]."</a></td>\n";
			
			// Fifth column
			print "\t<td valign=\"top\"><a href=\"downloadgedcom.php?ged=$gedc\">".$pgv_lang["ged_download"]."</a></td>";
			
			// Sixth column
			if (file_exists("slklist.php")) {
				print "\t<td valign=\"top\"><a href=\"slklist.php?ged=$gedc\">".$pgv_lang["make_slklist"]."</a></td>\n";
			}
			print "</tr>";
			print "<tr><td colspan=\"6\"><br /><hr class=\"gedcom_table\" /><br /></td></tr>";
		}
	}
	print "</table>\n";
}
print "</form>";
print "<table class=\"gedcom_table\">";
if (userIsAdmin($username)) {
	print "<tr><td class=\"list_label\">";
	print "<a href=\"uploadgedcom.php?action=add_form\">".$pgv_lang["add_gedcom"]."</a>";
	print_help_link("help_addgedcom.php", "qm");
	print "</td>";
}
print "<td class=\"list_label\"><a href=\"uploadgedcom.php\">".$pgv_lang["upload_gedcom"]."</a>";
print_help_link("help_uploadgedcom.php", "qm");
print "</td>";
if (userIsAdmin($username)) {
	print "<td class=\"list_label\">";
	print "<a href=\"addnewgedcom.php\">".$pgv_lang["add_new_gedcom"]."</a>";
	print_help_link("help_addnewgedcom.php", "qm");
	print "</td>";
}
print  "<td class=\"list_label\"><a href=\"admin.php\">" . $pgv_lang["lang_back_admin"] . "</a></td>";
print "</table>";
print "<br /><br />\n";
print "</center>";

print_footer();

/**
 * Display a warning when gedcom file is downloadable over the internet
 *
 * @author opus27
 * @param string $gedfile gedcom file
 * @return boolean true if file is downloadable, false if not
 */
function check_gedcom_downloadable($gedfile) {
	global $SERVER_URL, $pgv_lang;
	
	$url = $SERVER_URL;
	if (substr($url,-1,1)!="/") $url .= "/";
	$url .= $gedfile;
	$handle = @fopen ($url, "r");
	if (!$handle) return false;
	$txt = fread ($handle, 80);
	fclose($handle);
	if (strpos($txt, " HEAD")==false) return false;
	if (!isset($pgv_lang["gedcom_downloadable"])) $pgv_lang["gedcom_downloadable"] = "Security issue !!! This gedcom file is downloadable over the internet";
	print "<br />\n";
	print "<span class=\"error\">".$pgv_lang["gedcom_downloadable"]." :</span>";
	print "<br /><a href=\"$url\">$url</a>";
	return true;
}
?>
