Location: PHPKode > projects > Apartment Classfieds > apartment_classifieds_script/install.php
<?php
	// make sure script wasn't instaled already
	if (file_exists('user_settings.php'))
	{
		die('Script already installed, edit user_settings.php to change values');
	}

	// make sure script can write the user config file
	if(file_put_contents('test_write.tmp', ' ') === false)
	{
		die('
		<div style="width: 500px; padding: 10px; border: 1px solid black">
		<div style="color: red; font-weight: bold;">
		<p>Installation cannot continue (no write access)</b><p></div>
		<hr>
		<p>To install this script, a configuration file needs to be written,
		please give the web server permission to create a file in this
		directory. Contact the tech support department of your web host for 
		assistance.</p>
			
		<p>After installation, this script does not need to write files, so
		it should be safe to restore settings back to what they were.</p>
		</div>');
	}
?>

<html>
<head>
<title>Install</title>
<style type="text/css">
body
{
	font-family: sans;
}

input
{
	width: 100%;
}

table
{
	border-top: 1px solid #ccc;
	border-left: 1px solid #ccc;
}

td
{
	vertical-align: top;
	padding: 10px;
	border-bottom: 1px solid #ccc;
	border-right: 1px solid #ccc;
}
</style>
</head>
<body>
<?php
	// deletes files as necessary
	function cleanup($install_success)
	{
		// delete the text write file
		unlink('test_write.tmp');

		if(!$install_success)
		{
			// delete the incorrect settings file so they can go back and try again
			unlink('user_settings.php');
		}
	}

	if (!isset($_POST['submit']))
	{
		echo '
		<center><h1>Free Apartment Classifieds Script Installation</h1></center>
		<form action="install.php" method="POST">
		<table width="700" align="center" cellspacing="0" cellpadding="0">
		<tr>
		<td width="200">Site title</td><td><input type="text" name="site_title" value="Example.com"></td>
		</tr>
		<tr>
		<td>Site tagline</td><td><input type="text" name="site_tagline" value="Free Apartment Classified Ads"></td>
		</tr>
		<tr>
		<td>Results per page</td><td><input type="text" name="results_per_page" value="7"></td>
		</tr>
		<tr>
		<td>Database user</td><td><input type="text" name="db_user" value="root"><br>Your MySQL database username.</td>
		</tr>
		<tr>
		<td>Database password</td><td><input type="text" name="db_pass" value="root"><br>Your MySQL database password.</td>
		</tr>
		<tr>
		<td>Database hostname</td><td><input type="text" name="db_host" value="localhost"><br>Your MySQL database hostname.</td>
		</tr>
		<tr>
		<td>Database name</td><td><input type="text" name="db_name" value="apartments"><br>Your MySQL database.</td>
		</tr>
		<tr>
		<td>Database table prefix</td><td><input type="text" name="db_table_prefix" value=""><br>
		If you are installing this script multiple times, or have many scripts using the same database, type in something unique for this install.
<p>Note: do not put anything other than numbers and letters in this field.</td>
		</tr>
		<tr>
		<td>Administration password</td><td><input type="text" name="admin_password" value="password"></td>
		</tr>
		<tr>
		<td>Insert threshold</td><td><input type="text" name="insert_threshold" value="120"><br>
		Minimum time in seconds between posts for an IP address.</td>
		</tr>
		<tr>
		<td>Click the button to install:</td><td><input type="submit" name="submit" value="Install" style="width: 100px; background-color: darkgreen; border: 3px outset black; color: white;"></td>
		</tr>
		</table>
		</form>';
	}
	else
	{
		$settings = null;
		$create_table_failure = null;
		$db = null;
		$results = null;
		$sql = null;
		$statement = null;

		// write the settings file

		$settings = "
			<?php
			define('TEMPLATE_FILE', 'template/template_main.html');
			define('TEMPLATE_FILE_TAC', 'template/template_tac.html');
			define('SITE_TITLE', '".$_POST['site_title']."');
			define('SITE_TAGLINE', '".$_POST['site_tagline']."');
			define('RESULTS_PER_PAGE', '".$_POST['results_per_page']."');
			define('DB_USER', '".$_POST['db_user']."');
			define('DB_PASS', '".$_POST['db_pass']."');
			define('DB_SOURCE', 'mysql:dbname=".$_POST['db_name'].";host=".$_POST['db_host']."');
			define('INSERT_THRESHOLD', ".$_POST['insert_threshold'].");
			define('DB_TABLE_PREFIX', '".$_POST['db_table_prefix']."');
			define('ADMIN_PASSWORD','".$_POST['admin_password']."');
			?>";
	
		file_put_contents('user_settings.php',$settings);

		echo '1. Writing configuration file - SUCCESS';

		// create the tables

		require_once('globals.php');
		require_once('class_database.php');
		$create_table_failure = false;
	
		$db = new Database();

		if(!$db->connect())
		{
			cleanup(false);
			die('<p>2. Connecting to database - FAIL - please press back, check your database settings and try again</p>');
		}

		echo '<p>2. Connecting to database - SUCCESS</p>';
		
		$results = array();
		$sql = file_get_contents('install.sql');
		$sql = str_replace('__DB_TABLE_PREFIX__',DB_TABLE_PREFIX,$sql); // put in table prefix

		try
		{
			$statement = $db->get_handle()->prepare($sql);
			$statement->execute();
		}
		catch (Exception $exc)
		{	
			$create_table_failure = true;
		}

		if ($create_table_failure)
		{
			cleanup(false);
			die('<p>3. Creating database tables - FAIL - please press back, check your database user permissions and try again<p>');
		}

		echo '<p>3. Creating database tables - SUCCESS</p>

		<p><a href="index.php">You can now click here to view the installation.</a></p>';

		cleanup(true);
	}
?>
</body>
</html>

Return current item: Apartment Classfieds