Bootstrap.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 (spl_autoload_functions() === false) {
  77. spl_autoload_register($autoload);
  78. } else {
  79. // prepend flag exists, no need for shenanigans
  80. spl_autoload_register($autoload, true, true);
  81. }
  82. }
  83. }
  84. // vim: et sw=4 sts=4