Printer.php 5.1 KB

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