<?php
// $Id: install4.php,v 1.47 2004/02/09 11:59:16 prazgod Exp $
/**
* Step 4 of Installation
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.47 $
**/

include_once("common.php");

$DBhostname = trim( mosGetParam( $_POST, 'DBhostname', '' ) );
$DBuserName = trim( mosGetParam( $_POST, 'DBuserName', '' ) );
$DBpassword = trim( mosGetParam( $_POST, 'DBpassword', '' ) );
$DBname  	= trim( mosGetParam( $_POST, 'DBname', '' ) );
$sitename  	= trim( mosGetParam( $_POST, 'sitename', '' ) );
$adminEmail = trim( mosGetParam( $_POST, 'adminEmail', '') );
$siteUrl  	= trim( mosGetParam( $_POST, 'siteUrl', '' ) );
$absolutePath = trim( mosGetParam( $_POST, 'absolutePath', '' ) );
$adminPassword = trim( mosGetParam( $_POST, 'adminPassword', '') );

if ((trim($adminEmail== "")) || (preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $adminEmail )==false)) {
	echo "<form name=\"stepBack\" method=\"post\" action=\"install3.php\">
		<input type=\"hidden\" name=\"DBhostname\" value=\"$DBhostname\">
		<input type=\"hidden\" name=\"DBuserName\" value=\"$DBuserName\">
		<input type=\"hidden\" name=\"DBpassword\" value=\"$DBpassword\">
		<input type=\"hidden\" name=\"DBname\" value=\"$DBname\">
		<input type=\"hidden\" name=\"DBcreated\" value=1>
		<input type=\"hidden\" name=\"sitename\" value=\"$sitename\">
		<input type=\"hidden\" name=\"adminEmail\" value=\"$adminEmail\">
		<input type=\"hidden\" name=\"siteUrl\" value=\"$siteUrl\">
		<input type=\"hidden\" name=\"absolutePath\" value=\"$absolutePath\">
		</form>";

	echo "<SCRIPT>alert('You must provide a valid admin email address.'); document.stepBack.submit(); </SCRIPT>";
	return;
}

if($DBhostname && $DBuserName && $DBname) {
	$configArray['DBhostname'] = $DBhostname;
	$configArray['DBuserName'] = $DBuserName;
	$configArray['DBpassword'] = $DBpassword;
	$configArray['DBname']     = $DBname;
} else {
	echo "<form name=\"stepBack\" method=\"post\" action=\"install3.php\">
		<input type=\"hidden\" name=\"DBhostname\" value=\"$DBhostname\">
		<input type=\"hidden\" name=\"DBuserName\" value=\"$DBuserName\">
		<input type=\"hidden\" name=\"DBpassword\" value=\"$DBpassword\">
		<input type=\"hidden\" name=\"DBname\" value=\"$DBname\">
		<input type=\"hidden\" name=\"DBcreated\" value=1>
		<input type=\"hidden\" name=\"sitename\" value=\"$sitename\">
		<input type=\"hidden\" name=\"adminEmail\" value=\"$adminEmail\">
		<input type=\"hidden\" name=\"siteUrl\" value=\"$siteUrl\">
		<input type=\"hidden\" name=\"absolutePath\" value=\"$absolutePath\">
		</form>";

	echo "<SCRIPT>alert('The database details provided are incorrect and/or empty'); document.stepBack.submit(); </SCRIPT>";
	return;
}

if ($sitename) {
	if (!get_magic_quotes_gpc()) {
		$configArray['sitename'] = addslashes($sitename);
	} else {
		$configArray['sitename'] = $sitename;
	}
} else {
	echo "<form name=\"stepBack\" method=\"post\" action=\"install3.php\">
		<input type=\"hidden\" name=\"DBhostname\" value=\"$DBhostname\">
		<input type=\"hidden\" name=\"DBuserName\" value=\"$DBuserName\">
		<input type=\"hidden\" name=\"DBpassword\" value=\"$DBpassword\">
		<input type=\"hidden\" name=\"DBname\" value=\"$DBname\">
		<input type=\"hidden\" name=\"DBcreated\" value=1>
		<input type=\"hidden\" name=\"sitename\" value=\"$sitename\">
		<input type=\"hidden\" name=\"adminEmail\" value=\"$adminEmail\">
		<input type=\"hidden\" name=\"siteUrl\" value=\"$siteUrl\">
		<input type=\"hidden\" name=\"absolutePath\" value=\"$absolutePath\">
		</form>";

	echo "<SCRIPT>alert('The sitename has not been provided'); document.stepBack2.submit();</SCRIPT>";
	return;
}

if (file_exists( '../configuration.php' )) {
	$canWrite = is_writable( '../configuration.php' );
} else {
	$canWrite = is_writable( '..' );
}

if ($siteUrl) {
	$configArray['siteUrl']=$siteUrl;
	// Fix for Windows
	$absolutePath= str_replace("\\","/", $absolutePath);
	$configArray['absolutePath']=$absolutePath;

	$config = "<?php\n";
	$config .= "\$mosConfig_offline = '0';\n";
	$config .= "\$mosConfig_host = '{$configArray['DBhostname']}';\n";
	$config .= "\$mosConfig_user = '{$configArray['DBuserName']}';\n";
	$config .= "\$mosConfig_password = '{$configArray['DBpassword']}';\n";
	$config .= "\$mosConfig_db = '{$configArray['DBname']}';\n";
	$config .= "\$mosConfig_dbprefix = 'mos_';\n";
	$config .= "\$mosConfig_lang = 'english';\n";
	$config .= "\$mosConfig_absolute_path = '{$configArray['absolutePath']}';\n";
	$config .= "\$mosConfig_live_site = '{$configArray['siteUrl']}';\n";
	$config .= "\$mosConfig_sitename = '{$configArray['sitename']}';\n";
	$config .= "\$mosConfig_shownoauth = '0';\n";
	$config .= "\$mosConfig_emailpass = '1';\n";
	$config .= "\$mosConfig_offline_message = 'This site is down for maintenance.<br /> Please check back again soon.';\n";
	$config .= "\$mosConfig_error_message = 'This site is temporarily unavailable.<br /> Please notify the System Administrator';\n";
	$config .= "\$mosConfig_UseBanner = '1';\n";
	$config .= "\$mosConfig_debug = '0';\n";
	$config .= "\$mosConfig_lifetime = '900';\n";
	$config .= "\$mosConfig_MetaDesc = 'Mambo Open Source - the dynamic portal engine and content management system';\n";
	$config .= "\$mosConfig_MetaKeys = 'mambo, mambo open source, Mambo, Mambo Open Source';\n";
	$config .= "\$mosConfig_locale = 'en_GB';\n";
	$config .= "\$mosConfig_offset = '0';\n";
	$config .= "\$mosConfig_hideAuthor = '0';\n";
	$config .= "\$mosConfig_hideCreateDate = '0';\n";
	$config .= "\$mosConfig_hideModifyDate = '0';\n";
	$config .= "\$mosConfig_hidePdf = '".intval( !is_writable( "{$configArray['absolutePath']}/media/" ) )."';\n";
	$config .= "\$mosConfig_hidePrint = '0';\n";
	$config .= "\$mosConfig_hideEmail = '0';\n";
	$config .= "\$mosConfig_enable_log_items = '0';\n";
	$config .= "\$mosConfig_enable_log_searches = '0';\n";
	$config .= "\$mosConfig_sef = '0';\n";
	$config .= "\$mosConfig_vote = '0';\n";
	$config .= "\$mosConfig_gzip = '0';\n";
	$config .= "\$mosConfig_multipage_toc = '0';\n";
	$config .= "\$mosConfig_allowUserRegistration = '1';\n";
	$config .= "\$mosConfig_link_titles = '0';\n";
	$config .= "\$mosConfig_error_reporting = -1;\n";
	$config .= "\$mosConfig_editor = 'htmlarea2';\n";
	$config .= "setlocale (LC_TIME, \$mosConfig_locale);\n";
	$config .= "?>";

	if ($canWrite && ($fp = fopen("../configuration.php", "w"))) {
		fputs( $fp, $config, strlen( $config ) );
		fclose( $fp );
	} else {
		$canWrite = false;
	}

	$cryptpass=md5($adminPassword);

	mysql_connect($DBhostname, $DBuserName, $DBpassword);
	mysql_select_db($DBname);

// create the admin user
	$installdate = date("Y-m-d H:i:s");
	$query = "INSERT INTO `mos_users` VALUES (62, 'Administrator', 'admin', '$adminEmail', '$cryptpass', 'superadministrator', 0, 1, 25, '$installdate', '0000-00-00 00:00:00')";
	mysql_query( $query );
// add the ARO (Access Request Object)
	$query = "INSERT INTO `mos_core_acl_aro` VALUES (10,'users','62',0,'Administrator',0)";
	mysql_query( $query );
// add the map between the ARO and the Group
	$query = "INSERT INTO `mos_core_acl_groups_aro_map` VALUES (25,'',10)";
	mysql_query( $query );

	if (@chmod ($absolutePath."/images/", 0777) &&
			@chmod ($absolutePath."/components/", 0707) &&
			@chmod ($absolutePath."/modules/", 0707) &&
			@chmod ($absolutePath."/templates/", 0707) &&
			@chmod ($absolutePath."/language/", 0707) &&
			@chmod ($absolutePath."/media/", 0777) &&
			@chmod ($absolutePath."/uploadfiles/", 0777) &&
			@chmod ($absolutePath."/administrator/components/", 0707) &&
			@chmod ($absolutePath."/administrator/backups/", 0707)) {
		$chmod_report = "Directory permissions successfully changed.";
	} else {
		$chmod_report = "Directory permissions could not be changed.  Please CHMOD 777 the following directories manually:<br />
		<strong>/images<br />
		/components/<br />
		/language/<br />
		/modules/<br />
		/templates/<br />
		/media/<br />
		/uploadfiles/<br />
		/administrator/components/<br />
		/administrator/backups/</strong>";
	}
} else { ?>
<form action="install3.php" method="post" name="stepBack3" id="stepBack3">
  <input type="hidden" name="dbhostname" value="<?php echo $DBhostname;?>" />
  <input type="hidden" name="dbusername" value="<?php echo $DBuserName;?>" />
  <input type="hidden" name="dbpassword" value="<?php echo $DBpassword;?>" />
  <input type="hidden" name="dbname" value="<?php echo $DBname;?>" />
  <input type="hidden" name="dbcreated" value="1" />
  <input type="hidden" name="sitename" value="<?php echo $sitename;?>" />
</form>
<?php
echo "<script>alert('The site url has not been provided'); document.stepBack3.submit();</script>";
}

?>
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Mambo Open Source Installer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="install.css" type="text/css" />
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="dottedline">
  <tr>
    <td align="center" class="contentbackgr"><span class="installheader">Welcome
      to Mambo Open Source installation</span></td>
  </tr>
</table>
<br />
<form action="<?php echo"$PHP_SELF?$QUERY_STRING"; ?>" method="post" name="form" id="form">
  <input type="hidden" name="DBhostname" value="<?php echo "$DBhostname"; ?>" />
  <input type="hidden" name="DBuserName" value="<?php echo "$DBuserName"; ?>" />
  <input type="hidden" name="DBpassword" value="<?php echo "$DBpassword"; ?>" />
  <input type="hidden" name="DBname" value="<?php echo "$DBname"; ?>" />
  <input type="hidden" name="sitename" value="<?php echo "$sitename"; ?>" />
  <input type="hidden" name="siteUrl" value="<?php echo "$siteUrl"; ?>" />
  <input type="hidden" name="absolutePath" value="<?php echo "$absolutePath"; ?>" />
   <table width="550" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="10" height="10"><img src="../images/M_images/top_left_corner.png" width="10" height="10" /></td>
      <td background="../images/M_images/top_line.png"></td>
      <td width="10" height="10"><img src="../images/M_images/top_right_corner.png" width="10" height="10" /></td>
    </tr>
    <tr>
      <td background="../images/M_images/left_line.png"></td>
      <td>
  <table width="100%" border="0" align="center" cellpadding="0" cellspacing="4" class="installation">
                <tr>
                  <td colspan="2" class="heading">Step 4 - Congratulations! Mambo Open Source is installed</td>
                </tr>
		<tr>
		<td colspan="2" class="error" align="center"><br />** PLEASE REMEMBER TO COMPLETELY REMOVE THE INSTALLATION DIRECTORY **</td>
		</tr>
                <tr>
                <td colspan="2" align="center"><h5>Administration Login Details</h5></td>
                </tr>
                <tr>
                <td colspan="2" align="center"><b>Username : admin</b></td>
                </tr>
                <tr>
                <td colspan="2" align="center"><b>Password : <?php echo $adminPassword; ?></b></td>
                </tr>
                <tr>
                  <td colspan="2"> <p><br />Click the "Run" button to start Mambo Open Source site
                      or "Admin" to take you to administrator login.</p></td>
                </tr>
                <tr>
                  <td colspan="2" align="right">
                    <input class="button" type="button" name="runSite" value="Run"
				 			<?php if ($siteUrl){
				 				print "onClick='window.location.href=\"$siteUrl"."/index.php\" '";
				 			}
				 			else
				 			{
				 				print "onClick='window.location.href=\"{$configArray['siteURL']}"."/index.php\" '";
							}?> />

                    <input class="button" type="button" name="Admin" value="Admin"
				 			<?php if ($siteUrl){
				 				print "onClick='window.location.href=\"$siteUrl"."/administrator/index.php\" '";
				 			}
				 			else
				 			{
				 				print "onClick='window.location.href=\"{$configArray['siteURL']}"."/administrator/index.php\" '";
							}?> /> </td>
                </tr>
<?php if (!$canWrite) { ?>
			<tr>

      <td colspan="2" class="small"> Your configuration file or directory is not writeable,
        or there was a problem creating the configuration file. You'll have to
        upload the following code by hand. Click in the textarea to highlight
        all of the code. </td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<textarea rows="5" cols="65" name="configcode" onclick="javascript:this.form.configcode.focus();this.form.configcode.select();" ><?php echo htmlspecialchars( $config );?></textarea>
				</td>
			</tr>
<?php } ?>
            <tr>
                <td colspan="2" class="small"><?php //echo $chmod_report; ?></td>
            </tr>
        </table></td>
      <td background="../images/M_images/right_line.png"></td>
    </tr>
    <tr>
      <td width="10" height="10" background="../images/M_images/bottom_left_corner.png"></td>
      <td background="../images/M_images/bottom_line.png"></td>
      <td width="10" height="10" background="../images/M_images/bottom_right_corner.png"></td>
    </tr>
  </table>
</form>
</body>
</html>
