This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
lib.php
280 lines (260 loc) · 8.47 KB
/
lib.php
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?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/>.
/**
*
* @package tool_coursebank
* @author Dmitrii Metelkin <[email protected]>
* @copyright 2015 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/admin/tool/coursebank/locallib.php');
require_once($CFG->dirroot.'/lib/adminlib.php');
define('CRON_MOODLE', 1);
define('CRON_EXTERNAL', 2);
define('TEMP_BACKUP_FILE_NAME', 'backup.mbz');
/**
* Recursively delete a directory that is not empty
*
* @param string $dir path
*/
function tool_coursebank_rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") {
tool_coursebank_rrmdir($dir."/".$object);
} else {
unlink($dir."/".$object);
}
}
}
reset($objects);
rmdir($dir);
}
}
/**
* Legacy cron function
*/
function tool_coursebank_cron() {
return tool_coursebank_send_backups_with_lock(CRON_MOODLE);
}
/**
* Fetch backups from automated system and send them.
*
* This function will need to acquire a lock before continuing.
*
* Returns true if successfully run.
*
* @param int Execution context: CRON_MOODLE or CRON_EXTERNAL
* @return boolean
*/
function tool_coursebank_send_backups_with_lock($type) {
mtrace('Started at ' . date('Y-m-d h:i:s', time()));
$canrun = tool_coursebank_can_run_cron($type);
if (is_string($canrun)) {
mtrace(get_string($canrun, 'tool_coursebank'));
mtrace('Failed. ' . date('Y-m-d h:i:s', time()));
return false;
}
// If there's an existing lock...
if (tool_coursebank_get_cron_lock()) {
if (!tool_coursebank_cron_lock_can_be_cleared()) {
mtrace(get_string('cron_locked', 'tool_coursebank'));
mtrace('Failed. ' . date('Y-m-d h:i:s', time()));
return false;
} else {
mtrace(get_string('cron_lock_cleared', 'tool_coursebank'));
}
}
if (!tool_coursebank_set_cron_lock()) {
mtrace(get_string('cron_locked', 'tool_coursebank'));
mtrace('Failed. ' . date('Y-m-d h:i:s', time()));
return false;
}
mtrace(get_string('cron_sending', 'tool_coursebank'));
tool_coursebank::fetch_backups();
mtrace('Completed at ' . date('Y-m-d h:i:s', time()));
tool_coursebank_clear_cron_lock();
return true;
}
/**
* Check if we can run cron.
*
* @param integer $type Type of cron run (moodle or external)
* @return boolean|string If can't run returns a key of lang string.
*/
function tool_coursebank_can_run_cron($type) {
$enabled = get_config('tool_coursebank', 'enable');
$externalcronenabled = get_config('tool_coursebank', 'externalcron');
if ($enabled) {
if ($type == CRON_MOODLE and $externalcronenabled) {
return 'cron_skippingmoodle';
} else if ($type == CRON_EXTERNAL and !$externalcronenabled) {
return 'cron_skippingexternal';
}
} else {
return 'disabled';
}
return true;
}
/**
* Get the CourseBank cron lock.
*
* If the lock is present, returns the value of the lock (unix timestamp).
* If there is no lock, returns null.
*
* @global type DB
* @param string $name cron lock's name
* @return int|null
*/
function tool_coursebank_get_cron_lock() {
return get_config('tool_coursebank', 'tool_coursebank_cronlock');
}
/**
* Set the CourseBank cron lock.
*
* This is intended to ensure that the plugin talks to CourseBank one
* session at time.
* Normally should be called without $time parameter so that the lock value can
* default to now.
*
* No attempt is made to check if it *should* be set.
* @see tool_coursebank_cron_lock_can_be_cleared .
*
* @param int $time unix timestamp which is used as the lock value.
* @return bool Whether lock was set.
*/
function tool_coursebank_set_cron_lock($time=null) {
if (!$time) {
$time = time();
}
if (!is_int($time)) {
return false;
}
return set_config('tool_coursebank_cronlock', $time, 'tool_coursebank');
}
/**
* Removes CourseBank cron lock if present.
*
* No attempt is made to check if it *should* be cleared.
* @see tool_coursebank_cron_lock_can_be_cleared .
*
* @param int $time unix timestamp which is used as the lock value.
* @return bool Whether lock was cleared.
*/
function tool_coursebank_clear_cron_lock($time=null) {
return unset_config('tool_coursebank_cronlock', 'tool_coursebank');
}
/**
* Determines if the existing cron lock can be cleared.
*
* The intention is to clear out a stale cron lock (one that should
* have been cleared).
*
* @return bool
*/
function tool_coursebank_cron_lock_can_be_cleared($maxsecs=tool_coursebank::CRON_LOCK_TIMEOUT) {
$now = time();
$time = tool_coursebank_get_cron_lock();
if (!$time) {
return true;
}
if (($now - $time) >= $maxsecs) {
return true;
}
return false;
}
/**
* Check if URL is valid
*
* @param string $url
*/
function tool_coursebank_check_url($url) {
// Validate URL first.
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
return false;
}
return true;
}
/**
* Check if URL returns valid header.
*
* @param string $url
* @param array $invaldheaders A list of invalid responses e.g 404, 500.
* @return boolean
*/
function tool_coursebank_is_url_available($url, $invaldheaders=array('404', '403', '500')) {
$headers = get_headers($url);
if (empty($headers)) {
return false;
}
foreach ($invaldheaders as $invalidheader) {
if (strstr($headers[0], $invalidheader)) {
return false;
}
}
return true;
}
/**
* This class is a somewhat hacky way to ensure that the Course Bank session
* key is discarded whenever settings are changed.
*
* (This is important because settings changes may include auth token or the
* External Course Bank URL. Although Course Bank will re-authenticate as
* necessary if a session key does not work, it is conceivable that using a
* session key associated with an old URL or token value might cause External
* Course Bank to attribute data sent by this Course Bank instance to some
* other instance, resulting in data corruption or overwriting.)
*
* This class extends the configempty class and overrides the output_html
* method in order to output a completely empty, hidden setting item. When a
* settings page form is saved in Moodle, settings are read in from form
* elements, so this invisible element allows us to empty the session key
* on submit without cluttering the settings page unnecessarily.
*/
class admin_setting_configsessionkey extends admin_setting_configtext {
/**
* @param string $name
* @param string $visiblename
* @param string $description
*/
public function __construct($name, $visiblename, $description) {
parent::__construct($name, $visiblename, $description, '', PARAM_RAW);
}
/**
* Returns an XHTML string for the hidden field
*
* @param string $data
* @param string $query
* @return string XHTML string for the editor
*/
public function output_html($data, $query='') {
$html = '<div class="form-item clearfix" id="admin-sessionkey">' .
'<div class="form-setting"> '.
'<div class="form-empty" >' .
'<input type="hidden" '.
'id="' . $this->get_id() . '"' .
'name="' . $this->get_full_name() . '"' .
'value=""' .
'/>' .
'</div>' .
'</div>' .
'</div>';
return $html;
}
}