Location: PHPKode > projects > PHPKode - PHPGuestbook > installer/kode_operations.php
<?php

  /*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  require_once("kode_settings.php");
  require_once('kode_functions.php');

  $action = $_POST["action"];
  $messageReturn = "Error:The parameter is incorrect";

  if (empty($action) || !in_array($action, array("connectMySQL", "connectSQLite", "startInstall", "makeConfig", "importSQL"))) {
      returnMsg($messageReturn);
  }

  switch ($action) {
      case "connectMySQL":
          connectMySQL();
          break;
      case "connectSQLite":
          connectSQLite();
          break;
      case "startInstall":
          startInstall();
          break;
      default:
          returnMsg($messageReturn);
  }

  function connectMySQL() {
      $mysql_host = $_POST["mysql_host"];
      $mysql_port = $_POST["mysql_port"];
      $mysql_db_name = $_POST["mysql_db_name"];
      $mysql_username = $_POST["mysql_username"];
      $mysql_password = $_POST["mysql_password"];

      if (empty($mysql_host)) {
          returnMsg("Error: MySQL Host Address field is required.");
      }
      if (empty($mysql_db_name)) {
          returnMsg("Error: Database Name field is required.");
      }
      if (empty($mysql_username)) {
          returnMsg("Error: Database Account field is required.");
      }
      if (empty($mysql_password)) {
          returnMsg("Error: Database Password field is required.");
      }
      if (empty($mysql_port)) {
          $mysql_port = "3306";
      }

      $conn = mysql_connect($mysql_host . ":" . $mysql_port, $mysql_username, $mysql_password);
      if ($conn) {
          $rs = mysql_select_db($mysql_db_name, $conn);
          $result = "Connect database successfully.";
      }
      else {
          $result = "Error: Can not connect database.";
      }
      @mysql_close($conn);
      returnMsg($result);
  }

  function connectSQLite() {
      $set_sqlite_db_name = $_POST["sqlite_db_name"];
      $sqldb = @sqlite_open($set_sqlite_db_name);
      if ($sqldb) {

          $result = "Connect database successfully.";
      }
      else {
          $result = "Error: unable to open database: " . $set_sqlite_db_name;
      }
      @sqlite_close($sqldb);
      returnMsg($result);
  }

  function startInstall() {
      global $set_mysql_dump, $set_sqlite_file_dump, $set_sqlite_file_dir, $set_config_file_template;
      global $set_mysql_table_prefix, $set_config_file_path, $set_install_lockfile_path;
      global $set_config_sql_tag, $set_admin_username_tag, $set_admin_password_tag, $set_page_tile_tag, $set_home_page_tag;

      $new_sqlite_db_name = "";

      $db_type = $_POST["db_type"];
      $mysql_host = $_POST["mysql_host"];
      $mysql_port = $_POST["mysql_port"];
      $mysql_db_name = $_POST["mysql_db_name"];
      $mysql_table_prefix = $_POST["mysql_table_prefix"];
      $mysql_username = $_POST["mysql_username"];
      $mysql_password = $_POST["mysql_password"];
      $sqlite_db_name = $_POST["sqlite_db_name"];

      $admin_username = $_POST["admin_username"];
      $admin_password = md5($_POST["admin_password"]);
      $page_tile = $_POST["page_tile"];
      $home_page = $_POST["home_page"];

      $messageReturn = "";


      if ($db_type == "mysql") {
          //import sql
          $link = @mysql_connect($mysql_host . ":" . $mysql_port, $mysql_username, $mysql_password)
                  or die("Error: Could not connect. " . $mysql_host . ":" . $mysql_port);

          @mysql_select_db($mysql_db_name, $link) or die("Error: Could not use: " . $mysql_db_name);

          $lines = file($set_mysql_dump);
          foreach ($lines as $line_num => $line) {
              $query = $line;
              if (strlen($line) > 5) {

                  if (strpos($line, $set_config_sql_tag) == 0) {
                      $query = str_replace($set_config_sql_tag, "", $query);
                      $query = str_replace($set_admin_username_tag, $admin_username, $query);
                      $query = str_replace($set_admin_password_tag, $admin_password, $query);
                      $query = str_replace($set_page_tile_tag, $page_tile, $query);
                      $query = str_replace($set_home_page_tag, $home_page, $query);
                  }

                  $query = str_replace('[' . $mysql_table_prefix . ']', $mysql_table_prefix, $query);
                  mysql_query($query, $link);
              }
          }
          mysql_close($link);

          $messageReturn .= "Import Mysql database sucessfully.<br />";
      }
      else {
          $new_sqlite_db_name = randChars(12) . ".db";
          copy($set_sqlite_file_dump, $set_sqlite_file_dir . $new_sqlite_db_name);
          $messageReturn .= "Create Sqlite database sucessfully.<br />";
      }

      //create config file
      $fp = fopen($set_config_file_template, "r");
      $configConent = fread($fp, filesize($set_config_file_template));
      fclose($fp);

      //replace with new values
      $configConent = str_replace("[db_type]", $db_type, $configConent);
      $configConent = str_replace("[mysql_host]", $mysql_host, $configConent);
      $configConent = str_replace("[mysql_port]", $mysql_port, $configConent);
      $configConent = str_replace("[mysql_db_name]", $mysql_db_name, $configConent);
      $configConent = str_replace("[mysql_table_prefix]", $mysql_table_prefix, $configConent);
      $configConent = str_replace("[mysql_username]", $mysql_username, $configConent);
      $configConent = str_replace("[mysql_password]", $mysql_password, $configConent);
      $configConent = str_replace("[sqlite_db_name]", $new_sqlite_db_name, $configConent);

      $fp = fopen($set_config_file_path, "w") or die("You did not create $set_config_file_path");
      fwrite($fp, $configConent);
      fclose($fp);

      $messageReturn .= "Create $set_config_file_path sucessfully.<br />";

      // create installed lock file.

      $fp = fopen($set_install_lockfile_path, 'w');
      fwrite($fp, 'PHPKode PHPGuestbook');
      fclose($fp);

      $messageReturn .= "Create $set_install_lockfile_path sucessfully.<br /> <br />";
      $messageReturn = "Installation Completed. Thank you for choosing PHPKode product.";

      returnMsg($messageReturn);
  }

  // return info
  function returnMsg($msg) {
      exit($msg);
  }

?>
Return current item: PHPKode - PHPGuestbook