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]

   1  <?php
   2  
   3  /**
   4   * Validates shorthand CSS property background.
   5   * @warning Does not support url tokens that have internal spaces.
   6   */
   7  class HTMLPurifier_AttrDef_CSS_Background extends HTMLPurifier_AttrDef
   8  {
   9  
  10      /**
  11       * Local copy of component validators.
  12       * @type HTMLPurifier_AttrDef[]
  13       * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
  14       */
  15      protected $info;
  16  
  17      /**
  18       * @param HTMLPurifier_Config $config
  19       */
  20      public function __construct($config)
  21      {
  22          $def = $config->getCSSDefinition();
  23          $this->info['background-color'] = $def->info['background-color'];
  24          $this->info['background-image'] = $def->info['background-image'];
  25          $this->info['background-repeat'] = $def->info['background-repeat'];
  26          $this->info['background-attachment'] = $def->info['background-attachment'];
  27          $this->info['background-position'] = $def->info['background-position'];
  28          $this->info['background-size'] = $def->info['background-size'];
  29      }
  30  
  31      /**
  32       * @param string $string
  33       * @param HTMLPurifier_Config $config
  34       * @param HTMLPurifier_Context $context
  35       * @return bool|string
  36       */
  37      public function validate($string, $config, $context)
  38      {
  39          // regular pre-processing
  40          $string = $this->parseCDATA($string);
  41          if ($string === '') {
  42              return false;
  43          }
  44  
  45          // munge rgb() decl if necessary
  46          $string = $this->mungeRgb($string);
  47  
  48          // assumes URI doesn't have spaces in it
  49          $bits = explode(' ', $string); // bits to process
  50  
  51          $caught = array();
  52          $caught['color'] = false;
  53          $caught['image'] = false;
  54          $caught['repeat'] = false;
  55          $caught['attachment'] = false;
  56          $caught['position'] = false;
  57          $caught['size'] = false;
  58  
  59          $i = 0; // number of catches
  60  
  61          foreach ($bits as $bit) {
  62              if ($bit === '') {
  63                  continue;
  64              }
  65              foreach ($caught as $key => $status) {
  66                  if ($key != 'position') {
  67                      if ($status !== false) {
  68                          continue;
  69                      }
  70                      $r = $this->info['background-' . $key]->validate($bit, $config, $context);
  71                  } else {
  72                      $r = $bit;
  73                  }
  74                  if ($r === false) {
  75                      continue;
  76                  }
  77                  if ($key == 'position') {
  78                      if ($caught[$key] === false) {
  79                          $caught[$key] = '';
  80                      }
  81                      $caught[$key] .= $r . ' ';
  82                  } else {
  83                      $caught[$key] = $r;
  84                  }
  85                  $i++;
  86                  break;
  87              }
  88          }
  89  
  90          if (!$i) {
  91              return false;
  92          }
  93          if ($caught['position'] !== false) {
  94              $caught['position'] = $this->info['background-position']->
  95                  validate($caught['position'], $config, $context);
  96          }
  97  
  98          $ret = array();
  99          foreach ($caught as $value) {
 100              if ($value === false) {
 101                  continue;
 102              }
 103              $ret[] = $value;
 104          }
 105  
 106          if (empty($ret)) {
 107              return false;
 108          }
 109          return implode(' ', $ret);
 110      }
 111  }
 112  
 113  // vim: et sw=4 sts=4