workOnFood
Новичок
Всем доброго дня. Пишу систему сообщений которая будет работать с использованием аякса и предполагается что она будет довольно сильно нагружена поэтому нужно чтобы все работало максимально быстро. Ввиду этих обстоятельств решил вместо использования mySql написать свое подобие базы с функционалом заточенным исключительно под нужды приложения на файлах. Хотелось бы узнать ваше мнение по поводу структуры, а также по поводу самого кода. Заранее извиняюсь если увидите откровенный говнокод, я пока еще только учусь. Кода не много поэтому выкладываю сюда, а также архивчик со структурой базы.
PHP:
class Filebase{
protected $dirOfBase;
protected $basedir;
function __construct($path_to_filebase){
$this->dirOfBase = $path_to_filebase;
}
function sys_getUserArr($id){
return $arr = array('Vasia','img/avatar.png');
}
function sys_addUserIfNotEx($id,$gr_id){
if(file_exists($this->dirOfBase.'users/'.$id.'.fb')){
$fp = fopen($this->dirOfBase.'users/'.$id.'.fb','a');
fputs($fp,$new_group."\t");
fclose($fp);
}else{
$user = $this->sys_getUserArr($id);
file_put_contents($this->dirOfBase.'users/'.$id.'.fb',$id."\t".$user[0]."\t".$user[1]."\t".$gr_id."\t");
}
}
function startGroup($my_id,$other_id,$my_mes,$title){
$my_mes = preg_replace("/\n/s",'<br/>',htmlspecialchars($my_mes));
$title = preg_replace("/\n/s",' ',htmlspecialchars($title));
$gr_id = file_get_contents($this->dirOfBase.'groups/maxgr.fb')+1;
file_put_contents($this->dirOfBase.'groups/maxgr.fb',$gr_id);
$new_group = $my_id.'|1 '.$other_id.'|0 ';
foreach(array($my_id,$other_id) as $id){
$this->sys_addUserIfNotEx($id,$gr_id);
}
mkdir($this->dirOfBase.'groups/'.$gr_id);
$fp = fopen($this->dirOfBase.'groups/'.$gr_id.'/gr.fb','a');
fseek($fp,filesize($this->dirOfBase.'groups/'.$gr_id.'/gr.fb'));
$pos = ftell($fp);
fputs($fp,$my_mes."\n");
fclose($fp);
$fp = fopen($this->dirOfBase.'groups/'.$gr_id.'/m_index.fb','a');
fputs($fp,$my_id."\t".time()."\t".$pos."\n");
fclose($fp);
file_put_contents($this->dirOfBase.'groups/'.$gr_id.'/users.fb',$new_group);
file_put_contents($this->dirOfBase.'groups/'.$gr_id.'/title.fb',$title);
}
function inviteUser($id,$gr){
$this->sys_addUserIfNotEx($id,$gr);
$fp = fopen($this->dirOfBase.'groups/'.$gr.'/users.fb','a');
fputs($fp,$id.'|0 ');
fclose($fp);
}
function writeMessage($id,$gr,$mes){
$mes = preg_replace("/\n/s",'<br/>',htmlspecialchars($mes));
$fp = fopen($this->dirOfBase.'groups/'.$gr.'/gr.fb','a');
fseek($fp,filesize($this->dirOfBase.'groups/'.$gr.'/gr.fb'));
$pos = ftell($fp);
fputs($fp,$mes."\n");
fclose($fp);
$fp = fopen($this->dirOfBase.'groups/'.$gr.'/m_index.fb','a');
fputs($fp,$id."\t".time()."\t".$pos."\n");
fclose($fp);
}
function getDialogArr($gr,$from=0,$howmany=0,$rev=true){
$index_arr = file($this->dirOfBase.'groups/'.$gr.'/m_index.fb');
if($from>0){
if($howmany>0){
$index_arr = array_slice($index_arr,($rev?0-$from:$from),$howmany);
}else{
$index_arr = array_slice($index_arr,($rev?0-$from:$from));
}
}
foreach($index_arr as $mes_x){
$mes_x = explode("\t",$mes_x);
$fp =fopen($this->dirOfBase.'groups/'.$gr.'/gr.fb','r');
fseek($fp,$mes_x[2]);
$mes = fgets($fp);
fclose($fp);
if(date("j",$mes_x[1])<date("j"))$df = "d.m.Y H:i";
else $df = "H:i";
$user = file_get_contents($this->dirOfBase.'users/'.$mes_x[0].'.fb');
$user = explode("\t",$user);
$disp_arr[] = array($user[1],$user[2],date($df,$mes_x[1]),$mes);
}
return $disp_arr;
}
function groupDelUser($id,$gr){
$gr_users = file_get_contents($this->dirOfBase.'groups/'.$gr.'/users.fb');
$gr_users = explode(" ",$gr_users);
foreach($gr_users as $user){
$u_arr = explode('|',$user);
if($u_arr[0]!=$id && $u_arr[0]!=''){
$allowed.=$user.' ';
}
}
file_put_contents($this->dirOfBase.'groups/'.$gr.'/users.fb',$allowed);
$user = file_get_contents($this->dirOfBase.'users/'.$id.'.fb');
$user = explode("\t",$user);
$allowed = $user[0]."\t".$user[1]."\t".$user[2]."\t";
for($i=3;$i<count($user);$i++){
if($user[$i]!=$gr && $user[$i]!=''){
$allowed.=$user[$i]."\t";
}
}
file_put_contents($this->dirOfBase.'users/'.$id.'.fb',$allowed);
}
function renameGroup($gr,$title){
$title = preg_replace("/\n/s",' ',htmlspecialchars($title));
file_put_contents($this->dirOfBase.'groups/'.$gr.'/title.fb',$title);
}
}
Вложения
-
1,5 KB Просмотры: 7