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

105 lines
3.3 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
require_once __DIR__.'/../init.php';
2019-05-09 18:54:39 +12:00
cli_set_process_title('Webhooks V1 Worker');
echo APP_NAME.' webhooks worker v1 has started';
2019-05-09 18:54:39 +12:00
2020-03-29 01:42:16 +13:00
use Utopia\Config\Config;
use Appwrite\Database\Database;
use Appwrite\Database\Validator\Authorization;
2019-05-09 18:54:39 +12:00
class WebhooksV1
{
public $args = [];
public function setUp()
{
}
public function perform()
{
2020-03-29 01:42:16 +13:00
global $consoleDB, $request;
2019-05-09 18:54:39 +12:00
$errors = [];
// Event
$projectId = $this->args['projectId'];
$event = $this->args['event'];
$payload = json_encode($this->args['payload']);
2019-05-09 18:54:39 +12:00
// Webhook
Authorization::disable();
$project = $consoleDB->getDocument($projectId);
Authorization::enable();
2020-02-17 20:16:11 +13:00
if (is_null($project->getId()) || Database::SYSTEM_COLLECTION_PROJECTS !== $project->getCollection()) {
2019-05-09 18:54:39 +12:00
throw new Exception('Project Not Found');
}
foreach ($project->getAttribute('webhooks', []) as $webhook) {
if (!(isset($webhook['events']) && is_array($webhook['events']) && in_array($event, $webhook['events']))) {
2019-05-09 18:54:39 +12:00
continue;
}
$name = (isset($webhook['name'])) ? $webhook['name'] : '';
$signature = (isset($webhook['signature'])) ? $webhook['signature'] : 'not-yet-implemented';
$url = (isset($webhook['url'])) ? $webhook['url'] : '';
$security = (isset($webhook['security'])) ? (bool) $webhook['security'] : true;
$httpUser = (isset($webhook['httpUser'])) ? $webhook['httpUser'] : null;
$httpPass = (isset($webhook['httpPass'])) ? $webhook['httpPass'] : null;
2019-05-09 18:54:39 +12:00
$ch = curl_init($url);
2019-05-09 18:54:39 +12: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);
2020-03-02 06:05:51 +13:00
curl_setopt($ch, CURLOPT_USERAGENT, sprintf(APP_USERAGENT,
2020-03-29 01:42:16 +13:00
Config::getParam('version'),
2020-03-02 06:05:51 +13:00
$request->getServer('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)
));
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
2019-05-09 18:54:39 +12:00
'Content-Type: application/json',
'Content-Length: '.strlen($payload),
2020-02-03 08:06:16 +13:00
'X-'.APP_NAME.'-Webhook-Event: '.$event,
'X-'.APP_NAME.'-Webhook-Name: '.$name,
'X-'.APP_NAME.'-Webhook-Signature: '.$signature,
2019-05-09 18:54:39 +12:00
]
);
if (!$security) {
2019-05-09 18:54:39 +12:00
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
if (!empty($httpUser) && !empty($httpPass)) {
2019-05-09 18:54:39 +12:00
curl_setopt($ch, CURLOPT_USERPWD, "$httpUser:$httpPass");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
if (false === curl_exec($ch)) {
$errors[] = curl_error($ch).' in event '.$event.' for webhook '.$name;
2019-05-09 18:54:39 +12:00
}
curl_close($ch);
}
if (!empty($errors)) {
throw new Exception(implode(" / \n\n", $errors));
2019-05-09 18:54:39 +12:00
}
}
public function tearDown()
{
// ... Remove environment for this job
}
}