Skip to content
Snippets Groups Projects
Commit 4a3c19af authored by Olav Morken's avatar Olav Morken
Browse files

sqlauth: Set character set for connections.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1759 44740490-163a-0410-bde0-09ae8108e29a
parent e99d5f8a
No related branches found
No related tags found
No related merge requests found
...@@ -127,6 +127,41 @@ class sspmod_sqlauth_Auth_Source_SQL extends sspmod_core_Auth_UserPassBase { ...@@ -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. * 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 { ...@@ -144,14 +179,7 @@ class sspmod_sqlauth_Auth_Source_SQL extends sspmod_core_Auth_UserPassBase {
assert('is_string($username)'); assert('is_string($username)');
assert('is_string($password)'); assert('is_string($password)');
try { $db = $this->connect();
$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);
try { try {
$sth = $db->prepare($this->query); $sth = $db->prepare($this->query);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment