Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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   * Check API manager
  19   *
  20   * @package    core
  21   * @category   check
  22   * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\check;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Check API manager
  32   *
  33   * @package    core
  34   * @category   check
  35   * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class manager {
  39  
  40      /**
  41       * The list of valid check types
  42       */
  43      public const TYPES = ['status', 'security', 'performance'];
  44  
  45      /**
  46       * Return all status checks
  47       *
  48       * @param string $type of checks to fetch
  49       * @return array of check objects
  50       */
  51      public static function get_checks(string $type): array {
  52          if (!in_array($type, self::TYPES)) {
  53              throw new \moodle_exception("Invalid check type '$type'");
  54          }
  55          $method = 'get_' . $type . '_checks';
  56          $checks = self::$method();
  57          return $checks;
  58      }
  59  
  60      /**
  61       * Return all performance checks
  62       *
  63       * @return array of check objects
  64       */
  65      static public function get_performance_checks() : array {
  66          $checks = [
  67              new performance\designermode(),
  68              new performance\cachejs(),
  69              new performance\debugging(),
  70              new performance\backups(),
  71              new performance\stats(),
  72              new performance\dbschema(),
  73          ];
  74  
  75          // Any plugin can add status checks to this report by implementing a callback
  76          // <component>_status_checks() which returns a check object.
  77          $morechecks = get_plugins_with_function('performance_checks', 'lib.php');
  78          foreach ($morechecks as $plugintype => $plugins) {
  79              foreach ($plugins as $plugin => $pluginfunction) {
  80                  $result = $pluginfunction();
  81                  foreach ($result as $check) {
  82                      $check->set_component($plugintype . '_' . $plugin);
  83                      $checks[] = $check;
  84                  }
  85              }
  86          }
  87          return $checks;
  88      }
  89  
  90      /**
  91       * Return all status checks
  92       *
  93       * @return array of check objects
  94       */
  95      public static function get_status_checks(): array {
  96          $checks = [
  97              new environment\environment(),
  98              new environment\upgradecheck(),
  99              new environment\antivirus(),
 100          ];
 101  
 102          // Any plugin can add status checks to this report by implementing a callback
 103          // <component>_status_checks() which returns a check object.
 104          $morechecks = get_plugins_with_function('status_checks', 'lib.php');
 105          foreach ($morechecks as $plugintype => $plugins) {
 106              foreach ($plugins as $plugin => $pluginfunction) {
 107                  $result = $pluginfunction();
 108                  foreach ($result as $check) {
 109                      $check->set_component($plugintype . '_' . $plugin);
 110                      $checks[] = $check;
 111                  }
 112              }
 113          }
 114          return $checks;
 115      }
 116  
 117      /**
 118       * Return all security checks
 119       *
 120       * @return array of check objects
 121       */
 122      public static function get_security_checks(): array {
 123          $checks = [
 124              new environment\displayerrors(),
 125              new environment\unsecuredataroot(),
 126              new environment\publicpaths(),
 127              new environment\configrw(),
 128              new environment\preventexecpath(),
 129              new security\embed(),
 130              new security\openprofiles(),
 131              new security\crawlers(),
 132              new security\passwordpolicy(),
 133              new security\emailchangeconfirmation(),
 134              new security\webcron(),
 135              new http\cookiesecure(),
 136              new access\riskadmin(),
 137              new access\riskxss(),
 138              new access\riskbackup(),
 139              new access\defaultuserrole(),
 140              new access\guestrole(),
 141              new access\frontpagerole(),
 142          ];
 143          // Any plugin can add security checks to this report by implementing a callback
 144          // <component>_security_checks() which returns a check object.
 145          $morechecks = get_plugins_with_function('security_checks', 'lib.php');
 146          foreach ($morechecks as $plugintype => $plugins) {
 147              foreach ($plugins as $plugin => $pluginfunction) {
 148                  $result = $pluginfunction();
 149                  foreach ($result as $check) {
 150                      $check->set_component($plugintype . '_' . $plugin);
 151                      $checks[] = $check;
 152                  }
 153              }
 154          }
 155          return $checks;
 156      }
 157  }