install/includes/stages/database_post.php
changeset 348 87e08a6e4fec
child 352 9d7225c0db6d
equal deleted inserted replaced
347:299a90e28abc 348:87e08a6e4fec
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.1
       
     6  * Copyright (C) 2006-2007 Dan Fuhry
       
     7  * Installation package
       
     8  * database_post.php - Database installation, stage 1
       
     9  *
       
    10  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    11  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    15  */
       
    16 
       
    17 if ( !defined('IN_ENANO_INSTALL') )
       
    18   die();
       
    19 
       
    20 // Start up the DBAL
       
    21 require( ENANO_ROOT . '/includes/dbal.php' );
       
    22 require( ENANO_ROOT . '/install/includes/sql_parse.php' );
       
    23 $dbal = new $driver();
       
    24 $db_host =& $_POST['db_host'];
       
    25 $db_user =& $_POST['db_user'];
       
    26 $db_pass =& $_POST['db_pass'];
       
    27 $db_name =& $_POST['db_name'];
       
    28 $db_prefix =& $_POST['table_prefix'];
       
    29 
       
    30 $result = $dbal->connect(true, $db_host, $db_user, $db_pass, $db_name);
       
    31 
       
    32 $ui->show_header();
       
    33 
       
    34 if ( $result )
       
    35 {
       
    36   // We're good, write out a config file
       
    37   $ch = @fopen( ENANO_ROOT . '/config.new.php', 'w' );
       
    38   if ( !$ch )
       
    39   {
       
    40     ?>
       
    41     <form action="install.php?stage=database" method="post" name="database_info">
       
    42       <h3>Configuration file generation failed.</h3>
       
    43       <p>Couldn't open the configuration file to write out database settings. Check your file permissions.</p>
       
    44       <p>
       
    45         <input type="submit" name="_cont" value="Go back" />
       
    46       </p>
       
    47     </form>
       
    48     <?php
       
    49     return true;
       
    50   }
       
    51   $db_host = str_replace("'", "\\'", $db_host);
       
    52   $db_user = str_replace("'", "\\'", $db_user);
       
    53   $db_pass = str_replace("'", "\\'", $db_pass);
       
    54   $db_name = str_replace("'", "\\'", $db_name);
       
    55   $db_prefix = str_replace("'", "\\'", $db_prefix);
       
    56   if ( !preg_match('/^[a-z0-9_]*$/', $db_prefix) )
       
    57   {
       
    58     echo '<p>That table prefix isn\'t going to work.</p>';
       
    59     return true;
       
    60   }
       
    61   fwrite($ch, "<?php
       
    62 // Enano temporary configuration file, will be OVERWRITTEN after installation.
       
    63 
       
    64 \$dbdriver = '$driver';
       
    65 \$dbhost = '$db_host';
       
    66 \$dbname = '$db_name';
       
    67 \$dbuser = '$db_user';
       
    68 \$dbpasswd = '$db_pass';
       
    69 @define('table_prefix', '$db_prefix');
       
    70 
       
    71 @define('ENANO_INSTALL_HAVE_CONFIG', 1);
       
    72 ");
       
    73   fclose($ch);
       
    74   // Create the config table
       
    75   try
       
    76   {
       
    77     $sql_parser = new SQL_Parser( ENANO_ROOT . "/install/schemas/{$driver}_stage1.sql" );
       
    78   }
       
    79   catch ( Exception $e )
       
    80   {
       
    81     ?>
       
    82     <h3>Can't load schema file</h3>
       
    83     <p>The SQL schema file couldn't be loaded.</p>
       
    84     <?php echo "<pre>$e</pre>"; ?>
       
    85     <?php
       
    86     return true;
       
    87   }
       
    88   // Check to see if the config table already exists
       
    89   $q = $dbal->sql_query('SELECT config_name, config_value FROM ' . $db_prefix . 'config LIMIT 1;');
       
    90   if ( !$q )
       
    91   {
       
    92     $sql_parser->assign_vars(array(
       
    93         'TABLE_PREFIX' => $db_prefix
       
    94       ));
       
    95     $sql = $sql_parser->parse();
       
    96     foreach ( $sql as $q )
       
    97     {
       
    98       if ( !$dbal->sql_query($q) )
       
    99       {
       
   100         ?>
       
   101         <form action="install.php?stage=database" method="post" name="database_info">
       
   102           <input type="hidden" name="language" value="<?php echo $lang_id; ?>" />
       
   103           <input type="hidden" name="driver" value="<?php echo $driver; ?>" />
       
   104           <h3>Database operation failed</h3>
       
   105           <p>The installer couldn't create one of the tables used for installation.</p>
       
   106           <p>Error description:
       
   107             <?php
       
   108             echo $dbal->sql_error();
       
   109             ?>
       
   110           </p>
       
   111           <p>
       
   112             <input type="submit" name="_cont" value="Go back" />
       
   113           </p>
       
   114         </form>
       
   115         <?php
       
   116         return true;
       
   117       }
       
   118     }
       
   119   }
       
   120   else
       
   121   {
       
   122     $dbal->free_result();
       
   123     if ( !$dbal->sql_query('DELETE FROM ' . $db_prefix . 'config WHERE config_name = \'install_aes_key\';') )
       
   124     {
       
   125       $dbal->_die('install database_post.php trying to remove old AES installer key');
       
   126     }
       
   127   }
       
   128   $dbal->close();
       
   129   ?>
       
   130   <form action="install.php?stage=website" method="post" name="install_db_post" onsubmit="return verify();">
       
   131   <input type="hidden" name="language" value="<?php echo $lang_id; ?>" />
       
   132   <?php
       
   133   // FIXME: l10n
       
   134   ?>
       
   135   <h3>Connection successful</h3>
       
   136   <p>The database has been contacted and initial tables created successfully. Redirecting...</p>
       
   137   <p><input type="submit" name="_cont" value="<?php echo $lang->get('meta_btn_continue'); ?>" />  Click if you're not redirected within 2 seconds</p>
       
   138   </form>
       
   139   <script type="text/javascript">
       
   140     setTimeout(function()
       
   141       {
       
   142         var frm = document.forms.install_db_post;
       
   143         frm.submit();
       
   144       }, 200);
       
   145   </script>
       
   146   <?php
       
   147 }
       
   148 else
       
   149 {
       
   150   // FIXME: l10n
       
   151   ?>
       
   152   <form action="install.php?stage=database" method="post" name="database_info">
       
   153     <input type="hidden" name="language" value="<?php echo $lang_id; ?>" />
       
   154     <input type="hidden" name="driver" value="<?php echo $driver; ?>" />
       
   155     <h3>Database connection failed</h3>
       
   156     <p>The installer couldn't connect to the database because something went wrong while the connection attempt was being made. Please press your browser's back button and correct your database information.</p>
       
   157     <p>Error description:
       
   158       <?php
       
   159       echo $dbal->sql_error();
       
   160       ?>
       
   161     </p>
       
   162     <p>
       
   163       <input type="submit" name="_cont" value="Go back" />
       
   164     </p>
       
   165   </form>
       
   166   <?php
       
   167 }
       
   168