Bootstrap.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 $class Class to load
  32. */
  33. public static function autoload($class) {
  34. $file = HTMLPurifier_Bootstrap::getPath($class);
  35. if (!$file) return false;
  36. // Technically speaking, it should be ok and more efficient to
  37. // just do 'require', but Antonio Parraga reports that with
  38. // Zend extensions such as Zend debugger and APC, this invariant
  39. // may be broken. Since we have efficient alternatives, pay
  40. // the cost here and avoid the bug.
  41. require_once HTMLPURIFIER_PREFIX . '/' . $file;
  42. return true;
  43. }
  44. /**
  45. * Returns the path for a specific class.
  46. */
  47. public static function getPath($class) {
  48. if (strncmp('HTMLPurifier', $class, 12) !== 0) return false;
  49. // Custom implementations
  50. if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
  51. $code = str_replace('_', '-', substr($class, 22));
  52. $file = 'HTMLPurifier/Language/classes/' . $code . '.php';
  53. } else {
  54. $file = str_replace('_', '/', $class) . '.php';
  55. }
  56. if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) return false;
  57. return $file;
  58. }
  59. /**
  60. * "Pre-registers" our autoloader on the SPL stack.
  61. */
  62. public static function registerAutoload() {
  63. $autoload = array('HTMLPurifier_Bootstrap', 'autoload');
  64. if ( ($funcs = spl_autoload_functions()) === false ) {
  65. spl_autoload_register($autoload);
  66. } elseif (function_exists('spl_autoload_unregister')) {
  67. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  68. // prepend flag exists, no need for shenanigans
  69. spl_autoload_register($autoload, true, true);
  70. } else {
  71. $buggy = version_compare(PHP_VERSION, '5.2.11', '<');
  72. $compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
  73. version_compare(PHP_VERSION, '5.1.0', '>=');
  74. foreach ($funcs as $func) {
  75. if ($buggy && is_array($func)) {
  76. // :TRICKY: There are some compatibility issues and some
  77. // places where we need to error out
  78. $reflector = new ReflectionMethod($func[0], $func[1]);
  79. if (!$reflector->isStatic()) {
  80. throw new Exception('
  81. HTML Purifier autoloader registrar is not compatible
  82. with non-static object methods due to PHP Bug #44144;
  83. Please do not use HTMLPurifier.autoload.php (or any
  84. file that includes this file); instead, place the code:
  85. spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
  86. after your own autoloaders.
  87. ');
  88. }
  89. // Suprisingly, spl_autoload_register supports the
  90. // Class::staticMethod callback format, although call_user_func doesn't
  91. if ($compat) $func = implode('::', $func);
  92. }
  93. spl_autoload_unregister($func);
  94. }
  95. spl_autoload_register($autoload);
  96. foreach ($funcs as $func) spl_autoload_register($func);
  97. }
  98. }
  99. }
  100. }
  101. // vim: et sw=4 sts=4