View Single Post
  #1 (permalink)  
Old 08-18-2007, 11:13 PM
.Silver .Silver is offline
Onlooker
 
Advanced Installation in OOP (PHP)

So we've got the OOP side of the Advanced Installation, but now how do we go about doing the PHP side of it? Well this tutorial is going to help you learn that. The first thing we need to do though is include the file we need.

The file that we need is the file with out class in it, which I saved in classes/install.php. We then create the new object and get the x from the url ?x=

Code:
<?php
  require_once('classes/install.php');
  $install = new install;
  $x = $_GET['x'];
?>
So we will be using a switch function for our steps of installation, so let's get that started.

Code:
<?php
  switch($x)**
    default:

    break;
  }
?>
The switch functions allows us to switch the content on the page, if the ?x isn't defined, then we use the default page, which step one.

Now we fill in the default content, this content will be for setting up our mysql information, there are four fields we need to ask for, mysql host, mysql username, mysql password and the database name.

Code:
<?php
      print 'Welcome to the installation for Silver Blog. This      installation file will guide you through to setting up the database and admin.<br /><br />
      <form method="post" action="?x=1">
      <strong>MySQL Host</strong><br />
      <em>This is the host of your mysql, usually localhost.</em><br />';
			
      $install->do_form('','db_host');
			
      print'<strong>MySQL Username</strong><br />
			<em>The username for your database.</em><br />';
			
      $install->do_form('','db_user');
			
      print'<strong>MySQL Password</strong><br />
      <em>The password for your database.</em><br />';
			
      $install->do_form('password','db_pass');
			
      print'<strong>Database Name</strong><br />
      <em>The database to put the tables in</em><br />';
			
      $install->do_form('text','db_name');
      $install->do_form('submit','submit');
			
      print'</form>';
?>
All pretty simple, apart from our $install->doform. What's this? Well remember in the OOP part we did a function call doform, this $install->doform calls for this function, we do not need to say text for the first lot because our variable we set up in the class is set as text, the second value is for our field name, in the mysql password, we change the field to a password field, then back to text and then we use submit.

If you look where our form action is, we direct this form to ?x=1, so we're going to the next step with this.

Code:
<?php
    case'1':

    break;
?>
So if ?x=1 then show this content. In here, we check the fields, and change the variables in the class for the database information.

Code:
<?php
      $install->db_host = $install->check_form($_POST['db_host']);
      $install->db_user = $install->check_form($_POST['db_user']);
      $install->db_pass = $install->check_form($_POST['db_pass']);
      $install->db_name = $install->check_form($_POST['db_name']);
?>
First variables are changing our variables in the class, the second is calling the function check_form. But we're not finishing here yet, in our check_form function we set an error message is something is wrong. So now we need to see if this is filled in, if so, put it on the screen.

Code:
<?php
      if($install->error)**
        $install->check_error();
        exit();
      }
?>
The exit stops our script so it can't go any further. Ok so all our fields are ok, so we carry on with the script, we now need to make sure the information is correct by checking the connection.

Code:
<?php
      $install->check_connect();
?>
Well that's the end of that step, our function redirects us to the next step if everything is ok.

Code:
<?php
    case '2':

    break;
?>
In this one, we're going to set up our blog table.

Code:
<?php
      print'Creating table blog...<br />';
      $sql = 'CREATE TABLE `blog`(
      `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `title` VARCHAR(30) NOT NULL,
      `user` VARCHAR(30) NOT NULL,
      `postdate` VARCHAR(70) NOT NULL,
      `message` TEXT NOT NULL
      ) ENGINE = MYISAM ;';
      $install->query($sql);	
      $install->redir('3');
?>
We store the SQL in a variable, then send it to the query function, we then redirect them to the next step.

Code:
<?php
    case'3':

    break;
?>
In this section, we say the blog has been created and insert a dummy row.

Code:
<?php
      print 'Creating table blog...<br />
      Table blog created...';
      $sql = "INSERT INTO `blog`
      (`title`,`user`,`postdate`,`message`) VALUES
      ('Welcome','Dummy','".date('D M y g:ia')."','This message is to confirm that you have installed the news system.')";
      $install->query($sql);
      $install->redir('4');
?>
Pretty simple, nothing new at all. We then carry this on to the next step. Here is the next few steps since it's nothing new at all, it's setting up the blog_comments table and the members table.

Code:
<?php
    case'4':
      print'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />';
      $sql = 'CREATE TABLE `blog_comments`(
      `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `blog_id` INT(11) NOT NULL,
      `user` VARCHAR(30) NOT NULL,
      `postdate` VARCHAR(70) NOT NULL,
      `message` TEXT NOT NULL
      ) ENGINE = MYISAM ;';
      $install->query($sql);	
      $install->redir('5');
    break;
    case'5':
      print 'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />
      Table blog_comments created...';
      $sql = "INSERT INTO `blog_comments` (`blog_id`,`user`,`postdate`,`message`) VALUES ('1','Dummy','".date('D M y g:ia')."','This message is to confirm that you have installed the news system.')";
      $install->query($sql);
      $install->redir('6');
    break;
    case'6':
      print'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />
      Table blog_comments created...<br />
      Creating table members...<br />';
      $sql = "CREATE TABLE `members`(
      `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `username` INT(11) NOT NULL,
      `password` VARCHAR(30) NOT NULL,
      `email` VARCHAR(70) NOT NULL,
      `joined` VARCHAR(70) NOT NULL,
      `level` INT(1) NOT NULL default '1',
      `banned` INT(1) NOT NULL default '0'
      ) ENGINE = MYISAM ;";
      $install->query($sql);
      $install->redir('7');
    break;
    case'7':
      print 'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />
      Table blog_comments created...<br />
      Creating table members...<br />
      Table members created...';
      $install->redir('8');
    break;
?>
So now we move onto ?x=8, in this section, we another form to ask for the admin information. So again, it's nothing new.

Code:
<?php
    case'8':
      print 'Enter in the fields below add your admin account.<br /><br />
      <form method="post" action="?x=9">
      <strong>Username</strong><br />';

      $install->do_form('text','username');

      print '<strong>Password</strong><br />';

      $install->do_form('password','password');
			
      print '<strong>Email</strong><br />';
			
      $install->do_form('text','email');
      $install->do_form('submit','submit');
    break;
?>
Our ?x=9 is exactly the same as ?x=1 but we're checking for the admin this time not the connection.

Code:
<?php
    case'9':
      $user = $install->check_form($_POST['username']);
      $pass = $install->check_form($_POST['password']);
      $email = $install->check_form($_POST['email']);
      if($install->error)**			     
        $install->check_error();
        exit();
      }
?>
Look familiar? We're not finished yet though, we now need to md5 hash the password, and insert the admin.

Code:
<?php
      md5(md5($pass));
			
      $sql = "INSERT INTO `members`
      (`username`,`password`,`email`,`joined`,`level`) VALUES
      ('$user','$pass','$email','".date('D M y')."','5')";
      $install->query($sql);
			
      print 'Successfully added admin.';
			
      $install->redir('10');
    break;
?>
So we md5 hash the password, then once again for extra security, then we do the SQL, if all complete say we've added the admin, and redirect to ?x=10. Don't worry, this is the last one now, and the smallest one too. Here we just delete the install file and tell the user that installation is complete.

Code:
<?php
    case'10':
     $install->delete($_SERVER['SCRIPT_FILENAME']);
      print 'This installation script has attempted to delete the installation file, you should double check to make sure that it has succeeded.<br /><br />
      You can now log in.';
    break;
?>
Simple as, so below is the full code if you didn't get all of it, read through it carefully so you understand.

Code:
<?php
  require_once('classes/install.php');
  $install = new install;
  $x = $_GET['x'];
  switch($x)**
    default:
      print 'Welcome to the installation for Silver Blog. This installation file will guide you through to setting up the database and admin.<br /><br />
      <form method="post" action="?x=1">
      <strong>MySQL Host</strong><br />
      <em>This is the host of your mysql, usually localhost.</em><br />';
			
      $install->do_form('','db_host');
			
      print'<strong>MySQL Username</strong><br />
      <em>The username for your database.</em><br />';
			
      $install->do_form('','db_user');
			
      print'<strong>MySQL Password</strong><br />
      <em>The password for your database.</em><br />';
			
      $install->do_form('password','db_pass');
			
      print'<strong>Database Name</strong><br />
      <em>The database to put the tables in</em><br />';
			
      $install->do_form('text','db_name');
      die("This script has died due to you copy and pasting, read through it.");
      //DELETE THE LINE ABOVE
      $install->do_form('submit','submit');
			
      print'</form>';			
    break;
    case'1':
      $install->db_host = $install->check_form($_POST['db_host']);
      $install->db_user = $install->check_form($_POST['db_user']);
      $install->db_pass = $install->check_form($_POST['db_pass']);
      $install->db_name = $install->check_form($_POST['db_name']);
      if($install->error)**
        $install->check_error();
        exit();
      }
      $install->check_connect();
    break;
    case'2':
      print'Creating table blog...<br />';
      $sql = 'CREATE TABLE `blog`(
      `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `title` VARCHAR(30) NOT NULL,
      `user` VARCHAR(30) NOT NULL,
      `postdate` VARCHAR(70) NOT NULL,
      `message` TEXT NOT NULL
      ) ENGINE = MYISAM ;';
      $install->query($sql);	
      $install->redir('3');
    break;
    case'3':
      print 'Creating table blog...<br />
      Table blog created...';
      $sql = "INSERT INTO `blog`
      (`title`,`user`,`postdate`,`message`) VALUES
      ('Welcome','Dummy','".date('D M y g:ia')."','This message is to confirm that you have installed the news system.')";
      $install->query($sql);
      $install->redir('4');
    break;
    case'4':
      print'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />';
      $sql = 'CREATE TABLE `blog_comments`(
      `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `blog_id` INT(11) NOT NULL,
      `user` VARCHAR(30) NOT NULL,
      `postdate` VARCHAR(70) NOT NULL,
      `message` TEXT NOT NULL
      ) ENGINE = MYISAM ;';
      $install->query($sql);	
      $install->redir('5');
    break;
    case'5':
      print 'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />
      Table blog_comments created...';
      $sql = "INSERT INTO `blog_comments`
      (`blog_id`,`user`,`postdate`,`message`) VALUES
      ('1','Dummy','".date('D M y g:ia')."','This message is to confirm that you have installed the news system.')";
      $install->query($sql);
      $install->redir('6');
    break;
    case'6':
      print'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />
      Table blog_comments created...<br />
      Creating table members...<br />';
      $sql = "CREATE TABLE `members`(
      `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      `username` INT(11) NOT NULL,
      `password` VARCHAR(30) NOT NULL,
      `email` VARCHAR(70) NOT NULL,
      `joined` VARCHAR(70) NOT NULL,
      `level` INT(1) NOT NULL default '1',
      `banned` INT(1) NOT NULL default '0'
      ) ENGINE = MYISAM ;";
      $install->query($sql);
      $install->redir('7');
    break;
    case'7':
      print 'Creating table blog...<br />
      Table blog created...<br />
      Creating table blog_comments...<br />
      Table blog_comments created...<br />
      Creating table members...<br />
      Table members created...';
      $install->redir('8');
    break;
    case'8':
      print 'Enter in the fields below add your admin account.<br /><br />
      <form method="post" action="?x=9">
      <strong>Username</strong><br />';

      $install->do_form('text','username');
		
      print '<strong>Password</strong><br />';
			
      $install->do_form('password','password');
			
      print '<strong>Email</strong><br />';
			
      $install->do_form('text','email');
      $install->do_form('submit','submit');
    break;
    case'9':
      $user = $install->check_form($_POST['username']);
      $pass = $install->check_form($_POST['password']);
      $email = $install->check_form($_POST['email']);
      if($install->error)**
        $install->check_error();
        exit();
      }
      md5(md5($pass));
		
      $sql = "INSERT INTO `members`
      (`username`,`password`,`email`,`joined`,`level`) VALUES
      ('$user','$pass','$email','".date('D M y')."','5')";
      $install->query($sql);
			
      print 'Successfully added admin.';
			
      $install->redir('10');
    break;
    case'10':
      $install->delete($_SERVER['SCRIPT_FILENAME']);
      print 'This installation script has attempted to delete the installation file, you should double check to make sure that it has succeeded.<br /><br />
      You can now log in.';
    break;
  }
?>
__________________