Add Request and validation

This commit is contained in:
2024-08-07 17:46:01 +02:00
parent 1aa26c836a
commit e25502f4f8
4 changed files with 72 additions and 33 deletions

View File

@@ -2,7 +2,54 @@
namespace Shikiryu\Bot;
use JsonException;
class Request
{
public string $nc_signature;
public string $nc_random;
public string $nc_server;
public string $delivery;
public string $event;
public string $signature;
public string $body;
public function __construct()
{
$this->nc_signature = $_SERVER['HTTP_X_NEXTCLOUD_TALK_SIGNATURE'] ?? '';
$this->nc_random = $_SERVER['HTTP_X_NEXTCLOUD_TALK_RANDOM'] ?? '';
$this->nc_server = $_SERVER['HTTP_X_NEXTCLOUD_TALK_BACKEND'] ?? '';
$this->delivery = $_SERVER['HTTP_X_H1_DELIVERY'] ?? '';
$this->event = $_SERVER['HTTP_X_H1_EVENT'] ?? '';
$this->signature = $_SERVER['HTTP_X_H1_SIGNATURE'] ?? '';
$this->body = file_get_contents('php://input');
}
public function getJSONBody(): array
{
try {
return json_decode($this->body, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
return [];
}
}
public function isMessage(): bool
{
$data = $this->getJSONBody();
return $data['type'] === 'Create' &&
$data['object']['name'] === 'message' &&
!empty($data['object']['content']);
}
public function getMessage(): string
{
try {
$data = $this->getJSONBody();
return json_decode($data['object']['content'], true, 512, JSON_THROW_ON_ERROR)['message'];
} catch (JsonException $e) {
return '';
}
}
}