<?php
/*
   +----------------------------------------------------------------------+
   | About Page                                                           |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox 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 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox 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 Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Installation script						  |
   +----------------------------------------------------------------------+
*/

#Set Variables
$steps[1] = Array('ffmpeg_path','qt_faststart_path','mediainfo_path','convert_path','identify_path','nice_path');
$steps[2] = Array('db_host','db_port','db_name','db_user','db_pass');
$steps[3] = Array('path','full_site','domain','cookie_domain','media_path','media_path_modified','dump_dir');
$steps[4] = Array('admin_email','admin_password');
$steps[5] = Array('site_title','text_copyright');
$last_step = count($steps)+1;

#The following fields are stored as arrays
$array_fields = Array('footer_links','favorite_photos','video_files','default_prefix','acceptable_media');

#Set Text and Title
$text['ffmpeg_path'] = 'Path to ffmpeg <a href="http://www.ffmpeg.org/" title="FFMPEG">http://www.ffmpeg.org/</a>';
$title['ffmpeg_path'] = 'ffmpeg location';
$text['qt_faststart_path'] = 'Path to qt_faststart, a component of ffmpeg, to make videos streamable';
$title['qt_faststart_path'] = 'qt_faststart location';
$text['mediainfo_path'] = 'Path to mediainfo <a href="http://mediainfo.sourceforge.net/" title="mediainfo">http://mediainfo.sourceforge.net/</a>';
$title['mediainfo_path'] = 'mediainfo location';
$text['convert_path'] = 'Path to Imagemagick convert to make thumbnails <a href="http://www.imagemagick.org/script/index.php" title="Imagemagick">http://www.imagemagick.org/script/index.php</a>';
$title['convert_path'] = 'convert location';
$text['identify_path'] = 'Path to Imagemagick identify to make signatures <a href="http://www.imagemagick.org/script/index.php" title="Imagemagick">http://www.imagemagick.org/script/index.php</a>';
$title['identify_path'] = 'signature location';
$text['nice_path'] = 'Path to nice (*nix only) to prioritize image resizing';
$title['nice_path'] = 'Path to nice';

$text['db_host'] = 'The location of your database server.  This is usually "localhost."';
$title['db_host'] = 'MySQL Host';
$text['db_port'] = 'Port of your MySQL server.  Default is 3306';
$title['db_port'] = 'MySQL Port';
$text['db_name'] = 'Name of your database';
$title['db_name'] = 'MySQL Database Name';
$text['db_user'] = 'The user to connect with';
$title['db_user'] = 'MySQL Username';
$text['db_pass'] = 'The MySQL password';
$title['db_pass'] = 'MySQL Password';

$text['path'] = 'The location of your Lifebox php scripts.  Trailing slash necessary.';
$title['path'] = 'Full path to Lifebox';
$text['full_site'] = 'The full url where your site will be accessed';
$title['full_site'] = 'Full URL to Lifebox';
$text['domain'] = 'Your domain for e-mails sent from the website';
$title['domain'] = 'E-Mail domain';
$text['cookie_domain'] = 'The domain for which cookies should be valid';
$title['cookie_domain'] = 'Cookie domain';
$text['media_path'] = 'This is where your photos & videos are located.  No trailing slash.';
$title['media_path'] = 'Media directory (Originals)';
$text['media_path_modified'] = 'If you have a separate directory for modified files, specify it here.  No trailing slash.';
$title['media_path_modified'] = 'Media directory (Modified)';
$text['dump_dir'] = 'The directory where lifebox should store its 3 cache directories.  Trailing slash necessary.';
$title['dump_dir'] = 'Lifebox cache base';

$text['admin_password'] = 'This is the password you will use to access the site';
$title['admin_password'] = 'Choose a password';
$text['admin_email'] = 'This is your e-mail address.  Since your e-mail is well known by your friends, you\'ll enter using your password';
$title['admin_email'] = 'Your e-mail address';

$text['text_copyright'] = 'Copyright text to be displayed in the footer of your site';
$title['text_copyright'] = 'Copyright footer';
$text['site_title'] = 'The title of your website';
$title['site_title'] = 'Site title';
#Check if setup configuration file exists
if (file_exists('../config/config.sample.php')) {
        include '../config/config.sample.php';
        $CONFIG_EXAMPLE = $CONFIG;
	$SETTINGS = $CONFIG;
} else {
        $ERROR = 'Could not locate config.sample.php';
}

#Check if existing configuration exists
if (!isset($ERROR) AND file_exists('../config/config.inc.php')) {
        include '../config/config.inc.php';
	$SETTINGS = $CONFIG;
        $new_installation = false;
	unset($CONFIG);
} else {
        $new_installation = true;
}

#Grab the POST
if (isset($_POST)) {
	foreach ($_POST as $key => $value) {
		$SETTINGS[$key] = stripslashes($value);
	}
}

if (!isset($_GET['stage'])) {
	$stage = false;
} else {
	$stage = $_GET['stage'];
}

#Construct STEP 6 -- Everything else
if (isset($SETTINGS)) {
	foreach ($SETTINGS as $key=>$value) {
		if (!in_array($key,$steps[1]) AND
		    !in_array($key,$steps[2]) AND
		    !in_array($key,$steps[3]) AND
		    !in_array($key,$steps[4]) AND
		    !in_array($key,$steps[5]) AND
		    $key !== 'email_signature') {
			$steps[6][] = $key;
		}
	}
}

#Check user input
$ERROR = false;

#Check for password
if ((!empty($SETTINGS['admin_password']) AND !isset($SETTINGS['password'])) OR
     !empty($SETTINGS['admin_password']) AND $SETTINGS['admin_password'] !== $SETTINGS['password']) {

	if (!$new_installation) {
		$stage = 0;
	}
}

if ($stage == 1) {

	#Check for the default locations of all executables.
	if (file_exists('/usr/local/bin/ffmpeg')) {
		$SETTINGS['ffmpeg_path'] = '/usr/local/bin/ffmpeg';
		$VERIFIED['ffmpeg_path'] = true;
	} elseif (file_exists('/usr/bin/ffmpeg')) {
		$SETTINGS['ffmpeg_path'] = '/usr/bin/ffmpeg';
		$VERIFIED['ffmpeg_path'] = true;
	} else {
		$SETTINGS['ffmpeg_path'] = '';
		$HIGHLIGHT['ffmpeg_path'] = true;
		$text['ffmpeg_path'] = '<strong>Could not automatically locate ffmpeg.  Make sure you followed the INSTALL directions.</strong>';
	}
	if (file_exists('/usr/local/bin/qt-faststart')) {
		$SETTINGS['qt_faststart_path'] = '/usr/local/bin/qt-faststart';
		$VERIFIED['qt_faststart_path'] = true;
	} elseif (file_exists('/usr/bin/qt-faststart')) {
		$SETTINGS['qt_faststart_path'] = '/usr/bin/qt-faststart';
		$VERIFIED['qt_faststart_path'] = true;
	} else {
		$SETTINGS['qt_faststart_path'] = '';
		$HIGHLIGHT['qt_faststart_path'] = true;
		$text['qt_faststart_path'] = '<strong>Could not automatically locate qt-faststart.  Make sure you followed the INSTALL directions.</strong>';
	}
	if (file_exists('/usr/local/bin/mediainfo')) {
		$SETTINGS['mediainfo_path'] = '/usr/local/bin/mediainfo';
		$VERIFIED['mediainfo_path'] = true;
	} elseif (file_exists('/usr/bin/mediainfo')) {
		$SETTINGS['mediainfo_path'] = '/usr/bin/mediainfo';
		$VERIFIED['mediainfo_path'] = true;
	} else {
		$SETTINGS['mediainfo_path'] = '';
		$HIGHLIGHT['mediainfo_path'] = true;
		$text['mediainfo_path'] = '<strong>Could not automatically locate mediainfo.  Make sure you followed the INSTALL directions.</strong>';
	}
	if (file_exists('/usr/local/bin/convert')) {
		$SETTINGS['convert_path'] = '/usr/local/bin/convert';
		$VERIFIED['convert_path'] = true;
	} elseif (file_exists('/usr/bin/convert')) {
		$SETTINGS['convert_path'] = '/usr/bin/convert';
		$VERIFIED['convert_path'] = true;
	} else {
		$SETTINGS['convert_path'] = '';
		$HIGHLIGHT['convert_path'] = true;
		$text['convert_path'] = '<strong>Could not automatically locate convert.  Make sure you followed the INSTALL directions.</strong>';
	}
	if (file_exists('/usr/local/bin/identify')) {
		$SETTINGS['identify_path'] = '/usr/local/bin/identify';
		$VERIFIED['identifty_path'] = true;
	} elseif (file_exists('/usr/bin/identify')) {
		$SETTINGS['identify_path'] = '/usr/bin/identify';
		$VERIFIED['identify_path'] = true;
	} else {
		$SETTINGS['identify_path'] = '';
		$HIGHLIGHT['identify_path'] = true;
		$text['identify_path'] = '<strong>Could not automatically locate identify.  Make sure you followed the INSTALL directions.</strong>';
	}
	if (file_exists('/usr/local/bin/nice')) {
		$SETTINGS['nice_path'] = '/usr/local/bin/nice';
		$VERIFIED['nice_path'] = true;
	} elseif (file_exists('/usr/bin/nice')) {
		$SETTINGS['nice_path'] = '/usr/bin/nice';
		$VERIFIED['nice_path'] = true;
	} elseif (file_exists('/bin/nice')) {
		$SETTINGS['nice_path'] = '/bin/nice';
	} else {
		$SETTINGS['nice_path'] = '';
		$HIGHLIGHT['nice_path'] = true;
		$text['nice_path'] = '<strong>Could not automatically locate nice.  This step is optional</strong>';
	}

} 

if ($stage == 2) {
	$link = @mysql_connect($SETTINGS['db_host'].':'.$SETTINGS['db_port'],$SETTINGS['db_user'],$SETTINGS['db_pass']);
	$db_selected = @mysql_select_db($SETTINGS['db_name'], $link);
	$error = mysql_error();	
	if (empty($error)) {
		$VERIFIED['db_host'] = true;
		$VERIFIED['db_port'] = true;
		$VERIFIED['db_name'] = true;
		$VERIFIED['db_user'] = true;
		$VERIFIED['db_pass'] = true;
	}
	
}
		
if ($stage == 2 AND isset($_POST)) {
        if (!file_exists($SETTINGS['ffmpeg_path'])) {
                $ERROR[] = 'ffmpeg could not be found at the below path';
                $HIGHLIGHT['ffmpeg_path'] = true;
        } else {
		$VERIFIED['ffmpeg_path'] = true;
	}

	if (!file_exists($SETTINGS['qt_faststart_path'])) {
                $ERROR[] = 'qt-faststart could not be found at the below path';
                $HIGHLIGHT['qt_faststart_path'] = true;
        } else {
		$VERIFIED['qt_faststart_path'] = true;
	}

        if (!file_exists($SETTINGS['mediainfo_path'])) {
                $ERROR[] = 'mediainfo could not be found at the below path';
                $HIGHLIGHT['mediainfo_path'] = true;
        } else {
		$VERIFIED['mediainfo_path'] = true;
	}

        if (!file_exists($SETTINGS['convert_path'])) {
                $ERROR[] = 'convert could not be found at the below path';
                $HIGHLIGHT['convert_path'] = true;
        } else {
		$VERIFIED['convert_path'] = true;
	}

        if (!file_exists($SETTINGS['identify_path'])) {
                $ERROR[] = 'identify could not be found at the below path';
                $HIGHLIGHT['identify_path'] = true;
        } else {
		$VERIFIED['identify_path'] = true;
	}

        if (!file_exists($SETTINGS['nice_path']) AND !empty($SETTINGS['nice_path'])) {
                $ERROR[] = 'nice could not be found at the below path';
                $HIGHLIGHT['nice_path'] = true;
        } else {
		$VERIFIED['nice_path'] = true;
	}

}

if ($stage == 3 AND isset($_POST)) {

	#Check that All fields except password
	#Are complete
	if (empty($_POST['db_host'])) {
		$ERROR[] = 'You must specify a host';
		$HIGHLIGHT['db_host'] = true;
	}
	if (empty($_POST['db_port'])) {
		$ERROR[] = 'You must specify a port';
                $HIGHLIGHT['db_port'] = true;
	}
	if (empty($_POST['db_name'])) {
                $ERROR[] = 'You must specify a name for the database';
                $HIGHLIGHT['db_name'] = true;
        } 
	if (empty($_POST['db_user'])) {
                $ERROR[] = 'You must specify a username to connect with';
                $HIGHLIGHT['db_user'] = true;
        }

	#Check Database connectivity (database)
	if (!$ERROR) {
		$link = @mysql_connect($SETTINGS['db_host'].':'.$SETTINGS['db_port'],$SETTINGS['db_user'],$SETTINGS['db_pass']);
		if (!$link) {
			$ERROR[] = 'Could not connect to database using the username and password provided.  Check your settings.';
			$HIGHLIGHT['db_user'] = true;
			$HIGHLIGHT['db_pass'] = true;
		} else {
			$VERIFIED['db_user'] = true;
			$VERIFIED['db_pass'] = true;	
		}
	}

	#Check Database connectivity (table)
        if (!$ERROR) {
		$db_selected = @mysql_select_db($SETTINGS['db_name'], $link);
                if (!$db_selected) {
			$ERROR[] = 'The name of the db you have specified does not exist.';
			$HIGHLIGHT['db_name'] = true;
                } else {
			$VERIFIED['db_name'] = true;
		}
        }

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `access` (
			  `id` varchar(32) NOT NULL default '',
			  `first` varchar(15) NOT NULL default '',
			  `last` varchar(20) NOT NULL default '',
			  `email` varchar(50) NOT NULL default '',
			  `ip` varchar(15) NOT NULL default '',
			  `host` varchar(50) NOT NULL default '',
			  `num_logins` int(5) NOT NULL default '0',
			  `num_hits` int(8) NOT NULL default '0',
			  `datetime_login` datetime NOT NULL default '0000-00-00 00:00:00',
			  `datetime_access` datetime NOT NULL default '0000-00-00 00:00:00',
			  `uniqueid` varchar(32) NOT NULL default '',
			  `verid` varchar(16) NOT NULL,
			  `datetime_verid_sent` datetime NOT NULL,
			  `enabled` tinyint(1) NOT NULL default '1',
			  `level` tinyint(1) NOT NULL default '2',
			  PRIMARY KEY  (`id`),
			  UNIQUE KEY `email` (`email`),
			  KEY `key` (`uniqueid`),
			  KEY `verid` (`verid`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `albums` (
			  `album_id` varchar(32) NOT NULL default '',
			  `title` varchar(100) NOT NULL default '',
			  `title_url` varchar(50) NOT NULL default '',
			  `location` varchar(100) NOT NULL,
			  `text` text NOT NULL,
			  `timestamp_created` int(11) NOT NULL default '0',
			  `timestamp_modified` int(11) NOT NULL default '0',
			  `num_photos` int(5) NOT NULL default '0',
			  `type` tinyint(1) NOT NULL default '1',
			  `highlight_photo_id` varchar(64) NOT NULL,
			  `timestamp_last_view` int(11) NOT NULL default '0',
			  `last_viewer` varchar(32) NOT NULL default '',
			  `views` int(10) NOT NULL default '0',
			  `min_level` tinyint(1) NOT NULL default '2',
			  `level` tinyint(1) NOT NULL default '2',
			  PRIMARY KEY  (`album_id`),
			  UNIQUE KEY `title` (`title`),
			  UNIQUE KEY `title_url` (`title_url`),
			  KEY `highlight_photo_id` (`highlight_photo_id`),
			  KEY `lowest_level` (`min_level`),
			  KEY `level` (`level`),
			  KEY `timestamp_created` (`timestamp_created`),
			  FULLTEXT KEY `title_2` (`title`,`text`,`location`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `album_photo_map` (
			  `album_id` varchar(32) NOT NULL default '',
			  `photo_id` varchar(64) NOT NULL,
			  PRIMARY KEY  (`album_id`,`photo_id`),
			  KEY `photo_id` (`photo_id`),
			  KEY `album_id` (`album_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `album_types` (
			  `type` int(2) NOT NULL,
			  `title` varchar(20) NOT NULL,
			  PRIMARY KEY  (`type`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `changelog` (
			  `item` varchar(64) NOT NULL,
			  `member_id` varchar(32) NOT NULL default '',
			  `first` varchar(50) NOT NULL default '',
			  `last` varchar(50) NOT NULL default '',
			  `email` varchar(50) NOT NULL default '',
			  `ip` varchar(12) NOT NULL,
			  `host` varchar(100) NOT NULL,
			  `timestamp_created` int(11) NOT NULL default '0',
			  `old` varchar(64) NOT NULL,
			  `new` varchar(64) NOT NULL,
			  `type` int(2) NOT NULL default '0',
			  KEY `photo_id` (`item`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `comments` (
			  `comment_id` varchar(32) NOT NULL,
			  `photo_id` varchar(64) NOT NULL,
			  `first` varchar(100) NOT NULL,
			  `last` varchar(100) NOT NULL,
			  `email` varchar(50) NOT NULL,
			  `site` varchar(100) NOT NULL,
			  `ip` varchar(15) NOT NULL,
			  `host` varchar(32) NOT NULL,
			  `text` text NOT NULL,
			  `timestamp_created` int(11) NOT NULL,
			  PRIMARY KEY  (`comment_id`),
			  UNIQUE KEY `comment_id` (`comment_id`,`photo_id`,`email`,`text`(50)),
			  UNIQUE KEY `no_duplicates` (`photo_id`,`email`,`text`(50)),
			  KEY `photo_id` (`photo_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `dir_scan_cache` (
			  `full_path` varchar(500) NOT NULL,
			  `folder_original_ctime` int(11) NOT NULL,
			  `folder_original_mtime` int(11) NOT NULL,
			  PRIMARY KEY  (`full_path`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `logins` (
			  `email` varchar(50) NOT NULL default '',
			  `datetime_auth` datetime NOT NULL default '0000-00-00 00:00:00',
			  `ip` varchar(15) NOT NULL default '',
			  `host` varchar(100) NOT NULL default '',
			  `success` char(1) NOT NULL default '',
			  `url` text NOT NULL,
			  KEY `ip` (`ip`),
			  KEY `url` (`url`(100)),
			  KEY `email` (`email`,`datetime_auth`,`ip`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";
	$db_schema[] = "CREATE TABLE IF NOT EXISTS `photos` (
			  `photo_id` varchar(64) NOT NULL,
			  `photo_md5` varchar(32) NOT NULL,
			  `sidecar_md5` varchar(32) NOT NULL,
			  `title` varchar(250) NOT NULL default '',
			  `title_url` varchar(150) NOT NULL,
			  `title_url_previous` varchar(150) NOT NULL,
			  `text` text NOT NULL,
			  `tag_list` text NOT NULL,
			  `comment_list` text NOT NULL,
			  `rating` tinyint(1) NOT NULL,
			  `path` varchar(200) NOT NULL,
			  `filename` varchar(100) NOT NULL,
			  `filename_extension` varchar(4) NOT NULL default '',
			  `filename_noextension` varchar(250) NOT NULL default '',
			  `height` int(5) NOT NULL default '0',
			  `width` int(5) NOT NULL default '0',
			  `type` int(2) NOT NULL default '0',
			  `bits` int(2) NOT NULL default '0',
			  `channels` int(2) NOT NULL default '0',
			  `mime` varchar(20) NOT NULL default '',
			  `filesize_bytes` float(15,5) NOT NULL default '0.00000',
			  `filesize_kilobytes` float(15,5) NOT NULL default '0.00000',
			  `filesize_megabytes` float(15,5) NOT NULL default '0.00000',
			  `EXIF_Make` varchar(100) NOT NULL default '',
			  `EXIF_Model` varchar(100) NOT NULL default '',
			  `EXIF_Orientation` tinyint(1) NOT NULL default '0',
			  `EXIF_Software` varchar(100) NOT NULL default '',
			  `EXIF_DateTime` datetime NOT NULL default '0000-00-00 00:00:00',
			  `EXIF_HostComputer` varchar(100) NOT NULL default '',
			  `EXIF_DateTimeOriginal` datetime NOT NULL default '0000-00-00 00:00:00',
			  `EXIF_DateTimeDigitized` datetime NOT NULL default '0000-00-00 00:00:00',
			  `EXIF_FNumber` varchar(10) NOT NULL default '',
			  `EXIF_ExposureProgram` varchar(10) NOT NULL default '',
			  `EXIF_ISOSpeedRatings` varchar(10) NOT NULL default '',
			  `EXIF_CompressedBitsPerPixel` varchar(10) NOT NULL default '',
			  `EXIF_ShutterSpeedValue` varchar(10) NOT NULL default '',
			  `EXIF_ApertureValue` varchar(10) NOT NULL default '',
			  `EXIF_BrightnessValue` varchar(10) NOT NULL default '',
			  `EXIF_ExposureBiasValue` varchar(10) NOT NULL default '',
			  `EXIF_MaxApertureValue` varchar(10) NOT NULL default '',
			  `EXIF_MeteringMode` varchar(10) NOT NULL default '',
			  `EXIF_Flash` varchar(10) NOT NULL default '',
			  `EXIF_FocalLength` varchar(10) NOT NULL default '',
			  `video_width` int(4) NOT NULL,
			  `video_height` int(4) NOT NULL,
			  `video_duration` varchar(10) NOT NULL,
			  `video_frame_rate` int(3) NOT NULL,
			  `video_display_aspect_ratio` varchar(5) NOT NULL,
			  `video_resolution` int(2) NOT NULL,
			  `timestamp_original_ctime` int(11) NOT NULL,
			  `timestamp_original_mtime` int(11) NOT NULL,
			  `timestamp_modified_ctime` int(11) NOT NULL,
			  `timestamp_modified_mtime` int(11) NOT NULL,
			  `is_video` tinyint(1) NOT NULL default '0',
			  `micro_width` int(5) NOT NULL default '0',
			  `micro_height` int(5) NOT NULL default '0',
			  `micro_generated` tinyint(1) NOT NULL default '0',
			  `thumb_width` int(5) NOT NULL default '0',
			  `thumb_height` int(5) NOT NULL default '0',
			  `thumb_generated` tinyint(1) NOT NULL default '0',
			  `preview_width` int(5) NOT NULL default '0',
			  `preview_height` int(5) NOT NULL default '0',
			  `preview_generated` tinyint(1) NOT NULL default '0',
			  `full_width` int(5) NOT NULL default '0',
			  `full_height` int(5) NOT NULL default '0',
			  `full_generated` tinyint(1) NOT NULL default '0',
			  `level` tinyint(1) NOT NULL default '2',
			  `timestamp_last_view` int(11) unsigned NOT NULL default '0',
			  `last_viewer` varchar(32) NOT NULL default '',
			  `timestamp_last_comment` int(11) NOT NULL,
			  `views` int(10) NOT NULL default '0',
			  `comments` int(5) NOT NULL default '0',
			  `tags` int(5) NOT NULL,
			  PRIMARY KEY  (`photo_id`,`level`),
			  UNIQUE KEY `photo_id` (`photo_id`),
			  UNIQUE KEY `photo_md5_2` (`photo_md5`),
			  KEY `title_url` (`title_url`),
			  KEY `level` (`level`),
			  KEY `title` (`title`),
			  KEY `EXIF_DateTimeOriginal` (`EXIF_DateTimeOriginal`),
			  KEY `title_url_previous` (`title_url_previous`),
			  KEY `last_comment` (`timestamp_last_comment`),
			  KEY `timestamp_last_view` (`timestamp_last_view`),
			  KEY `is_video` (`is_video`),
			  KEY `path` (`path`,`filename`),
			  KEY `sidecar_md5` (`sidecar_md5`),
			  FULLTEXT KEY `search` (`title`,`text`,`tag_list`,`comment_list`,`filename_noextension`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `s_access` (
			  `level` tinyint(1) NOT NULL default '0',
			  `title` varchar(25) NOT NULL default '',
			  PRIMARY KEY  (`level`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `tags` (
			  `tag_id` varchar(32) NOT NULL,
			  `title` varchar(50) NOT NULL,
			  `title_url` varchar(50) NOT NULL,
			  `timestamp_created` int(11) NOT NULL,
			  `count` int(5) NOT NULL,
			  PRIMARY KEY  (`tag_id`),
			  UNIQUE KEY `title` (`title`),
			  UNIQUE KEY `title_url` (`title_url`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `tag_photo_map` (
			  `tag_id` varchar(32) NOT NULL,
			  `photo_id` varchar(64) NOT NULL,
			  `x1` int(4) NOT NULL,
			  `y1` int(4) NOT NULL,
			  `x2` int(4) NOT NULL,
			  `y2` int(4) NOT NULL,
			  `email` varchar(100) NOT NULL,
			  PRIMARY KEY  (`tag_id`,`photo_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `tag_queue` (
			  `tag_queue_id` varchar(32) NOT NULL,
			  `from_first` varchar(25) NOT NULL,
			  `from_last` varchar(25) NOT NULL,
			  `to_first` varchar(25) NOT NULL,
			  `to_last` varchar(25) NOT NULL,
			  `to_email` varchar(50) NOT NULL,
			  `photo_id` varchar(64) NOT NULL,
			  `album_title_url` varchar(250) NOT NULL,
			  `photo_title_url` varchar(250) NOT NULL default '',
			  `tag_type` varchar(15) NOT NULL,
			  `media_type` varchar(10) NOT NULL,
			  `album_type` varchar(10) NOT NULL,
			  `timestamp_created` int(11) NOT NULL,
			  `timestamp_sent` int(11) NOT NULL,
			  `sent` tinyint(1) NOT NULL,
			  PRIMARY KEY  (`tag_queue_id`),
			  KEY `sent` (`sent`),
			  KEY `timestamp_created` (`timestamp_created`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	$db_schema[] = "CREATE TABLE IF NOT EXISTS `views` (
			  `ip` varchar(15) NOT NULL default '',
			  `host` varchar(100) NOT NULL default '',
			  `photo_id` varchar(64) NOT NULL,
			  `album_id` varchar(32) NOT NULL default '',
			  `referer` varchar(150) NOT NULL default '',
			  `url` varchar(150) NOT NULL default '',
			  `timestamp_created` timestamp NOT NULL default '0000-00-00 00:00:00',
			  `first` varchar(20) NOT NULL default '',
			  `last` varchar(20) NOT NULL default '',
			  `email` varchar(50) NOT NULL default '',
			  KEY `host` (`host`),
			  KEY `photo_id` (`photo_id`),
			  KEY `timestamp_created` (`timestamp_created`),
			  KEY `album_id` (`album_id`),
			  KEY `ip` (`ip`,`url`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1;
			";

	#Attempt to generate the structure
	if (empty($ERROR)) {
		foreach ($db_schema as $query) {
			$result = @mysql_query($query);
			if (!$result) {
				$ERROR[] = 'Error creating schema: ' . mysql_error();
			}
		}
	}

	if (empty($ERROR) AND $new_installation) {
		
		$db_seed[] = "INSERT INTO `s_access` (`level`, `title`) VALUES
				(1, 'Public'),
				(2, 'Friend'),
				(3, 'Family'),
				(4, 'Administrator'),
				(5, 'Root'),
				(6, 'Nobody');
				";

		$db_seed[] = "INSERT INTO `album_types` (`type`, `title`) VALUES
				(1, 'Roll'),
				(2, 'General Album'),
				(3, 'Family Albums');
				";

		#Insert the data
		foreach ($db_seed as $query) {
			$result = @mysql_query($query);
			if (substr(strtolower(mysql_error()),0,15) == 'duplicate entry') {
				$NOTICE[] = 'Records already exist.  This must be an existing installation';
			} else if (!$result) {
				$ERROR[] = 'Error creating default data: ' . mysql_error();
			}
		}
	}
}
if ($stage == 3){

	#If no path, detect it
	if (empty($SETTINGS['path'])) {
		$SETTINGS['path'] = $_SERVER['DOCUMENT_ROOT'];
		$SETTINGS['path'] = str_replace('/html','',$SETTINGS['path']);
		if (substr($SETTINGS['path'],-1) !== '/') {
			$SETTINGS['path'] = $SETTINGS['path'].'/';
		}
	} elseif (file_exists($SETTINGS['path'])) {
		$VERIFIED['path'] = true;
	}

	#If no full_site, detect it
        if (empty($SETTINGS['full_site'])) {
                $SETTINGS['full_site'] = 'http://'.$_SERVER['HTTP_HOST'];
                if (substr($SETTINGS['full_site'],-1) !== '/') {
                        $SETTINGS['full_site'] = $SETTINGS['full_site'].'/';
                }       
        } else {
                $VERIFIED['full_site'] = true;
        }

	#If no domain, detect iti
        if (empty($SETTINGS['domain'])) {
                $SETTINGS['domain'] = $_SERVER['HTTP_HOST'];
        } else {
                $VERIFIED['domain'] = true;
        }

	#Set cookie domain
	if (empty($SETTINGS['cookie_domain'])) {
                $SETTINGS['cookie_domain'] = $_SERVER['HTTP_HOST'];
		#If a domain like example.com, add a leading period 
		$domain_string = explode('.',$SETTINGS['cookie_domain']);
		if (count($domain_string) == 2) {
			$SETTINGS['cookie_domain'] = '.'.$SETTINGS['cookie_domain'];
		}
        } else {
                $VERIFIED['cookie_domain'] = true;
        }

	#If no domain, detect iti
        if (empty($SETTINGS['media_path'])) {
                $SETTINGS['media_path'] = $SETTINGS['path'].'media/originals';
        } elseif (file_exists($SETTINGS['media_path'])) {
                $VERIFIED['media_path'] = true;
        }

	#If no domain, detect iti
        if (empty($SETTINGS['media_path_modified'])) {
                $SETTINGS['media_path_modified'] = $SETTINGS['path'].'media/modified';
        } elseif (file_exists($SETTINGS['media_path_modified'])) {
                $VERIFIED['media_path_modified'] = true;
        }

	#If no dump dir is set, set it
        if (empty($SETTINGS['dump_dir'])) {
                $SETTINGS['dump_dir'] = $SETTINGS['path'].'dump/';
        } elseif (file_exists($SETTINGS['dump_dir'])) {
                $VERIFIED['dump_dir'] = true;
	}

} 
if ($stage == 4 AND isset($_POST)) {
	#Check that all 3 dump directories are writeable
	if (!file_exists($SETTINGS['dump_dir'].'/cache-adodb')) {
		$ERROR[] = 'The subdirectory '.$SETTINGS['dump_dir'].'/cache-adodb does not exist.  Check your dump directory or make this folder.';
		$HIGHLIGHT['dump_dir'] = true;
	} else if (!is_writeable($SETTINGS['dump_dir'].'/cache-adodb')) {
                $ERROR[] = $SETTINGS['dump_dir'].'/cache-adodb'.' is not writeable.  Please enter:<br /><code>chmod 777 '.$SETTINGS['dump_dir'].'/cache-adodb </code>';
		$HIGHLIGHT['dump_dir'] = true;
        }

	if (!file_exists($SETTINGS['lifebox_cache'])) {
		$ERROR[] = 'The subdirectory '.$SETTINGS['lifebox_cache'].' does not exist.  Check your dump directory or make this folder.';
		$HIGHLIGHT['dump_dir'] = true;
	} else if (!is_writeable($SETTINGS['lifebox_cache'])) {
                $ERROR[] = $SETTINGS['lifebox_cache'].' is not writeable.  Please enter:<br /><code>chmod 777 '.$SETTINGS['lifebox_cache'].'</code>';
		$HIGHLIGHT['dump_dir'] = true;
        }

	if (!file_exists($SETTINGS['smarty_templates_c'])) {
		$ERROR[] = 'The subdirectory '.$SETTINGS['smarty_templates_c'].' does not exist.  Check your dump directory or make this folder.';
		$HIGHLIGHT['dump_dir'] = true;
	} else if (!is_writeable($SETTINGS['smarty_templates_c'])) {
                $ERROR[] = $SETTINGS['smarty_templates_c'].' is not writeable.  Please enter:<br /><code>chmod 777 '.$SETTINGS['smarty_templates_c'].'</code>';
		$HIGHLIGHT['dump_dir'] = true;
        }
}

if ($stage == 4) {
	#If there's already a password set, make this green
	if (!empty($SETTINGS['admin_password'])) {
		$VERIFIED['admin_password'] = true;
	}

	if (!empty($SETTINGS['admin_email'])) {
		$VERIFIED['admin_email'] = true;
	}
} 

if ($stage == 5 AND isset($_POST))  {

	#Check that password has been set
        if (empty($SETTINGS['admin_password'])) {
                $ERROR[] = 'You must enter an admin password to access your site';
		$HIGHLIGHT['admin_password'] = true;
        } else {
		$VERIFIED['admin_password'] = true;
	}

	#Check that the admin e-mail address has been set
        if (empty($SETTINGS['admin_email'])) {
                $ERROR[] = 'You must enter your e-mail address to access the site';
                $HIGHLIGHT['admin_email'] = true;
        } elseif (!strpos($SETTINGS['admin_email'],'@')) {
		$ERROR[] = 'You have entered an invalid e-mail address';
		$HIGHLIGHT['admin_email'] = true;
	} else {
		$VERIFIED['admin_email'] = true;
	}
	
	#Insert admin info
	if ($new_installation AND empty($ERROR)) {
		$link = @mysql_connect($SETTINGS['db_host'].':'.$SETTINGS['db_port'],$SETTINGS['db_user'],$SETTINGS['db_pass']);
		$db_selected = @mysql_select_db($SETTINGS['db_name'], $link);
		$query = "INSERT INTO access (id, first, last, email,level) VALUES
			  (md5(now()), 'Site', 'Administrator', '".$SETTINGS['admin_email']."','5');";
		$result = @mysql_query($query);
		if (substr(strtolower(mysql_error()),0,15) == 'duplicate entry') {
                       $NOTICE[] = 'Records already exist.  This must be an existing installation';
                } else if (!$result) {
			$ERROR[] = 'Could not create administrative user: ' . mysql_error();
		}
	}
} 
if ($stage == 5) {
	#If a footer is set, highlight it
	if (!empty($SETTINGS['text_copyright'])) {
		$VERIFIED['text_copyright'] = true;
	}

	#If a site title is set, highlight it
	if (!empty($SETTINGS['site_title'])) {
		$VERIFIED['site_title'] = true;
	}

} 

if ($stage == 6 AND isset($_POST)) {
	#Check that site title has been entered
	if (empty($SETTINGS['site_title'])) {
		$ERROR[] = 'What is the title of your site going to be?';
	} else {
		$VERIFIED['site_title'] = true;
	}

	#Check copyright notice
	if (empty($SETTINGS['text_copyright'])) {
		$ERROR[] = 'Please enter a message for the copyright section of your site';
	} else {
		$VERIFIED['text_copyright'] = true;
	}
}

if ($stage == $last_step+1) {
		$output_file = $SETTINGS['path'].'html/config/config.inc.php';

		#First, test for write
		if (file_exists($output_file) AND !is_writeable($output_file)) {
			$ERROR[0] = 'Could not write to config/config.inc.php<br />Please enter <code>chmod 777 '.$output_file.'</code>';
                } else if (!@touch($output_file)) {
                        $ERROR[0] = 'Could not write to config/config.inc.php<br />Please enter <code>chmod 777 '.$SETTINGS['path'].'html/config'.'</code>';
                }

		if (empty($ERROR)) {
			exec('echo "<?php" > '.$output_file);
			$output_content = "<?php\r\n";
			foreach ($SETTINGS as $key=>$value) {
				if (in_array($key,$array_fields)) {
					$array_from_value = explode(',',$value);
					$string = '';
					foreach ($array_from_value as $item) {
						$string .= $item.',';
					}
					$string = substr($string,0,-1);
					$value = 'Array('.$string.')';
				} else {
					$value = addslashes($value);
					$value = "'".$value."'";
				}
				$value = str_replace('$','\$',$value);

				#Some fields don't need to be outputted
				if ($key !== 'password') {
					$exec = 'echo "'.'\$CONFIG[\''.$key.'\']='.$value.';" >> '.$output_file;
					$output_content .= '$CONFIG[\''.$key.'\']='.$value.';'."\r\n";
				}
				
				$result = exec($exec);
			}

			#Now, insert some things manually
			$exec = 'echo "'.'\$ADODB_CACHE_DIR=\''.$SETTINGS['dump_dir'].'/cache-adodb'.'\';" >> '.$output_file;
                        $output_content .= $exec."\r\n";
			$result = exec($exec);

			$exec = 'echo "'.'\$ADODB_FETCH_MODE=\'ADODB_FETCH_ASSOC\';" >> '.$output_file;
                        $output_content .= $exec."\r\n";
                        $result = exec($exec);

			exec('echo "?>" >> '.$output_file);
			$output_content .= "?>";
		}

		#First, test for write
		$output_file = $SETTINGS['path'].'html/.htaccess';

                if (file_exists($output_file) AND !is_writeable($output_file)) {
                        $ERROR[1] = 'Could not write to html/.htaccess<br />Please enter <code>chmod 666 '.$output_file.'</code>';
                } else if (!@touch($output_file)) {
                        $ERROR[1] = 'Could not create html/.htaccess<br />Please enter <code>touch '.$output_file.'<br />chmod 777 '.$output_file.'</code> and try again';
                }

		if (empty($ERROR)) {
			$htaccess[] = 'RewriteEngine On';
			$htaccess[] = 'RewriteRule ^[/]?$ _index.php';
			$htaccess[] = 'RewriteRule ^setup[/]?$ setup/index.php [L]';
			$htaccess[] = 'RewriteRule ^(photo)/([abcdef0-9]{64})-(main_album|main_event|main_recent|main_image|album|thumb|preview|full|micro|featured|download|roll|video)\.([a-zA-Z0-9]*)$ _photo.php?a=$1&s=$2&a2=$3&s2=$4';
			$htaccess[] = 'RewriteRule ^(rss)[/]?([a-zA-Z0-9?=&_\'\-]*)[/]?([a-zA-Z0-9]*)[/]?$ _$1.php?a=$1&s=$2';
			$htaccess[] = 'RewriteRule ^(albums|rolls|photos|videos|comments)[/]?(.*) _$1.php$2';
			$htaccess[] = 'RewriteRule ^(search)[/]?(.*) _$1.php$2';
			$htaccess[] = 'RewriteRule ^(profile|about|admin|faq|privacy|terms|contact|statistics)[/]?([a-zA-Z0-9?=&_\']*)[/]?([a-zA-Z0-9]*)[/]?([a-zA-Z0-9]*)[/]?$ _$1.php?a=$1&s=$2&a2=$3&s2=$4';
			$htaccess[] = 'RewriteRule ^[/]?$ _index.php';
			$htaccess[] = 'RewriteRule ^[/]?([a-zA-Z0-9_-]*)[/]?$ _contents.php?a=$1&$2';
			$htaccess[] = 'RewriteRule ^[/]?([a-zA-Z0-9_-]*)/(&[a-zA-Z0-9=&]*)$ _contents.php?a=$1&s=$2';
			$htaccess[] = 'RewriteRule ^[/]?([a-zA-Z0-9_-]*)[/]?([a-zA-Z0-9_-]*)[/]?([&]?[a-zA-Z0-9=&]*)$ _view.php?a=$1&s=$2&sort=$3';

			$exec = 'echo "#LIFEBOX CONFIGURATION" > '.$output_file;
			exec($exec);
			foreach ($htaccess as $line) {
				$exec = 'echo "'.$line.'" >> '.$output_file;
				$exec = str_replace('$','\$',$exec);
				exec($exec);
			}
		}
}
#If an error, send back a stage
if (!empty($ERROR)) {
	$stage--;
}

#Set the items to be displayed depending on the stage
if (!empty($stage) AND isset($CONFIG_EXAMPLE)) {
        foreach ($CONFIG_EXAMPLE as $key => $value) {
                if (isset($steps[$stage]) AND in_array($key,$steps[$stage])) {
                        $DISPLAY[$key] = $value;
                }
        }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
                <title>Lifebox Configuration</title>
                <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                <link rel="icon" href="templates/img/favicon.ico" type="image/x-icon" />
		<style type="text/css">
			body {
				font-family: "Trebuchet MS", sans-serif;
				font-size: 13px;
				background-color: #FFFFFF;
				color: #000000;
				text-align: center;
			}

			fieldset {
				border: 0px;
				margin: 0px;
				padding: 0px;
			}

			p {
				margin: 0;
			}
			p.notice {
				background-color: #ADADCC;
				padding: 5px;
				border: 1px solid #3333AD;
				margin: 0px 0px 5px 0px;
			}
			#header {
				text-align: left;
				color: #000000;
				background-image: url(lifebox_logo_small.jpg);
				background-repeat: no-repeat;
				height: 90px;
				padding-left: 95px;
			}
			#header h1 {
				margin: 0px;
				font-size: 16px;
			}

			#header h2 {
				margin: 0px;
				font-size: 13px;
			}
			.input_text {
				width: 200px;
				border: 1px solid #9A9A9A;
				height: 20px;
				font-size: 16px;
				padding-left: 2px;
			}
			.button {
			}
			.highlight {
				background-color: #F3CFCF;
			}
			.verified {
				background-color: #CFF3CF;
			}
			input.error {
				background-color: #EBC6C6;
				color: #860F0F;
			}
			#error {
				padding: 10px;
				background-color: #E9D4D4;
				border: 1px solid #E07171;
				margin: 0px 0px 30px 0px;
			}
			#greeting {
				margin: 0 auto;
				width: 500px;
				height: 600px;
				padding-top: 175px;
				margin-top: 75px;
				background-image: url(logo.jpg);	
				background-repeat: no-repeat;
				background-position: center top;
			}
			#greeting input {
				margin-top: 10px;
			}
			#main {
				margin: 0 auto;
				width: 600px;
				text-align: left;
				margin-top: 25px;
			}
			.option {
				margin: 10px 0px 10px 0px;
				clear: both;
				overflow: hidden;
			}
			.title {
				float: left;
				width: 150px;
			}
		
			.text {
				float: left;
				margin-left: 10px;
				width: 150px;
				font-size: 10px;
				color: #333333;
			}
			.field {
				float: left;
			}
   		</style>
        </head>
        <body>
	<div id="content">
		<?php if (!empty($stage) AND $stage <= $last_step) { ?>
			<div id="header">
				<h1>Welcome to Lifebox</h1>
				<h2>Now, let's get it installed</h2>
			</div>

			<div id="main">
				<?php if ($ERROR) { ?>
					<div id="error">
					<ul>
						<?php foreach ($ERROR as $e) { ?>
							<li><?php echo $e; ?></li>
						<?php } ?>
					</ul>
					</div>
				<?php } ?>
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>?stage=<?php echo $stage+1; ?>" method="post">
				<?php if (isset($SETTINGS)) {
					foreach ($SETTINGS as $key => $value) {
						#Process arrays differently
						if (is_array($value)){
							$string = '';
							if ($key === 'footer_links') {
								foreach ($value as $key1=>$value1) {
									$string .= "'".$key1."'=>'".$value1."',";
								}
							} else {
								foreach ($value as $item) {
									$string .= "'".$item."',";
								}
							}
							$SETTINGS[$key] = substr($string,0,-1);
						}
						#If this wasn't submitted
						print '<input type="hidden" name="'.$key.'" value="'.$SETTINGS[$key].'" />'."\r\n";
					} 
				} ?>
				<?php if (!empty($DISPLAY)) {
					if ($stage == 6) {
						print '<p class="notice">You can leave all of these advanced settings at the default unless you specifically want to change something.  I recommend scrolling down and clicking next unless you know what you are doing.</p>';
					}
					foreach ($DISPLAY as $key => $value) {
					
					if(isset($title[$key])) { $title_string = $title[$key]; } else { $title_string = $key; }
					if(isset($text[$key])) { $text_string = $text[$key]; } else { $text_string = ''; }
					if(isset($HIGHLIGHT[$key])) { $error_class = ' error'; } else { $error_class = ''; }
					if(isset($VERIFIED[$key])) { $verified_class = ' verified'; } else { $verified_class = ''; }
					if(isset($SETTINGS[$key])) { $value_string = $SETTINGS[$key]; } else { $value_string = $CONFIG[$key]; }
	
					print '<div class="option">'."\r\n";
					print '<div class="title">'.$title_string.'</div>'."\r\n";
					print '<div class="field"><input type="text" name="'.$key.'" value="'.$value_string.'" class="input_text'.$error_class.''.$verified_class.'" /></div>'."\r\n";
					print '<div class="text">'.$text_string.'</div>'."\r\n";
					print '</div>';
					}
				} ?>
					<?php if ($stage == $last_step) { ?>
					<input type="submit" value="Save Changes and Launch Lifebox" class="button" />
					<?php } elseif ($stage > $last_step) { ?>
					<?php } else { ?>
						<input type="button" value="Start Again" onclick="javascript:window.location='/setup/';" class="button" />
						<?php if ($stage > 1) { ?>
							<input type="button" value="<< Go Back" onclick="javascript:history.go(-1);" class="button" />
						<?php } ?>
						<input type="submit" value="Check and continue >>" class="button" />
					<?php } ?>

				</form>
			</div>
		<?php } elseif ($stage > $last_step) { ?>
			<div id="greeting">Congratulations, your site is ready.  Please enter the below to secure your configuration:
			<p><code>chmod 755 <?php echo $SETTINGS['path'].'html/.htaccess'; ?><br />sudo chmod 755 <?php echo $SETTINGS['path'].'html/config -R'; ?></code></p>
                        <input type="button" value="Launch Lifebox" onclick="javascript:window.location='<?php echo $SETTINGS['full_site']; ?>'" />
                        </div>
		<?php } else { ?>
			<div id="greeting">
			<?php if (empty($SETTINGS['admin_password'])) { ?>
			<p>Hi, and greeting to the Lifebox installer.  This installer will take you through several steps designed to simplify the installation process of Lifebox on your machine.  Click below to begin.</p>
			<input type="button" value="Begin Configuration >>" onclick="javascript:window.location='?stage=1'" />
			<?php } else { ?>
			<form action="<?php echo $_SERVER['PHP_SELF']; ?>?stage=<?php echo $stage+1; ?>" method="post">
			<p>Enter your site administration password</p>
			<fieldset>
			<input type="password" name="password" class="input_text" value="<?php echo $_POST['password']; ?>"/><br />
			<input type="submit" value="Authenticate & Begin Configuration" class="button" />
			</fieldset>
			</form>
			<?php } ?>
		<?php } ?>
		</div>
	</div>
	</body>
</html>
