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.

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