Location: PHPKode > projects > JaxBlog > JaxBlog/app/install/install_database/install_database.php
<?php

require_once('../../../config.php');
require_once(FOLDER_RELATIVE_COMMON . 'html.php');
require_once(FOLDER_RELATIVE_COMMON . 'database.php');

// Check for installation previously completed, allow reconfig if authorized.
if (FLAG_INSTALLED == 'Y') {
	require_once(FOLDER_RELATIVE_COMMON . 'authorization.php');
}

$errors = '';
$request = isset($_GET['request']) ? $_GET['request'] : '';

function errorTrapErrors($code, $msg, $file, $line, $context) { die(); }

if ($request == 'xml') {
	
	// Stage: what level of connectivity is available.
	//  0 = missing database and admin tool credentials
	//  1 = unable to get a connection
	//  2 = can get a connection, tables not available
	//  3 = good to go
	$stage = 3;
	
	// See if creds have changed.
	$database_username = USERNAME;
	$database_password = PASSWORD;
	$admin_username = ADMINISTRATIVE_USERNAME;
	$admin_password = ADMINISTRATIVE_PASSWORD;
	$database_credentials = '<span class="setup_invalid">credentials invalid</span>';
	if ($database_username == 'ENTER A USERNAME' || $database_password == 'ENTER A PASSWORD' || $admin_username == 'ENTER A USERNAME' || $admin_password == 'ENTER A PASSWORD') {
		if ($stage == 3) $stage = 0;
	} else {
		$database_credentials = '<span class="setup_valid">credentials updated</span>';
	}
	
	// Attempt to establish a database connection, suppress errors.
	if ($stage == 3) {
		$connection = NewADOConnection(VENDOR);
		if (VENDOR == 'sqlite') {
			@$connection->PConnect(FILE_SQLITE, USERNAME, PASSWORD) or $stage = 1;
		} else {
			@$connection->PConnect(SERVER, USERNAME, PASSWORD, DATABASE) or $stage = 1;
		}
	}
	
	// If successful grabbing a connection, try to grab a rowcount to see if the stories table exists.
	if ($stage == 3) {
		@$rows = $connection->GetArray('SELECT count(*) as rowcount FROM stories', array()) or $stage = 2;
		if (!($rows && isset($rows) && count($rows) > 0)) $stage = 2;
	}
	
	// Set up the connectivity messages.
	$database_check_connection = '<span class="setup_invalid">unable to connect</span>';
	$database_check_table_access = '<span class="setup_invalid">unable to access tables</span>';
	
	if ($stage == 3) {
		$database_check_table_access = '<span class="setup_valid">able to access tables</span>';
	}
	if ($stage == 3 || $stage == 2) {
		$database_check_connection = '<span class="setup_valid">able to connect</span>';
	}
	
	$database_vendor   = VENDOR;
	$database_server   = SERVER;
	$database_name     = DATABASE;
	
	header('Content-Type: text/xml');
	$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
	$xml .= "<root>\n";
	
	$xml .= "	<controls_id>\n";
	$xml .= "		<record><key>database_check_connection</key><value><![CDATA[$database_check_connection]]></value></record>\n";
	$xml .= "		<record><key>database_check_table_access</key><value><![CDATA[$database_check_table_access]]></value></record>\n";
	$xml .= "		<record><key>database_credentials</key><value><![CDATA[$database_credentials]]></value></record>\n";
	$xml .= "	</controls_id>\n";
	
	$xml .= "	<controls_input>\n";
	
	$xml .= "		<record><key>database_vendor</key><value><![CDATA[$database_vendor]]></value></record>\n";
	$xml .= "		<record><key>database_server</key><value><![CDATA[$database_server]]></value></record>\n";
	$xml .= "		<record><key>database_name</key><value><![CDATA[$database_name]]></value></record>\n";
	$xml .= "		<record><key>database_username</key><value><![CDATA[$database_username]]></value></record>\n";
	$xml .= "		<record><key>database_password</key><value><![CDATA[$database_password]]></value></record>\n";
	
	$xml .= "		<record><key>admin_username</key><value><![CDATA[$admin_username]]></value></record>\n";
	$xml .= "		<record><key>admin_password</key><value><![CDATA[$admin_password]]></value></record>\n";
	
	$xml .= "	</controls_input>\n";
	
	$xml .= "</root>\n";
	echo $xml;
	exit;
}

// Write config changes to config.php then redirect back.
// The redirect is necessary to get a fresh read of config.php at the top of this script.
if ($request == 'configure') {
	require_once(FOLDER_RELATIVE_COMMON . 'filesystem.php');
	require_once(FOLDER_RELATIVE_COMMON . 'configurator.php');
	writeConfigurationChangeToDisk();
	header('location:install_database.php');
	exit;
}

// Check for create tables action, and if called, create the tables then redirect back.
$check_action = isset($_GET['check_action']) ? $_GET['check_action'] : '';
if ($check_action == "create") {
	require_once(FOLDER_RELATIVE_COMMON . 'refolder.php');
	
	$contents = '';
	if (stripos(VENDOR, "postgres") > -1) {
		$contents = fileRead(FOLDER_RELATIVE_BASE . "resource/install/CREATE-TABLES-postgres.sql");
	} else if (stripos(VENDOR, "mysql") > -1) {
		$contents = fileRead(FOLDER_RELATIVE_BASE . "resource/install/CREATE-TABLES-mysql.sql");
	} else if (stripos(VENDOR, "sqlite") > -1) {
		$contents = fileRead(FOLDER_RELATIVE_BASE . "resource/install/CREATE-TABLES-sqlite.sql");
	}
	
	$contents = str_replace("\r", '', $contents);
	$multisql = explode(";\n", $contents);
	
	foreach ($multisql as $sql) {
		$sql = trim($sql);
		if (isset($sql) && $sql != '') {
			databaseExecute($sql, null);
		}
	}
	
	// We would do the installer justice to run these inserts after creating tables.
	// These inserts should make use of the sequences created by the db vendor or
	// via adodb in the case of mysql.
	databaseExecuteReturnId("INSERT INTO pages (id, parent_id, title) VALUES (?, 0, 'Home');", array(0), "pages_id_seq");
	databaseExecuteReturnId("INSERT INTO files (id, parent_id, title, path) VALUES (?, 1, 'File Root','../../../upload/');", array(0), "files_id_seq");
	databaseExecuteReturnId("INSERT INTO stories (id, title, content) VALUES (?, 'Welcome', 'Welcome to JaxCMS, have a look around');", array(0), "stories_id_seq");
	databaseExecuteReturnId("INSERT INTO stories (id, title, content) VALUES (?, 'Admin Section', 'To log into the admin section, try username: admin and password: admin');", array(0), "stories_id_seq");
	databaseExecute("INSERT INTO pagetostory VALUES (1, 1);", null);
	databaseExecute("INSERT INTO pagetostory VALUES (1, 2);", null);
	databaseExecuteReturnId("INSERT INTO calendar (id, scheduled_date, title, description) VALUES (?, '2007-01-15 05:00', 'Test Event 1', 'This is just a test calendar event, feel free to delete me.');", array(0), "calendar_id_seq");
	databaseExecuteReturnId("INSERT INTO calendar (id, scheduled_date, title, description) VALUES (?, '2007-01-18 19:45', 'Test Event 2', 'Yet another test calendar event, please delete me!');", array(0), "calendar_id_seq");
	
	databaseExecute("INSERT INTO panes (id, name, description, display) VALUES (1, 'Main Navigation Bar', 'The navigation bar that displays along the top or side of each page, and contains links to top level pages.  Is smart about displaying nested pages.', 1);", array());
	databaseExecute("INSERT INTO panes (id, name, description, display, page_id, disabled) VALUES (2, 'Navigation Bar Child Pages', 'Displays links to child pages of the named page.', 2, 1, 'Y');", array());
	databaseExecute("INSERT INTO panes (id, name, description, display, page_id, disabled) VALUES (3, 'Navigation Bar Stories', 'Displays links for stories associated with the selected page.', 3, 1, 'Y');", array());
	databaseExecute("INSERT INTO panes (id, name, description, display, story_id, disabled) VALUES (4, 'Top Story', 'Any story you''d like to highlight separate from and alongside normal content.', 4, 1, 'Y');", array());
	databaseExecute("INSERT INTO panes (id, name, description, display, folder_path) VALUES (5, 'Slideshow', 'Provide a directory on disk that contains images (gif, jpg, png), and they will display in a slideshow pane.', 5, '../../../module/JaxSlideshow/image/');", array());
//	databaseExecute("INSERT INTO panes (id, name, description, display) VALUES (6, 'Videos', 'A set of links to YouTube that, when clicked by the user, will cause the video to play embedded.', 6);", array());
	databaseExecute("INSERT INTO panes (id, name, description, display) VALUES (7, 'Calendar', 'Causes a mini calendar pane to appear, populated with events entered on the admin Calendar page.', 7);", array());
//	databaseExecute("INSERT INTO panes (id, name, description, display) VALUES (8, 'Search', 'Represents a search box that will allow users to query stories.', 9);", array());
	databaseExecute("INSERT INTO panes (id, name, description, display, disabled) VALUES (9, 'Login', 'A login area.  Enable this pane to allow users to access private content.', 10, 'Y');", array());
	databaseExecute("INSERT INTO panes (id, name, description, display, content, disabled) VALUES (10, 'Hotlinks', 'Contains links to other points of interest offsite.', 100, '<a href=''http://ubuntu.com''>Ubuntu Linux</a><br />\n<a href=''http://postgresql.org''>PostgreSQL</a><br />\n<a href=''http://php.net''>PHP</a><br />', 'Y');", array());
	databaseExecute("INSERT INTO panes (id, name, description, display, content) VALUES (11, 'Sponsors', 'Contains &lt;img&gt; tags with links to sponsor websites.', 100, '<a href=''http://ubuntu.com''>Ubuntu Linux</a><br />\n<a href=''http://postgresql.org''>PostgreSQL</a><br />\n<a href=''http://php.net''>PHP</a><br />');", array());
	
	header('location:install_database.php');
	exit;
}

require_once(FOLDER_RELATIVE_COMMON . 'builder-install.php');

$header  = '<script language="Javascript" type="text/javascript" src="install_database.js"></script>' . "\n";
$onload  = 'initializePage();';
$title   = 'Database';
$content = fileRead('install_database.html');
$success = 3;
$page    = buildInstallPage($header, $onload, $title, $content, $success);

echo $page;
?>
		
Return current item: JaxBlog