A small database engine in php. Perfect for small ARM devices and low usage.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 7.8KB

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