| [21] | 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * @todo Unit test |
|---|
| 5 | */ |
|---|
| 6 | class HTMLPurifier_ContentSets |
|---|
| 7 | { |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * List of content set strings (pipe seperators) indexed by name. |
|---|
| 11 | */ |
|---|
| 12 | public $info = array(); |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * List of content set lookups (element => true) indexed by name. |
|---|
| 16 | * @note This is in HTMLPurifier_HTMLDefinition->info_content_sets |
|---|
| 17 | */ |
|---|
| 18 | public $lookup = array(); |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Synchronized list of defined content sets (keys of info) |
|---|
| 22 | */ |
|---|
| 23 | protected $keys = array(); |
|---|
| 24 | /** |
|---|
| 25 | * Synchronized list of defined content values (values of info) |
|---|
| 26 | */ |
|---|
| 27 | protected $values = array(); |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * Merges in module's content sets, expands identifiers in the content |
|---|
| 31 | * sets and populates the keys, values and lookup member variables. |
|---|
| 32 | * @param $modules List of HTMLPurifier_HTMLModule |
|---|
| 33 | */ |
|---|
| 34 | public function __construct($modules) { |
|---|
| 35 | if (!is_array($modules)) $modules = array($modules); |
|---|
| 36 | // populate content_sets based on module hints |
|---|
| 37 | // sorry, no way of overloading |
|---|
| 38 | foreach ($modules as $module_i => $module) { |
|---|
| 39 | foreach ($module->content_sets as $key => $value) { |
|---|
| 40 | if (isset($this->info[$key])) { |
|---|
| 41 | // add it into the existing content set |
|---|
| 42 | $this->info[$key] = $this->info[$key] . ' | ' . $value; |
|---|
| 43 | } else { |
|---|
| 44 | $this->info[$key] = $value; |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | // perform content_set expansions |
|---|
| 49 | $this->keys = array_keys($this->info); |
|---|
| 50 | foreach ($this->info as $i => $set) { |
|---|
| 51 | // only performed once, so infinite recursion is not |
|---|
| 52 | // a problem |
|---|
| 53 | $this->info[$i] = |
|---|
| 54 | str_replace( |
|---|
| 55 | $this->keys, |
|---|
| 56 | // must be recalculated each time due to |
|---|
| 57 | // changing substitutions |
|---|
| 58 | array_values($this->info), |
|---|
| 59 | $set); |
|---|
| 60 | } |
|---|
| 61 | $this->values = array_values($this->info); |
|---|
| 62 | |
|---|
| 63 | // generate lookup tables |
|---|
| 64 | foreach ($this->info as $name => $set) { |
|---|
| 65 | $this->lookup[$name] = $this->convertToLookup($set); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Accepts a definition; generates and assigns a ChildDef for it |
|---|
| 71 | * @param $def HTMLPurifier_ElementDef reference |
|---|
| 72 | * @param $module Module that defined the ElementDef |
|---|
| 73 | */ |
|---|
| 74 | public function generateChildDef(&$def, $module) { |
|---|
| 75 | if (!empty($def->child)) return; // already done! |
|---|
| 76 | $content_model = $def->content_model; |
|---|
| 77 | if (is_string($content_model)) { |
|---|
| 78 | $def->content_model = str_replace( |
|---|
| 79 | $this->keys, $this->values, $content_model); |
|---|
| 80 | } |
|---|
| 81 | $def->child = $this->getChildDef($def, $module); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Instantiates a ChildDef based on content_model and content_model_type |
|---|
| 86 | * member variables in HTMLPurifier_ElementDef |
|---|
| 87 | * @note This will also defer to modules for custom HTMLPurifier_ChildDef |
|---|
| 88 | * subclasses that need content set expansion |
|---|
| 89 | * @param $def HTMLPurifier_ElementDef to have ChildDef extracted |
|---|
| 90 | * @return HTMLPurifier_ChildDef corresponding to ElementDef |
|---|
| 91 | */ |
|---|
| 92 | public function getChildDef($def, $module) { |
|---|
| 93 | $value = $def->content_model; |
|---|
| 94 | if (is_object($value)) { |
|---|
| 95 | trigger_error( |
|---|
| 96 | 'Literal object child definitions should be stored in '. |
|---|
| 97 | 'ElementDef->child not ElementDef->content_model', |
|---|
| 98 | E_USER_NOTICE |
|---|
| 99 | ); |
|---|
| 100 | return $value; |
|---|
| 101 | } |
|---|
| 102 | switch ($def->content_model_type) { |
|---|
| 103 | case 'required': |
|---|
| 104 | return new HTMLPurifier_ChildDef_Required($value); |
|---|
| 105 | case 'optional': |
|---|
| 106 | return new HTMLPurifier_ChildDef_Optional($value); |
|---|
| 107 | case 'empty': |
|---|
| 108 | return new HTMLPurifier_ChildDef_Empty(); |
|---|
| 109 | case 'custom': |
|---|
| 110 | return new HTMLPurifier_ChildDef_Custom($value); |
|---|
| 111 | } |
|---|
| 112 | // defer to its module |
|---|
| 113 | $return = false; |
|---|
| 114 | if ($module->defines_child_def) { // save a func call |
|---|
| 115 | $return = $module->getChildDef($def); |
|---|
| 116 | } |
|---|
| 117 | if ($return !== false) return $return; |
|---|
| 118 | // error-out |
|---|
| 119 | trigger_error( |
|---|
| 120 | 'Could not determine which ChildDef class to instantiate', |
|---|
| 121 | E_USER_ERROR |
|---|
| 122 | ); |
|---|
| 123 | return false; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * Converts a string list of elements separated by pipes into |
|---|
| 128 | * a lookup array. |
|---|
| 129 | * @param $string List of elements |
|---|
| 130 | * @return Lookup array of elements |
|---|
| 131 | */ |
|---|
| 132 | protected function convertToLookup($string) { |
|---|
| 133 | $array = explode('|', str_replace(' ', '', $string)); |
|---|
| 134 | $ret = array(); |
|---|
| 135 | foreach ($array as $i => $k) { |
|---|
| 136 | $ret[$k] = true; |
|---|
| 137 | } |
|---|
| 138 | return $ret; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | |
|---|