Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  // Enum = Enumerated

   4  /**

   5   * Validates a keyword against a list of valid values.

   6   * @warning The case-insensitive compare of this function uses PHP's

   7   *          built-in strtolower and ctype_lower functions, which may

   8   *          cause problems with international comparisons

   9   */
  10  class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
  11  {
  12  
  13      /**

  14       * Lookup table of valid values.

  15       * @type array

  16       * @todo Make protected

  17       */
  18      public $valid_values = array();
  19  
  20      /**

  21       * Bool indicating whether or not enumeration is case sensitive.

  22       * @note In general this is always case insensitive.

  23       */
  24      protected $case_sensitive = false; // values according to W3C spec

  25  
  26      /**

  27       * @param array $valid_values List of valid values

  28       * @param bool $case_sensitive Whether or not case sensitive

  29       */
  30      public function __construct($valid_values = array(), $case_sensitive = false)
  31      {
  32          $this->valid_values = array_flip($valid_values);
  33          $this->case_sensitive = $case_sensitive;
  34      }
  35  
  36      /**

  37       * @param string $string

  38       * @param HTMLPurifier_Config $config

  39       * @param HTMLPurifier_Context $context

  40       * @return bool|string

  41       */
  42      public function validate($string, $config, $context)
  43      {
  44          $string = trim($string);
  45          if (!$this->case_sensitive) {
  46              // we may want to do full case-insensitive libraries

  47              $string = ctype_lower($string) ? $string : strtolower($string);
  48          }
  49          $result = isset($this->valid_values[$string]);
  50  
  51          return $result ? $string : false;
  52      }
  53  
  54      /**

  55       * @param string $string In form of comma-delimited list of case-insensitive

  56       *      valid values. Example: "foo,bar,baz". Prepend "s:" to make

  57       *      case sensitive

  58       * @return HTMLPurifier_AttrDef_Enum

  59       */
  60      public function make($string)
  61      {
  62          if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
  63              $string = substr($string, 2);
  64              $sensitive = true;
  65          } else {
  66              $sensitive = false;
  67          }
  68          $values = explode(',', $string);
  69          return new HTMLPurifier_AttrDef_Enum($values, $sensitive);
  70      }
  71  }
  72  
  73  // vim: et sw=4 sts=4