File indexing completed on 2024-04-28 05:58:54

0001 <?php
0002 
0003 // OUT OF DATE, NEEDS UPDATING!
0004 // USE XMLWRITER!
0005 
0006 class HTMLPurifier_Printer
0007 {
0008 
0009     /**
0010      * For HTML generation convenience funcs.
0011      * @type HTMLPurifier_Generator
0012      */
0013     protected $generator;
0014 
0015     /**
0016      * For easy access.
0017      * @type HTMLPurifier_Config
0018      */
0019     protected $config;
0020 
0021     /**
0022      * Initialize $generator.
0023      */
0024     public function __construct()
0025     {
0026     }
0027 
0028     /**
0029      * Give generator necessary configuration if possible
0030      * @param HTMLPurifier_Config $config
0031      */
0032     public function prepareGenerator($config)
0033     {
0034         $all = $config->getAll();
0035         $context = new HTMLPurifier_Context();
0036         $this->generator = new HTMLPurifier_Generator($config, $context);
0037     }
0038 
0039     /**
0040      * Main function that renders object or aspect of that object
0041      * @note Parameters vary depending on printer
0042      */
0043     // function render() {}
0044 
0045     /**
0046      * Returns a start tag
0047      * @param string $tag Tag name
0048      * @param array $attr Attribute array
0049      * @return string
0050      */
0051     protected function start($tag, $attr = array())
0052     {
0053         return $this->generator->generateFromToken(
0054             new HTMLPurifier_Token_Start($tag, $attr ? $attr : array())
0055         );
0056     }
0057 
0058     /**
0059      * Returns an end tag
0060      * @param string $tag Tag name
0061      * @return string
0062      */
0063     protected function end($tag)
0064     {
0065         return $this->generator->generateFromToken(
0066             new HTMLPurifier_Token_End($tag)
0067         );
0068     }
0069 
0070     /**
0071      * Prints a complete element with content inside
0072      * @param string $tag Tag name
0073      * @param string $contents Element contents
0074      * @param array $attr Tag attributes
0075      * @param bool $escape whether or not to escape contents
0076      * @return string
0077      */
0078     protected function element($tag, $contents, $attr = array(), $escape = true)
0079     {
0080         return $this->start($tag, $attr) .
0081             ($escape ? $this->escape($contents) : $contents) .
0082             $this->end($tag);
0083     }
0084 
0085     /**
0086      * @param string $tag
0087      * @param array $attr
0088      * @return string
0089      */
0090     protected function elementEmpty($tag, $attr = array())
0091     {
0092         return $this->generator->generateFromToken(
0093             new HTMLPurifier_Token_Empty($tag, $attr)
0094         );
0095     }
0096 
0097     /**
0098      * @param string $text
0099      * @return string
0100      */
0101     protected function text($text)
0102     {
0103         return $this->generator->generateFromToken(
0104             new HTMLPurifier_Token_Text($text)
0105         );
0106     }
0107 
0108     /**
0109      * Prints a simple key/value row in a table.
0110      * @param string $name Key
0111      * @param mixed $value Value
0112      * @return string
0113      */
0114     protected function row($name, $value)
0115     {
0116         if (is_bool($value)) {
0117             $value = $value ? 'On' : 'Off';
0118         }
0119         return
0120             $this->start('tr') . "\n" .
0121             $this->element('th', $name) . "\n" .
0122             $this->element('td', $value) . "\n" .
0123             $this->end('tr');
0124     }
0125 
0126     /**
0127      * Escapes a string for HTML output.
0128      * @param string $string String to escape
0129      * @return string
0130      */
0131     protected function escape($string)
0132     {
0133         $string = HTMLPurifier_Encoder::cleanUTF8($string);
0134         $string = htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
0135         return $string;
0136     }
0137 
0138     /**
0139      * Takes a list of strings and turns them into a single list
0140      * @param string[] $array List of strings
0141      * @param bool $polite Bool whether or not to add an end before the last
0142      * @return string
0143      */
0144     protected function listify($array, $polite = false)
0145     {
0146         if (empty($array)) {
0147             return 'None';
0148         }
0149         $ret = '';
0150         $i = count($array);
0151         foreach ($array as $value) {
0152             $i--;
0153             $ret .= $value;
0154             if ($i > 0 && !($polite && $i == 1)) {
0155                 $ret .= ', ';
0156             }
0157             if ($polite && $i == 1) {
0158                 $ret .= 'and ';
0159             }
0160         }
0161         return $ret;
0162     }
0163 
0164     /**
0165      * Retrieves the class of an object without prefixes, as well as metadata
0166      * @param object $obj Object to determine class of
0167      * @param string $sec_prefix Further prefix to remove
0168      * @return string
0169      */
0170     protected function getClass($obj, $sec_prefix = '')
0171     {
0172         static $five = null;
0173         if ($five === null) {
0174             $five = version_compare(PHP_VERSION, '5', '>=');
0175         }
0176         $prefix = 'HTMLPurifier_' . $sec_prefix;
0177         if (!$five) {
0178             $prefix = strtolower($prefix);
0179         }
0180         $class = str_replace($prefix, '', get_class($obj));
0181         $lclass = strtolower($class);
0182         $class .= '(';
0183         switch ($lclass) {
0184             case 'enum':
0185                 $values = array();
0186                 foreach ($obj->valid_values as $value => $bool) {
0187                     $values[] = $value;
0188                 }
0189                 $class .= implode(', ', $values);
0190                 break;
0191             case 'css_composite':
0192                 $values = array();
0193                 foreach ($obj->defs as $def) {
0194                     $values[] = $this->getClass($def, $sec_prefix);
0195                 }
0196                 $class .= implode(', ', $values);
0197                 break;
0198             case 'css_multiple':
0199                 $class .= $this->getClass($obj->single, $sec_prefix) . ', ';
0200                 $class .= $obj->max;
0201                 break;
0202             case 'css_denyelementdecorator':
0203                 $class .= $this->getClass($obj->def, $sec_prefix) . ', ';
0204                 $class .= $obj->element;
0205                 break;
0206             case 'css_importantdecorator':
0207                 $class .= $this->getClass($obj->def, $sec_prefix);
0208                 if ($obj->allow) {
0209                     $class .= ', !important';
0210                 }
0211                 break;
0212         }
0213         $class .= ')';
0214         return $class;
0215     }
0216 }
0217 
0218 // vim: et sw=4 sts=4