<?php

/* 
 *	W-AGORA 4.0
 *	-----------
 *	$Id: upgrade_site_401.php,v 1.3 2004/10/05 17:11:47 mdruilhe Exp $
 *	Usage:		Upgrade a site (must be called from upgrade.php)
 *	Author:		Marc Druilhe <mdruilhe@w-agora.com>
 */

	if (!defined('_GLOBALS')) {
		die('Hacking attempt');
	}

# Site configuration
# ------------------

	if (!is_file ("$cfg_dir/site_${site}.$ext") ) {
		print "Cannot open file: $cfg_dir/site_${site}.$ext, make sure that site $site really exists";
		exit;
	}

	include "$cfg_dir/site_${site}.$ext";

	print "<ul>";

	// update messages
	echo "<li> copy $tmpl_dir/admin/msgs.php $bn_dir/msgs.$ext <br>";
	copy ("$tmpl_dir/admin/msgs.php", "$bn_dir/msgs.$ext");

	// update site fields
	echo "<li> copy $tmpl_dir/admin/site_fields.php $bn_dir/site_fields.$ext <br>";
	copy ("$tmpl_dir/admin/site_fields.php", "$bn_dir/site_fields.$ext");

# Update tables in database
# --------------------------
	include "$inc_dir/$bn_access.$ext";
	$db_class = "${bn_access}_access";
	$db = new $db_class;
	$db->openDB($dbhost, $dbport, $dbuser, $dbpassword, $dbname, $site);

	updateSite($site);

# rename backup configuration file
# --------------------------------
	if (file_exists ("$cfg_dir/site_${site}.$ext.bak") ) {
		echo "<li> remame backup file  <B>$cfg_dir/site_${site}.$ext.bak</B>";
		copy ("$cfg_dir/site_${site}.$ext.bak", "$cfg_dir/site_${site}.$ext.bak.$ext");
		unlink ("$cfg_dir/site_${site}.$ext.bak");
	}

# Update default configuration files for this site
# ------------------------------------------------

	// default configuration file for new forums
	echo "<li> copy $tmpl_dir/default/agora/conf.php $bn_dir_default/conf.$ext <br>";
	copy ("$tmpl_dir/default/agora/conf.php", "$bn_dir_default/conf.$ext");

	// update registration form
	if (!file_exists ("$bn_dir_default/register_form.$ext")  ) {
		echo "<li> copy $tmpl_dir/default/agora/register_form.php $bn_dir_default/register_form.$ext <br>";
		copy ("$tmpl_dir/default/agora/register_form.php", "$bn_dir_default/register_form.$ext");
	}

	// update login form
	if (!file_exists ("$bn_dir_default/login_form.$ext")  ) {
		echo "<li> copy $tmpl_dir/default/agora/login_form.php $bn_dir_default/login_form.$ext <br>";
		copy ("$tmpl_dir/default/agora/login_form.php", "$bn_dir_default/login_form.$ext");
	}

	// update messages
	if (file_exists ("$bn_dir_default/msgs.$ext")  ) {
		updateMsgFile ("$bn_dir_default/msgs.$ext");
	} else {
		echo "<li> copy $tmpl_dir/default/agora/msgs.php $bn_dir_default/msgs.$ext <br>";
		copy ("$tmpl_dir/default/agora/msgs.php", "$bn_dir_default/msgs.$ext");
	}

	// update fields
	if (file_exists ("$bn_dir_default/fields.$ext") ) {
		updateFields ("$bn_dir_default/fields.$ext");
	} else {
		echo "<li> copy $tmpl_dir/default/agora/fields.php $bn_dir_default/fields.$ext <br>";
		copy ("$tmpl_dir/default/agora/fields.php", "$bn_dir_default/fields.$ext");
	}

	// update template
	if (file_exists ("$bn_dir_default/template.$ext") ) {
		updateTemplate ("$bn_dir_default/template.$ext");
	} else {
		echo "<li> copy $tmpl_dir/default/agora/template.php $bn_dir_default/template.$ext <br>";
		copy ("$tmpl_dir/default/agora/template.php", "$bn_dir_default/template.$ext");
	}

	// gets all configured forums
	if (is_array($forums) ) {
		unset($forums);
	}
	$forums = Array();
	$db->query ("select bn_name, bn_db from $site");
	while ($db->next_record() ) {
		$bn_name = $db->f("bn_name");
		$forums[$bn_name] = $db->f("bn_db");
	}

	reset ($forums);
	while (list ($name, $table) = each($forums) ) {
		print "<li><I>updating forum</I>: <B>$name</B> <br>";
		print "<ul>";

		include "$cfg_dir/${name}.$ext";

		// rename backup configuration file
		if (file_exists ("$cfg_dir/${name}.$ext.bak") ) {
			echo "<li> remame backup file  <B>$cfg_dir/${name}.$ext.bak</B>";
			copy ("$cfg_dir/${name}.$ext.bak", "$cfg_dir/${name}.$ext.bak.$ext");
			unlink ("$cfg_dir/${name}.$ext.bak");
		}

		// update messages
		if (!empty($bn_msgs) && file_exists ("$bn_dir/$bn_msgs")  ) {
			updateMsgFile ("$bn_dir/$bn_msgs");
		}

		// update fields
		if (!empty($bn_fields) && file_exists ("$bn_dir/$bn_fields") ) {
			updateFields ("$bn_dir/$bn_fields");
		}

		// update template
		if (!empty($bn_template) && file_exists ("$bn_dir/$bn_template") ) {
			updateTemplate ("$bn_dir/$bn_template");
		}

		// update forum table
		updateForum ($table);

		print "</ul>";
	}

	$db->closeDB();
	unset ($db);

	print "</ul>";

?>