All pastes #519925 Raw Edit

Untitled

public text v1 · immutable
#519925 ·published 2007-05-29 20:03 UTC
rendered paste body
<?php
/*
Version: 2.5 Final

Copyright 2007 Peter Caprioli

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.

Fixed in this version:

* Booleans are now saved as booleans and not as an int
* Supports 6 types: array, string, boolean, int, float, null
* Supports empty arrays
* Rewritten core functions
* Faster, takes less space
* Fixed FLOCK regardless if database is read or written to

This code finally seems to be stable enough. It is currently handling 26000+
users over at Aprelium and is faster than MySQL.

Benchmark:

Change: 3273 accesses/sec
Read: 5469 accesses/sec
Write: 3591 accesses/sec

*/

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)) {
		return false;
	}
	return true;
}

function fb_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_swap, no keys given.</b>"); }
	if (!isset($array[$swap1]) || !isset($array[$swap2])) { die("<b>FlatBase Error: fb_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))."!A";
		} elseif (is_int($value)) {
			$__return .= " ".base64_encode($key)."!".base64_encode($value)."!I";
		} elseif (is_float($value)) {
			$__return .= " ".base64_encode($key)."!".base64_encode($value)."!F";
		} elseif (is_string($value)) {
			$__return .= " ".base64_encode($key)."!".base64_encode($value)."!S";
		} elseif (is_null($value)) {
			$__return .= " ".base64_encode($key)."!!N";
		} elseif (is_bool($value)) {
			if ($value) {
				$__return .= " ".base64_encode($key)."!T!B";
			} else {
				$__return .= " ".base64_encode($key)."!F!B";
			}
		} else {
			die("<b>FlatBase Error: Unsupported datatype</b>");
		}
	}
	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] == "A") {
			if (empty($data[1])) {
				$return[$data[0]] = array();
			} else {
				$return[$data[0]] = fb_s2a($data[1]);
			}
		} elseif ($data[2] == "I") {
			$return[$data[0]] = (int)$data[1];
		} elseif ($data[2] == "F") {
			$return[$data[0]] = (float)$data[1];
		} elseif ($data[2] == "S") {
			$return[$data[0]] = (string)$data[1];
		} elseif ($data[2] == "B") {
			if ($data[1] == "T") {
				$return[$data[0]] = true;
			} elseif ($data[1] == "F") {
				$return[$data[0]] = false;
			} else {
				die("<b>FlatBase Error: Flagged as boolean but incorrect value</b>");
			}
		} elseif ($data[2] == "N") {
			$return[$data[0]] = NULL;
		} else {
			die("<b>FlatBase Error: Unsupported type: ".htmlentities($data[2])."</b>");
		}
	}
	return $return;
}

function fb_open($__database_name) {
	$__contents = fb_open_raw($__database_name);
	return $__contents['data'];
}

function fb_save($__database_name, $__contents) {
	fb_save_raw($__database_name, array("data" => $__contents, "changed" => time()));
	return true;
}

function fb_name($__value_db) {
	return sha1($__value_db).md5($__value_db).".secure.fb";
}

function fb_open_raw($__value_db) {
	if (file_exists(sha1($__value_db).md5($__value_db).".secure.fb")) {
		
		$__handle = fopen(sha1($__value_db).md5($__value_db).".secure.fb", "r");
		if (!flock($__handle, LOCK_EX)) { die("<b>FlatBase Error: Unable to get exclusive lock on database ".$__value_db."</b>"); }
		$__content = file_get_contents(sha1($__value_db).md5($__value_db).".secure.fb");
		fclose($__handle);
		
		if ($__content == "@NUL") {
			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_save_raw($__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.fb", "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 = "@NUL";
	}
	fwrite($__database_handle, $__this_save);
	fclose($__database_handle);
	if (file_exists(sha1($__value_db).md5($__value_db).".secure.fb")) {
		return true;
	} else {
		die("<b>FlatBase Error: Unable to change database array ".$__value_db."</b>");
	}
}

function fb_exists($__value_db) {
	if (file_exists(sha1($__value_db).md5($__value_db).".secure.fb")) {
		return true;
	} else {
		return false;
	}
}

function fb_drop($__value_db) {
	if (file_exists(sha1($__value_db).md5($__value_db).".secure.fb")) {
		unlink(sha1($__value_db).md5($__value_db).".secure.fb");
		if (!file_exists(sha1($__value_db).md5($__value_db).".secure.fb")) {
			return true;
		} else {
			$errors = 0;
			while(file_exists(sha1($__value_db).md5($__value_db).".secure.fb")) {
				unlink(sha1($__value_db).md5($__value_db).".secure.fb");
				if (!file_exists(sha1($__value_db).md5($__value_db).".secure.fb")) {
					return true;
				} else {
					$errors++;
					sleep(1);
					if ($errors == 10) { die("<b>FlatBase Error: Unable to drop database ".$__value_db."</b>"); }
				}
			}
		}
	} else {
		return false;
	}
}
?>