Skip to content
Snippets Groups Projects
Commit 3e94234d authored by Tim van Dijen's avatar Tim van Dijen
Browse files

PSR-2 compliancy

parent 096cf1ba
Branches
Tags
No related merge requests found
...@@ -9,195 +9,210 @@ ...@@ -9,195 +9,210 @@
* @author Andreas Åkre Solberg <andreas@uninett.no>, UNINETT AS. * @author Andreas Åkre Solberg <andreas@uninett.no>, UNINETT AS.
* @package SimpleSAMLphp * @package SimpleSAMLphp
*/ */
class sspmod_core_Storage_SQLPermanentStorage { class sspmod_core_Storage_SQLPermanentStorage
{
private $db; private $db;
function __construct($name, $config = NULL) { public function __construct($name, $config = null)
if (is_null($config)) {
$config = SimpleSAML_Configuration::getInstance(); if (is_null($config)) {
$config = SimpleSAML_Configuration::getInstance();
$datadir = $config->getPathValue('datadir', 'data/'); }
if (!is_dir($datadir)) $datadir = $config->getPathValue('datadir', 'data/');
throw new Exception('Data directory [' . $datadir. '] does not exist');
if (!is_writable($datadir)) if (!is_dir($datadir)) {
throw new Exception('Data directory [' . $datadir. '] is not writable'); throw new Exception('Data directory [' . $datadir. '] does not exist');
} else if (!is_writable($datadir)) {
$sqllitedir = $datadir . 'sqllite/'; throw new Exception('Data directory [' . $datadir. '] is not writable');
if (!is_dir($sqllitedir)) { }
mkdir($sqllitedir);
} $sqllitedir = $datadir . 'sqllite/';
if (!is_dir($sqllitedir)) {
$dbfile = 'sqlite:' . $sqllitedir . $name . '.sqlite'; mkdir($sqllitedir);
if ($this->db = new \PDO($dbfile)) { }
$q = @$this->db->query('SELECT key1 FROM data LIMIT 1');
if ($q === false) { $dbfile = 'sqlite:' . $sqllitedir . $name . '.sqlite';
$this->db->exec(' if ($this->db = new \PDO($dbfile)) {
CREATE TABLE data ( $q = @$this->db->query('SELECT key1 FROM data LIMIT 1');
key1 text, if ($q === false) {
key2 text, $this->db->exec('
type text, CREATE TABLE data (
value text, key1 text,
created timestamp, key2 text,
updated timestamp, type text,
expire timestamp, value text,
PRIMARY KEY (key1,key2,type) created timestamp,
); updated timestamp,
'); expire timestamp,
} PRIMARY KEY (key1,key2,type)
} else { );
throw new Exception('Error creating SQL lite database [' . $dbfile . '].'); ');
} }
} } else {
throw new Exception('Error creating SQL lite database [' . $dbfile . '].');
public function set($type, $key1, $key2, $value, $duration = NULL) { }
if ($this->exists($type, $key1, $key2)) { }
$this->update($type, $key1, $key2, $value, $duration);
} else { public function set($type, $key1, $key2, $value, $duration = null)
$this->insert($type, $key1, $key2, $value, $duration); {
} if ($this->exists($type, $key1, $key2)) {
} $this->update($type, $key1, $key2, $value, $duration);
} else {
private function insert($type, $key1, $key2, $value, $duration = NULL) { $this->insert($type, $key1, $key2, $value, $duration);
}
$setDuration = ''; }
if (is_null($duration)) {
$expire = null; private function insert($type, $key1, $key2, $value, $duration = null)
} else { {
$expire = time() + $duration; $expire = is_null($duration) ? null : (time() + $duration);
}
$query = "INSERT INTO data (key1, key2, type, created, updated, expire, value)" .
$query = "INSERT INTO data (key1, key2, type, created, updated, expire, value)" . " VALUES(:key1, :key2, :type, :created, :updated, :expire, :value)";
" VALUES(:key1, :key2, :type, :created, :updated, :expire, :value)"; $prepared = $this->db->prepare($query);
$prepared = $this->db->prepare($query); $data = array(':key1' => $key1, ':key2' => $key2,
$data = array(':key1' => $key1, ':key2' => $key2, ':type' => $type, ':created' => time(),
':type' => $type, ':created' => time(), ':updated' => time(), ':expire' => $expire,
':updated' => time(), ':expire' => $expire, ':value' => serialize($value));
':value' => serialize($value)); $prepared->execute($data);
$prepared->execute($data); $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
$results = $prepared->fetchAll(PDO::FETCH_ASSOC); return $results;
return $results; }
}
private function update($type, $key1, $key2, $value, $duration = null)
private function update($type, $key1, $key2, $value, $duration = NULL) { {
$expire = is_null($duration) ? null : (time() + $duration);
$setDuration = '';
if (is_null($duration)) { $query = "UPDATE data SET updated = :updated, value = :value, expire = :expire WHERE key1 = :key1 AND key2 = :key2 AND type = :type";
$expire = null; $prepared = $this->db->prepare($query);
} else { $data = array(':key1' => $key1, ':key2' => $key2,
$expire = time() + $duration; ':type' => $type, ':updated' => time(),
} ':expire' => $expire, ':value' => serialize($value));
$prepared->execute($data);
$query = "UPDATE data SET updated = :updated, value = :value, expire = :expire WHERE key1 = :key1 AND key2 = :key2 AND type = :type"; $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
$prepared = $this->db->prepare($query); return $results;
$data = array(':key1' => $key1, ':key2' => $key2, }
':type' => $type, ':updated' => time(),
':expire' => $expire, ':value' => serialize($value)); public function get($type = null, $key1 = null, $key2 = null)
$prepared->execute($data); {
$results = $prepared->fetchAll(PDO::FETCH_ASSOC); $conditions = self::getCondition($type, $key1, $key2);
return $results; $query = 'SELECT * FROM data WHERE ' . $conditions;
}
$prepared = $this->db->prepare($query);
public function get($type = NULL, $key1 = NULL, $key2 = NULL) { $prepared->execute();
$conditions = self::getCondition($type, $key1, $key2); $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
$query = 'SELECT * FROM data WHERE ' . $conditions; if (count($results) !== 1) {
return null;
$prepared = $this->db->prepare($query); }
$prepared->execute();
$results = $prepared->fetchAll(PDO::FETCH_ASSOC); $res = $results[0];
if (count($results) !== 1) return NULL; $res['value'] = unserialize($res['value']);
return $res;
$res = $results[0]; }
$res['value'] = unserialize($res['value']);
return $res; /*
} * Return the value directly (not in a container)
*/
/* public function getValue($type = null, $key1 = null, $key2 = null)
* Return the value directly (not in a container) {
*/ $res = $this->get($type, $key1, $key2);
public function getValue($type = NULL, $key1 = NULL, $key2 = NULL) { if ($res === null) {
$res = $this->get($type, $key1, $key2); return null;
if ($res === NULL) return NULL; }
return $res['value']; return $res['value'];
} }
public function exists($type, $key1, $key2) { public function exists($type, $key1, $key2)
$query = 'SELECT * FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2 LIMIT 1'; {
$prepared = $this->db->prepare($query); $query = 'SELECT * FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2 LIMIT 1';
$data = array(':type' => $type, ':key1' => $key1, ':key2' => $key2); $prepared = $this->db->prepare($query);
$prepared->execute($data); $data = array(':type' => $type, ':key1' => $key1, ':key2' => $key2);
$results = $prepared->fetchAll(PDO::FETCH_ASSOC); $prepared->execute($data);
return (count($results) == 1); $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
} return (count($results) == 1);
}
public function getList($type = NULL, $key1 = NULL, $key2 = NULL) {
$conditions = self::getCondition($type, $key1, $key2); public function getList($type = null, $key1 = null, $key2 = null)
$query = 'SELECT * FROM data WHERE ' . $conditions; {
$prepared = $this->db->prepare($query); $conditions = self::getCondition($type, $key1, $key2);
$prepared->execute(); $query = 'SELECT * FROM data WHERE ' . $conditions;
$results = $prepared->fetchAll(PDO::FETCH_ASSOC); $prepared = $this->db->prepare($query);
if (count($results) == 0) return NULL; $prepared->execute();
foreach($results AS $key => $value) { $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
$results[$key]['value'] = unserialize($results[$key]['value']); if (count($results) == 0) {
} return null;
return $results; }
}
foreach ($results as $key => $value) {
public function getKeys($type = NULL, $key1 = NULL, $key2 = NULL, $whichKey = 'type') { $results[$key]['value'] = unserialize($results[$key]['value']);
if (!in_array($whichKey, array('key1', 'key2', 'type'), true)) }
throw new Exception('Invalid key type'); return $results;
}
$conditions = self::getCondition($type, $key1, $key2);
$query = 'SELECT DISTINCT :whichKey FROM data WHERE ' . $conditions; public function getKeys($type = null, $key1 = null, $key2 = null, $whichKey = 'type')
$prepared = $this->db->prepare($query); {
$data = array('whichKey' => $whichKey); if (!in_array($whichKey, array('key1', 'key2', 'type'), true)) {
$prepared->execute($data); throw new Exception('Invalid key type');
$results = $prepared->fetchAll(PDO::FETCH_ASSOC); }
if (count($results) == 0) return NULL; $conditions = self::getCondition($type, $key1, $key2);
$query = 'SELECT DISTINCT :whichKey FROM data WHERE ' . $conditions;
$resarray = array(); $prepared = $this->db->prepare($query);
foreach($results AS $key => $value) { $data = array('whichKey' => $whichKey);
$resarray[] = $value[$whichKey]; $prepared->execute($data);
} $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
return $resarray; if (count($results) == 0) {
} return null;
}
public function remove($type, $key1, $key2) { $resarray = array();
$query = 'DELETE FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2'; foreach ($results as $key => $value) {
$prepared = $this->db->prepare($query); $resarray[] = $value[$whichKey];
$data = array(':type' => $type, ':key1' => $key1, ':key2' => $key2); }
$prepared->execute($data); return $resarray;
$results = $prepared->fetchAll(PDO::FETCH_ASSOC); }
return (count($results) == 1);
} public function remove($type, $key1, $key2)
{
public function removeExpired() { $query = 'DELETE FROM data WHERE type = :type AND key1 = :key1 AND key2 = :key2';
$query = "DELETE FROM data WHERE expire NOT NULL AND expire < :expire"; $prepared = $this->db->prepare($query);
$prepared = $this->db->prepare($query); $data = array(':type' => $type, ':key1' => $key1, ':key2' => $key2);
$data = array(':expire' => time()); $prepared->execute($data);
$prepared->execute($data); $results = $prepared->fetchAll(PDO::FETCH_ASSOC);
return $prepared->rowCount(); return (count($results) == 1);
} }
/** public function removeExpired()
* Create a SQL condition statement based on parameters {
*/ $query = "DELETE FROM data WHERE expire NOT NULL AND expire < :expire";
private function getCondition($type = NULL, $key1 = NULL, $key2 = NULL) { $prepared = $this->db->prepare($query);
$conditions = array(); $data = array(':expire' => time());
if (!is_null($type)) $conditions[] = "type = " . $this->db->quote($type); $prepared->execute($data);
if (!is_null($key1)) $conditions[] = "key1 = " . $this->db->quote($key1); return $prepared->rowCount();
if (!is_null($key2)) $conditions[] = "key2 = " . $this->db->quote($key2); }
if (count($conditions) === 0) return '1'; /**
* Create a SQL condition statement based on parameters
$condition = join(' AND ', $conditions); */
private function getCondition($type = null, $key1 = null, $key2 = null)
return $condition; {
} $conditions = array();
if (!is_null($type)) {
$conditions[] = "type = " . $this->db->quote($type);
}
if (!is_null($key1)) {
$conditions[] = "key1 = " . $this->db->quote($key1);
}
if (!is_null($key2)) {
$conditions[] = "key2 = " . $this->db->quote($key2);
}
if (count($conditions) === 0) {
return '1';
}
return join(' AND ', $conditions);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment