Location: PHPKode > projects > Write2Left > install.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: install.php
| Description:
|   Installs Write2Left
| Created Aug-6-2003
+------------------------------------------------------
*/

// I ain't wussing out here.. :P
error_reporting  ( E_ALL );
set_magic_quotes_runtime( 0 );

// Lets load up the default scripts
require( './general.php' );
$func = new functions();

// Get the config if we've created it
if( file_exists( './config.php' ) )
{
	require( './config.php' );

	// Load up our database module
	require( "./Database/".$CONFIG['db_driver'].".php" );
	
	$db = new database(	$CONFIG['db_server'], 
						$CONFIG['db_user'], 
						$CONFIG['db_pass'],
						$CONFIG['db_name'],
						$CONFIG['db_prefix'] );
}

/* Class: W2L
 * Description:
 *   Info class to hold info about the current iteration of the script
 */
class W2L
{
	var $time_now = 0;
	var $settings = array();
	var $input    = array();
	var $log_id   = 0;

	function W2L()
	{
		global $CONFIG, $func;

		$this->time_now = time();
		$this->settings = $CONFIG;
		$this->input	= $func->safe_input( true );
	}

	function start()
	{
		if( is_writeable( './Uploads/' ) && is_writeable( '.' ) )
			print <<<EOF
<b>Welcome to the Write2Left Installer!</b>
<br /><br />
First, please enter your database information. If you are unsure as to what to 
enter, please contact your system administrator or web hosting provider.

<form action="install.php?Action=CreateDatabase" method="Post">
<table align="center" style="font-weight: bold; border:solid 1px #000" cellspacing="0" cellpadding="5">

<tr class="dark">
<td colspan="2">Database Information:</td>
</tr>

<tr>
<td class="bevel">Database Server Address</td>
<td class="bevel"><input type="text" name="db_server" value="localhost" /></td>
</tr>

<tr>
<td class="bevel">Database Username</td>
<td class="bevel"><input type="text" name="db_user" value="root" /></td>
</tr>

<tr>
<td class="bevel">Database Password</td>
<td class="bevel"><input type="text" name="db_pass" value="" /></td>
</tr>

<tr>
<td class="bevel">Database Name</td>
<td class="bevel"><input type="text" name="db_name" value="test_w2l" /></td>
</tr>

<tr>
<td class="bevel">Table Prefix</td>
<td class="bevel"><input type="text" name="db_prefix" value="w2l_" /></td>
</tr>

<tr><td colspan="2" align="center" class="bevel">
<input type="submit" value="Next Step &gt;" />
</td></tr>

</table>
</form>

EOF;
		else
			print $this->error( "The Install and/or Uploads folder's permissions 
								were not set properly. Please check the permissions 
								of these folders and ensure they are set to '777' or writeable." );
	
}
	
	function create_database()
	{
		global $W2L;
		
		if( !array_key_exists( 'db_server', $W2L->input ) )
		{
			print "Incorrect Action";
			return;
		}
		
		if( @mysql_connect( $W2L->input['db_server'],
							$W2L->input['db_user'],
							$W2L->input['db_pass'] ) == FALSE )
		{
			print $this->error( "A database connection could not be made.
								Please ensure that the database information 
								entered is correct." );
			
			return;
		}
		
		if( mysql_select_db( $W2L->input['db_name'] ) == FALSE )
		{
			if( array_key_exists( 'create', $W2L->input ) )
			{
				if( !mysql_query( "CREATE DATABASE IF NOT EXISTS {$W2L->input['db_name']}" ) )
				{
					print $this->error( "The database could not be created. Please
										consult your provider or administrator." );
					return;
				}
			}
			else
			{
				print $this->error( "
A database name entered does not correspond to an existing database on the server.
Please ensure that the database information entered is correct.<br /><br />
The installer can attempt to create the database for you. This may not be 
supported by your server, so check with your provider or administrator if an 
error is returned.<br />
<b><form action='install.php?Action=CreateDatabase&amp;create' name='create' method='Post'>
<input type='hidden' name='db_server' value='{$W2L->input['db_server']}' />
<input type='hidden' name='db_user'   value='{$W2L->input['db_user']}' />
<input type='hidden' name='db_pass'   value='{$W2L->input['db_pass']}' />
<input type='hidden' name='db_name'   value='{$W2L->input['db_name']}' />
<input type='hidden' name='db_prefix' value='{$W2L->input['db_prefix']}' />
<input type='submit' value='Create Database' />
</form></b>" );
			
				return;
			}
		}
		
		$contents = "<?php\n\n";
		
		$contents .= "\$CONFIG['debug'] = '0';\n";
		$contents .= "\$CONFIG['enable_gzip'] = '0';\n";
		$contents .= "\$CONFIG['session_timeout'] = '3600';\n";		
		$contents .= "\$CONFIG['db_driver'] = 'MySQL';\n";
		
		$contents .= "\$CONFIG['db_server'] = '{$W2L->input['db_server']}';\n";
		$contents .= "\$CONFIG['db_user'] = '{$W2L->input['db_user']}';\n";
		$contents .= "\$CONFIG['db_pass'] = '{$W2L->input['db_pass']}';\n";
		$contents .= "\$CONFIG['db_name'] = '{$W2L->input['db_name']}';\n";
		$contents .= "\$CONFIG['db_prefix'] = '{$W2L->input['db_prefix']}';\n";
		
		$contents .= "\n?>";
			
		$config_fp = fopen( 'config.php', 'wt' );
		if( $config_fp == FALSE )
		{
			print $this->error( "There was an error writing to the configuration file exists. 
							    Please check that this directory's permissions are set to '777' or writable." );
			return;
		}
		fwrite( $config_fp, $contents );
		fclose( $config_fp );
		
		print "Config file written successfully!<br /><br />
			  <a href='install.php?Action=CreateTables'>Next Step &gt;</a>" ;
	}

	function create_tables()
	{
		global $db;
		
		$db->query( "CREATE TABLE `w2l_categories` (
					  `category_id` mediumint(8) unsigned NOT NULL auto_increment,
					  `log_id` mediumint(8) unsigned NOT NULL default '0',
					  `name` varchar(128) NOT NULL default '',
					  `description` text,
					  `image` varchar(255) NOT NULL default '',
					  PRIMARY KEY  (`category_id`),
					  KEY `log_id` (`log_id`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_comments` (
					  `comment_id` int(11) NOT NULL auto_increment,
					  `post_id` int(11) NOT NULL default '0',
					  `log_id` mediumint(8) unsigned NOT NULL default '0',
					  `body` text NOT NULL,
					  `author_id` int(11) NOT NULL default '0',
					  `author_name` varchar(64) NOT NULL default '',
					  `author_email` varchar(100) NOT NULL default '',
					  `author_url` varchar(255) NOT NULL default '',
					  `ip_address` varchar(16) NOT NULL default '',
					  `time` int(11) NOT NULL default '0',
					  PRIMARY KEY  (`comment_id`),
					  KEY `author_id` (`author_id`),
					  KEY `post_id` (`post_id`,`log_id`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_editors` (
					  `link_id` tinyint(3) unsigned NOT NULL auto_increment,
					  `user_id` mediumint(8) unsigned NOT NULL default '0',
					  `log_id` mediumint(8) unsigned NOT NULL default '0',
					  `can_access` enum('0','1') NOT NULL default '0',
					  `can_post` enum('0','1') NOT NULL default '0',
					  `can_edit` enum('0','1') NOT NULL default '0',
					  `can_upload` enum('0','1') NOT NULL default '0',
					  `can_config` enum('0','1') NOT NULL default '0',
					  `can_template` enum('0','1') NOT NULL default '0',
					  `can_notify` enum('0','1') NOT NULL default '0',
					  `can_mail` enum('0','1') NOT NULL default '0',
					  `can_moderate` enum('0','1') NOT NULL default '0',
					  `can_editors` tinyint(3) unsigned NOT NULL default '0',
					  PRIMARY KEY  (`link_id`),
					  KEY `site_user` (`log_id`,`user_id`),
					  KEY `user_id` (`user_id`),
					  KEY `site_id` (`log_id`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_logs` (
					  `log_id` mediumint(8) NOT NULL auto_increment,
					  `name` varchar(96) NOT NULL default '0',
					  `path` tinytext NOT NULL,
					  `cache_path` tinytext NOT NULL,
					  `url` tinytext NOT NULL,
					  `timezone` tinyint(3) NOT NULL default '0',
					  `owner_id` mediumint(8) unsigned NOT NULL default '0',
					  `error_template` text,
					  `notify_template` text,
					  `display_type` varchar(10) NOT NULL default '',
					  `display_count` int(10) unsigned NOT NULL default '0',
					  `date_format` varchar(32) NOT NULL default '',
					  `def_category_id` smallint(5) unsigned NOT NULL default '0',
					  `arc_days` enum('0','1') NOT NULL default '0',
					  `arc_weeks` enum('0','1') NOT NULL default '0',
					  `arc_months` enum('0','1') NOT NULL default '0',
					  `arc_years` enum('0','1') NOT NULL default '0',
					  `anon_comments` enum('0','1') NOT NULL default '0',
					  `notify_from_mail` varchar(150) default NULL,
					  `notify_reply_mail` varchar(150) default NULL,
					  `notify_default` tinyint(3) unsigned NOT NULL default '1',
					  PRIMARY KEY  (`log_id`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_plugins` (
					  `plugin_id` mediumint(8) unsigned NOT NULL auto_increment,
					  `name` varchar(64) NOT NULL default '',
					  `filename` varchar(64) NOT NULL default '',
					  PRIMARY KEY  (`plugin_id`),
					  KEY `name` (`name`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_post_extra` (
					  `post_id` int(10) unsigned NOT NULL default '0',
					  `name` varchar(50) NOT NULL default '',
					  `value` text,
					  KEY `post_id` (`post_id`,`name`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_posts` (
					  `post_id` int(11) unsigned NOT NULL auto_increment,
					  `log_id` mediumint(8) unsigned NOT NULL default '0',
					  `author_id` mediumint(8) unsigned NOT NULL default '0',
					  `title` varchar(128) default NULL,
					  `body` text NOT NULL,
					  `extended` text,
					  `category_id` mediumint(8) unsigned default '0',
					  `time` int(11) unsigned NOT NULL default '0',
					  `state` varchar(10) default NULL,
					  `allow_comments` tinyint(3) unsigned NOT NULL default '0',
					  `allow_pings` tinyint(3) unsigned NOT NULL default '0',
					  `comment_count` mediumint(8) unsigned NOT NULL default '0',
					  `last_comment_name` varchar(64) NOT NULL default '',
					  `last_comment_time` int(11) NOT NULL default '0',
					  PRIMARY KEY  (`post_id`),
					  KEY `log_id` (`log_id`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_sessions` (
					  `sid` varchar(32) NOT NULL default '',
					  `user_name` varchar(32) NOT NULL default '',
					  `user_id` int(10) unsigned NOT NULL default '0',
					  `user_pass` varchar(64) NOT NULL default '',
					  `user_email` varchar(255) NOT NULL default '0',
					  `user_perms` varchar(255) NOT NULL default '',
					  `log_id` mediumint(8) unsigned NOT NULL default '0',
					  `log_name` varchar(96) NOT NULL default '',
					  `log_perms` tinytext NOT NULL,
					  `ip_address` varchar(16) NOT NULL default '',
					  `start_time` int(11) NOT NULL default '0',
					  `last_time` int(11) NOT NULL default '0',
					  `last_action` varchar(32) NOT NULL default '',
					  PRIMARY KEY  (`sid`)
					) TYPE=MyISAM;" );
					
		$db->query( "CREATE TABLE `w2l_subs` (
					  `sub_id` mediumint(8) unsigned NOT NULL auto_increment,
					  `log_id` mediumint(8) unsigned NOT NULL default '0',
					  `name` varchar(32) default NULL,
					  `address` varchar(150) NOT NULL default '',
					  `type` varchar(16) NOT NULL default '',
					  PRIMARY KEY  (`sub_id`),
					  KEY `log_id` (`log_id`)
					) TYPE=MyISAM;" );
										
		$db->query( "CREATE TABLE `w2l_templates` (
					  `temp_id` int(10) unsigned NOT NULL auto_increment,
					  `log_id` int(10) unsigned NOT NULL default '0',
					  `name` varchar(32) NOT NULL default '',
					  `filename` varchar(32) NOT NULL default '',
					  `data` text NOT NULL,
					  PRIMARY KEY  (`temp_id`),
					  KEY `set_id` (`log_id`)
					) TYPE=MyISAM;" );
		
		$db->query( "CREATE TABLE `w2l_users` (
					  `user_id` int(10) unsigned NOT NULL auto_increment,
					  `name` varchar(32) NOT NULL default '',
					  `pass` varchar(64) NOT NULL default '',
					  `email` varchar(255) default NULL,
					  `creator` varchar(32) default NULL,
					  `can_login` enum('0','1') default NULL,
					  `can_logs` enum('0','1') default NULL,
					  `can_users` enum('0','1') default NULL,
					  `can_options` enum('0','1') default NULL,
					  `can_install` enum('0','1') default NULL,
					  `can_import` enum('0','1') default NULL,
					  PRIMARY KEY  (`user_id`)
					) TYPE=MyISAM;" );
		
		print "Database Tables Created!<br /><br />
			  <a href='install.php?Action=CreateAdmin'>Next Step &gt;</a>" ;
	}

	function create_admin( $error = '' )
	{
		if( $error != '' )
			$error = "<tr><td class='bevel' colspan='2'><font color='red'>$error</font></td><tr>";
			
		print <<<EOF
Now you must save your login information. Without this step, no one will be able to log in to
Write2Left. Ensure that all information is entered properly.
<form action="install.php?Action=SaveAdmin" method="Post">
<table align="center" style="font-weight: bold; border:solid 1px #000" cellspacing="0" cellpadding="5">

<tr class="dark">
<td colspan="2">User Information:</td>
</tr>

<tr>
<td class="bevel">Username</td>
<td class="bevel"><input type="text" name="user_name" /></td>
</tr>

<tr>
<td class="bevel">Password</td>
<td class="bevel"><input type="password" name="user_pass" /></td>
</tr>

<tr>
<td class="bevel">Password Confirm</td>
<td class="bevel"><input type="password" name="user_confirm" /></td>
</tr>

<tr>
<td class="bevel">Email</td>
<td class="bevel"><input type="text" name="user_email" /></td>
</tr>

<tr><td colspan="2" align="center" class="bevel">
<input type="submit" value="Next Step &gt;" />
</td></tr>

</table>

</form>
EOF;
	}

	function save_admin()
	{
		global $W2L, $db;
		
		if( $W2L->input['user_pass'] != $W2L->input['user_confirm'] )
		{
			$this->create_admin( "Passwords did not match" );
			return;	
		}
		
		$db->query( "INSERT INTO w2l_users VALUES 
							( NULL,
							  '{$W2L->input['user_name']}', 
							  MD5('{$W2L->input['user_pass']}'), 
							  '{$W2L->input['user_email']}', 
							  '', '1', '1', '1', '1', '1', '1' )" );
							  
		print "User Created!<br /><br />
			  You may now <a href='.'>log in to Write2Left</a>" ;
		
	}
	
	function error( $errtext )
	{
		return "<font color='red'><big><b>Error!</b></big><br /><br />$errtext</font>";	
	}
}

// And then we create our main object
$W2L = new W2L();

// Check if this is the begining
if( !array_key_exists( 'Action', $W2L->input ) )
    $W2L->input['Action'] = 'Start';
    
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Write2Left :: Install</title>
<link rel="stylesheet" href="./Skin/files/main.css" type="text/css" />
</head>

<body>
<table width="100%" cellpadding="0" cellspacing="0">
<tr class="logo">
<td class="logo" colspan="3" valign="top">
&nbsp;<a href="http://write2left.com"><img src="./Skin/files/logo.png" alt="logo!" /></a>
</td>
</tr>

<tr class="header">
<td colspan="3">Installer</td>
</tr>

<tr class="spacer"><td colspan="3">&nbsp;</td></tr>

<tr>
<td class="space"></td>

<td class="main" valign="top"> 
<?php

if( file_exists( './install.lock' ) )
{
			print <<<EOF
<font color="red">
<big><b>Error!</b></big>
<br /><br />
This installer is locked. If you wish to re-run the installer, please
remove the 'install.lock' file from your Write2Left installation folder.</font>	
EOF;
}
else 
{
	// Head out in the right direction
	switch( $W2L->input['Action'] )
	{
		case 'Start':
			$W2L->start();
			break;
		case 'CreateDatabase':
			$W2L->create_database();
			break;
		case 'CreateTables':
			$W2L->create_tables();
			break;
		case 'CreateAdmin':
			$W2L->create_admin();
			break;
		case 'SaveAdmin':
			$W2L->save_admin();
			break;
		default:
			print "Incorrect action";
	}
}
?>

</td>

<td class="space"></td>
</tr>

<tr class="spacer"><td colspan="3">&nbsp;</td></tr>

<tr class="header"><td colspan="3">
&nbsp;Copyright <a href="http://timdorr.com" class="header">timdorr.com</a> scripting
</td></tr>

<table>
</body>
</html>
Return current item: Write2Left