You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.5KB

  1. <?php
  2. // constants are slow, so we use as few as possible
  3. if (!defined('HTMLPURIFIER_PREFIX')) {
  4. define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));
  5. }
  6. // accomodations for versions earlier than 5.0.2
  7. // borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net>
  8. if (!defined('PHP_EOL')) {
  9. switch (strtoupper(substr(PHP_OS, 0, 3))) {
  10. case 'WIN':
  11. define('PHP_EOL', "\r\n");
  12. break;
  13. case 'DAR':
  14. define('PHP_EOL', "\r");
  15. break;
  16. default:
  17. define('PHP_EOL', "\n");
  18. }
  19. }
  20. /**
  21. * Bootstrap class that contains meta-functionality for HTML Purifier such as
  22. * the autoload function.
  23. *
  24. * @note
  25. * This class may be used without any other files from HTML Purifier.
  26. */
  27. class HTMLPurifier_Bootstrap
  28. {
  29. /**
  30. * Autoload function for HTML Purifier
  31. * @param string $class Class to load
  32. * @return bool
  33. */
  34. public static function autoload($class)
  35. {
  36. $file = HTMLPurifier_Bootstrap::getPath($class);
  37. if (!$file) {
  38. return false;
  39. }
  40. // Technically speaking, it should be ok and more efficient to
  41. // just do 'require', but Antonio Parraga reports that with
  42. // Zend extensions such as Zend debugger and APC, this invariant
  43. // may be broken. Since we have efficient alternatives, pay
  44. // the cost here and avoid the bug.
  45. require_once HTMLPURIFIER_PREFIX . '/' . $file;
  46. return true;
  47. }
  48. /**
  49. * Returns the path for a specific class.
  50. * @param string $class Class path to get
  51. * @return string
  52. */
  53. public static function getPath($class)
  54. {
  55. if (strncmp('HTMLPurifier', $class, 12) !== 0) {
  56. return false;
  57. }
  58. // Custom implementations
  59. if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
  60. $code = str_replace('_', '-', substr($class, 22));
  61. $file = 'HTMLPurifier/Language/classes/' . $code . '.php';
  62. } else {
  63. $file = str_replace('_', '/', $class) . '.php';
  64. }
  65. if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) {
  66. return false;
  67. }
  68. return $file;
  69. }
  70. /**
  71. * "Pre-registers" our autoloader on the SPL stack.
  72. */
  73. public static function registerAutoload()
  74. {
  75. $autoload = array('HTMLPurifier_Bootstrap', 'autoload');
  76. if (($funcs = spl_autoload_functions()) === false) {
  77. spl_autoload_register($autoload);
  78. } elseif (function_exists('spl_autoload_unregister')) {
  79. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  80. // prepend flag exists, no need for shenanigans
  81. spl_autoload_register($autoload, true, true);
  82. } else {
  83. $buggy = version_compare(PHP_VERSION, '5.2.11', '<');
  84. $compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
  85. version_compare(PHP_VERSION, '5.1.0', '>=');
  86. foreach ($funcs as $func) {
  87. if ($buggy && is_array($func)) {
  88. // :TRICKY: There are some compatibility issues and some
  89. // places where we need to error out
  90. $reflector = new ReflectionMethod($func[0], $func[1]);
  91. if (!$reflector->isStatic()) {
  92. throw new Exception(
  93. 'HTML Purifier autoloader registrar is not compatible
  94. with non-static object methods due to PHP Bug #44144;
  95. Please do not use HTMLPurifier.autoload.php (or any
  96. file that includes this file); instead, place the code:
  97. spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
  98. after your own autoloaders.'
  99. );
  100. }
  101. // Suprisingly, spl_autoload_register supports the
  102. // Class::staticMethod callback format, although call_user_func doesn't
  103. if ($compat) {
  104. $func = implode('::', $func);
  105. }
  106. }
  107. spl_autoload_unregister($func);
  108. }
  109. spl_autoload_register($autoload);
  110. foreach ($funcs as $func) {
  111. spl_autoload_register($func);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. // vim: et sw=4 sts=4