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] [Versions 401 and 402] [Versions 401 and 403]

   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   * Defines classes used for plugin info.
  19   *
  20   * @package    core_antivirus
  21   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  use moodle_url, part_of_admin_tree, admin_settingpage;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  
  31  /**
  32   * Class for Antiviruses
  33   *
  34   * @package    core_antivirus
  35   * @copyright  2015 Ruslan Kabalin, Lancaster University.
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class antivirus extends base {
  39      /**
  40       * Finds all enabled plugins, the result may include missing plugins.
  41       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  42       */
  43      public static function get_enabled_plugins() {
  44          global $CFG;
  45  
  46          if (empty($CFG->antiviruses)) {
  47              return array();
  48          }
  49  
  50          $enabled = array();
  51          foreach (explode(',', $CFG->antiviruses) as $antivirus) {
  52              $enabled[$antivirus] = $antivirus;
  53          }
  54  
  55          return $enabled;
  56      }
  57  
  58      public static function enable_plugin(string $pluginname, int $enabled): bool {
  59          global $CFG;
  60  
  61          $haschanged = false;
  62          $plugins = [];
  63          if (!empty($CFG->antiviruses)) {
  64              $plugins = array_flip(explode(',', $CFG->antiviruses));
  65          }
  66          // Only set visibility if it's different from the current value.
  67          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  68              $plugins[$pluginname] = $pluginname;
  69              $haschanged = true;
  70          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  71              unset($plugins[$pluginname]);
  72              $haschanged = true;
  73          }
  74  
  75          if ($haschanged) {
  76              $new = implode(',', array_flip($plugins));
  77              add_to_config_log('antiviruses', $CFG->antiviruses, $new, 'core');
  78              set_config('antiviruses', $new);
  79              // Reset caches.
  80              \core_plugin_manager::reset_caches();
  81          }
  82  
  83          return $haschanged;
  84      }
  85  
  86      /**
  87       * Return the node name to use in admin settings menu for this plugin.
  88       *
  89       * @return string node name
  90       */
  91      public function get_settings_section_name() {
  92          return 'antivirussettings' . $this->name;
  93      }
  94  
  95      /**
  96       * Loads plugin settings to the settings tree
  97       *
  98       * This function usually includes settings.php file in plugins folder.
  99       * Alternatively it can create a link to some settings page (instance of admin_externalpage)
 100       *
 101       * @param \part_of_admin_tree $adminroot
 102       * @param string $parentnodename
 103       * @param bool $hassiteconfig whether the current user has moodle/site:config capability
 104       */
 105      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 106          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 107          $ADMIN = $adminroot; // May be used in settings.php.
 108          $plugininfo = $this; // Also can be used inside settings.php.
 109          $antivirus = $this;  // Also can be used inside settings.php.
 110  
 111          if (!$this->is_installed_and_upgraded()) {
 112              return;
 113          }
 114  
 115          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 116              return;
 117          }
 118  
 119          $section = $this->get_settings_section_name();
 120  
 121          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 122          include($this->full_path('settings.php')); // This may also set $settings to null.
 123  
 124          if ($settings) {
 125              $ADMIN->add($parentnodename, $settings);
 126          }
 127      }
 128  
 129      /**
 130       * Clamav antivirus can not be uninstalled.
 131       */
 132      public function is_uninstall_allowed() {
 133          if ($this->name === 'clamav') {
 134              return false;
 135          } else {
 136              return true;
 137          }
 138      }
 139  
 140      /**
 141       * Return URL used for management of plugins of this type.
 142       * @return moodle_url
 143       */
 144      public static function get_manage_url() {
 145          return new moodle_url('/admin/settings.php', array('section' => 'manageantiviruses'));
 146      }
 147  
 148      /**
 149       * Pre-uninstall hook.
 150       */
 151      public function uninstall_cleanup() {
 152          global $CFG;
 153  
 154          if (!empty($CFG->antiviruses)) {
 155              $antiviruses = explode(',', $CFG->antiviruses);
 156              $antiviruses = array_unique($antiviruses);
 157          } else {
 158              $antiviruses = array();
 159          }
 160          if (($key = array_search($this->name, $antiviruses)) !== false) {
 161              unset($antiviruses[$key]);
 162              set_config('antiviruses', implode(',', $antiviruses));
 163          }
 164          parent::uninstall_cleanup();
 165      }
 166  }