-
Notifications
You must be signed in to change notification settings - Fork 0
/
varDx.php
executable file
·98 lines (84 loc) · 2 KB
/
varDx.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
98
<?php
namespace varDX;
/*
varDX - PHP flat-file storage
by @rahuldottech
v1.4
--
https://github.com/rahuldottech/
https://rahul.tech/
*/
class cDX {
private $dataFile;
public function def($filename){
$this->dataFile = $filename;
}
public function write($varName, $varVal){
if(file_exists($this->dataFile)){
$foundLine = $this->check($varName);
} else {
$foundLine = false;
}
if(!$foundLine){
$writeData = $varName.'__=__'.urlencode(serialize($varVal)).PHP_EOL;
file_put_contents($this->dataFile, $writeData, FILE_APPEND);
} else {
return "ERR_DX_KEY_ALREADY_EXISTS";
}
}
public function read($varName){
if(file_exists($this->dataFile)){
foreach(file($this->dataFile) as $line) {
if(strpos($line, $varName) !== false) {
list(, $new_str) = explode("__=__", $line);
$foundLine = true;
}
}
if($foundLine){
$val = rtrim($new_str);
return unserialize(urldecode($val));
} else {
return "ERR_DX_KEY_NOT_FOUND";
}
} else {
return "ERR_DX_FILE_DOES_NOT_EXIST";
}
}
public function del($varName){
if(file_exists($this->dataFile)){
$f = $this->dataFile;
$term = $varName.'__=__';
$arr = file($f);
foreach ($arr as $key=> $line) {
if(stristr($line,$term)!== false){unset($arr[$key]);break;}
}
//reindexing array
$arr = array_values($arr);
//writing to file
file_put_contents($f, implode($arr));
} else {
return "ERR_DX_FILE_DOES_NOT_EXIST";
}
}
public function modify($varName, $varVal){
if(file_exists($this->dataFile)){
if($this->check($varName)){
$this->del($varName);
}
}
$writeData = $varName.'__=__'.urlencode(serialize($varVal)).PHP_EOL;
file_put_contents($this->dataFile, $writeData, FILE_APPEND);
}
public function check($varName){
if(file_exists($this->dataFile)){
foreach(file($this->dataFile) as $line) {
if(stripos($line, $varName.'__=__') === 0){
return true;
}
}
return false;
} else {
return "ERR_DX_FILE_DOES_NOT_EXIST";
}
}
}