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