1. <?php
  2. /*
  3. Copyright 2007 Kisom
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22.  
  23. Contact: h@ck3r.org
  24.  
  25. */
  26.  
  27. function fb_test() {
  28. $__random_name = rand();
  29. while (file_exists($__random_name)) {
  30. $__random_name = rand().time();
  31. }
  32. $handle = fopen($__random_name, "w");
  33. $__data_value = rand();
  34. fwrite($handle, $__data_value);
  35. fclose($handle);
  36. if (file_get_contents($__random_name) != $__data_value) { unlink($__random_name); return false; }
  37. unlink($__random_name);
  38. if (file_exists($__random_name)) {
  39. return false;
  40. }
  41. return true;
  42. }
  43.  
  44. function fb_array_swap($array, $swap1, $swap2) {
  45. if (!is_array($array)) { die("<b>FlatBase Error: fb_array_swap, invalid type given (expected array).</b>"); }
  46. if (!isset($swap1) || !isset($swap2)) { die("<b>FlatBase Error: fb_array_swap, no keys given.</b>"); }
  47. if (!isset($array[$swap1]) || !isset($array[$swap2])) { die("<b>FlatBase Error: fb_array_swap, keys do not exist.</b>"); }
  48. $__temp = $array[$swap1];
  49. $array[$swap1] = $array[$swap2];
  50. $array[$swap2] = $__temp;
  51. return $array;
  52. }
  53.  
  54. function fb_a2s($Array) {
  55. $__return_value = '';
  56. $__null_value = "^^^";
  57. foreach ($Array as $Key => $__current_value) {
  58. if (is_array($__current_value)) {
  59. $__return_value='^^array^'.fb_a2s($__current_value);
  60. } else {
  61. $__return_value = (strlen($__current_value)>0)?$__current_value:$__null_value;
  62. }
  63. $__return_value .= urlencode(base64_encode($Key)) . '|' . urlencode(base64_encode($__return_value)).'||';
  64. }
  65. return urlencode(substr($__return_value,0,-2));
  66. }
  67.  
  68. function fb_s2a($__string_value) {
  69. $__return_value = array();
  70. $__string_value = urldecode($__string_value);
  71. $TempArray = explode('||',$__string_value);
  72. $__null_value = urlencode(base64_encode("^^^"));
  73. foreach ($TempArray as $__tmp_value) {
  74. list($Key, $__current_value) = explode('|', $__tmp_value);
  75. $DecodedKey = base64_decode(urldecode($Key));
  76. if ($__current_value != $__null_value) {
  77. $__return_value = base64_decode(urldecode($__current_value));
  78. if(substr($__return_value,0,8) == '^^array^') {
  79. $__return_value=fb_s2a(substr($__return_value,8));
  80. }
  81. $__return_value[$DecodedKey] = $__return_value;
  82. }
  83. else
  84. $__return_value[$DecodedKey] = NULL;
  85. }
  86. return $__return_value;
  87. }
  88.  
  89. function fb_read($__value_db) {
  90. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  91. $__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
  92. $__errors = 0;
  93. while (empty($__content)) {
  94. if ($__errors == 100) { die("<b>FlatBase Error: Access error in database ".$__value_db."</b>"); }
  95. $__errors++;
  96. $__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
  97. usleep(100000);
  98. }
  99. if ($__content == "@EMPTY") {
  100. return("");
  101. } else {
  102. return base64_decode($__content);
  103. }
  104. } else {
  105. die("<b>FlatBase Error: Unable to read database ".$__value_db."</b>");
  106. }
  107. }
  108.  
  109. function fb_name($__value_db) {
  110. return sha1($__value_db).md5($__value_db).".secure.fba";
  111. }
  112.  
  113. function fb_array_read($__value_db) {
  114. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  115. $__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
  116. $__errors = 0;
  117. while (empty($__content)) {
  118. if ($__errors == 100) { die("<b>FlatBase Error: Access error in database ".$__value_db."</b>"); }
  119. $__errors++;
  120. $__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
  121. usleep(100000);
  122. }
  123. if ($__content == "@EMPTY_ARRAY") {
  124. return array();
  125. } else {
  126. $__return = fb_s2a(base64_decode($__content));
  127. if (is_array($__return)) {
  128. return $__return;
  129. } else {
  130. die("<b>FlatBase Error: Database ".$__value_db." is corrupt</b>");
  131. }
  132. }
  133. } else {
  134. die("<b>FlatBase Error: Unable to read database ".$__value_db."</b>");
  135. }
  136. }
  137.  
  138. function fb_boolean_read($__value_db) {
  139. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  140. $__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
  141. $__errors = 0;
  142. while (empty($__content)) {
  143. if ($__errors == 100) { die("<b>FlatBase Error: Access error in database ".$__value_db."</b>"); }
  144. $__errors++;
  145. $__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
  146. usleep(100000);
  147. }
  148. if ($__content == 1) {
  149. return true;
  150. } elseif ($__content == 2) {
  151. return false;
  152. } else {
  153. die("<b>FlatBase Error: database ".$__value_db." is corrupt</b>");
  154. }
  155. } else {
  156. die("<b>FlatBase Error: Unable to read boolean from ".$__value_db."</b>");
  157. }
  158. }
  159.  
  160. function fb_exists($__value_db) {
  161. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167.  
  168. function fb_drop($__value_db) {
  169. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  170. unlink(sha1($__value_db).md5($__value_db).".secure.fba");
  171. if (!file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  172. return true;
  173. } else {
  174. $errors = 0;
  175. while(file_exists(sha1($__value_db).md5($__value_db).".secure.fba") && $errors != 100) {
  176. unlink(sha1($__value_db).md5($__value_db).".secure.fba");
  177. if (!file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  178. return true;
  179. } else {
  180. $errors++;
  181. sleep(1);
  182. if ($errors == 10) { die("<b>FlatBase Error: Unable to drop database ".$__value_db."</b>"); }
  183. }
  184. }
  185. }
  186. } else {
  187. return false;
  188. }
  189. }
  190.  
  191. function fb_write($__value_db, $__value_content) {
  192. $__database_handle = fopen(sha1($__value_db).md5($__value_db).".secure.fba", "w");
  193. if (!flock($__database_handle, LOCK_EX)) { die("<b>FlatBase Error: Unable to get exclusive lock on database ".$__value_db."</b>"); }
  194. if (empty($__value_content)) {
  195. $__value_content = "@EMPTY";
  196. } else {
  197. $__value_content = base64_encode($__value_content);
  198. }
  199. fwrite($__database_handle, $__value_content);
  200. fclose($__database_handle);
  201. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  202. return true;
  203. } else {
  204. die("<b>FlatBase Error: Unable to change database ".$__value_db."</b>");
  205. }
  206. }
  207.  
  208. function fb_array_write($__value_db, $__value_content) {
  209. if (!is_array($__value_content)) { die("<b>FlatBase Error: Wrong datatype in ".$__value_db.". Expected array.</b>"); }
  210. $__database_handle = fopen(sha1($__value_db).md5($__value_db).".secure.fba", "w");
  211. if (!flock($__database_handle, LOCK_EX)) { die("<b>FlatBase Error: Unable to get exclusive lock on database ".$__value_db."</b>"); }
  212. $__this_save = base64_encode(fb_a2s($__value_content));
  213. if (empty($__this_save)) {
  214. $__this_save = "@EMPTY_ARRAY";
  215. }
  216. fwrite($__database_handle, $__this_save);
  217. fclose($__database_handle);
  218. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  219. return true;
  220. } else {
  221. die("<b>FlatBase Error: Unable to change database array ".$__value_db."</b>");
  222. }
  223. }
  224.  
  225. function fb_boolean_write($__value_db, $__value_content) {
  226. if (!is_bool($__value_content)) { die("<b>FlatBase Error: Wrong datatype in ".$__value_db.". Expected boolean.</b>"); }
  227. $__database_handle = fopen(sha1($__value_db).md5($__value_db).".secure.fba", "w");
  228. if (!flock($__database_handle, LOCK_EX)) { die("<b>FlatBase Error: Unable to get exclusive lock on database ".$__value_db."</b>"); }
  229. if ($__value_content) {
  230. $__value_content = 1;
  231. } else {
  232. $__value_content = 2;
  233. }
  234. fwrite($__database_handle, $__value_content);
  235. fclose($__database_handle);
  236. if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
  237. return true;
  238. } else {
  239. die("<b>FlatBase Error: Unable to change database boolean ".$__value_db."</b>");
  240. }
  241. }
  242. ?>