File indexing completed on 2024-05-26 06:02:31

0001 <?php
0002 
0003 /**
0004  * Validates a rel/rev link attribute against a directive of allowed values
0005  * @note We cannot use Enum because link types allow multiple
0006  *       values.
0007  * @note Assumes link types are ASCII text
0008  */
0009 class HTMLPurifier_AttrDef_HTML_LinkTypes extends HTMLPurifier_AttrDef
0010 {
0011 
0012     /**
0013      * Name config attribute to pull.
0014      * @type string
0015      */
0016     protected $name;
0017 
0018     /**
0019      * @param string $name
0020      */
0021     public function __construct($name)
0022     {
0023         $configLookup = array(
0024             'rel' => 'AllowedRel',
0025             'rev' => 'AllowedRev'
0026         );
0027         if (!isset($configLookup[$name])) {
0028             trigger_error(
0029                 'Unrecognized attribute name for link ' .
0030                 'relationship.',
0031                 E_USER_ERROR
0032             );
0033             return;
0034         }
0035         $this->name = $configLookup[$name];
0036     }
0037 
0038     /**
0039      * @param string $string
0040      * @param HTMLPurifier_Config $config
0041      * @param HTMLPurifier_Context $context
0042      * @return bool|string
0043      */
0044     public function validate($string, $config, $context)
0045     {
0046         $allowed = $config->get('Attr.' . $this->name);
0047         if (empty($allowed)) {
0048             return false;
0049         }
0050 
0051         $string = $this->parseCDATA($string);
0052         $parts = explode(' ', $string);
0053 
0054         // lookup to prevent duplicates
0055         $ret_lookup = array();
0056         foreach ($parts as $part) {
0057             $part = strtolower(trim($part));
0058             if (!isset($allowed[$part])) {
0059                 continue;
0060             }
0061             $ret_lookup[$part] = true;
0062         }
0063 
0064         if (empty($ret_lookup)) {
0065             return false;
0066         }
0067         $string = implode(' ', array_keys($ret_lookup));
0068         return $string;
0069     }
0070 }
0071 
0072 // vim: et sw=4 sts=4