raspisms/controllers/publics/Webhook.php

178 lines
5.4 KiB
PHP
Raw Normal View History

2019-12-04 03:04:45 +01:00
<?php
/*
* This file is part of RaspiSMS.
*
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace controllers\publics;
/**
* Page des webhooks.
*/
class Webhook extends \descartes\Controller
{
private $internal_webhook;
private $internal_event;
public function __construct()
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
$this->internal_webhook = new \controllers\internals\Webhook($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
\controllers\internals\Tool::verifyconnect();
}
/**
2020-01-17 18:19:25 +01:00
* List all webhooks.
2019-12-04 03:04:45 +01:00
*
* @param mixed $page
*/
2020-04-07 03:02:33 +02:00
public function list()
2019-12-04 03:04:45 +01:00
{
2020-04-07 03:02:33 +02:00
$webhooks = $this->internal_webhook->list_for_user($_SESSION['user']['id']);
2019-12-04 03:04:45 +01:00
$this->render('webhook/list', ['webhooks' => $webhooks]);
}
/**
2020-01-17 18:19:25 +01:00
* Delete a list of webhooks.
2019-12-04 03:04:45 +01:00
*
* @param array int $_GET['ids'] : Les id des webhooks à supprimer
* @param mixed $csrf
*
* @return boolean;
*/
public function delete($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
$this->redirect(\descartes\Router::url('Webhook', 'list'));
return false;
}
$ids = $_GET['ids'] ?? [];
foreach ($ids as $id)
{
$this->internal_webhook->delete_for_user($_SESSION['user']['id'], $id);
}
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
/**
* Cette fonction retourne la page d'ajout d'une webhook.
*/
public function add()
{
$this->render('webhook/add');
}
/**
2020-01-17 18:19:25 +01:00
* Edit a list of webhooks.
2019-12-04 03:04:45 +01:00
*
* @param array int $_GET['ids'] : ids of webhooks to edit
*/
public function edit()
{
$ids = $_GET['ids'] ?? [];
$webhooks = $this->internal_webhook->gets_in_for_user($_SESSION['user']['id'], $ids);
$this->render('webhook/edit', [
'webhooks' => $webhooks,
]);
}
/**
2020-01-17 18:19:25 +01:00
* Insert a new webhook.
2019-12-04 03:04:45 +01:00
*
* @param $csrf : Le jeton CSRF
2020-01-17 18:19:25 +01:00
* @param string $_POST['url'] : URL to call on webhook release
2019-12-04 03:04:45 +01:00
* @param string $_POST['type'] : Type of webhook, either 'send_sms' or 'receive_sms'
*
* @return boolean;
*/
public function create($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
$url = $_POST['url'] ?? false;
$type = $_POST['type'] ?? false;
if (!$url || !$type)
{
\FlashMessage\FlashMessage::push('danger', 'Renseignez au moins une URL et un type de webhook.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
if (!$this->internal_webhook->create($_SESSION['user']['id'], $url, $type))
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce webhook, vérifiez qu\'il s\'agit bien d\'une URL HTTP(S) valide.');
2019-12-04 03:04:45 +01:00
return $this->redirect(\descartes\Router::url('Webhook', 'add'));
2019-12-04 03:04:45 +01:00
}
\FlashMessage\FlashMessage::push('success', 'La webhook a bien été créé.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
/**
* Cette fonction met à jour une webhook.
*
* @param $csrf : Le jeton CSRF
* @param array $_POST['webhooks'] : Un tableau des webhooks avec leur nouvelle valeurs
*
* @return boolean;
*/
public function update($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
$nb_update = 0;
foreach ($_POST['webhooks'] as $webhook)
{
$url = $webhook['url'] ?? false;
$type = $webhook['type'] ?? false;
if (!$url || !$type)
{
continue;
}
$success = $this->internal_webhook->update_for_user($_SESSION['user']['id'], $webhook['id'], $url, $type);
$nb_update += (int) $success;
}
if ($nb_update !== \count($_POST['webhooks']))
{
\FlashMessage\FlashMessage::push('info', 'Certains webhooks n\'ont pas pu êtres mis à jour.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Tous les webhooks ont été modifiés avec succès.');
return $this->redirect(\descartes\Router::url('Webhook', 'list'));
}
}