openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

commit 005ac14d20e453ff1a11bd6630988598644c2344
parent 72143f9479486d54f8df3784a78fb59da18f0860
Author: Jan Dankert <develop@jandankert.de>
Date:   Mon, 30 Nov 2020 09:57:36 +0100

Fix: aborting transaction before changing the database connection; Refactoring: Cleanup databases

Diffstat:
Mmodules/cms/action/LoginAction.class.php | 1+
Mmodules/cms/auth/RememberAuth.class.php | 14++++++++++----
Mmodules/cms/generator/ValueGenerator.class.php | 2+-
Mmodules/database/Database.class.php | 62++++++++++++++++++++++++++++++++------------------------------
Mmodules/database/Statement.class.php | 9+--------
Mmodules/database/driver/PDODriver.class.php | 4+---
6 files changed, 46 insertions(+), 46 deletions(-)

diff --git a/modules/cms/action/LoginAction.class.php b/modules/cms/action/LoginAction.class.php @@ -84,6 +84,7 @@ class LoginAction extends BaseAction // Getting the first not-null information about the connection. return array_values(array_filter( array( $dbconf->get('description'), + $dbconf->get('label' ), $dbconf->get('name' ), $dbconf->get('host' ), $dbconf->get('driver'), diff --git a/modules/cms/auth/RememberAuth.class.php b/modules/cms/auth/RememberAuth.class.php @@ -117,13 +117,19 @@ SQL protected function makeDBWritable( $dbid ) { + $oldDB = Session::getDatabase(); + if ( $oldDB ) { + $oldDB->rollback(); + $oldDB->disconnect(); + } + $dbConfig = Configuration::subset(['database',$dbid]); $key = 'write'; - $db = new Database($dbConfig->merge( $dbConfig->subset($key) )->getConfig()); - $db->id = $dbid; - $db->start(); + $writableDB = new Database($dbConfig->merge( $dbConfig->subset($key) )->getConfig()); + $writableDB->id = $dbid; + $writableDB->start(); - Session::setDatabase( $db ); + Session::setDatabase( $writableDB ); } } \ No newline at end of file diff --git a/modules/cms/generator/ValueGenerator.class.php b/modules/cms/generator/ValueGenerator.class.php @@ -826,7 +826,7 @@ class ValueGenerator extends BaseGenerator $inhalt = DB::get()->id; break; case 'db_name': - $inhalt = @DB::get()->conf['label']; + $inhalt = @DB::get()->getLabel(); break; case 'project_id': $inhalt = $page->projectid; diff --git a/modules/database/Database.class.php b/modules/database/Database.class.php @@ -40,41 +40,27 @@ class Database * * @var String */ - var $id; + public $id; /** * Konfiguration der Datenbank-Verbindung * * @var array */ - var $conf; + public $conf; /** - * Kennzeichen, ob die Datenbank verf�gbar ist. - * - * @var Boolean - */ - var $available; - - /** - * Enth�lt eine Fehlermeldung (sofern verf�gbar). - * - * @var String - */ - var $error; - - /** * Client. * * @var PDODriver */ - var $client; + private $client; /** * Schalter, ob eine Transaktion begonnen wurde. * @var boolean */ - var $transactionInProgress = false; + private $transactionInProgress = false; /** @@ -82,30 +68,31 @@ class Database * @var array */ private static $DEFAULT_CONFIG = [ + // we need at least 1 prefix or suffix, because the raw table names are partially keywords in ANSI SQL. 'prefix' => 'cms_', 'suffix' => '', 'enabled' => true, 'name' => '', 'description' => '', - 'type' => 'pdo', + 'type' => 'pdo', // we are only supporting PDO 'driver' => 'mysql', - 'dsn' => '', + 'dsn' => '', // if no DSN is given, it will be created from user,host,port. 'user' => '', 'password' => '', 'host' => 'localhost', 'port' => 0, 'database' => '', - 'base64' => false, - 'persistent' => true, - 'charset' => 'UTF-8', - 'connection_sql' => '', - 'cmd' => '', - 'prepare' => true, - 'transaction' => true, + 'base64' => false, // should BLOBs be converted to Base64? + 'persistent' => true, // persistent connections are faster + 'charset' => 'UTF-8', // should be UTF-8 + 'connection_sql' => '', // Startup-SQL + 'cmd' => '', // maybe you want to start a SSH tunnel here + 'prepare' => true, // using prepared statements is a good idea + 'transaction' => true, // using transaction is a good idea 'update' => [ ], - 'auto_update' => true, + 'auto_update' => true, // auto update should always be enabled ]; @@ -171,8 +158,6 @@ class Database Logger::debug('Database connection established'); - - $this->available = true; } /** @@ -224,6 +209,7 @@ class Database public function disconnect() { $this->client->disconnect(); + $this->client = null; // clear references to the client } /** * @param $sql string das SQL @@ -251,4 +237,20 @@ class Database } } + + /** + * database label. + * + * @return string + */ + public function getLabel() { + return array_values(array_filter( array( + @$this->conf['description'], + @$this->conf['name' ], + $this->id, + @$this->conf['host' ], + @$this->conf['driver'], + @$this->conf['type' ], + )))[0]; + } } \ No newline at end of file diff --git a/modules/database/Statement.class.php b/modules/database/Statement.class.php @@ -98,14 +98,7 @@ class Statement public function execute( ) { // Ausfuehren... - $result = $this->client->query($this->stmt, $this->sql); - - if ( $result === FALSE ) - { - throw new DatabaseException( 'Statement '.$this->sql->query.' could not be executed: '.$this->client->error); - } - - return $result; + return $this->client->query($this->stmt, $this->sql); } diff --git a/modules/database/driver/PDODriver.class.php b/modules/database/driver/PDODriver.class.php @@ -172,10 +172,8 @@ class PDODriver $erg = $stmt->execute(); if ( $erg === false ) - { throw new DatabaseException( 'Could not execute prepared statement "'.$query->query.'": '.implode('/',$stmt->errorInfo()) ); - } - + return $stmt; }