@@ -31,197 +31,197 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef |
| 33 | 33 | { |
| 34 | - public $allow_empty = false; |
|
| 35 | - public $type = 'table'; |
|
| 36 | - public $elements = array('tr' => true, 'tbody' => true, 'thead' => true, |
|
| 37 | - 'tfoot' => true, 'caption' => true, 'colgroup' => true, 'col' => true); |
|
| 38 | - public function __construct() {} |
|
| 39 | - public function validateChildren($tokens_of_children, $config, $context) { |
|
| 40 | - if (empty($tokens_of_children)) return false; |
|
| 41 | - |
|
| 42 | - // this ensures that the loop gets run one last time before closing |
|
| 43 | - // up. It's a little bit of a hack, but it works! Just make sure you |
|
| 44 | - // get rid of the token later. |
|
| 45 | - $tokens_of_children[] = false; |
|
| 46 | - |
|
| 47 | - // only one of these elements is allowed in a table |
|
| 48 | - $caption = false; |
|
| 49 | - $thead = false; |
|
| 50 | - $tfoot = false; |
|
| 51 | - |
|
| 52 | - // as many of these as you want |
|
| 53 | - $cols = array(); |
|
| 54 | - $content = array(); |
|
| 55 | - |
|
| 56 | - $nesting = 0; // current depth so we can determine nodes |
|
| 57 | - $is_collecting = false; // are we globbing together tokens to package |
|
| 58 | - // into one of the collectors? |
|
| 59 | - $collection = array(); // collected nodes |
|
| 60 | - $tag_index = 0; // the first node might be whitespace, |
|
| 61 | - // so this tells us where the start tag is |
|
| 62 | - $tbody_mode = false; // if true, then we need to wrap any stray |
|
| 63 | - // <tr>s with a <tbody>. |
|
| 64 | - |
|
| 65 | - foreach ($tokens_of_children as $token) { |
|
| 66 | - $is_child = ($nesting == 0); |
|
| 67 | - |
|
| 68 | - if ($token === false) { |
|
| 69 | - // terminating sequence started |
|
| 70 | - } elseif ($token instanceof HTMLPurifier_Token_Start) { |
|
| 71 | - $nesting++; |
|
| 72 | - } elseif ($token instanceof HTMLPurifier_Token_End) { |
|
| 73 | - $nesting--; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // handle node collection |
|
| 77 | - if ($is_collecting) { |
|
| 78 | - if ($is_child) { |
|
| 79 | - // okay, let's stash the tokens away |
|
| 80 | - // first token tells us the type of the collection |
|
| 81 | - switch ($collection[$tag_index]->name) { |
|
| 82 | - case 'tbody': |
|
| 83 | - $tbody_mode = true; |
|
| 84 | - case 'tr': |
|
| 85 | - $content[] = $collection; |
|
| 86 | - break; |
|
| 87 | - case 'caption': |
|
| 88 | - if ($caption !== false) break; |
|
| 89 | - $caption = $collection; |
|
| 90 | - break; |
|
| 91 | - case 'thead': |
|
| 92 | - case 'tfoot': |
|
| 93 | - $tbody_mode = true; |
|
| 94 | - // XXX This breaks rendering properties with |
|
| 95 | - // Firefox, which never floats a <thead> to |
|
| 96 | - // the top. Ever. (Our scheme will float the |
|
| 97 | - // first <thead> to the top.) So maybe |
|
| 98 | - // <thead>s that are not first should be |
|
| 99 | - // turned into <tbody>? Very tricky, indeed. |
|
| 100 | - |
|
| 101 | - // access the appropriate variable, $thead or $tfoot |
|
| 102 | - $var = $collection[$tag_index]->name; |
|
| 103 | - if ($$var === false) { |
|
| 104 | - $$var = $collection; |
|
| 105 | - } else { |
|
| 106 | - // Oops, there's a second one! What |
|
| 107 | - // should we do? Current behavior is to |
|
| 108 | - // transmutate the first and last entries into |
|
| 109 | - // tbody tags, and then put into content. |
|
| 110 | - // Maybe a better idea is to *attach |
|
| 111 | - // it* to the existing thead or tfoot? |
|
| 112 | - // We don't do this, because Firefox |
|
| 113 | - // doesn't float an extra tfoot to the |
|
| 114 | - // bottom like it does for the first one. |
|
| 115 | - $collection[$tag_index]->name = 'tbody'; |
|
| 116 | - $collection[count($collection)-1]->name = 'tbody'; |
|
| 117 | - $content[] = $collection; |
|
| 118 | - } |
|
| 119 | - break; |
|
| 120 | - case 'colgroup': |
|
| 121 | - $cols[] = $collection; |
|
| 122 | - break; |
|
| 123 | - } |
|
| 124 | - $collection = array(); |
|
| 125 | - $is_collecting = false; |
|
| 126 | - $tag_index = 0; |
|
| 127 | - } else { |
|
| 128 | - // add the node to the collection |
|
| 129 | - $collection[] = $token; |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // terminate |
|
| 134 | - if ($token === false) break; |
|
| 135 | - |
|
| 136 | - if ($is_child) { |
|
| 137 | - // determine what we're dealing with |
|
| 138 | - if ($token->name == 'col') { |
|
| 139 | - // the only empty tag in the possie, we can handle it |
|
| 140 | - // immediately |
|
| 141 | - $cols[] = array_merge($collection, array($token)); |
|
| 142 | - $collection = array(); |
|
| 143 | - $tag_index = 0; |
|
| 144 | - continue; |
|
| 145 | - } |
|
| 146 | - switch($token->name) { |
|
| 147 | - case 'caption': |
|
| 148 | - case 'colgroup': |
|
| 149 | - case 'thead': |
|
| 150 | - case 'tfoot': |
|
| 151 | - case 'tbody': |
|
| 152 | - case 'tr': |
|
| 153 | - $is_collecting = true; |
|
| 154 | - $collection[] = $token; |
|
| 155 | - continue; |
|
| 156 | - default: |
|
| 157 | - if (!empty($token->is_whitespace)) { |
|
| 158 | - $collection[] = $token; |
|
| 159 | - $tag_index++; |
|
| 160 | - } |
|
| 161 | - continue; |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if (empty($content)) return false; |
|
| 167 | - |
|
| 168 | - $ret = array(); |
|
| 169 | - if ($caption !== false) $ret = array_merge($ret, $caption); |
|
| 170 | - if ($cols !== false) foreach ($cols as $token_array) $ret = array_merge($ret, $token_array); |
|
| 171 | - if ($thead !== false) $ret = array_merge($ret, $thead); |
|
| 172 | - if ($tfoot !== false) $ret = array_merge($ret, $tfoot); |
|
| 173 | - |
|
| 174 | - if ($tbody_mode) { |
|
| 175 | - // a little tricky, since the start of the collection may be |
|
| 176 | - // whitespace |
|
| 177 | - $inside_tbody = false; |
|
| 178 | - foreach ($content as $token_array) { |
|
| 179 | - // find the starting token |
|
| 180 | - foreach ($token_array as $t) { |
|
| 181 | - if ($t->name === 'tr' || $t->name === 'tbody') { |
|
| 182 | - break; |
|
| 183 | - } |
|
| 184 | - } // iterator variable carries over |
|
| 185 | - if ($t->name === 'tr') { |
|
| 186 | - if ($inside_tbody) { |
|
| 187 | - $ret = array_merge($ret, $token_array); |
|
| 188 | - } else { |
|
| 189 | - $ret[] = new HTMLPurifier_Token_Start('tbody'); |
|
| 190 | - $ret = array_merge($ret, $token_array); |
|
| 191 | - $inside_tbody = true; |
|
| 192 | - } |
|
| 193 | - } elseif ($t->name === 'tbody') { |
|
| 194 | - if ($inside_tbody) { |
|
| 195 | - $ret[] = new HTMLPurifier_Token_End('tbody'); |
|
| 196 | - $inside_tbody = false; |
|
| 197 | - $ret = array_merge($ret, $token_array); |
|
| 198 | - } else { |
|
| 199 | - $ret = array_merge($ret, $token_array); |
|
| 200 | - } |
|
| 201 | - } else { |
|
| 202 | - trigger_error("tr/tbody in content invariant failed in Table ChildDef", E_USER_ERROR); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - if ($inside_tbody) { |
|
| 206 | - $ret[] = new HTMLPurifier_Token_End('tbody'); |
|
| 207 | - } |
|
| 208 | - } else { |
|
| 209 | - foreach ($content as $token_array) { |
|
| 210 | - // invariant: everything in here is <tr>s |
|
| 211 | - $ret = array_merge($ret, $token_array); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - if (!empty($collection) && $is_collecting == false){ |
|
| 216 | - // grab the trailing space |
|
| 217 | - $ret = array_merge($ret, $collection); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - array_pop($tokens_of_children); // remove phantom token |
|
| 221 | - |
|
| 222 | - return ($ret === $tokens_of_children) ? true : $ret; |
|
| 223 | - |
|
| 224 | - } |
|
| 34 | + public $allow_empty = false; |
|
| 35 | + public $type = 'table'; |
|
| 36 | + public $elements = array('tr' => true, 'tbody' => true, 'thead' => true, |
|
| 37 | + 'tfoot' => true, 'caption' => true, 'colgroup' => true, 'col' => true); |
|
| 38 | + public function __construct() {} |
|
| 39 | + public function validateChildren($tokens_of_children, $config, $context) { |
|
| 40 | + if (empty($tokens_of_children)) return false; |
|
| 41 | + |
|
| 42 | + // this ensures that the loop gets run one last time before closing |
|
| 43 | + // up. It's a little bit of a hack, but it works! Just make sure you |
|
| 44 | + // get rid of the token later. |
|
| 45 | + $tokens_of_children[] = false; |
|
| 46 | + |
|
| 47 | + // only one of these elements is allowed in a table |
|
| 48 | + $caption = false; |
|
| 49 | + $thead = false; |
|
| 50 | + $tfoot = false; |
|
| 51 | + |
|
| 52 | + // as many of these as you want |
|
| 53 | + $cols = array(); |
|
| 54 | + $content = array(); |
|
| 55 | + |
|
| 56 | + $nesting = 0; // current depth so we can determine nodes |
|
| 57 | + $is_collecting = false; // are we globbing together tokens to package |
|
| 58 | + // into one of the collectors? |
|
| 59 | + $collection = array(); // collected nodes |
|
| 60 | + $tag_index = 0; // the first node might be whitespace, |
|
| 61 | + // so this tells us where the start tag is |
|
| 62 | + $tbody_mode = false; // if true, then we need to wrap any stray |
|
| 63 | + // <tr>s with a <tbody>. |
|
| 64 | + |
|
| 65 | + foreach ($tokens_of_children as $token) { |
|
| 66 | + $is_child = ($nesting == 0); |
|
| 67 | + |
|
| 68 | + if ($token === false) { |
|
| 69 | + // terminating sequence started |
|
| 70 | + } elseif ($token instanceof HTMLPurifier_Token_Start) { |
|
| 71 | + $nesting++; |
|
| 72 | + } elseif ($token instanceof HTMLPurifier_Token_End) { |
|
| 73 | + $nesting--; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // handle node collection |
|
| 77 | + if ($is_collecting) { |
|
| 78 | + if ($is_child) { |
|
| 79 | + // okay, let's stash the tokens away |
|
| 80 | + // first token tells us the type of the collection |
|
| 81 | + switch ($collection[$tag_index]->name) { |
|
| 82 | + case 'tbody': |
|
| 83 | + $tbody_mode = true; |
|
| 84 | + case 'tr': |
|
| 85 | + $content[] = $collection; |
|
| 86 | + break; |
|
| 87 | + case 'caption': |
|
| 88 | + if ($caption !== false) break; |
|
| 89 | + $caption = $collection; |
|
| 90 | + break; |
|
| 91 | + case 'thead': |
|
| 92 | + case 'tfoot': |
|
| 93 | + $tbody_mode = true; |
|
| 94 | + // XXX This breaks rendering properties with |
|
| 95 | + // Firefox, which never floats a <thead> to |
|
| 96 | + // the top. Ever. (Our scheme will float the |
|
| 97 | + // first <thead> to the top.) So maybe |
|
| 98 | + // <thead>s that are not first should be |
|
| 99 | + // turned into <tbody>? Very tricky, indeed. |
|
| 100 | + |
|
| 101 | + // access the appropriate variable, $thead or $tfoot |
|
| 102 | + $var = $collection[$tag_index]->name; |
|
| 103 | + if ($$var === false) { |
|
| 104 | + $$var = $collection; |
|
| 105 | + } else { |
|
| 106 | + // Oops, there's a second one! What |
|
| 107 | + // should we do? Current behavior is to |
|
| 108 | + // transmutate the first and last entries into |
|
| 109 | + // tbody tags, and then put into content. |
|
| 110 | + // Maybe a better idea is to *attach |
|
| 111 | + // it* to the existing thead or tfoot? |
|
| 112 | + // We don't do this, because Firefox |
|
| 113 | + // doesn't float an extra tfoot to the |
|
| 114 | + // bottom like it does for the first one. |
|
| 115 | + $collection[$tag_index]->name = 'tbody'; |
|
| 116 | + $collection[count($collection)-1]->name = 'tbody'; |
|
| 117 | + $content[] = $collection; |
|
| 118 | + } |
|
| 119 | + break; |
|
| 120 | + case 'colgroup': |
|
| 121 | + $cols[] = $collection; |
|
| 122 | + break; |
|
| 123 | + } |
|
| 124 | + $collection = array(); |
|
| 125 | + $is_collecting = false; |
|
| 126 | + $tag_index = 0; |
|
| 127 | + } else { |
|
| 128 | + // add the node to the collection |
|
| 129 | + $collection[] = $token; |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // terminate |
|
| 134 | + if ($token === false) break; |
|
| 135 | + |
|
| 136 | + if ($is_child) { |
|
| 137 | + // determine what we're dealing with |
|
| 138 | + if ($token->name == 'col') { |
|
| 139 | + // the only empty tag in the possie, we can handle it |
|
| 140 | + // immediately |
|
| 141 | + $cols[] = array_merge($collection, array($token)); |
|
| 142 | + $collection = array(); |
|
| 143 | + $tag_index = 0; |
|
| 144 | + continue; |
|
| 145 | + } |
|
| 146 | + switch($token->name) { |
|
| 147 | + case 'caption': |
|
| 148 | + case 'colgroup': |
|
| 149 | + case 'thead': |
|
| 150 | + case 'tfoot': |
|
| 151 | + case 'tbody': |
|
| 152 | + case 'tr': |
|
| 153 | + $is_collecting = true; |
|
| 154 | + $collection[] = $token; |
|
| 155 | + continue; |
|
| 156 | + default: |
|
| 157 | + if (!empty($token->is_whitespace)) { |
|
| 158 | + $collection[] = $token; |
|
| 159 | + $tag_index++; |
|
| 160 | + } |
|
| 161 | + continue; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if (empty($content)) return false; |
|
| 167 | + |
|
| 168 | + $ret = array(); |
|
| 169 | + if ($caption !== false) $ret = array_merge($ret, $caption); |
|
| 170 | + if ($cols !== false) foreach ($cols as $token_array) $ret = array_merge($ret, $token_array); |
|
| 171 | + if ($thead !== false) $ret = array_merge($ret, $thead); |
|
| 172 | + if ($tfoot !== false) $ret = array_merge($ret, $tfoot); |
|
| 173 | + |
|
| 174 | + if ($tbody_mode) { |
|
| 175 | + // a little tricky, since the start of the collection may be |
|
| 176 | + // whitespace |
|
| 177 | + $inside_tbody = false; |
|
| 178 | + foreach ($content as $token_array) { |
|
| 179 | + // find the starting token |
|
| 180 | + foreach ($token_array as $t) { |
|
| 181 | + if ($t->name === 'tr' || $t->name === 'tbody') { |
|
| 182 | + break; |
|
| 183 | + } |
|
| 184 | + } // iterator variable carries over |
|
| 185 | + if ($t->name === 'tr') { |
|
| 186 | + if ($inside_tbody) { |
|
| 187 | + $ret = array_merge($ret, $token_array); |
|
| 188 | + } else { |
|
| 189 | + $ret[] = new HTMLPurifier_Token_Start('tbody'); |
|
| 190 | + $ret = array_merge($ret, $token_array); |
|
| 191 | + $inside_tbody = true; |
|
| 192 | + } |
|
| 193 | + } elseif ($t->name === 'tbody') { |
|
| 194 | + if ($inside_tbody) { |
|
| 195 | + $ret[] = new HTMLPurifier_Token_End('tbody'); |
|
| 196 | + $inside_tbody = false; |
|
| 197 | + $ret = array_merge($ret, $token_array); |
|
| 198 | + } else { |
|
| 199 | + $ret = array_merge($ret, $token_array); |
|
| 200 | + } |
|
| 201 | + } else { |
|
| 202 | + trigger_error("tr/tbody in content invariant failed in Table ChildDef", E_USER_ERROR); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + if ($inside_tbody) { |
|
| 206 | + $ret[] = new HTMLPurifier_Token_End('tbody'); |
|
| 207 | + } |
|
| 208 | + } else { |
|
| 209 | + foreach ($content as $token_array) { |
|
| 210 | + // invariant: everything in here is <tr>s |
|
| 211 | + $ret = array_merge($ret, $token_array); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + if (!empty($collection) && $is_collecting == false){ |
|
| 216 | + // grab the trailing space |
|
| 217 | + $ret = array_merge($ret, $collection); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + array_pop($tokens_of_children); // remove phantom token |
|
| 221 | + |
|
| 222 | + return ($ret === $tokens_of_children) ? true : $ret; |
|
| 223 | + |
|
| 224 | + } |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | // vim: et sw=4 sts=4 |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | // doesn't float an extra tfoot to the |
| 114 | 114 | // bottom like it does for the first one. |
| 115 | 115 | $collection[$tag_index]->name = 'tbody'; |
| 116 | - $collection[count($collection)-1]->name = 'tbody'; |
|
| 116 | + $collection[count($collection) - 1]->name = 'tbody'; |
|
| 117 | 117 | $content[] = $collection; |
| 118 | 118 | } |
| 119 | 119 | break; |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | $tag_index = 0; |
| 144 | 144 | continue; |
| 145 | 145 | } |
| 146 | - switch($token->name) { |
|
| 146 | + switch ($token->name) { |
|
| 147 | 147 | case 'caption': |
| 148 | 148 | case 'colgroup': |
| 149 | 149 | case 'thead': |
@@ -212,7 +212,7 @@ discard block |
||
| 212 | 212 | } |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - if (!empty($collection) && $is_collecting == false){ |
|
| 215 | + if (!empty($collection) && $is_collecting == false) { |
|
| 216 | 216 | // grab the trailing space |
| 217 | 217 | $ret = array_merge($ret, $collection); |
| 218 | 218 | } |
@@ -37,7 +37,9 @@ discard block |
||
| 37 | 37 | 'tfoot' => true, 'caption' => true, 'colgroup' => true, 'col' => true); |
| 38 | 38 | public function __construct() {} |
| 39 | 39 | public function validateChildren($tokens_of_children, $config, $context) { |
| 40 | - if (empty($tokens_of_children)) return false; |
|
| 40 | + if (empty($tokens_of_children)) { |
|
| 41 | + return false; |
|
| 42 | + } |
|
| 41 | 43 | |
| 42 | 44 | // this ensures that the loop gets run one last time before closing |
| 43 | 45 | // up. It's a little bit of a hack, but it works! Just make sure you |
@@ -85,7 +87,9 @@ discard block |
||
| 85 | 87 | $content[] = $collection; |
| 86 | 88 | break; |
| 87 | 89 | case 'caption': |
| 88 | - if ($caption !== false) break; |
|
| 90 | + if ($caption !== false) { |
|
| 91 | + break; |
|
| 92 | + } |
|
| 89 | 93 | $caption = $collection; |
| 90 | 94 | break; |
| 91 | 95 | case 'thead': |
@@ -131,7 +135,9 @@ discard block |
||
| 131 | 135 | } |
| 132 | 136 | |
| 133 | 137 | // terminate |
| 134 | - if ($token === false) break; |
|
| 138 | + if ($token === false) { |
|
| 139 | + break; |
|
| 140 | + } |
|
| 135 | 141 | |
| 136 | 142 | if ($is_child) { |
| 137 | 143 | // determine what we're dealing with |
@@ -163,13 +169,23 @@ discard block |
||
| 163 | 169 | } |
| 164 | 170 | } |
| 165 | 171 | |
| 166 | - if (empty($content)) return false; |
|
| 172 | + if (empty($content)) { |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 167 | 175 | |
| 168 | 176 | $ret = array(); |
| 169 | - if ($caption !== false) $ret = array_merge($ret, $caption); |
|
| 170 | - if ($cols !== false) foreach ($cols as $token_array) $ret = array_merge($ret, $token_array); |
|
| 171 | - if ($thead !== false) $ret = array_merge($ret, $thead); |
|
| 172 | - if ($tfoot !== false) $ret = array_merge($ret, $tfoot); |
|
| 177 | + if ($caption !== false) { |
|
| 178 | + $ret = array_merge($ret, $caption); |
|
| 179 | + } |
|
| 180 | + if ($cols !== false) { |
|
| 181 | + foreach ($cols as $token_array) $ret = array_merge($ret, $token_array); |
|
| 182 | + } |
|
| 183 | + if ($thead !== false) { |
|
| 184 | + $ret = array_merge($ret, $thead); |
|
| 185 | + } |
|
| 186 | + if ($tfoot !== false) { |
|
| 187 | + $ret = array_merge($ret, $tfoot); |
|
| 188 | + } |
|
| 173 | 189 | |
| 174 | 190 | if ($tbody_mode) { |
| 175 | 191 | // a little tricky, since the start of the collection may be |
@@ -17,692 +17,692 @@ |
||
| 17 | 17 | class HTMLPurifier_Config |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * HTML Purifier's version |
|
| 22 | - */ |
|
| 23 | - public $version = '4.4.0'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Bool indicator whether or not to automatically finalize |
|
| 27 | - * the object if a read operation is done |
|
| 28 | - */ |
|
| 29 | - public $autoFinalize = true; |
|
| 30 | - |
|
| 31 | - // protected member variables |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Namespace indexed array of serials for specific namespaces (see |
|
| 35 | - * getSerial() for more info). |
|
| 36 | - */ |
|
| 37 | - protected $serials = array(); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Serial for entire configuration object |
|
| 41 | - */ |
|
| 42 | - protected $serial; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Parser for variables |
|
| 46 | - */ |
|
| 47 | - protected $parser = null; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Reference HTMLPurifier_ConfigSchema for value checking |
|
| 51 | - * @note This is public for introspective purposes. Please don't |
|
| 52 | - * abuse! |
|
| 53 | - */ |
|
| 54 | - public $def; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Indexed array of definitions |
|
| 58 | - */ |
|
| 59 | - protected $definitions; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Bool indicator whether or not config is finalized |
|
| 63 | - */ |
|
| 64 | - protected $finalized = false; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Property list containing configuration directives. |
|
| 68 | - */ |
|
| 69 | - protected $plist; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Whether or not a set is taking place due to an |
|
| 73 | - * alias lookup. |
|
| 74 | - */ |
|
| 75 | - private $aliasMode; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Set to false if you do not want line and file numbers in errors |
|
| 79 | - * (useful when unit testing). This will also compress some errors |
|
| 80 | - * and exceptions. |
|
| 81 | - */ |
|
| 82 | - public $chatty = true; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Current lock; only gets to this namespace are allowed. |
|
| 86 | - */ |
|
| 87 | - private $lock; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param $definition HTMLPurifier_ConfigSchema that defines what directives |
|
| 91 | - * are allowed. |
|
| 92 | - */ |
|
| 93 | - public function __construct($definition, $parent = null) { |
|
| 94 | - $parent = $parent ? $parent : $definition->defaultPlist; |
|
| 95 | - $this->plist = new HTMLPurifier_PropertyList($parent); |
|
| 96 | - $this->def = $definition; // keep a copy around for checking |
|
| 97 | - $this->parser = new HTMLPurifier_VarParser_Flexible(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Convenience constructor that creates a config object based on a mixed var |
|
| 102 | - * @param mixed $config Variable that defines the state of the config |
|
| 103 | - * object. Can be: a HTMLPurifier_Config() object, |
|
| 104 | - * an array of directives based on loadArray(), |
|
| 105 | - * or a string filename of an ini file. |
|
| 106 | - * @param HTMLPurifier_ConfigSchema Schema object |
|
| 107 | - * @return Configured HTMLPurifier_Config object |
|
| 108 | - */ |
|
| 109 | - public static function create($config, $schema = null) { |
|
| 110 | - if ($config instanceof HTMLPurifier_Config) { |
|
| 111 | - // pass-through |
|
| 112 | - return $config; |
|
| 113 | - } |
|
| 114 | - if (!$schema) { |
|
| 115 | - $ret = HTMLPurifier_Config::createDefault(); |
|
| 116 | - } else { |
|
| 117 | - $ret = new HTMLPurifier_Config($schema); |
|
| 118 | - } |
|
| 119 | - if (is_string($config)) $ret->loadIni($config); |
|
| 120 | - elseif (is_array($config)) $ret->loadArray($config); |
|
| 121 | - return $ret; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Creates a new config object that inherits from a previous one. |
|
| 126 | - * @param HTMLPurifier_Config $config Configuration object to inherit |
|
| 127 | - * from. |
|
| 128 | - * @return HTMLPurifier_Config object with $config as its parent. |
|
| 129 | - */ |
|
| 130 | - public static function inherit(HTMLPurifier_Config $config) { |
|
| 131 | - return new HTMLPurifier_Config($config->def, $config->plist); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Convenience constructor that creates a default configuration object. |
|
| 136 | - * @return Default HTMLPurifier_Config object. |
|
| 137 | - */ |
|
| 138 | - public static function createDefault() { |
|
| 139 | - $definition = HTMLPurifier_ConfigSchema::instance(); |
|
| 140 | - $config = new HTMLPurifier_Config($definition); |
|
| 141 | - return $config; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Retreives a value from the configuration. |
|
| 146 | - * @param $key String key |
|
| 147 | - */ |
|
| 148 | - public function get($key, $a = null) { |
|
| 149 | - if ($a !== null) { |
|
| 150 | - $this->triggerError("Using deprecated API: use \$config->get('$key.$a') instead", E_USER_WARNING); |
|
| 151 | - $key = "$key.$a"; |
|
| 152 | - } |
|
| 153 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 154 | - if (!isset($this->def->info[$key])) { |
|
| 155 | - // can't add % due to SimpleTest bug |
|
| 156 | - $this->triggerError('Cannot retrieve value of undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 157 | - E_USER_WARNING); |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - if (isset($this->def->info[$key]->isAlias)) { |
|
| 161 | - $d = $this->def->info[$key]; |
|
| 162 | - $this->triggerError('Cannot get value from aliased directive, use real name ' . $d->key, |
|
| 163 | - E_USER_ERROR); |
|
| 164 | - return; |
|
| 165 | - } |
|
| 166 | - if ($this->lock) { |
|
| 167 | - list($ns) = explode('.', $key); |
|
| 168 | - if ($ns !== $this->lock) { |
|
| 169 | - $this->triggerError('Cannot get value of namespace ' . $ns . ' when lock for ' . $this->lock . ' is active, this probably indicates a Definition setup method is accessing directives that are not within its namespace', E_USER_ERROR); |
|
| 170 | - return; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - return $this->plist->get($key); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Retreives an array of directives to values from a given namespace |
|
| 178 | - * @param $namespace String namespace |
|
| 179 | - */ |
|
| 180 | - public function getBatch($namespace) { |
|
| 181 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 182 | - $full = $this->getAll(); |
|
| 183 | - if (!isset($full[$namespace])) { |
|
| 184 | - $this->triggerError('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 185 | - E_USER_WARNING); |
|
| 186 | - return; |
|
| 187 | - } |
|
| 188 | - return $full[$namespace]; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Returns a md5 signature of a segment of the configuration object |
|
| 193 | - * that uniquely identifies that particular configuration |
|
| 194 | - * @note Revision is handled specially and is removed from the batch |
|
| 195 | - * before processing! |
|
| 196 | - * @param $namespace Namespace to get serial for |
|
| 197 | - */ |
|
| 198 | - public function getBatchSerial($namespace) { |
|
| 199 | - if (empty($this->serials[$namespace])) { |
|
| 200 | - $batch = $this->getBatch($namespace); |
|
| 201 | - unset($batch['DefinitionRev']); |
|
| 202 | - $this->serials[$namespace] = md5(serialize($batch)); |
|
| 203 | - } |
|
| 204 | - return $this->serials[$namespace]; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Returns a md5 signature for the entire configuration object |
|
| 209 | - * that uniquely identifies that particular configuration |
|
| 210 | - */ |
|
| 211 | - public function getSerial() { |
|
| 212 | - if (empty($this->serial)) { |
|
| 213 | - $this->serial = md5(serialize($this->getAll())); |
|
| 214 | - } |
|
| 215 | - return $this->serial; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Retrieves all directives, organized by namespace |
|
| 220 | - * @warning This is a pretty inefficient function, avoid if you can |
|
| 221 | - */ |
|
| 222 | - public function getAll() { |
|
| 223 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 224 | - $ret = array(); |
|
| 225 | - foreach ($this->plist->squash() as $name => $value) { |
|
| 226 | - list($ns, $key) = explode('.', $name, 2); |
|
| 227 | - $ret[$ns][$key] = $value; |
|
| 228 | - } |
|
| 229 | - return $ret; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Sets a value to configuration. |
|
| 234 | - * @param $key String key |
|
| 235 | - * @param $value Mixed value |
|
| 236 | - */ |
|
| 237 | - public function set($key, $value, $a = null) { |
|
| 238 | - if (strpos($key, '.') === false) { |
|
| 239 | - $namespace = $key; |
|
| 240 | - $directive = $value; |
|
| 241 | - $value = $a; |
|
| 242 | - $key = "$key.$directive"; |
|
| 243 | - $this->triggerError("Using deprecated API: use \$config->set('$key', ...) instead", E_USER_NOTICE); |
|
| 244 | - } else { |
|
| 245 | - list($namespace) = explode('.', $key); |
|
| 246 | - } |
|
| 247 | - if ($this->isFinalized('Cannot set directive after finalization')) return; |
|
| 248 | - if (!isset($this->def->info[$key])) { |
|
| 249 | - $this->triggerError('Cannot set undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false) . ' to value', |
|
| 250 | - E_USER_WARNING); |
|
| 251 | - return; |
|
| 252 | - } |
|
| 253 | - $def = $this->def->info[$key]; |
|
| 254 | - |
|
| 255 | - if (isset($def->isAlias)) { |
|
| 256 | - if ($this->aliasMode) { |
|
| 257 | - $this->triggerError('Double-aliases not allowed, please fix '. |
|
| 258 | - 'ConfigSchema bug with' . $key, E_USER_ERROR); |
|
| 259 | - return; |
|
| 260 | - } |
|
| 261 | - $this->aliasMode = true; |
|
| 262 | - $this->set($def->key, $value); |
|
| 263 | - $this->aliasMode = false; |
|
| 264 | - $this->triggerError("$key is an alias, preferred directive name is {$def->key}", E_USER_NOTICE); |
|
| 265 | - return; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - // Raw type might be negative when using the fully optimized form |
|
| 269 | - // of stdclass, which indicates allow_null == true |
|
| 270 | - $rtype = is_int($def) ? $def : $def->type; |
|
| 271 | - if ($rtype < 0) { |
|
| 272 | - $type = -$rtype; |
|
| 273 | - $allow_null = true; |
|
| 274 | - } else { |
|
| 275 | - $type = $rtype; |
|
| 276 | - $allow_null = isset($def->allow_null); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - try { |
|
| 280 | - $value = $this->parser->parse($value, $type, $allow_null); |
|
| 281 | - } catch (HTMLPurifier_VarParserException $e) { |
|
| 282 | - $this->triggerError('Value for ' . $key . ' is of invalid type, should be ' . HTMLPurifier_VarParser::getTypeName($type), E_USER_WARNING); |
|
| 283 | - return; |
|
| 284 | - } |
|
| 285 | - if (is_string($value) && is_object($def)) { |
|
| 286 | - // resolve value alias if defined |
|
| 287 | - if (isset($def->aliases[$value])) { |
|
| 288 | - $value = $def->aliases[$value]; |
|
| 289 | - } |
|
| 290 | - // check to see if the value is allowed |
|
| 291 | - if (isset($def->allowed) && !isset($def->allowed[$value])) { |
|
| 292 | - $this->triggerError('Value not supported, valid values are: ' . |
|
| 293 | - $this->_listify($def->allowed), E_USER_WARNING); |
|
| 294 | - return; |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - $this->plist->set($key, $value); |
|
| 298 | - |
|
| 299 | - // reset definitions if the directives they depend on changed |
|
| 300 | - // this is a very costly process, so it's discouraged |
|
| 301 | - // with finalization |
|
| 302 | - if ($namespace == 'HTML' || $namespace == 'CSS' || $namespace == 'URI') { |
|
| 303 | - $this->definitions[$namespace] = null; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - $this->serials[$namespace] = false; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Convenience function for error reporting |
|
| 311 | - */ |
|
| 312 | - private function _listify($lookup) { |
|
| 313 | - $list = array(); |
|
| 314 | - foreach ($lookup as $name => $b) $list[] = $name; |
|
| 315 | - return implode(', ', $list); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Retrieves object reference to the HTML definition. |
|
| 320 | - * @param $raw Return a copy that has not been setup yet. Must be |
|
| 321 | - * called before it's been setup, otherwise won't work. |
|
| 322 | - * @param $optimized If true, this method may return null, to |
|
| 323 | - * indicate that a cached version of the modified |
|
| 324 | - * definition object is available and no further edits |
|
| 325 | - * are necessary. Consider using |
|
| 326 | - * maybeGetRawHTMLDefinition, which is more explicitly |
|
| 327 | - * named, instead. |
|
| 328 | - */ |
|
| 329 | - public function getHTMLDefinition($raw = false, $optimized = false) { |
|
| 330 | - return $this->getDefinition('HTML', $raw, $optimized); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Retrieves object reference to the CSS definition |
|
| 335 | - * @param $raw Return a copy that has not been setup yet. Must be |
|
| 336 | - * called before it's been setup, otherwise won't work. |
|
| 337 | - * @param $optimized If true, this method may return null, to |
|
| 338 | - * indicate that a cached version of the modified |
|
| 339 | - * definition object is available and no further edits |
|
| 340 | - * are necessary. Consider using |
|
| 341 | - * maybeGetRawCSSDefinition, which is more explicitly |
|
| 342 | - * named, instead. |
|
| 343 | - */ |
|
| 344 | - public function getCSSDefinition($raw = false, $optimized = false) { |
|
| 345 | - return $this->getDefinition('CSS', $raw, $optimized); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Retrieves object reference to the URI definition |
|
| 350 | - * @param $raw Return a copy that has not been setup yet. Must be |
|
| 351 | - * called before it's been setup, otherwise won't work. |
|
| 352 | - * @param $optimized If true, this method may return null, to |
|
| 353 | - * indicate that a cached version of the modified |
|
| 354 | - * definition object is available and no further edits |
|
| 355 | - * are necessary. Consider using |
|
| 356 | - * maybeGetRawURIDefinition, which is more explicitly |
|
| 357 | - * named, instead. |
|
| 358 | - */ |
|
| 359 | - public function getURIDefinition($raw = false, $optimized = false) { |
|
| 360 | - return $this->getDefinition('URI', $raw, $optimized); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Retrieves a definition |
|
| 365 | - * @param $type Type of definition: HTML, CSS, etc |
|
| 366 | - * @param $raw Whether or not definition should be returned raw |
|
| 367 | - * @param $optimized Only has an effect when $raw is true. Whether |
|
| 368 | - * or not to return null if the result is already present in |
|
| 369 | - * the cache. This is off by default for backwards |
|
| 370 | - * compatibility reasons, but you need to do things this |
|
| 371 | - * way in order to ensure that caching is done properly. |
|
| 372 | - * Check out enduser-customize.html for more details. |
|
| 373 | - * We probably won't ever change this default, as much as the |
|
| 374 | - * maybe semantics is the "right thing to do." |
|
| 375 | - */ |
|
| 376 | - public function getDefinition($type, $raw = false, $optimized = false) { |
|
| 377 | - if ($optimized && !$raw) { |
|
| 378 | - throw new HTMLPurifier_Exception("Cannot set optimized = true when raw = false"); |
|
| 379 | - } |
|
| 380 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 381 | - // temporarily suspend locks, so we can handle recursive definition calls |
|
| 382 | - $lock = $this->lock; |
|
| 383 | - $this->lock = null; |
|
| 384 | - $factory = HTMLPurifier_DefinitionCacheFactory::instance(); |
|
| 385 | - $cache = $factory->create($type, $this); |
|
| 386 | - $this->lock = $lock; |
|
| 387 | - if (!$raw) { |
|
| 388 | - // full definition |
|
| 389 | - // --------------- |
|
| 390 | - // check if definition is in memory |
|
| 391 | - if (!empty($this->definitions[$type])) { |
|
| 392 | - $def = $this->definitions[$type]; |
|
| 393 | - // check if the definition is setup |
|
| 394 | - if ($def->setup) { |
|
| 395 | - return $def; |
|
| 396 | - } else { |
|
| 397 | - $def->setup($this); |
|
| 398 | - if ($def->optimized) $cache->add($def, $this); |
|
| 399 | - return $def; |
|
| 400 | - } |
|
| 401 | - } |
|
| 402 | - // check if definition is in cache |
|
| 403 | - $def = $cache->get($this); |
|
| 404 | - if ($def) { |
|
| 405 | - // definition in cache, save to memory and return it |
|
| 406 | - $this->definitions[$type] = $def; |
|
| 407 | - return $def; |
|
| 408 | - } |
|
| 409 | - // initialize it |
|
| 410 | - $def = $this->initDefinition($type); |
|
| 411 | - // set it up |
|
| 412 | - $this->lock = $type; |
|
| 413 | - $def->setup($this); |
|
| 414 | - $this->lock = null; |
|
| 415 | - // save in cache |
|
| 416 | - $cache->add($def, $this); |
|
| 417 | - // return it |
|
| 418 | - return $def; |
|
| 419 | - } else { |
|
| 420 | - // raw definition |
|
| 421 | - // -------------- |
|
| 422 | - // check preconditions |
|
| 423 | - $def = null; |
|
| 424 | - if ($optimized) { |
|
| 425 | - if (is_null($this->get($type . '.DefinitionID'))) { |
|
| 426 | - // fatally error out if definition ID not set |
|
| 427 | - throw new HTMLPurifier_Exception("Cannot retrieve raw version without specifying %$type.DefinitionID"); |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - if (!empty($this->definitions[$type])) { |
|
| 431 | - $def = $this->definitions[$type]; |
|
| 432 | - if ($def->setup && !$optimized) { |
|
| 433 | - $extra = $this->chatty ? " (try moving this code block earlier in your initialization)" : ""; |
|
| 434 | - throw new HTMLPurifier_Exception("Cannot retrieve raw definition after it has already been setup" . $extra); |
|
| 435 | - } |
|
| 436 | - if ($def->optimized === null) { |
|
| 437 | - $extra = $this->chatty ? " (try flushing your cache)" : ""; |
|
| 438 | - throw new HTMLPurifier_Exception("Optimization status of definition is unknown" . $extra); |
|
| 439 | - } |
|
| 440 | - if ($def->optimized !== $optimized) { |
|
| 441 | - $msg = $optimized ? "optimized" : "unoptimized"; |
|
| 442 | - $extra = $this->chatty ? " (this backtrace is for the first inconsistent call, which was for a $msg raw definition)" : ""; |
|
| 443 | - throw new HTMLPurifier_Exception("Inconsistent use of optimized and unoptimized raw definition retrievals" . $extra); |
|
| 444 | - } |
|
| 445 | - } |
|
| 446 | - // check if definition was in memory |
|
| 447 | - if ($def) { |
|
| 448 | - if ($def->setup) { |
|
| 449 | - // invariant: $optimized === true (checked above) |
|
| 450 | - return null; |
|
| 451 | - } else { |
|
| 452 | - return $def; |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - // if optimized, check if definition was in cache |
|
| 456 | - // (because we do the memory check first, this formulation |
|
| 457 | - // is prone to cache slamming, but I think |
|
| 458 | - // guaranteeing that either /all/ of the raw |
|
| 459 | - // setup code or /none/ of it is run is more important.) |
|
| 460 | - if ($optimized) { |
|
| 461 | - // This code path only gets run once; once we put |
|
| 462 | - // something in $definitions (which is guaranteed by the |
|
| 463 | - // trailing code), we always short-circuit above. |
|
| 464 | - $def = $cache->get($this); |
|
| 465 | - if ($def) { |
|
| 466 | - // save the full definition for later, but don't |
|
| 467 | - // return it yet |
|
| 468 | - $this->definitions[$type] = $def; |
|
| 469 | - return null; |
|
| 470 | - } |
|
| 471 | - } |
|
| 472 | - // check invariants for creation |
|
| 473 | - if (!$optimized) { |
|
| 474 | - if (!is_null($this->get($type . '.DefinitionID'))) { |
|
| 475 | - if ($this->chatty) { |
|
| 476 | - $this->triggerError("Due to a documentation error in previous version of HTML Purifier, your definitions are not being cached. If this is OK, you can remove the %$type.DefinitionRev and %$type.DefinitionID declaration. Otherwise, modify your code to use maybeGetRawDefinition, and test if the returned value is null before making any edits (if it is null, that means that a cached version is available, and no raw operations are necessary). See <a href='http://htmlpurifier.org/docs/enduser-customize.html#optimized'>Customize</a> for more details", E_USER_WARNING); |
|
| 477 | - } else { |
|
| 478 | - $this->triggerError("Useless DefinitionID declaration", E_USER_WARNING); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - // initialize it |
|
| 483 | - $def = $this->initDefinition($type); |
|
| 484 | - $def->optimized = $optimized; |
|
| 485 | - return $def; |
|
| 486 | - } |
|
| 487 | - throw new HTMLPurifier_Exception("The impossible happened!"); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - private function initDefinition($type) { |
|
| 491 | - // quick checks failed, let's create the object |
|
| 492 | - if ($type == 'HTML') { |
|
| 493 | - $def = new HTMLPurifier_HTMLDefinition(); |
|
| 494 | - } elseif ($type == 'CSS') { |
|
| 495 | - $def = new HTMLPurifier_CSSDefinition(); |
|
| 496 | - } elseif ($type == 'URI') { |
|
| 497 | - $def = new HTMLPurifier_URIDefinition(); |
|
| 498 | - } else { |
|
| 499 | - throw new HTMLPurifier_Exception("Definition of $type type not supported"); |
|
| 500 | - } |
|
| 501 | - $this->definitions[$type] = $def; |
|
| 502 | - return $def; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - public function maybeGetRawDefinition($name) { |
|
| 506 | - return $this->getDefinition($name, true, true); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - public function maybeGetRawHTMLDefinition() { |
|
| 510 | - return $this->getDefinition('HTML', true, true); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - public function maybeGetRawCSSDefinition() { |
|
| 514 | - return $this->getDefinition('CSS', true, true); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - public function maybeGetRawURIDefinition() { |
|
| 518 | - return $this->getDefinition('URI', true, true); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * Loads configuration values from an array with the following structure: |
|
| 523 | - * Namespace.Directive => Value |
|
| 524 | - * @param $config_array Configuration associative array |
|
| 525 | - */ |
|
| 526 | - public function loadArray($config_array) { |
|
| 527 | - if ($this->isFinalized('Cannot load directives after finalization')) return; |
|
| 528 | - foreach ($config_array as $key => $value) { |
|
| 529 | - $key = str_replace('_', '.', $key); |
|
| 530 | - if (strpos($key, '.') !== false) { |
|
| 531 | - $this->set($key, $value); |
|
| 532 | - } else { |
|
| 533 | - $namespace = $key; |
|
| 534 | - $namespace_values = $value; |
|
| 535 | - foreach ($namespace_values as $directive => $value) { |
|
| 536 | - $this->set($namespace .'.'. $directive, $value); |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * Returns a list of array(namespace, directive) for all directives |
|
| 544 | - * that are allowed in a web-form context as per an allowed |
|
| 545 | - * namespaces/directives list. |
|
| 546 | - * @param $allowed List of allowed namespaces/directives |
|
| 547 | - */ |
|
| 548 | - public static function getAllowedDirectivesForForm($allowed, $schema = null) { |
|
| 549 | - if (!$schema) { |
|
| 550 | - $schema = HTMLPurifier_ConfigSchema::instance(); |
|
| 551 | - } |
|
| 552 | - if ($allowed !== true) { |
|
| 553 | - if (is_string($allowed)) $allowed = array($allowed); |
|
| 554 | - $allowed_ns = array(); |
|
| 555 | - $allowed_directives = array(); |
|
| 556 | - $blacklisted_directives = array(); |
|
| 557 | - foreach ($allowed as $ns_or_directive) { |
|
| 558 | - if (strpos($ns_or_directive, '.') !== false) { |
|
| 559 | - // directive |
|
| 560 | - if ($ns_or_directive[0] == '-') { |
|
| 561 | - $blacklisted_directives[substr($ns_or_directive, 1)] = true; |
|
| 562 | - } else { |
|
| 563 | - $allowed_directives[$ns_or_directive] = true; |
|
| 564 | - } |
|
| 565 | - } else { |
|
| 566 | - // namespace |
|
| 567 | - $allowed_ns[$ns_or_directive] = true; |
|
| 568 | - } |
|
| 569 | - } |
|
| 570 | - } |
|
| 571 | - $ret = array(); |
|
| 572 | - foreach ($schema->info as $key => $def) { |
|
| 573 | - list($ns, $directive) = explode('.', $key, 2); |
|
| 574 | - if ($allowed !== true) { |
|
| 575 | - if (isset($blacklisted_directives["$ns.$directive"])) continue; |
|
| 576 | - if (!isset($allowed_directives["$ns.$directive"]) && !isset($allowed_ns[$ns])) continue; |
|
| 577 | - } |
|
| 578 | - if (isset($def->isAlias)) continue; |
|
| 579 | - if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') continue; |
|
| 580 | - $ret[] = array($ns, $directive); |
|
| 581 | - } |
|
| 582 | - return $ret; |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Loads configuration values from $_GET/$_POST that were posted |
|
| 587 | - * via ConfigForm |
|
| 588 | - * @param $array $_GET or $_POST array to import |
|
| 589 | - * @param $index Index/name that the config variables are in |
|
| 590 | - * @param $allowed List of allowed namespaces/directives |
|
| 591 | - * @param $mq_fix Boolean whether or not to enable magic quotes fix |
|
| 592 | - * @param $schema Instance of HTMLPurifier_ConfigSchema to use, if not global copy |
|
| 593 | - */ |
|
| 594 | - public static function loadArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null) { |
|
| 595 | - $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $schema); |
|
| 596 | - $config = HTMLPurifier_Config::create($ret, $schema); |
|
| 597 | - return $config; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * Merges in configuration values from $_GET/$_POST to object. NOT STATIC. |
|
| 602 | - * @note Same parameters as loadArrayFromForm |
|
| 603 | - */ |
|
| 604 | - public function mergeArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true) { |
|
| 605 | - $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $this->def); |
|
| 606 | - $this->loadArray($ret); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * Prepares an array from a form into something usable for the more |
|
| 611 | - * strict parts of HTMLPurifier_Config |
|
| 612 | - */ |
|
| 613 | - public static function prepareArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null) { |
|
| 614 | - if ($index !== false) $array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array(); |
|
| 615 | - $mq = $mq_fix && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc(); |
|
| 616 | - |
|
| 617 | - $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $schema); |
|
| 618 | - $ret = array(); |
|
| 619 | - foreach ($allowed as $key) { |
|
| 620 | - list($ns, $directive) = $key; |
|
| 621 | - $skey = "$ns.$directive"; |
|
| 622 | - if (!empty($array["Null_$skey"])) { |
|
| 623 | - $ret[$ns][$directive] = null; |
|
| 624 | - continue; |
|
| 625 | - } |
|
| 626 | - if (!isset($array[$skey])) continue; |
|
| 627 | - $value = $mq ? stripslashes($array[$skey]) : $array[$skey]; |
|
| 628 | - $ret[$ns][$directive] = $value; |
|
| 629 | - } |
|
| 630 | - return $ret; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * Loads configuration values from an ini file |
|
| 635 | - * @param $filename Name of ini file |
|
| 636 | - */ |
|
| 637 | - public function loadIni($filename) { |
|
| 638 | - if ($this->isFinalized('Cannot load directives after finalization')) return; |
|
| 639 | - $array = parse_ini_file($filename, true); |
|
| 640 | - $this->loadArray($array); |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - /** |
|
| 644 | - * Checks whether or not the configuration object is finalized. |
|
| 645 | - * @param $error String error message, or false for no error |
|
| 646 | - */ |
|
| 647 | - public function isFinalized($error = false) { |
|
| 648 | - if ($this->finalized && $error) { |
|
| 649 | - $this->triggerError($error, E_USER_ERROR); |
|
| 650 | - } |
|
| 651 | - return $this->finalized; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * Finalizes configuration only if auto finalize is on and not |
|
| 656 | - * already finalized |
|
| 657 | - */ |
|
| 658 | - public function autoFinalize() { |
|
| 659 | - if ($this->autoFinalize) { |
|
| 660 | - $this->finalize(); |
|
| 661 | - } else { |
|
| 662 | - $this->plist->squash(true); |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * Finalizes a configuration object, prohibiting further change |
|
| 668 | - */ |
|
| 669 | - public function finalize() { |
|
| 670 | - $this->finalized = true; |
|
| 671 | - $this->parser = null; |
|
| 672 | - } |
|
| 673 | - |
|
| 674 | - /** |
|
| 675 | - * Produces a nicely formatted error message by supplying the |
|
| 676 | - * stack frame information OUTSIDE of HTMLPurifier_Config. |
|
| 677 | - */ |
|
| 678 | - protected function triggerError($msg, $no) { |
|
| 679 | - // determine previous stack frame |
|
| 680 | - $extra = ''; |
|
| 681 | - if ($this->chatty) { |
|
| 682 | - $trace = debug_backtrace(); |
|
| 683 | - // zip(tail(trace), trace) -- but PHP is not Haskell har har |
|
| 684 | - for ($i = 0, $c = count($trace); $i < $c - 1; $i++) { |
|
| 685 | - if ($trace[$i + 1]['class'] === 'HTMLPurifier_Config') { |
|
| 686 | - continue; |
|
| 687 | - } |
|
| 688 | - $frame = $trace[$i]; |
|
| 689 | - $extra = " invoked on line {$frame['line']} in file {$frame['file']}"; |
|
| 690 | - break; |
|
| 691 | - } |
|
| 692 | - } |
|
| 693 | - trigger_error($msg . $extra, $no); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * Returns a serialized form of the configuration object that can |
|
| 698 | - * be reconstituted. |
|
| 699 | - */ |
|
| 700 | - public function serialize() { |
|
| 701 | - $this->getDefinition('HTML'); |
|
| 702 | - $this->getDefinition('CSS'); |
|
| 703 | - $this->getDefinition('URI'); |
|
| 704 | - return serialize($this); |
|
| 705 | - } |
|
| 20 | + /** |
|
| 21 | + * HTML Purifier's version |
|
| 22 | + */ |
|
| 23 | + public $version = '4.4.0'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Bool indicator whether or not to automatically finalize |
|
| 27 | + * the object if a read operation is done |
|
| 28 | + */ |
|
| 29 | + public $autoFinalize = true; |
|
| 30 | + |
|
| 31 | + // protected member variables |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Namespace indexed array of serials for specific namespaces (see |
|
| 35 | + * getSerial() for more info). |
|
| 36 | + */ |
|
| 37 | + protected $serials = array(); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Serial for entire configuration object |
|
| 41 | + */ |
|
| 42 | + protected $serial; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Parser for variables |
|
| 46 | + */ |
|
| 47 | + protected $parser = null; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Reference HTMLPurifier_ConfigSchema for value checking |
|
| 51 | + * @note This is public for introspective purposes. Please don't |
|
| 52 | + * abuse! |
|
| 53 | + */ |
|
| 54 | + public $def; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Indexed array of definitions |
|
| 58 | + */ |
|
| 59 | + protected $definitions; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Bool indicator whether or not config is finalized |
|
| 63 | + */ |
|
| 64 | + protected $finalized = false; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Property list containing configuration directives. |
|
| 68 | + */ |
|
| 69 | + protected $plist; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Whether or not a set is taking place due to an |
|
| 73 | + * alias lookup. |
|
| 74 | + */ |
|
| 75 | + private $aliasMode; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Set to false if you do not want line and file numbers in errors |
|
| 79 | + * (useful when unit testing). This will also compress some errors |
|
| 80 | + * and exceptions. |
|
| 81 | + */ |
|
| 82 | + public $chatty = true; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Current lock; only gets to this namespace are allowed. |
|
| 86 | + */ |
|
| 87 | + private $lock; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param $definition HTMLPurifier_ConfigSchema that defines what directives |
|
| 91 | + * are allowed. |
|
| 92 | + */ |
|
| 93 | + public function __construct($definition, $parent = null) { |
|
| 94 | + $parent = $parent ? $parent : $definition->defaultPlist; |
|
| 95 | + $this->plist = new HTMLPurifier_PropertyList($parent); |
|
| 96 | + $this->def = $definition; // keep a copy around for checking |
|
| 97 | + $this->parser = new HTMLPurifier_VarParser_Flexible(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Convenience constructor that creates a config object based on a mixed var |
|
| 102 | + * @param mixed $config Variable that defines the state of the config |
|
| 103 | + * object. Can be: a HTMLPurifier_Config() object, |
|
| 104 | + * an array of directives based on loadArray(), |
|
| 105 | + * or a string filename of an ini file. |
|
| 106 | + * @param HTMLPurifier_ConfigSchema Schema object |
|
| 107 | + * @return Configured HTMLPurifier_Config object |
|
| 108 | + */ |
|
| 109 | + public static function create($config, $schema = null) { |
|
| 110 | + if ($config instanceof HTMLPurifier_Config) { |
|
| 111 | + // pass-through |
|
| 112 | + return $config; |
|
| 113 | + } |
|
| 114 | + if (!$schema) { |
|
| 115 | + $ret = HTMLPurifier_Config::createDefault(); |
|
| 116 | + } else { |
|
| 117 | + $ret = new HTMLPurifier_Config($schema); |
|
| 118 | + } |
|
| 119 | + if (is_string($config)) $ret->loadIni($config); |
|
| 120 | + elseif (is_array($config)) $ret->loadArray($config); |
|
| 121 | + return $ret; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Creates a new config object that inherits from a previous one. |
|
| 126 | + * @param HTMLPurifier_Config $config Configuration object to inherit |
|
| 127 | + * from. |
|
| 128 | + * @return HTMLPurifier_Config object with $config as its parent. |
|
| 129 | + */ |
|
| 130 | + public static function inherit(HTMLPurifier_Config $config) { |
|
| 131 | + return new HTMLPurifier_Config($config->def, $config->plist); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Convenience constructor that creates a default configuration object. |
|
| 136 | + * @return Default HTMLPurifier_Config object. |
|
| 137 | + */ |
|
| 138 | + public static function createDefault() { |
|
| 139 | + $definition = HTMLPurifier_ConfigSchema::instance(); |
|
| 140 | + $config = new HTMLPurifier_Config($definition); |
|
| 141 | + return $config; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Retreives a value from the configuration. |
|
| 146 | + * @param $key String key |
|
| 147 | + */ |
|
| 148 | + public function get($key, $a = null) { |
|
| 149 | + if ($a !== null) { |
|
| 150 | + $this->triggerError("Using deprecated API: use \$config->get('$key.$a') instead", E_USER_WARNING); |
|
| 151 | + $key = "$key.$a"; |
|
| 152 | + } |
|
| 153 | + if (!$this->finalized) $this->autoFinalize(); |
|
| 154 | + if (!isset($this->def->info[$key])) { |
|
| 155 | + // can't add % due to SimpleTest bug |
|
| 156 | + $this->triggerError('Cannot retrieve value of undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 157 | + E_USER_WARNING); |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + if (isset($this->def->info[$key]->isAlias)) { |
|
| 161 | + $d = $this->def->info[$key]; |
|
| 162 | + $this->triggerError('Cannot get value from aliased directive, use real name ' . $d->key, |
|
| 163 | + E_USER_ERROR); |
|
| 164 | + return; |
|
| 165 | + } |
|
| 166 | + if ($this->lock) { |
|
| 167 | + list($ns) = explode('.', $key); |
|
| 168 | + if ($ns !== $this->lock) { |
|
| 169 | + $this->triggerError('Cannot get value of namespace ' . $ns . ' when lock for ' . $this->lock . ' is active, this probably indicates a Definition setup method is accessing directives that are not within its namespace', E_USER_ERROR); |
|
| 170 | + return; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + return $this->plist->get($key); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Retreives an array of directives to values from a given namespace |
|
| 178 | + * @param $namespace String namespace |
|
| 179 | + */ |
|
| 180 | + public function getBatch($namespace) { |
|
| 181 | + if (!$this->finalized) $this->autoFinalize(); |
|
| 182 | + $full = $this->getAll(); |
|
| 183 | + if (!isset($full[$namespace])) { |
|
| 184 | + $this->triggerError('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 185 | + E_USER_WARNING); |
|
| 186 | + return; |
|
| 187 | + } |
|
| 188 | + return $full[$namespace]; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Returns a md5 signature of a segment of the configuration object |
|
| 193 | + * that uniquely identifies that particular configuration |
|
| 194 | + * @note Revision is handled specially and is removed from the batch |
|
| 195 | + * before processing! |
|
| 196 | + * @param $namespace Namespace to get serial for |
|
| 197 | + */ |
|
| 198 | + public function getBatchSerial($namespace) { |
|
| 199 | + if (empty($this->serials[$namespace])) { |
|
| 200 | + $batch = $this->getBatch($namespace); |
|
| 201 | + unset($batch['DefinitionRev']); |
|
| 202 | + $this->serials[$namespace] = md5(serialize($batch)); |
|
| 203 | + } |
|
| 204 | + return $this->serials[$namespace]; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Returns a md5 signature for the entire configuration object |
|
| 209 | + * that uniquely identifies that particular configuration |
|
| 210 | + */ |
|
| 211 | + public function getSerial() { |
|
| 212 | + if (empty($this->serial)) { |
|
| 213 | + $this->serial = md5(serialize($this->getAll())); |
|
| 214 | + } |
|
| 215 | + return $this->serial; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Retrieves all directives, organized by namespace |
|
| 220 | + * @warning This is a pretty inefficient function, avoid if you can |
|
| 221 | + */ |
|
| 222 | + public function getAll() { |
|
| 223 | + if (!$this->finalized) $this->autoFinalize(); |
|
| 224 | + $ret = array(); |
|
| 225 | + foreach ($this->plist->squash() as $name => $value) { |
|
| 226 | + list($ns, $key) = explode('.', $name, 2); |
|
| 227 | + $ret[$ns][$key] = $value; |
|
| 228 | + } |
|
| 229 | + return $ret; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Sets a value to configuration. |
|
| 234 | + * @param $key String key |
|
| 235 | + * @param $value Mixed value |
|
| 236 | + */ |
|
| 237 | + public function set($key, $value, $a = null) { |
|
| 238 | + if (strpos($key, '.') === false) { |
|
| 239 | + $namespace = $key; |
|
| 240 | + $directive = $value; |
|
| 241 | + $value = $a; |
|
| 242 | + $key = "$key.$directive"; |
|
| 243 | + $this->triggerError("Using deprecated API: use \$config->set('$key', ...) instead", E_USER_NOTICE); |
|
| 244 | + } else { |
|
| 245 | + list($namespace) = explode('.', $key); |
|
| 246 | + } |
|
| 247 | + if ($this->isFinalized('Cannot set directive after finalization')) return; |
|
| 248 | + if (!isset($this->def->info[$key])) { |
|
| 249 | + $this->triggerError('Cannot set undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false) . ' to value', |
|
| 250 | + E_USER_WARNING); |
|
| 251 | + return; |
|
| 252 | + } |
|
| 253 | + $def = $this->def->info[$key]; |
|
| 254 | + |
|
| 255 | + if (isset($def->isAlias)) { |
|
| 256 | + if ($this->aliasMode) { |
|
| 257 | + $this->triggerError('Double-aliases not allowed, please fix '. |
|
| 258 | + 'ConfigSchema bug with' . $key, E_USER_ERROR); |
|
| 259 | + return; |
|
| 260 | + } |
|
| 261 | + $this->aliasMode = true; |
|
| 262 | + $this->set($def->key, $value); |
|
| 263 | + $this->aliasMode = false; |
|
| 264 | + $this->triggerError("$key is an alias, preferred directive name is {$def->key}", E_USER_NOTICE); |
|
| 265 | + return; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + // Raw type might be negative when using the fully optimized form |
|
| 269 | + // of stdclass, which indicates allow_null == true |
|
| 270 | + $rtype = is_int($def) ? $def : $def->type; |
|
| 271 | + if ($rtype < 0) { |
|
| 272 | + $type = -$rtype; |
|
| 273 | + $allow_null = true; |
|
| 274 | + } else { |
|
| 275 | + $type = $rtype; |
|
| 276 | + $allow_null = isset($def->allow_null); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + try { |
|
| 280 | + $value = $this->parser->parse($value, $type, $allow_null); |
|
| 281 | + } catch (HTMLPurifier_VarParserException $e) { |
|
| 282 | + $this->triggerError('Value for ' . $key . ' is of invalid type, should be ' . HTMLPurifier_VarParser::getTypeName($type), E_USER_WARNING); |
|
| 283 | + return; |
|
| 284 | + } |
|
| 285 | + if (is_string($value) && is_object($def)) { |
|
| 286 | + // resolve value alias if defined |
|
| 287 | + if (isset($def->aliases[$value])) { |
|
| 288 | + $value = $def->aliases[$value]; |
|
| 289 | + } |
|
| 290 | + // check to see if the value is allowed |
|
| 291 | + if (isset($def->allowed) && !isset($def->allowed[$value])) { |
|
| 292 | + $this->triggerError('Value not supported, valid values are: ' . |
|
| 293 | + $this->_listify($def->allowed), E_USER_WARNING); |
|
| 294 | + return; |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + $this->plist->set($key, $value); |
|
| 298 | + |
|
| 299 | + // reset definitions if the directives they depend on changed |
|
| 300 | + // this is a very costly process, so it's discouraged |
|
| 301 | + // with finalization |
|
| 302 | + if ($namespace == 'HTML' || $namespace == 'CSS' || $namespace == 'URI') { |
|
| 303 | + $this->definitions[$namespace] = null; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + $this->serials[$namespace] = false; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Convenience function for error reporting |
|
| 311 | + */ |
|
| 312 | + private function _listify($lookup) { |
|
| 313 | + $list = array(); |
|
| 314 | + foreach ($lookup as $name => $b) $list[] = $name; |
|
| 315 | + return implode(', ', $list); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Retrieves object reference to the HTML definition. |
|
| 320 | + * @param $raw Return a copy that has not been setup yet. Must be |
|
| 321 | + * called before it's been setup, otherwise won't work. |
|
| 322 | + * @param $optimized If true, this method may return null, to |
|
| 323 | + * indicate that a cached version of the modified |
|
| 324 | + * definition object is available and no further edits |
|
| 325 | + * are necessary. Consider using |
|
| 326 | + * maybeGetRawHTMLDefinition, which is more explicitly |
|
| 327 | + * named, instead. |
|
| 328 | + */ |
|
| 329 | + public function getHTMLDefinition($raw = false, $optimized = false) { |
|
| 330 | + return $this->getDefinition('HTML', $raw, $optimized); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Retrieves object reference to the CSS definition |
|
| 335 | + * @param $raw Return a copy that has not been setup yet. Must be |
|
| 336 | + * called before it's been setup, otherwise won't work. |
|
| 337 | + * @param $optimized If true, this method may return null, to |
|
| 338 | + * indicate that a cached version of the modified |
|
| 339 | + * definition object is available and no further edits |
|
| 340 | + * are necessary. Consider using |
|
| 341 | + * maybeGetRawCSSDefinition, which is more explicitly |
|
| 342 | + * named, instead. |
|
| 343 | + */ |
|
| 344 | + public function getCSSDefinition($raw = false, $optimized = false) { |
|
| 345 | + return $this->getDefinition('CSS', $raw, $optimized); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Retrieves object reference to the URI definition |
|
| 350 | + * @param $raw Return a copy that has not been setup yet. Must be |
|
| 351 | + * called before it's been setup, otherwise won't work. |
|
| 352 | + * @param $optimized If true, this method may return null, to |
|
| 353 | + * indicate that a cached version of the modified |
|
| 354 | + * definition object is available and no further edits |
|
| 355 | + * are necessary. Consider using |
|
| 356 | + * maybeGetRawURIDefinition, which is more explicitly |
|
| 357 | + * named, instead. |
|
| 358 | + */ |
|
| 359 | + public function getURIDefinition($raw = false, $optimized = false) { |
|
| 360 | + return $this->getDefinition('URI', $raw, $optimized); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Retrieves a definition |
|
| 365 | + * @param $type Type of definition: HTML, CSS, etc |
|
| 366 | + * @param $raw Whether or not definition should be returned raw |
|
| 367 | + * @param $optimized Only has an effect when $raw is true. Whether |
|
| 368 | + * or not to return null if the result is already present in |
|
| 369 | + * the cache. This is off by default for backwards |
|
| 370 | + * compatibility reasons, but you need to do things this |
|
| 371 | + * way in order to ensure that caching is done properly. |
|
| 372 | + * Check out enduser-customize.html for more details. |
|
| 373 | + * We probably won't ever change this default, as much as the |
|
| 374 | + * maybe semantics is the "right thing to do." |
|
| 375 | + */ |
|
| 376 | + public function getDefinition($type, $raw = false, $optimized = false) { |
|
| 377 | + if ($optimized && !$raw) { |
|
| 378 | + throw new HTMLPurifier_Exception("Cannot set optimized = true when raw = false"); |
|
| 379 | + } |
|
| 380 | + if (!$this->finalized) $this->autoFinalize(); |
|
| 381 | + // temporarily suspend locks, so we can handle recursive definition calls |
|
| 382 | + $lock = $this->lock; |
|
| 383 | + $this->lock = null; |
|
| 384 | + $factory = HTMLPurifier_DefinitionCacheFactory::instance(); |
|
| 385 | + $cache = $factory->create($type, $this); |
|
| 386 | + $this->lock = $lock; |
|
| 387 | + if (!$raw) { |
|
| 388 | + // full definition |
|
| 389 | + // --------------- |
|
| 390 | + // check if definition is in memory |
|
| 391 | + if (!empty($this->definitions[$type])) { |
|
| 392 | + $def = $this->definitions[$type]; |
|
| 393 | + // check if the definition is setup |
|
| 394 | + if ($def->setup) { |
|
| 395 | + return $def; |
|
| 396 | + } else { |
|
| 397 | + $def->setup($this); |
|
| 398 | + if ($def->optimized) $cache->add($def, $this); |
|
| 399 | + return $def; |
|
| 400 | + } |
|
| 401 | + } |
|
| 402 | + // check if definition is in cache |
|
| 403 | + $def = $cache->get($this); |
|
| 404 | + if ($def) { |
|
| 405 | + // definition in cache, save to memory and return it |
|
| 406 | + $this->definitions[$type] = $def; |
|
| 407 | + return $def; |
|
| 408 | + } |
|
| 409 | + // initialize it |
|
| 410 | + $def = $this->initDefinition($type); |
|
| 411 | + // set it up |
|
| 412 | + $this->lock = $type; |
|
| 413 | + $def->setup($this); |
|
| 414 | + $this->lock = null; |
|
| 415 | + // save in cache |
|
| 416 | + $cache->add($def, $this); |
|
| 417 | + // return it |
|
| 418 | + return $def; |
|
| 419 | + } else { |
|
| 420 | + // raw definition |
|
| 421 | + // -------------- |
|
| 422 | + // check preconditions |
|
| 423 | + $def = null; |
|
| 424 | + if ($optimized) { |
|
| 425 | + if (is_null($this->get($type . '.DefinitionID'))) { |
|
| 426 | + // fatally error out if definition ID not set |
|
| 427 | + throw new HTMLPurifier_Exception("Cannot retrieve raw version without specifying %$type.DefinitionID"); |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + if (!empty($this->definitions[$type])) { |
|
| 431 | + $def = $this->definitions[$type]; |
|
| 432 | + if ($def->setup && !$optimized) { |
|
| 433 | + $extra = $this->chatty ? " (try moving this code block earlier in your initialization)" : ""; |
|
| 434 | + throw new HTMLPurifier_Exception("Cannot retrieve raw definition after it has already been setup" . $extra); |
|
| 435 | + } |
|
| 436 | + if ($def->optimized === null) { |
|
| 437 | + $extra = $this->chatty ? " (try flushing your cache)" : ""; |
|
| 438 | + throw new HTMLPurifier_Exception("Optimization status of definition is unknown" . $extra); |
|
| 439 | + } |
|
| 440 | + if ($def->optimized !== $optimized) { |
|
| 441 | + $msg = $optimized ? "optimized" : "unoptimized"; |
|
| 442 | + $extra = $this->chatty ? " (this backtrace is for the first inconsistent call, which was for a $msg raw definition)" : ""; |
|
| 443 | + throw new HTMLPurifier_Exception("Inconsistent use of optimized and unoptimized raw definition retrievals" . $extra); |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + // check if definition was in memory |
|
| 447 | + if ($def) { |
|
| 448 | + if ($def->setup) { |
|
| 449 | + // invariant: $optimized === true (checked above) |
|
| 450 | + return null; |
|
| 451 | + } else { |
|
| 452 | + return $def; |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + // if optimized, check if definition was in cache |
|
| 456 | + // (because we do the memory check first, this formulation |
|
| 457 | + // is prone to cache slamming, but I think |
|
| 458 | + // guaranteeing that either /all/ of the raw |
|
| 459 | + // setup code or /none/ of it is run is more important.) |
|
| 460 | + if ($optimized) { |
|
| 461 | + // This code path only gets run once; once we put |
|
| 462 | + // something in $definitions (which is guaranteed by the |
|
| 463 | + // trailing code), we always short-circuit above. |
|
| 464 | + $def = $cache->get($this); |
|
| 465 | + if ($def) { |
|
| 466 | + // save the full definition for later, but don't |
|
| 467 | + // return it yet |
|
| 468 | + $this->definitions[$type] = $def; |
|
| 469 | + return null; |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | + // check invariants for creation |
|
| 473 | + if (!$optimized) { |
|
| 474 | + if (!is_null($this->get($type . '.DefinitionID'))) { |
|
| 475 | + if ($this->chatty) { |
|
| 476 | + $this->triggerError("Due to a documentation error in previous version of HTML Purifier, your definitions are not being cached. If this is OK, you can remove the %$type.DefinitionRev and %$type.DefinitionID declaration. Otherwise, modify your code to use maybeGetRawDefinition, and test if the returned value is null before making any edits (if it is null, that means that a cached version is available, and no raw operations are necessary). See <a href='http://htmlpurifier.org/docs/enduser-customize.html#optimized'>Customize</a> for more details", E_USER_WARNING); |
|
| 477 | + } else { |
|
| 478 | + $this->triggerError("Useless DefinitionID declaration", E_USER_WARNING); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + // initialize it |
|
| 483 | + $def = $this->initDefinition($type); |
|
| 484 | + $def->optimized = $optimized; |
|
| 485 | + return $def; |
|
| 486 | + } |
|
| 487 | + throw new HTMLPurifier_Exception("The impossible happened!"); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + private function initDefinition($type) { |
|
| 491 | + // quick checks failed, let's create the object |
|
| 492 | + if ($type == 'HTML') { |
|
| 493 | + $def = new HTMLPurifier_HTMLDefinition(); |
|
| 494 | + } elseif ($type == 'CSS') { |
|
| 495 | + $def = new HTMLPurifier_CSSDefinition(); |
|
| 496 | + } elseif ($type == 'URI') { |
|
| 497 | + $def = new HTMLPurifier_URIDefinition(); |
|
| 498 | + } else { |
|
| 499 | + throw new HTMLPurifier_Exception("Definition of $type type not supported"); |
|
| 500 | + } |
|
| 501 | + $this->definitions[$type] = $def; |
|
| 502 | + return $def; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + public function maybeGetRawDefinition($name) { |
|
| 506 | + return $this->getDefinition($name, true, true); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + public function maybeGetRawHTMLDefinition() { |
|
| 510 | + return $this->getDefinition('HTML', true, true); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + public function maybeGetRawCSSDefinition() { |
|
| 514 | + return $this->getDefinition('CSS', true, true); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + public function maybeGetRawURIDefinition() { |
|
| 518 | + return $this->getDefinition('URI', true, true); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * Loads configuration values from an array with the following structure: |
|
| 523 | + * Namespace.Directive => Value |
|
| 524 | + * @param $config_array Configuration associative array |
|
| 525 | + */ |
|
| 526 | + public function loadArray($config_array) { |
|
| 527 | + if ($this->isFinalized('Cannot load directives after finalization')) return; |
|
| 528 | + foreach ($config_array as $key => $value) { |
|
| 529 | + $key = str_replace('_', '.', $key); |
|
| 530 | + if (strpos($key, '.') !== false) { |
|
| 531 | + $this->set($key, $value); |
|
| 532 | + } else { |
|
| 533 | + $namespace = $key; |
|
| 534 | + $namespace_values = $value; |
|
| 535 | + foreach ($namespace_values as $directive => $value) { |
|
| 536 | + $this->set($namespace .'.'. $directive, $value); |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * Returns a list of array(namespace, directive) for all directives |
|
| 544 | + * that are allowed in a web-form context as per an allowed |
|
| 545 | + * namespaces/directives list. |
|
| 546 | + * @param $allowed List of allowed namespaces/directives |
|
| 547 | + */ |
|
| 548 | + public static function getAllowedDirectivesForForm($allowed, $schema = null) { |
|
| 549 | + if (!$schema) { |
|
| 550 | + $schema = HTMLPurifier_ConfigSchema::instance(); |
|
| 551 | + } |
|
| 552 | + if ($allowed !== true) { |
|
| 553 | + if (is_string($allowed)) $allowed = array($allowed); |
|
| 554 | + $allowed_ns = array(); |
|
| 555 | + $allowed_directives = array(); |
|
| 556 | + $blacklisted_directives = array(); |
|
| 557 | + foreach ($allowed as $ns_or_directive) { |
|
| 558 | + if (strpos($ns_or_directive, '.') !== false) { |
|
| 559 | + // directive |
|
| 560 | + if ($ns_or_directive[0] == '-') { |
|
| 561 | + $blacklisted_directives[substr($ns_or_directive, 1)] = true; |
|
| 562 | + } else { |
|
| 563 | + $allowed_directives[$ns_or_directive] = true; |
|
| 564 | + } |
|
| 565 | + } else { |
|
| 566 | + // namespace |
|
| 567 | + $allowed_ns[$ns_or_directive] = true; |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | + } |
|
| 571 | + $ret = array(); |
|
| 572 | + foreach ($schema->info as $key => $def) { |
|
| 573 | + list($ns, $directive) = explode('.', $key, 2); |
|
| 574 | + if ($allowed !== true) { |
|
| 575 | + if (isset($blacklisted_directives["$ns.$directive"])) continue; |
|
| 576 | + if (!isset($allowed_directives["$ns.$directive"]) && !isset($allowed_ns[$ns])) continue; |
|
| 577 | + } |
|
| 578 | + if (isset($def->isAlias)) continue; |
|
| 579 | + if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') continue; |
|
| 580 | + $ret[] = array($ns, $directive); |
|
| 581 | + } |
|
| 582 | + return $ret; |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * Loads configuration values from $_GET/$_POST that were posted |
|
| 587 | + * via ConfigForm |
|
| 588 | + * @param $array $_GET or $_POST array to import |
|
| 589 | + * @param $index Index/name that the config variables are in |
|
| 590 | + * @param $allowed List of allowed namespaces/directives |
|
| 591 | + * @param $mq_fix Boolean whether or not to enable magic quotes fix |
|
| 592 | + * @param $schema Instance of HTMLPurifier_ConfigSchema to use, if not global copy |
|
| 593 | + */ |
|
| 594 | + public static function loadArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null) { |
|
| 595 | + $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $schema); |
|
| 596 | + $config = HTMLPurifier_Config::create($ret, $schema); |
|
| 597 | + return $config; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * Merges in configuration values from $_GET/$_POST to object. NOT STATIC. |
|
| 602 | + * @note Same parameters as loadArrayFromForm |
|
| 603 | + */ |
|
| 604 | + public function mergeArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true) { |
|
| 605 | + $ret = HTMLPurifier_Config::prepareArrayFromForm($array, $index, $allowed, $mq_fix, $this->def); |
|
| 606 | + $this->loadArray($ret); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + /** |
|
| 610 | + * Prepares an array from a form into something usable for the more |
|
| 611 | + * strict parts of HTMLPurifier_Config |
|
| 612 | + */ |
|
| 613 | + public static function prepareArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null) { |
|
| 614 | + if ($index !== false) $array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array(); |
|
| 615 | + $mq = $mq_fix && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc(); |
|
| 616 | + |
|
| 617 | + $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $schema); |
|
| 618 | + $ret = array(); |
|
| 619 | + foreach ($allowed as $key) { |
|
| 620 | + list($ns, $directive) = $key; |
|
| 621 | + $skey = "$ns.$directive"; |
|
| 622 | + if (!empty($array["Null_$skey"])) { |
|
| 623 | + $ret[$ns][$directive] = null; |
|
| 624 | + continue; |
|
| 625 | + } |
|
| 626 | + if (!isset($array[$skey])) continue; |
|
| 627 | + $value = $mq ? stripslashes($array[$skey]) : $array[$skey]; |
|
| 628 | + $ret[$ns][$directive] = $value; |
|
| 629 | + } |
|
| 630 | + return $ret; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * Loads configuration values from an ini file |
|
| 635 | + * @param $filename Name of ini file |
|
| 636 | + */ |
|
| 637 | + public function loadIni($filename) { |
|
| 638 | + if ($this->isFinalized('Cannot load directives after finalization')) return; |
|
| 639 | + $array = parse_ini_file($filename, true); |
|
| 640 | + $this->loadArray($array); |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + /** |
|
| 644 | + * Checks whether or not the configuration object is finalized. |
|
| 645 | + * @param $error String error message, or false for no error |
|
| 646 | + */ |
|
| 647 | + public function isFinalized($error = false) { |
|
| 648 | + if ($this->finalized && $error) { |
|
| 649 | + $this->triggerError($error, E_USER_ERROR); |
|
| 650 | + } |
|
| 651 | + return $this->finalized; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * Finalizes configuration only if auto finalize is on and not |
|
| 656 | + * already finalized |
|
| 657 | + */ |
|
| 658 | + public function autoFinalize() { |
|
| 659 | + if ($this->autoFinalize) { |
|
| 660 | + $this->finalize(); |
|
| 661 | + } else { |
|
| 662 | + $this->plist->squash(true); |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * Finalizes a configuration object, prohibiting further change |
|
| 668 | + */ |
|
| 669 | + public function finalize() { |
|
| 670 | + $this->finalized = true; |
|
| 671 | + $this->parser = null; |
|
| 672 | + } |
|
| 673 | + |
|
| 674 | + /** |
|
| 675 | + * Produces a nicely formatted error message by supplying the |
|
| 676 | + * stack frame information OUTSIDE of HTMLPurifier_Config. |
|
| 677 | + */ |
|
| 678 | + protected function triggerError($msg, $no) { |
|
| 679 | + // determine previous stack frame |
|
| 680 | + $extra = ''; |
|
| 681 | + if ($this->chatty) { |
|
| 682 | + $trace = debug_backtrace(); |
|
| 683 | + // zip(tail(trace), trace) -- but PHP is not Haskell har har |
|
| 684 | + for ($i = 0, $c = count($trace); $i < $c - 1; $i++) { |
|
| 685 | + if ($trace[$i + 1]['class'] === 'HTMLPurifier_Config') { |
|
| 686 | + continue; |
|
| 687 | + } |
|
| 688 | + $frame = $trace[$i]; |
|
| 689 | + $extra = " invoked on line {$frame['line']} in file {$frame['file']}"; |
|
| 690 | + break; |
|
| 691 | + } |
|
| 692 | + } |
|
| 693 | + trigger_error($msg . $extra, $no); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * Returns a serialized form of the configuration object that can |
|
| 698 | + * be reconstituted. |
|
| 699 | + */ |
|
| 700 | + public function serialize() { |
|
| 701 | + $this->getDefinition('HTML'); |
|
| 702 | + $this->getDefinition('CSS'); |
|
| 703 | + $this->getDefinition('URI'); |
|
| 704 | + return serialize($this); |
|
| 705 | + } |
|
| 706 | 706 | |
| 707 | 707 | } |
| 708 | 708 | |
@@ -116,8 +116,11 @@ discard block |
||
| 116 | 116 | } else { |
| 117 | 117 | $ret = new HTMLPurifier_Config($schema); |
| 118 | 118 | } |
| 119 | - if (is_string($config)) $ret->loadIni($config); |
|
| 120 | - elseif (is_array($config)) $ret->loadArray($config); |
|
| 119 | + if (is_string($config)) { |
|
| 120 | + $ret->loadIni($config); |
|
| 121 | + } elseif (is_array($config)) { |
|
| 122 | + $ret->loadArray($config); |
|
| 123 | + } |
|
| 121 | 124 | return $ret; |
| 122 | 125 | } |
| 123 | 126 | |
@@ -150,7 +153,9 @@ discard block |
||
| 150 | 153 | $this->triggerError("Using deprecated API: use \$config->get('$key.$a') instead", E_USER_WARNING); |
| 151 | 154 | $key = "$key.$a"; |
| 152 | 155 | } |
| 153 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 156 | + if (!$this->finalized) { |
|
| 157 | + $this->autoFinalize(); |
|
| 158 | + } |
|
| 154 | 159 | if (!isset($this->def->info[$key])) { |
| 155 | 160 | // can't add % due to SimpleTest bug |
| 156 | 161 | $this->triggerError('Cannot retrieve value of undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
@@ -178,7 +183,9 @@ discard block |
||
| 178 | 183 | * @param $namespace String namespace |
| 179 | 184 | */ |
| 180 | 185 | public function getBatch($namespace) { |
| 181 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 186 | + if (!$this->finalized) { |
|
| 187 | + $this->autoFinalize(); |
|
| 188 | + } |
|
| 182 | 189 | $full = $this->getAll(); |
| 183 | 190 | if (!isset($full[$namespace])) { |
| 184 | 191 | $this->triggerError('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
@@ -220,7 +227,9 @@ discard block |
||
| 220 | 227 | * @warning This is a pretty inefficient function, avoid if you can |
| 221 | 228 | */ |
| 222 | 229 | public function getAll() { |
| 223 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 230 | + if (!$this->finalized) { |
|
| 231 | + $this->autoFinalize(); |
|
| 232 | + } |
|
| 224 | 233 | $ret = array(); |
| 225 | 234 | foreach ($this->plist->squash() as $name => $value) { |
| 226 | 235 | list($ns, $key) = explode('.', $name, 2); |
@@ -244,7 +253,9 @@ discard block |
||
| 244 | 253 | } else { |
| 245 | 254 | list($namespace) = explode('.', $key); |
| 246 | 255 | } |
| 247 | - if ($this->isFinalized('Cannot set directive after finalization')) return; |
|
| 256 | + if ($this->isFinalized('Cannot set directive after finalization')) { |
|
| 257 | + return; |
|
| 258 | + } |
|
| 248 | 259 | if (!isset($this->def->info[$key])) { |
| 249 | 260 | $this->triggerError('Cannot set undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false) . ' to value', |
| 250 | 261 | E_USER_WARNING); |
@@ -311,7 +322,9 @@ discard block |
||
| 311 | 322 | */ |
| 312 | 323 | private function _listify($lookup) { |
| 313 | 324 | $list = array(); |
| 314 | - foreach ($lookup as $name => $b) $list[] = $name; |
|
| 325 | + foreach ($lookup as $name => $b) { |
|
| 326 | + $list[] = $name; |
|
| 327 | + } |
|
| 315 | 328 | return implode(', ', $list); |
| 316 | 329 | } |
| 317 | 330 | |
@@ -377,7 +390,9 @@ discard block |
||
| 377 | 390 | if ($optimized && !$raw) { |
| 378 | 391 | throw new HTMLPurifier_Exception("Cannot set optimized = true when raw = false"); |
| 379 | 392 | } |
| 380 | - if (!$this->finalized) $this->autoFinalize(); |
|
| 393 | + if (!$this->finalized) { |
|
| 394 | + $this->autoFinalize(); |
|
| 395 | + } |
|
| 381 | 396 | // temporarily suspend locks, so we can handle recursive definition calls |
| 382 | 397 | $lock = $this->lock; |
| 383 | 398 | $this->lock = null; |
@@ -395,7 +410,9 @@ discard block |
||
| 395 | 410 | return $def; |
| 396 | 411 | } else { |
| 397 | 412 | $def->setup($this); |
| 398 | - if ($def->optimized) $cache->add($def, $this); |
|
| 413 | + if ($def->optimized) { |
|
| 414 | + $cache->add($def, $this); |
|
| 415 | + } |
|
| 399 | 416 | return $def; |
| 400 | 417 | } |
| 401 | 418 | } |
@@ -524,7 +541,9 @@ discard block |
||
| 524 | 541 | * @param $config_array Configuration associative array |
| 525 | 542 | */ |
| 526 | 543 | public function loadArray($config_array) { |
| 527 | - if ($this->isFinalized('Cannot load directives after finalization')) return; |
|
| 544 | + if ($this->isFinalized('Cannot load directives after finalization')) { |
|
| 545 | + return; |
|
| 546 | + } |
|
| 528 | 547 | foreach ($config_array as $key => $value) { |
| 529 | 548 | $key = str_replace('_', '.', $key); |
| 530 | 549 | if (strpos($key, '.') !== false) { |
@@ -550,7 +569,9 @@ discard block |
||
| 550 | 569 | $schema = HTMLPurifier_ConfigSchema::instance(); |
| 551 | 570 | } |
| 552 | 571 | if ($allowed !== true) { |
| 553 | - if (is_string($allowed)) $allowed = array($allowed); |
|
| 572 | + if (is_string($allowed)) { |
|
| 573 | + $allowed = array($allowed); |
|
| 574 | + } |
|
| 554 | 575 | $allowed_ns = array(); |
| 555 | 576 | $allowed_directives = array(); |
| 556 | 577 | $blacklisted_directives = array(); |
@@ -572,11 +593,19 @@ discard block |
||
| 572 | 593 | foreach ($schema->info as $key => $def) { |
| 573 | 594 | list($ns, $directive) = explode('.', $key, 2); |
| 574 | 595 | if ($allowed !== true) { |
| 575 | - if (isset($blacklisted_directives["$ns.$directive"])) continue; |
|
| 576 | - if (!isset($allowed_directives["$ns.$directive"]) && !isset($allowed_ns[$ns])) continue; |
|
| 596 | + if (isset($blacklisted_directives["$ns.$directive"])) { |
|
| 597 | + continue; |
|
| 598 | + } |
|
| 599 | + if (!isset($allowed_directives["$ns.$directive"]) && !isset($allowed_ns[$ns])) { |
|
| 600 | + continue; |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + if (isset($def->isAlias)) { |
|
| 604 | + continue; |
|
| 605 | + } |
|
| 606 | + if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') { |
|
| 607 | + continue; |
|
| 577 | 608 | } |
| 578 | - if (isset($def->isAlias)) continue; |
|
| 579 | - if ($directive == 'DefinitionID' || $directive == 'DefinitionRev') continue; |
|
| 580 | 609 | $ret[] = array($ns, $directive); |
| 581 | 610 | } |
| 582 | 611 | return $ret; |
@@ -611,7 +640,9 @@ discard block |
||
| 611 | 640 | * strict parts of HTMLPurifier_Config |
| 612 | 641 | */ |
| 613 | 642 | public static function prepareArrayFromForm($array, $index = false, $allowed = true, $mq_fix = true, $schema = null) { |
| 614 | - if ($index !== false) $array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array(); |
|
| 643 | + if ($index !== false) { |
|
| 644 | + $array = (isset($array[$index]) && is_array($array[$index])) ? $array[$index] : array(); |
|
| 645 | + } |
|
| 615 | 646 | $mq = $mq_fix && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc(); |
| 616 | 647 | |
| 617 | 648 | $allowed = HTMLPurifier_Config::getAllowedDirectivesForForm($allowed, $schema); |
@@ -623,7 +654,9 @@ discard block |
||
| 623 | 654 | $ret[$ns][$directive] = null; |
| 624 | 655 | continue; |
| 625 | 656 | } |
| 626 | - if (!isset($array[$skey])) continue; |
|
| 657 | + if (!isset($array[$skey])) { |
|
| 658 | + continue; |
|
| 659 | + } |
|
| 627 | 660 | $value = $mq ? stripslashes($array[$skey]) : $array[$skey]; |
| 628 | 661 | $ret[$ns][$directive] = $value; |
| 629 | 662 | } |
@@ -635,7 +668,9 @@ discard block |
||
| 635 | 668 | * @param $filename Name of ini file |
| 636 | 669 | */ |
| 637 | 670 | public function loadIni($filename) { |
| 638 | - if ($this->isFinalized('Cannot load directives after finalization')) return; |
|
| 671 | + if ($this->isFinalized('Cannot load directives after finalization')) { |
|
| 672 | + return; |
|
| 673 | + } |
|
| 639 | 674 | $array = parse_ini_file($filename, true); |
| 640 | 675 | $this->loadArray($array); |
| 641 | 676 | } |
@@ -153,20 +153,20 @@ discard block |
||
| 153 | 153 | if (!$this->finalized) $this->autoFinalize(); |
| 154 | 154 | if (!isset($this->def->info[$key])) { |
| 155 | 155 | // can't add % due to SimpleTest bug |
| 156 | - $this->triggerError('Cannot retrieve value of undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 156 | + $this->triggerError('Cannot retrieve value of undefined directive '.htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 157 | 157 | E_USER_WARNING); |
| 158 | 158 | return; |
| 159 | 159 | } |
| 160 | 160 | if (isset($this->def->info[$key]->isAlias)) { |
| 161 | 161 | $d = $this->def->info[$key]; |
| 162 | - $this->triggerError('Cannot get value from aliased directive, use real name ' . $d->key, |
|
| 162 | + $this->triggerError('Cannot get value from aliased directive, use real name '.$d->key, |
|
| 163 | 163 | E_USER_ERROR); |
| 164 | 164 | return; |
| 165 | 165 | } |
| 166 | 166 | if ($this->lock) { |
| 167 | 167 | list($ns) = explode('.', $key); |
| 168 | 168 | if ($ns !== $this->lock) { |
| 169 | - $this->triggerError('Cannot get value of namespace ' . $ns . ' when lock for ' . $this->lock . ' is active, this probably indicates a Definition setup method is accessing directives that are not within its namespace', E_USER_ERROR); |
|
| 169 | + $this->triggerError('Cannot get value of namespace '.$ns.' when lock for '.$this->lock.' is active, this probably indicates a Definition setup method is accessing directives that are not within its namespace', E_USER_ERROR); |
|
| 170 | 170 | return; |
| 171 | 171 | } |
| 172 | 172 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | if (!$this->finalized) $this->autoFinalize(); |
| 182 | 182 | $full = $this->getAll(); |
| 183 | 183 | if (!isset($full[$namespace])) { |
| 184 | - $this->triggerError('Cannot retrieve undefined namespace ' . htmlspecialchars($namespace, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 184 | + $this->triggerError('Cannot retrieve undefined namespace '.htmlspecialchars($namespace, ENT_COMPAT | ENT_HTML401, 'UTF-8', false), |
|
| 185 | 185 | E_USER_WARNING); |
| 186 | 186 | return; |
| 187 | 187 | } |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | } |
| 247 | 247 | if ($this->isFinalized('Cannot set directive after finalization')) return; |
| 248 | 248 | if (!isset($this->def->info[$key])) { |
| 249 | - $this->triggerError('Cannot set undefined directive ' . htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false) . ' to value', |
|
| 249 | + $this->triggerError('Cannot set undefined directive '.htmlspecialchars($key, ENT_COMPAT | ENT_HTML401, 'UTF-8', false).' to value', |
|
| 250 | 250 | E_USER_WARNING); |
| 251 | 251 | return; |
| 252 | 252 | } |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | if (isset($def->isAlias)) { |
| 256 | 256 | if ($this->aliasMode) { |
| 257 | 257 | $this->triggerError('Double-aliases not allowed, please fix '. |
| 258 | - 'ConfigSchema bug with' . $key, E_USER_ERROR); |
|
| 258 | + 'ConfigSchema bug with'.$key, E_USER_ERROR); |
|
| 259 | 259 | return; |
| 260 | 260 | } |
| 261 | 261 | $this->aliasMode = true; |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | try { |
| 280 | 280 | $value = $this->parser->parse($value, $type, $allow_null); |
| 281 | 281 | } catch (HTMLPurifier_VarParserException $e) { |
| 282 | - $this->triggerError('Value for ' . $key . ' is of invalid type, should be ' . HTMLPurifier_VarParser::getTypeName($type), E_USER_WARNING); |
|
| 282 | + $this->triggerError('Value for '.$key.' is of invalid type, should be '.HTMLPurifier_VarParser::getTypeName($type), E_USER_WARNING); |
|
| 283 | 283 | return; |
| 284 | 284 | } |
| 285 | 285 | if (is_string($value) && is_object($def)) { |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | } |
| 290 | 290 | // check to see if the value is allowed |
| 291 | 291 | if (isset($def->allowed) && !isset($def->allowed[$value])) { |
| 292 | - $this->triggerError('Value not supported, valid values are: ' . |
|
| 292 | + $this->triggerError('Value not supported, valid values are: '. |
|
| 293 | 293 | $this->_listify($def->allowed), E_USER_WARNING); |
| 294 | 294 | return; |
| 295 | 295 | } |
@@ -422,7 +422,7 @@ discard block |
||
| 422 | 422 | // check preconditions |
| 423 | 423 | $def = null; |
| 424 | 424 | if ($optimized) { |
| 425 | - if (is_null($this->get($type . '.DefinitionID'))) { |
|
| 425 | + if (is_null($this->get($type.'.DefinitionID'))) { |
|
| 426 | 426 | // fatally error out if definition ID not set |
| 427 | 427 | throw new HTMLPurifier_Exception("Cannot retrieve raw version without specifying %$type.DefinitionID"); |
| 428 | 428 | } |
@@ -431,16 +431,16 @@ discard block |
||
| 431 | 431 | $def = $this->definitions[$type]; |
| 432 | 432 | if ($def->setup && !$optimized) { |
| 433 | 433 | $extra = $this->chatty ? " (try moving this code block earlier in your initialization)" : ""; |
| 434 | - throw new HTMLPurifier_Exception("Cannot retrieve raw definition after it has already been setup" . $extra); |
|
| 434 | + throw new HTMLPurifier_Exception("Cannot retrieve raw definition after it has already been setup".$extra); |
|
| 435 | 435 | } |
| 436 | 436 | if ($def->optimized === null) { |
| 437 | 437 | $extra = $this->chatty ? " (try flushing your cache)" : ""; |
| 438 | - throw new HTMLPurifier_Exception("Optimization status of definition is unknown" . $extra); |
|
| 438 | + throw new HTMLPurifier_Exception("Optimization status of definition is unknown".$extra); |
|
| 439 | 439 | } |
| 440 | 440 | if ($def->optimized !== $optimized) { |
| 441 | 441 | $msg = $optimized ? "optimized" : "unoptimized"; |
| 442 | 442 | $extra = $this->chatty ? " (this backtrace is for the first inconsistent call, which was for a $msg raw definition)" : ""; |
| 443 | - throw new HTMLPurifier_Exception("Inconsistent use of optimized and unoptimized raw definition retrievals" . $extra); |
|
| 443 | + throw new HTMLPurifier_Exception("Inconsistent use of optimized and unoptimized raw definition retrievals".$extra); |
|
| 444 | 444 | } |
| 445 | 445 | } |
| 446 | 446 | // check if definition was in memory |
@@ -471,7 +471,7 @@ discard block |
||
| 471 | 471 | } |
| 472 | 472 | // check invariants for creation |
| 473 | 473 | if (!$optimized) { |
| 474 | - if (!is_null($this->get($type . '.DefinitionID'))) { |
|
| 474 | + if (!is_null($this->get($type.'.DefinitionID'))) { |
|
| 475 | 475 | if ($this->chatty) { |
| 476 | 476 | $this->triggerError("Due to a documentation error in previous version of HTML Purifier, your definitions are not being cached. If this is OK, you can remove the %$type.DefinitionRev and %$type.DefinitionID declaration. Otherwise, modify your code to use maybeGetRawDefinition, and test if the returned value is null before making any edits (if it is null, that means that a cached version is available, and no raw operations are necessary). See <a href='http://htmlpurifier.org/docs/enduser-customize.html#optimized'>Customize</a> for more details", E_USER_WARNING); |
| 477 | 477 | } else { |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | $namespace = $key; |
| 534 | 534 | $namespace_values = $value; |
| 535 | 535 | foreach ($namespace_values as $directive => $value) { |
| 536 | - $this->set($namespace .'.'. $directive, $value); |
|
| 536 | + $this->set($namespace.'.'.$directive, $value); |
|
| 537 | 537 | } |
| 538 | 538 | } |
| 539 | 539 | } |
@@ -690,7 +690,7 @@ discard block |
||
| 690 | 690 | break; |
| 691 | 691 | } |
| 692 | 692 | } |
| 693 | - trigger_error($msg . $extra, $no); |
|
| 693 | + trigger_error($msg.$extra, $no); |
|
| 694 | 694 | } |
| 695 | 695 | |
| 696 | 696 | /** |
@@ -89,6 +89,7 @@ discard block |
||
| 89 | 89 | /** |
| 90 | 90 | * @param $definition HTMLPurifier_ConfigSchema that defines what directives |
| 91 | 91 | * are allowed. |
| 92 | + * @param HTMLPurifier_PropertyList $parent |
|
| 92 | 93 | */ |
| 93 | 94 | public function __construct($definition, $parent = null) { |
| 94 | 95 | $parent = $parent ? $parent : $definition->defaultPlist; |
@@ -104,7 +105,7 @@ discard block |
||
| 104 | 105 | * an array of directives based on loadArray(), |
| 105 | 106 | * or a string filename of an ini file. |
| 106 | 107 | * @param HTMLPurifier_ConfigSchema Schema object |
| 107 | - * @return Configured HTMLPurifier_Config object |
|
| 108 | + * @return HTMLPurifier_Config HTMLPurifier_Config object |
|
| 108 | 109 | */ |
| 109 | 110 | public static function create($config, $schema = null) { |
| 110 | 111 | if ($config instanceof HTMLPurifier_Config) { |
@@ -133,7 +134,7 @@ discard block |
||
| 133 | 134 | |
| 134 | 135 | /** |
| 135 | 136 | * Convenience constructor that creates a default configuration object. |
| 136 | - * @return Default HTMLPurifier_Config object. |
|
| 137 | + * @return HTMLPurifier_Config HTMLPurifier_Config object. |
|
| 137 | 138 | */ |
| 138 | 139 | public static function createDefault() { |
| 139 | 140 | $definition = HTMLPurifier_ConfigSchema::instance(); |
@@ -143,7 +144,7 @@ discard block |
||
| 143 | 144 | |
| 144 | 145 | /** |
| 145 | 146 | * Retreives a value from the configuration. |
| 146 | - * @param $key String key |
|
| 147 | + * @param string $key String key |
|
| 147 | 148 | */ |
| 148 | 149 | public function get($key, $a = null) { |
| 149 | 150 | if ($a !== null) { |
@@ -543,7 +544,7 @@ discard block |
||
| 543 | 544 | * Returns a list of array(namespace, directive) for all directives |
| 544 | 545 | * that are allowed in a web-form context as per an allowed |
| 545 | 546 | * namespaces/directives list. |
| 546 | - * @param $allowed List of allowed namespaces/directives |
|
| 547 | + * @param boolean $allowed List of allowed namespaces/directives |
|
| 547 | 548 | */ |
| 548 | 549 | public static function getAllowedDirectivesForForm($allowed, $schema = null) { |
| 549 | 550 | if (!$schema) { |
@@ -585,7 +586,6 @@ discard block |
||
| 585 | 586 | /** |
| 586 | 587 | * Loads configuration values from $_GET/$_POST that were posted |
| 587 | 588 | * via ConfigForm |
| 588 | - * @param $array $_GET or $_POST array to import |
|
| 589 | 589 | * @param $index Index/name that the config variables are in |
| 590 | 590 | * @param $allowed List of allowed namespaces/directives |
| 591 | 591 | * @param $mq_fix Boolean whether or not to enable magic quotes fix |
@@ -632,7 +632,7 @@ discard block |
||
| 632 | 632 | |
| 633 | 633 | /** |
| 634 | 634 | * Loads configuration values from an ini file |
| 635 | - * @param $filename Name of ini file |
|
| 635 | + * @param string $filename Name of ini file |
|
| 636 | 636 | */ |
| 637 | 637 | public function loadIni($filename) { |
| 638 | 638 | if ($this->isFinalized('Cannot load directives after finalization')) return; |
@@ -674,6 +674,7 @@ discard block |
||
| 674 | 674 | /** |
| 675 | 675 | * Produces a nicely formatted error message by supplying the |
| 676 | 676 | * stack frame information OUTSIDE of HTMLPurifier_Config. |
| 677 | + * @param integer $no |
|
| 677 | 678 | */ |
| 678 | 679 | protected function triggerError($msg, $no) { |
| 679 | 680 | // determine previous stack frame |
@@ -5,159 +5,159 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | class HTMLPurifier_ConfigSchema { |
| 7 | 7 | |
| 8 | - /** |
|
| 9 | - * Defaults of the directives and namespaces. |
|
| 10 | - * @note This shares the exact same structure as HTMLPurifier_Config::$conf |
|
| 11 | - */ |
|
| 12 | - public $defaults = array(); |
|
| 8 | + /** |
|
| 9 | + * Defaults of the directives and namespaces. |
|
| 10 | + * @note This shares the exact same structure as HTMLPurifier_Config::$conf |
|
| 11 | + */ |
|
| 12 | + public $defaults = array(); |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * The default property list. Do not edit this property list. |
|
| 16 | - */ |
|
| 17 | - public $defaultPlist; |
|
| 14 | + /** |
|
| 15 | + * The default property list. Do not edit this property list. |
|
| 16 | + */ |
|
| 17 | + public $defaultPlist; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Definition of the directives. The structure of this is: |
|
| 21 | - * |
|
| 22 | - * array( |
|
| 23 | - * 'Namespace' => array( |
|
| 24 | - * 'Directive' => new stdclass(), |
|
| 25 | - * ) |
|
| 26 | - * ) |
|
| 27 | - * |
|
| 28 | - * The stdclass may have the following properties: |
|
| 29 | - * |
|
| 30 | - * - If isAlias isn't set: |
|
| 31 | - * - type: Integer type of directive, see HTMLPurifier_VarParser for definitions |
|
| 32 | - * - allow_null: If set, this directive allows null values |
|
| 33 | - * - aliases: If set, an associative array of value aliases to real values |
|
| 34 | - * - allowed: If set, a lookup array of allowed (string) values |
|
| 35 | - * - If isAlias is set: |
|
| 36 | - * - namespace: Namespace this directive aliases to |
|
| 37 | - * - name: Directive name this directive aliases to |
|
| 38 | - * |
|
| 39 | - * In certain degenerate cases, stdclass will actually be an integer. In |
|
| 40 | - * that case, the value is equivalent to an stdclass with the type |
|
| 41 | - * property set to the integer. If the integer is negative, type is |
|
| 42 | - * equal to the absolute value of integer, and allow_null is true. |
|
| 43 | - * |
|
| 44 | - * This class is friendly with HTMLPurifier_Config. If you need introspection |
|
| 45 | - * about the schema, you're better of using the ConfigSchema_Interchange, |
|
| 46 | - * which uses more memory but has much richer information. |
|
| 47 | - */ |
|
| 48 | - public $info = array(); |
|
| 19 | + /** |
|
| 20 | + * Definition of the directives. The structure of this is: |
|
| 21 | + * |
|
| 22 | + * array( |
|
| 23 | + * 'Namespace' => array( |
|
| 24 | + * 'Directive' => new stdclass(), |
|
| 25 | + * ) |
|
| 26 | + * ) |
|
| 27 | + * |
|
| 28 | + * The stdclass may have the following properties: |
|
| 29 | + * |
|
| 30 | + * - If isAlias isn't set: |
|
| 31 | + * - type: Integer type of directive, see HTMLPurifier_VarParser for definitions |
|
| 32 | + * - allow_null: If set, this directive allows null values |
|
| 33 | + * - aliases: If set, an associative array of value aliases to real values |
|
| 34 | + * - allowed: If set, a lookup array of allowed (string) values |
|
| 35 | + * - If isAlias is set: |
|
| 36 | + * - namespace: Namespace this directive aliases to |
|
| 37 | + * - name: Directive name this directive aliases to |
|
| 38 | + * |
|
| 39 | + * In certain degenerate cases, stdclass will actually be an integer. In |
|
| 40 | + * that case, the value is equivalent to an stdclass with the type |
|
| 41 | + * property set to the integer. If the integer is negative, type is |
|
| 42 | + * equal to the absolute value of integer, and allow_null is true. |
|
| 43 | + * |
|
| 44 | + * This class is friendly with HTMLPurifier_Config. If you need introspection |
|
| 45 | + * about the schema, you're better of using the ConfigSchema_Interchange, |
|
| 46 | + * which uses more memory but has much richer information. |
|
| 47 | + */ |
|
| 48 | + public $info = array(); |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Application-wide singleton |
|
| 52 | - */ |
|
| 53 | - static protected $singleton; |
|
| 50 | + /** |
|
| 51 | + * Application-wide singleton |
|
| 52 | + */ |
|
| 53 | + static protected $singleton; |
|
| 54 | 54 | |
| 55 | - public function __construct() { |
|
| 56 | - $this->defaultPlist = new HTMLPurifier_PropertyList(); |
|
| 57 | - } |
|
| 55 | + public function __construct() { |
|
| 56 | + $this->defaultPlist = new HTMLPurifier_PropertyList(); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Unserializes the default ConfigSchema. |
|
| 61 | - */ |
|
| 62 | - public static function makeFromSerial() { |
|
| 63 | - $contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'); |
|
| 64 | - $r = unserialize($contents); |
|
| 65 | - if (!$r) { |
|
| 66 | - $hash = sha1($contents); |
|
| 67 | - trigger_error("Unserialization of configuration schema failed, sha1 of file was $hash", E_USER_ERROR); |
|
| 68 | - } |
|
| 69 | - return $r; |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * Unserializes the default ConfigSchema. |
|
| 61 | + */ |
|
| 62 | + public static function makeFromSerial() { |
|
| 63 | + $contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'); |
|
| 64 | + $r = unserialize($contents); |
|
| 65 | + if (!$r) { |
|
| 66 | + $hash = sha1($contents); |
|
| 67 | + trigger_error("Unserialization of configuration schema failed, sha1 of file was $hash", E_USER_ERROR); |
|
| 68 | + } |
|
| 69 | + return $r; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Retrieves an instance of the application-wide configuration definition. |
|
| 74 | - */ |
|
| 75 | - public static function instance($prototype = null) { |
|
| 76 | - if ($prototype !== null) { |
|
| 77 | - HTMLPurifier_ConfigSchema::$singleton = $prototype; |
|
| 78 | - } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) { |
|
| 79 | - HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial(); |
|
| 80 | - } |
|
| 81 | - return HTMLPurifier_ConfigSchema::$singleton; |
|
| 82 | - } |
|
| 72 | + /** |
|
| 73 | + * Retrieves an instance of the application-wide configuration definition. |
|
| 74 | + */ |
|
| 75 | + public static function instance($prototype = null) { |
|
| 76 | + if ($prototype !== null) { |
|
| 77 | + HTMLPurifier_ConfigSchema::$singleton = $prototype; |
|
| 78 | + } elseif (HTMLPurifier_ConfigSchema::$singleton === null || $prototype === true) { |
|
| 79 | + HTMLPurifier_ConfigSchema::$singleton = HTMLPurifier_ConfigSchema::makeFromSerial(); |
|
| 80 | + } |
|
| 81 | + return HTMLPurifier_ConfigSchema::$singleton; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Defines a directive for configuration |
|
| 86 | - * @warning Will fail of directive's namespace is defined. |
|
| 87 | - * @warning This method's signature is slightly different from the legacy |
|
| 88 | - * define() static method! Beware! |
|
| 89 | - * @param $namespace Namespace the directive is in |
|
| 90 | - * @param $name Key of directive |
|
| 91 | - * @param $default Default value of directive |
|
| 92 | - * @param $type Allowed type of the directive. See |
|
| 93 | - * HTMLPurifier_DirectiveDef::$type for allowed values |
|
| 94 | - * @param $allow_null Whether or not to allow null values |
|
| 95 | - */ |
|
| 96 | - public function add($key, $default, $type, $allow_null) { |
|
| 97 | - $obj = new stdclass(); |
|
| 98 | - $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type]; |
|
| 99 | - if ($allow_null) $obj->allow_null = true; |
|
| 100 | - $this->info[$key] = $obj; |
|
| 101 | - $this->defaults[$key] = $default; |
|
| 102 | - $this->defaultPlist->set($key, $default); |
|
| 103 | - } |
|
| 84 | + /** |
|
| 85 | + * Defines a directive for configuration |
|
| 86 | + * @warning Will fail of directive's namespace is defined. |
|
| 87 | + * @warning This method's signature is slightly different from the legacy |
|
| 88 | + * define() static method! Beware! |
|
| 89 | + * @param $namespace Namespace the directive is in |
|
| 90 | + * @param $name Key of directive |
|
| 91 | + * @param $default Default value of directive |
|
| 92 | + * @param $type Allowed type of the directive. See |
|
| 93 | + * HTMLPurifier_DirectiveDef::$type for allowed values |
|
| 94 | + * @param $allow_null Whether or not to allow null values |
|
| 95 | + */ |
|
| 96 | + public function add($key, $default, $type, $allow_null) { |
|
| 97 | + $obj = new stdclass(); |
|
| 98 | + $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type]; |
|
| 99 | + if ($allow_null) $obj->allow_null = true; |
|
| 100 | + $this->info[$key] = $obj; |
|
| 101 | + $this->defaults[$key] = $default; |
|
| 102 | + $this->defaultPlist->set($key, $default); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Defines a directive value alias. |
|
| 107 | - * |
|
| 108 | - * Directive value aliases are convenient for developers because it lets |
|
| 109 | - * them set a directive to several values and get the same result. |
|
| 110 | - * @param $namespace Directive's namespace |
|
| 111 | - * @param $name Name of Directive |
|
| 112 | - * @param $aliases Hash of aliased values to the real alias |
|
| 113 | - */ |
|
| 114 | - public function addValueAliases($key, $aliases) { |
|
| 115 | - if (!isset($this->info[$key]->aliases)) { |
|
| 116 | - $this->info[$key]->aliases = array(); |
|
| 117 | - } |
|
| 118 | - foreach ($aliases as $alias => $real) { |
|
| 119 | - $this->info[$key]->aliases[$alias] = $real; |
|
| 120 | - } |
|
| 121 | - } |
|
| 105 | + /** |
|
| 106 | + * Defines a directive value alias. |
|
| 107 | + * |
|
| 108 | + * Directive value aliases are convenient for developers because it lets |
|
| 109 | + * them set a directive to several values and get the same result. |
|
| 110 | + * @param $namespace Directive's namespace |
|
| 111 | + * @param $name Name of Directive |
|
| 112 | + * @param $aliases Hash of aliased values to the real alias |
|
| 113 | + */ |
|
| 114 | + public function addValueAliases($key, $aliases) { |
|
| 115 | + if (!isset($this->info[$key]->aliases)) { |
|
| 116 | + $this->info[$key]->aliases = array(); |
|
| 117 | + } |
|
| 118 | + foreach ($aliases as $alias => $real) { |
|
| 119 | + $this->info[$key]->aliases[$alias] = $real; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Defines a set of allowed values for a directive. |
|
| 125 | - * @warning This is slightly different from the corresponding static |
|
| 126 | - * method definition. |
|
| 127 | - * @param $namespace Namespace of directive |
|
| 128 | - * @param $name Name of directive |
|
| 129 | - * @param $allowed Lookup array of allowed values |
|
| 130 | - */ |
|
| 131 | - public function addAllowedValues($key, $allowed) { |
|
| 132 | - $this->info[$key]->allowed = $allowed; |
|
| 133 | - } |
|
| 123 | + /** |
|
| 124 | + * Defines a set of allowed values for a directive. |
|
| 125 | + * @warning This is slightly different from the corresponding static |
|
| 126 | + * method definition. |
|
| 127 | + * @param $namespace Namespace of directive |
|
| 128 | + * @param $name Name of directive |
|
| 129 | + * @param $allowed Lookup array of allowed values |
|
| 130 | + */ |
|
| 131 | + public function addAllowedValues($key, $allowed) { |
|
| 132 | + $this->info[$key]->allowed = $allowed; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Defines a directive alias for backwards compatibility |
|
| 137 | - * @param $namespace |
|
| 138 | - * @param $name Directive that will be aliased |
|
| 139 | - * @param $new_namespace |
|
| 140 | - * @param $new_name Directive that the alias will be to |
|
| 141 | - */ |
|
| 142 | - public function addAlias($key, $new_key) { |
|
| 143 | - $obj = new stdclass; |
|
| 144 | - $obj->key = $new_key; |
|
| 145 | - $obj->isAlias = true; |
|
| 146 | - $this->info[$key] = $obj; |
|
| 147 | - } |
|
| 135 | + /** |
|
| 136 | + * Defines a directive alias for backwards compatibility |
|
| 137 | + * @param $namespace |
|
| 138 | + * @param $name Directive that will be aliased |
|
| 139 | + * @param $new_namespace |
|
| 140 | + * @param $new_name Directive that the alias will be to |
|
| 141 | + */ |
|
| 142 | + public function addAlias($key, $new_key) { |
|
| 143 | + $obj = new stdclass; |
|
| 144 | + $obj->key = $new_key; |
|
| 145 | + $obj->isAlias = true; |
|
| 146 | + $this->info[$key] = $obj; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Replaces any stdclass that only has the type property with type integer. |
|
| 151 | - */ |
|
| 152 | - public function postProcess() { |
|
| 153 | - foreach ($this->info as $key => $v) { |
|
| 154 | - if (count((array) $v) == 1) { |
|
| 155 | - $this->info[$key] = $v->type; |
|
| 156 | - } elseif (count((array) $v) == 2 && isset($v->allow_null)) { |
|
| 157 | - $this->info[$key] = -$v->type; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - } |
|
| 149 | + /** |
|
| 150 | + * Replaces any stdclass that only has the type property with type integer. |
|
| 151 | + */ |
|
| 152 | + public function postProcess() { |
|
| 153 | + foreach ($this->info as $key => $v) { |
|
| 154 | + if (count((array) $v) == 1) { |
|
| 155 | + $this->info[$key] = $v->type; |
|
| 156 | + } elseif (count((array) $v) == 2 && isset($v->allow_null)) { |
|
| 157 | + $this->info[$key] = -$v->type; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | 162 | } |
| 163 | 163 | |
@@ -96,7 +96,9 @@ |
||
| 96 | 96 | public function add($key, $default, $type, $allow_null) { |
| 97 | 97 | $obj = new stdclass(); |
| 98 | 98 | $obj->type = is_int($type) ? $type : HTMLPurifier_VarParser::$types[$type]; |
| 99 | - if ($allow_null) $obj->allow_null = true; |
|
| 99 | + if ($allow_null) { |
|
| 100 | + $obj->allow_null = true; |
|
| 101 | + } |
|
| 100 | 102 | $this->info[$key] = $obj; |
| 101 | 103 | $this->defaults[$key] = $default; |
| 102 | 104 | $this->defaultPlist->set($key, $default); |
@@ -60,7 +60,7 @@ |
||
| 60 | 60 | * Unserializes the default ConfigSchema. |
| 61 | 61 | */ |
| 62 | 62 | public static function makeFromSerial() { |
| 63 | - $contents = file_get_contents(HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema.ser'); |
|
| 63 | + $contents = file_get_contents(HTMLPURIFIER_PREFIX.'/HTMLPurifier/ConfigSchema/schema.ser'); |
|
| 64 | 64 | $r = unserialize($contents); |
| 65 | 65 | if (!$r) { |
| 66 | 66 | $hash = sha1($contents); |
@@ -7,37 +7,37 @@ |
||
| 7 | 7 | class HTMLPurifier_ConfigSchema_Builder_ConfigSchema |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - public function build($interchange) { |
|
| 11 | - $schema = new HTMLPurifier_ConfigSchema(); |
|
| 12 | - foreach ($interchange->directives as $d) { |
|
| 13 | - $schema->add( |
|
| 14 | - $d->id->key, |
|
| 15 | - $d->default, |
|
| 16 | - $d->type, |
|
| 17 | - $d->typeAllowsNull |
|
| 18 | - ); |
|
| 19 | - if ($d->allowed !== null) { |
|
| 20 | - $schema->addAllowedValues( |
|
| 21 | - $d->id->key, |
|
| 22 | - $d->allowed |
|
| 23 | - ); |
|
| 24 | - } |
|
| 25 | - foreach ($d->aliases as $alias) { |
|
| 26 | - $schema->addAlias( |
|
| 27 | - $alias->key, |
|
| 28 | - $d->id->key |
|
| 29 | - ); |
|
| 30 | - } |
|
| 31 | - if ($d->valueAliases !== null) { |
|
| 32 | - $schema->addValueAliases( |
|
| 33 | - $d->id->key, |
|
| 34 | - $d->valueAliases |
|
| 35 | - ); |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - $schema->postProcess(); |
|
| 39 | - return $schema; |
|
| 40 | - } |
|
| 10 | + public function build($interchange) { |
|
| 11 | + $schema = new HTMLPurifier_ConfigSchema(); |
|
| 12 | + foreach ($interchange->directives as $d) { |
|
| 13 | + $schema->add( |
|
| 14 | + $d->id->key, |
|
| 15 | + $d->default, |
|
| 16 | + $d->type, |
|
| 17 | + $d->typeAllowsNull |
|
| 18 | + ); |
|
| 19 | + if ($d->allowed !== null) { |
|
| 20 | + $schema->addAllowedValues( |
|
| 21 | + $d->id->key, |
|
| 22 | + $d->allowed |
|
| 23 | + ); |
|
| 24 | + } |
|
| 25 | + foreach ($d->aliases as $alias) { |
|
| 26 | + $schema->addAlias( |
|
| 27 | + $alias->key, |
|
| 28 | + $d->id->key |
|
| 29 | + ); |
|
| 30 | + } |
|
| 31 | + if ($d->valueAliases !== null) { |
|
| 32 | + $schema->addValueAliases( |
|
| 33 | + $d->id->key, |
|
| 34 | + $d->valueAliases |
|
| 35 | + ); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + $schema->postProcess(); |
|
| 39 | + return $schema; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -7,99 +7,99 @@ |
||
| 7 | 7 | class HTMLPurifier_ConfigSchema_Builder_Xml extends XMLWriter |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - protected $interchange; |
|
| 11 | - private $namespace; |
|
| 12 | - |
|
| 13 | - protected function writeHTMLDiv($html) { |
|
| 14 | - $this->startElement('div'); |
|
| 15 | - |
|
| 16 | - $purifier = HTMLPurifier::getInstance(); |
|
| 17 | - $html = $purifier->purify($html); |
|
| 18 | - $this->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml'); |
|
| 19 | - $this->writeRaw($html); |
|
| 20 | - |
|
| 21 | - $this->endElement(); // div |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - protected function export($var) { |
|
| 25 | - if ($var === array()) return 'array()'; |
|
| 26 | - return var_export($var, true); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function build($interchange) { |
|
| 30 | - // global access, only use as last resort |
|
| 31 | - $this->interchange = $interchange; |
|
| 32 | - |
|
| 33 | - $this->setIndent(true); |
|
| 34 | - $this->startDocument('1.0', 'UTF-8'); |
|
| 35 | - $this->startElement('configdoc'); |
|
| 36 | - $this->writeElement('title', $interchange->name); |
|
| 37 | - |
|
| 38 | - foreach ($interchange->directives as $directive) { |
|
| 39 | - $this->buildDirective($directive); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - if ($this->namespace) $this->endElement(); // namespace |
|
| 43 | - |
|
| 44 | - $this->endElement(); // configdoc |
|
| 45 | - $this->flush(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - public function buildDirective($directive) { |
|
| 49 | - |
|
| 50 | - // Kludge, although I suppose having a notion of a "root namespace" |
|
| 51 | - // certainly makes things look nicer when documentation is built. |
|
| 52 | - // Depends on things being sorted. |
|
| 53 | - if (!$this->namespace || $this->namespace !== $directive->id->getRootNamespace()) { |
|
| 54 | - if ($this->namespace) $this->endElement(); // namespace |
|
| 55 | - $this->namespace = $directive->id->getRootNamespace(); |
|
| 56 | - $this->startElement('namespace'); |
|
| 57 | - $this->writeAttribute('id', $this->namespace); |
|
| 58 | - $this->writeElement('name', $this->namespace); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - $this->startElement('directive'); |
|
| 62 | - $this->writeAttribute('id', $directive->id->toString()); |
|
| 63 | - |
|
| 64 | - $this->writeElement('name', $directive->id->getDirective()); |
|
| 65 | - |
|
| 66 | - $this->startElement('aliases'); |
|
| 67 | - foreach ($directive->aliases as $alias) $this->writeElement('alias', $alias->toString()); |
|
| 68 | - $this->endElement(); // aliases |
|
| 69 | - |
|
| 70 | - $this->startElement('constraints'); |
|
| 71 | - if ($directive->version) $this->writeElement('version', $directive->version); |
|
| 72 | - $this->startElement('type'); |
|
| 73 | - if ($directive->typeAllowsNull) $this->writeAttribute('allow-null', 'yes'); |
|
| 74 | - $this->text($directive->type); |
|
| 75 | - $this->endElement(); // type |
|
| 76 | - if ($directive->allowed) { |
|
| 77 | - $this->startElement('allowed'); |
|
| 78 | - foreach ($directive->allowed as $value => $x) $this->writeElement('value', $value); |
|
| 79 | - $this->endElement(); // allowed |
|
| 80 | - } |
|
| 81 | - $this->writeElement('default', $this->export($directive->default)); |
|
| 82 | - $this->writeAttribute('xml:space', 'preserve'); |
|
| 83 | - if ($directive->external) { |
|
| 84 | - $this->startElement('external'); |
|
| 85 | - foreach ($directive->external as $project) $this->writeElement('project', $project); |
|
| 86 | - $this->endElement(); |
|
| 87 | - } |
|
| 88 | - $this->endElement(); // constraints |
|
| 89 | - |
|
| 90 | - if ($directive->deprecatedVersion) { |
|
| 91 | - $this->startElement('deprecated'); |
|
| 92 | - $this->writeElement('version', $directive->deprecatedVersion); |
|
| 93 | - $this->writeElement('use', $directive->deprecatedUse->toString()); |
|
| 94 | - $this->endElement(); // deprecated |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $this->startElement('description'); |
|
| 98 | - $this->writeHTMLDiv($directive->description); |
|
| 99 | - $this->endElement(); // description |
|
| 100 | - |
|
| 101 | - $this->endElement(); // directive |
|
| 102 | - } |
|
| 10 | + protected $interchange; |
|
| 11 | + private $namespace; |
|
| 12 | + |
|
| 13 | + protected function writeHTMLDiv($html) { |
|
| 14 | + $this->startElement('div'); |
|
| 15 | + |
|
| 16 | + $purifier = HTMLPurifier::getInstance(); |
|
| 17 | + $html = $purifier->purify($html); |
|
| 18 | + $this->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml'); |
|
| 19 | + $this->writeRaw($html); |
|
| 20 | + |
|
| 21 | + $this->endElement(); // div |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + protected function export($var) { |
|
| 25 | + if ($var === array()) return 'array()'; |
|
| 26 | + return var_export($var, true); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function build($interchange) { |
|
| 30 | + // global access, only use as last resort |
|
| 31 | + $this->interchange = $interchange; |
|
| 32 | + |
|
| 33 | + $this->setIndent(true); |
|
| 34 | + $this->startDocument('1.0', 'UTF-8'); |
|
| 35 | + $this->startElement('configdoc'); |
|
| 36 | + $this->writeElement('title', $interchange->name); |
|
| 37 | + |
|
| 38 | + foreach ($interchange->directives as $directive) { |
|
| 39 | + $this->buildDirective($directive); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + if ($this->namespace) $this->endElement(); // namespace |
|
| 43 | + |
|
| 44 | + $this->endElement(); // configdoc |
|
| 45 | + $this->flush(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + public function buildDirective($directive) { |
|
| 49 | + |
|
| 50 | + // Kludge, although I suppose having a notion of a "root namespace" |
|
| 51 | + // certainly makes things look nicer when documentation is built. |
|
| 52 | + // Depends on things being sorted. |
|
| 53 | + if (!$this->namespace || $this->namespace !== $directive->id->getRootNamespace()) { |
|
| 54 | + if ($this->namespace) $this->endElement(); // namespace |
|
| 55 | + $this->namespace = $directive->id->getRootNamespace(); |
|
| 56 | + $this->startElement('namespace'); |
|
| 57 | + $this->writeAttribute('id', $this->namespace); |
|
| 58 | + $this->writeElement('name', $this->namespace); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + $this->startElement('directive'); |
|
| 62 | + $this->writeAttribute('id', $directive->id->toString()); |
|
| 63 | + |
|
| 64 | + $this->writeElement('name', $directive->id->getDirective()); |
|
| 65 | + |
|
| 66 | + $this->startElement('aliases'); |
|
| 67 | + foreach ($directive->aliases as $alias) $this->writeElement('alias', $alias->toString()); |
|
| 68 | + $this->endElement(); // aliases |
|
| 69 | + |
|
| 70 | + $this->startElement('constraints'); |
|
| 71 | + if ($directive->version) $this->writeElement('version', $directive->version); |
|
| 72 | + $this->startElement('type'); |
|
| 73 | + if ($directive->typeAllowsNull) $this->writeAttribute('allow-null', 'yes'); |
|
| 74 | + $this->text($directive->type); |
|
| 75 | + $this->endElement(); // type |
|
| 76 | + if ($directive->allowed) { |
|
| 77 | + $this->startElement('allowed'); |
|
| 78 | + foreach ($directive->allowed as $value => $x) $this->writeElement('value', $value); |
|
| 79 | + $this->endElement(); // allowed |
|
| 80 | + } |
|
| 81 | + $this->writeElement('default', $this->export($directive->default)); |
|
| 82 | + $this->writeAttribute('xml:space', 'preserve'); |
|
| 83 | + if ($directive->external) { |
|
| 84 | + $this->startElement('external'); |
|
| 85 | + foreach ($directive->external as $project) $this->writeElement('project', $project); |
|
| 86 | + $this->endElement(); |
|
| 87 | + } |
|
| 88 | + $this->endElement(); // constraints |
|
| 89 | + |
|
| 90 | + if ($directive->deprecatedVersion) { |
|
| 91 | + $this->startElement('deprecated'); |
|
| 92 | + $this->writeElement('version', $directive->deprecatedVersion); |
|
| 93 | + $this->writeElement('use', $directive->deprecatedUse->toString()); |
|
| 94 | + $this->endElement(); // deprecated |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $this->startElement('description'); |
|
| 98 | + $this->writeHTMLDiv($directive->description); |
|
| 99 | + $this->endElement(); // description |
|
| 100 | + |
|
| 101 | + $this->endElement(); // directive |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | 104 | } |
| 105 | 105 | |
@@ -22,7 +22,9 @@ discard block |
||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | protected function export($var) { |
| 25 | - if ($var === array()) return 'array()'; |
|
| 25 | + if ($var === array()) { |
|
| 26 | + return 'array()'; |
|
| 27 | + } |
|
| 26 | 28 | return var_export($var, true); |
| 27 | 29 | } |
| 28 | 30 | |
@@ -39,7 +41,10 @@ discard block |
||
| 39 | 41 | $this->buildDirective($directive); |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | - if ($this->namespace) $this->endElement(); // namespace |
|
| 44 | + if ($this->namespace) { |
|
| 45 | + $this->endElement(); |
|
| 46 | + } |
|
| 47 | + // namespace |
|
| 43 | 48 | |
| 44 | 49 | $this->endElement(); // configdoc |
| 45 | 50 | $this->flush(); |
@@ -51,7 +56,10 @@ discard block |
||
| 51 | 56 | // certainly makes things look nicer when documentation is built. |
| 52 | 57 | // Depends on things being sorted. |
| 53 | 58 | if (!$this->namespace || $this->namespace !== $directive->id->getRootNamespace()) { |
| 54 | - if ($this->namespace) $this->endElement(); // namespace |
|
| 59 | + if ($this->namespace) { |
|
| 60 | + $this->endElement(); |
|
| 61 | + } |
|
| 62 | + // namespace |
|
| 55 | 63 | $this->namespace = $directive->id->getRootNamespace(); |
| 56 | 64 | $this->startElement('namespace'); |
| 57 | 65 | $this->writeAttribute('id', $this->namespace); |
@@ -64,25 +72,35 @@ discard block |
||
| 64 | 72 | $this->writeElement('name', $directive->id->getDirective()); |
| 65 | 73 | |
| 66 | 74 | $this->startElement('aliases'); |
| 67 | - foreach ($directive->aliases as $alias) $this->writeElement('alias', $alias->toString()); |
|
| 75 | + foreach ($directive->aliases as $alias) { |
|
| 76 | + $this->writeElement('alias', $alias->toString()); |
|
| 77 | + } |
|
| 68 | 78 | $this->endElement(); // aliases |
| 69 | 79 | |
| 70 | 80 | $this->startElement('constraints'); |
| 71 | - if ($directive->version) $this->writeElement('version', $directive->version); |
|
| 81 | + if ($directive->version) { |
|
| 82 | + $this->writeElement('version', $directive->version); |
|
| 83 | + } |
|
| 72 | 84 | $this->startElement('type'); |
| 73 | - if ($directive->typeAllowsNull) $this->writeAttribute('allow-null', 'yes'); |
|
| 85 | + if ($directive->typeAllowsNull) { |
|
| 86 | + $this->writeAttribute('allow-null', 'yes'); |
|
| 87 | + } |
|
| 74 | 88 | $this->text($directive->type); |
| 75 | 89 | $this->endElement(); // type |
| 76 | 90 | if ($directive->allowed) { |
| 77 | 91 | $this->startElement('allowed'); |
| 78 | - foreach ($directive->allowed as $value => $x) $this->writeElement('value', $value); |
|
| 92 | + foreach ($directive->allowed as $value => $x) { |
|
| 93 | + $this->writeElement('value', $value); |
|
| 94 | + } |
|
| 79 | 95 | $this->endElement(); // allowed |
| 80 | 96 | } |
| 81 | 97 | $this->writeElement('default', $this->export($directive->default)); |
| 82 | 98 | $this->writeAttribute('xml:space', 'preserve'); |
| 83 | 99 | if ($directive->external) { |
| 84 | 100 | $this->startElement('external'); |
| 85 | - foreach ($directive->external as $project) $this->writeElement('project', $project); |
|
| 101 | + foreach ($directive->external as $project) { |
|
| 102 | + $this->writeElement('project', $project); |
|
| 103 | + } |
|
| 86 | 104 | $this->endElement(); |
| 87 | 105 | } |
| 88 | 106 | $this->endElement(); // constraints |
@@ -8,34 +8,34 @@ |
||
| 8 | 8 | class HTMLPurifier_ConfigSchema_Interchange |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Name of the application this schema is describing. |
|
| 13 | - */ |
|
| 14 | - public $name; |
|
| 11 | + /** |
|
| 12 | + * Name of the application this schema is describing. |
|
| 13 | + */ |
|
| 14 | + public $name; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Array of Directive ID => array(directive info) |
|
| 18 | - */ |
|
| 19 | - public $directives = array(); |
|
| 16 | + /** |
|
| 17 | + * Array of Directive ID => array(directive info) |
|
| 18 | + */ |
|
| 19 | + public $directives = array(); |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Adds a directive array to $directives |
|
| 23 | - */ |
|
| 24 | - public function addDirective($directive) { |
|
| 25 | - if (isset($this->directives[$i = $directive->id->toString()])) { |
|
| 26 | - throw new HTMLPurifier_ConfigSchema_Exception("Cannot redefine directive '$i'"); |
|
| 27 | - } |
|
| 28 | - $this->directives[$i] = $directive; |
|
| 29 | - } |
|
| 21 | + /** |
|
| 22 | + * Adds a directive array to $directives |
|
| 23 | + */ |
|
| 24 | + public function addDirective($directive) { |
|
| 25 | + if (isset($this->directives[$i = $directive->id->toString()])) { |
|
| 26 | + throw new HTMLPurifier_ConfigSchema_Exception("Cannot redefine directive '$i'"); |
|
| 27 | + } |
|
| 28 | + $this->directives[$i] = $directive; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Convenience function to perform standard validation. Throws exception |
|
| 33 | - * on failed validation. |
|
| 34 | - */ |
|
| 35 | - public function validate() { |
|
| 36 | - $validator = new HTMLPurifier_ConfigSchema_Validator(); |
|
| 37 | - return $validator->validate($this); |
|
| 38 | - } |
|
| 31 | + /** |
|
| 32 | + * Convenience function to perform standard validation. Throws exception |
|
| 33 | + * on failed validation. |
|
| 34 | + */ |
|
| 35 | + public function validate() { |
|
| 36 | + $validator = new HTMLPurifier_ConfigSchema_Validator(); |
|
| 37 | + return $validator->validate($this); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 40 | } |
| 41 | 41 | |
@@ -6,71 +6,71 @@ |
||
| 6 | 6 | class HTMLPurifier_ConfigSchema_Interchange_Directive |
| 7 | 7 | { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * ID of directive, instance of HTMLPurifier_ConfigSchema_Interchange_Id. |
|
| 11 | - */ |
|
| 12 | - public $id; |
|
| 9 | + /** |
|
| 10 | + * ID of directive, instance of HTMLPurifier_ConfigSchema_Interchange_Id. |
|
| 11 | + */ |
|
| 12 | + public $id; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * String type, e.g. 'integer' or 'istring'. |
|
| 16 | - */ |
|
| 17 | - public $type; |
|
| 14 | + /** |
|
| 15 | + * String type, e.g. 'integer' or 'istring'. |
|
| 16 | + */ |
|
| 17 | + public $type; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Default value, e.g. 3 or 'DefaultVal'. |
|
| 21 | - */ |
|
| 22 | - public $default; |
|
| 19 | + /** |
|
| 20 | + * Default value, e.g. 3 or 'DefaultVal'. |
|
| 21 | + */ |
|
| 22 | + public $default; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * HTML description. |
|
| 26 | - */ |
|
| 27 | - public $description; |
|
| 24 | + /** |
|
| 25 | + * HTML description. |
|
| 26 | + */ |
|
| 27 | + public $description; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Boolean whether or not null is allowed as a value. |
|
| 31 | - */ |
|
| 32 | - public $typeAllowsNull = false; |
|
| 29 | + /** |
|
| 30 | + * Boolean whether or not null is allowed as a value. |
|
| 31 | + */ |
|
| 32 | + public $typeAllowsNull = false; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Lookup table of allowed scalar values, e.g. array('allowed' => true). |
|
| 36 | - * Null if all values are allowed. |
|
| 37 | - */ |
|
| 38 | - public $allowed; |
|
| 34 | + /** |
|
| 35 | + * Lookup table of allowed scalar values, e.g. array('allowed' => true). |
|
| 36 | + * Null if all values are allowed. |
|
| 37 | + */ |
|
| 38 | + public $allowed; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * List of aliases for the directive, |
|
| 42 | - * e.g. array(new HTMLPurifier_ConfigSchema_Interchange_Id('Ns', 'Dir'))). |
|
| 43 | - */ |
|
| 44 | - public $aliases = array(); |
|
| 40 | + /** |
|
| 41 | + * List of aliases for the directive, |
|
| 42 | + * e.g. array(new HTMLPurifier_ConfigSchema_Interchange_Id('Ns', 'Dir'))). |
|
| 43 | + */ |
|
| 44 | + public $aliases = array(); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Hash of value aliases, e.g. array('alt' => 'real'). Null if value |
|
| 48 | - * aliasing is disabled (necessary for non-scalar types). |
|
| 49 | - */ |
|
| 50 | - public $valueAliases; |
|
| 46 | + /** |
|
| 47 | + * Hash of value aliases, e.g. array('alt' => 'real'). Null if value |
|
| 48 | + * aliasing is disabled (necessary for non-scalar types). |
|
| 49 | + */ |
|
| 50 | + public $valueAliases; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Version of HTML Purifier the directive was introduced, e.g. '1.3.1'. |
|
| 54 | - * Null if the directive has always existed. |
|
| 55 | - */ |
|
| 56 | - public $version; |
|
| 52 | + /** |
|
| 53 | + * Version of HTML Purifier the directive was introduced, e.g. '1.3.1'. |
|
| 54 | + * Null if the directive has always existed. |
|
| 55 | + */ |
|
| 56 | + public $version; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * ID of directive that supercedes this old directive, is an instance |
|
| 60 | - * of HTMLPurifier_ConfigSchema_Interchange_Id. Null if not deprecated. |
|
| 61 | - */ |
|
| 62 | - public $deprecatedUse; |
|
| 58 | + /** |
|
| 59 | + * ID of directive that supercedes this old directive, is an instance |
|
| 60 | + * of HTMLPurifier_ConfigSchema_Interchange_Id. Null if not deprecated. |
|
| 61 | + */ |
|
| 62 | + public $deprecatedUse; |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Version of HTML Purifier this directive was deprecated. Null if not |
|
| 66 | - * deprecated. |
|
| 67 | - */ |
|
| 68 | - public $deprecatedVersion; |
|
| 64 | + /** |
|
| 65 | + * Version of HTML Purifier this directive was deprecated. Null if not |
|
| 66 | + * deprecated. |
|
| 67 | + */ |
|
| 68 | + public $deprecatedVersion; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * List of external projects this directive depends on, e.g. array('CSSTidy'). |
|
| 72 | - */ |
|
| 73 | - public $external = array(); |
|
| 70 | + /** |
|
| 71 | + * List of external projects this directive depends on, e.g. array('CSSTidy'). |
|
| 72 | + */ |
|
| 73 | + public $external = array(); |
|
| 74 | 74 | |
| 75 | 75 | } |
| 76 | 76 | |
@@ -6,31 +6,31 @@ |
||
| 6 | 6 | class HTMLPurifier_ConfigSchema_Interchange_Id |
| 7 | 7 | { |
| 8 | 8 | |
| 9 | - public $key; |
|
| 10 | - |
|
| 11 | - public function __construct($key) { |
|
| 12 | - $this->key = $key; |
|
| 13 | - } |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @warning This is NOT magic, to ensure that people don't abuse SPL and |
|
| 17 | - * cause problems for PHP 5.0 support. |
|
| 18 | - */ |
|
| 19 | - public function toString() { |
|
| 20 | - return $this->key; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function getRootNamespace() { |
|
| 24 | - return substr($this->key, 0, strpos($this->key, ".")); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function getDirective() { |
|
| 28 | - return substr($this->key, strpos($this->key, ".") + 1); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public static function make($id) { |
|
| 32 | - return new HTMLPurifier_ConfigSchema_Interchange_Id($id); |
|
| 33 | - } |
|
| 9 | + public $key; |
|
| 10 | + |
|
| 11 | + public function __construct($key) { |
|
| 12 | + $this->key = $key; |
|
| 13 | + } |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @warning This is NOT magic, to ensure that people don't abuse SPL and |
|
| 17 | + * cause problems for PHP 5.0 support. |
|
| 18 | + */ |
|
| 19 | + public function toString() { |
|
| 20 | + return $this->key; |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public function getRootNamespace() { |
|
| 24 | + return substr($this->key, 0, strpos($this->key, ".")); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function getDirective() { |
|
| 28 | + return substr($this->key, strpos($this->key, ".") + 1); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public static function make($id) { |
|
| 32 | + return new HTMLPurifier_ConfigSchema_Interchange_Id($id); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | |
@@ -3,177 +3,177 @@ |
||
| 3 | 3 | class HTMLPurifier_ConfigSchema_InterchangeBuilder |
| 4 | 4 | { |
| 5 | 5 | |
| 6 | - /** |
|
| 7 | - * Used for processing DEFAULT, nothing else. |
|
| 8 | - */ |
|
| 9 | - protected $varParser; |
|
| 10 | - |
|
| 11 | - public function __construct($varParser = null) { |
|
| 12 | - $this->varParser = $varParser ? $varParser : new HTMLPurifier_VarParser_Native(); |
|
| 13 | - } |
|
| 14 | - |
|
| 15 | - public static function buildFromDirectory($dir = null) { |
|
| 16 | - $builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder(); |
|
| 17 | - $interchange = new HTMLPurifier_ConfigSchema_Interchange(); |
|
| 18 | - return $builder->buildDir($interchange, $dir); |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - public function buildDir($interchange, $dir = null) { |
|
| 22 | - if (!$dir) $dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema'; |
|
| 23 | - if (file_exists($dir . '/info.ini')) { |
|
| 24 | - $info = parse_ini_file($dir . '/info.ini'); |
|
| 25 | - $interchange->name = $info['name']; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - $files = array(); |
|
| 29 | - $dh = opendir($dir); |
|
| 30 | - while (false !== ($file = readdir($dh))) { |
|
| 31 | - if (!$file || $file[0] == '.' || strrchr($file, '.') !== '.txt') { |
|
| 32 | - continue; |
|
| 33 | - } |
|
| 34 | - $files[] = $file; |
|
| 35 | - } |
|
| 36 | - closedir($dh); |
|
| 37 | - |
|
| 38 | - sort($files); |
|
| 39 | - foreach ($files as $file) { |
|
| 40 | - $this->buildFile($interchange, $dir . '/' . $file); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - return $interchange; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function buildFile($interchange, $file) { |
|
| 47 | - $parser = new HTMLPurifier_StringHashParser(); |
|
| 48 | - $this->build( |
|
| 49 | - $interchange, |
|
| 50 | - new HTMLPurifier_StringHash( $parser->parseFile($file) ) |
|
| 51 | - ); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Builds an interchange object based on a hash. |
|
| 56 | - * @param $interchange HTMLPurifier_ConfigSchema_Interchange object to build |
|
| 57 | - * @param $hash HTMLPurifier_ConfigSchema_StringHash source data |
|
| 58 | - */ |
|
| 59 | - public function build($interchange, $hash) { |
|
| 60 | - if (!$hash instanceof HTMLPurifier_StringHash) { |
|
| 61 | - $hash = new HTMLPurifier_StringHash($hash); |
|
| 62 | - } |
|
| 63 | - if (!isset($hash['ID'])) { |
|
| 64 | - throw new HTMLPurifier_ConfigSchema_Exception('Hash does not have any ID'); |
|
| 65 | - } |
|
| 66 | - if (strpos($hash['ID'], '.') === false) { |
|
| 67 | - if (count($hash) == 2 && isset($hash['DESCRIPTION'])) { |
|
| 68 | - $hash->offsetGet('DESCRIPTION'); // prevent complaining |
|
| 69 | - } else { |
|
| 70 | - throw new HTMLPurifier_ConfigSchema_Exception('All directives must have a namespace'); |
|
| 71 | - } |
|
| 72 | - } else { |
|
| 73 | - $this->buildDirective($interchange, $hash); |
|
| 74 | - } |
|
| 75 | - $this->_findUnused($hash); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - public function buildDirective($interchange, $hash) { |
|
| 79 | - $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive(); |
|
| 80 | - |
|
| 81 | - // These are required elements: |
|
| 82 | - $directive->id = $this->id($hash->offsetGet('ID')); |
|
| 83 | - $id = $directive->id->toString(); // convenience |
|
| 84 | - |
|
| 85 | - if (isset($hash['TYPE'])) { |
|
| 86 | - $type = explode('/', $hash->offsetGet('TYPE')); |
|
| 87 | - if (isset($type[1])) $directive->typeAllowsNull = true; |
|
| 88 | - $directive->type = $type[0]; |
|
| 89 | - } else { |
|
| 90 | - throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '$id' not defined"); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if (isset($hash['DEFAULT'])) { |
|
| 94 | - try { |
|
| 95 | - $directive->default = $this->varParser->parse($hash->offsetGet('DEFAULT'), $directive->type, $directive->typeAllowsNull); |
|
| 96 | - } catch (HTMLPurifier_VarParserException $e) { |
|
| 97 | - throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '$id'"); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - if (isset($hash['DESCRIPTION'])) { |
|
| 102 | - $directive->description = $hash->offsetGet('DESCRIPTION'); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if (isset($hash['ALLOWED'])) { |
|
| 106 | - $directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED'))); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - if (isset($hash['VALUE-ALIASES'])) { |
|
| 110 | - $directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES')); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if (isset($hash['ALIASES'])) { |
|
| 114 | - $raw_aliases = trim($hash->offsetGet('ALIASES')); |
|
| 115 | - $aliases = preg_split('/\s*,\s*/', $raw_aliases); |
|
| 116 | - foreach ($aliases as $alias) { |
|
| 117 | - $directive->aliases[] = $this->id($alias); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - if (isset($hash['VERSION'])) { |
|
| 122 | - $directive->version = $hash->offsetGet('VERSION'); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if (isset($hash['DEPRECATED-USE'])) { |
|
| 126 | - $directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE')); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - if (isset($hash['DEPRECATED-VERSION'])) { |
|
| 130 | - $directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION'); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if (isset($hash['EXTERNAL'])) { |
|
| 134 | - $directive->external = preg_split('/\s*,\s*/', trim($hash->offsetGet('EXTERNAL'))); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $interchange->addDirective($directive); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Evaluates an array PHP code string without array() wrapper |
|
| 142 | - */ |
|
| 143 | - protected function evalArray($contents) { |
|
| 144 | - return eval('return array('. $contents .');'); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Converts an array list into a lookup array. |
|
| 149 | - */ |
|
| 150 | - protected function lookup($array) { |
|
| 151 | - $ret = array(); |
|
| 152 | - foreach ($array as $val) $ret[$val] = true; |
|
| 153 | - return $ret; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Convenience function that creates an HTMLPurifier_ConfigSchema_Interchange_Id |
|
| 158 | - * object based on a string Id. |
|
| 159 | - */ |
|
| 160 | - protected function id($id) { |
|
| 161 | - return HTMLPurifier_ConfigSchema_Interchange_Id::make($id); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Triggers errors for any unused keys passed in the hash; such keys |
|
| 166 | - * may indicate typos, missing values, etc. |
|
| 167 | - * @param $hash Instance of ConfigSchema_StringHash to check. |
|
| 168 | - */ |
|
| 169 | - protected function _findUnused($hash) { |
|
| 170 | - $accessed = $hash->getAccessed(); |
|
| 171 | - foreach ($hash as $k => $v) { |
|
| 172 | - if (!isset($accessed[$k])) { |
|
| 173 | - trigger_error("String hash key '$k' not used by builder", E_USER_NOTICE); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 6 | + /** |
|
| 7 | + * Used for processing DEFAULT, nothing else. |
|
| 8 | + */ |
|
| 9 | + protected $varParser; |
|
| 10 | + |
|
| 11 | + public function __construct($varParser = null) { |
|
| 12 | + $this->varParser = $varParser ? $varParser : new HTMLPurifier_VarParser_Native(); |
|
| 13 | + } |
|
| 14 | + |
|
| 15 | + public static function buildFromDirectory($dir = null) { |
|
| 16 | + $builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder(); |
|
| 17 | + $interchange = new HTMLPurifier_ConfigSchema_Interchange(); |
|
| 18 | + return $builder->buildDir($interchange, $dir); |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + public function buildDir($interchange, $dir = null) { |
|
| 22 | + if (!$dir) $dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema'; |
|
| 23 | + if (file_exists($dir . '/info.ini')) { |
|
| 24 | + $info = parse_ini_file($dir . '/info.ini'); |
|
| 25 | + $interchange->name = $info['name']; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + $files = array(); |
|
| 29 | + $dh = opendir($dir); |
|
| 30 | + while (false !== ($file = readdir($dh))) { |
|
| 31 | + if (!$file || $file[0] == '.' || strrchr($file, '.') !== '.txt') { |
|
| 32 | + continue; |
|
| 33 | + } |
|
| 34 | + $files[] = $file; |
|
| 35 | + } |
|
| 36 | + closedir($dh); |
|
| 37 | + |
|
| 38 | + sort($files); |
|
| 39 | + foreach ($files as $file) { |
|
| 40 | + $this->buildFile($interchange, $dir . '/' . $file); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + return $interchange; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function buildFile($interchange, $file) { |
|
| 47 | + $parser = new HTMLPurifier_StringHashParser(); |
|
| 48 | + $this->build( |
|
| 49 | + $interchange, |
|
| 50 | + new HTMLPurifier_StringHash( $parser->parseFile($file) ) |
|
| 51 | + ); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Builds an interchange object based on a hash. |
|
| 56 | + * @param $interchange HTMLPurifier_ConfigSchema_Interchange object to build |
|
| 57 | + * @param $hash HTMLPurifier_ConfigSchema_StringHash source data |
|
| 58 | + */ |
|
| 59 | + public function build($interchange, $hash) { |
|
| 60 | + if (!$hash instanceof HTMLPurifier_StringHash) { |
|
| 61 | + $hash = new HTMLPurifier_StringHash($hash); |
|
| 62 | + } |
|
| 63 | + if (!isset($hash['ID'])) { |
|
| 64 | + throw new HTMLPurifier_ConfigSchema_Exception('Hash does not have any ID'); |
|
| 65 | + } |
|
| 66 | + if (strpos($hash['ID'], '.') === false) { |
|
| 67 | + if (count($hash) == 2 && isset($hash['DESCRIPTION'])) { |
|
| 68 | + $hash->offsetGet('DESCRIPTION'); // prevent complaining |
|
| 69 | + } else { |
|
| 70 | + throw new HTMLPurifier_ConfigSchema_Exception('All directives must have a namespace'); |
|
| 71 | + } |
|
| 72 | + } else { |
|
| 73 | + $this->buildDirective($interchange, $hash); |
|
| 74 | + } |
|
| 75 | + $this->_findUnused($hash); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + public function buildDirective($interchange, $hash) { |
|
| 79 | + $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive(); |
|
| 80 | + |
|
| 81 | + // These are required elements: |
|
| 82 | + $directive->id = $this->id($hash->offsetGet('ID')); |
|
| 83 | + $id = $directive->id->toString(); // convenience |
|
| 84 | + |
|
| 85 | + if (isset($hash['TYPE'])) { |
|
| 86 | + $type = explode('/', $hash->offsetGet('TYPE')); |
|
| 87 | + if (isset($type[1])) $directive->typeAllowsNull = true; |
|
| 88 | + $directive->type = $type[0]; |
|
| 89 | + } else { |
|
| 90 | + throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '$id' not defined"); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if (isset($hash['DEFAULT'])) { |
|
| 94 | + try { |
|
| 95 | + $directive->default = $this->varParser->parse($hash->offsetGet('DEFAULT'), $directive->type, $directive->typeAllowsNull); |
|
| 96 | + } catch (HTMLPurifier_VarParserException $e) { |
|
| 97 | + throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '$id'"); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + if (isset($hash['DESCRIPTION'])) { |
|
| 102 | + $directive->description = $hash->offsetGet('DESCRIPTION'); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if (isset($hash['ALLOWED'])) { |
|
| 106 | + $directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED'))); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + if (isset($hash['VALUE-ALIASES'])) { |
|
| 110 | + $directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES')); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if (isset($hash['ALIASES'])) { |
|
| 114 | + $raw_aliases = trim($hash->offsetGet('ALIASES')); |
|
| 115 | + $aliases = preg_split('/\s*,\s*/', $raw_aliases); |
|
| 116 | + foreach ($aliases as $alias) { |
|
| 117 | + $directive->aliases[] = $this->id($alias); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if (isset($hash['VERSION'])) { |
|
| 122 | + $directive->version = $hash->offsetGet('VERSION'); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if (isset($hash['DEPRECATED-USE'])) { |
|
| 126 | + $directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE')); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + if (isset($hash['DEPRECATED-VERSION'])) { |
|
| 130 | + $directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION'); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if (isset($hash['EXTERNAL'])) { |
|
| 134 | + $directive->external = preg_split('/\s*,\s*/', trim($hash->offsetGet('EXTERNAL'))); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $interchange->addDirective($directive); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Evaluates an array PHP code string without array() wrapper |
|
| 142 | + */ |
|
| 143 | + protected function evalArray($contents) { |
|
| 144 | + return eval('return array('. $contents .');'); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Converts an array list into a lookup array. |
|
| 149 | + */ |
|
| 150 | + protected function lookup($array) { |
|
| 151 | + $ret = array(); |
|
| 152 | + foreach ($array as $val) $ret[$val] = true; |
|
| 153 | + return $ret; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Convenience function that creates an HTMLPurifier_ConfigSchema_Interchange_Id |
|
| 158 | + * object based on a string Id. |
|
| 159 | + */ |
|
| 160 | + protected function id($id) { |
|
| 161 | + return HTMLPurifier_ConfigSchema_Interchange_Id::make($id); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Triggers errors for any unused keys passed in the hash; such keys |
|
| 166 | + * may indicate typos, missing values, etc. |
|
| 167 | + * @param $hash Instance of ConfigSchema_StringHash to check. |
|
| 168 | + */ |
|
| 169 | + protected function _findUnused($hash) { |
|
| 170 | + $accessed = $hash->getAccessed(); |
|
| 171 | + foreach ($hash as $k => $v) { |
|
| 172 | + if (!isset($accessed[$k])) { |
|
| 173 | + trigger_error("String hash key '$k' not used by builder", E_USER_NOTICE); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | 178 | } |
| 179 | 179 | |
@@ -19,7 +19,9 @@ discard block |
||
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public function buildDir($interchange, $dir = null) { |
| 22 | - if (!$dir) $dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema'; |
|
| 22 | + if (!$dir) { |
|
| 23 | + $dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema'; |
|
| 24 | + } |
|
| 23 | 25 | if (file_exists($dir . '/info.ini')) { |
| 24 | 26 | $info = parse_ini_file($dir . '/info.ini'); |
| 25 | 27 | $interchange->name = $info['name']; |
@@ -84,7 +86,9 @@ discard block |
||
| 84 | 86 | |
| 85 | 87 | if (isset($hash['TYPE'])) { |
| 86 | 88 | $type = explode('/', $hash->offsetGet('TYPE')); |
| 87 | - if (isset($type[1])) $directive->typeAllowsNull = true; |
|
| 89 | + if (isset($type[1])) { |
|
| 90 | + $directive->typeAllowsNull = true; |
|
| 91 | + } |
|
| 88 | 92 | $directive->type = $type[0]; |
| 89 | 93 | } else { |
| 90 | 94 | throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '$id' not defined"); |
@@ -149,7 +153,9 @@ discard block |
||
| 149 | 153 | */ |
| 150 | 154 | protected function lookup($array) { |
| 151 | 155 | $ret = array(); |
| 152 | - foreach ($array as $val) $ret[$val] = true; |
|
| 156 | + foreach ($array as $val) { |
|
| 157 | + $ret[$val] = true; |
|
| 158 | + } |
|
| 153 | 159 | return $ret; |
| 154 | 160 | } |
| 155 | 161 | |
@@ -19,9 +19,9 @@ discard block |
||
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public function buildDir($interchange, $dir = null) { |
| 22 | - if (!$dir) $dir = HTMLPURIFIER_PREFIX . '/HTMLPurifier/ConfigSchema/schema'; |
|
| 23 | - if (file_exists($dir . '/info.ini')) { |
|
| 24 | - $info = parse_ini_file($dir . '/info.ini'); |
|
| 22 | + if (!$dir) $dir = HTMLPURIFIER_PREFIX.'/HTMLPurifier/ConfigSchema/schema'; |
|
| 23 | + if (file_exists($dir.'/info.ini')) { |
|
| 24 | + $info = parse_ini_file($dir.'/info.ini'); |
|
| 25 | 25 | $interchange->name = $info['name']; |
| 26 | 26 | } |
| 27 | 27 | |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | sort($files); |
| 39 | 39 | foreach ($files as $file) { |
| 40 | - $this->buildFile($interchange, $dir . '/' . $file); |
|
| 40 | + $this->buildFile($interchange, $dir.'/'.$file); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | return $interchange; |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | $parser = new HTMLPurifier_StringHashParser(); |
| 48 | 48 | $this->build( |
| 49 | 49 | $interchange, |
| 50 | - new HTMLPurifier_StringHash( $parser->parseFile($file) ) |
|
| 50 | + new HTMLPurifier_StringHash($parser->parseFile($file)) |
|
| 51 | 51 | ); |
| 52 | 52 | } |
| 53 | 53 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | try { |
| 95 | 95 | $directive->default = $this->varParser->parse($hash->offsetGet('DEFAULT'), $directive->type, $directive->typeAllowsNull); |
| 96 | 96 | } catch (HTMLPurifier_VarParserException $e) { |
| 97 | - throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '$id'"); |
|
| 97 | + throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage()." in DEFAULT in directive hash '$id'"); |
|
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | * Evaluates an array PHP code string without array() wrapper |
| 142 | 142 | */ |
| 143 | 143 | protected function evalArray($contents) { |
| 144 | - return eval('return array('. $contents .');'); |
|
| 144 | + return eval('return array('.$contents.');'); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |