1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Entity model representing quiz settings for the seb plugin.
19 *
20 * @package    quizaccess_seb
21 * @author     Andrew Madden <andrewmadden@catalyst-au.net>
22 * @copyright  2019 Catalyst IT
23 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace quizaccess_seb;
27
28use CFPropertyList\CFArray;
29use CFPropertyList\CFBoolean;
30use CFPropertyList\CFDictionary;
31use CFPropertyList\CFNumber;
32use CFPropertyList\CFString;
33use core\persistent;
34use lang_string;
35use moodle_exception;
36use moodle_url;
37
38defined('MOODLE_INTERNAL') || die();
39
40/**
41 * Entity model representing quiz settings for the seb plugin.
42 *
43 * @copyright  2020 Catalyst IT
44 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class quiz_settings extends persistent {
47
48    /** Table name for the persistent. */
49    const TABLE = 'quizaccess_seb_quizsettings';
50
51    /** @var property_list $plist The SEB config represented as a Property List object. */
52    private $plist;
53
54    /** @var string $config The SEB config represented as a string. */
55    private $config;
56
57    /** @var string $configkey The SEB config key represented as a string. */
58    private $configkey;
59
60
61    /**
62     * Return the definition of the properties of this model.
63     *
64     * @return array
65     */
66    protected static function define_properties() : array {
67        return [
68            'quizid' => [
69                'type' => PARAM_INT,
70            ],
71            'cmid' => [
72                'type' => PARAM_INT,
73            ],
74            'templateid' => [
75                'type' => PARAM_INT,
76                'default' => 0,
77            ],
78            'requiresafeexambrowser' => [
79                'type' => PARAM_INT,
80                'default' => 0,
81            ],
82            'showsebtaskbar' => [
83                'type' => PARAM_INT,
84                'default' => 1,
85                'null' => NULL_ALLOWED,
86            ],
87            'showwificontrol' => [
88                'type' => PARAM_INT,
89                'default' => 0,
90                'null' => NULL_ALLOWED,
91            ],
92            'showreloadbutton' => [
93                'type' => PARAM_INT,
94                'default' => 1,
95                'null' => NULL_ALLOWED,
96            ],
97            'showtime' => [
98                'type' => PARAM_INT,
99                'default' => 1,
100                'null' => NULL_ALLOWED,
101            ],
102            'showkeyboardlayout' => [
103                'type' => PARAM_INT,
104                'default' => 1,
105                'null' => NULL_ALLOWED,
106            ],
107            'allowuserquitseb' => [
108                'type' => PARAM_INT,
109                'default' => 1,
110                'null' => NULL_ALLOWED,
111            ],
112            'quitpassword' => [
113                'type' => PARAM_TEXT,
114                'default' => '',
115                'null' => NULL_ALLOWED,
116            ],
117            'linkquitseb' => [
118                'type' => PARAM_URL,
119                'default' => '',
120                'null' => NULL_ALLOWED,
121            ],
122            'userconfirmquit' => [
123                'type' => PARAM_INT,
124                'default' => 1,
125                'null' => NULL_ALLOWED,
126            ],
127            'enableaudiocontrol' => [
128                'type' => PARAM_INT,
129                'default' => 0,
130                'null' => NULL_ALLOWED,
131            ],
132            'muteonstartup' => [
133                'type' => PARAM_INT,
134                'default' => 0,
135                'null' => NULL_ALLOWED,
136            ],
137            'allowspellchecking' => [
138                'type' => PARAM_INT,
139                'default' => 0,
140                'null' => NULL_ALLOWED,
141            ],
142            'allowreloadinexam' => [
143                'type' => PARAM_INT,
144                'default' => 1,
145                'null' => NULL_ALLOWED,
146            ],
147            'activateurlfiltering' => [
148                'type' => PARAM_INT,
149                'default' => 0,
150                'null' => NULL_ALLOWED,
151            ],
152            'filterembeddedcontent' => [
153                'type' => PARAM_INT,
154                'default' => 0,
155                'null' => NULL_ALLOWED,
156            ],
157            'expressionsallowed' => [
158                'type' => PARAM_TEXT,
159                'default' => '',
160                'null' => NULL_ALLOWED,
161            ],
162            'regexallowed' => [
163                'type' => PARAM_TEXT,
164                'default' => '',
165                'null' => NULL_ALLOWED,
166            ],
167            'expressionsblocked' => [
168                'type' => PARAM_TEXT,
169                'default' => '',
170                'null' => NULL_ALLOWED,
171            ],
172            'regexblocked' => [
173                'type' => PARAM_TEXT,
174                'default' => '',
175                'null' => NULL_ALLOWED,
176            ],
177            'showsebdownloadlink' => [
178                'type' => PARAM_INT,
179                'default' => 1,
180                'null' => NULL_ALLOWED,
181            ],
182            'allowedbrowserexamkeys' => [
183                'type' => PARAM_TEXT,
184                'default' => '',
185                'null' => NULL_ALLOWED,
186            ],
187        ];
188    }
189
190    /**
191     * Return an instance by quiz id.
192     *
193     * This method gets data from cache before doing any DB calls.
194     *
195     * @param int $quizid Quiz id.
196     * @return false|\quizaccess_seb\quiz_settings
197     */
198    public static function get_by_quiz_id(int $quizid) {
199        if ($data = self::get_quiz_settings_cache()->get($quizid)) {
200            return new static(0, $data);
201        }
202
203        return self::get_record(['quizid' => $quizid]);
204    }
205
206    /**
207     * Return cached SEB config represented as a string by quiz ID.
208     *
209     * @param int $quizid Quiz id.
210     * @return string|null
211     */
212    public static function get_config_by_quiz_id(int $quizid) : ?string {
213        $config = self::get_config_cache()->get($quizid);
214
215        if ($config !== false) {
216            return $config;
217        }
218
219        $config = null;
220        if ($settings = self::get_by_quiz_id($quizid)) {
221            $config = $settings->get_config();
222            self::get_config_cache()->set($quizid, $config);
223        }
224
225        return $config;
226    }
227
228    /**
229     * Return cached SEB config key by quiz ID.
230     *
231     * @param int $quizid Quiz id.
232     * @return string|null
233     */
234    public static function get_config_key_by_quiz_id(int $quizid) : ?string {
235        $configkey = self::get_config_key_cache()->get($quizid);
236
237        if ($configkey !== false) {
238            return $configkey;
239        }
240
241        $configkey = null;
242        if ($settings = self::get_by_quiz_id($quizid)) {
243            $configkey = $settings->get_config_key();
244            self::get_config_key_cache()->set($quizid, $configkey);
245        }
246
247        return $configkey;
248    }
249
250    /**
251     * Return SEB config key cache instance.
252     *
253     * @return \cache_application
254     */
255    private static function get_config_key_cache() : \cache_application {
256        return \cache::make('quizaccess_seb', 'configkey');
257    }
258
259    /**
260     * Return SEB config cache instance.
261     *
262     * @return \cache_application
263     */
264    private static function get_config_cache() : \cache_application {
265        return \cache::make('quizaccess_seb', 'config');
266    }
267
268    /**
269     * Return quiz settings cache object,
270     *
271     * @return \cache_application
272     */
273    private static function get_quiz_settings_cache() : \cache_application {
274        return \cache::make('quizaccess_seb', 'quizsettings');
275    }
276
277    /**
278     * Adds the new record to the cache.
279     */
280    protected function after_create() {
281        $this->after_save();
282    }
283
284    /**
285     * Updates the cache record.
286     *
287     * @param bool $result
288     */
289    protected function after_update($result) {
290        $this->after_save();
291    }
292
293    /**
294     * Helper method to execute common stuff after create and update.
295     */
296    private function after_save() {
297        self::get_quiz_settings_cache()->set($this->get('quizid'), $this->to_record());
298        self::get_config_cache()->set($this->get('quizid'), $this->config);
299        self::get_config_key_cache()->set($this->get('quizid'), $this->configkey);
300    }
301
302    /**
303     * Removes unnecessary stuff from db.
304     */
305    protected function before_delete() {
306        $key = $this->get('quizid');
307        self::get_quiz_settings_cache()->delete($key);
308        self::get_config_cache()->delete($key);
309        self::get_config_key_cache()->delete($key);
310    }
311
312    /**
313     * Validate the browser exam keys string.
314     *
315     * @param string $keys Newline separated browser exam keys.
316     * @return true|lang_string If there is an error, an error string is returned.
317     */
318    protected function validate_allowedbrowserexamkeys($keys) {
319        $keys = $this->split_keys($keys);
320        foreach ($keys as $i => $key) {
321            if (!preg_match('~^[a-f0-9]{64}$~', $key)) {
322                return new lang_string('allowedbrowserkeyssyntax', 'quizaccess_seb');
323            }
324        }
325        if (count($keys) != count(array_unique($keys))) {
326            return new lang_string('allowedbrowserkeysdistinct', 'quizaccess_seb');
327        }
328        return true;
329    }
330
331    /**
332     * Get the browser exam keys as a pre-split array instead of just as a string.
333     *
334     * @return array
335     */
336    protected function get_allowedbrowserexamkeys() : array {
337        $keysstring = $this->raw_get('allowedbrowserexamkeys');
338        $keysstring = empty($keysstring) ? '' : $keysstring;
339        return $this->split_keys($keysstring);
340    }
341
342    /**
343     * Hook to execute before an update.
344     *
345     * Please note that at this stage the data has already been validated and therefore
346     * any new data being set will not be validated before it is sent to the database.
347     */
348    protected function before_update() {
349        $this->before_save();
350    }
351
352    /**
353     * Hook to execute before a create.
354     *
355     * Please note that at this stage the data has already been validated and therefore
356     * any new data being set will not be validated before it is sent to the database.
357     */
358    protected function before_create() {
359        $this->before_save();
360    }
361
362    /**
363     * As there is no hook for before both create and update, this function is called by both hooks.
364     */
365    private function before_save() {
366        // Set template to 0 if using anything different to template.
367        if ($this->get('requiresafeexambrowser') != settings_provider::USE_SEB_TEMPLATE) {
368            $this->set('templateid', 0);
369        }
370
371        // Process configs to make sure that all data is set correctly.
372        $this->process_configs();
373    }
374
375    /**
376     * Before validate hook.
377     */
378    protected function before_validate() {
379        // Template can't be null.
380        if (is_null($this->get('templateid'))) {
381            $this->set('templateid', 0);
382        }
383    }
384
385    /**
386     * Create or update the config string based on the current quiz settings.
387     */
388    private function process_configs() {
389        switch ($this->get('requiresafeexambrowser')) {
390            case settings_provider::USE_SEB_NO:
391                $this->process_seb_config_no();
392                break;
393
394            case settings_provider::USE_SEB_CONFIG_MANUALLY:
395                $this->process_seb_config_manually();
396                break;
397
398            case settings_provider::USE_SEB_TEMPLATE:
399                $this->process_seb_template();
400                break;
401
402            case settings_provider::USE_SEB_UPLOAD_CONFIG:
403                $this->process_seb_upload_config();
404                break;
405
406            default: // Also settings_provider::USE_SEB_CLIENT_CONFIG.
407                $this->process_seb_client_config();
408        }
409
410        // Generate config key based on given SEB config.
411        if (!empty($this->config)) {
412            $this->configkey = config_key::generate($this->config)->get_hash();
413        } else {
414            $this->configkey = null;
415        }
416    }
417
418    /**
419     * Return SEB config key.
420     *
421     * @return string|null
422     */
423    public function get_config_key() : ?string {
424        $this->process_configs();
425
426        return $this->configkey;
427    }
428
429    /**
430     * Return string representation of the config.
431     *
432     * @return string|null
433     */
434    public function get_config() : ?string {
435        $this->process_configs();
436
437        return $this->config;
438    }
439
440    /**
441     * Case for USE_SEB_NO.
442     */
443    private function process_seb_config_no() {
444        $this->config = null;
445    }
446
447    /**
448     * Case for USE_SEB_CONFIG_MANUALLY. This creates a plist and applies all settings from the posted form, along with
449     * some defaults.
450     */
451    private function process_seb_config_manually() {
452        // If at any point a configuration file has been uploaded and parsed, clear the settings.
453        $this->plist = new property_list();
454
455        $this->process_bool_settings();
456        $this->process_quit_password_settings();
457        $this->process_quit_url_from_settings();
458        $this->process_url_filters();
459        $this->process_required_enforced_settings();
460
461        // One of the requirements for USE_SEB_CONFIG_MANUALLY is setting examSessionClearCookiesOnStart to false.
462        $this->plist->set_or_update_value('examSessionClearCookiesOnStart', new CFBoolean(false));
463        $this->plist->set_or_update_value('allowPreferencesWindow', new CFBoolean(false));
464        $this->config = $this->plist->to_xml();
465    }
466
467    /**
468     * Case for USE_SEB_TEMPLATE. This creates a plist from the template uploaded, then applies the quit password
469     * setting and some defaults.
470     */
471    private function process_seb_template() {
472        $template = template::get_record(['id' => $this->get('templateid')]);
473        $this->plist = new property_list($template->get('content'));
474
475        $this->process_bool_setting('allowuserquitseb');
476        $this->process_quit_password_settings();
477        $this->process_quit_url_from_template_or_config();
478        $this->process_required_enforced_settings();
479
480        $this->config = $this->plist->to_xml();
481    }
482
483    /**
484     * Case for USE_SEB_UPLOAD_CONFIG. This creates a plist from an uploaded configuration file, then applies the quiz
485     * password settings and some defaults.
486     */
487    private function process_seb_upload_config() {
488        $file = settings_provider::get_module_context_sebconfig_file($this->get('cmid'));
489
490        // If there was no file, create an empty plist so the rest of this wont explode.
491        if (empty($file)) {
492            throw new moodle_exception('noconfigfilefound', 'quizaccess_seb', '', $this->get('cmid'));
493        } else {
494            $this->plist = new property_list($file->get_content());
495        }
496
497        $this->process_quit_url_from_template_or_config();
498        $this->process_required_enforced_settings();
499
500        $this->config = $this->plist->to_xml();
501    }
502
503    /**
504     * Case for USE_SEB_CLIENT_CONFIG. This creates an empty plist to remove the config stored.
505     */
506    private function process_seb_client_config() {
507        $this->config = null;
508    }
509
510    /**
511     * Sets or updates some sensible default settings, these are the items 'startURL' and 'sendBrowserExamKey'.
512     */
513    private function process_required_enforced_settings() {
514        global $CFG;
515
516        $quizurl = new moodle_url($CFG->wwwroot . "/mod/quiz/view.php", ['id' => $this->get('cmid')]);
517        $this->plist->set_or_update_value('startURL', new CFString($quizurl->out(true)));
518        $this->plist->set_or_update_value('sendBrowserExamKey', new CFBoolean(true));
519    }
520
521    /**
522     * Use the boolean map to add Moodle boolean setting to config PList.
523     */
524    private function process_bool_settings() {
525        $settings = $this->to_record();
526        $map = $this->get_bool_seb_setting_map();
527        foreach ($settings as $setting => $value) {
528            if (isset($map[$setting])) {
529                $this->process_bool_setting($setting);
530            }
531        }
532    }
533
534    /**
535     * Process provided single bool setting.
536     *
537     * @param string $name Setting name matching one from self::get_bool_seb_setting_map.
538     */
539    private function process_bool_setting(string $name) {
540        $map = $this->get_bool_seb_setting_map();
541
542        if (!isset($map[$name])) {
543            throw new \coding_exception('Provided setting name can not be found in known bool settings');
544        }
545
546        $enabled = $this->raw_get($name) == 1 ? true : false;
547        $this->plist->set_or_update_value($map[$name], new CFBoolean($enabled));
548    }
549
550    /**
551     * Turn hashed quit password and quit link into PList strings and add to config PList.
552     */
553    private function process_quit_password_settings() {
554        $settings = $this->to_record();
555        if (!empty($settings->quitpassword) && is_string($settings->quitpassword)) {
556            // Hash quit password.
557            $hashedpassword = hash('SHA256', $settings->quitpassword);
558            $this->plist->add_element_to_root('hashedQuitPassword', new CFString($hashedpassword));
559        } else if (!is_null($this->plist->get_element_value('hashedQuitPassword'))) {
560            $this->plist->delete_element('hashedQuitPassword');
561        }
562    }
563
564    /**
565     * Sets the quitURL if found in the quiz_settings.
566     */
567    private function process_quit_url_from_settings() {
568        $settings = $this->to_record();
569        if (!empty($settings->linkquitseb) && is_string($settings->linkquitseb)) {
570            $this->plist->set_or_update_value('quitURL', new CFString($settings->linkquitseb));
571        }
572    }
573
574    /**
575     * Sets the quiz_setting's linkquitseb if a quitURL value was found in a template or uploaded config.
576     */
577    private function process_quit_url_from_template_or_config() {
578        // Does the plist (template or config file) have an existing quitURL?
579        $quiturl = $this->plist->get_element_value('quitURL');
580        if (!empty($quiturl)) {
581            $this->set('linkquitseb', $quiturl);
582        }
583    }
584
585    /**
586     * Turn return separated strings for URL filters into a PList array and add to config PList.
587     */
588    private function process_url_filters() {
589        $settings = $this->to_record();
590        // Create rules to each expression provided and add to config.
591        $urlfilterrules = [];
592        // Get all rules separated by newlines and remove empty rules.
593        $expallowed = array_filter(explode(PHP_EOL, $settings->expressionsallowed));
594        $expblocked = array_filter(explode(PHP_EOL, $settings->expressionsblocked));
595        $regallowed = array_filter(explode(PHP_EOL, $settings->regexallowed));
596        $regblocked = array_filter(explode(PHP_EOL, $settings->regexblocked));
597        foreach ($expallowed as $rulestring) {
598            $urlfilterrules[] = $this->create_filter_rule($rulestring, true, false);
599        }
600        foreach ($expblocked as $rulestring) {
601            $urlfilterrules[] = $this->create_filter_rule($rulestring, false, false);
602        }
603        foreach ($regallowed as $rulestring) {
604            $urlfilterrules[] = $this->create_filter_rule($rulestring, true, true);
605        }
606        foreach ($regblocked as $rulestring) {
607            $urlfilterrules[] = $this->create_filter_rule($rulestring, false, true);
608        }
609        $this->plist->add_element_to_root('URLFilterRules', new CFArray($urlfilterrules));
610    }
611
612    /**
613     * Create a CFDictionary represeting a URL filter rule.
614     *
615     * @param string $rulestring The expression to filter with.
616     * @param bool $allowed Allowed or blocked.
617     * @param bool $isregex Regex or simple.
618     * @return CFDictionary A PList dictionary.
619     */
620    private function create_filter_rule(string $rulestring, bool $allowed, bool $isregex) : CFDictionary {
621        $action = $allowed ? 1 : 0;
622        return new CFDictionary([
623                    'action' => new CFNumber($action),
624                    'active' => new CFBoolean(true),
625                    'expression' => new CFString(trim($rulestring)),
626                    'regex' => new CFBoolean($isregex),
627                    ]);
628    }
629
630    /**
631     * Map the settings that are booleans to the Safe Exam Browser config keys.
632     *
633     * @return array Moodle setting as key, SEB setting as value.
634     */
635    private function get_bool_seb_setting_map() : array {
636        return [
637            'activateurlfiltering' => 'URLFilterEnable',
638            'allowspellchecking' => 'allowSpellCheck',
639            'allowreloadinexam' => 'browserWindowAllowReload',
640            'allowuserquitseb' => 'allowQuit',
641            'enableaudiocontrol' => 'audioControlEnabled',
642            'filterembeddedcontent' => 'URLFilterEnableContentFilter',
643            'muteonstartup' => 'audioMute',
644            'showkeyboardlayout' => 'showInputLanguage',
645            'showreloadbutton' => 'showReloadButton',
646            'showsebtaskbar' => 'showTaskBar',
647            'showtime' => 'showTime',
648            'showwificontrol' => 'allowWlan',
649            'userconfirmquit' => 'quitURLConfirm',
650        ];
651    }
652
653    /**
654     * This helper method takes list of browser exam keys in a string and splits it into an array of separate keys.
655     *
656     * @param string|null $keys the allowed keys.
657     * @return array of string, the separate keys.
658     */
659    private function split_keys($keys) : array {
660        $keys = preg_split('~[ \t\n\r,;]+~', $keys, -1, PREG_SPLIT_NO_EMPTY);
661        foreach ($keys as $i => $key) {
662            $keys[$i] = strtolower($key);
663        }
664        return $keys;
665    }
666}
667