Bootstrap.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $buggy = version_compare(PHP_VERSION, '5.2.11', '<');
  68. $compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
  69. version_compare(PHP_VERSION, '5.1.0', '>=');
  70. foreach ($funcs as $func) {
  71. if ($buggy && is_array($func)) {
  72. // :TRICKY: There are some compatibility issues and some
  73. // places where we need to error out
  74. $reflector = new ReflectionMethod($func[0], $func[1]);
  75. if (!$reflector->isStatic()) {
  76. throw new Exception('
  77. HTML Purifier autoloader registrar is not compatible
  78. with non-static object methods due to PHP Bug #44144;
  79. Please do not use HTMLPurifier.autoload.php (or any
  80. file that includes this file); instead, place the code:
  81. spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
  82. after your own autoloaders.
  83. ');
  84. }
  85. // Suprisingly, spl_autoload_register supports the
  86. // Class::staticMethod callback format, although call_user_func doesn't
  87. if ($compat) $func = implode('::', $func);
  88. }
  89. spl_autoload_unregister($func);
  90. }
  91. spl_autoload_register($autoload);
  92. foreach ($funcs as $func) spl_autoload_register($func);
  93. }
  94. }
  95. }
  96. // vim: et sw=4 sts=4