Location: PHPKode > projects > WebArchive > phparchive.0.1.82.beta/install.php
<?php
/**
 * @version $Id: install.php,v 1.6 2004/01/18 19:56:33 freedev Exp $
 * @author Vincenzo D'Amore <hide@address.com>
 * @package wa_base
 */

/**
 *
 */

header("Cache-control: private, no-cache");

require_once("./wacommon.inc.php");
require_once("./includes/databases.inc.php");
require_once("./installmsg.inc.php");

if (!isset($_POST["hdnStatus"]))
{
	$iStatus = 0;
}
else
{
	$iStatus = (int)$_POST["hdnStatus"];
}

$sSystemMessages = "";
$sSchemaDBDump = "db/phparchive.schema.dmp";
$sDataDBDump = "db/phparchive.data.dmp";
$FormDBInfo = &$GLOBALS["WADBInfo"];

switch ($iStatus)
{
case 0:
	$bRetVal = True;
	$sButton = WA_INSTALL_CMD_NEXT;
	$sConsoleTitle = WA_INSTALL_STEP." ".$iStatus."<br>".WA_INSTALL_MAIN_TITLE;
	$sConsole = WA_INSTALL_WELCOME_1.WA_INSTALL_WELCOME_2;
	$sConsole .= "<p>".WA_INSTALL_PUSH_BUTTON;
	break;
case 1:
	$bRetVal = True;
	$sButton = "";
	$sConsoleTitle = WA_INSTALL_STEP." ".$iStatus."<br>".WA_INSTALL_MAIN_TITLE;
	if ((!is_file($sSchemaDBDump)) || (!is_file($sDataDBDump)))
	{
		$sSystemMessages .= WA_INSTALL_RESOURCE_UNAVAILABLE." ".$sSchemaDBDump." or ".$sDataDBDump.".";
		$sConsole = "<p><b>".WA_INSTALL_FAILED."</b><br>";
		$sConsole .= "<p><b>".WA_INSTALL_CHECK_CONFIG."</b>";
		$sConsole .= "<p><a href='".$_SERVER['PHP_SELF']."?'>".WA_INSTALL_RETRY."</a>";
		$bRetVal = False;
	}
	elseif (CheckDBRepository())
	{
		$sSystemMessages = WA_INSTALL_IS_REPOSITORY;
		$sConsole = "<p><a href='index.php'>".WA_MSG_BACK_TO_LOGIN."</a>";
		$bRetVal = False;
	}
	if (!empty($GLOBALS['WA_DIRECTORY_SESSIONS']))
	{
		if (!is_dir($GLOBALS['WA_DIRECTORY_SESSIONS']))
		{
			$sSystemMessages = ".";
			$sSystemMessages .= WA_INSTALL_RESOURCE_UNAVAILABLE." ( ".$GLOBALS['WA_DIRECTORY_SESSIONS']." ).";
			$sConsole = "<p><b>".WA_INSTALL_FAILED."</b><br>";
			$sConsole .= "<p><b>".WA_INSTALL_CHECK_CONFIG."</b>";
			$sConsole .= "<p><a href='".$_SERVER['PHP_SELF']."?'>".WA_INSTALL_RETRY."</a>";
			$bRetVal = False;
		}
		elseif (!CheckWritePermission($GLOBALS['WA_DIRECTORY_SESSIONS'])) 
		{
			$sSystemMessages = ".";
			$sSystemMessages .= WA_INSTALL_RESOURCE_UNWRITEBLE." ( ".$GLOBALS['WA_DIRECTORY_SESSIONS']." ).";
			$sConsole = "<p><b>".WA_INSTALL_FAILED."</b><br>";
			$sConsole .= "<p><b>".WA_INSTALL_CHECK_CONFIG."</b>";
			$sConsole .= "<p><a href='".$_SERVER['PHP_SELF']."?'>".WA_INSTALL_RETRY."</a>";
			$bRetVal = False;
		}
	}
	if ($GLOBALS['WA_UPLOAD_FILES'])
	{
		if (!is_dir($GLOBALS['WA_UPLOAD_DIR']))
		{
			$sSystemMessages = WA_INSTALL_UPLOAD_DIR_UNDEFINED;
			$sConsole = "<p><b>".WA_INSTALL_FAILED."</b><br>";
			$sConsole .= "<p><b>".WA_INSTALL_CHECK_CONFIG."</b>";
			$sConsole .= "<p><a href='".$_SERVER['PHP_SELF']."?'>".WA_INSTALL_RETRY."</a>";
			$bRetVal = False;
		}
		elseif (!CheckWritePermission($GLOBALS['WA_UPLOAD_DIR']))
		{
			$sSystemMessages = ".";
			$sSystemMessages .= WA_INSTALL_RESOURCE_UNWRITEBLE." ( ".$GLOBALS['WA_UPLOAD_DIR'].").";
			$sConsole = "<p><b>".WA_INSTALL_FAILED."</b><br>";
			$sConsole .= "<p><b>".WA_INSTALL_CHECK_CONFIG."</b>";
			$sConsole .= "<p><a href='".$_SERVER['PHP_SELF']."?'>".WA_INSTALL_RETRY."</a>";
			$bRetVal = False;
		}
	}

	if ($bRetVal)
	{
		$sButton = WA_INSTALL_CMD_NEXT;
		$sConsole = "<p>".WA_INSTALL_PUSH_BUTTON;
//		$sConsole = "Premere il pulsante <b>\"".$sButton."\"</b> per creare il database repository.<br>";
		$sConsole .= "<br><b>System: ".php_uname()."</b>";
		if ($GLOBALS['WA_UPLOAD_FILES'])
		{
			$sConsole .= "<br><b>WAUpload Dir: ".$GLOBALS['WA_UPLOAD_DIR'];
		}
		$sConsole .= "<br><br><b>Host: ".$GLOBALS['WA_CONF_DBHOST']."</b>";
		$sConsole .= "<br><b>Database: ".$GLOBALS['WA_CONF_DBNAME']."</b>";
		$sConsole .= "<br><b>Schema Dump: ".$sSchemaDBDump."</b>";
		$sConsole .= "<br><b>Data Dump: ".$sDataDBDump."</b>";
		$sConsole .= "<br><b>User: ".$GLOBALS['WA_CONF_DBUSER']."</b>";
		$sConsole .= "<br><b>Type: ".$GLOBALS['WA_CONF_DBTYPE']."</b>";
	}
	break;
case 2:
	$sButton = "";
	$sConsoleTitle = WA_INSTALL_STEP." ".$iStatus."<br>".WA_INSTALL_MAIN_TITLE;
	$rec = new WARecordset();
	$rec->SetupFromGlobalCnn();
	$bRetVal = false;
	// Check if database exist.
	if (!$rec->WAIsConnected())
	{
		// Database does not exist so we have to create a new one.
		if (!CreateDatabase($sSystemMessages, $FormDBInfo))
		{
			$sSystemMessages .= WA_INSTALL_MYSQL_UNAVAILABLE."<br>";
			$sConsole = "<p><b>".WA_INSTALL_FAILED."</b><br>";
			$sConsole .= "<p><b>".WA_INSTALL_CHECK_CONFIG."</b>";
			$sConsole .= "<p><a href='".$_SERVER['PHP_SELF']."?'>".WA_INSTALL_RETRY."</a>";
		}
	}
	// If the repository was not already loaded we load a new one.
	if (!CheckDBRepository())
	{
		$bRetVal = InstallDatabase($sSystemMessages, $sSchemaDBDump, $FormDBInfo);
		$bRetVal = InstallDatabase($sSystemMessages, $sDataDBDump, $FormDBInfo);
	}
	if ($bRetVal)
	{
		$sConsole = "<p><b>".WA_INSTALL_SUCCESS."</b>";
		$sConsole .= "<p><a href='index.php'>".WA_MSG_BACK_TO_LOGIN."</a>";
	}
	else
	{
		$sConsole = "<b>".WA_INSTALL_FAILED."</b>";
		$sConsole .= "<p><a href='".$_SERVER['PHP_SELF']."?'>".WA_INSTALL_RETRY."</a>";
	}
	break;
}

$iStatus++;

	PrintHTMLHeader("phparchive Installation", "");

?>
<body class="BodyStandard">
<?php include "header.php"; ?>
<table width="100%" border="0" cellspacing="2" cellpadding="2">
	<tr>
		<td colspan="2"><br>
		</td>
	</tr>
	<tr>
	<td valign=top>
		<table width="80%" border="0" cellpadding="2" cellspacing="2" class="MenuTable">
			<tr >
				<td width="20%">
					<img src="images/install.gif" border="0">
				</td>
				<td>
					<table width="80%" border="0" cellpadding="2" cellspacing="2">
						<tr>
							<td valing="top" align="left" >
								<span class="BigTitle">
									<?php
									   echo $sConsoleTitle."<br><br>";
									?>
								</span>
							</td>
						</tr>
						<tr>
							<td align="left" >
								<span class="FieldStd">
									<?php
									   echo $sConsole;
									?>
								</span>
							</td>
						</tr>
						<?php
						if (strlen($sSystemMessages))
						{
							echo "<tr><td align='left' class='Inputs'>";
							echo "<span class='FixedFont'>".$sSystemMessages."</span>";
							echo "</td></tr>";
							echo "<tr><td align='left'><br></td></tr>";
						}
						?>
						<tr>
							<td align="left" >
								<br><br>
						      <form name="formInstall" method="post" action="install.php" class="Form">
								<input type='hidden' name='hdnStatus' value='<?php echo $iStatus; ?>' >
								<?php
									if (($iStatus < 3) && ($bRetVal))
										echo "<input class='Buttons' type='submit' name='cmdInstall' value='".$sButton."' >";
								?>
						      </form>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</td>
  </tr>
</table>
<?php
	include "footer.php";
?>
</body>
</html>

<?php

function CheckWritePermission($sPath)
{
	$sPath .= "/test.tmp";
	$fp = fopen($sPath, "w");
	if ($fp == False)
	{
		return false;
	}
	fclose($fp);
	unlink($sPath);
	return true;
}

function CheckDBRepository()
{
	$rec = new WARecordset();
	$rec->SetupFromGlobalCnn();
	if ($rec->WAIsConnected())
	{
		$sSql = "select * from tbtables";
		$rec->WAOpenRecordset($sSql);
		if ($rec->WAIsOpen())
		{
			$rec->WACloseRecordset();
			return True;
		}
	}
	return False;
}

function CreateDatabase(&$sSystemMessage, $FormDBInfo)
{
	$bRC = false;
	$link = mysql_connect($FormDBInfo["WAHost"], $FormDBInfo["WAUser"], $FormDBInfo["WAPassword"]);
	if (is_resource($link))
	{
		if (mysql_create_db($FormDBInfo["WADBName"], $link))
			$bRC = true;
	}
	return $bRC;
}

function InstallDatabase(&$sSystemMessage, $sDBDump, $FormDBInfo)
{
	$bRC = False;
	$rec = new WARecordset();
	$rec->SetupFromGlobalCnn();
	if (!$rec->WAIsConnected())
	{
		$sSystemMessage .= WA_INSTALL_MYSQL_UNAVAILABLE."<br>";
		return $bRC;
	}
	else
	{
		switch ($FormDBInfo["WADBType"])
		{
			case WA_DB_MYSQL_ID:
				// Load DB Tables
				$bError = false;
				$sSql = "";
				$sSystemMessage .= "<p>Load database<br>";
				$fd = fopen($sDBDump, "r");
				while ($line = fgets($fd, 100000))
				{
					if (substr($line, 0, 2) != "--")
					{
						$line = str_replace("\n", "", $line);
						$sSql .= $line;
						if (substr($line, strlen($line)- 1) == ";")
						{
							$rec->WAOpenRecordset($sSql);
							
							if ($rec->WANumRows() > 0)
							{
								$bError = true;
								break;
							}
							$sSql = "";
						}
					}
				}
				if (!$bError)
					$bRC = true;
				fclose ($fd);
				break;
			default:
				$sSystemMessage .= WA_INSTALL_MYSQL_CREATE_ERR." ".$sDBName."<br>";
				$sSystemMessage .= WA_INSTALL_DBUNSUPPORTED;
		}
	}
	$rec->WACloseRecordset();
	return $bRC;
}

?>


Return current item: WebArchive