1: <?php
2:
3: 4: 5:
6: class HTMLPurifier_ContentSets
7: {
8:
9: 10: 11:
12: public $info = array();
13:
14: 15: 16: 17:
18: public $lookup = array();
19:
20: 21: 22:
23: protected $keys = array();
24: 25: 26:
27: protected $values = array();
28:
29: 30: 31: 32: 33:
34: public function __construct($modules) {
35: if (!is_array($modules)) $modules = array($modules);
36:
37:
38: foreach ($modules as $module_i => $module) {
39: foreach ($module->content_sets as $key => $value) {
40: $temp = $this->convertToLookup($value);
41: if (isset($this->lookup[$key])) {
42:
43: $this->lookup[$key] = array_merge($this->lookup[$key], $temp);
44: } else {
45: $this->lookup[$key] = $temp;
46: }
47: }
48: }
49: $old_lookup = false;
50: while ($old_lookup !== $this->lookup) {
51: $old_lookup = $this->lookup;
52: foreach ($this->lookup as $i => $set) {
53: $add = array();
54: foreach ($set as $element => $x) {
55: if (isset($this->lookup[$element])) {
56: $add += $this->lookup[$element];
57: unset($this->lookup[$i][$element]);
58: }
59: }
60: $this->lookup[$i] += $add;
61: }
62: }
63:
64: foreach ($this->lookup as $key => $lookup) {
65: $this->info[$key] = implode(' | ', array_keys($lookup));
66: }
67: $this->keys = array_keys($this->info);
68: $this->values = array_values($this->info);
69: }
70:
71: 72: 73: 74: 75:
76: public function generateChildDef(&$def, $module) {
77: if (!empty($def->child)) return;
78: $content_model = $def->content_model;
79: if (is_string($content_model)) {
80:
81: $def->content_model = preg_replace_callback(
82: '/\b(' . implode('|', $this->keys) . ')\b/',
83: array($this, 'generateChildDefCallback'),
84: $content_model
85: );
86:
87:
88: }
89: $def->child = $this->getChildDef($def, $module);
90: }
91:
92: public function generateChildDefCallback($matches) {
93: return $this->info[$matches[0]];
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103:
104: public function getChildDef($def, $module) {
105: $value = $def->content_model;
106: if (is_object($value)) {
107: trigger_error(
108: 'Literal object child definitions should be stored in '.
109: 'ElementDef->child not ElementDef->content_model',
110: E_USER_NOTICE
111: );
112: return $value;
113: }
114: switch ($def->content_model_type) {
115: case 'required':
116: return new HTMLPurifier_ChildDef_Required($value);
117: case 'optional':
118: return new HTMLPurifier_ChildDef_Optional($value);
119: case 'empty':
120: return new HTMLPurifier_ChildDef_Empty();
121: case 'custom':
122: return new HTMLPurifier_ChildDef_Custom($value);
123: }
124:
125: $return = false;
126: if ($module->defines_child_def) {
127: $return = $module->getChildDef($def);
128: }
129: if ($return !== false) return $return;
130:
131: trigger_error(
132: 'Could not determine which ChildDef class to instantiate',
133: E_USER_ERROR
134: );
135: return false;
136: }
137:
138: 139: 140: 141: 142: 143:
144: protected function convertToLookup($string) {
145: $array = explode('|', str_replace(' ', '', $string));
146: $ret = array();
147: foreach ($array as $i => $k) {
148: $ret[$k] = true;
149: }
150: return $ret;
151: }
152:
153: }
154:
155:
156: