PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/library/HTMLPurifier/ContentSets.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 171 lines | 107 code | 15 blank | 49 comment | 10 complexity | e6319b851e8b55e7d24f0cbef09fa79e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * @todo Unit test
  4. */
  5. class HTMLPurifier_ContentSets
  6. {
  7. /**
  8. * List of content set strings (pipe seperators) indexed by name.
  9. */
  10. public $info = array();
  11. /**
  12. * List of content set lookups (element => true) indexed by name.
  13. * @note This is in HTMLPurifier_HTMLDefinition->info_content_sets
  14. */
  15. public $lookup = array();
  16. /**
  17. * Synchronized list of defined content sets (keys of info)
  18. */
  19. protected $keys = array();
  20. /**
  21. * Synchronized list of defined content values (values of info)
  22. */
  23. protected $values = array();
  24. /**
  25. * Merges in module's content sets, expands identifiers in the content
  26. * sets and populates the keys, values and lookup member variables.
  27. * @param $modules List of HTMLPurifier_HTMLModule
  28. */
  29. public function __construct($modules)
  30. {
  31. if (! is_array($modules))
  32. $modules = array($modules);
  33. // populate content_sets based on module hints
  34. // sorry, no way of overloading
  35. foreach ($modules as $module_i => $module)
  36. {
  37. foreach ($module->content_sets as $key => $value)
  38. {
  39. $temp = $this->convertToLookup($value);
  40. if (isset($this->lookup[$key]))
  41. {
  42. // add it into the existing content set
  43. $this->lookup[$key] = array_merge($this->lookup[$key], $temp);
  44. }
  45. else
  46. {
  47. $this->lookup[$key] = $temp;
  48. }
  49. }
  50. }
  51. $old_lookup = false;
  52. while ($old_lookup !== $this->lookup)
  53. {
  54. $old_lookup = $this->lookup;
  55. foreach ($this->lookup as $i => $set)
  56. {
  57. $add = array();
  58. foreach ($set as $element => $x)
  59. {
  60. if (isset($this->lookup[$element]))
  61. {
  62. $add += $this->lookup[$element];
  63. unset($this->lookup[$i][$element]);
  64. }
  65. }
  66. $this->lookup[$i] += $add;
  67. }
  68. }
  69. foreach ($this->lookup as $key => $lookup)
  70. {
  71. $this->info[$key] = implode(' | ', array_keys($lookup));
  72. }
  73. $this->keys = array_keys($this->info);
  74. $this->values = array_values($this->info);
  75. }
  76. /**
  77. * Accepts a definition; generates and assigns a ChildDef for it
  78. * @param $def HTMLPurifier_ElementDef reference
  79. * @param $module Module that defined the ElementDef
  80. */
  81. public function generateChildDef(&$def, $module)
  82. {
  83. if (! empty($def->child))
  84. return; // already done!
  85. $content_model = $def->content_model;
  86. if (is_string($content_model))
  87. {
  88. // Assume that $this->keys is alphanumeric
  89. $def->content_model = preg_replace_callback('/\b(' . implode('|', $this->keys) . ')\b/', array(
  90. $this, 'generateChildDefCallback'), $content_model);
  91. //$def->content_model = str_replace(
  92. // $this->keys, $this->values, $content_model);
  93. }
  94. $def->child = $this->getChildDef($def, $module);
  95. }
  96. public function generateChildDefCallback($matches)
  97. {
  98. return $this->info[$matches[0]];
  99. }
  100. /**
  101. * Instantiates a ChildDef based on content_model and content_model_type
  102. * member variables in HTMLPurifier_ElementDef
  103. * @note This will also defer to modules for custom HTMLPurifier_ChildDef
  104. * subclasses that need content set expansion
  105. * @param $def HTMLPurifier_ElementDef to have ChildDef extracted
  106. * @return HTMLPurifier_ChildDef corresponding to ElementDef
  107. */
  108. public function getChildDef($def, $module)
  109. {
  110. $value = $def->content_model;
  111. if (is_object($value))
  112. {
  113. trigger_error('Literal object child definitions should be stored in ' . 'ElementDef->child not ElementDef->content_model', E_USER_NOTICE);
  114. return $value;
  115. }
  116. switch ($def->content_model_type)
  117. {
  118. case 'required' :
  119. return new HTMLPurifier_ChildDef_Required($value);
  120. case 'optional' :
  121. return new HTMLPurifier_ChildDef_Optional($value);
  122. case 'empty' :
  123. return new HTMLPurifier_ChildDef_Empty();
  124. case 'custom' :
  125. return new HTMLPurifier_ChildDef_Custom($value);
  126. }
  127. // defer to its module
  128. $return = false;
  129. if ($module->defines_child_def)
  130. { // save a func call
  131. $return = $module->getChildDef($def);
  132. }
  133. if ($return !== false)
  134. return $return;
  135. // error-out
  136. trigger_error('Could not determine which ChildDef class to instantiate', E_USER_ERROR);
  137. return false;
  138. }
  139. /**
  140. * Converts a string list of elements separated by pipes into
  141. * a lookup array.
  142. * @param $string List of elements
  143. * @return Lookup array of elements
  144. */
  145. protected function convertToLookup($string)
  146. {
  147. $array = explode('|', str_replace(' ', '', $string));
  148. $ret = array();
  149. foreach ($array as $i => $k)
  150. {
  151. $ret[$k] = true;
  152. }
  153. return $ret;
  154. }
  155. }
  156. // vim: et sw=4 sts=4