PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/purifier/vendor/htmlpurifier/library/HTMLPurifier/Printer.php

https://bitbucket.org/levjke/kohanablogds
PHP | 176 lines | 103 code | 18 blank | 55 comment | 13 complexity | c07a2a22e713aa38f5b1391375bef514 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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