6 use \SimpleSAML\Logger;
53 $config = Configuration::getInstance();
55 $dsn =
$config->getString(
'store.sql.dsn');
56 $username =
$config->getString(
'store.sql.username', null);
58 $this->prefix =
$config->getString(
'store.sql.prefix',
'simpleSAMLphp');
60 $this->pdo = new \PDO($dsn, $username,
$password);
61 $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
63 $this->driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
65 if ($this->driver ===
'mysql') {
66 $this->pdo->exec(
'SET time_zone = "+00:00"');
79 $this->tableVersions =
array();
82 $fetchTableVersion = $this->pdo->query(
'SELECT _name, _version FROM '.$this->prefix.
'_tableVersion');
83 }
catch (\PDOException $e) {
85 'CREATE TABLE '.$this->prefix.
86 '_tableVersion (_name VARCHAR(30) NOT NULL UNIQUE, _version INTEGER NOT NULL)' 91 while ((
$row = $fetchTableVersion->fetch(\PDO::FETCH_ASSOC)) !==
false) {
92 $this->tableVersions[
$row[
'_name']] = (int) $row[
'_version'];
108 if ($this->driver ===
'mysql') {
110 $text_t =
'LONGTEXT';
112 $query =
'CREATE TABLE '.$this->prefix.
113 '_kvstore (_type VARCHAR(30) NOT NULL, _key VARCHAR(50) NOT NULL, _value '.$text_t.
114 ' NOT NULL, _expire TIMESTAMP, PRIMARY KEY (_key, _type))';
117 $query =
'CREATE INDEX '.$this->prefix.
'_kvstore_expire ON '.$this->prefix.
'_kvstore (_expire)';
133 assert(
'is_string($name)');
135 if (!isset($this->tableVersions[
$name])) {
139 return $this->tableVersions[
$name];
151 assert(
'is_string($name)');
152 assert(
'is_int($version)');
155 $this->prefix.
'_tableVersion',
174 assert(
'is_string($table)');
176 $colNames =
'('.implode(
', ', array_keys($data)).
')';
177 $values =
'VALUES(:'.implode(
', :', array_keys($data)).
')';
179 switch ($this->driver) {
181 $query =
'REPLACE INTO '.$table.
' '.$colNames.
' '.$values;
186 $query =
'INSERT OR REPLACE INTO '.$table.
' '.$colNames.
' '.$values;
193 $insertQuery =
'INSERT INTO '.$table.
' '.$colNames.
' '.$values;
194 $insertQuery = $this->pdo->prepare($insertQuery);
196 $insertQuery->execute($data);
198 }
catch (\PDOException $e) {
199 $ecode = (
string) $e->getCode();
209 $updateCols =
array();
211 foreach ($data as $col => $value) {
212 $tmp = $col.
' = :'.$col;
214 if (in_array($col, $keys,
true)) {
217 $updateCols[] = $tmp;
221 $updateQuery =
'UPDATE '.$table.
' SET '.implode(
',', $updateCols).
' WHERE '.implode(
' AND ', $condCols);
222 $updateQuery = $this->pdo->prepare($updateQuery);
223 $updateQuery->execute($data);
234 $query =
'DELETE FROM '.$this->prefix.
'_kvstore WHERE _expire < :now';
252 assert(
'is_string($type)');
253 assert(
'is_string($key)');
255 if (strlen(
$key) > 50) {
259 $query =
'SELECT _value FROM '.$this->prefix.
260 '_kvstore WHERE _type = :type AND _key = :key AND (_expire IS NULL OR _expire > :now)';
267 if (
$row ===
false) {
271 $value =
$row[
'_value'];
272 if (is_resource($value)) {
273 $value = stream_get_contents($value);
275 $value = urldecode($value);
276 $value = unserialize($value);
278 if ($value ===
false) {
295 assert(
'is_string($type)');
296 assert(
'is_string($key)');
297 assert(
'is_null($expire) || (is_int($expire) && $expire > 2592000)');
299 if (rand(0, 1000) < 10) {
303 if (strlen($key) > 50) {
307 if ($expire !== null) {
308 $expire = gmdate(
'Y-m-d H:i:s', $expire);
311 $value = serialize($value);
312 $value = rawurlencode($value);
318 '_expire' => $expire,
333 assert(
'is_string($type)');
334 assert(
'is_string($key)');
336 if (strlen($key) > 50) {
345 $query =
'DELETE FROM '.$this->prefix.
'_kvstore WHERE _type=:_type AND _key=:_key';
initKVTable()
Initialize key-value table.
setTableVersion($name, $version)
Set table version.
initTableVersionTable()
Initialize the table-version table.
cleanKVStore()
Clean the key-value table of expired entries.
insertOrUpdate($table, array $keys, array $data)
Insert or update a key-value in the store.
Create styles array
The data for the language used.
getTableVersion($name)
Get table version.
__construct()
Initialize the SQL data store.
if(empty($password)) $table