Location: PHPKode > projects > LySite > lysite/install.php
<?php
/*----------------------------------------------------------

       LySite
       Brad Landis (hide@address.com)

   Copyright (C) 2005 Brad Landis

   This program is free software; you can redistribute it
   and/or modify it under the terms of the GNU General
   Public License as published by the Free Software
   Foundation; either version 2 of the License, or (at your
   option) any later version.

   This program 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 General Public License
   for more details.
   
   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth
   Floor, Boston, MA  02110-1301, USA.

----------------------------------------------------------*/
ob_start();
?>
<html>
<head>
<title>Installing LySite</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Installing LySite</h1>
<hr>
<?php
if(isset($_POST['step2_5'])) {
	@mysql_connect($_POST['DB_HOST'], $_POST['DB_USER'], $_POST['DB_PASS']) or die("Database connection failed: " . mysql_error());
	@mysql_select_db($_POST['DB_NAME']) or die("Database failed to load: ".mysql_error());
	
	$query = "SELECT * FROM ".$_POST['DB_PREFIX']."users LIMIT 1";
	if(mysql_query($query)) {
		echo "The tables have been successfully created.\n";
		echo "<form action='setup.php' method='post'>\n";
		foreach($_POST as $k=>$v) {
			if(strtoupper($k) == $k)
				echo "<input type='hidden' name='$k' value='$v'>\n";
		}
		echo "<input type='submit' name='step3' value='Next Step'></form>\n";
	} else {
		echo "<p>Error: ".mysql_error()."</p>\n";
		echo "<p>SQL Statement: ".$query."";
		echo "<p>Please return to previous step and create the MySQL tables.</p>\n";
	}
} else if(isset($_POST['step2'])) {
	if($_POST['DB_PREFIX'] != "" && substr($_POST['DB_PREFIX'],-1) != "_")
		$_POST['DB_PREFIX'] .= "_";
	@mysql_connect($_POST['DB_HOST'], $_POST['DB_USER'], $_POST['DB_PASS']) or die("Database connection failed: " . mysql_error());
	@mysql_select_db($_POST['DB_NAME']) or die("Database failed to load: ".mysql_error());
	
	echo "<h3>Creating Tables</h3>\n";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]album_tracks` ( `tid` smallint(6) NOT NULL auto_increment, `aid` smallint(6) NOT NULL default '0', `lid` smallint(6) NOT NULL default '0', `track_num` tinyint(2) NOT NULL default '0', `user_id` smallint(6) NOT NULL default '1', PRIMARY KEY (`tid`), UNIQUE KEY `aid` (`aid`,`lid`,`track_num`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]albums` ( `aid` smallint(6) NOT NULL auto_increment, `album_title` varchar(60) NOT NULL default '', `album_artist` varchar(40) NOT NULL default '', `album_art` varchar(12) NOT NULL default '', `user_id` smallint(6) NOT NULL default '1', `approved` enum('y','n') NOT NULL default 'n', PRIMARY KEY (`aid`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]attachments` ( `attachment_id` smallint(6) NOT NULL auto_increment, `filename` varchar(45) default NULL, `url` varchar(80) default NULL, `lid` smallint(6) NOT NULL default '0', `user_id` smallint(6) NOT NULL default '0', `approved` enum('y','n') NOT NULL default 'n', PRIMARY KEY (`attachment_id`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]category_link` ( `cat_id` smallint(6) NOT NULL auto_increment, `cat_names_id` tinyint(3) NOT NULL default '0', `lid` smallint(6) NOT NULL default '0', `user_id` smallint(6) NOT NULL default '0', PRIMARY KEY (`cat_id`), UNIQUE KEY `cat_names_id` (`cat_names_id`,`lid`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]category_names` ( `cat_names_id` tinyint(3) NOT NULL auto_increment, `cat_name` varchar(50) NOT NULL default '', `user_id` smallint(6) NOT NULL default '0', `approved` enum('y','n') NOT NULL default 'n', PRIMARY KEY (`cat_names_id`), UNIQUE KEY `cat_name` (`cat_name`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]comments` ( `comment_id` smallint(7) NOT NULL auto_increment, `date` datetime NOT NULL default '0000-00-00 00:00:00', `user_id` smallint(6) NOT NULL default '0', `lid` smallint(6) NOT NULL default '0', `comment` text NOT NULL, `reported_by` smallint(6) default NULL, PRIMARY KEY (`comment_id`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]email_archives` ( `month` date NOT NULL default '0000-00-00', `message` text NOT NULL, PRIMARY KEY (`month`)) TYPE=MyISAM;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]favs` ( `fav_id` smallint(6) NOT NULL auto_increment, `lid` smallint(6) NOT NULL default '0', `user_id` smallint(6) NOT NULL default '0', PRIMARY KEY (`fav_id`), UNIQUE KEY `lid` (`lid`,`user_id`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]log` ( `log_id` smallint(6) NOT NULL auto_increment, `time` datetime NOT NULL default '0000-00-00 00:00:00', `level` tinyint(3) NOT NULL default '1', `user_id` smallint(6) default NULL, `ip_addr` int(11) NOT NULL default '0', `url` varchar(100) default NULL, `title` varchar(100) NOT NULL default '', `desc` text, PRIMARY KEY (`log_id`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]lyrics` ( `lid` smallint(6) NOT NULL auto_increment, `title` varchar(45) NOT NULL default '', `author` varchar(100) default NULL, `text` text NOT NULL, `user_id` smallint(6) NOT NULL default '1', `copyright` varchar(120) NOT NULL default '', `approved` enum('y','n') NOT NULL default 'n', `monthly_notified` enum('y','n') NOT NULL default 'n', PRIMARY KEY (`lid`), FULLTEXT KEY `title` (`title`,`text`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]options` ( `k` varchar(60) NOT NULL default '', `v` text, PRIMARY KEY (`k`)) TYPE=MyISAM;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]top_searches` ( `search_id` smallint(6) NOT NULL auto_increment, `search_term` varchar(125) NOT NULL default '', `date` date NOT NULL default '0000-00-00', `ip` int(11) NOT NULL default '0', PRIMARY KEY (`search_id`)) TYPE=MyISAM ;";
	$query[] = "CREATE TABLE `$_POST[DB_PREFIX]users` ( `user_id` smallint(6) NOT NULL auto_increment, `username` varchar(40) NOT NULL default '', `password` varchar(16) NOT NULL default '', `name` varchar(50) NOT NULL default '', `email` varchar(60) NOT NULL default '', `permissions` smallint(3) NOT NULL default '5', `subscribe` enum('y','monthly','n') NOT NULL default 'n', `rand32` varchar(32) NOT NULL default '', PRIMARY KEY (`user_id`), UNIQUE KEY `username` (`username`)) TYPE=MyISAM ;";
	$manual_create = false;
	foreach($query as $q) {
		if(mysql_query($q)) {
			echo "<p>".$q."<br>\n";
			echo "Success\n";
		} else if (strpos(mysql_error(), "Access denied") !== false) {
			echo "<p>You are not allowed to create tables. Please use PHPMyAdmin or a similar tool to execute the following SQL Statements:</p>\n";
			$manual_create = true;
			break;
		} else {
			echo "<p>".$q."<br>\n";
			echo "Error: ".mysql_error()."\n";
			$err = true;
		}
	}
	if($manual_create == true) {
		echo "<code style='display: block; font: monospace 10px; background: #eee; border: solid 1px #ddd; padding: 6px;'>\n";
		foreach($query as $q) {
			echo "$q<br/><br/>\n";
		}
		echo "</code>\n";
		
		echo "<form action='install.php' method='post'>\n";
		foreach($_POST as $k=>$v) {
			if(strtoupper($k) == $k)
				echo "<input type='hidden' name='$k' value='$v'>\n";
		}
		echo "<input type='submit' name='step2_5' value='Next Step'></form>\n";
	} else if($err != true) {
		echo "<form action='setup.php' method='post'>\n";
		foreach($_POST as $k=>$v) {
			if(strtoupper($k) == $k)
				echo "<input type='hidden' name='$k' value='$v'>\n";
		}
		echo "<input type='submit' name='step3' value='Next Step'></form>\n";
	} else {
		echo "<p>There was a problem while trying to create tables.\n";
	}
} else {
	if(!is_writeable("options.php")) echo "<p>Note: \"options.php\" is not writeable. Installation will be easier if it is.\n";
	if(!is_writeable("uploads/test271912.jpg")) echo "<p>Please make sure the 'uploads' directory is writeable by the server.\n";
	if(!is_writeable("album_art/test271912.jpg")) echo "<p>Please make sure the 'album_art' directory is writeable by the server.\n";
	
	echo "<form method='post' action='install.php'>\n";
	echo "<fieldset><legend>Database Connection</legend>\n";
	echo "<p><label>Host</label><br>		<input type='text' name='DB_HOST'>\n";
	echo "<p><label>Database Name</label><br>	<input type='text' name='DB_NAME'>\n";
	echo "<p><label>Username</label><br>		<input type='text' name='DB_USER'>\n";
	echo "<p><label>Password</label><br>		<input type='password' name='DB_PASS'>\n";
	echo "<p><label>Table Prefix: (if you're only using one database for your site)</label><br> <input type='text' name='DB_PREFIX'>\n";
	echo "</fieldset>\n";
	echo "<input type='submit' name='step2' value='Next Step'>\n";
	echo "</form>\n";
}
?>
</body>
</html>
Return current item: LySite