Permalink
Browse files

Merge pull request #2289 from YOURLS/issue2288

More checks at init for need to install. Should fix #2288
  • Loading branch information...
ozh committed Sep 6, 2017
2 parents c2bc799 + daf8269 commit ff104507c63449cf1d3d110eac3efc1405559c29
Showing with 65 additions and 21 deletions.
  1. +27 −6 includes/YOURLS/Database/Options.php
  2. +25 −7 includes/YOURLS/Database/YDB.php
  3. +3 −2 includes/class-mysql.php
  4. +10 −6 includes/functions.php
@@ -18,7 +18,7 @@
namespace YOURLS\Database;
use YOURLS\Database\YDB;
use PDOException;
class Options {
@@ -34,22 +34,43 @@ public function __construct(YDB $ydb) {
}
/**
* Read all options from DB at once
* Read all options from DB at once, return bool
*
* @since 1.7.3
* @return bool
* @return bool True on success, false on failure (eg table missing or empty)
*/
public function get_all_options() {
// Get option values from DB
$table = YOURLS_DB_TABLE_OPTIONS;
$sql = "SELECT option_name, option_value FROM $table WHERE 1=1";
$options = (array) $this->ydb->fetchPairs($sql);
if(empty($options)) {
try {
$options = (array) $this->ydb->fetchPairs($sql);
} catch ( PDOException $e ) {
// We could not fetch value from the table. Let's check if the option table exists
try {
$check = $this->ydb->fetchAffected(sprintf("SHOW TABLES LIKE '%s'", $table));
// Table doesn't exist
if ($check ==0) {
return false;
}
// Error at this point means the database isn't readable
} catch ( PDOException $e ) {
$this->ydb->dead_or_error($e);
}
}
// Unlikely scenario, but who knows: table exists, but is empty
if (empty($options)) {
return false;
}
foreach($options as $name => $value) {
foreach ($options as $name => $value) {
$this->ydb->set_option($name, yourls_maybe_unserialize($value));
}
@@ -70,10 +70,6 @@ class YDB extends ExtendedPdo {
protected $is_emulate_prepare;
/**
* Class constructor
*
* Don't forget to end with a call to the parent constructor
*
* @since 1.7.3
* @param string $dsn The data source name
* @param string $user The username
@@ -83,12 +79,24 @@ class YDB extends ExtendedPdo {
*/
public function __construct($dsn, $user, $pass, $options, $attributes) {
parent::__construct($dsn, $user, $pass, $options, $attributes);
}
/**
* Init everything needed
*
* Everything we need to set up is done here in init(), not in the constructor, so even
* when the connection fails (eg config error or DB dead), the constructor has worked
* and we have a $ydb object properly instantiated (and for instance yourls_die() can
* correctly die, even if using $ydb methods)
*
* @since 1.7.3
* @return void
*/
public function init() {
$this->connect_to_DB();
$this->set_emulate_state();
// Log query infos
$this->start_profiler();
}
@@ -132,18 +140,28 @@ public function connect_to_DB() {
try {
$this->connect();
} catch ( \Exception $e ) {
$this->dead_or_error($e);
}
}
/**
* Die with an error message
*
* @since 1.7.3
* @return void
*/
public function dead_or_error(\Exception $exception) {
// Use any /user/db_error.php file
if( file_exists( YOURLS_USERDIR . '/db_error.php' ) ) {
include_once( YOURLS_USERDIR . '/db_error.php' );
die();
}
$message = yourls__( 'Incorrect DB config, or could not connect to DB' );
$message .= '<br/>' . get_class($e) .': ' . $e->getMessage();
$message .= '<br/>' . get_class($exception) .': ' . $exception->getMessage();
yourls_die( yourls__( $message ), yourls__( 'Fatal error' ), 503 );
die();
}
}
/**
View
@@ -53,11 +53,12 @@ function yourls_db_connect() {
$attributes = yourls_apply_filter( 'db_connect_attributes', array() ); // attributes as key-value pairs
$ydb = new \YOURLS\Database\YDB( $dsn, $user, $pass, $driver_options, $attributes );
$ydb->init();
// Past this point, we're connected
yourls_debug_log(sprintf('Connected to database %s on %s ', $dbname, $dbhost));
yourls_debug_mode(YOURLS_DEBUG);
return $ydb;
}
View
@@ -1094,12 +1094,10 @@ function yourls_get_all_options() {
global $ydb;
$options = new \YOURLS\Database\Options($ydb);
if($options->get_all_options() === false) {
// Zero option found: either YOURLS is not installed or DB server is dead
if( !yourls_is_db_alive() ) {
yourls_db_dead(); // YOURLS will die here
}
if ($options->get_all_options() === false) {
// Zero option found but no unexpected error so far: YOURLS isn't installed
yourls_set_installed(false);
return;
}
yourls_set_installed(true);
@@ -2320,7 +2318,13 @@ function yourls_debug_log( $msg ) {
*/
function yourls_get_debug_log() {
global $ydb;
return $ydb->getProfiler()->get_log();
// Check if we have a profiler registered (will not be the case if the DB hasn't been properly connected to)
if ($ydb->getProfiler()) {
return $ydb->getProfiler()->get_log();
}
return array();
}
/**

0 comments on commit ff10450

Please sign in to comment.