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