2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
require_once __DIR__.'/../init.php';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12:00
|
|
|
\cli_set_process_title('Webhooks V1 Worker');
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
echo APP_NAME.' webhooks worker v1 has started';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
use Appwrite\Database\Database;
|
|
|
|
use Appwrite\Database\Validator\Authorization;
|
2020-06-29 08:45:36 +12:00
|
|
|
use Utopia\App;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
class WebhooksV1
|
|
|
|
{
|
|
|
|
public $args = [];
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function perform()
|
|
|
|
{
|
2020-07-05 20:03:57 +12:00
|
|
|
global $consoleDB;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
// Event
|
2019-09-07 05:08:48 +12:00
|
|
|
$projectId = $this->args['projectId'];
|
|
|
|
$event = $this->args['event'];
|
2020-06-20 23:20:49 +12:00
|
|
|
$payload = \json_encode($this->args['payload']);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
// Webhook
|
|
|
|
|
|
|
|
Authorization::disable();
|
|
|
|
|
|
|
|
$project = $consoleDB->getDocument($projectId);
|
|
|
|
|
2020-07-03 17:58:19 +12:00
|
|
|
Authorization::reset();
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:20:49 +12: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) {
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-07 05:08:48 +12:00
|
|
|
$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
|
|
|
|
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');
|
|
|
|
\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 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(
|
2019-09-30 19:13:40 +13:00
|
|
|
$ch,
|
|
|
|
CURLOPT_HTTPHEADER,
|
|
|
|
[
|
2019-05-09 18:54:39 +12:00
|
|
|
'Content-Type: application/json',
|
2020-06-20 23:20:49 +12:00
|
|
|
'Content-Length: '.\strlen($payload),
|
2020-02-03 08:06:16 +13:00
|
|
|
'X-'.APP_NAME.'-Webhook-Event: '.$event,
|
2019-09-07 05:08:48 +12:00
|
|
|
'X-'.APP_NAME.'-Webhook-Name: '.$name,
|
|
|
|
'X-'.APP_NAME.'-Webhook-Signature: '.$signature,
|
2019-05-09 18:54:39 +12:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2019-09-07 05:08:48 +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
|
|
|
}
|
|
|
|
|
2019-09-07 05:08:48 +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)) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2019-09-07 05:08:48 +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 tearDown()
|
|
|
|
{
|
|
|
|
// ... Remove environment for this job
|
|
|
|
}
|
2019-09-07 05:08:48 +12:00
|
|
|
}
|