/** * Load custom DB error or display HiveQueen DB error. * * If a file exists in the hq-content directory named db-error.php, then it will * be loaded instead of displaying the WordPress DB error. If it is not found, * then the WordPress DB error will be displayed instead. * * The HiveQueen DB error sets the HTTP status header to 500 to try to prevent * search engines from caching the message. Custom DB messages should do the * same. * * @since 0.0.1 * * @global hqdb $hqdb HiveQueen database abstraction object. */ function dead_db() { global $hqdb; hq_load_translations_early(); // Load custom DB error template, if present. if (file_exists(HQ_CONTENT_DIR . '/db-error.php')) { require_once HQ_CONTENT_DIR . '/db-error.php'; die; } // If installing or in the admin, provide the verbose message. if (defined('HQ_INSTALLING') || defined('HQ_ADMIN')) { hq_die($hqdb->error); } // Otherwise, be terse. status_header(500); nocache_headers(); header('Content-Type: text/html; charset=utf-8'); ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"<?php if (is_rtl()) { echo ' dir="rtl"'; } ?> > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php _e('Database Error'); ?> </title> </head> <body> <h1><?php _e('Error establishing a database connection'); ?> </h1> </body> </html> <?php die; }
/** * Set the database table prefix and the format specifiers for database * table columns. * * Columns not listed here default to `%s`. * * @since 0.0.1 * @access private * * @global hqdb $hqdb The HiveQueen database class. * @global string $table_prefix The database table prefix. */ function hq_set_hqdb_vars() { global $hqdb, $table_prefix; if (!empty($hqdb->error)) { dead_db(); } //TODO: Redefine db /* $hqdb->field_types = array( 'post_author' => '%d', 'post_parent' => '%d', 'menu_order' => '%d', 'term_id' => '%d', 'term_group' => '%d', 'term_taxonomy_id' => '%d', 'parent' => '%d', 'count' => '%d','object_id' => '%d', 'term_order' => '%d', 'ID' => '%d', 'comment_ID' => '%d', 'comment_post_ID' => '%d', 'comment_parent' => '%d', 'user_id' => '%d', 'link_id' => '%d', 'link_owner' => '%d', 'link_rating' => '%d', 'option_id' => '%d', 'blog_id' => '%d', 'meta_id' => '%d', 'post_id' => '%d', 'user_status' => '%d', 'umeta_id' => '%d', 'comment_karma' => '%d', 'comment_count' => '%d', // multisite: 'active' => '%d', 'cat_id' => '%d', 'deleted' => '%d', 'lang_id' => '%d', 'mature' => '%d', 'public' => '%d', 'site_id' => '%d', 'spam' => '%d', ); */ $hqdb->field_types = array('menu_order' => '%d', 'term_id' => '%d', 'term_group' => '%d', 'parent' => '%d', 'count' => '%d', 'object_id' => '%d', 'term_order' => '%d', 'ID' => '%d', 'user_id' => '%d', 'option_id' => '%d', 'user_status' => '%d'); $prefix = $hqdb->set_prefix($table_prefix); if (is_hq_error($prefix)) { hq_load_translations_early(); hq_die(__('<strong>ERROR</strong>: <code>$table_prefix</code> in <code>hq-config.php</code> can only contain numbers, letters, and underscores.')); } }
} elseif (file_exists(dirname(ABSPATH) . '/hq-config.php') && !file_exists(dirname(ABSPATH) . '/hq-settings.php')) { /** The config file resides one level above ABSPATH but is not part of another install */ require_once dirname(ABSPATH) . '/hq-config.php'; } else { // A config file doesn't exist define('HQINC', 'hq-includes'); require_once ABSPATH . HQINC . '/load.php'; // Standardize $_SERVER variables across setups. hq_fix_server_vars(); require_once ABSPATH . HQINC . '/functions.php'; $path = hq_guess_url() . '/hq-admin/setup-config.php'; /* * We're going to redirect to setup-config.php. While this shouldn't result * in an infinite loop, that's a silly thing to assume, don't you think? If * we're traveling in circles, our last-ditch effort is "Need more help?" */ if (false === strpos($_SERVER['REQUEST_URI'], 'setup-config')) { header('Location: ' . $path); exit; } define('HQ_CONTENT_DIR', ABSPATH . 'hq-content'); require_once ABSPATH . HQINC . '/version.php'; hq_check_php_mysql_versions(); hq_load_translations_early(); // Die with an error message $die = __("There doesn't seem to be a <code>hq-config.php</code> file. I need this before we can get started.") . '</p>'; $die .= '<p>' . __("Need more help? <a href='https://codex.wordpress.org/Editing_hq-config.php'>We got it</a>.") . '</p>'; $die .= '<p>' . __("You can create a <code>hq-config.php</code> file through a web interface, but this doesn't work for all server setups. The safest way is to manually create the file.") . '</p>'; $die .= '<p><a href="' . $path . '" class="button button-large">' . __("Create a Configuration File") . '</a>'; hq_die($die, __('HiveQueen › Error')); }
/** * Connect to and select database. * * If $allow_bail is false, the lack of database connection will need * to be handled manually. * * @since 0.0.1 * @since 0.0.1 $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) { $this->is_mysql = true; /* * Deprecated in 3.9+ when using MySQLi. No equivalent * $new_link parameter exists for mysqli_* functions. */ $new_link = defined('MYSQL_NEW_LINK') ? MYSQL_NEW_LINK : true; $client_flags = defined('MYSQL_CLIENT_FLAGS') ? MYSQL_CLIENT_FLAGS : 0; if ($this->use_mysqli) { $this->dbh = mysqli_init(); // mysqli_real_connect doesn't support the host param including a port or socket // like mysql_connect does. This duplicates how mysql_connect detects a port and/or socket file. $port = null; $socket = null; $host = $this->dbhost; $port_or_socket = strstr($host, ':'); if (!empty($port_or_socket)) { $host = substr($host, 0, strpos($host, ':')); $port_or_socket = substr($port_or_socket, 1); if (0 !== strpos($port_or_socket, '/')) { $port = intval($port_or_socket); $maybe_socket = strstr($port_or_socket, ':'); if (!empty($maybe_socket)) { $socket = substr($maybe_socket, 1); } } else { $socket = $port_or_socket; } } if (HQ_DEBUG) { mysqli_real_connect($this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags); } else { @mysqli_real_connect($this->dbh, $host, $this->dbuser, $this->dbpassword, null, $port, $socket, $client_flags); } if ($this->dbh->connect_errno) { $this->dbh = null; /* It's possible ext/mysqli is misconfigured. Fall back to ext/mysql if: * - We haven't previously connected, and * - HQ_USE_EXT_MYSQL isn't set to false, and * - ext/mysql is loaded. */ $attempt_fallback = true; if ($this->has_connected) { $attempt_fallback = false; } elseif (defined('HQ_USE_EXT_MYSQL') && !HQ_USE_EXT_MYSQL) { $attempt_fallback = false; } elseif (!function_exists('mysql_connect')) { $attempt_fallback = false; } if ($attempt_fallback) { $this->use_mysqli = false; $this->db_connect(); } } } else { if (HQ_DEBUG) { $this->dbh = mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags); } else { $this->dbh = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags); } } if (!$this->dbh && $allow_bail) { hq_load_translations_early(); // Load custom DB error template, if present. if (file_exists(HQ_CONTENT_DIR . '/db-error.php')) { require_once HQ_CONTENT_DIR . '/db-error.php'; die; } $this->bail(sprintf(__("\n<h1>Error establishing a database connection</h1>\n<p>This either means that the username and password information in your <code>hq-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p>\n<ul>\n <li>Are you sure you have the correct username and password?</li>\n <li>Are you sure that you have typed the correct hostname?</li>\n <li>Are you sure that the database server is running?</li>\n</ul>\n<p>If you're unsure what these terms mean you should probably contact your host.</p>\n"), htmlspecialchars($this->dbhost, ENT_QUOTES)), 'db_connect_fail'); return false; } elseif ($this->dbh) { if (!$this->has_connected) { $this->init_charset(); } $this->has_connected = true; $this->set_charset($this->dbh); $this->ready = true; $this->set_sql_mode(); $this->select($this->dbname, $this->dbh); return true; } return false; }