appwrite/app/workers/webhooks.php

119 lines
3.8 KiB
PHP
Raw Normal View History

2019-05-09 06:54:39 +00:00
<?php
use Utopia\App;
2020-05-09 16:39:50 +00:00
use Utopia\CLI\Console;
use Utopia\Config\Config;
use Appwrite\Database\Database;
2020-07-05 19:46:04 +00:00
use Appwrite\Database\Adapter\MySQL as MySQLAdapter;
use Appwrite\Database\Adapter\Redis as RedisAdapter;
use Appwrite\Database\Validator\Authorization;
2021-03-10 08:08:17 +00:00
use Appwrite\Resque\Worker;
2020-05-09 16:39:50 +00:00
require_once __DIR__.'/../init.php';
2019-05-09 06:54:39 +00:00
2021-01-15 06:02:48 +00:00
Console::title('Webhooks V1 Worker');
2019-05-09 06:54:39 +00:00
2020-05-09 16:39:50 +00:00
Console::success(APP_NAME.' webhooks worker v1 has started');
2019-05-09 06:54:39 +00:00
2021-03-10 08:08:17 +00:00
class WebhooksV1 extends Worker
2019-05-09 06:54:39 +00:00
{
public $args = [];
2021-03-10 08:08:17 +00:00
public function init(): void
2019-05-09 06:54:39 +00:00
{
}
2021-03-10 08:08:17 +00:00
public function execute(): void
2019-05-09 06:54:39 +00:00
{
2020-07-05 19:46:04 +00:00
global $register;
2019-05-09 06:54:39 +00:00
2020-07-05 19:46:04 +00:00
$consoleDB = new Database();
$consoleDB->setAdapter(new RedisAdapter(new MySQLAdapter($register), $register));
$consoleDB->setNamespace('app_console'); // Main DB
$consoleDB->setMocks(Config::getParam('collections', []));
2019-05-09 06:54:39 +00:00
$errors = [];
// Event
2020-11-18 22:08:01 +00:00
$projectId = $this->args['projectId'] ?? '';
$userId = $this->args['userId'] ?? '';
$event = $this->args['event'] ?? '';
2020-06-20 11:20:49 +00:00
$payload = \json_encode($this->args['payload']);
2019-05-09 06:54:39 +00:00
// Webhook
Authorization::disable();
$project = $consoleDB->getDocument($projectId);
2020-07-03 05:58:19 +00:00
Authorization::reset();
2019-05-09 06:54:39 +00:00
2020-06-20 11:20:49 +00:00
if (\is_null($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS !== $project->getCollection()) {
2019-05-09 06:54:39 +00:00
throw new Exception('Project Not Found');
}
foreach ($project->getAttribute('webhooks', []) as $webhook) {
2020-06-20 11:20:49 +00:00
if (!(isset($webhook['events']) && \is_array($webhook['events']) && \in_array($event, $webhook['events']))) {
2019-05-09 06:54:39 +00:00
continue;
}
2020-12-03 17:56:07 +00:00
$id = $webhook['$id'] ?? '';
2020-10-14 21:34:57 +00:00
$name = $webhook['name'] ?? '';
$signature = $webhook['signature'] ?? 'not-yet-implemented';
$url = $webhook['url'] ?? '';
$security = (bool) $webhook['security'] ?? true;
$httpUser = $webhook['httpUser'] ?? null;
$httpPass = $webhook['httpPass'] ?? null;
2019-05-09 06:54:39 +00:00
2020-06-20 11:20:49 +00:00
$ch = \curl_init($url);
2019-05-09 06:54:39 +00:00
2020-06-20 11:20:49 +00:00
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
\curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
\curl_setopt($ch, CURLOPT_HEADER, 0);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($ch, CURLOPT_USERAGENT, \sprintf(APP_USERAGENT,
2020-06-30 11:09:28 +00:00
App::getEnv('_APP_VERSION', 'UNKNOWN'),
2020-06-28 20:45:36 +00:00
App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
2020-03-01 17:05:51 +00:00
));
2020-06-20 11:20:49 +00:00
\curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
2019-05-09 06:54:39 +00:00
'Content-Type: application/json',
2020-06-20 11:20:49 +00:00
'Content-Length: '.\strlen($payload),
2020-12-03 17:56:07 +00:00
'X-'.APP_NAME.'-Webhook-Id: '.$id,
2020-02-02 19:06:16 +00:00
'X-'.APP_NAME.'-Webhook-Event: '.$event,
'X-'.APP_NAME.'-Webhook-Name: '.$name,
2020-12-03 17:56:07 +00:00
'X-'.APP_NAME.'-Webhook-User-Id: '.$userId,
'X-'.APP_NAME.'-Webhook-Project-Id: '.$projectId,
'X-'.APP_NAME.'-Webhook-Signature: '.$signature,
2019-05-09 06:54:39 +00:00
]
);
if (!$security) {
2020-06-20 11:20:49 +00:00
\curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
\curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
2019-05-09 06:54:39 +00:00
}
if (!empty($httpUser) && !empty($httpPass)) {
2020-06-20 11:20:49 +00:00
\curl_setopt($ch, CURLOPT_USERPWD, "$httpUser:$httpPass");
\curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
2019-05-09 06:54:39 +00:00
}
2020-06-20 11:20:49 +00:00
if (false === \curl_exec($ch)) {
$errors[] = \curl_error($ch).' in event '.$event.' for webhook '.$name;
2019-05-09 06:54:39 +00:00
}
2020-06-20 11:20:49 +00:00
\curl_close($ch);
2019-05-09 06:54:39 +00:00
}
if (!empty($errors)) {
2020-06-20 11:20:49 +00:00
throw new Exception(\implode(" / \n\n", $errors));
2019-05-09 06:54:39 +00:00
}
}
2021-03-10 08:08:17 +00:00
public function shutdown(): void
2019-05-09 06:54:39 +00:00
{
}
}