Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

219 lines
5.8KB

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