Location: PHPKode > scripts > Php AddressBook 2 > php-addressbook-2/setup/index.php
<?php
/*
Created by: Adam Patterson

This is a free aplication you can change what 
ever you like as long as you keep mention of my name. 

	Copyright (c) 2007, Adam Patterson
	http://www.adam-patterson.com | http://www.studiolounge.net
	Installer is released under the GPL license
	http://www.gnu.org/licenses/gpl.txt

This script is designed to let users create a config.php file used to connect 
to a MySQL DB and install the default MySQL into the DB.

*/

if (!is_writable('../')) die("Sorry, I can't write to the directory. You'll have to either change the permissions on your installation directory or create your config.php manually.");

if (isset($_GET['step']))
	$step = $_GET['step'];
else
	$step = 0;
header( 'Content-Type: text/html; charset=utf-8' );
?>
<!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>Installer &rsaquo; Setup Configuration File</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style media="screen" type="text/css">
<!--
html {
	background: #eee;
}
body {
	background: #fff;
	color: #000;
	font-family: Geneva, Arial, Helvetica, sans-serif;
	margin-left: 20%;
	margin-right: 20%;
	padding: .2em 2em;
}
h1 {
	color: #000000;
	font-size: 36px;
	font-weight: lighter;
	font-family: Geneva, Arial, Helvetica, sans-serif;
	text-decoration: underline;
}
h2 {
	font-size: 16px;
}
p, li, dt {
	line-height: 140%;
	padding-bottom: 2px;
}
ul, ol {
	padding: 5px 5px 5px 20px;
}
.step a, .step input {
	font-size: 14px;
}
td input {
	font-size: 14px;
}
.step, th {
	text-align: right;
}
#fsubmit {
	width: 67px;
	height: 23px;
	border: none;
	text-indent: -900em;
	line-height: 0px;
	background-image: url(button-submit.png);
	background-repeat: no-repeat;
}

#footer {
	text-align: center;
	border-top: 1px solid #ccc;
	padding-top: 1em;
	font-style: italic;
}
.step1 {		text-align: right;
}
-->
</style>
</head>
<body>
<h1>Installer</h1>
<?php
switch($step) {
	case 0:
// Check if config.php has been created
if (file_exists('../config.php'))
	die("<p>The file 'config.php' already exists. If you need to reset any of the configuration items in this file, please delete it first.</p></body></html>");
?>
<p>Welcome to your install. Before getting started, we need some information on the database. You will need to know the following items before proceeding.</p>
<ol>
  <li>Database name</li>
  <li>Database username</li>
  <li>Database password</li>
  <li>Database host</li>
</ol>
<p><strong>If for any reason this automatic file creation doesn't work, don't worry. All this does is fill in the database information to a configuration file. You may also simply open <code>config-sample.php</code> in a text editor, fill in your information, and save it as <code>config.php</code>. </strong></p>
<p>In all likelihood, these items were supplied to you by your Hosting Company. If you do not have this information, then you will need to contact them before you can continue. If you&#8217;re all ready, <a href="?step=1">let&#8217;s go</a>! </p>
<?php
	break;

	case 1:
	?>
</p>
<form method="post" action="index.php?step=2">
  <p>Below you should enter your database connection details. If you're not sure about these, contact your host. </p>
  <table>
    <tr>
      <th scope="row">Database Name</th>
      <td><input name="dbname" type="text" size="25" /></td>
      <td>The name of the database you want to run your script in. </td>
    </tr>
    <tr>
      <th scope="row">User Name</th>
      <td><input name="uname" type="text" size="25" /></td>
      <td>Your MySQL username</td>
    </tr>
    <tr>
      <th scope="row">Password</th>
      <td><input name="pwd" type="text" size="25" /></td>
      <td>Your MySQL password.</td>
    </tr>
    <tr>
      <th scope="row">Database Host</th>
      <td><input name="dbhost" type="text" size="25" /></td>
      <td>Most Likely  won't need to change this value.</td>
    </tr>
    <tr>
      <th scope="row">GMaps Key</th>
      <td><input name="googleapi" type="text" size="25" value="API Key" /></td>
      <td><a href="http://www.google.com/apis/maps/signup.html" target="_blank">http://www.google.com/apis/maps/signup.html</a></td>
    </tr>
    <tr>
      <th scope="row">Your E-mail</th>
      <td><input name="yourmail" type="text" size="25" value="E-mail" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th scope="row"> First Name </th>
      <td><input name="yourfirst" type="text" size="25" value="First Name" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th scope="row">Last Name </th>
      <td><input name="yourlast" type="text" size="25" value="Last Name" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>
  <h2 class="step1">
    <input name="submit" id= "fsubmit" type="submit" value="Submit" />
  </h2>
</form>
<?php
	break;	
	case 2:
	$dbname  = trim($_POST['dbname']);
    $dbuser   = trim($_POST['uname']);
    $dbpwd = trim($_POST['pwd']);
    $host  = trim($_POST['dbhost']);

    // We'll fail here if the values are no good.
    require_once('../open-db.php');
	$handle = fopen('../config.php', 'w');
	
$source = array (
"<? \n",
"$","dbname = 'databasename';	// The name of the database \n",
"$","dbuser = 'username'; 	// MySQL username \n",		
"$","dbpwd = 'password';	// MySQL Password \n",	
"$","host = 'localhost';	// Most likely you wont need to change this \n",
"$","key = 'googleapi';	  // Google Maps API Key\n",
"$","yourmail = 'youremail';	//E-Mail\n",
"$","yourfirst = 'yourfirstname';	//First Name\n",
"$","yourlast = 'yourlastname';	//Last Name\n",
"$","version = '2.5';       //General Information \n",
"$","pagesize = '20';       //Listings per page \n",
"?>" );

$search = array ( databasename, username, password, localhost, youremail, yourfirstname, yourlastname, googleapi);
$replace = array ($dbname, $dbuser, $dbpwd, $host, $yourmail, $yourfirst, $yourlast, $googleapi);

$source = str_replace ( $search, $replace, $source );
foreach ( $source as $str )
	fwrite($handle, $str);
?>
<form method="post" action="index.php?step=3">
    <p>Below you should enter your Username and Password</p>
  <table>
    <tr>
      <th scope="row">Username</th>
      <td><input name="username" type="text" size="25" value="User Name" /></td>
    </tr>
    <tr>
      <th scope="row">Password</th>
      <td><input name="password" type="text" size="25" value="Password" /></td>
    </tr>
    <tr>
      <th scope="row">E-Mail</th>
      <td><input name="aemail" type="text" size="25" value="E-Mail" /></td>
    </tr>
    <tr>
      <th scope="row">Page Size</th>
      <td><input name="pagesize" type="text" size="25" value="20" /></td>
    </tr>
  </table>
  <h2 class="step1">
    <input name="submit" id= "fsubmit" type="submit" value="Submit" />
  </h2>
</form>
<p>All right everything is ready to roll!</p>
<a href="?step=3">let&#8217;s install the MySQL</a>!
</p>
<?php
	break;
	case 3:
	
if (file_exists("../config.php")) {

// Prefix the table names. These already have `backticks` around them!
$db_schema = array();

/*
Insert your MySQL in the $db_schema[] array, simply cut and paste the 
$db_schema[] chunk leaving the opening " and closing ";

You can also put $variables in the MySQL and have the data carried over.
*/
$passwordHash = sha1(strip_tags($_POST['password']));
$username = $_POST['username'];
$pagesize = $_POST['pagesize'];
$aemail = $_POST['aemail'];

 $db_schema[] = "CREATE TABLE IF NOT EXISTS `addressbook` (
  `id` int(100) unsigned NOT NULL auto_increment,
  `firstname` varchar(255) NOT NULL default '',
  `lastname` varchar(255) NOT NULL default '',
  `address` text NOT NULL,
  `postalcode` varchar(12) NOT NULL default '',
  `city` text NOT NULL,
  `province` text NOT NULL,
  `country` text NOT NULL,
  `home` text NOT NULL,
  `mobile` text NOT NULL,
  `work` text NOT NULL,
  `fax` text NOT NULL,
  `email` text NOT NULL,
  `email2` text NOT NULL,
  `yahoo` varchar(255) NOT NULL default '',
  `msn` varchar(255) NOT NULL default '',
  `icq` varchar(255) NOT NULL default '',
  `website` varchar(255) NOT NULL default '',
  `profile` varchar(255) NOT NULL default '',
  `comments` text NOT NULL,
  `aim` varchar(250) NOT NULL default '',
  `skype` varchar(250) NOT NULL default '',
  `jabber` varchar(250) NOT NULL default '',
  `gtalk` varchar(250) NOT NULL default '',
  PRIMARY KEY  (`id`)
);";

$db_schema[] = "CREATE TABLE IF NOT EXISTS `members` (
  `id` int(4) NOT NULL auto_increment,
  `username` varchar(100) NOT NULL default '',
  `password` varchar(40) NOT NULL default '',
  `pagesize` varchar(100) NOT NULL default '',
  `aemail` varchar(250) default NULL,
  PRIMARY KEY  (`id`)
)";

$db_schema[] = "CREATE TABLE IF NOT EXISTS `groups` (
  `gid` int(100) NOT NULL auto_increment,
  `groups` varchar(250) NOT NULL default '',
  PRIMARY KEY  (`gid`)
);";


$db_schema[] = "INSERT INTO `groups` (`gid`, `groups`) VALUES (1, 'Un Assigned'),
(2, 'Friends'),
(3, 'Family'),
(4, 'Co-Workers');";

$db_schema[] = "ALTER TABLE `addressbook` ADD `gid` INT( 100 ) NOT NULL DEFAULT '1' AFTER `id` ;";
$db_schema[] = "ALTER TABLE `addressbook` ADD `email3` TEXT NOT NULL AFTER `email2` ;";
$db_schema[] = "ALTER TABLE `addressbook` ADD `work2` TEXT NOT NULL AFTER `work` ;";
$db_schema[] = "ALTER TABLE `addressbook` ADD `lat` VARCHAR( 50 ) NOT NULL AFTER `gtalk` ,ADD `lng` VARCHAR( 50 ) NOT NULL AFTER `lat` ;";
$db_schema[] = "ALTER TABLE `addressbook` ADD `suite` VARCHAR( 100 ) NOT NULL AFTER `address` ;";

// This line will insert the nessesary information for login and information that will be used further on down the road.
$db_schema[] = "INSERT INTO `members` (`id`, `username`, `password`, `pagesize`, `aemail`) VALUES (1, '$username', '$passwordHash', '$pagesize', '$aemail');";

  
require_once('../config.php');
require_once('../open-db.php');

      echo "<h3>Creating tables...</h3>";
	  echo "<h3>Updating tables...</h3>";
      foreach($db_schema as $sql) {
       mysql_query($sql);
      }
      echo "<h3>Done!</h3>";
	  echo "Make sure to CHMOD 777 the folders backup and profiles.";
  }
	
	break;
}
?>
<p id="footer"><a href="http://www.mapleflow.com" target="_blank">Maple Flow</a> installer.</p>
</body>
</html>
Return current item: Php AddressBook 2