diff options
author | Olav Morken <olav.morken@uninett.no> | 2009-09-17 10:52:58 +0000 |
---|---|---|
committer | Olav Morken <olav.morken@uninett.no> | 2009-09-17 10:52:58 +0000 |
commit | 4a3c19afad7608652c8e3eb6436c3345e61c16b9 (patch) | |
tree | 797f31cc26054003ba830838348eb7bc1f8495bf /modules/sqlauth/lib/Auth/Source/SQL.php | |
parent | e99d5f8a1e391be891027ec2126f7605c0c02c0a (diff) | |
download | simplesamlphp-4a3c19afad7608652c8e3eb6436c3345e61c16b9.zip simplesamlphp-4a3c19afad7608652c8e3eb6436c3345e61c16b9.tar.gz simplesamlphp-4a3c19afad7608652c8e3eb6436c3345e61c16b9.tar.bz2 |
sqlauth: Set character set for connections.
git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1759 44740490-163a-0410-bde0-09ae8108e29a
Diffstat (limited to 'modules/sqlauth/lib/Auth/Source/SQL.php')
-rw-r--r-- | modules/sqlauth/lib/Auth/Source/SQL.php | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/modules/sqlauth/lib/Auth/Source/SQL.php b/modules/sqlauth/lib/Auth/Source/SQL.php index 36ffb77..a60087a 100644 --- a/modules/sqlauth/lib/Auth/Source/SQL.php +++ b/modules/sqlauth/lib/Auth/Source/SQL.php @@ -128,6 +128,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. * * On a successful login, this function should return the users attributes. On failure, @@ -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); |