User Tools

Site Tools


projects:matrix_php_class

Matrix PHP Class

See also: https://opensource.glasgow.social/projects/matrix_php_class

matrix.class.php
class matrix {
 
   function __construct() {
      $this->homeserver = "glasgow.social";
      $this->access_token = "ACCESS_TOKEN_HERE";
      // room lookups
      $this->rooms = array(      
                                 "#room_name"=> "!room_id:glasgow.social",
                           );
   }
 
   function post($msg, $room_name = "#glasgow", $formatted_msg = null) {
      $msgtype = "m.text";
      if(preg_match("/^#/", $room_name)) {
         $room = $this->rooms[$room_name];
      } else {
         $room = $room_name;
      }
      $url = "https://".$this->homeserver."/_matrix/client/r0/rooms/$room/send/m.room.message?access_token=".$this->access_token;
      $payload = array("msgtype"=>$msgtype, "body"=>$msg);
      if(!is_null($formatted_msg)) {
         $payload['format'] = "org.matrix.custom.html";
         $payload['formatted_body'] = $formatted_msg;
      }
      $payload = json_encode($payload);
      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
      $response = curl_exec($ch);
   }
 
   function get_rooms() {
      $url = "https://".$this->homeserver."/_matrix/client/r0/joined_rooms?access_token=".$this->access_token;
      $data = file_get_contents($url);
      return json_decode($data, true);
   }
 
   function send_direct_message($hostmask, $msg) {
      if(preg_match("/^!/", $hostmask)) {
         $private_room_id = $hostmask;
      } else {
         $private_room_id = $this->create_private_room($hostmask);
      }
      if($private_room_id) {
         $this->post($msg, $private_room_id);
         return $private_room_id;
      } else {
         error_log("Unable to create private room for $hostmask");
      }
      return false;
   }
 
   function get_new_events($room, $from = null) {
 
      if(is_null($from))
         $from = $this->get_from_time($room);
 
      $url = "https://{$this->homeserver}/_matrix/client/r0/rooms/$room/messages?access_token={$this->access_token}&from=$from&limit=40";
 
      $cache_filename = "/tmp/".md5($room).".cache";
      if(!file_exists($cache_filename)) {
         $content = file_get_contents($url);
         file_put_contents($cache_filename, $content);
      } else {
         $cache_ts = filemtime($cache_filename);
         if($cache_ts >= strtotime('-5 seconds')) {
            $content = file_get_contents($cache_filename);
         } else {
            $content = file_get_contents($url);
         } else {
            $content = file_get_contents($url);
            file_put_contents($cache_filename, $content);
         }
      }
 
   $data = json_decode($content, true);
   return $data;
}
 
   function get_sync_token($room) {
      $json_filter["room"]["rooms"] = array("$room");
      $url = "https://{$this->homeserver}/_matrix/client/r0/sync?access_token={$this->access_token}&filter=".json_encode($json_filter);
      $content = file_get_contents($url);
      $data = json_decode($content, true);
      return false;
   }
 
 
   function get_from_time($room) {
      $filename = "/tmp/".md5($room).".fromts";
      if(file_exists($filename)) {
         $from_ts = filemtime($filename);
         if($from_ts <= strtotime('-30 minutes')) {
            error_log("GETNEWEVENTS: FROMTS Cache hit");
            $from = $this->set_from_time($room);
            file_put_contents($filename, $from);
         }
         $data = file_get_contents($filename);
         return $data;
      } else {
         error_log("GETNEWEVENTS: FROMTS file didn't exist.");
         $from = $this->set_from_time($room);
         return $from;
      }
   }
 
   function set_from_time($room, $from = null) {
      if(is_null($from)) {
         $from = $this->get_sync_token($room);
      }
      $filename = "/tmp/".md5($room).".fromts";
      file_put_contents($filename, $from);
      file_put_contents($filename, $from);
      return $from;
   }
 
   function del_cache($room) {
      $cache_filename = "/tmp/".md5($room).".cache";
      unlink($cache_filename);
   }
}
}
projects/matrix_php_class.txt · Last modified: 2021/04/07 20:59 by admin