Merge pull request #2646 from tperalta82/master

Possibly fixed all the MySQL errors from my previous pull request
This commit is contained in:
Sebastian Grewe 2017-12-13 14:56:51 +01:00 committed by GitHub
commit 9628c34e82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 1375 deletions

2
.gitignore vendored
View File

@ -43,4 +43,4 @@ tests/_output/*
/nbproject/* /nbproject/*
# No need for composer.lock # No need for composer.lock
composer.lock /composer.lock

1358
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -253,8 +253,9 @@ class Base {
$this->setErrorMessage(call_user_func_array(array($this, 'getErrorMsg'), func_get_args())); $this->setErrorMessage(call_user_func_array(array($this, 'getErrorMsg'), func_get_args()));
} }
// Default to SQL error for debug and cron errors // Default to SQL error for debug and cron errors
$this->debug->append($this->getErrorMsg('E0019', $this->mysqli->error)); $this->debug->append($this->getErrorMsg('E0019', $this->mysqli->lastused->errno));
$this->setCronMessage($this->getErrorMsg('E0019', $this->mysqli->error)); $this->setCronMessage($this->getErrorMsg('E0019', $this->mysqli->lastused->errno));
return false; return false;
} }

View File

@ -6,7 +6,9 @@ class mysqlims extends mysqli
{ {
private $mysqliW; private $mysqliW;
private $mysqliR = null; private $mysqliR = null;
private $slave = false;
public $lastused = null;
/* /*
* Pass main and slave connection arrays to the constructor, and strict as true/false * Pass main and slave connection arrays to the constructor, and strict as true/false
* *
@ -27,6 +29,7 @@ class mysqlims extends mysqli
$this->mysqliR = new mysqli_strict($slave['host'], $this->mysqliR = new mysqli_strict($slave['host'],
$slave['user'], $slave['pass'], $slave['user'], $slave['pass'],
$slave['name'], $slave['port']); $slave['name'], $slave['port']);
$this->slave = true;
} }
} else { } else {
$this->mysqliW = new mysqli($main['host'], $this->mysqliW = new mysqli($main['host'],
@ -37,6 +40,7 @@ class mysqlims extends mysqli
$this->mysqliR = new mysqli($slave['host'], $this->mysqliR = new mysqli($slave['host'],
$slave['user'], $slave['pass'], $slave['user'], $slave['pass'],
$slave['name'], $slave['port']); $slave['name'], $slave['port']);
$this->slave = true;
} }
} }
@ -44,7 +48,7 @@ class mysqlims extends mysqli
throw new Exception("Failed to connect to MySQL: (".$this->mysqliW->connect_errno.") ".$this->mysqliW->connect_error); throw new Exception("Failed to connect to MySQL: (".$this->mysqliW->connect_errno.") ".$this->mysqliW->connect_error);
} }
if ($this->mysqliR->connect_errno) { if ($this->slave === true && $this->mysqliR->connect_errno) {
throw new Exception("Failed to connect to MySQL: (".$this->mysqliR->connect_errno.") ".$this->mysqliR->connect_error); throw new Exception("Failed to connect to MySQL: (".$this->mysqliR->connect_errno.") ".$this->mysqliR->connect_error);
} }
} }
@ -57,9 +61,11 @@ class mysqlims extends mysqli
*/ */
public function prepare($query) public function prepare($query)
{ {
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) { if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->slave !== false) {
$this->lastused = $this->mysqliR;
return $this->mysqliR->prepare($query); return $this->mysqliR->prepare($query);
} else { } else {
$this->lastused = $this->mysqliW;
return $this->mysqliW->prepare($query); return $this->mysqliW->prepare($query);
} }
} }
@ -74,9 +80,11 @@ class mysqlims extends mysqli
*/ */
public function query($query, $resultmode = MYSQLI_STORE_RESULT) public function query($query, $resultmode = MYSQLI_STORE_RESULT)
{ {
if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) {/* Use readonly server */ if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->slave !== false) {/* Use readonly server */
$this->lastused = $this->mysqliR;
return $this->mysqliR->query($query, $resultmode); return $this->mysqliR->query($query, $resultmode);
} else { } else {
$this->lastused = $this->mysqliW;
return $this->mysqliW->query($query, $resultmode); return $this->mysqliW->query($query, $resultmode);
} }
} }

View File

@ -22,8 +22,10 @@ class Notification extends Mail {
$this->debug->append("STA " . __METHOD__, 4); $this->debug->append("STA " . __METHOD__, 4);
$data = json_encode($aData); $data = json_encode($aData);
$stmt = $this->mysqli->prepare("SELECT id FROM $this->table WHERE data = ? AND active = 1 LIMIT 1"); $stmt = $this->mysqli->prepare("SELECT id FROM $this->table WHERE data = ? AND active = 1 LIMIT 1");
if ($stmt && $stmt->bind_param('s', $data) && $stmt->execute() && $stmt->store_result() && $stmt->num_rows == 1) if ($stmt && $stmt->bind_param('s', $data) && $stmt->execute() && $stmt->store_result() && $stmt->num_rows == 1) {
return true; return true;
}
return $this->sqlError('E0041'); return $this->sqlError('E0041');
} }

View File

@ -79,7 +79,7 @@ class Template extends Base {
} }
$this->setErrorMessage('Failed to get active templates'); $this->setErrorMessage('Failed to get active templates');
$this->debug->append('Template::getActiveTemplates failed: ' . $this->mysqli->error); $this->debug->append('Template::getActiveTemplates failed: ' . $this->mysqli->lastused->error);
return false; return false;
} }
@ -172,7 +172,7 @@ class Template extends Base {
return $result->fetch_assoc(); return $result->fetch_assoc();
$this->setErrorMessage('Failed to get the template'); $this->setErrorMessage('Failed to get the template');
$this->debug->append('Template::getEntry failed: ' . $this->mysqli->error); $this->debug->append('Template::getEntry failed: ' . $this->mysqli->lastused->error);
return false; return false;
} }
@ -206,7 +206,7 @@ class Template extends Base {
return true; return true;
$this->setErrorMessage('Database error'); $this->setErrorMessage('Database error');
$this->debug->append('Template::updateEntry failed: ' . $this->mysqli->error); $this->debug->append('Template::updateEntry failed: ' . $this->mysqli->lastused->error);
return false; return false;
} }
} }

View File

@ -575,7 +575,7 @@ class User extends Base {
} }
// Catchall // Catchall
$this->setErrorMessage('Failed to update your account'); $this->setErrorMessage('Failed to update your account');
$this->debug->append('Account update failed: ' . $this->mysqli->error); $this->debug->append('Account update failed: ' . $this->mysqli->lastused->error);
return false; return false;
} }
@ -832,7 +832,7 @@ class User extends Base {
$signup_time = time(); $signup_time = time();
if ($this->checkStmt($stmt) && $stmt->bind_param('sssissi', $username_clean, $password_hash, $email1, $signup_time, $pin_hash, $apikey_hash, $is_locked) && $stmt->execute()) { if ($this->checkStmt($stmt) && $stmt->bind_param('sssissi', $username_clean, $password_hash, $email1, $signup_time, $pin_hash, $apikey_hash, $is_locked) && $stmt->execute()) {
$new_account_id = $this->mysqli->insert_id; $new_account_id = $this->mysqli->lastused->insert_id;
if (!is_null($coinaddress)) $this->coin_address->add($new_account_id, $coinaddress); if (!is_null($coinaddress)) $this->coin_address->add($new_account_id, $coinaddress);
if (! $this->setting->getValue('accounts_confirm_email_disabled') && $is_admin != 1) { if (! $this->setting->getValue('accounts_confirm_email_disabled') && $is_admin != 1) {
if ($token = $this->token->createToken('confirm_email', $stmt->insert_id)) { if ($token = $this->token->createToken('confirm_email', $stmt->insert_id)) {
@ -855,8 +855,8 @@ class User extends Base {
} }
} else { } else {
$this->setErrorMessage( 'Unable to register' ); $this->setErrorMessage( 'Unable to register' );
$this->debug->append('Failed to insert user into DB: ' . $this->mysqli->error); $this->debug->append('Failed to insert user into DB: ' . $this->mysqli->lastused->error);
echo $this->mysqli->error; echo $this->mysqli->lastused->error;
if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Username or email already registered' ); if ($stmt->sqlstate == '23000') $this->setErrorMessage( 'Username or email already registered' );
return false; return false;
} }
@ -895,7 +895,7 @@ class User extends Base {
} else { } else {
$this->setErrorMessage('Invalid token: ' . $this->token->getError()); $this->setErrorMessage('Invalid token: ' . $this->token->getError());
} }
$this->debug->append('Failed to update password:' . $this->mysqli->error); $this->debug->append('Failed to update password:' . $this->mysqli->lastused->error);
return false; return false;
} }