install/includes/stages/database_mysql.php
changeset 348 87e08a6e4fec
child 390 9bcc185dc151
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_mysql.php - Installer database info page, MySQL
       
     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 if ( isset($_POST['_cont']) )
       
    21 {
       
    22   $allow_go = true;
       
    23   // Do we have everything? If so, continue with installation.
       
    24   foreach ( array('db_host', 'db_name', 'db_user', 'db_pass') as $field )
       
    25   {
       
    26     if ( empty($_POST[$field]) )
       
    27     {
       
    28       $allow_go = false;
       
    29     }
       
    30   }
       
    31   if ( $allow_go )
       
    32   {
       
    33     require( ENANO_ROOT . '/install/includes/stages/database_post.php' );
       
    34     return true;
       
    35   }
       
    36 }
       
    37 
       
    38 if ( isset($_POST['ajax_test']) )
       
    39 {
       
    40   // Test the database connection
       
    41   $return = array(
       
    42       'can_install' => false,
       
    43       'host_good' => true,
       
    44       'creating_user' => false,
       
    45       'db_exist' => false,
       
    46       'creating_db' => false,
       
    47       'creating_db_grant' => false,
       
    48       'root_fail' => false,
       
    49       'version' => array(
       
    50         'version' => 'unknown',
       
    51         'good' => 'indeterminate'
       
    52       ),
       
    53       'last_error' => ''
       
    54     );
       
    55   
       
    56   if ( !isset($_POST['info']) )
       
    57     die();
       
    58   
       
    59   $info = $_POST['info'];
       
    60   
       
    61   // From here on out will be JSON responses
       
    62   header('Content-type: application/json');
       
    63   
       
    64   try
       
    65   {
       
    66     $info = @enano_json_decode($info);
       
    67   }
       
    68   catch ( Zend_Json_Exception $e )
       
    69   {
       
    70     die(enano_json_encode(array(
       
    71         'mode' => 'error',
       
    72         'error' => 'Exception in JSON decoder'
       
    73       )));
       
    74   }
       
    75   
       
    76   // Try to connect as the normal user
       
    77   $test = @mysql_connect($info['db_host'], $info['db_user'], $info['db_pass']);
       
    78   if ( !$test )
       
    79   {
       
    80     $return['creating_user'] = true;
       
    81     $return['last_error'] = mysql_error();
       
    82     if ( strstr( $return['last_error'], 'Lost connection' ) || strstr( $return['last_error'], 'Unknown MySQL server host' ) )
       
    83     {
       
    84       $return['host_good'] = false;
       
    85     }
       
    86     // Doing that failed. If we have root credentials, test those
       
    87     if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
       
    88     {
       
    89       // Log in with root rights and if that works, tell 'em we'll reset the password or create
       
    90       // the account if it doesn't exist already. This is done with GRANT ALL PRIVILEGES ON enano_db.*
       
    91       // etc etc, a little hackish but known to work with MySQL >= 4.1.
       
    92       $test_root = @mysql_connect($info['db_host'], $info['db_root_user'], $info['db_root_pass']);
       
    93       if ( $test_root )
       
    94       {
       
    95         // We logged in with root rights, assume that we have appropriate permissions.
       
    96         // If not, well, the installation will fail. Tough on the user, but creating
       
    97         // test databases/users is too risky.
       
    98         
       
    99         // Does the database exist?
       
   100         $q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test_root);
       
   101         if ( !$q )
       
   102         {
       
   103           // Nope, we'll have to create it
       
   104           $return['creating_db'] = true;
       
   105           $return['last_error'] = mysql_error();
       
   106         }
       
   107         
       
   108         $version = mysql_get_server_info($test_root);
       
   109         $return['version'] = array(
       
   110           'version' => $version,
       
   111           'good' => version_compare($version, '4.0.17', '>=')
       
   112         );
       
   113         
       
   114         $return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   115       }
       
   116       else
       
   117       {
       
   118         // Well that helped. Root credentials are bad.
       
   119         $return['creating_db'] = true;
       
   120         $return['root_fail'] = true;
       
   121       }
       
   122     }
       
   123     else
       
   124     {
       
   125       // No root credentials, fail out
       
   126       $return['root_fail'] = true;
       
   127     }
       
   128   }
       
   129   else
       
   130   {
       
   131     // We're connected; do we have permission to use the database?
       
   132     $have_database = false;
       
   133     $q = @mysql_query('USE `' . mysql_real_escape_string($info['db_name']) . '`;', $test);
       
   134     if ( $q )
       
   135     {
       
   136       // Permissions are good and we're all connected. Perform version check...
       
   137       $version = mysql_get_server_info($test);
       
   138       $return['version'] = array(
       
   139         'version' => $version,
       
   140         'good' => version_compare($version, '4.0.17', '>=')
       
   141       );
       
   142       
       
   143       $return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   144     }
       
   145     else
       
   146     {
       
   147       $return['last_error'] = mysql_error();
       
   148       $return['creating_db'] = true;
       
   149       
       
   150       // We don't have permission to use the database or it doesn't exist.
       
   151       // See if we have a root login to work with, if not then fail
       
   152       if ( !empty($info['db_root_user']) && !empty($info['db_root_pass']) )
       
   153       {
       
   154         // Log in with root rights and if that works, tell 'em we'll create the database.
       
   155         $test_root = @mysql_connect($info['db_host'], $info['db_root_user'], $info['db_root_pass']);
       
   156         if ( $test_root )
       
   157         {
       
   158           // We logged in with root rights, assume that we have appropriate permissions.
       
   159           // If not, well, the installation will fail. Tough on the user, but creating
       
   160           // test databases/users is too risky.
       
   161           
       
   162           // See if the database already exists
       
   163           $dbname = mysql_real_escape_string($info['db_name']);
       
   164           $q = @mysql_query("SHOW DATABASES LIKE '$dbname';", $test_root);
       
   165           if ( $q )
       
   166           {
       
   167             if ( mysql_num_rows($q) > 0 )
       
   168             {
       
   169               $return['creating_db'] = false;
       
   170               $return['creating_db_grant'] = true;
       
   171             }
       
   172             @mysql_free_result($q);
       
   173           }
       
   174           
       
   175           $version = mysql_get_server_info($test);
       
   176           $return['version'] = array(
       
   177             'version' => $version,
       
   178             'good' => version_compare($version, '4.0.17', '>=')
       
   179           );
       
   180           
       
   181           $return['can_install'] = ( $return['version']['good'] ) ? true : false;
       
   182         }
       
   183         else
       
   184         {
       
   185           // Well that helped. Root credentials are bad.
       
   186           $return['creating_db'] = true;
       
   187           $return['root_fail'] = true;
       
   188         }
       
   189       }
       
   190       // No root credentials, fail out
       
   191     }
       
   192   }
       
   193   
       
   194   if ( isset($test) && @is_resource($test) )
       
   195     @mysql_close($test);
       
   196   
       
   197   if ( isset($test_root) && @is_resource($test_root) )
       
   198     @mysql_close($test_root);
       
   199   
       
   200   echo enano_json_encode($return);
       
   201   
       
   202   exit();
       
   203 }
       
   204 
       
   205 $ui->add_header('<script type="text/javascript" src="includes/js/formutils.js"></script>');
       
   206 $ui->show_header();
       
   207 
       
   208 ?>
       
   209 
       
   210 <div style="float: right; padding: 10px 0 10px 10px;">
       
   211   <img alt="MySQL logo" src="../images/about-powered-mysql.png" />
       
   212 </div>
       
   213 
       
   214 <p><?php echo $lang->get('dbmysql_blurb_needdb'); ?></p>
       
   215 <p><?php echo $lang->get('dbmysql_blurb_howtomysql'); ?></p>
       
   216 <?php
       
   217 if ( @file_exists('/etc/enano-is-virt-appliance') )
       
   218 {
       
   219   echo '<p>
       
   220           ' . $lang->get('database_vm_login_info', array( 'host' => 'localhost', 'user' => 'enano', 'pass' => 'clurichaun', 'name' => 'enano_www1' )) . '
       
   221         </p>';
       
   222 }
       
   223 ?>
       
   224 
       
   225 <script type="text/javascript">
       
   226 
       
   227   var tested = false;
       
   228 
       
   229   function verify(field)
       
   230   {
       
   231     if ( tested && !field )
       
   232       return true;
       
   233     tested = false;
       
   234     if ( document.getElementById('verify_error').className != '' )
       
   235     {
       
   236       document.getElementById('verify_error').className = '';
       
   237       document.getElementById('verify_error').innerHTML = '';
       
   238     }
       
   239     var frm = document.forms.database_info;
       
   240     // List of fields
       
   241     var fields = {
       
   242       db_host: frm.db_host,
       
   243       db_name: frm.db_name,
       
   244       db_user: frm.db_user,
       
   245       db_pass: frm.db_pass,
       
   246       table_prefix: frm.table_prefix,
       
   247       db_root_user: frm.db_root_user,
       
   248       db_root_pass: frm.db_root_pass
       
   249     };
       
   250     var passed = true;
       
   251     // Main validation
       
   252     if ( field == fields.db_host || !field )
       
   253     {
       
   254       var matches = fields.db_host.value.match(/^([a-z0-9_-]+)((\.([a-z0-9_-]+))*)?$/);
       
   255       document.getElementById('s_db_host').src = ( matches ) ? img_neu : img_bad;
       
   256       if ( !matches )
       
   257         passed = false;
       
   258     }
       
   259     if ( field == fields.db_name || !field )
       
   260     {
       
   261       var matches = fields.db_name.value.match(/^[A-z0-9_-]+$/);
       
   262       document.getElementById('s_db_name').src = ( matches ) ? img_neu : img_bad;
       
   263       if ( !matches )
       
   264         passed = false;
       
   265     }
       
   266     if ( field == fields.db_user || field == fields.db_pass || !field )
       
   267     {
       
   268       var matches = fields.db_user.value.match(/^[A-z0-9_-]+$/);
       
   269       document.getElementById('s_db_auth').src = ( matches ) ? img_neu : img_bad;
       
   270       if ( !matches )
       
   271         passed = false;
       
   272     }
       
   273     if ( field == fields.table_prefix || !field )
       
   274     {
       
   275       var matches = fields.table_prefix.value.match(/^[a-z0-9_]*$/);
       
   276       document.getElementById('s_table_prefix').src = ( matches ) ? img_good : img_bad;
       
   277       if ( !matches )
       
   278         passed = false;
       
   279     }
       
   280     if ( field == fields.db_root_user || field == fields.db_root_pass || !field )
       
   281     {
       
   282       var matches = ( ( fields.db_root_user.value.match(/^[A-z0-9_-]+$/) && fields.db_root_pass.value.match(/^.+$/) ) || fields.db_root_user.value == '' );
       
   283       document.getElementById('s_db_root').src = ( matches ) ? img_neu : img_bad;
       
   284       if ( !matches )
       
   285         passed = false;
       
   286     }
       
   287     return passed;
       
   288   }
       
   289   
       
   290   function ajaxTestConnection()
       
   291   {
       
   292     if ( !verify() )
       
   293     {
       
   294       document.body.scrollTop = 0;
       
   295       new Spry.Effect.Shake('enano-body', {duration: 750}).start();
       
   296       document.getElementById('verify_error').className = 'error-box-mini';
       
   297       document.getElementById('verify_error').innerHTML = $lang.get('meta_msg_err_verification');
       
   298       return false;
       
   299     }
       
   300     install_set_ajax_loading();
       
   301     
       
   302     var frm = document.forms.database_info;
       
   303     var connection_info = 'info=' + ajaxEscape(toJSONString({
       
   304         db_host: frm.db_host.value,
       
   305         db_name: frm.db_name.value,
       
   306         db_user: frm.db_user.value,
       
   307         db_pass: frm.db_pass.value,
       
   308         db_root_user: frm.db_root_user.value,
       
   309         db_root_pass: frm.db_root_pass.value
       
   310       }));
       
   311     
       
   312     ajaxPost(scriptPath + '/install/install.php?stage=database', connection_info + '&driver=mysql&ajax_test=on&language=' + enano_lang_code[ENANO_LANG_ID], function()
       
   313       {
       
   314         if ( ajax.readyState == 4 )
       
   315         {
       
   316           setTimeout('install_unset_ajax_loading();', 750);
       
   317           // Process response
       
   318           var response = String(ajax.responseText + '');
       
   319           if ( response.substr(0, 1) != '{' )
       
   320           {
       
   321             alert('Received an invalid JSON response from the server.');
       
   322             return false;
       
   323           }
       
   324           response = parseJSON(response);
       
   325           document.getElementById('e_db_host').innerHTML = '';
       
   326           document.getElementById('e_db_name').innerHTML = '';
       
   327           document.getElementById('e_db_auth').innerHTML = '';
       
   328           document.getElementById('e_db_root').innerHTML = '';
       
   329           if ( response.can_install )
       
   330           {
       
   331             tested = true;
       
   332             var statuses = ['s_db_host', 's_db_name', 's_db_auth', 's_table_prefix', 's_db_root', 's_mysql_version'];
       
   333             for ( var i in statuses )
       
   334             {
       
   335               var img = document.getElementById(statuses[i]);
       
   336               if ( img )
       
   337                 img.src = img_good;
       
   338             }
       
   339             document.getElementById('e_mysql_version').innerHTML = $lang.get('dbmysql_msg_info_mysql_good');
       
   340             document.getElementById('verify_error').className = 'info-box-mini';
       
   341             document.getElementById('verify_error').innerHTML = $lang.get('dbmysql_msg_test_success');
       
   342             if ( response.creating_db )
       
   343             {
       
   344               document.getElementById('e_db_name').innerHTML = $lang.get('dbmysql_msg_warn_creating_db');
       
   345             }
       
   346             if ( response.creating_user )
       
   347             {
       
   348               document.getElementById('e_db_auth').innerHTML = $lang.get('dbmysql_msg_warn_creating_user');
       
   349             }
       
   350           }
       
   351           else
       
   352           {
       
   353             // Oh dear, oh dear, oh dear, oh dear, oh dear...
       
   354             if ( response.creating_db )
       
   355             {
       
   356               document.getElementById('e_db_name').innerHTML = $lang.get('dbmysql_msg_err_mysql_dbexist', { mysql_error: response.last_error });
       
   357               document.getElementById('s_db_name').src = img_bad;
       
   358             }
       
   359             if ( response.creating_user )
       
   360             {
       
   361               document.getElementById('e_db_auth').innerHTML = $lang.get('dbmysql_msg_err_mysql_auth', { mysql_error: response.last_error });
       
   362               document.getElementById('s_db_auth').src = img_bad;
       
   363             }
       
   364             if ( !response.host_good )
       
   365             {
       
   366               document.getElementById('e_db_host').innerHTML = $lang.get('dbmysql_msg_err_mysql_connect', { db_host: frm.db_host.value, mysql_error: response.last_error });
       
   367               document.getElementById('s_db_host').src = img_bad;
       
   368             }
       
   369           }
       
   370         }
       
   371       });
       
   372   }
       
   373 
       
   374 </script>
       
   375 
       
   376 <form action="install.php?stage=database" method="post" name="database_info">
       
   377 <input type="hidden" name="language" value="<?php echo $lang_id; ?>" />
       
   378 <input type="hidden" name="driver" value="mysql" />
       
   379 
       
   380 <table border="0" cellspacing="0" cellpadding="10" width="100%">
       
   381   <tr>
       
   382     <td colspan="3" style="text-align: center">
       
   383       <h3><?php echo $lang->get('dbmysql_table_title'); ?></h3>
       
   384     </td>
       
   385   </tr>
       
   386   <tr>
       
   387     <td>
       
   388       <b><?php echo $lang->get('dbmysql_field_hostname_title'); ?></b>
       
   389       <br /><?php echo $lang->get('dbmysql_field_hostname_body'); ?>
       
   390       <br /><span style="color: #993300" id="e_db_host"></span>
       
   391     </td>
       
   392     <td>
       
   393       <input onkeyup="verify(this);" tabindex="1" name="db_host" size="30" type="text" />
       
   394     </td>
       
   395     <td>
       
   396       <img id="s_db_host" alt="Good/bad icon" src="../images/bad.gif" />
       
   397     </td>
       
   398   </tr>
       
   399   <tr>
       
   400     <td>
       
   401       <b><?php echo $lang->get('dbmysql_field_dbname_title'); ?></b><br />
       
   402       <?php echo $lang->get('dbmysql_field_dbname_body'); ?><br />
       
   403       <span style="color: #993300" id="e_db_name"></span>
       
   404     </td>
       
   405     <td>
       
   406       <input onkeyup="verify(this);" tabindex="2" name="db_name" size="30" type="text" />
       
   407     </td>
       
   408     <td>
       
   409       <img id="s_db_name" alt="Good/bad icon" src="../images/bad.gif" />
       
   410     </td>
       
   411   </tr>
       
   412   <tr>
       
   413     <td>
       
   414       <b><?php echo $lang->get('dbmysql_field_dbauth_title'); ?></b><br />
       
   415       <?php echo $lang->get('dbmysql_field_dbauth_body'); ?><br />
       
   416       <span style="color: #993300" id="e_db_auth"></span>
       
   417     </td>
       
   418     <td>
       
   419       <input onkeyup="verify(this);" tabindex="3" name="db_user" size="30" type="text" /><br />
       
   420       <br />
       
   421       <input name="db_pass" tabindex="4" size="30" type="password" />
       
   422     </td>
       
   423     <td>
       
   424       <img id="s_db_auth" alt="Good/bad icon" src="../images/bad.gif" />
       
   425     </td>
       
   426   </tr>
       
   427   <tr>
       
   428     <td colspan="3" style="text-align: center">
       
   429       <h3><?php echo $lang->get('database_heading_optionalinfo'); ?></h3>
       
   430     </td>
       
   431   </tr>
       
   432   <tr>
       
   433     <td>
       
   434       <b><?php echo $lang->get('dbmysql_field_tableprefix_title'); ?></b><br />
       
   435       <?php echo $lang->get('dbmysql_field_tableprefix_body'); ?>
       
   436     </td>
       
   437     <td>
       
   438       <input onkeyup="verify(this);" tabindex="5" name="table_prefix" size="30" type="text" />
       
   439     </td>
       
   440     <td>
       
   441       <img id="s_table_prefix" alt="Good/bad icon" src="../images/good.gif" />
       
   442     </td>
       
   443   </tr>
       
   444   <tr>
       
   445     <td>
       
   446       <b><?php echo $lang->get('dbmysql_field_rootauth_title'); ?></b><br />
       
   447       <?php echo $lang->get('dbmysql_field_rootauth_body'); ?><br />
       
   448       <span style="color: #993300" id="e_db_root"></span>
       
   449     </td>
       
   450     <td>
       
   451       <input onkeyup="verify(this);" tabindex="6" name="db_root_user" size="30" type="text" /><br />
       
   452       <br />
       
   453       <input onkeyup="verify(this);" tabindex="7" name="db_root_pass" size="30" type="password" />
       
   454     </td>
       
   455     <td>
       
   456       <img id="s_db_root" alt="Good/bad icon" src="../images/good.gif" />
       
   457     </td>
       
   458   </tr>
       
   459   <tr>
       
   460     <td>
       
   461       <b><?php echo $lang->get('dbmysql_field_mysqlversion_title'); ?></b>
       
   462     </td>
       
   463     <td id="e_mysql_version">
       
   464       <?php echo $lang->get('dbmysql_field_mysqlversion_blurb_willbechecked'); ?>
       
   465     </td>
       
   466     <td>
       
   467       <img id="s_mysql_version" alt="Good/bad icon" src="../images/unknown.gif" />
       
   468     </td>
       
   469   </tr>
       
   470   <tr>
       
   471     <td>
       
   472       <b><?php echo $lang->get('dbmysql_field_droptables_title'); ?></b><br />
       
   473       <?php echo $lang->get('dbmysql_field_droptables_body'); ?>
       
   474     </td>
       
   475     <td colspan="2">
       
   476       <input type="checkbox" tabindex="8" name="drop_tables" id="dtcheck" />  <label for="dtcheck"><?php echo $lang->get('dbmysql_field_droptables_lbl'); ?></label>
       
   477     </td>
       
   478   </tr>
       
   479   <tr>
       
   480     <td colspan="3" style="text-align: center">
       
   481       <input type="button" tabindex="9" value="<?php echo $lang->get('dbmysql_btn_testconnection'); ?>" onclick="ajaxTestConnection();" />
       
   482       <div id="verify_error"></div>
       
   483     </td>
       
   484   </tr>
       
   485 
       
   486 </table>
       
   487 
       
   488 <table border="0">
       
   489   <tr>
       
   490     <td>
       
   491       <input type="submit" tabindex="10" value="<?php echo $lang->get('meta_btn_continue'); ?>" onclick="return verify();" name="_cont" />
       
   492     </td>
       
   493     <td>
       
   494       <p>
       
   495         <span style="font-weight: bold;"><?php echo $lang->get('meta_lbl_before_continue'); ?></span><br />
       
   496         &bull; <?php echo $lang->get('database_objective_test'); ?><br />
       
   497         &bull; <?php echo $lang->get('database_objective_uncrypt'); ?>
       
   498       </p>
       
   499     </td>
       
   500   </tr>
       
   501 </table>
       
   502 
       
   503 </form>
       
   504 
       
   505 <script type="text/javascript">
       
   506   verify();
       
   507 </script>
       
   508