2023-11-07 15:42:40 +03:00
|
|
|
<?php
|
|
|
|
namespace ATF;
|
|
|
|
|
|
|
|
use Exception;
|
2023-11-07 16:08:42 +03:00
|
|
|
use Amp;
|
2023-11-07 15:42:40 +03:00
|
|
|
|
|
|
|
interface IMChan
|
|
|
|
{
|
|
|
|
function post(string $message, array $fields = array()) : array;
|
|
|
|
function updatePost(string $thread_id, array $fields = array());
|
2024-10-11 13:35:41 +03:00
|
|
|
function postFile(string $title, string $mime_type, string $file_path, array $fields = array()) : array;
|
2023-11-07 15:42:40 +03:00
|
|
|
function postPNG(string $title, string $png_file, array $fields = array()) : array;
|
|
|
|
function getPermalink(string $id) : string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NullMessenger implements IMChan
|
|
|
|
{
|
|
|
|
function post(string $message, array $fields = array()) : array
|
|
|
|
{
|
|
|
|
return array('ok' => true, 'id' => 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updatePost(string $id, array $fields = array()) : array
|
|
|
|
{
|
|
|
|
return array('ok' => true);
|
|
|
|
}
|
|
|
|
|
2024-10-11 13:35:41 +03:00
|
|
|
function postFile(string $title, string $mime_type, string $file_path, array $fields = array()) : array
|
|
|
|
{
|
|
|
|
return array('ok' => true);
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:42:40 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-10-11 13:35:41 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:42:40 +03:00
|
|
|
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())
|
|
|
|
{
|
2023-11-07 16:08:42 +03:00
|
|
|
$ch = _mm_post_curl($server, $token, $chan, $message, $fields);
|
2023-11-07 15:42:40 +03:00
|
|
|
$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) {
|
|
|
|
|
2023-11-07 16:08:42 +03:00
|
|
|
$ch = _mm_post_curl($server, $token, $chan, $message, $fields);
|
|
|
|
$result = yield _multi_curl_async($ch, true);
|
2023-11-07 15:42:40 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-10-11 13:35:41 +03:00
|
|
|
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()
|
|
|
|
)
|
2023-11-07 15:42:40 +03:00
|
|
|
{
|
|
|
|
$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";
|
|
|
|
}
|
|
|
|
|
2023-11-07 16:08:42 +03:00
|
|
|
function _mm_post_curl(string $server, string $token, string $chan, string $message, $fields = array())
|
2023-11-07 15:42:40 +03:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|