-
Notifications
You must be signed in to change notification settings - Fork 31
/
locallib.php
327 lines (289 loc) · 10.6 KB
/
locallib.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?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/>.
/**
* Usefull classes for package local_moodlecheck
*
* @package local_moodlecheck
* @copyright 2012 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// phpcs:disable moodle.Commenting.VariableComment.Missing
// phpcs:disable moodle.Commenting.MissingDocblock.Missing
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->dirroot . '/local/moodlecheck/file.php');
/**
* Handles one rule
*
* @package local_moodlecheck
* @copyright 2012 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_moodlecheck_rule {
protected $code;
protected $callback;
protected $rulestring;
protected $errorstring;
protected $severity = 'error';
public function __construct($code) {
$this->code = $code;
}
public function set_callback($callback) {
$this->callback = $callback;
return $this;
}
public function set_rulestring($rulestring) {
$this->rulestring = $rulestring;
return $this;
}
public function set_errorstring($errorstring) {
$this->errorstring = $errorstring;
return $this;
}
public function set_severity($severity) {
$this->severity = $severity;
return $this;
}
public function get_name() {
if ($this->rulestring !== null && get_string_manager()->string_exists($this->rulestring, 'local_moodlecheck')) {
return get_string($this->rulestring, 'local_moodlecheck');
} else if (get_string_manager()->string_exists('rule_'. $this->code, 'local_moodlecheck')) {
return get_string('rule_'. $this->code, 'local_moodlecheck');
} else {
return $this->code;
}
}
public function get_error_message($args) {
if (!empty($this->errorstring) && get_string_manager()->string_exists($this->errorstring, 'local_moodlecheck')) {
return get_string($this->errorstring, 'local_moodlecheck', $args);
} else if (get_string_manager()->string_exists('error_'. $this->code, 'local_moodlecheck')) {
return get_string('error_'. $this->code, 'local_moodlecheck', $args);
} else {
if (isset($args['line'])) {
// Do not dump line number, it will be included in the final message.
unset($args['line']);
}
if (is_array($args)) {
$args = ': '. var_export($args, true);
} else if ($args !== true && $args !== null) {
$args = ': '. $args;
} else {
$args = '';
}
return $this->get_name(). '. Error'. $args;
}
}
public function validatefile(local_moodlecheck_file $file) {
$callback = $this->callback;
$reterrors = $callback($file);
$ruleerrors = [];
foreach ($reterrors as $args) {
$ruleerrors[] = [
'line' => $args['line'],
'severity' => $args["severity"] ?? $this->severity,
'message' => $this->get_error_message($args),
'source' => $this->code,
];
}
return $ruleerrors;
}
}
/**
* Rule registry
*
* @package local_moodlecheck
* @copyright 2012 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_moodlecheck_registry {
protected static $rules = [];
protected static $enabledrules = [];
public static function add_rule($code) {
$rule = new local_moodlecheck_rule($code);
self::$rules[$code] = $rule;
return $rule;
}
public static function get_registered_rules() {
return self::$rules;
}
public static function enable_rule($code, $enable = true) {
if (!isset(self::$rules[$code])) {
// Can not enable/disable unexisting rule.
return;
}
if (!$enable) {
if (isset(self::$enabledrules[$code])) {
unset(self::$enabledrules[$code]);
}
} else {
self::$enabledrules[$code] = self::$rules[$code];
}
}
public static function &get_enabled_rules() {
return self::$enabledrules;
}
public static function enable_all_rules() {
foreach (array_keys(self::$rules) as $code) {
self::enable_rule($code);
}
}
}
/**
* Handles one path being validated (file or directory)
*
* @package local_moodlecheck
* @copyright 2012 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_moodlecheck_path {
protected $path = null;
protected $ignorepaths = null;
protected $file = null;
protected $subpaths = null;
protected $validated = false;
protected $rootpath = true;
public function __construct($path, $ignorepaths) {
$path = clean_param(trim($path), PARAM_PATH);
// If the path is already one existing full path
// accept it, else assume it's a relative one.
if (!file_exists($path) && substr($path, 0, 1) == '/') {
$path = substr($path, 1);
}
$this->path = $path;
$this->ignorepaths = $ignorepaths;
}
public function get_fullpath() {
global $CFG;
// It's already one full path.
if (file_exists($this->path)) {
return $this->path;
}
return $CFG->dirroot. '/'. $this->path;
}
public function validate() {
if ($this->validated) {
// Prevent from second validation.
return;
}
if (is_file($this->get_fullpath())) {
$this->file = new local_moodlecheck_file($this->get_fullpath());
} else if (is_dir($this->get_fullpath())) {
$this->subpaths = [];
if ($dh = opendir($this->get_fullpath())) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..' && $file != '.git' && $file != '.hg' && !$this->is_ignored($file)) {
$subpath = new local_moodlecheck_path($this->path . '/'. $file, $this->ignorepaths);
$subpath->set_rootpath(false);
$this->subpaths[] = $subpath;
}
}
closedir($dh);
}
}
$this->validated = true;
}
protected function is_ignored($file) {
$filepath = $this->path. '/'. $file;
foreach ($this->ignorepaths as $ignorepath) {
$ignorepath = rtrim($ignorepath, '/');
if ($filepath == $ignorepath || substr($filepath, 0, strlen($ignorepath) + 1) == $ignorepath . '/') {
return true;
}
}
return false;
}
public function is_file() {
return $this->file !== null;
}
public function is_dir() {
return $this->subpaths !== null;
}
public function get_path() {
return $this->path;
}
public function get_file() {
return $this->file;
}
public function get_subpaths() {
return $this->subpaths;
}
protected function set_rootpath($rootpath) {
$this->rootpath = (boolean)$rootpath;
}
public function is_rootpath() {
return $this->rootpath;
}
public static function get_components($componentsfile = null) {
static $components = [];
if (!empty($components)) {
return $components;
}
if (empty($componentsfile)) {
return [];
}
if (file_exists($componentsfile) && is_readable($componentsfile)) {
$fh = fopen($componentsfile, 'r');
while (($line = fgets($fh, 4096)) !== false) {
$split = explode(',', $line);
if (count($split) != 3) {
// Wrong count of elements in the line.
continue;
}
if (trim($split[0]) != 'plugin' && trim($split[0]) != 'subsystem') {
// Wrong type.
continue;
}
// Let's assume it's a correct line.
$components[trim($split[0])][trim($split[1])] = trim($split[2]);
}
fclose($fh);
}
return $components;
}
}
/**
* Form for check options
*
* @package local_moodlecheck
* @copyright 2012 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class local_moodlecheck_form extends moodleform {
protected function definition() {
$mform = $this->_form;
$mform->addElement('textarea', 'path', get_string('path', 'local_moodlecheck'),
['rows' => 8, 'cols' => 120]);
$mform->addHelpButton('path', 'path', 'local_moodlecheck');
$mform->addElement('header', 'selectivecheck', get_string('options'));
$mform->setExpanded('selectivecheck', false);
$mform->addElement('textarea', 'ignorepath', get_string('ignorepath', 'local_moodlecheck'),
['rows' => 3, 'cols' => 120]);
$mform->addElement('radio', 'checkall', '', get_string('checkallrules', 'local_moodlecheck'), 'all');
$mform->addElement('radio', 'checkall', '', get_string('checkselectedrules', 'local_moodlecheck'), 'selected');
$mform->setDefault('checkall', 'all');
$group = [];
foreach (local_moodlecheck_registry::get_registered_rules() as $code => $rule) {
$group[] =& $mform->createElement('checkbox', "rule[$code]", ' ', $rule->get_name());
}
$mform->addGroup($group, 'checkboxgroup', '', ['<br>'], false);
foreach (local_moodlecheck_registry::get_registered_rules() as $code => $rule) {
$group[] =& $mform->createElement('checkbox', "rule[$code]", ' ', $rule->get_name());
$mform->setDefault("rule[$code]", 0);
$mform->disabledIf("rule[$code]", 'checkall', 'eq', 'all');
}
$this->add_action_buttons(false, get_string('check', 'local_moodlecheck'));
}
}