true, 'id' => 1); } function updatePost(string $id, array $fields = array()) : array { return array('ok' => true); } function postFile(string $title, string $mime_type, string $file_path, array $fields = array()) : array { return array('ok' => true); } function postPNG(string $title, string $png_file, array $fields = array()) : array { return array('ok' => true); } function getPermalink(string $id) : string { return "https://fake/$id"; } } class MattermostChan implements IMChan { private string $server; private string $token; private string $team; private string $chan; function __construct(string $server, string $token, string $team, string $chan) { $this->server = $server; $this->token = $token; $this->team = $team; $this->chan = $chan; } function post(string $message, array $fields = array()) : array { return mm_post($this->server, $this->token, $this->chan, $message, $fields); } function updatePost(string $id, array $fields = array()) : array { return mm_update($this->server, $this->token, $id, $fields); } function postFile(string $title, string $mime_type, string $file_path, array $fields = array()) : array { return mm_post_file($this->server, $this->token, $this->chan, $title, $mime_type, $file_path, $fields); } function postPNG(string $title, string $png_file, array $fields = array()) : array { return mm_post_png($this->server, $this->token, $this->chan, $title, $png_file, $fields); } function getPermalink(string $id) : string { return mm_get_permalink($this->server, $this->team, $id); } } function mm_post(string $server, string $token, string $chan, string $message, $fields = array()) { $ch = _mm_post_curl($server, $token, $chan, $message, $fields); $result = curl_exec($ch); curl_close($ch); $json = json_decode($result, true); if(!$json) return array('ok' => false, 'id' => 0); return array('ok' => true, 'id' => $json['id']); } function mm_post_async(string $server, string $token, string $chan, string $message, $fields = array()) : Amp\Promise { return Amp\call(function() use($server, $token, $chan, $message, $fields) { $ch = _mm_post_curl($server, $token, $chan, $message, $fields); $result = yield _multi_curl_async($ch, true); curl_close($ch); $json = json_decode($result, true); if(!$json) return array('ok' => false, 'id' => 0); return array('ok' => true, 'id' => $json['id']); }); } function mm_update(string $server, string $token, string $id, array $fields) { $ch = curl_init("$server/api/v4/posts/$id"); $headers = array( 'Content-Type: application/json', 'Authorization: Bearer ' . $token ); $fields["post_id"] = $id; $fields["id"] = $id; curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); curl_close($ch); return json_decode($result, true); } function mm_post_file( string $server, string $token, string $chan, string $title, string $mime_type, string $file_path, $msg_fields = array() ) { $headers = array( 'Content-Type: multipart/form-data', 'Authorization: Bearer ' . $token ); $file = new \CurlFile(realpath($file_path), $mime_type, basename($file_path)); $fields = array(); $fields['channel_id'] = $chan; $fields['files'] = $file; $ch = curl_init("$server/api/v4/files"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if($status != 201) throw new Exception("File failed with status: " . $status .", result: " . var_export($result, true)); $result = json_decode($result, true); $file_id = $result['file_infos'][0]['id']; $msg_fields['file_ids'] = array($file_id); return mm_post($server, $token, $chan, $title, $msg_fields); } function mm_post_png( string $server, string $token, string $chan, string $title, string $png_file, $msg_fields = array() ) { $headers = array( 'Content-Type: multipart/form-data', 'Authorization: Bearer ' . $token ); $file = new \CurlFile(realpath($png_file), 'image/png', basename($png_file)); $fields = array(); $fields['channel_id'] = $chan; $fields['files'] = $file; $ch = curl_init("$server/api/v4/files"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if($status != 201) throw new Exception("File failed with status: " . $status .", result: " . $result); $result = json_decode($result, true); $file_id = $result['file_infos'][0]['id']; $msg_fields['file_ids'] = array($file_id); return mm_post($server, $token, $chan, $title, $msg_fields); } function mm_get_permalink(string $server, string $team, string $msg_id) { return "$server/$team/pl/$msg_id"; } function _mm_post_curl(string $server, string $token, string $chan, string $message, $fields = array()) { $ch = curl_init("$server/api/v4/posts"); $headers = array( 'Content-Type: application/json', 'Authorization: Bearer ' . $token ); if(!isset($fields['channel_id'])) $fields['channel_id'] = $chan; $fields['message'] = $message; curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); return $ch; }