2017-10-19 07:04:14 +04:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
2017-10-26 17:34:34 +04:00
|
|
|
* The event handler.
|
|
|
|
*
|
|
|
|
* @package local_webhooks
|
|
|
|
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2017-10-19 07:04:14 +04:00
|
|
|
|
|
|
|
namespace local_webhooks;
|
|
|
|
|
2018-06-19 22:22:45 +04:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2017-10-19 07:04:14 +04:00
|
|
|
|
2018-11-02 00:36:09 +04:00
|
|
|
global $CFG;
|
2017-11-23 19:18:32 +04:00
|
|
|
|
2018-11-02 00:36:09 +04:00
|
|
|
require_once(__DIR__ . '/../lib.php');
|
|
|
|
require_once(__DIR__ . '/../locallib.php');
|
|
|
|
|
|
|
|
require_once($CFG->libdir . '/filelib.php');
|
|
|
|
|
|
|
|
use curl;
|
|
|
|
use local_webhooks_events;
|
2017-10-26 11:07:01 +04:00
|
|
|
|
2017-10-25 08:46:07 +04:00
|
|
|
/**
|
|
|
|
* Defines how to work with events.
|
|
|
|
*
|
|
|
|
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2017-10-27 08:51:27 +04:00
|
|
|
class handler {
|
2017-10-25 08:46:07 +04:00
|
|
|
/**
|
|
|
|
* External handler.
|
|
|
|
*
|
|
|
|
* @param object $event
|
2018-11-02 00:36:09 +04:00
|
|
|
*
|
|
|
|
* @throws \dml_exception
|
|
|
|
* @throws \coding_exception
|
2017-10-25 08:46:07 +04:00
|
|
|
*/
|
2017-10-27 08:51:27 +04:00
|
|
|
public static function events($event) {
|
2017-11-15 10:46:09 +04:00
|
|
|
$data = $event->get_data();
|
2017-10-19 07:04:14 +04:00
|
|
|
|
2018-11-02 02:06:47 +04:00
|
|
|
if (!empty($callbacks = local_webhooks_get_list_records())) {
|
2017-10-25 08:46:07 +04:00
|
|
|
foreach ($callbacks as $callback) {
|
2017-10-26 06:49:33 +04:00
|
|
|
self::handler_callback($data, $callback);
|
2017-10-25 08:46:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-26 06:45:53 +04:00
|
|
|
* Processes each callback.
|
2017-10-25 08:46:07 +04:00
|
|
|
*
|
2017-11-23 21:54:50 +04:00
|
|
|
* @param array $data
|
2017-10-25 08:46:07 +04:00
|
|
|
* @param object $callback
|
2018-11-02 00:36:09 +04:00
|
|
|
*
|
|
|
|
* @throws \coding_exception
|
|
|
|
* @throws \dml_exception
|
2017-10-25 08:46:07 +04:00
|
|
|
*/
|
2017-10-26 06:49:33 +04:00
|
|
|
private static function handler_callback($data, $callback) {
|
2017-11-18 07:34:38 +04:00
|
|
|
global $CFG;
|
|
|
|
|
2018-11-02 00:36:09 +04:00
|
|
|
if ((bool) $callback->enable && !empty($callback->events[$data['eventname']])) {
|
|
|
|
$urlparse = parse_url($CFG->wwwroot);
|
2017-10-26 08:01:16 +04:00
|
|
|
|
2018-11-02 00:36:09 +04:00
|
|
|
$data['host'] = $urlparse['host'];
|
|
|
|
$data['token'] = $callback->token;
|
|
|
|
$data['extra'] = $callback->other;
|
2017-10-29 11:32:50 +04:00
|
|
|
|
2018-11-02 00:36:09 +04:00
|
|
|
self::send($data, $callback);
|
2017-10-19 07:04:14 +04:00
|
|
|
}
|
|
|
|
}
|
2017-10-25 08:46:07 +04:00
|
|
|
|
2017-10-26 06:45:53 +04:00
|
|
|
/**
|
|
|
|
* Sending data to the node.
|
|
|
|
*
|
2017-11-23 21:54:50 +04:00
|
|
|
* @param array $data
|
2017-10-26 06:45:53 +04:00
|
|
|
* @param object $callback
|
2018-11-02 00:36:09 +04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws \coding_exception
|
|
|
|
* @throws \dml_exception
|
2017-10-26 06:45:53 +04:00
|
|
|
*/
|
|
|
|
private static function send($data, $callback) {
|
2018-11-02 00:36:09 +04:00
|
|
|
$curl = new curl();
|
|
|
|
$curl->setHeader(array('Content-Type: application/' . $callback->type));
|
2017-10-26 17:34:34 +04:00
|
|
|
$curl->post($callback->url, json_encode($data));
|
2017-11-23 18:54:31 +04:00
|
|
|
|
2017-10-27 12:22:55 +04:00
|
|
|
$response = $curl->getResponse();
|
2018-11-02 00:36:09 +04:00
|
|
|
local_webhooks_events::response_answer($callback->id, $response);
|
|
|
|
|
2017-10-27 12:22:55 +04:00
|
|
|
return $response;
|
|
|
|
}
|
2017-10-19 07:04:14 +04:00
|
|
|
}
|