Location: PHPKode > projects > Cynus > cynus/install/install.php
<?php

# define the version of Cynus that this install
# script is meant to be used with.
#
# if you want to add extra data (like cvs or rc1)
# to the version, add a forth dot and put it after that.
# keeps everything running smoothly if you do.
# will break stuff if you don't.
#
# config_version is the version of the SQL and config file.
# if anything at all changes in the config, no matter how small, 
# increment the config_version number.
define (CYNUS_VERSION, '0.6.2');
define (CONFIG_VERSION, '1');




# this is the new installation system. 
# yay for it.
#
# this install system will create basically a mysql batch file that
# the very last page will run (==parse and query to mysql)
# the batch file will also be able to server as a install log, and a "restore defaults" backup file.
#
#
#print_r ($_POST);

# giving us issues
#set_magic_quotes_runtime(0);

# until we get cynus caught up with it's glorious installation script,
# we have to ignore the prefix...
#$_POST['mysql_prefix'] = '';



# gotta see what page we're on...
# this is POST so they can't be tricky with
# get vars, and possibly break stuff..
#
# notice that after page 2 (when we start getting real info) every page
# checks the previously sent data, and returns to the previous page if there
# are errors.
switch ($_POST['page']) {
	case '1':
		print page_1();
		exit;
	case '2':
		print page_2();
		exit;
	case '3':
		print page_3();
		exit;
	case '4':
		print page_4();
		exit;
	case '5':
		print page_5();
		exit;
	case '6':
		print page_6();
		exit;
	
	default:
		print page_1();
		exit;
}


















##############################
#
# PAGE FUNCTIONS
#
##############################


# Page one:
#	Checks for:
#		Previous config.php files (but can only overwrite them currently)
#		Actual write permissions
#		MySQL functions 
#		Windows or not based installation
#		Gettext support if in linux (emulates it if not found, or if in windows..doesn't work yet)
#				
function page_1 () {
	
	# checking write permission
	if($fp=@fopen('../config.php', 'r+')) {
		fclose ($fp);
		if (filesize ('../config.php') > 0) {
			# it's very unlikely this will ever show up
			$permissions = '<dt>File permissions...</dt><dd>Ok!  (Old installation found.)</dd>';
		}
		else {
			$permissions = '<dt>File permissions...</dt><dd>Ok!  (New installation)</dd>';
		}
		
	}
	else {
		$permissions = '<dt>File permissions...</dt><dd><a style="color:#ff0000">ERROR.</a></dd>';
	}
	
	# checking for mysql functions
	if (function_exists (mysql_connect)) {
		$mysql = '<dt>PHP MySQL support...</dt><dd>Ok!</dd>';
	}
	else {
		$mysql = '<dt>PHP MySQL support...</dt><dd><a style="color:#ff0000">ERROR.</a><br />' .
			'Cynus requires a version of PHP that includes MySQL functions' .
			'and a current MySQL installation.  ' .
			'MySQL is included in many Linux distrubutions, ' .
			'but may not be installed by default.  MySQL is also available' .
			'for Windows.  For more information, visit:<br />' .
			'<a href="http://www.mysql.com">http://www.mysql.com</a></dt>';
		$fatal_error=1;
	}
	
	# checking for Windows or not
        if(substr(PHP_OS, 0, 3) == 'WIN') {
		$os_type = 'win';
                $os = '<dt>Server operating system...</dt><dd>Windows.  (Baa...BAAAA!  Sheep!)</dd>';
        }
        elseif (stristr(PHP_OS, "linux")) {
		$os_type = 'linux';
                $os = '<dt>Server operating system...</dt><dd>Linux.  (Power overwhelming.)</dd>';
        } 
	else {
		$os_type = 'other';
                $os = '<dt>Server operating system...</dt><dd>Other.  (Other...interesting.  Give us an email.)</dd>';
	}

#	they don't need to know this....
#	if (!function_exists (_) && $os_type!='win') {
#		$gettext = '<dt>Gettext support...</dt><dd>Ok!</dd>';
#	}
#	else {
#		$gettext = '<dt>Gettext support...</dt>' .
#		'<dd>Not found.  Gettext is a localization software that enables programs operate in different '.
#		'languages.  Because it was not found on your server, Cynus will emulate its basic functions. ' .
#		'For more information about gettext, visit:<br /> ' .
#		'<a href="http://gettext.sourceforge.net/">http://gettext.sourceforge.net/</a></dd>';
#	}

	$content =<<<___eofh
	
	Cynus is checking the state of your system:
	<dl>
		$permissions<br />
		$mysql<br />
		$os<br />
	</dl>
	
___eofh;
	
	# checking for errors.
	# if fatal_error == 1, spit out a message and don't let them continue.
	# else, continue
	if ($fatal_error==1) {
		$content.=<<<___eofh
		
		<big style="color: #ff0000">Fatal Error</big><br /><br />
		
		Some required programs could not be found on your server.
		Cynus installation has been halted.  Please correct any problems stated above, and try
		to install Cynus again.<br /><br />
		<small>Please note that the Cynus authors have no control over your server,
		and will not be able to assist with server related problems.  If you are not comfortable
		installing and configuring the software Cynus requires, please contact your System Administrator.

___eofh;
	}
	else {
		$content .=<<<___eofh
		Your system passes the initial tests.  Please continue to the second page. <br /><br />
		
		<form action="{$_SERVER['PHP_SELF']}" method="POST">
		<input type="hidden" name="page" value="2">
		<input type="submit" value="Continue">
		</form>
___eofh;
	}

	return make_page("Cynus Installation: Page 1 -- Sanity Checks", $content);
}




# Page two:
#	Gathers mysql information
function page_2 ($error=false) {
	
	#seeing if we've been circled back to here beacuse couldn't connect...
	if ($error) {
		$content = '<a style="color: #ff0000">Error with MySQL server!</a> <br />' . $error . '<br /><br />';
	}
	$mysql_prefix=set_default('cynus_', $_POST['mysql_prefix']);
	$mysql_server=set_default('localhost', $_POST['mysql_server']);
	$mysql_database=set_default('cynus', $_POST['mysql_database']);

	$content.=<<<___eofh
	Please enter your MySQL connection details:
	<form method="POST" action="install.php">
	<table>
		<tr>
			<td>Username</td>
			<td><input type="text" name="mysql_username" value="$_POST[mysql_username]" /></td>
		</tr>
		<tr>
			<td>Password</td>
			<td><input type="password" name="mysql_password" /></td>
		</tr>
		<tr>
			<td>Server</td>
			<td><input type="text" name="mysql_server" value="$mysql_server" /></td>
		</tr>
		<tr>
			<td>Database</td>
			<td><input type="text" name="mysql_database" value="$mysql_database" /></td>
		</tr>
		<tr>
			<td style="vertical-align: top;">Table Prefix</td>
			<td><input type="text" name="mysql_prefix" value="$mysql_prefix"><br />
			<small>If specified, all tables created by Cynus and its modules will be mysql_prefixed
			with this.  This is useful if you have to have two Cynus installations in one mysql_database.
			</small>
		</tr>
			<td>Create Database</td>
			<td><input type="checkbox" name="create_db" value="1" id="create_db" />
			<label for="create_db">Create MySQL database</label><br />
			<small>If you have proper permissions, this will create
			the database specifed above.</small>
			</td>
		</tr>
	</table>
	<input type="hidden" name="page" value="3" />
	<input type="submit" value="Next">
___eofh;

	return make_page("Cynus Installation: Page 2 -- MySQL details", $content);
}

# Page three:
#	district
#	webmaster
#	webmaster_email
#	
#	cynus_full_url
#	cynus_base_dir
#	url_base_dir
#	doc_root
#	777'd dir (randomly created name as default)
#
#	main_columns
#	theme???
#
#	modules_url
#	use_proxy
#	proxy
#	proxy_port
function page_3 ($error=FALSE) {
	
	# passing old values up through type = hidden tags
	$content=<<<___eofh
		<form method="POST" action="{$_SERVER['PHP_SELF']}">
		<input type="hidden" name="mysql_username" value="{$_POST['mysql_username']}">
		<input type="hidden" name="mysql_password" value="{$_POST['mysql_password']}">
		<input type="hidden" name="mysql_server" value="{$_POST['mysql_server']}">
		<input type="hidden" name="mysql_database" value="{$_POST['mysql_database']}">
		<input type="hidden" name="mysql_prefix" value="{$_POST['mysql_prefix']}">

___eofh;
	
	# testing for page 2 and previous installations.
	if(!$mysql_conn=@mysql_connect($_POST['mysql_server'], $_POST['mysql_username'], $_POST['mysql_password'])) {
		#return them to page 2 to fix their mistakes.
		page_2('Please check the username, password, and server and try again');
		exit;
	}
	else {
		if($_POST['create_db']==1) {
			$query = "CREATE DATABASE `{$_POST['mysql_database']}`";
			if (!@mysql_query ($query)) {
				$error=mysql_error ();
				page_2('Could not create the database.  MySQL replied: ' . $error);
				exit;
			}
		}

		
		elseif(!@mysql_select_db($_POST['mysql_database'], $mysql_conn)) {
			page_2('Could not select the database.  Check database name, and permissions.');
			exit;
		}
		else {
			$tables=mysql_list_tables($_POST['mysql_database'], $mysql_conn);
			$num_tables=mysql_num_rows($tables);
			for($x=0;$x<$num_tables;$x++) {
				$table_name=mysql_tablename($tables, $x);
				$install_info = load_install_info();
				# having to alter the install info a bit to take into account the
				# prefix option...
				foreach ($install_info['tables'] as $k => $v) {
					$install_info['tables'][]= $_POST['mysql_prefix'].$v;
				}
				if(in_array($table_name, $install_info['tables'])) {
					$errors .= "<b>$table_name</b><br />\n";
					$fatal_error = 1;
				}
			}
		}
	}
	# end testing.
	
	# old installation found.  complain.
	# eventually we'll want to make a way to preserve this and magic fun goodness yummie yummie.
	if ($fatal_error==1) {
		$content.=<<<___eofh
			A previous version of Cynus MySQL tables has been found in the specified database.
			This version of Cynus is not able handle old tables, and expects a clean database.
			Please return to Page 2 of the installation and specify either a different database, or
			a different prefix to continue with a new installation of Cynus.<br /><br />
			
			To install over the old
			installation, you must first drop the Cynus tables listed below:<br />
			$errors
			
			<br /><br />
			<a href="#" onclick="javascript:history.back()">Page 2</a>
___eofh;
	}
	
	
	# no errors in sql, print out the whole ruddy installation goodness...mmMM
	else {
		# testing for errors
		if ($error) {
			$content .= '<big><a style="color: #ff0000">Error</a></big><br />' .
				'There has been an error in the submitted information.  Please check ' .
				'that you have left nothing blank.  See the messages below for more information.<br />' .
				$error . "<br /> <br />";
		}
		
		else {
			$content.=<<<___eofh
	Please complete the following configuration and double check the automatically detected values.
	<br /><br />
___eofh;
		}

		$install_info = load_install_info();
		$content.=<<<___eofh
<table cellspacing="3" cellpadding="3">
	<tr style="background-color:#CACACA;font-weight:bold;">
	<tr>
		<td class="table-header">Value</td>
		<td class="table-header">Description</td>
	</tr>\n
___eofh;
		$row1='#EEEEEE';
		$row2='#FFFFFF';
		foreach($install_info['config'] as $config_name => $config_info) {
			$value=set_default($config_info[0], $_POST[$config_name]);
			
			# adding spacers
			if ($config_info[2]=='spacer') {
				$content .= "<tr valign=\"top\" style=\"background-color:#cacaca; font-weight:bold;\"><td colspan=\"2\">$config_info[1]</td></tr>";
				#swap($row1, $row2);
				continue;
			}
				
			elseif($config_info[2]=='boolean') {
				$input = "<select name=\"$config_name\">\n";
				if($value==1) {
					$input.=<<<___eofh
	<option value="0">No</option>
	<option value="1" selected>Yes</option>
</select>
___eofh;
				}
				else {
					$input.=<<<___eofh
	<option value="0" selected>No</option>
	<option value="1">Yes</option>
</select>
___eofh;
				}
			}
			elseif($config_info[2]=='int') {
				$input = "<input type=\"text\" name=\"$config_name\" value=\"$value\" size=\"3\" />";
			}
			else{
				$input = "<input type=\"text\" name=\"$config_name\" value=\"$value\" size=\"35\" />";
			}
			if($config_info[3]==1) {$not_required='*';}
			else{$not_required='';}
			$content.=<<<___eofh
	<tr valign="top" style="background-color:$row1;">
		<td style="vertical-align:middle">$input</td>
		<td><span style="font-weight:bold;">$config_name $not_required</span> - $config_info[1]</td>
	</tr>\n
___eofh;
			swap($row1, $row2);
		}
		
		$content.=<<<___eofh
	</table>
	* - Not required<br />
	<input type="hidden" name="page" value="4" />
	<input type="submit" value="Continue" />
	</form>
___eofh;
		
	}
	
	return make_page("Cynus Installation: Page 3 -- Cynus Basics",  $content);
}


# add users
function page_4($error=FALSE) {
	# page 3 sanity checks...
	
	$install_info = load_install_info();
	foreach($install_info['config'] as $config_name => $config_info) {
		$value=set_default($config_info[0], $_POST[$config_name]);
		if ($config_info[3]!=1 && ($_POST[$config_name]===FALSE || $_POST[$config_name]=='')) {
			$error_msg.="$config_name cannot be empty!<br />\n";
		}
	}
	
	if ($error_msg) {
		page_3($error_msg);
		exit;
	}
	
	
	$content=<<<___eofh
	<form method="POST" action="{$_SERVER['PHP_SELF']}">


	
___eofh;

	foreach ($_POST as $k => $v) {
		if ($v!="") {
			$content .= "<input type=\"hidden\" name=\"$k\" value=\"" . htmlspecialchars (stripslashes ($v)) . "\">\n";
		}
	}
	
	if ($error) {
		$content.='<big><a style="color: #ff0000">Error</a></big><br />' .
			$error . '<br /><br />';
	}
		$su_username = set_default ('admin', $_POST['su_username']);
	
		$content.=<<<___eofh
		Please provide information for the super user account, who will have control over every 
		aspect of Cynus.  Because the password cannot be recovered if forgotten, it is vital that you chose
		a password that is secure, yet something you can remember.<br /><br />
		
		<table cellpadding = "3" cellspacing = "3">
		<tr>
			<td>Username:</td>
			<td><input type="text" name="su_username" value="$su_username"></td>
		</tr>
		<tr>
			<td>Password:</td>
			<td><input type="password" name="su_password1"></td>
		</tr>
		
		<tr>
			<td>Password Again:</td>
			<td><input type="password" name="su_password2"></td>
		</tr>
		
		<tr>
			<td>Real Name:</td>
			<td><input type="text" name="su_realname"></td>
		</tr>
		
		<tr>
			<td>Email Address:</td>
			<td><input type="text" name="su_email"></td>
		</tr>
		
		</table>
		
		<input type="hidden" name="page" value="5">
		<input type="submit" value="Continue">
		
___eofh;

	
	
	return make_page("Cynus Installation: Page 4 -- Super User Account", $content);
}

# review of the config, along with warnings/error if any
function page_5 ($error=FALSE) {


	if ($_POST['su_username']=="") { $error_msg.='User Name cannot be empty!<br />'; }
	if ($_POST['su_password1']=="" || $_POST['su_password2']=="") { $error_msg.='Both password fields must be completed!<br />'; }
	elseif ($_POST['su_password1']!=$_POST['su_password2']) { $error_msg .= 'Passwords must match!<br />'; }
	
	if ($_POST['su_realname']=="") { $error_msg.='Real Name cannot be empty!<br />'; }
	if ($_POST['su_email']=="") { $error_msg.='Email Address cannot be empty!<br />'; }
	
	if ($error_msg) {
		page_4($error_msg);
		exit;
	}
	
	
	$content=<<<___eofh
	<form method="POST" action="{$_SERVER['PHP_SELF']}">
	
___eofh;

#	foreach ($_POST as $k => $v) {
#		if ($v!="") {
#			$content .= "<input type=\"hidden\" name=\"$k\" value=\"" . htmlspecialchars (stripslashes ($v)) . "\">\n";
#		}
#	}
		
	$config = make_config();
	
	$filename = 'cynus_install_' . time () . '.php';
	if (!$fp=@fopen ($filename, 'w')) {
		$errors .= "Problem opening temp file.  Check permissions.<br />\n";
	}
	
	if (!@fwrite($fp, "<?php\n" . $config . "?>")) {
		$errors .= "Problem writing temp file.  Check permissions.<br />\n";
	}
	
	@fclose ($fp);

	if ($errors) {
		$content = '<a style="color: #ff0000">Error</a><br />' . $errors;
	}
	
	else {
		$content .= <<<___eofh
<input type="hidden" name="install_info_file" value="$filename">
$error_msg
Cynus is ready to start installation. You must press "Continue" for installation to finish. <br />
<br />
<input type="hidden" name="page" value="6">
<input type="submit" value="Continue">
___eofh;
	}
	
	return make_page("Cynus Installation: Page 5 -- Installation Review", $content);
}

# saves the config, and redirects to page 6
function page_6 () {
	$install_filename = $_POST['install_info_file'];
	
	# clearing out any possible variables.
	unset ($config);
	unset ($queries);
	
	include ($install_filename);
	
	umask (0000);

	$log_file = 'install_log_' . date ('n.j.Y.H.i.s') . '.php';
	$log_fp = @fopen ($log_file, 'w');
	
	fputs ($log_fp, "<?php\n#Starting Cynus Installation:" . date ('n.j.Y.g.i.s') . "\n" );
	
	# install the ../config.php file
	if (!$config_fp = @fopen ('../config.php', 'w')) {
		$errors .= "Cannot open to config file.<br />\n";
	}
	
	if (!@fputs ($config_fp, "<?php\n#Cynus Generated Config file.\n#DO NOT EDIT!\n\n")) {
		$errors .= "Cannot write to config file.<br />\n";
	}
	
	fputs ($log_fp, "# Config file...\n\n");
	
	foreach ($config as $k => $v) {
		if (!@fputs ($config_fp, "\$config['$k']=\"$v\";\n")) {
			$errors .= "Cannot write to config file.<br />\n";
		}
		else {
			# write to log file
			fputs ($log_fp, "# \$config['$k']=\"$v\";\n");
		}
	}
	
	fputs ($log_fp, "\n\n# SQL Queries...\n\n");
	
	if (!@fputs ($config_fp, "\n\n?>")) {
		$errors .= "Cannot write to config file.<br />\n";
	}
	
	@fclose ($config_fp);
#	chmod ('../config.php', 0600);
	
	# copy the modules dir
	#if (!copy_dir ('', '../modules')) {
	#	$errors .= "Could not copy modules.<br />\n";
	#}
	
	umask(000);
	if(!@mkdir('../modules', 0755)) {
		$error .= 'Cannot create the modules/ directory.';
	}
	else{
		if($mod_fp = @fopen ('../modules/index.php', wb)){
			$index_file = <<<___eofh
<?
 # GENERATED BY CYNUS INSTALL.
 # DO NOT EDIT OR REMOVE!!!
 #
?>

___eofh;
			if (@fputs ($mod_fp, $index_file)) {
				@chmod ('../modules/index.php', 0644);
			}
			else{
				$errors .= 'Cannot write to modules directory.';
			}
			@fclose ($mod_fp);
		}
		else{
			$error .= 'Cannot create the index.php files in the modules/ directory.';
		}
	}
	
	if(!@mkdir('../templates', 0755)) {
		$error .= 'Cannot create the templates/ directory.';
	}
	
	
	# conneting to sql server
	if(!$mysql_conn=@mysql_connect($config['sql_server'], $config['sql_user'], $config['sql_pass'])) {
		$errors .= 'Could not connect to MySQL server.<br />';
	}
	else {
		if(!@mysql_select_db($config['sql_db'], $mysql_conn)) {
			$errors .= 'Could not select the database.<br />';
		}
	}
	#stuff stuff into the mysql tables.
	foreach ($queries as $query) {
		$result = mysql_query ($query);
		fputs ($log_fp, "\$queries[$x]=\"$query\";\n");
		#fputs ($log_fp, "# " . @mysql_num_rows ($result) . "\n");
		if (@mysql_error()) {
			fputs ($log_fp, "# Errors:" . @mysql_error () . "\n");
			$errors.="$query<br />" . mysql_error() . "<br /><br />\n";
		}
	}
	
	@fclose ($log_fp);
	@chmod ($log_file, 0644);
	
	if (!$errors) {
		$content .= <<<___eofh
		
	Congratulations!  Cynus has been sucessfull installed.  Please secure your installation by running
	one of the included scripts, or by FTP, and <a href="../">login</a> to your new Cynus installation!
	
	
___eofh;

	unlink ($install_filename);
	}
	else {
		$content .= <<<___eofh
		<a style="color: #ff0000">Error</a><br />
		There was an error installing Cynus.  See below for details.<br /><br />
		$errors
		
___eofh;
	}
	
	return make_page("Cynus Installation: Installation", $content);
}

# prints congrats, errors, warnings, etc etc etc
# suggests to secure the installation, and log in.
function page_7 () {
	return make_page("Cynus Installation: Page 1", "Page One");
}

















##############################
#
# Displaying functions
#
##############################

function make_page ($title, $content, $head_extra=FALSE, $error=FALSE) {
	
	($error == 1) ? $title="Installation Error!" : $title=$title;
	
	print <<<___eofh
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>$title</title>
<link rel="stylesheet" type="text/css" href="../themes/blue/style.css" />
$head_extra
</head>

<body>
<div class="logo-area">
<a href="/brett/devel/cynus/"><img src="../themes/blue/images/crest_logo.png" class="logo" /></a>
$title
</div>

<table cellspacing="0" cellpadding="0" class="table">
	<tr>

		<td class="table-top-left-corner">&nbsp;</td>
		<td class="table-top">&nbsp;</td>
		<td class="table-top-right-corner">&nbsp;</td>
	</tr>
	<tr>
		<td class="table-left">&nbsp;</td>
		<td class="table-content">
		$content
		</td>
		<td class="table-right">&nbsp;</td>
	</tr>
	<tr>
		<td class="table-bottom-left-corner">&nbsp;</td>
		<td class="table-bottom">&nbsp;</td>
		<td class="table-bottom-right-corner">&nbsp;</td>
	</tr>
</table>

</body>
</html>
___eofh;
}





function create_config($prefix, $var_name, $value, $description, $section, $field_type, $extra) {
	
	# ugly hack.
	#$var_name = addslashes (stripslashes ($var_name));
	#$value = addslashes (stripslashes ($value));
	#$description = addslashes (stripslashes ($description));
	#$section = addslashes (stripslashes ($section));
	#$field_type = addslashes (stripslashes($field_type));
	#$extra = addslashes (stripslashes($extra));
	
        return ('INSERT into `'. $prefix . 'config` (`name`, `value`, `description`, `section`, `field_type`, `extra`) ' .
                "VALUES ('$var_name', '$value', '$description', '$section', '$field_type', '$extra')");
}

function make_config () {
	$install_info = load_install_info();
	
	# putting the mysql info in the config.php file
	$config['sql_server'] = $_POST['mysql_server'];
	$config['sql_user'] = $_POST['mysql_username'];
	$config['sql_pass'] = $_POST['mysql_password'];
	$config['sql_db'] = $_POST['mysql_database'];
	$config['sql_prefix'] = $_POST['mysql_prefix'];
	
	#adding the modules
	$module_num_array=array();
	$x=1;
	foreach($install_info['modules'] as $query) {
		$queries[] = "INSERT INTO `{$_POST['mysql_prefix']}modules` " . $query;
		array_push($module_num_array, $x);
		$x++;
	}
	
	# going through the stuff that needs to end up in the database.
	foreach($install_info['config'] as $config_name => $config_info) {
		if (!strstr ($config_name, 'space')) {
			$queries[] = create_config($_POST['mysql_prefix'], $config_name, $_POST[$config_name], $config_info[1],
					'base', $config_info[2], '');
		}
	}
	
	# this is here even though it's defined in load_config
	# so we only have to change things in one place
	# when the config changes.
	#$queries[] = $install_info['version'];
	#$queries[] = $install_info['cynus_version'];
	$queries[] = create_config($_POST['mysql_prefix'], 'version', $install_info['version'], 'The version of Cynus Base', 'base',
		'hidden', '');
		
	$queries[] = create_config($_POST['mysql_prefix'], 'config_version', $install_info['config_version'], 'The version of Cynus Config', 'base',
		'hidden', '');
			
	
	#creating the super user account
	$enc_pass=crypt($_POST['su_password1']);
	$permissions=implode(':', $module_num_array);
	$queries[]="INSERT into `{$_POST['mysql_prefix']}users` (`username`, `password`, `real_name`, `email`, `permissions`, `last_click`, `level`)" . 
		"VALUES ('{$_POST['su_username']}', '" . $enc_pass . "', '{$_POST['su_realname']}', '{$_POST['su_email']}', '$permissions', '', '4')";

	
	foreach ($config as $k => $v) {
		#$v = addslashes ($v);
		$install .= "\$config['$k']=\"$v\";\n";
	}
	
	
	$x = 0;
	#first doing the tables...
	foreach ($install_info['table_structure'] as $table_name => $v) {
		#$v = addslashes ($v);
		$install .= "\$queries[$x]=\"CREATE TABLE `{$_POST['mysql_prefix']}$table_name` $v\";\n";
		$x++;
	}
	
	# now doing the actual data.
	foreach ($queries as $v) {
		#$v = addslashes (stripslashes ($v));
		$v = addslashes ($v);
		$install .= "\$queries[$x]='$v';\n";
		$x++;
	}
	


	return $install;
}

/*******************************
Load Install Info - array load_install_info()
The information for the install is put here so that
it can be all organized into one spot
*******************************/
function load_install_info() {
	$info=array();
	$info['tables']=array('config', 'messages', 'modules', 'startup', 'users','display','handlers','templates');
	
	# removed the CREATE TABLE bits.
	# CREATE TABLE `config` 
	$info['table_structure']['config']="(
  `name` varchar(30) NOT NULL default '',
  `value` text NOT NULL,
  `description` text NOT NULL,
  `section` varchar(30) NOT NULL default '',
  `field_type` varchar(20) NOT NULL default '',
  `extra` text NOT NULL,
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;";

	$info['table_structure']['messages'] = "(
  `to` int(11) NOT NULL default '0',
  `from` int(11) NOT NULL default '0',
  `subject` text NOT NULL,
  `message` text NOT NULL,
  `date` int(11) NOT NULL default '0',
  `is_read` int(11) NOT NULL default '0',
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;";

	$info['table_structure']['modules'] = "(
  `friendly_name` varchar(30) NOT NULL default '',
  `name` varchar(30) NOT NULL default '',
  `location` varchar(100) NOT NULL default '',
  `icon` varchar(50) NOT NULL default '',
  `type` varchar(30) NOT NULL default '',
  `min_level` int(11) NOT NULL default '1',
  `version` varchar(11) NOT NULL default '0',
  `config_version` int(11) NOT NULL default '0',
  `author` tinytext NOT NULL,
  `deps` tinytext NOT NULL,
  `info` text NOT NULL,
  `site` tinytext NOT NULL,
  `email` tinytext NOT NULL,
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;";

	$info['table_structure']['startup'] = "(
  `name` text NOT NULL,
  `file` varchar(50) NOT NULL default '',
  `function` varchar(50) NOT NULL default '',
  `module` varchar(50) NOT NULL default '',
  `min_level` int(11) NOT NULL default '0',
  `enabled` int(2) NOT NULL default '0',
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;";

	$info['table_structure']['users'] = "(
  `username` varchar(20) NOT NULL default '',
  `password` varchar(50) NOT NULL default '',
  `real_name` varchar(50) NOT NULL default '',
  `email` varchar(75) NOT NULL default '',
  `permissions` text NOT NULL,
  `last_click` int(11) NOT NULL default '0',
  `level` int(11) NOT NULL default '0',
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;";

	$info['table_structure']['display'] = "(
`module` INT DEFAULT '0' NOT NULL ,
`display_function` CHAR( 75 ) NOT NULL ,
`includes` TEXT NOT NULL ,
`extra_url` TEXT NOT NULL ,
`active` INT DEFAULT '1' NOT NULL ,
`id` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY ( `id` )
) TYPE=MyISAM AUTO_INCREMENT=1 ;";
	
	$info['table_structure']['handlers'] = "(
  `signal` varchar(75) NOT NULL default '',
  `name` varchar(100) NOT NULL default '',
  `module` varchar(75) NOT NULL default '',
  `handler` varchar(100) NOT NULL default '',
  `includes` text NOT NULL,
  `active` int(1) NOT NULL default '1',
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;";

	$info['table_structure']['templates'] = "(
  `name` char(50) NOT NULL default '',
  `file` char(50) NOT NULL default '',
  `default` int(1) NOT NULL default '0',
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;";
	
	#stuff for the config table
	
#	cynus_full_url
#	cynus_base_dir
#	url_base_dir
#	doc_root
#	( 777'd dir (randomly created name as default) )
#
#	main_columns
#	theme???
#
#	modules_url
#	use_proxy
#	proxy
#	proxy_port
	
	$cynus_base_dir = str_replace ('/install/install.php', '', $_SERVER['SCRIPT_FILENAME']);
	$cynus_full_url = "http://{$_SERVER['HTTP_HOST']}" . str_replace ($_SERVER['DOCUMENT_ROOT'], '', $cynus_base_dir);
	$url_base_dir = str_replace ($_SERVER['DOCUMENT_ROOT'], '', $cynus_base_dir);

	
	$info['config']['space0']=array('','District and Webmaster Information','spacer', 1);

	$info['config']['district']=array('','The name of your school district.','text',0);
	$info['config']['webmaster']=array('','The webmaster maintaining this installation of Cynus.','text',0);
	$info['config']['webmaster_email']=array('','The email address of the webmaster.','text',0);
	
	$info['config']['space1']=array('','Cynus Installation Information','spacer', 1);

	$info['config']['cynus_full_url']=array($cynus_full_url,'The full url to Cynus. (For example, if Cynus is located at http://www.yourdomain.com/admin/cynus, then that would be this value.  No trailing slash.)','text',0);
	$info['config']['cynus_base_dir']=array($cynus_base_dir,'The base directory of Cynus on the server. (For example, if Cynus is installed at /var/www/cynus, then that would be this value. No trailing slash.)', 'text',0);
	$info['config']['url_base_dir']=array($url_base_dir,'The base directory of Cynus in relation to a URL. (For example, if Cynus is installed at http://www.yourdomain.com/admin/cynus then the value of this should be "/admin/cynus".  No trailing slash.)','text',0);
	$info['config']['doc_root']=array("{$_SERVER['DOCUMENT_ROOT']}",'The document root of your webserver. (For example, if all of your pages are stored in /var/www, then that would be the value. No trailing slash.)','text',0);
#	$info['config']['mod_dir']=array('modules_' . rand (0, 99999),'The name of the directory to store modules.  Do not change this value unless you know what you are doing.','text',0);
	
#	$info['config']['space2']=array('', 'Module Server Connection', 'spacer', 1);

#	$info['config']['modules_url']=array('FIXME','The address of the module server to connect.  Do not change this unless you know what you are doing.','text',0);
#	$info['config']['use_proxy']=array('','If your server requires a proxy to connect to the internet, set this to Yes.','boolean',0);
#	$info['config']['proxy']=array('','This is the proxy address.','text',1);
#	$info['config']['proxy_port']=array('','This is the proxy port.','int',1);
	
	$info['config']['space3']=array('', 'Visual Properties', 'spacer', 1);
	
	$info['config']['main_columns']=array('5','Max columns of icons to display.','int',0);
	$info['config']['theme']=array('default','The default theme you want to use.','text',0);

	
	# INSERT INTO `modules` bits were removed for the prefix option....
	# we deal with that when we write the batch file.
	$info['modules']['info']="VALUES ('My Info', 'info', 'info.php', '/images/info.png', 'base', 2, '" . CYNUS_VERSION . "', '" . CONFIG_VERSION . "', 'Cynus Development Team', '', 'Displays a user\'s information and allows them to edit it.', 'http://cynus.sourceforge.net/', 'hide@address.com', '');";
	$info['modules']['message']="VALUES ('Messaging', 'message', 'messages.php', '/images/messages.png', 'base', 2, '" . CYNUS_VERSION . "', '" . CONFIG_VERSION . "', '', '', '', '', '', '');";
	$info['modules']['message_send_permission']="VALUES ('Message Send Permission', 'message_send_permission', 'messages', '', 'pseudo', 2, '" . CYNUS_VERSION . "', '" . CONFIG_VERSION . "', '', '', '', '', '', '');";
#	$info['modules']['modules']="VALUES ('Modules', 'modules', 'admin.php?action=modules', '/images/modules.png', 'base', 4, '" . CYNUS_VERSION . "', '" . CONFIG_VERSION . "', 'The Cynus Development Team', '', 'Don\'t remove this.', 'http://cynus.sf.net', 'hide@address.com');";
	$info['modules']['users']="VALUES ('Users', 'users', 'admin.php?action=users', '/images/users.png', 'base', 3, '" . CYNUS_VERSION . "', '" . CONFIG_VERSION . "', 'The Cynus Development Team', '', 'Don\'t remove this.', 'http://cynus.sf.net', 'hide@address.com', '');";
	$info['modules']['templates']="VALUES ('Templates', 'templates', 'templates.php', '/images/templates.png', 'base', 2, '" . CYNUS_VERSION . "', '" . CONFIG_VERSION . "',  'Cynus Development Team', '', 'Handles the templates used by the page displayer.', 'http://cynus.sourceforge.net/', 'hide@address.com', '');";
	$info['modules']['config']="VALUES ('Configuration', 'config', 'admin.php?action=config', '/images/config.gif', 'base', 4, '" . CYNUS_VERSION . "', '" . CONFIG_VERSION . "', 'Cynus Development Team', '', 'The Configuration module allows you to edit your configuration variables through an interface instead of through direct manipulation of the mysql tables.', 'http://cynus.sourceforge.net/', 'hide@address.com', '');";	
	$info['version']=CYNUS_VERSION;
	$info['config_version']=CONFIG_VERSION;
	
	return $info;
}


# duplicated for installation.
function set_default($default, $new_value) {
	if($new_value != "") {
		return $new_value;
	}
	else{
		return $default;
	}
}

function swap(&$var1, &$var2) {
	$temp=$var1;
	$var1=$var2;
	$var2=$temp;
}

function copy_dir($from, $to) {
	if(@mkdir($to, 0755)) {
		if($dp=opendir($from)) {
			while($file=readdir($dp)) {
				if($file!=".." && $file != ".") {
					if(is_dir("$from/$file")) {
						copy_dir("$from/$file", "$to/$file");
					#	print "Copying $from/$file to $to/$file<br />\n";
					}
					if(is_file("$from/$file")) {
						copy("$from/$file", "$to/$file");
					#	print "$from/$file to $to/$file<br />\n";
					}
				}
			}
		}
	}
	return TRUE;
}

?>
Return current item: Cynus