Location: PHPKode > projects > Terra-Terra, A Virtual Operating System > terra-terra.0.0.5/install/index.php
<?php
/****m*
 * NAME
 *  index.php --- Terra-Terra installation procedure
 *
 * DESCRIPTION
 *
 * AUTHOR
 *  Oscar van Eijk, Oveas Functionality Provider
 *
 * COPYRIGHT
 *  (c) 2003-2006 by Oscar van Eijk/Oveas Functionality Provider 
 ***/
/*
 * This module is part of Terra-Terra, the Virtual Operating System
 * http://terra-terra.com
 * ------------------------------------------------------------------------
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License,
 * or any later version.
 * This library 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 Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ------------------------------------------------------------------------
 * $Id: index.php,v 1.6 2006/01/13 09:46:02 tt_oscar Exp $ 
 */

$WAList = array ();
$AnonymousUsername = 'anonymous';
define ('TT_ROOT', '..');
require_once (TT_ROOT . '/kernel/TTclass._TT.pinc');
require_once (TT_ROOT . '/lib/TTtools.pinc');
require_once (TT_ROOT . '/lib/TTstatus.codes.pinc');

function TTINST_error ($msg) {
	echo ("<h1>Kaboom!</h1>The installation procedure failed with the following message:<br /><em>$msg</em><br />");
	echo ("Please <a href='javascript:history.go(-1)'>click here to go back</a>");
	exit;
}

function TTINST_writeconfig () {
	
	global $__formdata;

	if (!$__fp = fopen ($__formdata['ttcfg'] . '/terra-terra.ini', 'w')) {
		TTINST_error("Error opening the file <b>" . $__formdata['ttcfg'] . "/terra-terra.ini</b> for write. Do you have write permission?"); 
	}

	echo ("Creating <b>" . $__formdata['ttcfg'] . "/terra-terra.ini</b>...<br />");

	fwrite ($__fp, "##\n");
	fwrite ($__fp, "# DBMS information.\n");
	fwrite ($__fp, "#\n");
	fwrite ($__fp, "dbtype		= " . $__formdata['db_type'] . "	# MySQL is currently the only supported DBMS\n");
	fwrite ($__fp, "dbserver	= " . $__formdata['db_server'] . "	# Server where the database runs\n");
	fwrite ($__fp, "dbname		= " . $__formdata['db_name'] . "		# Name of the database with all TT tables\n");
	fwrite ($__fp, "dbuser		= " . $__formdata['db_user'] . "	# Username for access to the database\n");
	fwrite ($__fp, "dbpassword	= " . $__formdata['db_passwd'] . "	# The username's password\n");
	fwrite ($__fp, "dbprefix	= " . $__formdata['db_prefix'] . "	# Prefix used in all database names\n");
	fwrite ($__fp, "#\n");
	fwrite ($__fp, "\n");
	fwrite ($__fp, "##\n");
	fwrite ($__fp, "# External program locations\n");
	fwrite ($__fp, "#\n");
	fwrite ($__fp, "perl		= " . $__formdata['perlbin'] . "	# PERL executable\n");
	fwrite ($__fp, "\n");
	fwrite ($__fp, "##\n");
	fwrite ($__fp, "# Security settings\n");
	fwrite ($__fp, "#\n");

	$__pp = popen($__formdata['perlbin'] . ' ./TTkeygen.pl ' . $__formdata['hostnm'], 'r');
	$__ttkey = fgets ($__pp, 4096);
	pclose ($__pp);
	echo ($__perlbin);
	$__ttkey = 
	fwrite ($__fp, "ttkey		= $__ttkey\n");
	fwrite ($__fp, "				# Server specific key generated by the\n");
	fwrite ($__fp, "				# installation procedure.\n");
	fwrite ($__fp, "				# DO NOT MODIFY!\n");
	fwrite ($__fp, "local_only	= 1		# Accept formdata from the local system only (1)\n");
	fwrite ($__fp, "				# or from any system (0)\n");
	fwrite ($__fp, "get_allowed	= 1		# Data submitted with GET\n");
	fwrite ($__fp, "				# (e.g. http://myserv.com/page.php?data=value)\n");
	fwrite ($__fp, "				# is allowed from remote systems (0) or also\n");
	fwrite ($__fp, "				# from the local system only (0)\n");
	fwrite ($__fp, "\n");
	fwrite ($__fp, "splash_text	= " . $__formdata['splash_text'] . "\n");
	fwrite ($__fp, "				# Text displayed while the Desktop is loading\n");
	fwrite ($__fp, "\n");
	fclose ($__fp);
}

function TTINST_rewrite_include () {
	global $__formdata;
	echo ("Rewriting the include file...<br />");
	
	if (!($__inif = file ('../lib/TTinclude._inc'))) {
		TTINST_error("Error opening the file <b>../lib/TTinclude._inc</b> for read."); 
	}
	if (!$__fp = fopen ('../lib/TTinclude.pinc', 'w')) {
		TTINST_error("Error opening the file <b>../lib/TTinclude.pinc</b> for write. Do you have write permission?"); 
	}

	$__sea = array (  "____TT_ROOT_DIRECTORY____"
			, "____TT_TOP_URL____"
			, "____TT_CONFIGURATION_FILE____"
			, "____TT_DB_PREFIX____");
	$__rep = array (  $__formdata['ttroot']
			, $__formdata['tturl']
			, $__formdata['ttcfg']
			, $__formdata['db_prefix']);
	foreach ($__inif as $__line) {
		$__line = str_replace ($__sea, $__rep, $__line);
		fwrite ($__fp, $__line);
	}
	fclose ($__fp);
}

function TTINST_create_group ($__id, $__grp) {
	global $TT_db;
	echo ("Creating group <b>$__grp</b>...");
	$TT_db->query = "INSERT INTO " . TT_DB_PREFIX . "group ( "
		 . "     groupname "
		 . "   , gid "
		 . ") VALUES ("
		 . "    '$__grp' "
		 . "   , NULL "
		 . ") ";

	$TT_db->write ();
	$__id = $TT_db->last_inserted_id();

	echo ("ID is <b>$__id</b><br />");
	return ($__id);
}

function TTINST_create_user ($__id, $__usr, $__passwd) {
	global $AnonymousUsername;
	global $TT_db;
	$__ugrp = TTINST_create_group ($__id, $__usr);

	echo ("Creating user <b>$__usr</b>...");
	$TT_db->query = "INSERT INTO " . TT_DB_PREFIX . "user ( "
		 . "     username "
		 . "   , uid "
		 . "   , gid "
		 . "   , pwdlifetime "
		 . "   , max_sessions "
		 . "   , password "
		 . "   , pwdchange "
		 . ") VALUES ("
		 . "     '$__usr' "
		 . "   , NULL "
		 . "   , $__ugrp "
		 . "   , " . (($__usr == $AnonymousUsername) ? 0 : 720) . " "
		 . "   , 0 "
		 . (($__passwd == '') ? ", ''" : "   ,  '" . TT_password($__passwd) . "' ")
		 . "   , NOW() "
		 . ") ";

	$TT_db->write ();
	$__id = $TT_db->last_inserted_id();
	echo ("ID is <b>$__id</b><br />");
	return ($__id);
}

function TTINST_user_info ($__id, $__uid, $__email, $__fname, $__mname, $__lname) {
	global $TT_db;

	$TT_db->query = "INSERT INTO " . TT_DB_PREFIX . "userinfo ( "
		 . "     uid "
		 . "   , email "
		 . "   , first_name "
		 . "   , middle_name "
		 . "   , last_name "
		 . ") VALUES ("
		 . "     $__uid "
		 . "   , '$__email' "
		 . "   , '$__fname' "
		 . (($__mname == '') ? ", NULL" : "   ,  '$__mname' ")
		 . "   , '$__lname' "
		 . ") ";
	$TT_db->write ();

	$TT_db->query = "INSERT INTO " . TT_DB_PREFIX . "usersource ( "
		 . "     uid "
		 . "   , host "
		 . "   , ip "
		 . "   , pid "
		 . ") VALUES ("
		 . "     $__uid "
		 . "   , NULL "
		 . "   , NULL "
		 . "   , 1 "
		 . ") ";
	$TT_db->write ();
}

function TTINST_user_profile ($__id, $__uid, $__flags) {
	global $TT_db;

	$TT_db->query = "INSERT INTO " . TT_DB_PREFIX . "userprofile ( "
		 . "     uid "
		 . "   , pid "
		 . "   , flags "
		 . ") VALUES ("
		 . "     $__uid "
		 . "   , 1 "
		 . (($__flags) ? "   , 'autologout' " : "   , NULL ")
		 . ") ";
	$TT_db->write ();
}

function TTINST_user_group ($__id, $__uid, $__gid) {
	global $TT_db;

	$TT_db->query = "INSERT INTO " . TT_DB_PREFIX . "usergroup ( "
		 . "     uid "
		 . "   , gid "
		 . "   , pid "
		 . ") VALUES ("
		 . "     $__uid "
		 . "   , $__gid "
		 . "   , 1 "
		 . ") ";
	$TT_db->write ();
}

function TTINST_remove_install_files () {
	global $_SERVER;
	$__location = dirname ($_SERVER['PATH_TRANSLATED']);

	$__dir = opendir ($__location);
	while ($__file = readdir($__dir)) {
		if ($__file == '.' || $__file == '..') {
			continue;
		}
		echo ("...removing <b>$__location/$__file</b>");
		if (!@unlink ("$__location/$__file")) {
			echo "...FAILED!<br />";
		} else {
			echo "...ok <br />";
		}
	}
	closedir ($__dir);
	echo ("...removing directory <b>$__location</b>");
	if (!@rmdir ($__location)) {
		echo "...FAILED!<br />";
	} else {
		echo "...ok <br />";
	}
}

$__id = null;
$__total_steps = 5;

if (function_exists ('version_compare')) {
	$__formdata = $_POST;
	$__me = $_SERVER['PHP_SELF'];
	$__docroot = $_SERVER['DOCUMENT_ROOT'];
	$__hostnm = $_SERVER['SERVER_NAME'];
} else {
	$__formdata = $HTTP_POST_VARS;
	$__me = $HTTP_SERVER_VARS['PHP_SELF'];
	$__docroot = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
	$__hostnm = $HTTP_SERVER_VARS['SERVER_NAME'];
}

if (isset($__formdata['step'])) {
	$__step = $__formdata['step'];
} else {
	$__step = 1;
}

if ($__step > 3) {
	require_once (TT_ROOT . '/lib/TTinclude.pinc');
	require_once (TT_ROOT . '/kernel/TTconfig.pinc');
	require_once (TT_ROOT . '/kernel/TTclass.dbhandle.pinc');
	$TT_conf = array ();
	$__status = TT_read_config_file (TT_CFG_FILE);
	$TT = array ('tt_conf' => $TT_conf); // For compatibility in DbHandle
	$TT_db =& new DbHandle ( $TT['tt_conf']['dbserver']
			, $TT['tt_conf']['dbname']
			, $TT['tt_conf']['dbuser']
			, $TT['tt_conf']['dbpassword']
			, $TT['tt_conf']['dbtype']
			, false);
} else {
	require_once (TT_ROOT . '/lib/TTinclude._inc');
}

switch ($__step) {
	case 1:
		break;
	case 2:
		// Create the database
		//
		require_once (TT_ROOT . '/kernel/TTclass.xmlhandler.dbase.pinc');
		$XMLhandler =& new XMLhandler ($__formdata);
		$XMLparser  =& new XMLparser (TT_ROOT . '/install/TTdb.xml', $XMLhandler);
		$XMLparser->parse(XML_PARSE_HANDLE);
		break;
	case 3:
		// ##### TODO ##### Form validation, set $__warning on error
		TTINST_writeconfig ();
		TTINST_rewrite_include ();
		break;
	case 4:
		$__visit_usr = TTINST_create_user ($__id, $AnonymousUsername, '');
		TTINST_user_info ($__id, $__visit_usr, '', 'TT', '', 'Guest');
		TTINST_user_profile ($__id, $__visit_usr, False);

		$__admin_usr = TTINST_create_user ($__id, $__formdata['admusr'], $__formdata['admpwd1']);
		TTINST_user_info ($__id, $__admin_usr, $__formdata['admemail'], $__formdata['admfname'], $__formdata['admmname'], $__formdata['admlname']);
		TTINST_user_profile ($__id, $__admin_usr, True);
		
		$__visit_grp = TTINST_create_group ($__id, 'TTvisitors');
		$__admin_grp = TTINST_create_group ($__id, 'TTadmin');

		TTINST_user_group ($__id, $__visit_usr, $__visit_grp);
		TTINST_user_group ($__id, $__admin_usr, $__admin_grp);

		break;
	case 5:
		// Load data
		//
		require_once (TT_ROOT . '/kernel/TTclass.xmlhandler.dbase.pinc');

		$XMLhandler =& new XMLhandler ();

		echo ("Load the data...<br />");
		$XMLparser  =& new XMLparser (TT_ROOT . '/install/TTdata.xml', $XMLhandler);
		$XMLparser->parse(XML_PARSE_HANDLE);
		$XMLparser->__destruct();

		echo ("Load the default language...<br />");
		$XMLparser  =& new XMLparser (TT_ROOT . '/xml/TTlang_def.xml', $XMLhandler);
		$XMLparser->parse(XML_PARSE_HANDLE);

		echo ("Write the configuration to the database...<br />");
		foreach ($__formdata as $__f_item => $__c_value) {
			if (($__c_item = preg_replace("/^cfg_/", "", $__f_item)) != $__f_item) {
				echo ("...$__c_item = $__c_value<br />");
				$TT_db->query = "INSERT INTO " . TT_DB_PREFIX . "configuration ( "
					 . "     item "
					 . "   , value "
					 . ") VALUES ("
					 . "     '$__c_item' "
					 . ",    '$__c_value' "
					 . ") ";
				$TT_db->write ();
			}
		}
		if (isset($__formdata['rem_install'])) {
			echo ("Removing the installation files...<br />");
				TTINST_remove_install_files();
		}
		break;
	default:

		break;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Terra-Terra Installation</title>
<link href="../themes/green/terra-terra.css" rel="stylesheet" type="text/css" />
</head>
<script language="JavaScript">
<!--
function CheckMe (FormOBJ, Step) {

	Rval = true;
	switch (Step) {
		case '2' :
			if (FormOBJ.ttcfg.value.indexOf("<?php echo ($__docroot); ?>") >= 0) {
				if (!confirm("It is not recommended to create the configuration file under your Document Root, since this file contains your database password. Are you sure you want to continue?")) {
					FormOBJ.ttcfg.value = "";
					FormOBJ.ttcfg.focus();
					Rval = false;
				}
			}
			break;
		case '3' :
			if (FormOBJ.admpwd1.value.length < 6) {
				alert ("Password is too short");
				FormOBJ.admpwd1.value = "";
				FormOBJ.admpwd2.value = "";
				FormOBJ.admpwd1.focus();
				Rval = false;
			}
			if (FormOBJ.admpwd1.value != FormOBJ.admpwd2.value) {
				alert ("Passwords do not match");
				FormOBJ.admpwd2.value = "";
				FormOBJ.admpwd2.focus();
				Rval = false;
			}
			break
		case '4' :
			if (FormOBJ.cfg_standard_email.value == '') {
				alert ("A valid email address is required, otherwise no mails can be sent");
				FormOBJ.cfg_standard_email.focus();
				Rval = false;
			}
			break
		default :
			break;
	}
	return (Rval);
}
-->
</script>
<body onload="this.location=this.location+'#step';return true;">

<a name="step"></a><h1>Terra-Terra Installation - step <?php echo ($__step . ' / ' . $__total_steps); ?></h1>

<?php if (isset($__warning) && !empty($__warning)) { echo ("<em>$__warning</em>"); } ?>

<form action="<?php echo ($__me); ?>" method="POST" onSubmit="return CheckMe(this, '<?php echo ($__step); ?>');">
<?php if ($__step == '1'): ?>
	<h2>Create the database</h2>
	<table>
		<tr>
			<td>Database server: </td>
			<td><input type="text" name="db_server" value="localhost" size=20 /></td>
		</tr>
		<tr>
			<td>Database type: </td>
			<td><select name="db_type">
				<option value="MySQL">MySQL</option>
				</select></td>
		</tr>
		<tr>
			<td>Database name:  </td>
			<td><input type="text" name="db_name" value="TTerra" size=20 /></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><input type="checkbox" name="create_db" /> create this database</td>
		</tr>
		<tr>
			<td>Prefix for database tables: </td>
			<td><input type="text" name="db_prefix" value="TT_" size=20 /></td>
		</tr>
		<tr>
			<td>Database user: </td>
			<td><input type="text" name="db_user" size=20 /></td>
		</tr>
		<tr>
			<td>Database password: </td>
			<td><input type="password" name="db_passwd" size=20 /></td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="Create Database" size=20 /></td>
		</tr>
	</table>
	<input type="hidden" name="step" value="2" />

<?php elseif ($__step == '2'): ?>
<?php
	$hostnm = $__hostnm;
	$__fp = popen('which perl', 'r');
	$ttroot = str_replace ("/install", "", getcwd());
	$tturl = str_replace ($__docroot, "", $ttroot);
	$tturl = "/"; // if ($tturl == '') { $tturl = "/"; } (Why was this v0.0.1 check removed?? OvE 20050222)
	$perlbin = fgets ($__fp, 4096);
	pclose ($__fp);
	$splash_text = "Loading the Terra-Terra workspace, one moment please...";
	$ttcfg = "";
?>
	<h2>Create the configuration file</h2>
	<table>
		<tr>
			<td>Host name:  </td>
			<td><input type="text" name="hostnm" value="<?php echo ($hostnm); ?>" size=20 /></td>
		</tr>
		<tr>
			<td>Terra-Terra installation directory:  </td>
			<td><input type="text" name="ttroot" value="<?php echo ($ttroot); ?>" size=20 /></td>
		</tr>
		<tr>
			<td>URL to Terra-Terra:  </td>
			<td><input type="text" name="tturl" value="<?php echo ($tturl); ?>" size=20 /></td>
		</tr>
		<tr>
			<td>Location of your PERL binary: </td>
			<td><input type="text" name="perlbin" value="<?php echo ($perlbin); ?>" size=20 /></td>
		</tr>
		<tr>
			<td>Where should your Terra-Terra configuration file be created: </td>
			<td><input type="text" name="ttcfg" value="<?php echo ($ttcfg); ?>" size=20 /></td>
		</tr>
		<tr>
			<td>Splash text:  </td>
			<td><input type="text" name="splash_text" value="<?php echo ($splash_text); ?>" size=20 /></td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="Write configuration file" size=20 /></td>
		</tr>
	</table>
	<input type="hidden" name="step" value="3" />
	<input type="hidden" name="db_type" value="<?php echo ($__formdata['db_type']); ?>" />
	<input type="hidden" name="db_server" value="<?php echo ($__formdata['db_server']); ?>" />
	<input type="hidden" name="db_name" value="<?php echo ($__formdata['db_name']); ?>" />
	<input type="hidden" name="db_user" value="<?php echo ($__formdata['db_user']); ?>" />
	<input type="hidden" name="db_passwd" value="<?php echo ($__formdata['db_passwd']); ?>" />
	<input type="hidden" name="db_prefix" value="<?php echo ($__formdata['db_prefix']); ?>" />

<?php elseif ($__step == '3'): ?>
	<h2>Create users and groups</h2>
	<table>
		<tr>
			<td colspan="2"><b>Administrator information</b> </td>
		</tr>
		<tr>
			<td>Username:  </td>
			<td><input type="text" name="admusr" value="admin" size=20 /></td>
		</tr>
		<tr>
			<td>Password:  </td>
			<td><input type="password" name="admpwd1" size=20 /></td>
		</tr>
		<tr>
			<td>Please retype the password for verification:  </td>
			<td><input type="password" name="admpwd2" size=20 /></td>
		</tr>
		<tr>
			<td>E-mail address:  </td>
			<td><input type="text" name="admemail" size=20 /></td>
		</tr>
		<tr>
			<td>First name:  </td>
			<td><input type="text" name="admfname" value="Terra-Terra" size=20 /></td>
		</tr>
		<tr>
			<td>Middle name:  </td>
			<td><input type="text" name="admmname" size=20 /></td>
		</tr>
		<tr>
			<td>Last name:  </td>
			<td><input type="text" name="admlname" value="Administrator" size=20 /></td>
		</tr>
		<tr>
			<td colspan="2">A visitor user account, <em><?php echo ($AnonymousUsername); ?></em>, will also be created.</td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="Create users and groups" size=20 /></td>
		</tr>
	</table>
	<input type="hidden" name="step" value="4" />
<?php elseif ($__step == '4'): ?>
	<h2>Basic configuration, standard applications and areas</h2>
	<table>
		<tr>
			<td colspan="2">The basic configuration contains some settings that have
			to exist in the Configuration table. The settings here can be changed later
			when logged in with the administratot account.</td>
		</tr>
		<tr>
			<td>Default language:<sup>*</sup>  </td>
			<td><input type="text" name="cfg_def_language" value="def" readonly size=3 /></td>
		</tr>
		<tr>
			<td>Default color theme:<sup>*</sup> </td>
			<td><input type="text" name="cfg_def_theme" value="ttgreen" readonly size=20 /></td>
		</tr>
		<tr>
			<td>Default session timeout </td>
			<td><input type="text" name="cfg_def_autologout" value="30" size=20 /> minutes</td>
		</tr>
		<tr>
			<td>Default password lifetime </td>
			<td><input type="text" name="cfg_pwdlifetime" value="60" size=20 /> days (0=disable)</td>
		</tr>
		<tr>
			<td>E-mail address for all outgoing mails:  </td>
			<td><input type="text" name="cfg_standard_email" size=20 /></td>
		</tr>
		<tr>
			<td colspan="2"><sup>*</sup> These items cannot be changed at this point,
			since no checks are implemented in these installation procedure to verify
			the existance of a new language and colortheme. <br />
			Please use the Administration tools when installation is complete.</td>
		</tr>
		<tr>
			<td colspan="2">Next, the default applications and workareas will be created.
			You need these objects to work with Terra-Terra. Most of the values that are created
			in the database now, can be modified later using the 'admin' application.</td>
		</tr>
		<tr>
			<td colspan="2"><input type="checkbox" value="1" name="rem_install" checked /> Remove installation directory
			and all contents
			<blockquote>
			<b><i>Note</i></b> If you are running PHP 4.3.2 of higher with Apache2, this will not work, unless you
			set <tt>AcceptPathInfo = On</tt> in httpd.conf.
			</blockquote>
			</td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" value="Create Configuration and Applications" size=20 /></td>
		</tr>
	</table>
	<input type="hidden" name="step" value="5" />
<?php else: ?>
	<h2>Congratulations!</h2>
	
	<p>Your Terra-Terra installation is now complete! Please <a href="../">click here</a>
	for the last step: go to the Terra-Terra workspace and play around! </p>
<?php endif; ?>
</form>

<?php
if ($__step > 3) {
	$TT_db->close ();
}
?>
</body>
</html>
Return current item: Terra-Terra, A Virtual Operating System