Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 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
  21   * @copyright  2011 David Mudrak <david@moodle.com>
  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, admin_externalpage;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class for text filters
  32   */
  33  class filter extends base {
  34  
  35      public function init_display_name() {
  36          if (!get_string_manager()->string_exists('filtername', $this->component)) {
  37              $this->displayname = '[filtername,' . $this->component . ']';
  38          } else {
  39              $this->displayname = get_string('filtername', $this->component);
  40          }
  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 $DB, $CFG;
  49          require_once("$CFG->libdir/filterlib.php");
  50  
  51          $enabled = array();
  52          $filters = $DB->get_records_select('filter_active', "active <> :disabled AND contextid = :contextid", array(
  53              'disabled' => TEXTFILTER_DISABLED, 'contextid' => \context_system::instance()->id), 'filter ASC', 'id, filter');
  54          foreach ($filters as $filter) {
  55              $enabled[$filter->filter] = $filter->filter;
  56          }
  57  
  58          return $enabled;
  59      }
  60  
  61      /**
  62       * Enable or disable a plugin.
  63       * When possible, the change will be stored into the config_log table, to let admins check when/who has modified it.
  64       *
  65       * @param string $pluginname The plugin name to enable/disable.
  66       * @param int $enabled Whether the pluginname should be TEXTFILTER_ON, TEXTFILTER_OFF or TEXTFILTER_DISABLED.
  67       *
  68       * @return bool It always return true because we don't know if the value has changed or not. That way, we guarantee any action
  69       * required if it's changed will be executed.
  70       */
  71      public static function enable_plugin(string $pluginname, int $enabled): bool {
  72          global $CFG;
  73          require_once("$CFG->libdir/filterlib.php");
  74          require_once("$CFG->libdir/weblib.php");
  75  
  76          filter_set_global_state($pluginname, $enabled);
  77          if ($enabled == TEXTFILTER_DISABLED) {
  78              filter_set_applies_to_strings($pluginname, false);
  79          }
  80  
  81          reset_text_filters_cache();
  82          \core_plugin_manager::reset_caches();
  83  
  84          return true;
  85      }
  86  
  87      /**
  88       * Returns current status for a pluginname.
  89       *
  90       * Filters have different values for enabled/disabled plugins so the current value needs to be calculated in a
  91       * different way than the default method in the base class.
  92       *
  93       * @param string $pluginname The plugin name to check.
  94       * @return int The current status (enabled, disabled...) of $pluginname.
  95       */
  96      public static function get_enabled_plugin(string $pluginname): int {
  97          global $DB, $CFG;
  98          require_once("$CFG->libdir/filterlib.php");
  99  
 100          $conditions = ['filter' => $pluginname, 'contextid' => \context_system::instance()->id];
 101          $record = $DB->get_record('filter_active', $conditions, 'active');
 102  
 103          return $record ? (int) $record->active : TEXTFILTER_DISABLED;
 104      }
 105  
 106      public function get_settings_section_name() {
 107          return 'filtersetting' . $this->name;
 108      }
 109  
 110      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 111          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 112          $ADMIN = $adminroot; // May be used in settings.php.
 113          $plugininfo = $this; // Also can be used inside settings.php.
 114          $filter = $this;     // Also can be used inside settings.php.
 115  
 116          if (!$this->is_installed_and_upgraded()) {
 117              return;
 118          }
 119  
 120          if (!$hassiteconfig) {
 121              return;
 122          }
 123          if (file_exists($this->full_path('settings.php'))) {
 124              $fullpath = $this->full_path('settings.php');
 125          } else if (file_exists($this->full_path('filtersettings.php'))) {
 126              $fullpath = $this->full_path('filtersettings.php');
 127          } else {
 128              return;
 129          }
 130  
 131          $section = $this->get_settings_section_name();
 132          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 133          include($fullpath); // This may also set $settings to null.
 134  
 135          if ($settings) {
 136              $ADMIN->add($parentnodename, $settings);
 137          }
 138      }
 139  
 140      public function is_uninstall_allowed() {
 141          return true;
 142      }
 143  
 144      /**
 145       * Return URL used for management of plugins of this type.
 146       * @return moodle_url
 147       */
 148      public static function get_manage_url() {
 149          return new moodle_url('/admin/filters.php');
 150      }
 151  
 152      /**
 153       * Pre-uninstall hook.
 154       *
 155       * This is intended for disabling of plugin, some DB table purging, etc.
 156       *
 157       * NOTE: to be called from uninstall_plugin() only.
 158       * @private
 159       */
 160      public function uninstall_cleanup() {
 161          global $DB, $CFG;
 162  
 163          $DB->delete_records('filter_active', array('filter' => $this->name));
 164          $DB->delete_records('filter_config', array('filter' => $this->name));
 165  
 166          if (empty($CFG->filterall)) {
 167              $stringfilters = array();
 168          } else if (!empty($CFG->stringfilters)) {
 169              $stringfilters = explode(',', $CFG->stringfilters);
 170              $stringfilters = array_combine($stringfilters, $stringfilters);
 171          } else {
 172              $stringfilters = array();
 173          }
 174  
 175          unset($stringfilters[$this->name]);
 176  
 177          set_config('stringfilters', implode(',', $stringfilters));
 178          set_config('filterall', !empty($stringfilters));
 179  
 180          parent::uninstall_cleanup();
 181      }
 182  }