Location: PHPKode > projects > ChurchCMS > churchcms/install/index.php
<?php
/**************************************************************************************************
 *                                            ChurchCMS
 * Copyright (C) 2005 jsvoyager
 *
 * Developers & Contributors:
 * jsvoyager					hide@address.com
 *
 * 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.
 * (license.txt)
 ***************************************************************************************************/

$version = "0.1.0";	//CHANGE FOR EACH NEW VERSION!!!

if ($_GET['step'] == 1 || !isset($_GET['step'])){
	$_GET['step'] = 1;
	
	//Basic Welcome to the user...no real code
	$content =<<<ENDCONTENT
<p>Welcome to the ChurchCMS Installation Script.  This script will guide you through the five step process
we have put together to make installing ChurchCMS a breeze.  These steps are:</p>
<ol>
	<li><b>Welcome to and Overview of ChurchCMS</b></li>
	<li>Basic Configuration</li>
	<li>Base Module Installation</li>
	<li>Administrator Setup</li>
	<li>Customization</li>
</ol>
<p>Before beginning the installation of ChurchCMS, we recommend that you have the following items and information
at your disposal:</p>
<ul>
	<li>MySQL Database Name and User that ChurchCMS will be using (most likely assigned by your ISP)</li>
	<li>A banner-like graphic of your church (will be displayed at the top of the pages) such as this one:</li>
	<img src="../theme/header.png" alt="Sample Graphic">
	<li><i>Optional:</i> Additional modules you may want to install into ChurchCMS, available from our website</li>
</ul>
<p>With these in hand, we can now begin the installation.  Thank you for choosing ChurchCMS!</p>
<a href="{$_SERVER['PHP_SELF']}?step=2">Next: Basic Configuration</a>
ENDCONTENT;
}elseif ($_GET['step'] == 2){
	//Information Gathering... Once again no install logic
	
	chdir("..");
	$root = getcwd();
	
	$content =<<<ENDCONTENT
<p>Before ChurchCMS can be installed, we must gather basic information about your setup.  Please fill in the
information requested below.  Most of this information can be obtained with help from your ISP, if you do not
know it.</p>
<form action="{$_SERVER['PHP_SELF']}?step=3" method="post">
<table width="0">
	<tr>
		<td>Database Server:</td>
		<td><input type="text" name="db_server" value="localhost"></td>
	</tr>
	<tr>
		<td>Database User:</td>
		<td><input type="text" name="db_user"></td>
	</tr>
	<tr>
		<td>Database User&#146;s Password:</td>
		<td><input type="text" name="db_pass"></td>
	</tr>
	<tr>
		<td>Database Name:</td>
		<td><input type="text" name="db_name"></td>
	</tr>
	<tr>
		<td>Table Prefix:</td>
		<td><input type="text" name="db_prefix">&nbsp;&nbsp;<i>(Most likely left blank.  Fill in if the database 
		  is being sharied with another program)</i></td>
	</tr>
	<tr>
		<td>ChurchCMS Root:</td>
		<td><input type="text" name="root" value="$root">&nbsp;&nbsp;<i>(Most likely 
		  default value is correct)</i></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="Next: Base Module Installation"></td>
	</tr>
</table>
</form>
ENDCONTENT;
}elseif ($_GET['step'] == 3){
	//Now for some install logic
	//First, write out the config file
	
	chdir("..");
	$config =<<<ENDCONFIG
<?php
define("_VERSION_",  "$version");
define("_ROOT_", "{$_POST['root']}");
define("_LANG_", "en");
define("_DB_SERVER_", "{$_POST['db_server']}");
define("_DB_USER_", "{$_POST['db_user']}");
define("_DB_PASS_", "{$_POST['db_pass']}");
define("_DB_NAME_", "{$_POST['db_name']}");
define("_DB_PREFIX_", "{$_POST['db_prefix']}");
//?>
ENDCONFIG;
	$content .= "<p class=\"title\">Configuring ChurchCMS and Database</p>";
	$content .= "<table border=\"0\">";
	$content .= "<tr><td>Config File Exsists?</td><td width=\"75\" ";
	//Test to see if it exsists
	if (file_exists("lib/config.inc.php")){
		$content .= "class=\"good\">Yes";
		
		//Now see if it is writable
		$content .= "</td></tr><tr><td>Config File Writable?</td><td ";
		if (!is_writeable("lib/config.inc.php")){
			//Now we got ourselves a prb
			$content .= "class=\"bad\">No</td>";
			$content .= "<tr><td>Able to change permision to write?</td><td ";
			
			//Try to fix it
			if (!chmod("lib/config.inc.php", 0755)){
				$content .= "class=\"bad\">No.  Please resolve manually.</td></tr></table>";
				die($content);
			}else{
				$content .= "class=\"good\">Yes";
			}
		}else{
			$content .= "class=\"good\">Yes";
		}
	}else{
		//Config File does not exsist
		$content .= "class=\"warn\">No";	
	}
	
	$content .= "</td></tr>";
	
	//Write it out
	$fh = fopen("lib/config.inc.php", "w");
	fwrite($fh, $config);
	fclose($fh);
	
	
	//Now we can include mainfile, which needed config.inc.php, so that we can
	//  open up our database
	include_once("lib/main.inc.php");
	
	//Now out the basic database stuff.
	/*
	 *Please Note:  I blatently stole this from a project I'm on called 
	 *  Webschool.  Thanks ProgrammerMatt!
	 *  From: install/step3.php
	 *  Website:  http://webschool.sf.net/
	 */
	$content .= "<tr><td>Finding SQL Files...</td><td ";
	if( file_exists("install/churchcms.sql") ) {
		$content .= "class=\"good\">Success</td></tr>";
		$sqlfound = true;
	} else {
		$content .= "class=\"bad\">Failed</td></tr>";
		$sqlfound = false;
	}

	if( $sqlfound == false ){
		$content .= "<tr bgcolor=red><td>Installation FAILED. No SQL Found.</td></tr>";
		die($content); //not preffered way to do this, but works well.
	}

	$content .= "<tr><td>Checking SQL Files... </td><td ";
	if( is_readable("install/churchcms.sql") ) {
		$content .= "class=\"good\">Success</td></tr>";
		$sqlread = true;
	} else {
		if( chmod("install/churchcms.sql", 0777) ){
			$content .= "class=\"good\">Success</td></tr>";
			$sqlread = true;
		} else {
			$content .= "class=\"bad\">Failed</td></tr>";
			$sqlread = false;
		}
	}

	if( $sqlread == false ){
		$content .= "<tr bgcolor=red><td>Installation FAILED. SQL Could not be read.</td></tr>";
		die($content); //not preffered way to do this, but works well.
	}

	$content .= "<tr><td colspan=2 style=\"background-color: gray; text-align: center;\">" . 
	  "Running SQL Queries:</td></tr>";
	//lets run the queries
	$queryfile = file("install/churchcms.sql");
	$querycache = "";
	foreach($queryfile as $query) {
		$query = eregi_replace("!prefix!_", _DB_PREFIX_, $query);
		if( !eregi("#", $query) ){
			if( eregi(";", $query) ){
				if($querycache == ""){
					$content .= "<tr><td><pre>$query</pre></td><td ";
					if( $db->query(__FILE__, __LINE__, eregi_replace(";", "", $query)) ){
						$content .= "class=\"good\">Success";
					}else{
						$content .= "<class=\"bad\">Failed";
					}
					$content .= "</td></tr>";
				} else {
					$querycache .= $query;
					$content .= "<tr><td><pre>$querycache</pre></td><td ";
					if( $db->query(__FILE__, __LINE__, eregi_replace(";", "", $querycache)) ){
						$content .= "class=\"good\">Success";
					}else{
						$content .= "class=\"bad\">Failed";
					}
					$content .= "</td></tr>";
					$querycache = "";
				}
			} else {
				$querycache .= $query;
			}
		}
	}
	
	$content .= "<tr><td>SQL Installation</td><td class=\"good\">Success</td></tr></table>";
	
	/////////////////////////////////////////////////////////
	// Now to tell what base modules we're installing
	////////////////////////////////////////////////////////
	$content .= "<p class=\"title\">Base Module Installation</p>";
	$content .= "ChurchCMS is now ready to install it's collection of base modules.  A &quot;module&quot; in " . 
	  "ChurchCMS is a broad feature or task that ChurchCMS has.  More modules are avaliable at our " . 
	  "<a href=\"http://churchcms.sf.net\">website</a>.  The following base modules are going to be installed: ";
	$content .= "<ul>";
	
   $dh = opendir("install/base_mods");
   while (($file = readdir($dh)) !== false) {
		if (preg_match("/\\.tar\\.gz/", $file)){
			$file = ucfirst( preg_replace("/\\.tar\\.gz/", "", $file));
			$content .= "<li>$file</li>";
		}
   }
   closedir($dh);
  
	$content .= "</ul>";
	$content .= "<a href=\"{$_SERVER['PHP_SELF']}?step=4\">Next: Administrative Setup</a>";
	
}elseif ($_GET['step'] == 4){
	chdir("..");
	include ("lib/main.inc.php");
	
	if ($_GET['error'] == "pass"){
		$content .= "<hr><p style=\"color: red;\">Passwords do not match</p><hr>";
	}
	
	$content .= "<p class=\"title\">Module Install</p>";
	
	//install each module
   $dh_me = opendir("install/base_mods");
   while (($file = readdir($dh_me)) !== false) {
		if (preg_match("/\\.tar\\.gz/", $file)){
			$file = _ROOT_ . "/install/base_mods/" . $file;
			
			//Poor man's passing of vars
			$_REQUEST['file'] = $file;
			$_GET['silent'] = true;
			$_GET['step'] = 1;
			
			//Copy our content var (cause it'll get overwriten
			$our_content = $content;
			
			//"Call" the install_mod.php "function"
			include ("install_mod.php");
			
			//Recopy our content var
			$content = $our_content;
			$_GET['step'] = 4;
			
			//Take advantage of that "module" var and output some good stuff
			$content .= "<p>Module: <b>{$module['name']}</b></p><ul>\n";
			$content .= "<li>Version: <b>{$module['name']}</b></li>\n";
			$content .= "<li>Description: <b>{$module['desc']}</b></li>\n";
			$content .= "<li>Included Tables:</li><ul>\n";
			for ($i=0; $i<count($module['tables']); $i++){
				$content .= "<li><b>{$module['tables'][$i]['name']}</b></li>\n";
			}
			$content .= "</ul></ul>\n\n";
		}
   }
   closedir($dh_me);
   
   //Now for the actual administrator setup
   $content .=<<<ENDCONTENT
<p class="title">Administrative Setup</p>
<p>Although there can be multiple useres with &quot;admin&quot; privlages, only one has the ability to affect 
ChurchCMS on a global scale.  For instance, only he can
<ul>
	<li>Upgrade ChurchCMS</li>
	<li>Install modules</li>
	<li>Grant users &quot;admin&quot; status</li>
	<li>Change the theme, banner image, etc...</li>
</ul>
  While other &quot;admins&quot; can add, edit, and delete
content, this account is the only one that can mess with the core of ChurchCMS.  Because his account is so 
important, it will be created now.  Please note that other users can be created and given &quot;admin&quot; 
status later by going to the &quot;users&quot; utility of the admin page.</p>
<form action="{$_SERVER['PHP_SELF']}?step=5" method="post">
<table width="0">
<tr>
	<td class="field-title">Username:</td>
	<td><b>Administrator</b></td>
</tr>
<tr>
	<td class="field-title">Password:</td>
	<td><input type="password" name="admin_pass1"></td>
</tr>
<tr>
	<td class="field-title">Confirm Password:</td>
	<td><input type="password" name="admin_pass2"></td>
</tr>
<tr>
	<td colspan="2"><input type="submit" value="Next: Customize ChurchCMS"></td>
</tr></table>
</form>
ENDCONTENT;

}elseif ($_GET['step'] == 5){
	chdir ("..");
	include ("lib/main.inc.php");
	
	if ($_POST['admin_pass1'] !== $_POST['admin_pass2']){
		header ("Location: index.php?step=4&error=pass");
		die();
	}
	//Save the admin into the db
	$query = "INSERT INTO "._USERS_." (id, uname, pass, disp_name, last_activity, admin) VALUES ('1', " . 
	  "'Administrator', password('" . $_POST['admin_pass1'] . "'), 'Administrator', '" . time() . "', '1')";
	$db->query(__FILE__,__LINE__,$query);
	
	//Now for the custimization
	$content =<<<ENDCONTENT
<p class="title">Customize ChurchCMS</p>
<p>Bering that this is not a high priority, customization is limited at this time.</p>
<form action="{$_SERVER['PHP_SELF']}?step=6" method="post">
<table width="0">
	<tr>
		<td>Church Name:</td>
		<td><input type="text" name="church_name"></td>
	</tr>
	<tr>
		<td>General Contact Email:</td>
		<td><input type="text" name="church_email"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="Complete Setup"></td>
	</tr>
</table>
</form>
<p><i>Please note:  All of these settings are re-setable from within the admin page of the Administrator.</i></p>

ENDCONTENT;
}elseif ($_GET['step'] == 6){
	//Put in the config database
	$query = "INSERT INTO "._CONFIG_." (name, setting) VALUES ('church_name', '{$_POST['church_name']}'), ('" . 
	  "church_email', '{$_POST['church_email']}')";
	
	//Show that we're finished
	$content =<<<ENDCONTENT
<p class="title">Installation Complete</p>
<p>ChurchCMS installation is now complete.  Click on the link below to go to the homepage.  From there you may 
want to login as the Administrator and:</p>
<ul>
	<li>Create more users</li>
	<li>Add events to the calendar</li>
	<li>Add announcements</li>
	<li>Add other content such as prayer requests and help wanted postings</li>
	<li>Find additional modules to fit your church&#146;s needs at our <a href="http://churchcms.sf.net/library/">
	module library</a></li>
</ul>
<a href="../">Goto ChurchCMS homepage</a>

ENDCONTENT;
	$_GET['step'] = 5;
}


////////////////////////////////////////////////////////////////////////////////////////////////////////
// The Core HTML (before templates are in, so we have to go with this)
///////////////////////////////////////////////////////////////////////////////////////////////////////
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<link href="../theme/theme.css" rel="stylesheet" type="text/css">
<style>
<!--
	.warn {
		color: yellow;
		font-size: 110%;
	}
	
	.good {
		color: green;
		font-size: 110%;
	}
	
	.bad {
		color: red;
		font-size: 110%;
	}
	
	td.good {
		background-color: #52A329;
		text-align: center;
	}
	
	td.warn {
		background-color: #FFD500;
		text-align: center;
	}
	
	td.bad {
		background-color: red;
		text-align: center;
	}
-->
</style>
<title>Installing ChurchCMS</title>
</head>

<body>
<center>
<table class="subwindow" width="85%">
<tr><td class="title">ChurchCMS Installation, Step <?=$_GET['step'] ?> of 5</td></tr>
<tr><td class="body">
<?=$content?>
</td></tr>
</table>
</center>
</body>
</html>
Return current item: ChurchCMS