Compare commits

..
17 Commits
12 changed files with 374 additions and 188 deletions
+1 -1
View File
@@ -60,6 +60,6 @@ class backup_performed extends \core\event\base {
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/managerservice.php");
return new \moodle_url("/local/webhooks/index.php");
}
}
+1 -1
View File
@@ -60,6 +60,6 @@ class backup_restored extends \core\event\base {
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/managerservice.php");
return new \moodle_url("/local/webhooks/index.php");
}
}
+1 -1
View File
@@ -60,6 +60,6 @@ class service_deleted extends \core\event\base {
* Get URL related to the action.
*/
public function get_url() {
return new \moodle_url("/local/webhooks/editservice.php", array("serviceid" => $this->objectid));
return new \moodle_url("/local/webhooks/index.php");
}
}
+9 -54
View File
@@ -26,6 +26,9 @@ namespace local_webhooks;
defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/../lib.php");
require_once(__DIR__ . "/../locallib.php");
require_once($CFG->libdir . "/filelib.php");
/**
@@ -42,26 +45,12 @@ class handler {
*/
public static function events($event) {
$data = $event->get_data();
self::transmitter($data);
}
/**
* Transmitter, processing event and services.
*
* @param array $data
*/
private static function transmitter($data) {
global $DB;
$callbacks = $DB->get_recordset("local_webhooks_service");
if ($callbacks->valid()) {
if ($callbacks = local_webhooks_get_list_records()) {
foreach ($callbacks as $callback) {
self::handler_callback($data, $callback);
}
}
$callbacks->close();
}
/**
@@ -74,22 +63,12 @@ class handler {
global $CFG;
if (boolval($callback->enable)) {
$events = array();
if (!empty($callback->events)) {
$events = unserialize(gzuncompress(base64_decode($callback->events)));
}
if (!empty($events[$data["eventname"]])) {
if (!empty($callback->events[$data["eventname"]])) {
$urlparse = parse_url($CFG->wwwroot);
$data["host"] = $urlparse['host'];
if (!empty($callback->token)) {
$data["token"] = $callback->token;
}
if (!empty($callback->other)) {
$data["extra"] = $callback->other;
}
self::send($data, $callback);
}
@@ -104,35 +83,11 @@ class handler {
*/
private static function send($data, $callback) {
$curl = new \curl();
$curl->setHeader(array("Content-Type: application/$callback->type"));
$curl->setHeader(array("Content-Type: application/" . $callback->type));
$curl->post($callback->url, json_encode($data));
$response = $curl->getResponse();
self::logger($callback, $response);
\local_webhooks_events::response_answer($callback->id, $response);
return $response;
}
/**
* Event logging.
*
* @param array $response
* @param object $callback
*/
private static function logger($callback, $response) {
$status = "Error sending request";
if (!empty($response["HTTP/1.1"])) {
$status = $response["HTTP/1.1"];
}
$event = \local_webhooks\event\response_answer::create(
array(
"context" => \context_system::instance(0),
"objectid" => $callback->id,
"other" => array(
"status" => $status
)
)
);
$event->trigger();
}
}
@@ -22,16 +22,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_webhooks;
defined("MOODLE_INTERNAL") || die();
require_once($CFG->libdir . "/formslib.php");
use lang_string;
use moodleform;
use report_eventlist_list_generator;
/**
* Description editing form definition.
*
@@ -46,19 +40,6 @@ class service_edit_form extends moodleform {
parent::__construct($baseurl);
}
/**
* Preparing data before displaying.
*
* @param object $record
*/
public function set_data($record) {
if (!empty($record->events)) {
$record->events = unserialize(gzuncompress(base64_decode($record->events)));
}
return parent::set_data($record);
}
/**
* Defines the standard structure of the form.
*/
+14 -37
View File
@@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Service editor.
* Page for editing the service.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
@@ -23,30 +23,25 @@
*/
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/editform.php");
require_once($CFG->libdir . "/adminlib.php");
require_once(__DIR__ . "/classes/service_form.php");
require_once(__DIR__ . "/lib.php");
admin_externalpage_setup("local_webhooks");
require_once($CFG->libdir . "/adminlib.php");
/* Optional parameters */
$serviceid = optional_param("serviceid", 0, PARAM_INT);
/* Link generation */
$urlparameters = array("serviceid" => $serviceid);
$managerservice = new moodle_url("/local/webhooks/managerservice.php", $urlparameters);
$baseurl = new moodle_url("/local/webhooks/editservice.php", $urlparameters);
$PAGE->set_url($baseurl, $urlparameters);
$managerservice = new moodle_url("/local/webhooks/index.php");
/* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$context = context_system::instance();
$PAGE->set_context($context);
/* Preparing a template for data */
$titlepage = new lang_string("externalservice", "webservice");
$servicerecord = new stdClass;
/* Create an editing form */
$mform = new \local_webhooks\service_edit_form($PAGE->url);
$mform = new service_edit_form($PAGE->url);
/* Cancel processing */
if ($mform->is_cancelled()) {
@@ -54,47 +49,29 @@ if ($mform->is_cancelled()) {
}
/* Getting the data */
$servicerecord = new stdClass();
if ($editing = boolval($serviceid)) {
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
$servicerecord = local_webhooks_get_record($serviceid);
$mform->set_data($servicerecord);
}
/* Processing of received data */
if ($data = $mform->get_data()) {
if (empty($data->events)) {
$data->events = array();
}
/* Pack the list of events */
$data->events = base64_encode(gzcompress(serialize($data->events), 9));
if ($editing) {
$data->id = $serviceid;
$DB->update_record("local_webhooks_service", $data);
/* Run the event */
$event = \local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $data->id));
$event->trigger();
local_webhooks_update_record($data, false);
redirect($managerservice, new lang_string("eventwebserviceserviceupdated", "webservice"));
} else {
$servicenewid = $DB->insert_record("local_webhooks_service", $data);
/* Run the event */
$event = \local_webhooks\event\service_added::create(array("context" => $context, "objectid" => $servicenewid));
$event->trigger();
local_webhooks_update_record($data, true);
redirect($managerservice, new lang_string("eventwebserviceservicecreated", "webservice"));
}
}
/* Page template */
$PAGE->set_pagelayout("admin");
/* The page title */
$titlepage = new lang_string("externalservice", "webservice");
$PAGE->navbar->add($titlepage);
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
/* The page title */
$PAGE->navbar->add($titlepage);
echo $OUTPUT->header();
/* Displays the form */
+19 -40
View File
@@ -23,78 +23,57 @@
*/
require_once(__DIR__ . "/../../config.php");
require_once($CFG->libdir . "/tablelib.php");
require_once($CFG->libdir . "/adminlib.php");
require_once(__DIR__ . "/lib.php");
admin_externalpage_setup("local_webhooks");
require_once($CFG->libdir . "/adminlib.php");
require_once($CFG->libdir . "/tablelib.php");
/* Optional parameters */
$backupservices = optional_param("getbackup", 0, PARAM_BOOL);
$deleteid = optional_param("deleteid", 0, PARAM_INT);
$hideshowid = optional_param("hideshowid", 0, PARAM_INT);
/* Used references */
$editservice = "/local/webhooks/editservice.php";
$managerservice = "/local/webhooks/managerservice.php";
$restorebackup = "/local/webhooks/restorebackup.php";
/* Link generation */
$editservice = "/local/webhooks/editservice.php";
$managerservice = "/local/webhooks/index.php";
$restorebackup = "/local/webhooks/restorebackup.php";
$baseurl = new moodle_url($managerservice);
$PAGE->set_url($baseurl);
/* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$context = context_system::instance();
$PAGE->set_context($context);
/* Delete the service */
if (boolval($deleteid) && confirm_sesskey()) {
$DB->delete_records("local_webhooks_service", array("id" => $deleteid));
/* Run the event */
$event = \local_webhooks\event\service_deleted::create(array("context" => $context, "objectid" => $deleteid));
$event->trigger();
if (boolval($deleteid)) {
local_webhooks_remove_record($deleteid);
redirect($PAGE->url, new lang_string("eventwebserviceservicedeleted", "webservice"));
}
/* Retrieving a list of services */
$callbacks = $DB->get_records_select("local_webhooks_service", null, null, $DB->sql_order_by_text("id"));
$callbacks = local_webhooks_get_list_records();
/* Upload settings as a file */
if (boolval($backupservices)) {
$filecontent = base64_encode(gzcompress(serialize($callbacks), 9));
$filecontent = local_webhooks_create_backup();
$filename = "webhooks_" . date("U") . ".backup";
/* Run the event */
$event = \local_webhooks\event\backup_performed::create(array("context" => $context, "objectid" => 0));
$event->trigger();
send_file($filecontent, $filename, 0, 0, true, true);
}
/* Switching the status of the service */
if (boolval($hideshowid) && confirm_sesskey()) {
if (boolval($hideshowid)) {
$callback = $callbacks[$hideshowid];
if (!empty($callback)) {
$callback->enable = !boolval($callback->enable);
$DB->update_record("local_webhooks_service", $callback);
/* Run the event */
$event = \local_webhooks\event\service_updated::create(array("context" => $context, "objectid" => $hideshowid));
$event->trigger();
local_webhooks_update_record($callback, false);
redirect($PAGE->url, new lang_string("eventwebserviceserviceupdated", "webservice"));
}
}
/* Page template */
$titlepage = new lang_string("pluginname", "local_webhooks");
$PAGE->set_pagelayout("admin");
$PAGE->set_title($titlepage);
$PAGE->set_heading($titlepage);
/* The page title */
$titlepage = new lang_string("pluginname", "local_webhooks");
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
echo $OUTPUT->header();
/* Table declaration */
@@ -120,7 +99,7 @@ foreach ($callbacks as $callback) {
}
/* Link to enable / disable the service */
$hideshowlink = new moodle_url($managerservice, array("hideshowid" => $callback->id, "sesskey" => sesskey()));
$hideshowlink = new moodle_url($managerservice, array("hideshowid" => $callback->id));
$hideshowitem = $OUTPUT->action_icon($hideshowlink, new pix_icon($hideshowicon, $hideshowstring));
/* Link for editing */
@@ -128,7 +107,7 @@ foreach ($callbacks as $callback) {
$edititem = $OUTPUT->action_icon($editlink, new pix_icon("t/edit", new lang_string("edit", "moodle")));
/* Link to remove */
$deletelink = new moodle_url($managerservice, array("deleteid" => $callback->id, "sesskey" => sesskey()));
$deletelink = new moodle_url($managerservice, array("deleteid" => $callback->id));
$deleteitem = $OUTPUT->action_icon($deletelink, new pix_icon("t/delete", new lang_string("delete", "moodle")));
/* Adding data to the table */
@@ -147,7 +126,7 @@ $backupurl = new moodle_url($managerservice, array("getbackup" => true));
echo $OUTPUT->single_button($backupurl, new lang_string("backup", "moodle"), "get");
/* Button for restoring settings */
$restorebackupurl = new moodle_url($restorebackup, array("sesskey" => sesskey()));
$restorebackupurl = new moodle_url($restorebackup);
echo $OUTPUT->single_button($restorebackupurl, new lang_string("restore", "moodle"), "get");
echo $OUTPUT->footer();
+165
View File
@@ -0,0 +1,165 @@
<?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/>.
/**
* Library code used by the service control interfaces.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined("MOODLE_INTERNAL") || die();
require_once(__DIR__ . "/locallib.php");
/**
* Getting a list of all services.
*
* @param number $limitfrom
* @param number $limitnum
* @return array
*/
function local_webhooks_get_list_records($limitfrom = 0, $limitnum = 0) {
global $DB;
$listservices = $DB->get_records("local_webhooks_service", null, "id", "*", $limitfrom, $limitnum);
foreach ($listservices as $servicerecord) {
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
}
}
return $listservices;
}
/**
* Getting information about the service.
*
* @param number $serviceid
* @return object
*/
function local_webhooks_get_record($serviceid = 0) {
global $DB;
$servicerecord = $DB->get_record("local_webhooks_service", array("id" => $serviceid), "*", MUST_EXIST);
if (!empty($servicerecord->events)) {
$servicerecord->events = local_webhooks_unarchive_data($servicerecord->events);
}
return $servicerecord;
}
/**
* Clear the database table.
*/
function local_webhooks_remove_list_records() {
global $DB;
$DB->delete_records("local_webhooks_service", null);
}
/**
* Delete the record.
*
* @param number $serviceid
*/
function local_webhooks_remove_record($serviceid = 0) {
global $DB;
$DB->delete_records("local_webhooks_service", array("id" => $serviceid));
local_webhooks_events::service_deleted($serviceid);
}
/**
* Update the record in the database.
*
* @param object $data
* @param boolean $insert
* @return boolean
*/
function local_webhooks_update_record($data, $insert = true) {
global $DB;
if (empty($data->events)) {
$data->events = array();
}
$data->events = local_webhooks_archiving_data($data->events);
if (boolval($insert)) {
$result = $DB->insert_record("local_webhooks_service", $data, true, false);
local_webhooks_events::service_added($result);
} else {
$result = $DB->update_record("local_webhooks_service", $data, false);
local_webhooks_events::service_updated($data->id);
}
return boolval($result);
}
/**
* Make a backup copy of all the services.
*
* @return string
*/
function local_webhooks_create_backup() {
$listservices = local_webhooks_get_list_records();
$listservices = local_webhooks_archiving_data($listservices);
local_webhooks_events::backup_performed();
return $listservices;
}
/**
* Restore the data from the backup.
*
* @param string $data
*/
function local_webhooks_restore_backup($listservices = "") {
$listservices = local_webhooks_unarchive_data($listservices);
local_webhooks_remove_list_records();
foreach ($listservices as $servicerecord) {
local_webhooks_update_record($servicerecord, true);
}
local_webhooks_events::backup_restored();
}
/**
* Compress an array into a string.
*
* @param array $data
* @return string
*/
function local_webhooks_archiving_data($data = array()) {
$result = base64_encode(gzcompress(serialize($data), 3));
return $result;
}
/**
* Gets an array from a compressed string.
*
* @param string $data
* @return array
*/
function local_webhooks_unarchive_data($data = "") {
$result = unserialize(gzuncompress(base64_decode($data)));
return $result;
}
+144
View File
@@ -0,0 +1,144 @@
<?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/>.
/**
* Classes of modules.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined("MOODLE_INTERNAL") || die();
/**
* Description of functions of the call of events
*
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_webhooks_events {
/**
* Call the event when creating a backup.
*/
public static function backup_performed() {
$context = context_system::instance();
$event = local_webhooks\event\backup_performed::create(
array(
"context" => $context,
"objectid" => 0
)
);
$event->trigger();
}
/**
* Call the event when restoring from a backup.
*/
public static function backup_restored() {
$context = context_system::instance();
$event = local_webhooks\event\backup_restored::create(
array(
"context" => $context,
"objectid" => 0
)
);
$event->trigger();
}
/**
* Call event when the response is received from the service
*
* @param number $objectid Service ID
* @param array $response Server response
*/
public static function response_answer($objectid = 0, $response = array()) {
$context = context_system::instance();
$status = "Error sending request";
if (!empty($response["HTTP/1.1"])) {
$status = $response["HTTP/1.1"];
}
$event = local_webhooks\event\response_answer::create(
array(
"context" => $context,
"objectid" => $objectid,
"other" => array("status" => $status)
)
);
$event->trigger();
}
/**
* Call the event when the service is added.
*
* @param number $objectid Service ID
*/
public static function service_added($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_added::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event->trigger();
}
/**
* Call the event when the service is deleted.
*
* @param number $objectid Service ID
*/
public static function service_deleted($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_deleted::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event->trigger();
}
/**
* Call event when the service is updated.
*
* @param number $objectid Service ID
*/
public static function service_updated($objectid = 0) {
$context = context_system::instance();
$event = local_webhooks\event\service_updated::create(
array(
"context" => $context,
"objectid" => $objectid
)
);
$event->trigger();
}
}
+11 -26
View File
@@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Restore settings page.
* Restore the settings page.
*
* @package local_webhooks
* @copyright 2017 "Valentin Popov" <info@valentineus.link>
@@ -23,22 +23,21 @@
*/
require_once(__DIR__ . "/../../config.php");
require_once(__DIR__ . "/classes/editform.php");
require_once(__DIR__ . "/classes/service_form.php");
require_once(__DIR__ . "/lib.php");
require_once($CFG->libdir . "/adminlib.php");
admin_externalpage_setup("local_webhooks");
/* Link generation */
$managerservice = new moodle_url("/local/webhooks/managerservice.php");
$baseurl = new moodle_url("/local/webhooks/restorebackup.php");
$PAGE->set_url($baseurl);
$managerservice = new moodle_url("/local/webhooks/index.php");
/* Configure the context of the page */
admin_externalpage_setup("local_webhooks", "", null, $baseurl, array());
$context = context_system::instance();
$PAGE->set_context($context);
/* Create an editing form */
$mform = new \local_webhooks\service_backup_form($PAGE->url);
$mform = new service_backup_form($PAGE->url);
/* Cancel processing */
if ($mform->is_cancelled()) {
@@ -46,31 +45,17 @@ if ($mform->is_cancelled()) {
}
/* Processing the received file */
$data = $mform->get_data();
if (boolval($data) && confirm_sesskey()) {
if ($data = $mform->get_data()) {
$content = $mform->get_file_content("backupfile");
$callbacks = unserialize(gzuncompress(base64_decode($content)));
$DB->delete_records("local_webhooks_service");
foreach ($callbacks as $callback) {
$DB->insert_record("local_webhooks_service", $callback);
}
/* Run the event */
$event = \local_webhooks\event\backup_restored::create(array("context" => $context, "objectid" => 0));
$event->trigger();
local_webhooks_restore_backup($content);
redirect($managerservice, new lang_string("restorefinished", "moodle"));
}
/* Page template */
/* The page title */
$titlepage = new lang_string("backup", "moodle");
$PAGE->set_pagelayout("admin");
$PAGE->navbar->add($titlepage);
$PAGE->set_heading($titlepage);
$PAGE->set_title($titlepage);
/* The page title */
$PAGE->navbar->add($titlepage);
echo $OUTPUT->header();
/* Displays the form */
+1 -1
View File
@@ -27,6 +27,6 @@ defined("MOODLE_INTERNAL") || die();
if ($hassiteconfig) {
$ADMIN->add("server", new admin_externalpage("local_webhooks",
new lang_string("pluginname", "local_webhooks"),
new moodle_url("/local/webhooks/managerservice.php")
new moodle_url("/local/webhooks/index.php")
));
}
+2 -2
View File
@@ -24,8 +24,8 @@
defined("MOODLE_INTERNAL") || die();
$plugin->release = "2.1.0 (Build: 2017111810)";
$plugin->version = 2017111810;
$plugin->release = "3.0.0 (Build: 2017112600)";
$plugin->version = 2017112600;
$plugin->requires = 2016112900;
$plugin->component = "local_webhooks";
$plugin->maturity = MATURITY_STABLE;