Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * HTML  tidy text filter.
  20   *
  21   * @package    filter
  22   * @subpackage tiny
  23   * @copyright  2004 Hannes Gassert <hannes at mediagonal dot ch>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  // This class looks for text including markup and
  30  // applies tidy's repair function to it.
  31  // Tidy is a HTML clean and
  32  // repair utility, which is currently available for PHP 4.3.x and PHP 5 as a
  33  // PECL extension from http://pecl.php.net/package/tidy, in PHP 5 you need only
  34  // to compile using the --with-tidy option.
  35  // If you don't have the tidy extension installed or don't know, you can enable
  36  // or disable this filter, it just won't have any effect.
  37  // If you want to know what you can set in $tidyoptions and what their default
  38  // values are, see http://php.net/manual/en/function.tidy-get-config.php.
  39  
  40  class filter_tidy extends moodle_text_filter {
  41      function filter($text, array $options = array()) {
  42  
  43      /// Configuration for tidy. Feel free to tune for your needs, e.g. to allow
  44      /// proprietary markup.
  45          $tidyoptions = array(
  46                   'output-xhtml' => true,
  47                   'show-body-only' => true,
  48                   'tidy-mark' => false,
  49                   'drop-proprietary-attributes' => true,
  50                   'drop-font-tags' => true,
  51                   'drop-empty-paras' => true,
  52                   'indent' => true,
  53                   'quiet' => true,
  54          );
  55  
  56      /// Do a quick check using strpos to avoid unnecessary work
  57          if (strpos($text, '<') === false) {
  58              return $text;
  59          }
  60  
  61  
  62      /// If enabled: run tidy over the entire string
  63          if (function_exists('tidy_repair_string')){
  64              $text = tidy_repair_string($text, $tidyoptions, 'utf8');
  65          }
  66  
  67          return $text;
  68      }
  69  }
  70