Location: PHPKode > projects > Spam free PHP GuestBook > guestbook/gb/admin/install/index.php
<?php
/*
Copyright (c) 2008 http://ramui.com. All right reserved.
This product is protected by copyright and distributed under licenses restricting copying, distribution. Permission is granted to the public to download and use this script provided that this Notice and any statement of authorship are reproduced in every page on all copies of the script.
*/
include "../../include/functions.php";
class install
{
private function install_now()
{
    $dbname=fw_strip_slashes(trim($_POST['dbname']));
    $dbuser=fw_strip_slashes(trim($_POST['dbuser']));
    $dbpw=fw_strip_slashes(trim($_POST['dbpw']));
    $savedpw=fw_strip_slashes($_POST['password']);
    $pw=md5($savedpw);
    $admin=fw_strip_slashes($_POST['admin']);
    $email=fw_strip_slashes($_POST['email']);
    $prefix=fw_strip_slashes(trim($_POST['prefix']));
    $dbhost='localhost';
    $dt=time();
    if (!(empty($_POST['dbhost']))){$dbhost=fw_strip_slashes(trim($_POST['dbhost']));}
    if(!(@mysql_connect($dbhost, $dbuser, $dbpw))){$this->inserror("Mysql connection error:","");}
    if(!(@mysql_selectdb($dbname))){$this->inserror("Database not found:","");}
    if(version_compare(phpversion(),'5.2.1')===-1){$this->inserror('Inadequate system:','PHP version requirement is 5.2.1 or higher. Your server php version is '.phpversion().'<br />Please contact with your hosting provider.');}
    $result=@mysql_query("select version() as ve");
    if($result!==false){
        $r=mysql_fetch_object($result);
        if(substr($r->ve,0,1)<5){$this->inserror('Inadequate system:','MYSQL version requirement is 5.0 or higher. Your MYSQL server version is '.$r->ve.'<br />Please contact with your hosting provider.');}}

    if(mysql_num_rows(mysql_query("SHOW TABLES LIKE \"$prefix%\""))) {$this->inserror("Table already exist", "Please use different table prefix.");}

    $filename="../../database/config.php";
    $handle=@fopen($filename,'w');
    if (!(@fwrite($handle,"<?php\n"))){
        @fclose($handle);
        $this->inserror("Error in file permission", "Please set chmod to &quot;777&quot; or &quot;0777&quot; of the file &quot;admin/database/config.php&quot;. Start installation again.");
    }
    fwrite($handle,'$dbhost=');
    fwrite($handle,"'$dbhost';\n");
    fwrite($handle,'$dbuser=');
    fwrite($handle,"'$dbuser';\n");
    fwrite($handle,'$dbpw=');
    fwrite($handle,"'$dbpw';\n");
    fwrite($handle,'$dbname=');
    fwrite($handle,"'$dbname';\n");
    fwrite($handle,'$prefix=');
    fwrite($handle,"'$prefix';\n");
    fwrite($handle,'?>');
    fclose($handle);

    $str=file("query.txt");
	$random=fw_random();
	$random=substr($random,0,12);
    for($i=0; $i<count($str); $i++){
            if(!empty($str[$i])){
				$query=str_replace('_RANDOM_',$random,str_replace('_DATE_',$dt,str_replace("prefix_",$prefix,trim($str[$i]))));
				$result = @mysql_query($query);
				if(!($result)){$this->inserror("Error executing query string => ".($i+1));}}
    }

    $selfurl = str_replace("www.","", strtolower(getenv("HTTP_HOST")));
    $query = sprintf("INSERT INTO ".$prefix."admin (user, password, email) VALUES ('%s', '%s', '%s')",mysql_real_escape_string($admin),mysql_real_escape_string($pw),mysql_real_escape_string($email));
    $result = @mysql_query($query);
    if(!($result)){$this->inserror("Error inserting user data in table user");}

    //Insert site settings.
    $description='This PHP guestbook script allows only registered users to post a new message or put comments over messages';
	$keywords='GuestBook,script,PHP,PHP GuestBook,guestbook script,PHP guestbook script';
	$query = "INSERT INTO ".$prefix."settings (sitename, description, keywords, defaultstyle, postsize, commentsize, listcount, imagecount, imagewidth, imageheight, allowemail, approval) VALUES (";
	$query.="'Guestbook', '$description', '$keywords', 'red', 5000, 1000, 5, 1, 400, 250, 'Y', 'A')";
    $result = @mysql_query($query);
    if(!($result)){$this->inserror("Error inserting user data in table ".$prefix."settings");}

	//Confirmation
    $from='hide@address.com';
    $subject='PHP-Guestbook has been successfully installed';
    $headers="From: hide@address.com\r\n";
    $message="Congratulations! Your PHP Guestbook script has been successfully installed.\r\n\r\nLogin information:\r\n------------------------------------------\r\n";
    $message.="User name: $admin";
    $message.="\r\nPassword: $savedpw";
    $message.="\r\nLogin url: http://".getenv('HTTP_HOST').fw_get_docroot()."gb/admin/index.php";
    $message.="\r\n------------------------------------------\r\n\r\nThank you.\r\nArchan Ghosal.(http://ramui.com)";
    @mail($email,$subject,$message,$headers);

    $self_path="http://".getenv("HTTP_HOST").str_replace('install/index.php', 'index.php', $_SERVER['SCRIPT_NAME']);
    @header("Location: $self_path");
}

private function build_head($title, $script)
{
    $leftmenu="";
    include "../include/head.php";
}

private function build_footer()
{
    include "../include/footer.html";
    exit;
}

public function get_install()
{
    if(filesize("../../database/config.php")>10){
        $self_path="http://".getenv('HTTP_HOST').fw_get_docroot()."gb/admin/index.php";
        @header("Location: $self_path");
        exit;}
    if((empty($_POST['dbname']))||(empty($_POST['dbuser']))){
        $this->build_head('Install','<script type="text/javascript" src="../script/install.js"></script>');
        include "frminstall.php";
        $this->build_footer();
    }
    else{
        $this->install_now();
    }
}

private function inserror($installerr_heading, $installerr_description='')
{
        if($installerr_description==''){$installerr_description=mysql_error();}
        $this->build_head('Installation error','<script type="text/javascript" src="../script/install.js"></script>');
        include "frminstall.php";
        $this->build_footer();
}
}
$obj=new install();
$obj->get_install();
?>
Return current item: Spam free PHP GuestBook