20 $datadir =
$config->getPathValue(
'datadir',
'data/');
22 if (!is_dir($datadir))
23 throw new Exception(
'Data directory [' . $datadir.
'] does not exist');
24 if (!is_writable($datadir))
25 throw new Exception(
'Data directory [' . $datadir.
'] is not writable');
27 $sqllitedir = $datadir .
'sqllite/';
28 if (!is_dir($sqllitedir)) {
32 $dbfile = $sqllitedir .
$name .
'.sqllite';
34 if ($this->db =
new SQLiteDatabase($dbfile)) {
35 $q = @$this->db->query(
'SELECT key1 FROM data LIMIT 1');
37 $this->db->queryExec(
' 46 PRIMARY KEY (key1,key2,type) 51 throw new Exception(
'Error creating SQL lite database [' . $dbfile .
'].');
55 public function set(
$type, $key1, $key2, $value, $duration = NULL) {
57 $this->
update(
$type, $key1, $key2, $value, $duration);
59 $this->
insert(
$type, $key1, $key2, $value, $duration);
63 private function insert(
$type, $key1, $key2, $value, $duration = NULL) {
66 if (is_null($duration)) {
67 $setDuration =
'NULL';
69 $setDuration =
"'" . sqlite_escape_string(
time() + $duration) .
"'";
72 $query =
"INSERT INTO data (key1,key2,type,created,updated,expire,value) VALUES (" .
73 "'" . sqlite_escape_string($key1) .
"'," .
74 "'" . sqlite_escape_string($key2) .
"'," .
75 "'" . sqlite_escape_string(
$type) .
"'," .
76 "'" . sqlite_escape_string(
time()) .
"'," .
77 "'" . sqlite_escape_string(
time()) .
"'," .
79 "'" . sqlite_escape_string(serialize($value)) .
"')";
84 private function update(
$type, $key1, $key2, $value, $duration = NULL) {
87 if (is_null($duration)) {
88 $setDuration =
", expire = NULL ";
90 $setDuration =
", expire = '" . sqlite_escape_string(
time() + $duration) .
"' ";
93 $query =
"UPDATE data SET " .
94 "updated = '" . sqlite_escape_string(
time()) .
"'," .
95 "value = '" . sqlite_escape_string(serialize($value)) .
"'" .
98 "key1 = '" . sqlite_escape_string($key1) .
"' AND " .
99 "key2 = '" . sqlite_escape_string($key2) .
"' AND " .
100 "type = '" . sqlite_escape_string(
$type) .
"'";
105 public function get(
$type = NULL, $key1 = NULL, $key2 = NULL) {
107 $condition = self::getCondition(
$type, $key1, $key2);
108 $query =
"SELECT * FROM data WHERE " . $condition;
111 if (count(
$results) !== 1)
return NULL;
114 $res[
'value'] = unserialize(
$res[
'value']);
123 if (
$res === NULL)
return NULL;
124 return $res[
'value'];
128 $query =
"SELECT * FROM data WHERE " .
129 "key1 = '" . sqlite_escape_string($key1) .
"' AND " .
130 "key2 = '" . sqlite_escape_string($key2) .
"' AND " .
131 "type = '" . sqlite_escape_string(
$type) .
"' LIMIT 1";
138 $condition = self::getCondition(
$type, $key1, $key2);
139 $query =
"SELECT * FROM data WHERE " . $condition;
141 if (count(
$results) == 0)
return NULL;
149 public function getKeys(
$type = NULL, $key1 = NULL, $key2 = NULL, $whichKey =
'type') {
151 if (!in_array($whichKey,
array(
'key1',
'key2',
'type'),
true))
154 $condition = self::getCondition(
$type, $key1, $key2);
156 $query =
"SELECT DISTINCT " . $whichKey .
" FROM data WHERE " . $condition;
159 if (count(
$results) == 0)
return NULL;
163 $resarray[] = $value[$whichKey];
170 public function remove(
$type, $key1, $key2) {
171 $query =
"DELETE FROM data WHERE " .
172 "key1 = '" . sqlite_escape_string($key1) .
"' AND " .
173 "key2 = '" . sqlite_escape_string($key2) .
"' AND " .
174 "type = '" . sqlite_escape_string(
$type) .
"'";
180 $query =
"DELETE FROM data WHERE expire NOT NULL AND expire < " .
time();
181 $this->db->arrayQuery(
$query, SQLITE_ASSOC);
182 $changes = $this->db->changes();
191 $conditions =
array();
193 if (!is_null(
$type)) $conditions[] =
"type = '" . sqlite_escape_string(
$type) .
"'";
194 if (!is_null($key1)) $conditions[] =
"key1 = '" . sqlite_escape_string($key1) .
"'";
195 if (!is_null($key2)) $conditions[] =
"key2 = '" . sqlite_escape_string($key2) .
"'";
197 if (count($conditions) === 0)
return '1';
199 $condition = join(
' AND ', $conditions);
getKeys($type=NULL, $key1=NULL, $key2=NULL, $whichKey='type')
foreach($_POST as $key=> $value) $res
getValue($type=NULL, $key1=NULL, $key2=NULL)
exists($type, $key1, $key2)
Create styles array
The data for the language used.
getList($type=NULL, $key1=NULL, $key2=NULL)
update($type, $key1, $key2, $value, $duration=NULL)
static getCondition($type=NULL, $key1=NULL, $key2=NULL)
Create a SQL condition statement based on parameters.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
__construct($name, $config=NULL)
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
insert($type, $key1, $key2, $value, $duration=NULL)