]> git.mxchange.org Git - friendica.git/blob - HTML/LinkTypes.php
wrapping up 2019.12
[friendica.git] / HTML / LinkTypes.php
1 <?php
2
3 /**
4  * Validates a rel/rev link attribute against a directive of allowed values
5  * @note We cannot use Enum because link types allow multiple
6  *       values.
7  * @note Assumes link types are ASCII text
8  */
9 class HTMLPurifier_AttrDef_HTML_LinkTypes extends HTMLPurifier_AttrDef
10 {
11
12     /** Name config attribute to pull. */
13     protected $name;
14
15     public function __construct($name) {
16         $configLookup = array(
17             'rel' => 'AllowedRel',
18             'rev' => 'AllowedRev'
19         );
20         if (!isset($configLookup[$name])) {
21             trigger_error('Unrecognized attribute name for link '.
22                 'relationship.', E_USER_ERROR);
23             return;
24         }
25         $this->name = $configLookup[$name];
26     }
27
28     public function validate($string, $config, $context) {
29
30         $allowed = $config->get('Attr.' . $this->name);
31         if (empty($allowed)) return false;
32
33         $string = $this->parseCDATA($string);
34         $parts = explode(' ', $string);
35
36         // lookup to prevent duplicates
37         $ret_lookup = array();
38         foreach ($parts as $part) {
39             $part = strtolower(trim($part));
40             if (!isset($allowed[$part])) continue;
41             $ret_lookup[$part] = true;
42         }
43
44         if (empty($ret_lookup)) return false;
45         $string = implode(' ', array_keys($ret_lookup));
46
47         return $string;
48
49     }
50
51 }
52
53 // vim: et sw=4 sts=4