install/includes/stages/database_post.php
changeset 352 9d7225c0db6d
parent 348 87e08a6e4fec
child 391 85f91037cd4f
equal deleted inserted replaced
351:8978cb3541ca 352:9d7225c0db6d
    24 $db_host =& $_POST['db_host'];
    24 $db_host =& $_POST['db_host'];
    25 $db_user =& $_POST['db_user'];
    25 $db_user =& $_POST['db_user'];
    26 $db_pass =& $_POST['db_pass'];
    26 $db_pass =& $_POST['db_pass'];
    27 $db_name =& $_POST['db_name'];
    27 $db_name =& $_POST['db_name'];
    28 $db_prefix =& $_POST['table_prefix'];
    28 $db_prefix =& $_POST['table_prefix'];
       
    29 $db_root_user =& $_POST['db_root_user'];
       
    30 $db_root_pass =& $_POST['db_root_pass'];
       
    31 
       
    32 if ( !preg_match('/^[a-z0-9_]*$/', $db_prefix) )
       
    33 {
       
    34   $ui->show_header();
       
    35   echo '<p>That table prefix isn\'t going to work.</p>';
       
    36   return true;
       
    37 }
    29 
    38 
    30 $result = $dbal->connect(true, $db_host, $db_user, $db_pass, $db_name);
    39 $result = $dbal->connect(true, $db_host, $db_user, $db_pass, $db_name);
    31 
    40 
       
    41 // If connection failed, we have the root login, AND we're on MySQL, try to force our way in
       
    42 if ( !$result && !empty($_POST['db_root_user']) && !empty($_POST['db_root_pass']) && $driver == 'mysql' )
       
    43 {
       
    44   // Allow a jump / breakout
       
    45   switch ( 'foo' ) { case 'foo':
       
    46       
       
    47     // Try to connect to the DB as root
       
    48     $result_root = $dbal->connect(true, $db_host, $db_root_user, $db_root_pass, 'mysql');
       
    49     if ( !$result_root )
       
    50       break;
       
    51     
       
    52     $q = $dbal->sql_query('CREATE DATABASE IF NOT EXISTS `' . $dbal->escape($db_name) . '`;');
       
    53     if ( !$q )
       
    54       break;
       
    55     
       
    56     if ( $db_host == 'localhost' || $db_host == '127.0.0.1' )
       
    57     {
       
    58       $q = $dbal->sql_query('GRANT ALL PRIVILEGES ON `' . $dbal->escape($db_name) . '`.* TO \'' . $dbal->escape($db_user) . '\'@\'localhost\'' . "\n" .
       
    59                             '  IDENTIFIED BY \'' . $dbal->escape($db_pass) . '\' WITH GRANT OPTION');
       
    60     }
       
    61     else
       
    62     {
       
    63       $q = $dbal->sql_query('GRANT ALL PRIVILEGES ON `' . $dbal->escape($db_name) . '`.* TO \'' . $dbal->escape($db_user) . '\'@\'%\'' . "\n" .
       
    64                             '  IDENTIFIED BY \'' . $dbal->escape($db_pass) . '\' WITH GRANT OPTION');
       
    65     }
       
    66     
       
    67     if ( !$q )
       
    68       break;
       
    69     
       
    70     $dbal->close();
       
    71     $result = $dbal->connect(true, $db_host, $db_user, $db_pass, $db_name);
       
    72       
       
    73     break;
       
    74   }
       
    75 }
       
    76 
    32 $ui->show_header();
    77 $ui->show_header();
    33 
    78 
    34 if ( $result )
    79 if ( $result )
    35 {
    80 {
    36   // We're good, write out a config file
    81   // We're good, do table drop if requested
       
    82   if ( isset($_POST['drop_tables']) )
       
    83   {
       
    84     global $system_table_list;
       
    85     foreach ( $system_table_list as $table )
       
    86     {
       
    87       $dbal->sql_query("DROP TABLE {$db_prefix}$table");
       
    88     }
       
    89   }
       
    90   // Write out a config file
    37   $ch = @fopen( ENANO_ROOT . '/config.new.php', 'w' );
    91   $ch = @fopen( ENANO_ROOT . '/config.new.php', 'w' );
    38   if ( !$ch )
    92   if ( !$ch )
    39   {
    93   {
    40     ?>
    94     ?>
    41     <form action="install.php?stage=database" method="post" name="database_info">
    95     <form action="install.php?stage=database" method="post" name="database_info">