<?php

session_start();

if (isset($_REQUEST['db_hostname']))
  $db_hostname = $_REQUEST['db_hostname'];

if (isset($_REQUEST['db_username']))
  $db_username = $_REQUEST['db_username'];

if (isset($_REQUEST['db_password']))
  $db_password = $_REQUEST['db_password'];

if (isset($_REQUEST['db_name']))
  $db_name = $_REQUEST['db_name'];

if (isset($_REQUEST['db_drop_tables']))
  $db_drop_tables = $_REQUEST['db_drop_tables'];

if (isset($_REQUEST['db_create']))
  $db_create = $_REQUEST['db_create'];

if (isset($_REQUEST['db_populate']))
  $db_populate = $_REQUEST['db_populate'];

if (isset($_REQUEST['site_URL']))
  $site_URL = $_REQUEST['site_URL'];

if (isset($_REQUEST['admin_password']))
  $admin_password = $_REQUEST['admin_password'];

$cache_dir = 'cache/';

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Miniclick Installer: Step 4</title>
  <link rel="stylesheet" href="install/install.css" type="text/css" />
</head>
<body leftMargin="0" topMargin="0" marginheight="0" marginwidth="0">
  <table width="75%" border="0" cellpadding="3" cellspacing="0" align="center" style="border-bottom: 1px dotted #CCCCCC;">
    <tbody>
      <tr>
        <td align="right"><h2>Step 4 of 5</h2></td>
      </tr>
    </tbody>
  </table>
  <table width="75%" align="center" cellpadding="10" cellspacing="0" border="0">
  <tbody>
    <tr>
      <td>
        <table width=100% cellpadding="0" cellspacing="0" border="0">
          <tbody>
            <tr>
              <td>
                <table width=100% cellpadding="0" cellspacing="0" border="0">
                  <tbody>
                    <tr>
                      <td><h3>Create configuration file</h3></td>
                      <td width="80%"><hr width="100%"></td>
                    </tr>
                  </tbody>
                </table>
              </td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
    <tr>
    <td width="100%">
<?php

if (isset($_REQUEST['root_directory']))
  $root_directory = $_REQUEST['root_directory'];

if (is_file('../includes/config.php'))
  $is_writable = is_writable('../includes/config.php');
else
  $is_writable = is_writable('.');

/* open template configuration file read only */
$templateFilename = '../includes/config.php.dist';
$templateHandle = fopen($templateFilename, "r");
if($templateHandle) {
  /* open include configuration file write only */
  $includeFilename = '../includes/config.php';
  $includeHandle = fopen($includeFilename, "w");
  if($includeHandle) {
    while (!feof($templateHandle)) {
      $buffer = fgets($templateHandle);

      /* replace _DBC_ variable */
      $buffer = str_replace( "_DB_SERVER_", $db_hostname, $buffer);
      $buffer = str_replace( "_DB_PORT_", "3306", $buffer);
      $buffer = str_replace( "_DB_USER_", $db_username, $buffer);
      $buffer = str_replace( "_DB_PASS_", $db_password, $buffer);
      $buffer = str_replace( "_DB_NAME_", $db_name, $buffer);
      $buffer = str_replace( "_DB_TYPE_", "mysql", $buffer);
      
      $buffer = str_replace( "_SITE_URL_", $site_URL, $buffer);
      
      /* replace dir variable */
      $buffer = str_replace( "_MC_ROOTDIR_", $root_directory, $buffer);
      fwrite($includeHandle, $buffer);
    }

    fclose($includeHandle);
  }

  fclose($templateHandle);
}

if ($templateHandle && $includeHandle) {
  echo "<br><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" align=\"center\"><tbody><tr><td align=\"left\">";
  echo "<h4>Successfully created configuration file (<b>config.php</b>) in : includes/config.php</h4></td>";
  echo "</tr></table>";
}
else {
  echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody><tr><td align=\"left\">";
  echo "Cannot write configuration file (config.inc.php ) in the directory <font color=red>".$root_directory."</font>.\n";
  echo "<P>You can continue this installation by manually creating the config.inc.php file and pasting the configuration information below inside.However, you <strong>must</strong> create the configuration file before you continue to the next step.<P>\n";
  echo  "<TEXTAREA class=\"dataInput\" rows=\"15\" cols=\"80\">".$config."</TEXTAREA>";
  echo "<P>Did you remember to create the config.inc.php file ?</td></tr>";
}

?>

      <tr><td>&nbsp;</td></tr>
        <tr>
          <td colspan="2" align="right">
            <form action="index.php" method="post" name="form" id="form">
              <input type="hidden" name="file" value="step5.php">

              <input type="hidden" class="dataInput" name="db_hostname" value="<?php if (isset($db_hostname)) echo "$db_hostname"; ?>" />
              <input type="hidden" class="dataInput" name="db_username" value="<?php if (isset($db_username)) echo "$db_username"; ?>" />
              <input type="hidden" class="dataInput" name="db_password" value="<?php if (isset($db_password)) echo "$db_password"; ?>" />
              <input type="hidden" class="dataInput" name="db_name" value="<?php if (isset($db_name)) echo "$db_name"; ?>" />
              <input type="hidden" class="dataInput" name="db_drop_tables" value="<?php if (isset($db_drop_tables)) echo "$db_drop_tables"; ?>" />
              <input type="hidden" class="dataInput" name="db_create" value="<?php if (isset($db_create)) echo "$db_create"; ?>" />
              <input type="hidden" class="dataInput" name="db_populate" value="<?php if (isset($db_populate)) echo "$db_populate"; ?>" />
              <input type="hidden" class="dataInput" name="admin_email" value="<?php if (isset($admin_email)) echo "$admin_email"; ?>" />
              <input type="hidden" class="dataInput" name="admin_password" value="<?php if (isset($admin_password)) echo "$admin_password"; ?>" />

              <input class="button" type="submit" name="next" value="Next" />
            </form>
          </td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
