Advertisement
IWBH_01

message chat php

Jun 4th, 2020
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. <?php
  2.  
  3.  $st=time();
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  function tru($up){
  10.   if(isset($_GET["up"])){
  11.    $rv=$_GET["up"];
  12.    if(strtolower($rv)=="false") $rv=!1;
  13.    else if(preg_match("/[^0-9]/",$rv)) $rv=!0;
  14.    else $rv=!!($rv*1);
  15.   }else{ $rv=!1; }
  16.   return $rv;
  17.  }
  18.  
  19.  
  20.  
  21.  
  22.  if(file_exists("msgs_.txt")){
  23.   $msgsdat=json_decode(file_get_contents("msgs_.txt"),!0);
  24.  }else{
  25.   $msgsdat=["claa"=>[],"chats"=>[]];
  26.   //claa = clients array
  27.  }
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  //client id set
  34.  if(isset($_COOKIE["csid"])){
  35.   $csid=$_COOKIE["csid"];
  36.  }else{
  37.   if(isset($_GET["csid"]))$csid=$_GET["csid"];
  38.   else{
  39.    $claa=$msgsdat["claa"];
  40.    do{ $csid=bin2hex(random_bytes(10)); }while(isset($claa[$csid]));
  41.    $msgsdat["claa"][$csid]=["Lreg"=>$st];
  42.   }
  43.   setcookie("csid",$csid);
  44.  } //end client id set
  45.  
  46.  
  47.  
  48.  
  49.  $outp=[];
  50.  
  51.  if(isset($_GET["chtid"])){
  52.   $chtid=$_GET["chtid"]; //chat id
  53.   if(tru("send")){
  54.    $msg=$file_get_contents("php://input); //NO! form/multipart
  55.   if(strlen($msg)){
  56.    if(!isset($msgsdat["chats"][$chtid])) $msgsdat["chats"][$chtid]=[];
  57.    array_push($msgsdat["chats"][$chtid],["t"=>$st,"csid"=>$csid,"msg"=>$msg]);
  58.   }
  59.  }
  60.  
  61.  if(tru("rcv")){
  62.   $cht=$msgsdat["chats"][$chtid];
  63.   $ocht=[];
  64.   $i=0; $L=count($cht);
  65.   while($i<$L){
  66.    $pv=$cht[$i];
  67.    if($pv["csid"]==$csid) $pv=1;
  68.    array_push($ocht,$pv);
  69.    $i++;
  70.   }
  71.  
  72.   $outp["rcv"]=$ocht;
  73.  }
  74.  
  75. }else{ $outp["err"]="no chat id (?chtid=) present"; }
  76.  
  77.  
  78. file_put_contents("msgs_.txt",json_encode($msgsdat));
  79.  
  80. echo json_encode($outp);
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement