Location: PHPKode > scripts > DNP script > install/install.php
<?PHP
/*
    This file is part of DNP Script, an open source domain portfolio manager.
    Copyright (C) 2011  Robert Picard

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
	
	error_reporting(0);
	// Get the information we need that was sent by the form
	$db_host	= $_POST['db_host'];
	$db_user	= $_POST['db_user'];
	$db_pass	= $_POST['db_pass'];
	$db_name	= $_POST['db_name'];
	
	// Connect to MySQL
	$con = mysql_connect($db_host, $db_user, $db_pass);

	// Error handling in case a connection can't be established.
	if(!$con){
		die('<span class="message_warning">Error connecting to MySQL: '.mysql_error().'</span>');
	}
	
	// The script will sanitize this input so that it's safe to
	// send in a MySQL query.
	$s_title		= mysql_real_escape_string($_POST['s_title']);
	$s_description	= mysql_real_escape_string($_POST['s_description']);
	$s_email		= mysql_real_escape_string($_POST['s_email']);
	$s_analytics	= mysql_real_escape_string($_POST['s_analytics']);
	
	// Make sure the passwords match. Javascript on the install page
	// should take care of this but we'll check here too just in case.
	if($_POST['s_pass'] != $_POST['s_pass2']){
		die('<span class="message_warning">Passwords do not match.</span>');
	}
	
	// Encrypt the password so it can be stored safely in the database.
	$s_pass		= sha1(md5($_POST['s_pass']));
		
	// Here we're creating the database. Our query is wrapped in an
	// if statement to let us handle any errors that may arise.
	if(!mysql_query('CREATE DATABASE '.$db_name, $con)){
		die('<span class="message_warning">Error creating database: '.mysql_error().'</span>');
	}
		
	// Select the database we've just created.
	mysql_select_db($db_name, $con);
		
	// The $sql variable and the following query (wrapped in an if statement
	// for error handling) will create the settings table.
	$sql = 'CREATE TABLE dnp_settings
			(
			id int(8) AUTO_INCREMENT,
			name varchar(255),
			title varchar(255),
			value varchar(255),
			PRIMARY KEY (id)
			)';
				
	if(!mysql_query($sql)){
		die('<span class="message_warning">Error creating table "dnp_settings": '.mysql_error().'</span>');
	}
		
	// The now reassigned $sql variable and the following query (wrapped in an
	// if statement for error handling) will create the domains table.
	$sql = 'CREATE TABLE dnp_domains
			(
			id int(8) AUTO_INCREMENT,
			domain varchar(255),
			registrar varchar(255),
			expiry date,
			price varchar(255),
			PRIMARY KEY (id)
			)';
				
	if(!mysql_query($sql)){
		die('<span class="message_warning">Error creating table "dnp_domains": '.mysql_error().'</span>');
	}
		
	// These queries are used to populate the "dn_settings" table with the
	// script's settings.
	$sql1	= "INSERT INTO dnp_settings (name, title, value)
					VALUES ('title', 'Site Title', '$s_title')";
	$sql2	= "INSERT INTO dnp_settings (name, title, value)
					VALUES ('description', 'Site Description', '$s_description')";
	$sql3	= "INSERT INTO dnp_settings (name, title, value)
					VALUES ('password', 'Password', '$s_pass')";
	$sql4	= "INSERT INTO dnp_settings (name, title, value)
					VALUES ('email', 'Email Address', '$s_email')";
	$sql6	= "INSERT INTO dnp_settings (name, title, value)
					VALUES ('analytics', 'Google Analytics', '$s_analytics')";
		
	if(!mysql_query($sql1)){
		die('<span class="message_warning">Error populating table "dnp_settings" with query 1: '.mysql_error().'</span>');
	}
	if(!mysql_query($sql2)){
		die('<span class="message_warning">Error populating table "dnp_settings" with query 2: '.mysql_error().'</span>');
	}
	if(!mysql_query($sql3)){
		die('<span class="message_warning">Error populating table "dnp_settings" with query 3: '.mysql_error().'</span>');
	}
	if(!mysql_query($sql4)){
		die('<span class="message_warning">Error populating table "dnp_settings" with query 4: '.mysql_error().'</span>');
	}
	if(!mysql_query($sql6)){
		die('<span class="message_warning">Error populating table "dnp_settings" with query 6: '.mysql_error().'</span>');
	}
	
	// Now the script will create config.php and write the database credentials
	// in it so that after the installation it remembers how to access it.
	$config = fopen('../config.php', 'w')
		or die('<span class="message_warning">Can\'t open file: config.php</span>');
		
	$file_contents = '<?PHP
/*
    This file is part of DNP Script, an open source domain portfolio manager.
    Copyright (C) 2011  Robert Picard

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
$db_host	= \''.$db_host.'\';
$db_user	= \''.$db_user.'\';
$db_pass	= \''.$db_pass.'\';
$db_name	= \''.$db_name.'\';
?>';
		
	fwrite($config, $file_contents)
		or die('<span class="message_warning">Can\'t write to file: config.php</span>');
		
	fclose($config);
	
	// We did it! The installation is complete. Now we'll send a success message back to the install page.
	echo('<span class="message_success">Congratulations! You\'ve successfully installed DNP Script. You should remove the /install directory from your server now. After that I recommend you get started in the <a href="../admin/">Admin Panel</a> to configure the site. Thanks for using DNP Script!</span>');
Return current item: DNP script