Файл: FileSQL/FileSQL.php
Строк: 167
<?php
######################################
# FileSQL, система управления #
# файловыми базами данных #
# ------ #
# Версия от 22.09.2008 #
######################################
# Copyright © 2008 S.P.Y. Games #
# Разработчик: @lex #
# ICQ: 376060111 #
# E-mail: Aleksey.alex@mail.ru #
# Сайт: http://spygms.ru #
# Вы имеете право изменять скрипт #
######################################
//---{Предустановки}---//
error_reporting(0);
set_time_limit(0);
$filesql_db=array();
$filesql_options=array();
$filesql_options['error_reporting']=1; # Включение режима отображения сообщений об экстренных ошибках FileSQL
$filesql_options['returned']=0; # Возврат boolean-значения (true или false) функцией FileSQL, обозначающего успех или провал операции
function md3($pass){
return md5(md5(md5($pass)));
}
function RootSaveDirectory(){
$dir="";
while(!file_exists($dir."_root.dat")){
$dir.="../";
}
return $dir;
}
function BytesSize($size){
if ($size<1024){
$output=$size." б.";
} else if ($size<1048576){
$output=round(($size/1024),2)." Кб.";
} else if ($size<1073741824) {
$output=round((($size/1024)/1024),2)." Мб.";
} else if ($size<1099511627776) {
$output=round(((($size/1024)/1024)/1024),2)." Гб.";
} else {
$output=round((((($size/1024)/1024)/1024)/1024),2)." Тб.";
}
return $output;
}
function SaveFile($file,$data,$flag,$ch){
$fp=fopen($file, $flag);
flock($fp,LOCK_EX);
fputs($fp,$data);
fflush($fp);
flock($fp,LOCK_UN);
fclose($fp);
if($ch){
chmod($file,0777);
}
}
function FatalError($error){
global $filesql_options;
$errors=array("Неверный запрос!","Указана несуществующая команда!","Объединение двух разных типов данных!","Ошибка подключения к базе данных!","Отсутствует подключение к базе данных!");
if($filesql_options['error_reporting']){
echo "<BR><font color='#FF0000'> FileSQL error: ".$errors[$error]."</font><BR>";
}
}
function FileSQL_connect($DataBase){
global $filesql_db;
if(trim(chop($DataBase))!=""){
$db_path=RootSaveDirectory().trim(chop($DataBase));
if(!file_exists($db_path)){
$path=explode("/",$db_path);
for($i=0; $i<count($path)-1; $i++){
$real_path=array($path[0]);
for($temp=1; $temp<$i+1; $temp++){
$real_path[$temp]=$path[$temp];
}
$real_path=implode("/",$real_path);
if($path[$i]!="." and $path[$i]!=".." and !is_dir($real_path)){
mkdir($real_path,0777);
chmod($real_path,0777);
}
}
SaveFile($db_path,"","w",1);
}
$db=file($db_path);
$file_id=md5_file($db_path);
while(isset($filesql_db[$file_id])){
$file_id[rand(0,strlen($file_id)-1)]=rand(0,9);
}
$filesql_db[$file_id]["db"]=$db;
$filesql_db[$file_id]["db_path"]=$db_path;
return $file_id;
} else {
FatalError(3);
return 0;
}
}
function FileSQL($fsql,$method, $ID, $data){
global $filesql_db,$filesql_options;
if(!isset($filesql_db[$fsql])){
FatalError(4);
return 0;
}
$db=$filesql_db[$fsql]["db"];
$db_path=$filesql_db[$fsql]["db_path"];
$commands=array("DB"=>array("SIZE","SIZE_BS","CLEAR","DELETE"),"COUNT"=>array("ADD","DEL","SIZE","PASTE","REPLACE","GET","JOIN","SEARCH"));
$method=strtoupper(chop($method));
$exp="";
if(ereg(": ",$method)){
$exp=": ";
} else if (ereg(":",$method)){
$exp=":";
} else {
FatalError(0);
return 0;
}
$command=explode($exp,$method);
if(!array_search($command[1],$commands[$command[0]]) and $command[1]!=$commands[$command[0]][0]){
FatalError(1);
return 0;
}
if($command[0]=="DB"){
if($command[1]=="SIZE"){
return filesize($db_path);
} else if($command[1]=="SIZE_BS"){
return BytesSize(filesize($db_path));
} else if($command[1]=="CLEAR"){
SaveFile($db_path,"","w");
$filesql_db[$fsql]["db"]="";
if($filesql_options['returned']){
(!filesize($db_path)) ? $r=1 : $r=0;
return $r;
}
} else if($command[1]=="DELETE"){
unlink($db_path);
unset($filesql_db[$fsql]);
if($filesql_options['returned']){
(!file_exists($db_path)) ? $r=1 : $r=0;
return $r;
}
}
} else if ($command[0]=="COUNT"){
if ($command[1]=="JOIN"){
$ID=(int)$ID;
$db[$ID]=unserialize($db[$ID]);
if(is_array($db[$ID])){
if(!is_array($data)){
FatalError(2);
return 0;
}
$temp=count($db[$ID]);
$db[$ID]=serialize(array_merge($db[$ID],$data))."rn";
} else {
if(is_array($data)){
FatalError(2);
return 0;
}
$temp=strlen($db[$ID]);
$db[$ID]=serialize($db[$ID].$data)."rn";
}
SaveFile($db_path,implode("",$db),"w");
$filesql_db[$fsql]["db"]=file($db_path);
if($filesql_options['returned']){
$db=$filesql_db[$fsql]["db"];
(is_array($data)) ? ((count(unserialize($db[$ID]))>$temp) ? $r=1 : $r=0) : ((strlen(unserialize($db[$ID]))>$temp) ? $r=1 : $r=0);
return $r;
}
} else if($command[1]=="GET"){
$ID=(int)$ID;
if(!isset($db[$ID]) | $ID<0){
return 0;
}
return unserialize(trim($db[$ID]));
} else if($command[1]=="ADD"){
$temp=count($db);
if(!isset($ID)){
return 0;
}
SaveFile($db_path,serialize($ID)."rn","a+");
$filesql_db[$fsql]["db"][]=serialize($ID)."rn";
if($filesql_options['returned']){
$db=file($db_path);
(count($db)==$temp+1) ? $r=1 : $r=0;
return $r;
}
} else if($command[1]=="REPLACE"){
$ID=(int)$ID;
if(!isset($data) | $ID<0){
return 0;
}
$db[$ID]=serialize($data)."rn";
SaveFile($db_path,implode("",$db),"w");
$filesql_db[$fsql]["db"][$ID]=serialize($data)."rn";
if($filesql_options['returned']){
return 1;
}
} else if($command[1]=="PASTE"){
$temp=count($db);
$ID=(int)$ID;
if(!isset($data) | $ID<0){
return 0;
}
$z=$ID;
while(!isset($db[$z-1]) and $z>0){
$db[$z-1]="rn";
$z--;
}
if(!isset($db[$ID])){
$db[$ID]=serialize($data)."rn";
} else {
$db[$ID]=serialize($data)."rn".$db[$ID];
}
SaveFile($db_path,implode("",$db),"w");
$filesql_db[$fsql]["db"]=file($db_path);
if($filesql_options['returned']){
$db=$filesql_db[$fsql]["db"];
($ID>$temp) ? ((count($db)==$ID+1) ? $r=1 : $r=0) : ((count($db)==$temp+1) ? $r=1 : $r=0);
return $r;
}
} else if ($command[1]=="DEL"){
$temp=count($db);
$ID=(int)$ID;
if(!isset($db[$ID])){
return 0;
}
$db[$ID]="";
SaveFile($db_path,implode("",$db),"w");
unset($filesql_db[$fsql]["db"][$ID]);
if($filesql_options['returned']){
$db=file($db_path);
(count($db)==$temp-1) ? $r=1 : $r=0;
return $r;
}
} else if($command[1]=="SIZE"){
return count($db);
} else if($command[1]=="SEARCH"){
function ExtractArray($array){
global $filesql_options;
$count=count($array);
for($i=0; $i<$count; $i++){
if(is_array($array[$i])){
$array[$i]=ExtractArray($array[$i]);
}
}
return implode($filesql_options['search_separator'],$array);
}
$data=(int)$data;
$special_symbols='[]{}()\$^.?-+*|';
$ID="$ID";
$filesql_options['search_separator']=chr(rand(1,255));
while(eregi("\".$filesql_options['search_separator'],$ID)){
$filesql_options['search_separator']=chr(rand(1,255));
}
for($i=0; $i<strlen($special_symbols);$i++){
$ID=str_replace($special_symbols[$i],"\".$special_symbols[$i],$ID);
}
$count=count($db);
if($count>0 and $data<$count and $data>=0 and strlen($ID)>0){
for($i=$data; $i<$count; $i++){
$for_search=unserialize($db[$i]);
if(is_array($for_search)){
$for_search=ExtractArray($for_search);
}
if(ereg($ID,$for_search)){
return $i;
}
}
return -1;
} else {
return -1;
}
}
}
}
?>