-
Notifications
You must be signed in to change notification settings - Fork 0
/
tr.php
97 lines (90 loc) · 3.03 KB
/
tr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
class tinyRegistry{
private $registry = null;
private $dbObject;
function __construct(){
try{
$this->dbObject = new SQLite3("tinyregistry.db");
}catch(PDOException $e){
echo("Caught Exception: $e");
}
}
function open($registry){
$this->registry = $registry;
// Open the Registry specified, set the private variable, create if it does not exist
try{
$this->dbObject->exec("CREATE TABLE {$this->registry}(id INTEGER PRIMARY KEY, key TEXT, value TEXT);");
}catch(PDOException $e){
echo("Caught Exception: $e");
}
}
function registrylist(){
$list = $this->dbObject->query("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name;");
$currentResult = $list->fetchArray();
$results = null;
while($currentResult['name'] != null){
$results[] = $currentResult;
$currentResult = $list->fetchArray();
}
return $results;
}
function registrydrop($registryname){
$this->dbObject->exec("DROP TABLE $registryname;");
return true;
}
function push($key, $value, $overwrite = true){
$key = sqlite_escape_string($key);
$value = sqlite_escape_string($value);
if($this->registry == null) return false;
$results = $this->dbObject->query("SELECT * FROM {$this->registry} WHERE key='$key';");
$resultsArray = $results->fetchArray();
if($overwrite === false){ //check if there is already a value
if($resultsArray['id'] != null){
return false;
}else{
$this->dbObject->exec("INSERT INTO {$this->registry} (key, value) VALUES ('$key','$value');");
return true;
}
}else{
if($resultsArray['id'] != null){
$this->dbObject->exec("UPDATE {$this->registry} SET value='$value' WHERE key='$key';");
return true;
}else{
$this->dbObject->exec("INSERT INTO {$this->registry} (key, value) VALUES ('$key','$value');");
return true;
}
}
}
function pull($result = true, $id=null, $filter='%', $order='DESC'){
if($this->registry == null) return false;
//if id is null, use filter otherwise use provided id
//result true returns array, false, deletes items that match, string returns template
if(ctype_digit($id)){
$pull = $this->dbObject->query("SELECT * FROM {$this->registry} WHERE id='$id';");
}else{
$pull = $this->dbObject->query("SELECT * FROM {$this->registry} WHERE key LIKE '$filter' UNION SELECT * FROM {$this->registry} WHERE value LIKE '$filter' ORDER BY key,value $order;");
}
$currentResult = $pull->fetchArray();
while($currentResult['id'] != null){
$results[] = $currentResult;
$currentResult = $pull->fetchArray();
}
if($result === true){
return $results;
}elseif($result === false){
if(ctype_digit($id)){
$this->dbObject->exec("DELETE FROM {$this->registry} WHERE id=$id;"); return true;
}
}else{
$returnString = null;
foreach($results as $resultItem){
$current = str_replace('%i%', $resultItem['id'], $result);
$current = str_replace('%k%', $resultItem['key'], $current);
$current = str_replace('%v%', $resultItem['value'], $current);
$returnString.= $current;
}
return $returnString;
}
}
}
?>