1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
<?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/>.
defined('MOODLE_INTERNAL') || die();
/**
* It's a base64 encoded?
*
* @param string $string
*
* @return bool
*/
function is_base64(string $string) {
return base64_encode(base64_decode($string)) === $string;
}
/**
* Create a table local_webhooks_events.
*
* @throws \ddl_exception
*/
function create_table_events() {
global $DB;
// Loads ddl manager and xmldb classes.
$dbman = $DB->get_manager();
// Define table local_webhooks_events to be created.
$table = new xmldb_table('local_webhooks_events');
// Adding fields to table local_webhooks_events.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
$table->add_field('serviceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL);
// Adding keys to table local_webhooks_events.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
// Conditionally launch create table for local_webhooks_events.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
}
/**
* Delete table local_webhooks_service.
*
* @throws \ddl_exception
* @throws \ddl_table_missing_exception
*/
function drop_table_service() {
global $DB;
// Loads ddl manager and xmldb classes.
$dbman = $DB->get_manager();
// Define table local_webhooks_service to be dropped.
$table = new xmldb_table('local_webhooks_service');
// Conditionally launch drop table for local_webhooks_service.
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
}
/**
* Create a table local_webhooks_service.
*
* @throws \ddl_exception
*/
function create_table_service() {
global $DB;
// Loads ddl manager and xmldb classes.
$dbman = $DB->get_manager();
// Define table local_webhooks_service to be created.
$table = new xmldb_table('local_webhooks_service');
// Adding fields to table local_webhooks_service.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
$table->add_field('header', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, 'application/json');
$table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
$table->add_field('point', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
$table->add_field('status', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_field('token', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL);
// Adding keys to table local_webhooks_service.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
// Conditionally launch create table for local_webhooks_service.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}
}
/**
* Save data to database.
*
* @param array $records
*
* @throws \dml_exception
*/
function save_records(array $records) {
global $DB;
foreach ($records as $record) {
if (!is_array($record)) {
continue;
}
$recordid = $DB->insert_record('local_webhooks_service', (object) $record);
if ($recordid && is_array($record['events'])) {
foreach ($record['events'] as $eventname) {
$DB->insert_record('local_webhooks_events', (object) [
'name' => $eventname,
'serviceid' => $recordid,
]);
}
}
}
}
/**
* Function to upgrade 'local_webhooks'.
*
* @param int $oldversion
*
* @return bool
* @throws \ddl_exception
* @throws \ddl_table_missing_exception
* @throws \dml_exception
* @throws \downgrade_exception
* @throws \upgrade_exception
*/
function xmldb_local_webhooks_upgrade(int $oldversion) {
global $DB;
/* Update from versions 0.* */
if (in_array($oldversion, [2017101900, 2017102500, 2017102600, 2017102610, 2017102620, 2017102630], true)) {
upgrade_plugin_savepoint(true, 2019040100, 'local', 'webhooks');
}
/* Update from versions 1.* */
if (in_array($oldversion, [2017102700, 2017102900, 2017102910], true)) {
upgrade_plugin_savepoint(true, 2019040100, 'local', 'webhooks');
}
/* Update from versions 2.* */
if (in_array($oldversion, [2017111800, 2017111810], true)) {
upgrade_plugin_savepoint(true, 2019040100, 'local', 'webhooks');
}
/* Update from versions 3.* */
if (in_array($oldversion, [2017112600, 2018061900, 2018061910, 2018061920], true)) {
$records = $DB->get_records('local_webhooks_service', null, 'id');
$services = [];
foreach ($records as $record) {
if (!is_object($record)) {
continue;
}
$service = [
'name' => $record->title,
'point' => $record->url,
'status' => (bool) $record->enable,
'token' => $record->token,
];
if ($record->type === 'json') {
$service['header'] = 'application/json';
} else {
$service['header'] = 'application/x-www-form-urlencoded';
}
if (!empty($record->events)) {
$record->events = unserialize(gzuncompress(base64_decode($record->events)), [
'allowed_classes' => false,
]);
}
if (is_array($record->events) && !empty($record->events)) {
foreach ($record->events as $eventname => $eventstatus) {
if ((bool) $eventstatus) {
$service['events'][] = is_base64($eventname)
? base64_decode($eventname, true)
: $eventname;
}
}
}
$services[] = $service;
}
/* Update structure */
drop_table_service();
create_table_events();
create_table_service();
/* Saving records */
save_records($services);
upgrade_plugin_savepoint(true, 2019040100, 'local', 'webhooks');
}
/* Update from versions 4.* */
if (in_array($oldversion, [2017122900, 2018022500], true)) {
upgrade_plugin_savepoint(true, 2019040100, 'local', 'webhooks');
}
return true;
}
|