diff --git a/modules/sqlauth/lib/Auth/Source/SQL.php b/modules/sqlauth/lib/Auth/Source/SQL.php index 36ffb77bcdf6b7b4600bbea5f40dff8e86f040fc..a60087ae5d3b40269a75ad66037f240b290fb836 100644 --- a/modules/sqlauth/lib/Auth/Source/SQL.php +++ b/modules/sqlauth/lib/Auth/Source/SQL.php @@ -127,6 +127,41 @@ class sspmod_sqlauth_Auth_Source_SQL extends sspmod_core_Auth_UserPassBase { } + /** + * Create a database connection. + * + * @return PDO The database connection. + */ + private function connect() { + try { + $db = new PDO($this->dsn, $this->username, $this->password); + } catch (PDOException $e) { + throw new Exception('sqlauth:' . $this->authId . ': - Failed to connect to \'' . + $this->dsn . '\': '. $e->getMessage()); + } + + $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + + $driver = explode(':', $this->dsn, 2); + $driver = strtolower($driver[0]); + + /* Driver specific initialization. */ + switch ($driver) { + case 'mysql': + /* Use UTF-8. */ + $db->exec("SET NAMES 'utf8'"); + break; + case 'pgsql': + /* Use UTF-8. */ + $db->exec("SET NAMES 'UTF8'"); + break; + } + + return $db; + } + + /** * Attempt to log in using the given username and password. * @@ -144,14 +179,7 @@ class sspmod_sqlauth_Auth_Source_SQL extends sspmod_core_Auth_UserPassBase { assert('is_string($username)'); assert('is_string($password)'); - try { - $db = new PDO($this->dsn, $this->username, $this->password); - } catch (PDOException $e) { - throw new Exception('sqlauth:' . $this->authId . ': - Failed to connect to \'' . - $this->dsn . '\': '. $e->getMessage()); - } - - $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $db = $this->connect(); try { $sth = $db->prepare($this->query);