Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * Class for generating and representing a Safe Exam Browser config key.
  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  
  26  namespace quizaccess_seb;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class for generating and representing a Safe Exam Browser config key.
  32   *
  33   * @copyright  2020 Catalyst IT
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class config_key {
  37  
  38      /** @var string $hash The Config Key hash. */
  39      private $hash;
  40  
  41      /**
  42       * The config_key constructor.
  43       *
  44       * @param string $hash The Config Key hash.
  45       */
  46      public function __construct(string $hash) {
  47          $this->hash = $hash;
  48      }
  49  
  50      /**
  51       * Generate the Config Key hash from an SEB Config XML string.
  52       *
  53       * See  https://safeexambrowser.org/developer/seb-config-key.html for more information about the process.
  54       *
  55       * @param string $xml A PList XML string, representing SEB config.
  56       * @return config_key This config key instance.
  57       */
  58      public static function generate(string $xml) : config_key {
  59          if (!empty($xml) && !helper::is_valid_seb_config($xml)) {
  60              throw new \invalid_parameter_exception('Invalid a PList XML string, representing SEB config');
  61          }
  62  
  63          $plist = new property_list($xml);
  64          // Remove the key "originatorVersion" first. This key is exempted from the SEB-JSON hash (it's a special key
  65          // which doesn't have any functionality, it's just meta data indicating which SEB version saved the config file).
  66          $plist->delete_element('originatorVersion');
  67          // Convert the plist XML of a decrypted/unencrypted SEB config file to a ordered JSON-like "SEB-JSON" object.
  68          $hash = $plist->to_json();
  69          // Hash the JSON with SHA256. Defaults to required Base16 encoding.
  70          $hash = hash('SHA256', $hash);
  71  
  72          return new self($hash);
  73      }
  74  
  75      /**
  76       * Get the Config Key hash.
  77       *
  78       * @return string The Config Key hash
  79       */
  80      public function get_hash() : string {
  81          return $this->hash;
  82      }
  83  }