dfstorm 3 months ago
parent
commit
07e5b80278
3 changed files with 308 additions and 1 deletions
  1. 47
    1
      README.md
  2. 1
    0
      api_keys.json.exemple
  3. 260
    0
      index.php

+ 47
- 1
README.md View File

@@ -1,3 +1,49 @@
1 1
 # small-memory
2 2
 
3
-A small database engine in php. Perfect for small ARM devices and low usage.
3
+A small database engine in php. Perfect for small ARM devices and low usage. NOT SECURE.
4
+
5
+## Usage
6
+
7
+Call the script with a GET parameter "query" (by default). The value must be in Base64. There is no "access control".
8
+
9
+### Create Database
10
+
11
+```
12
+CREATE DATABASE mydatabase
13
+```
14
+
15
+### Create Table
16
+
17
+```
18
+CREATE TABLE mytable IN mydatabase (field1,field2,[...])
19
+```
20
+
21
+### Insert
22
+
23
+```
24
+INSERT INTO mytable IN mydatabase ({"key":"value"})
25
+```
26
+
27
+### Select
28
+
29
+```
30
+SELECT mytable IN mydatabase (*)
31
+```
32
+
33
+OR
34
+
35
+```
36
+SELECT mytable IN mydatabase ({"key":"value"})
37
+```
38
+
39
+### Update
40
+
41
+```
42
+UPDATE mytable IN mydatabase ({"UnidID":"UniqID_Value","key":"new_value"})
43
+```
44
+
45
+### Delete
46
+
47
+```
48
+DELETE mytable IN mydatabase ({"UnidID":"UniqID_Value"})
49
+```

+ 1
- 0
api_keys.json.exemple View File

@@ -0,0 +1 @@
1
+{"keys":["example_A","example_B"]}

+ 260
- 0
index.php View File

@@ -0,0 +1,260 @@
1
+<?php
2
+	header('Content-Type: application/json');
3
+	class dsm {
4
+	
5
+		private	$datadir = "./data/";
6
+		public	$uid_prefix = "dsm_uid_";
7
+		public	$search_id = "uniqid";
8
+		public	$get_param = "query";
9
+		public	$iKey = 1;
10
+		public	$arrKeys = array();
11
+		public function delete($data, &$iFound)
12
+		{
13
+			$iFound = 1;
14
+			$result = array();
15
+			$result['iCode'] = 1;
16
+			if ($this->vtb($data, $r))
17
+			{
18
+				$r['json'] = json_decode($r['json'], true);
19
+				if (isset($r['json'][$this->search_id]))
20
+				{
21
+					if (strlen($r['json'][$this->search_id]) > 0 && strlen($r['database']) > 0 && strlen($r['table']) > 0)
22
+					{
23
+						$d = array_diff(scandir($this->datadir.$r['database'].'/'.$r['table'].'/'.$r['json'][$this->search_id], 1), array('.', '..'));
24
+						foreach ($d as $item => $val)
25
+						{
26
+							unlink($this->datadir.$r['database'].'/'.$r['table'].'/'.$r['json'][$this->search_id].'/'.$val);
27
+						}
28
+						if(rmdir($this->datadir.$r['database'].'/'.$r['table'].'/'.$r['json'][$this->search_id].'/'))
29
+							$result['iCode'] = 0;
30
+					}
31
+				}
32
+			}
33
+			echo json_encode($result);
34
+		}
35
+		
36
+		public function update($data, &$iFound)
37
+		{
38
+			$iFound = 1;
39
+			$result = array();
40
+			if ($this->vtb($data, $r))
41
+			{
42
+				$r['json'] = json_decode($r['json'], true);
43
+				if (!isset($r['json'][$this->search_id]))
44
+				{
45
+					$result['iCode'] = 1;
46
+					$result['message'] = "Error: no key used";
47
+				}
48
+				else
49
+				{
50
+					$m = explode(",", file_get_contents($this->datadir.$r['database'].'/'.$r['table'].'/meta'));
51
+					foreach ($r['json'] as $kk => $vv)
52
+						if (in_array($kk, $m))
53
+							file_put_contents($this->datadir.$r['database'].'/'.$r['table'].'/'.$r['json'][$this->search_id].'/'.$kk, $vv);
54
+					$result['iCode'] = 0;
55
+					$result['j'] = $r['json'];
56
+					$result['uid'] = $r['json'][$this->search_id];
57
+					
58
+				}
59
+			}
60
+			echo json_encode($result);
61
+		}
62
+		
63
+		public function select($data, &$iFound)
64
+		{
65
+			$iFound = 1;
66
+			$iMode = 0;
67
+			$itmp = 0;
68
+			if($this->vtb($data, $r))
69
+			{
70
+				$d = array_diff(scandir($this->datadir.$r['database'].'/'.$r['table'], 1), array('.', '..', 'meta'));
71
+				if ($r['json'] != '*')
72
+				{
73
+					$iMode = 1;
74
+					$r['json'] = json_decode($r['json']);
75
+				}
76
+				
77
+				$result = array();
78
+				$result['content'] = array();
79
+				foreach ($d as $key => $value)
80
+				{
81
+					$result['content'][$value] = array();
82
+					$m = explode(",", file_get_contents($this->datadir.$r['database'].'/'.$r['table'].'/meta'));
83
+					// TODO: Will create problem. fetch all then remove. Bad.
84
+					foreach ($m as $skey => $svalue)
85
+					{
86
+						$result['content'][$value][$svalue] = file_get_contents($this->datadir.$r['database'].'/'.$r['table'].'/'.$value.'/'.$svalue);
87
+						
88
+						$result['content'][$value][$svalue.'_suri'] = $this->datadir.$r['database'].'/'.$r['table'].'/'.$value.'/'.$svalue;
89
+					}
90
+					
91
+					$result['content'][$value]['uid'] = $value;
92
+					array_push($m, 'uid');
93
+					// Remove item that dont fit if mode allow it.
94
+					if ($iMode == 1)
95
+						foreach ($m as $skey => $svalue)
96
+							foreach ($r['json'] as $kk => $vv)
97
+								if ($kk == $svalue)
98
+									if ($result['content'][$value][$svalue] != $vv)
99
+										unset($result['content'][$value]);	
100
+				}
101
+				$result['iCode'] = 0;
102
+			}
103
+			else
104
+			{
105
+				$result['iCode'] = 1;
106
+			}
107
+			echo json_encode($result);
108
+		}
109
+		
110
+		public function insert($data, &$iFound)
111
+		{
112
+			$iFound = 1;
113
+			if($this->vtb($data, $r))
114
+			{
115
+				$r['json'] = json_decode($r['json'], true);
116
+				// create a random id.
117
+				$id = uniqid($this->uid_prefix,true);
118
+				// create a blank entry
119
+				mkdir($this->datadir.$r['database'].'/'.$r['table'].'/'.$id.'/', 0777, true);
120
+				// create base from meta
121
+				$m = explode(",", file_get_contents($this->datadir.$r['database'].'/'.$r['table'].'/meta'));
122
+				foreach ($m as $key => $value)
123
+				{
124
+					if (isset($r['json'][$value]))
125
+						file_put_contents($this->datadir.$r['database'].'/'.$r['table'].'/'.$id.'/'.$value, $r['json'][$value]);
126
+					else
127
+						file_put_contents($this->datadir.$r['database'].'/'.$r['table'].'/'.$id.'/'.$value, "");
128
+				}
129
+				echo json_encode(array("iCode" => 0, "uid" => $id));
130
+			}
131
+			else
132
+			{
133
+				echo json_encode(array("iCode" => 1, "message" => "Structural error."));
134
+			}
135
+			
136
+		}
137
+		// Validate if the table and database exist.
138
+		public function	vtb($data, &$r)
139
+		{
140
+			
141
+			$x = strpos($data, " IN ");
142
+			$y = strpos($data, " (");
143
+			if ($x !== false && $x > 0)
144
+				if ($this->dbexist($r['database'] = substr($data, $x + 4, $y - ($x + 4))))
145
+					if(is_writable($this->datadir))
146
+						if (strlen($r['table'] = substr($data, 0, $x)) > 0)
147
+							if (strlen($r['json'] = substr($data, $y + 2, strlen($data) - $y - 3 )) > 0)
148
+								return (1);
149
+			return (0);
150
+		}
151
+		
152
+		public function create_table($sName, &$iFound)
153
+		{
154
+			$sdbname = "";
155
+			$stbname = "";
156
+			$x = strpos($sName, " IN ");
157
+			$y = strpos($sName, " (");
158
+			if ($x !== false && $x > 0)
159
+			{
160
+				$sdbname = substr($sName, $x + 4, $y - ($x + 4));
161
+				if ($this->dbexist($sdbname))
162
+				{
163
+					if(is_writable($this->datadir))
164
+					{
165
+						$stbname = substr($sName, 0, $x);
166
+						if (mkdir($this->datadir.$sdbname.'/'.$stbname, 0777, true))
167
+						{
168
+							file_put_contents($this->datadir.$sdbname.'/'.$stbname.'/meta', substr($sName, $y + 2, strlen($sName) - $y - 3));
169
+						}
170
+						else
171
+						{
172
+							echo json_encode(array("iCode" => 1, "message" => "Table exist"));
173
+						}
174
+					}
175
+				}
176
+				else
177
+				{
178
+					// TODO: Database does not exist or error
179
+				}
180
+			}
181
+			$iFound = 1;
182
+		}
183
+		
184
+		public function create_database($sName, &$iFound)
185
+		{
186
+			$sName = str_replace(' ', '_', $sName);
187
+			$iFound = 1;
188
+			if (!$this->dbexist($sName)) {
189
+				if(is_writable($this->datadir))
190
+				{
191
+					if(mkdir($this->datadir.$sName.'/', 0777, true))
192
+						echo json_encode(array("iCode" => 0, "message" => "Database created"));
193
+					else
194
+						echo json_encode(array("iCode" => 1, "message" => "Error: Database not created"));
195
+				}
196
+				else
197
+				{
198
+					echo json_encode(array("iCode" => 1, "message" => "Error: Database not created : Data folder not writable."));
199
+				}
200
+			} else {
201
+				echo json_encode(array("iCode" => 1, "message" => "Database exist"));
202
+			}
203
+		}
204
+		
205
+		public function dbexist($sName)
206
+		{
207
+			if (strlen($sName) > 0)
208
+				if (file_exists($this->datadir.$sName.'/')) 
209
+					return (1);
210
+			return (0);
211
+		}
212
+		
213
+		public function extract_query($query)
214
+		{
215
+			$iFound = 0;
216
+			
217
+			if (($pos = strpos($query, 'CREATE DATABASE ')) === 0)
218
+				$this->create_database(substr($query, 16, strlen($query) - 16), $iFound);
219
+			if (($pos = strpos($query, 'CREATE TABLE ')) === 0)
220
+				$this->create_table(substr($query, 13, strlen($query) - 13), $iFound);
221
+			if (($pos = strpos($query, 'INSERT INTO ')) === 0)
222
+				$this->insert(substr($query, 12, strlen($query) - 12), $iFound);
223
+			if (($pos = strpos($query, 'SELECT ')) === 0)
224
+				$this->select(substr($query, 7, strlen($query) - 7), $iFound);
225
+			if (($pos = strpos($query, 'UPDATE ')) === 0)
226
+				$this->update(substr($query, 7, strlen($query) - 7), $iFound);
227
+			if (($pos = strpos($query, 'DELETE ')) === 0)
228
+				$this->delete(substr($query, 7, strlen($query) - 7), $iFound);
229
+			
230
+			 
231
+			
232
+			if(!$iFound)
233
+				echo json_encode(array("iCode" => 1, "message" => "Error while extrapolating the query."));
234
+		}
235
+	
236
+		public function init()
237
+		{
238
+		    $this->arrKeys = json_decode(file_get_contents("./api_keys.json"), true);
239
+		    if ($this->arrKeys == NULL)
240
+		    {
241
+		        echo json_encode(array("iCode" => -1, "message" => "Internal error: Error reading api_keys.json"));
242
+		        return (NULL);
243
+		    }
244
+		    $this->arrKeys = $this->arrKeys["keys"];
245
+			if(!isset($_GET['query']))
246
+				echo json_encode(array("iCode" => -1, "message" => "no query recived."));
247
+			else
248
+				if($this->iKey)
249
+				{
250
+					if(isset($_GET['key']))
251
+						if(in_array($_GET['key'], $this->arrKeys))
252
+							$this->extract_query(base64_decode($_GET[$this->get_param]));
253
+				}
254
+				else
255
+					$this->extract_query(base64_decode($_GET[$this->get_param]));
256
+		}
257
+	}
258
+	
259
+	$dsm = new dsm;
260
+	$dsm->init();

Loading…
Cancel
Save