| [21] | 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // OUT OF DATE, NEEDS UPDATING! |
|---|
| 4 | // USE XMLWRITER! |
|---|
| 5 | |
|---|
| 6 | class HTMLPurifier_Printer |
|---|
| 7 | { |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * Instance of HTMLPurifier_Generator for HTML generation convenience funcs |
|---|
| 11 | */ |
|---|
| 12 | protected $generator; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * Instance of HTMLPurifier_Config, for easy access |
|---|
| 16 | */ |
|---|
| 17 | protected $config; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Initialize $generator. |
|---|
| 21 | */ |
|---|
| 22 | public function __construct() { |
|---|
| 23 | $this->generator = new HTMLPurifier_Generator(); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Give generator necessary configuration if possible |
|---|
| 28 | */ |
|---|
| 29 | public function prepareGenerator($config) { |
|---|
| 30 | // hack for smoketests/configForm.php |
|---|
| 31 | $all = $config->getAll(); |
|---|
| 32 | if (empty($all['HTML'])) return; |
|---|
| 33 | $context = new HTMLPurifier_Context(); |
|---|
| 34 | $this->generator->generateFromTokens(array(), $config, $context); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Main function that renders object or aspect of that object |
|---|
| 39 | * @note Parameters vary depending on printer |
|---|
| 40 | */ |
|---|
| 41 | // function render() {} |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Returns a start tag |
|---|
| 45 | * @param $tag Tag name |
|---|
| 46 | * @param $attr Attribute array |
|---|
| 47 | */ |
|---|
| 48 | protected function start($tag, $attr = array()) { |
|---|
| 49 | return $this->generator->generateFromToken( |
|---|
| 50 | new HTMLPurifier_Token_Start($tag, $attr ? $attr : array()) |
|---|
| 51 | ); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Returns an end teg |
|---|
| 56 | * @param $tag Tag name |
|---|
| 57 | */ |
|---|
| 58 | protected function end($tag) { |
|---|
| 59 | return $this->generator->generateFromToken( |
|---|
| 60 | new HTMLPurifier_Token_End($tag) |
|---|
| 61 | ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Prints a complete element with content inside |
|---|
| 66 | * @param $tag Tag name |
|---|
| 67 | * @param $contents Element contents |
|---|
| 68 | * @param $attr Tag attributes |
|---|
| 69 | * @param $escape Bool whether or not to escape contents |
|---|
| 70 | */ |
|---|
| 71 | protected function element($tag, $contents, $attr = array(), $escape = true) { |
|---|
| 72 | return $this->start($tag, $attr) . |
|---|
| 73 | ($escape ? $this->escape($contents) : $contents) . |
|---|
| 74 | $this->end($tag); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | protected function elementEmpty($tag, $attr = array()) { |
|---|
| 78 | return $this->generator->generateFromToken( |
|---|
| 79 | new HTMLPurifier_Token_Empty($tag, $attr) |
|---|
| 80 | ); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | protected function text($text) { |
|---|
| 84 | return $this->generator->generateFromToken( |
|---|
| 85 | new HTMLPurifier_Token_Text($text) |
|---|
| 86 | ); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Prints a simple key/value row in a table. |
|---|
| 91 | * @param $name Key |
|---|
| 92 | * @param $value Value |
|---|
| 93 | */ |
|---|
| 94 | protected function row($name, $value) { |
|---|
| 95 | if (is_bool($value)) $value = $value ? 'On' : 'Off'; |
|---|
| 96 | return |
|---|
| 97 | $this->start('tr') . "\n" . |
|---|
| 98 | $this->element('th', $name) . "\n" . |
|---|
| 99 | $this->element('td', $value) . "\n" . |
|---|
| 100 | $this->end('tr') |
|---|
| 101 | ; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Escapes a string for HTML output. |
|---|
| 106 | * @param $string String to escape |
|---|
| 107 | */ |
|---|
| 108 | protected function escape($string) { |
|---|
| 109 | $string = HTMLPurifier_Encoder::cleanUTF8($string); |
|---|
| 110 | $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8'); |
|---|
| 111 | return $string; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * Takes a list of strings and turns them into a single list |
|---|
| 116 | * @param $array List of strings |
|---|
| 117 | * @param $polite Bool whether or not to add an end before the last |
|---|
| 118 | */ |
|---|
| 119 | protected function listify($array, $polite = false) { |
|---|
| 120 | if (empty($array)) return 'None'; |
|---|
| 121 | $ret = ''; |
|---|
| 122 | $i = count($array); |
|---|
| 123 | foreach ($array as $value) { |
|---|
| 124 | $i--; |
|---|
| 125 | $ret .= $value; |
|---|
| 126 | if ($i > 0 && !($polite && $i == 1)) $ret .= ', '; |
|---|
| 127 | if ($polite && $i == 1) $ret .= 'and '; |
|---|
| 128 | } |
|---|
| 129 | return $ret; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Retrieves the class of an object without prefixes, as well as metadata |
|---|
| 134 | * @param $obj Object to determine class of |
|---|
| 135 | * @param $prefix Further prefix to remove |
|---|
| 136 | */ |
|---|
| 137 | protected function getClass($obj, $sec_prefix = '') { |
|---|
| 138 | static $five = null; |
|---|
| 139 | if ($five === null) $five = version_compare(PHP_VERSION, '5', '>='); |
|---|
| 140 | $prefix = 'HTMLPurifier_' . $sec_prefix; |
|---|
| 141 | if (!$five) $prefix = strtolower($prefix); |
|---|
| 142 | $class = str_replace($prefix, '', get_class($obj)); |
|---|
| 143 | $lclass = strtolower($class); |
|---|
| 144 | $class .= '('; |
|---|
| 145 | switch ($lclass) { |
|---|
| 146 | case 'enum': |
|---|
| 147 | $values = array(); |
|---|
| 148 | foreach ($obj->valid_values as $value => $bool) { |
|---|
| 149 | $values[] = $value; |
|---|
| 150 | } |
|---|
| 151 | $class .= implode(', ', $values); |
|---|
| 152 | break; |
|---|
| 153 | case 'css_composite': |
|---|
| 154 | $values = array(); |
|---|
| 155 | foreach ($obj->defs as $def) { |
|---|
| 156 | $values[] = $this->getClass($def, $sec_prefix); |
|---|
| 157 | } |
|---|
| 158 | $class .= implode(', ', $values); |
|---|
| 159 | break; |
|---|
| 160 | case 'css_multiple': |
|---|
| 161 | $class .= $this->getClass($obj->single, $sec_prefix) . ', '; |
|---|
| 162 | $class .= $obj->max; |
|---|
| 163 | break; |
|---|
| 164 | } |
|---|
| 165 | $class .= ')'; |
|---|
| 166 | return $class; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | } |
|---|
| 170 | |
|---|