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_addUserOrGr($id,$gr_id){
if(file_exists($this->dirOfBase.'users/'.$id.'.fb')){
$fp = fopen($this->dirOfBase.'users/'.$id.'.fb','a');
flock($fp,LOCK_EX);
fputs($fp,$gr_id."\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));
$fp = fopen($this->dirOfBase.'groups/maxgr.fb','r+');
flock($fp,LOCK_EX);
$gr_id = fgets($fp);
fseek($fp,0);
fputs($fp,++$gr_id);
fclose($fp);
$new_group = $my_id.'|1 '.$other_id.'|0 ';
foreach(array($my_id,$other_id) as $id){
$this->sys_addUserOrGr($id,$gr_id);
}
mkdir($this->dirOfBase.'groups/'.$gr_id);
$fp = fopen($this->dirOfBase.'groups/'.$gr_id.'/gr.fb','a');
$pos = filesize($this->dirOfBase.'groups/'.$gr_id.'/gr.fb');
fputs($fp,$my_mes."\n");
fclose($fp);
$fp = fopen($this->dirOfBase.'groups/'.$gr_id.'/m_index.bin','wb');
$data_bin = pack('N3',$my_id,time(),$pos);
fputs($fp,$data_bin);
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_addUserOrGr($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');
flock($fp,LOCK_EX);
$pos = filesize($this->dirOfBase.'groups/'.$gr.'/gr.fb');
fputs($fp,$mes."\n");
fclose($fp);
$data_bin = pack('N3',$id,time(),$pos);
$fp = fopen($this->dirOfBase.'groups/'.$gr.'/m_index.bin','ab');
flock($fp,LOCK_EX);
fputs($fp,$data_bin);
fclose($fp);
}
function getDialogArr($gr,$from=0,$howmany=0){
$all = 0-filesize($this->dirOfBase.'groups/'.$gr.'/m_index.bin');
$fp = fopen($this->dirOfBase.'groups/'.$gr.'/m_index.bin','rb');
flock($fp,LOCK_EX);
$from = $from=='all'?$all:$from = 0 - $from*12;
$howmany = 0 - $howmany*12;
for($i=$from;$i<$howmany;$i+=12){
fseek($fp,$i,SEEK_END);
$index_arr[] = unpack('N3',fread($fp,12));
}
fclose($fp);
$fp =fopen($this->dirOfBase.'groups/'.$gr.'/gr.fb','r');
foreach($index_arr as $mes_x){
fseek($fp,$mes_x[3]);
$mes = fgets($fp);
if(time()-$mes_x[2]>86400)$df = "d.m.Y H:i";
else $df = "H:i";
preg_match("/^.*?\t(.*?)\t(.*?)\t/i",file_get_contents($this->dirOfBase.'users/'.$mes_x[1].'.fb'),$user);
$disp_arr[] = array($user[1],$user[2],date($df,$mes_x[2]),$mes);
}
fclose($fp);
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);
}
}