ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SimpleSAML\Store\SQL Class Reference
+ Inheritance diagram for SimpleSAML\Store\SQL:
+ Collaboration diagram for SimpleSAML\Store\SQL:

Public Member Functions

 getTableVersion ($name)
 Get table version. More...
 
 setTableVersion ($name, $version)
 Set table version. More...
 
 insertOrUpdate ($table, array $keys, array $data)
 Insert or update a key-value in the store. More...
 
 get ($type, $key)
 Retrieve a value from the data store. More...
 
 set ($type, $key, $value, $expire=null)
 Save a value in the data store. More...
 
 delete ($type, $key)
 Delete an entry from the data store. More...
 
- Public Member Functions inherited from SimpleSAML\Store
 get ($type, $key)
 Retrieve a value from the data store. More...
 
 set ($type, $key, $value, $expire=null)
 Save a value to the data store. More...
 
 delete ($type, $key)
 Delete a value from the data store. More...
 

Data Fields

 $pdo
 
 $driver
 
 $prefix
 

Protected Member Functions

 __construct ()
 Initialize the SQL data store. More...
 

Private Member Functions

 initTableVersionTable ()
 Initialize the table-version table. More...
 
 initKVTable ()
 Initialize key-value table. More...
 
 cleanKVStore ()
 Clean the key-value table of expired entries. More...
 

Private Attributes

 $tableVersions
 

Additional Inherited Members

- Static Public Member Functions inherited from SimpleSAML\Store
static getInstance ()
 Retrieve our singleton instance. More...
 

Detailed Description

Definition at line 14 of file SQL.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML\Store\SQL::__construct ( )
protected

Initialize the SQL data store.

Definition at line 51 of file SQL.php.

References $config, $password, SimpleSAML\Store\SQL\initKVTable(), and SimpleSAML\Store\SQL\initTableVersionTable().

52  {
53  $config = Configuration::getInstance();
54 
55  $dsn = $config->getString('store.sql.dsn');
56  $username = $config->getString('store.sql.username', null);
57  $password = $config->getString('store.sql.password', null);
58  $this->prefix = $config->getString('store.sql.prefix', 'simpleSAMLphp');
59 
60  $this->pdo = new \PDO($dsn, $username, $password);
61  $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
62 
63  $this->driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME);
64 
65  if ($this->driver === 'mysql') {
66  $this->pdo->exec('SET time_zone = "+00:00"');
67  }
68 
69  $this->initTableVersionTable();
70  $this->initKVTable();
71  }
initKVTable()
Initialize key-value table.
Definition: SQL.php:100
initTableVersionTable()
Initialize the table-version table.
Definition: SQL.php:77
$password
Definition: pwgen.php:17
+ Here is the call graph for this function:

Member Function Documentation

◆ cleanKVStore()

SimpleSAML\Store\SQL::cleanKVStore ( )
private

Clean the key-value table of expired entries.

Definition at line 230 of file SQL.php.

References $params, $query, array, and SimpleSAML\Logger\debug().

Referenced by SimpleSAML\Store\SQL\set().

231  {
232  Logger::debug('store.sql: Cleaning key-value store.');
233 
234  $query = 'DELETE FROM '.$this->prefix.'_kvstore WHERE _expire < :now';
235  $params = array('now' => gmdate('Y-m-d H:i:s'));
236 
237  $query = $this->pdo->prepare($query);
238  $query->execute($params);
239  }
$params
Definition: disable.php:11
static debug($string)
Definition: Logger.php:213
$query
Create styles array
The data for the language used.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ delete()

SimpleSAML\Store\SQL::delete (   $type,
  $key 
)

Delete an entry from the data store.

Parameters
string$typeThe type of the data
string$keyThe key to delete.

Definition at line 331 of file SQL.php.

References $data, $query, $type, and array.

332  {
333  assert('is_string($type)');
334  assert('is_string($key)');
335 
336  if (strlen($key) > 50) {
337  $key = sha1($key);
338  }
339 
340  $data = array(
341  '_type' => $type,
342  '_key' => $key,
343  );
344 
345  $query = 'DELETE FROM '.$this->prefix.'_kvstore WHERE _type=:_type AND _key=:_key';
346  $query = $this->pdo->prepare($query);
347  $query->execute($data);
348  }
$type
$query
Create styles array
The data for the language used.
$key
Definition: croninfo.php:18

◆ get()

SimpleSAML\Store\SQL::get (   $type,
  $key 
)

Retrieve a value from the data store.

Parameters
string$typeThe type of the data.
string$keyThe key to retrieve.
Returns
mixed|null The value associated with that key, or null if there's no such key.

Definition at line 250 of file SQL.php.

References $key, $params, $query, $row, $type, and array.

251  {
252  assert('is_string($type)');
253  assert('is_string($key)');
254 
255  if (strlen($key) > 50) {
256  $key = sha1($key);
257  }
258 
259  $query = 'SELECT _value FROM '.$this->prefix.
260  '_kvstore WHERE _type = :type AND _key = :key AND (_expire IS NULL OR _expire > :now)';
261  $params = array('type' => $type, 'key' => $key, 'now' => gmdate('Y-m-d H:i:s'));
262 
263  $query = $this->pdo->prepare($query);
264  $query->execute($params);
265 
266  $row = $query->fetch(\PDO::FETCH_ASSOC);
267  if ($row === false) {
268  return null;
269  }
270 
271  $value = $row['_value'];
272  if (is_resource($value)) {
273  $value = stream_get_contents($value);
274  }
275  $value = urldecode($value);
276  $value = unserialize($value);
277 
278  if ($value === false) {
279  return null;
280  }
281  return $value;
282  }
$params
Definition: disable.php:11
$type
$query
Create styles array
The data for the language used.
$key
Definition: croninfo.php:18

◆ getTableVersion()

SimpleSAML\Store\SQL::getTableVersion (   $name)

Get table version.

Parameters
string$nameTable name.
Returns
int The table version, or 0 if the table doesn't exist.

Definition at line 131 of file SQL.php.

References $name.

Referenced by SimpleSAML\Store\SQL\initKVTable().

132  {
133  assert('is_string($name)');
134 
135  if (!isset($this->tableVersions[$name])) {
136  return 0;
137  }
138 
139  return $this->tableVersions[$name];
140  }
if($format !==null) $name
Definition: metadata.php:146
+ Here is the caller graph for this function:

◆ initKVTable()

SimpleSAML\Store\SQL::initKVTable ( )
private

Initialize key-value table.

Definition at line 100 of file SQL.php.

References $query, SimpleSAML\Store\SQL\getTableVersion(), and SimpleSAML\Store\SQL\setTableVersion().

Referenced by SimpleSAML\Store\SQL\__construct().

101  {
102  if ($this->getTableVersion('kvstore') === 1) {
103  // Table initialized
104  return;
105  }
106 
107  $text_t = 'TEXT';
108  if ($this->driver === 'mysql') {
109  // TEXT data type has size constraints that can be hit at some point, so we use LONGTEXT instead
110  $text_t = 'LONGTEXT';
111  }
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))';
115  $this->pdo->exec($query);
116 
117  $query = 'CREATE INDEX '.$this->prefix.'_kvstore_expire ON '.$this->prefix.'_kvstore (_expire)';
118  $this->pdo->exec($query);
119 
120  $this->setTableVersion('kvstore', 1);
121  }
setTableVersion($name, $version)
Set table version.
Definition: SQL.php:149
$query
getTableVersion($name)
Get table version.
Definition: SQL.php:131
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initTableVersionTable()

SimpleSAML\Store\SQL::initTableVersionTable ( )
private

Initialize the table-version table.

Definition at line 77 of file SQL.php.

References $row, and array.

Referenced by SimpleSAML\Store\SQL\__construct().

78  {
79  $this->tableVersions = array();
80 
81  try {
82  $fetchTableVersion = $this->pdo->query('SELECT _name, _version FROM '.$this->prefix.'_tableVersion');
83  } catch (\PDOException $e) {
84  $this->pdo->exec(
85  'CREATE TABLE '.$this->prefix.
86  '_tableVersion (_name VARCHAR(30) NOT NULL UNIQUE, _version INTEGER NOT NULL)'
87  );
88  return;
89  }
90 
91  while (($row = $fetchTableVersion->fetch(\PDO::FETCH_ASSOC)) !== false) {
92  $this->tableVersions[$row['_name']] = (int) $row['_version'];
93  }
94  }
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ insertOrUpdate()

SimpleSAML\Store\SQL::insertOrUpdate (   $table,
array  $keys,
array  $data 
)

Insert or update a key-value in the store.

Since various databases implement different methods for doing this, we abstract it away here.

Parameters
string$tableThe table we should update.
array$keysThe key columns.
array$dataAssociative array with columns.

Definition at line 172 of file SQL.php.

References $query, array, SimpleSAML\Logger\error(), and string.

Referenced by SimpleSAML\Store\SQL\set(), and SimpleSAML\Store\SQL\setTableVersion().

173  {
174  assert('is_string($table)');
175 
176  $colNames = '('.implode(', ', array_keys($data)).')';
177  $values = 'VALUES(:'.implode(', :', array_keys($data)).')';
178 
179  switch ($this->driver) {
180  case 'mysql':
181  $query = 'REPLACE INTO '.$table.' '.$colNames.' '.$values;
182  $query = $this->pdo->prepare($query);
183  $query->execute($data);
184  return;
185  case 'sqlite':
186  $query = 'INSERT OR REPLACE INTO '.$table.' '.$colNames.' '.$values;
187  $query = $this->pdo->prepare($query);
188  $query->execute($data);
189  return;
190  }
191 
192  // default implementation, try INSERT, and UPDATE if that fails.
193  $insertQuery = 'INSERT INTO '.$table.' '.$colNames.' '.$values;
194  $insertQuery = $this->pdo->prepare($insertQuery);
195  try {
196  $insertQuery->execute($data);
197  return;
198  } catch (\PDOException $e) {
199  $ecode = (string) $e->getCode();
200  switch ($ecode) {
201  case '23505': // PostgreSQL
202  break;
203  default:
204  Logger::error('Error while saving data: '.$e->getMessage());
205  throw $e;
206  }
207  }
208 
209  $updateCols = array();
210  $condCols = array();
211  foreach ($data as $col => $value) {
212  $tmp = $col.' = :'.$col;
213 
214  if (in_array($col, $keys, true)) {
215  $condCols[] = $tmp;
216  } else {
217  $updateCols[] = $tmp;
218  }
219  }
220 
221  $updateQuery = 'UPDATE '.$table.' SET '.implode(',', $updateCols).' WHERE '.implode(' AND ', $condCols);
222  $updateQuery = $this->pdo->prepare($updateQuery);
223  $updateQuery->execute($data);
224  }
Add rich text string
$keys
static error($string)
Definition: Logger.php:168
$query
Create styles array
The data for the language used.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set()

SimpleSAML\Store\SQL::set (   $type,
  $key,
  $value,
  $expire = null 
)

Save a value in the data store.

Parameters
string$typeThe type of the data.
string$keyThe key to insert.
mixed$valueThe value itself.
int | null$expireThe expiration time (unix timestamp), or null if it never expires.

Definition at line 293 of file SQL.php.

References $data, $type, array, SimpleSAML\Store\SQL\cleanKVStore(), and SimpleSAML\Store\SQL\insertOrUpdate().

294  {
295  assert('is_string($type)');
296  assert('is_string($key)');
297  assert('is_null($expire) || (is_int($expire) && $expire > 2592000)');
298 
299  if (rand(0, 1000) < 10) {
300  $this->cleanKVStore();
301  }
302 
303  if (strlen($key) > 50) {
304  $key = sha1($key);
305  }
306 
307  if ($expire !== null) {
308  $expire = gmdate('Y-m-d H:i:s', $expire);
309  }
310 
311  $value = serialize($value);
312  $value = rawurlencode($value);
313 
314  $data = array(
315  '_type' => $type,
316  '_key' => $key,
317  '_value' => $value,
318  '_expire' => $expire,
319  );
320 
321  $this->insertOrUpdate($this->prefix.'_kvstore', array('_type', '_key'), $data);
322  }
$expire
Definition: saml2-acs.php:140
$type
cleanKVStore()
Clean the key-value table of expired entries.
Definition: SQL.php:230
insertOrUpdate($table, array $keys, array $data)
Insert or update a key-value in the store.
Definition: SQL.php:172
Create styles array
The data for the language used.
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:

◆ setTableVersion()

SimpleSAML\Store\SQL::setTableVersion (   $name,
  $version 
)

Set table version.

Parameters
string$nameTable name.
int$versionTable version.

Definition at line 149 of file SQL.php.

References $name, $version, array, and SimpleSAML\Store\SQL\insertOrUpdate().

Referenced by SimpleSAML\Store\SQL\initKVTable().

150  {
151  assert('is_string($name)');
152  assert('is_int($version)');
153 
154  $this->insertOrUpdate(
155  $this->prefix.'_tableVersion',
156  array('_name'),
157  array('_name' => $name, '_version' => $version)
158  );
159  $this->tableVersions[$name] = $version;
160  }
if($format !==null) $name
Definition: metadata.php:146
insertOrUpdate($table, array $keys, array $data)
Insert or update a key-value in the store.
Definition: SQL.php:172
Create styles array
The data for the language used.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $driver

SimpleSAML\Store\SQL::$driver

Definition at line 29 of file SQL.php.

◆ $pdo

SimpleSAML\Store\SQL::$pdo

Definition at line 21 of file SQL.php.

◆ $prefix

SimpleSAML\Store\SQL::$prefix

Definition at line 37 of file SQL.php.

◆ $tableVersions

SimpleSAML\Store\SQL::$tableVersions
private

Definition at line 45 of file SQL.php.


The documentation for this class was generated from the following file: