I've rewritten most of the FlatBase code in the 2.0 version. The new version is actually pretty stable.
Download is avaliable from
Pastebin (slow as usual, just give it a minute to load).
Fixes since 1.0:
* Empty arrays are now supported
* Rewritten all stolen code from scratch (Mainly because the old code was unstable)
* Supports collisions
* Works without error_reporting(0)
* Added a few functions
* Fixed about 15 bugs
Edit
Uploaded a simple proof-of-concept forum which is using Flatbase.
Download
<?php
/*
Copyright 2007 Kisom
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Contact: h at remove ck3r dotorg
*/
function fb_test() {
$__random_name = rand();
while (file_exists($__random_name)) {
$__random_name = rand().time();
}
$handle = fopen($__random_name, "w");
if (!$handle) { return false; }
$__data_value = rand();
fwrite($handle, $__data_value);
fclose($handle);
if (file_get_contents($__random_name) != $__data_value) { unlink($__random_name); return false; }
unlink($__random_name);
if (file_exists($__random_name)) {
return false;
}
return true;
}
function fb_static($__data, $__array) {
if (!in_array($__data, $__array)) {
die("<b>FlatBase Error: fb_static, value <u>".htmlentities($__data)."</u> cannot be used.</b>");
}
}
function fb_array_swap($array, $swap1, $swap2) {
if (!is_array($array)) { die("<b>FlatBase Error: fb_array_swap, invalid type given (expected array).</b>"); }
if (!isset($swap1) || !isset($swap2)) { die("<b>FlatBase Error: fb_array_swap, no keys given.</b>"); }
if (!isset($array[$swap1]) || !isset($array[$swap2])) { die("<b>FlatBase Error: fb_array_swap, keys do not exist.</b>"); }
$__temp = $array[$swap1];
$array[$swap1] = $array[$swap2];
$array[$swap2] = $__temp;
return $array;
}
function fb_a2s($__array) {
if (!isset($__array)) { die("<b>FlatBase Error: No data given in fb_a2s</b>"); }
if (!is_array($__array)) { die("<b>FlatBase Error: Wrong data given in fb_a2s, expected array</b>"); }
$__return = "";
foreach($__array as $key => $value) {
if (is_array($value)) {
$__return .= " ".base64_encode($key)."!".base64_encode(fb_a2s($value))."!ARR";
} else {
$__return .= " ".base64_encode($key)."!".base64_encode($value)."!REG";
}
}
return trim($__return);
}
function fb_s2a($__string) {
if (!isset($__string)) { die("<b>FlatBase Error: No data given in fb_s2a</b>"); }
if (!is_string($__string)) { die("<b>FlatBase Error: Wrong data given in fb_s2a, expected string</b>"); }
$__string = explode(" ", $__string);
foreach ($__string as $void => $data) {
$data = explode("!", $data);
if (isset($data[0]) && isset($data[1])) {
$data[1] = base64_decode($data[1]);
$data[0] = base64_decode($data[0]);
}
if ($data[2] == "ARR") {
if (empty($data[1])) {
$return[$data[0]] = array();
} else {
$return[$data[0]] = fb_s2a($data[1]);
}
} elseif ($data[2] == "REG") {
$return[$data[0]] = $data[1];
}
}
return $return;
}
function fb_read($__value_db) {
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
$__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
$__errors = 0;
while (empty($__content)) {
if ($__errors == 100) { die("<b>FlatBase Error: Access error in database ".$__value_db."</b>"); }
$__errors++;
$__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
usleep(100000);
}
if ($__content == "@EMPTY") {
return("");
} else {
return base64_decode($__content);
}
} else {
die("<b>FlatBase Error: Unable to read database ".$__value_db."</b>");
}
}
function fb_name($__value_db) {
return sha1($__value_db).md5($__value_db).".secure.fba";
}
function fb_array_read($__value_db) {
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
$__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
$__errors = 0;
while (empty($__content)) {
if ($__errors == 100) { die("<b>FlatBase Error: Access error in database ".$__value_db."</b>"); }
$__errors++;
$__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
usleep(100000);
}
if ($__content == "@EMPTY_ARRAY") {
return array();
} else {
$__return = fb_s2a($__content);
if (is_array($__return)) {
return $__return;
} else {
die("<b>FlatBase Error: Database ".$__value_db." is corrupt</b>");
}
}
} else {
die("<b>FlatBase Error: Unable to read database ".$__value_db."</b>");
}
}
function fb_boolean_read($__value_db) {
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
$__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
$__errors = 0;
while (empty($__content)) {
if ($__errors == 100) { die("<b>FlatBase Error: Access error in database ".$__value_db."</b>"); }
$__errors++;
$__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fba");
usleep(100000);
}
if ($__content == 1) {
return true;
} elseif ($__content == 2) {
return false;
} else {
die("<b>FlatBase Error: database ".$__value_db." is corrupt</b>");
}
} else {
die("<b>FlatBase Error: Unable to read boolean from ".$__value_db."</b>");
}
}
function fb_exists($__value_db) {
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
return true;
} else {
return false;
}
}
function fb_drop($__value_db) {
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
unlink(sha1($__value_db).md5($__value_db).".secure.fba");
if (!file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
return true;
} else {
$errors = 0;
while(file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
unlink(sha1($__value_db).md5($__value_db).".secure.fba");
if (!file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
return true;
} else {
$errors++;
sleep(1);
if ($errors == 10) { die("<b>FlatBase Error: Unable to drop database ".$__value_db."</b>"); }
}
}
}
} else {
return false;
}
}
function fb_write($__value_db, $__value_content) {
$__database_handle = fopen(sha1($__value_db).md5($__value_db).".secure.fba", "w");
if (!$__database_handle) { die("<b>FlatBase Error: Access denied to ".$__value_db."</b>"); }
if (!flock($__database_handle, LOCK_EX)) { die("<b>FlatBase Error: Unable to get exclusive lock on database ".$__value_db."</b>"); }
if (empty($__value_content)) {
$__value_content = "@EMPTY";
} else {
$__value_content = base64_encode($__value_content);
}
fwrite($__database_handle, $__value_content);
fclose($__database_handle);
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
return true;
} else {
die("<b>FlatBase Error: Unable to change database ".$__value_db."</b>");
}
}
function fb_array_write($__value_db, $__value_content) {
if (!is_array($__value_content)) { die("<b>FlatBase Error: Wrong datatype in ".$__value_db.". Expected array.</b>"); }
$__database_handle = fopen(sha1($__value_db).md5($__value_db).".secure.fba", "w");
if (!$__database_handle) { die("<b>FlatBase Error: Access denied to ".$__value_db."</b>"); }
if (!flock($__database_handle, LOCK_EX)) { die("<b>FlatBase Error: Unable to get exclusive lock on database ".$__value_db."</b>"); }
$__this_save = fb_a2s($__value_content);
if (empty($__this_save)) {
$__this_save = "@EMPTY_ARRAY";
}
fwrite($__database_handle, $__this_save);
fclose($__database_handle);
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
return true;
} else {
die("<b>FlatBase Error: Unable to change database array ".$__value_db."</b>");
}
}
function fb_boolean_write($__value_db, $__value_content) {
if (!is_bool($__value_content)) { die("<b>FlatBase Error: Wrong datatype in ".$__value_db.". Expected boolean.</b>"); }
$__database_handle = fopen(sha1($__value_db).md5($__value_db).".secure.fba", "w");
if (!$__database_handle) { die("<b>FlatBase Error: Access denied to ".$__value_db."</b>"); }
if (!flock($__database_handle, LOCK_EX)) { die("<b>FlatBase Error: Unable to get exclusive lock on database ".$__value_db."</b>"); }
if ($__value_content) {
$__value_content = 1;
} else {
$__value_content = 2;
}
fwrite($__database_handle, $__value_content);
fclose($__database_handle);
if (file_exists(sha1($__value_db).md5($__value_db).".secure.fba")) {
return true;
} else {
die("<b>FlatBase Error: Unable to change database boolean ".$__value_db."</b>");
}
}
?>