1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00
appwrite/app/workers/webhooks.php

94 lines
3.1 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
use Appwrite\Resque\Worker;
use Utopia\App;
2020-05-10 04:39:50 +12:00
use Utopia\CLI\Console;
2021-09-01 21:13:23 +12:00
require_once __DIR__ . '/../workers.php';
2019-05-09 18:54:39 +12:00
2021-01-15 19:02:48 +13:00
Console::title('Webhooks V1 Worker');
2021-09-01 21:13:23 +12:00
Console::success(APP_NAME . ' webhooks worker v1 has started');
2019-05-09 18:54:39 +12:00
class WebhooksV1 extends Worker
2019-05-09 18:54:39 +12:00
{
public function init(): void
2019-05-09 18:54:39 +12:00
{
}
public function run(): void
2019-05-09 18:54:39 +12:00
{
$errors = [];
// Event
2020-11-19 11:08:01 +13:00
$projectId = $this->args['projectId'] ?? '';
2021-05-17 21:49:17 +12:00
$webhooks = $this->args['webhooks'] ?? [];
2020-11-19 11:08:01 +13:00
$userId = $this->args['userId'] ?? '';
$event = $this->args['event'] ?? '';
2021-03-30 07:00:10 +13:00
$eventData = \json_encode($this->args['eventData']);
2019-05-09 18:54:39 +12:00
2021-05-17 21:49:17 +12:00
foreach ($webhooks as $webhook) {
2020-06-20 23:20:49 +12:00
if (!(isset($webhook['events']) && \is_array($webhook['events']) && \in_array($event, $webhook['events']))) {
2019-05-09 18:54:39 +12:00
continue;
}
2020-12-04 06:56:07 +13:00
$id = $webhook['$id'] ?? '';
2020-10-15 10:34:57 +13:00
$name = $webhook['name'] ?? '';
$signature = $webhook['signature'] ?? 'not-yet-implemented';
$url = $webhook['url'] ?? '';
$security = (bool) ($webhook['security'] ?? true);
2020-10-15 10:34:57 +13:00
$httpUser = $webhook['httpUser'] ?? null;
$httpPass = $webhook['httpPass'] ?? null;
2019-05-09 18:54:39 +12:00
2020-06-20 23:20:49 +12:00
$ch = \curl_init($url);
2019-05-09 18:54:39 +12:00
2020-06-20 23:20:49 +12:00
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
2021-03-30 07:00:10 +13:00
\curl_setopt($ch, CURLOPT_POSTFIELDS, $eventData);
2020-06-20 23:20:49 +12:00
\curl_setopt($ch, CURLOPT_HEADER, 0);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
2021-09-01 21:13:23 +12:00
\curl_setopt($ch, CURLOPT_USERAGENT, \sprintf(
APP_USERAGENT,
2020-06-30 23:09:28 +12:00
App::getEnv('_APP_VERSION', 'UNKNOWN'),
2020-06-29 08:45:36 +12:00
App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
2020-03-02 06:05:51 +13:00
));
2020-06-20 23:20:49 +12:00
\curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
2019-05-09 18:54:39 +12:00
'Content-Type: application/json',
2021-09-01 21:13:23 +12:00
'Content-Length: ' . \strlen($eventData),
'X-' . APP_NAME . '-Webhook-Id: ' . $id,
'X-' . APP_NAME . '-Webhook-Event: ' . $event,
'X-' . APP_NAME . '-Webhook-Name: ' . $name,
'X-' . APP_NAME . '-Webhook-User-Id: ' . $userId,
'X-' . APP_NAME . '-Webhook-Project-Id: ' . $projectId,
'X-' . APP_NAME . '-Webhook-Signature: ' . $signature,
2019-05-09 18:54:39 +12:00
]
);
if (!$security) {
2020-06-20 23:20:49 +12:00
\curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
\curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
2019-05-09 18:54:39 +12:00
}
if (!empty($httpUser) && !empty($httpPass)) {
2020-06-20 23:20:49 +12:00
\curl_setopt($ch, CURLOPT_USERPWD, "$httpUser:$httpPass");
\curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
2019-05-09 18:54:39 +12:00
}
2020-06-20 23:20:49 +12:00
if (false === \curl_exec($ch)) {
2021-09-01 21:13:23 +12:00
$errors[] = \curl_error($ch) . ' in event ' . $event . ' for webhook ' . $name;
2019-05-09 18:54:39 +12:00
}
2020-06-20 23:20:49 +12:00
\curl_close($ch);
2019-05-09 18:54:39 +12:00
}
if (!empty($errors)) {
2020-06-20 23:20:49 +12:00
throw new Exception(\implode(" / \n\n", $errors));
2019-05-09 18:54:39 +12:00
}
}
public function shutdown(): void
2019-05-09 18:54:39 +12:00
{
}
2021-09-01 21:13:23 +12:00
}