Exemplo n.º 1
0
 /**
  * Connect to and select database.
  *
  * If $allow_bail is false, the lack of database connection will need
  * to be handled manually. 
  *
  * @since 3.0.0
  * @since 3.9.0 $allow_bail parameter added. 
  *
  * @param bool $allow_bail Optional. Allows the function to bail. Default true.
  *
  * @return bool True with a successful connection, false on failure.
  */
 public function db_connect($allow_bail = true)
 {
     if (!$this->dbh) {
         return false;
     }
     $this->is_mysql = $this->dbh->is_mysql();
     if (false !== strpos($this->dbhost, ':')) {
         list($host, $port) = explode(':', $this->dbhost);
     } else {
         $host = $this->dbhost;
         $port = 3306;
     }
     $options = array();
     $options['key'] = defined('DB_SSL_KEY') ? DB_SSL_KEY : null;
     $options['cert'] = defined('DB_SSL_CERT') ? DB_SSL_CERT : null;
     $options['ca'] = defined('DB_SSL_CA') ? DB_SSL_CA : null;
     $options['ca_path'] = defined('DB_SSL_CA_PATH') ? DB_SSL_CA_PATH : null;
     $options['cipher'] = defined('DB_SSL_CIPHER') ? DB_SSL_CIPHER : null;
     $is_connected = $this->dbh->connect($host, $this->dbuser, $this->dbpassword, $port, $options);
     if (!$is_connected && !$this->dbh instanceof wpdb_driver_mysql) {
         $this->dbh = null;
         $attempt_fallback = true;
         if ($this->has_connected) {
             $attempt_fallback = false;
         } elseif (defined('WP_USE_EXT_MYSQL') && !WP_USE_EXT_MYSQL) {
             $attempt_fallback = false;
         }
         $drivers = WP_DB_Driver_Config::get_drivers();
         $driver = 'wpdb_driver_mysql';
         include_once $drivers[$driver];
         if ($attempt_fallback && call_user_func(array($driver, 'is_supported'))) {
             $this->dbh = new $driver();
             return $this->db_connect($allow_bail);
         }
     }
     if (!$is_connected && $allow_bail) {
         wp_load_translations_early();
         // Load custom DB error template, if present.
         if (file_exists(WP_CONTENT_DIR . '/db-error.php')) {
             require_once WP_CONTENT_DIR . '/db-error.php';
             die;
         }
         $message = '<h1>' . __('Error establishing a database connection') . "</h1>\n";
         $message .= '<p>' . sprintf(__('This either means that the username and password information in your %1$s file is incorrect or we can&#8217;t contact the database server at %2$s. This could mean your host&#8217;s database server is down.'), '<code>wp-config.php</code>', '<code>' . htmlspecialchars($this->dbhost, ENT_QUOTES) . '</code>') . "</p>\n";
         $message .= "<ul>\n";
         $message .= '<li>' . __('Are you sure you have the correct username and password?') . "</li>\n";
         $message .= '<li>' . __('Are you sure that you have typed the correct hostname?') . "</li>\n";
         $message .= '<li>' . __('Are you sure that the database server is running?') . "</li>\n";
         $message .= "</ul>\n";
         $message .= '<p>' . sprintf(__('If you&#8217;re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress Support Forums</a>.'), __('https://wordpress.org/support/')) . "</p>\n";
         $this->bail($message, 'db_connect_fail');
         return false;
     } elseif ($is_connected) {
         $this->has_connected = true;
         $this->ready = true;
         $this->set_charset($this->dbh);
         $this->set_sql_mode();
         $this->select($this->dbname, $this->dbh);
         return true;
     }
     return false;
 }
Exemplo n.º 2
0
 public function page_overview()
 {
     if (!current_user_can('manage_options')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     // Load if our custom wpdb wasn't loaded yet.
     require_once dirname(__FILE__) . '/inc/config.php';
     require_once dirname(__FILE__) . '/inc/interface-wp-db-driver.php';
     echo '<div class="wrap">';
     screen_icon('options-general');
     echo '<h2>' . esc_html(get_admin_page_title()) . '</h2>';
     // Don't force a specific file system method
     $method = '';
     // Define any extra pass-thru fields (none)
     $form_fields = array();
     // Define the URL to post back to (this one)
     $url = $_SERVER['REQUEST_URI'];
     // Install flags
     $do_install = isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'install-db-nonce');
     $do_uninstall = isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'uninstall-db-nonce');
     if (is_super_admin() && ($do_install || $do_uninstall)) {
         // Ask for credentials, if necessary
         if (false === ($creds = request_filesystem_credentials($url, $method, false, false, $form_fields))) {
             return true;
         } elseif (!WP_Filesystem($creds)) {
             // The credentials are bad, ask again
             request_filesystem_credentials($url, $method, true, false, $form_fields);
             return true;
         } else {
             // Once we get here, we should have credentials, do the file system operations
             global $wp_filesystem;
             // Install
             if ($do_install) {
                 if ($wp_filesystem->put_contents($wp_filesystem->wp_content_dir() . '/db.php', file_get_contents(dirname(__FILE__) . '/wp-content/db.php'), FS_CHMOD_FILE)) {
                     echo '<div class="updated"><p><strong>' . __('db.php has been installed.', 'wp-db-driver') . '</strong></p></div>';
                 } else {
                     echo '<div class="error"><p><strong>' . __("db.php couldn't be installed. Please try is manually", 'wp-db-driver') . '</strong></p></div>';
                 }
                 // Remove
             } elseif ($do_uninstall) {
                 if ($wp_filesystem->delete($wp_filesystem->wp_content_dir() . '/db.php')) {
                     echo '<div class="updated"><p><strong>' . __('db.php has been removed.', 'wp-db-driver') . '</strong></p></div>';
                 } else {
                     echo '<div class="error"><p><strong>' . __("db.php couldn't be removed. Please try is manually", 'wp-db-driver') . '</strong></p></div>';
                 }
             }
         }
     }
     echo '<div class="tool-box"><h3 class="title">' . __('Current driver', 'wp-db-driver') . '</h3></div>';
     if (file_exists(WP_CONTENT_DIR . '/db.php')) {
         $crc1 = md5_file(dirname(__FILE__) . '/wp-content/db.php');
         $crc2 = md5_file(WP_CONTENT_DIR . '/db.php');
         if ($crc1 === $crc2) {
             echo '<form method="post" style="display: inline;">';
             wp_nonce_field('uninstall-db-nonce');
             echo '<p><strong>' . call_user_func(array(WP_DB_Driver_Config::get_current_driver(), 'get_name')) . '</strong> &nbsp; ';
             if (function_exists('mysql') && is_super_admin()) {
                 submit_button(__('Remove', 'wp-db-driver'), 'primary', 'install-db-php', false);
             }
             echo '</p>';
             echo '</form>';
         } else {
             echo '<form method="post" style="display: inline;">';
             wp_nonce_field('install-db-nonce');
             echo '<p><strong>' . __('Another db.php is installed', 'wp-db-driver') . '</strong> &nbsp; ';
             if (is_super_admin()) {
                 submit_button(__('Install', 'wp-db-driver'), 'primary', 'install-db-php', false);
             }
             echo '</p>';
             echo '</form>';
         }
     } else {
         echo '<form method="post" style="display: inline;">';
         wp_nonce_field('install-db-nonce');
         echo '<p><strong>' . __('No custom db.php installed', 'wp-db-driver') . '</strong> &nbsp; ';
         if (is_super_admin()) {
             submit_button(__('Install', 'wp-db-driver'), 'primary', 'install-db-php', false);
         }
         echo '</p>';
         echo '</form>';
     }
     echo '<div class="tool-box"><h3 class="title">' . __('Supported drivers', 'wp-db-driver') . '</h3></div>';
     echo '<table class="form-table">';
     $drivers = WP_DB_Driver_Config::get_drivers();
     foreach ($drivers as $driver => $file) {
         include_once $file;
         echo '<tr>';
         echo '<th>' . call_user_func(array($driver, 'get_name')) . '</th>';
         echo '<td>';
         if (call_user_func(array($driver, 'is_supported'))) {
             _e('Installed', 'wp-db-driver');
         } else {
             _e('Not installed', 'wp-db-driver');
         }
         echo '</td>';
         echo '</tr>';
     }
     echo '</table>';
 }