From 32b6ccc9f798bb8db2287b084bfed4803ea782bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ho=CC=88=C3=9Fl?= Date: Fri, 14 Sep 2018 13:37:45 +0200 Subject: [PATCH] Allow ports in host name field - addresses #293 --- History.md | 1 + models/forms/AntragsgruenInitDb.php | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index 7f15165f53..51b544032b 100644 --- a/History.md +++ b/History.md @@ -6,6 +6,7 @@ - Bugfix: Underlined text was not rendered as such in the PHP-based PDF renderer. - The WYSIWYG-editor CKEDITOR was updated, including some bug fixes. - Temporary files for PDF and ODS generation are not stored in /tmp/ anymore, as there are hosters that block access to this directory. Instead, runtime/tmp is used. +- In the installation, host names including ports (like localhost:3306) are now supported for the database connection. ### Version 4.0.1 (2018-09-02) diff --git a/models/forms/AntragsgruenInitDb.php b/models/forms/AntragsgruenInitDb.php index b5a70de577..84e9abfb85 100644 --- a/models/forms/AntragsgruenInitDb.php +++ b/models/forms/AntragsgruenInitDb.php @@ -19,6 +19,7 @@ class AntragsgruenInitDb extends Model public $sqlHost; public $sqlUsername; public $sqlPassword; + public $sqlPort = 3306; public $sqlDB; public $sqlTablePrefix = ''; @@ -84,6 +85,11 @@ public function setAttributes($values, $safeOnly = true) } else { $this->prettyUrls = true; } + if (strpos($this->sqlHost, ':') !== false) { + list($host, $port) = explode(':', $this->sqlHost); + $this->sqlHost = $host; + $this->sqlPort = IntVal($port); + } } /** @@ -104,6 +110,9 @@ private function setMysqlFromEnv() $this->sqlDB = $_ENV['ANTRAGSGRUEN_MYSQL_DB']; $this->sqlHost = $_ENV['ANTRAGSGRUEN_MYSQL_HOST']; $this->sqlType = 'mysql'; + if (isset($_ENV['ANTRAGSGRUEN_MYSQL_PORT'])) { + $this->sqlPort = IntVal($_ENV['ANTRAGSGRUEN_MYSQL_PORT']); + } } /** @@ -136,6 +145,9 @@ private function setDatabaseFromParams($params) if ($parts[0] === 'host') { $this->sqlHost = $parts[1]; } + if ($parts[0] === 'port') { + $this->sqlPort = $parts[1]; + } } } } @@ -148,8 +160,12 @@ private function setDatabaseFromParams($params) protected function getDBConfig() { if ($this->sqlType == 'mysql') { + $dsn = 'mysql:host=' . $this->sqlHost . ';dbname=' . $this->sqlDB; + if ($this->sqlPort !== 3306) { + $dsn .= ';port=' . $this->sqlPort; + } return [ - 'dsn' => 'mysql:host=' . $this->sqlHost . ';dbname=' . $this->sqlDB, + 'dsn' => $dsn, 'emulatePrepare' => true, 'username' => $this->sqlUsername, 'password' => $this->sqlPassword, @@ -164,7 +180,7 @@ protected function getDBConfig() public function overwriteYiiConnection() { $connConfig = $this->getDBConfig(); - $connConfig['class'] = \yii\db\Connection::class; + $connConfig['class'] = Connection::class; \yii::$app->set('db', $connConfig); }