Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $st=time();
- function tru($up){
- if(isset($_GET["up"])){
- $rv=$_GET["up"];
- if(strtolower($rv)=="false") $rv=!1;
- else if(preg_match("/[^0-9]/",$rv)) $rv=!0;
- else $rv=!!($rv*1);
- }else{ $rv=!1; }
- return $rv;
- }
- if(file_exists("msgs_.txt")){
- $msgsdat=json_decode(file_get_contents("msgs_.txt"),!0);
- }else{
- $msgsdat=["claa"=>[],"chats"=>[]];
- //claa = clients array
- }
- //client id set
- if(isset($_COOKIE["csid"])){
- $csid=$_COOKIE["csid"];
- }else{
- if(isset($_GET["csid"]))$csid=$_GET["csid"];
- else{
- $claa=$msgsdat["claa"];
- do{ $csid=bin2hex(random_bytes(10)); }while(isset($claa[$csid]));
- $msgsdat["claa"][$csid]=["Lreg"=>$st];
- }
- setcookie("csid",$csid);
- } //end client id set
- $outp=[];
- if(isset($_GET["chtid"])){
- $chtid=$_GET["chtid"]; //chat id
- if(tru("send")){
- $msg=$file_get_contents("php://input); //NO! form/multipart
- if(strlen($msg)){
- if(!isset($msgsdat["chats"][$chtid])) $msgsdat["chats"][$chtid]=[];
- array_push($msgsdat["chats"][$chtid],["t"=>$st,"csid"=>$csid,"msg"=>$msg]);
- }
- }
- if(tru("rcv")){
- $cht=$msgsdat["chats"][$chtid];
- $ocht=[];
- $i=0; $L=count($cht);
- while($i<$L){
- $pv=$cht[$i];
- if($pv["csid"]==$csid) $pv=1;
- array_push($ocht,$pv);
- $i++;
- }
- $outp["rcv"]=$ocht;
- }
- }else{ $outp["err"]="no chat id (?chtid=) present"; }
- file_put_contents("msgs_.txt",json_encode($msgsdat));
- echo json_encode($outp);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement