Location: PHPKode > projects > Php4dvd - movie database > php4dvd/install/files/create_config.php
<?php
/**
================================================================================
 LISENCE
================================================================================

    This file is part of php4dvd.

    php4dvd 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 3 of the License, or
    (at your option) any later version.

    php4dvd 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.

    You should have received a copy of the GNU General Public License
    along with php4dvd. If not, see <http://www.gnu.org/licenses/>.
**/

/**
    This page of the installer will display several fields to fill in information
    about your database. Fill them in and click submit and it will create the
    settings.php file in the above directory.
 */

//Global db settings
$db = array();

//Function to display the needed fields for config file
function config_form ($server,$db,$user,$pass) {
  echo "  <p>
                <p>Please fill out the following fields so that we can create your settings file.</p>

                <p>Note: If your hosting company requires you to create the database through
                their web interface you can create it, and then fill in the settings here. If the database
                does not exist already we will try to create it during the installer.</p>

                <form method='POST' action='?go=new&sub=post' onsubmit='if(verify(this)) return true; else alert(\"Some fields are not filled in correctly!\"); return false;'>
                  <table>
                    <tr id='tr_dbserver'>
                      <td width='102'>DB Server: </td>
                      <td><input type='text' name='dbserver' id='text_dbserver_1' value='".$server."' class='textfield'></td>
                    </tr>
                    <tr id='tr_dbname'>
                      <td>DB Name: </td>
                      <td><input type='text' name='dbname' id='text_dbname_1' value='".$db."' class='textfield'></td>
                    </tr>
                    <tr id='tr_dbuser'>
                      <td>DB User: </td>
                      <td><input type='text' name='dbuser' id='text_dbuser_1' value='".$user."' class='textfield'></td>
                    </tr>
                    <tr id='tr_dbpass'>
                      <td>DB Pass: </td>
                      <td><input type='password' name='dbpass' id='text_dbpass_0' value='".$pass."' class='textfield'></td>
                    </tr>
                    <tr>
                      <td><input type='submit' value='Submit' name='submit' class='button'></td>
                      <td>&nbsp;</td>
                    </tr>
                  </table>
                </form></p>";
}

//Function that grabs the posted values and puts them into the config file
function post_config () {
    //Reference the settings and then create the config file
    global $db;

    create_config($db["server"],$db["name"],$db["user"],$db["pass"],"new");
}

//Function that grabs the values for the config file by importing the existing config.php
function include_config () {
        //Get the config file
        $filename=  loadConfig();

        //If its blank they should be in the new installer
        if ($filename=='')
            header('Location: ?go=new');

        //Get the file
        $loc = "";
        include($filename);

        //Overwrite it
        create_config($settings['db']['host'],$settings['db']['name'],$settings['db']['user'],$settings['db']['pass'],"upgrade");
}

//This is the actual function that creates the config file. This copies the default file and replaces the db settings with
//the ones provided by the form or from the old config file.
function create_config ($host,$name,$user,$pass,$type) {
    $filename = '../config/config.default.php';

    // If the default config does not exist, stop installation
    if(!file_exists($filename)) {
        echo "Installation cannot continue, because the file <tt>".$filename."</tt> was not found.";
        exit();
    }

    // Open the config.default.php file
    $handle = fopen($filename, "r");
    $contents = @fread($handle, filesize($filename)) or die("Can't read the contents of <tt>".$filename."</tt>.");
    fclose($handle);

    // Change the settings of the default config file
    $contents = preg_replace("/settings\['db'\]\['host'\] = \"localhost\";/", "settings['db']['host'] = \"".$host."\";", $contents);
    $contents = preg_replace("/settings\['db'\]\['name'\] = \"php4dvd\";/", "settings['db']['name'] = \"".$name."\";", $contents);
    $contents = preg_replace("/settings\['db'\]\['user'\] = \"root\";/", "settings['db']['user'] = \"".$user."\";", $contents);
    $contents = preg_replace("/settings\['db'\]\['pass'\] = \"\";/", "settings['db']['pass'] = \"".$pass."\";", $contents);

    // Write the new config file
    $setFile = "../config/config.php";
    $fh = @fopen($setFile, 'w') or die("Can't create file <tt>".$setFile."</tt>");
    fwrite($fh, $contents);
    fclose($fh);

	//Make sure that the new file exists
    if (is_readable($setFile)) {
        chmod($setFile,0777);
        echo "<p>Config file created succesfully.</p><a href='?go=".$type."&sub=db'>Next</a>";
    } else {
        echo 'Cannot read settings file. Copy <tt>'.$filename.'</tt> to <tt>'.$setFile.'</tt> and adjust your settings.';
    }
}

//This function gets any posted values from the config form and puts them into an array to be used later.
function get_post () {
        global $db;

        //Create blank variables in the array
        $db["server"]='';
        $db["name"]='';
        $db["user"]='';
        $db["pass"]='';

        //Now if they are poested populate them with thier values
        if(isset($_POST['dbserver']))   {$db["server"]=$_POST['dbserver'];}
        if(isset($_POST['dbname']))     {$db["name"]=$_POST['dbname'];}
        if(isset($_POST['dbuser']))     {$db["user"]=$_POST['dbuser'];}
        if(isset($_POST['dbpass']))     {$db["pass"]=$_POST['dbpass'];}
}

//If the db already exists we need to let them know that they can change it or continue on and overwrite thier current data.
//The database may already exist if they had an old version installed but would like to start clean with the new install.
//It may also exist if thier hosting company requires them to create the db with a control panel (usually when requiring a suffix/prefix)
function db_exists ($server,$db,$user,$pass) {
    echo "<form method='POST' action='?go=new&sub=postconfig'>
            <span class='inputerror'>
                <p>
                    The database you choose already exists. If you continue any
                    existing data will be lost.
                </p>
                <p>
                    <input type='hidden' name='dbserver'    value='".$server."'>
                    <input type='hidden' name='dbname'      value='".$db."'>
                    <input type='hidden' name='dbuser'      value='".$user."'>
                    <input type='hidden' name='dbpass'      value='".$pass."'>
                    Click <input type='submit' value='Here' name='submit' class='button'>
                    to continue or change the database name above and click submit.
                </p>
            </span>
         </form>";
}
?>
Return current item: Php4dvd - movie database