File indexing completed on 2025-01-05 05:24:26
0001 <?php 0002 0003 // constants are slow, so we use as few as possible 0004 if (!defined('HTMLPURIFIER_PREFIX')) { 0005 define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..')); 0006 } 0007 0008 // accomodations for versions earlier than 5.0.2 0009 // borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net> 0010 if (!defined('PHP_EOL')) { 0011 switch (strtoupper(substr(PHP_OS, 0, 3))) { 0012 case 'WIN': 0013 define('PHP_EOL', "\r\n"); 0014 break; 0015 case 'DAR': 0016 define('PHP_EOL', "\r"); 0017 break; 0018 default: 0019 define('PHP_EOL', "\n"); 0020 } 0021 } 0022 0023 /** 0024 * Bootstrap class that contains meta-functionality for HTML Purifier such as 0025 * the autoload function. 0026 * 0027 * @note 0028 * This class may be used without any other files from HTML Purifier. 0029 */ 0030 class HTMLPurifier_Bootstrap 0031 { 0032 0033 /** 0034 * Autoload function for HTML Purifier 0035 * @param string $class Class to load 0036 * @return bool 0037 */ 0038 public static function autoload($class) 0039 { 0040 $file = HTMLPurifier_Bootstrap::getPath($class); 0041 if (!$file) { 0042 return false; 0043 } 0044 // Technically speaking, it should be ok and more efficient to 0045 // just do 'require', but Antonio Parraga reports that with 0046 // Zend extensions such as Zend debugger and APC, this invariant 0047 // may be broken. Since we have efficient alternatives, pay 0048 // the cost here and avoid the bug. 0049 require_once HTMLPURIFIER_PREFIX . '/' . $file; 0050 return true; 0051 } 0052 0053 /** 0054 * Returns the path for a specific class. 0055 * @param string $class Class path to get 0056 * @return string 0057 */ 0058 public static function getPath($class) 0059 { 0060 if (strncmp('HTMLPurifier', $class, 12) !== 0) { 0061 return false; 0062 } 0063 // Custom implementations 0064 if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) { 0065 $code = str_replace('_', '-', substr($class, 22)); 0066 $file = 'HTMLPurifier/Language/classes/' . $code . '.php'; 0067 } else { 0068 $file = str_replace('_', '/', $class) . '.php'; 0069 } 0070 if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) { 0071 return false; 0072 } 0073 return $file; 0074 } 0075 0076 /** 0077 * "Pre-registers" our autoloader on the SPL stack. 0078 */ 0079 public static function registerAutoload() 0080 { 0081 $autoload = array('HTMLPurifier_Bootstrap', 'autoload'); 0082 if (($funcs = spl_autoload_functions()) === false) { 0083 spl_autoload_register($autoload); 0084 } elseif (function_exists('spl_autoload_unregister')) { 0085 if (version_compare(PHP_VERSION, '5.3.0', '>=')) { 0086 // prepend flag exists, no need for shenanigans 0087 spl_autoload_register($autoload, true, true); 0088 } else { 0089 $buggy = version_compare(PHP_VERSION, '5.2.11', '<'); 0090 $compat = version_compare(PHP_VERSION, '5.1.2', '<=') && 0091 version_compare(PHP_VERSION, '5.1.0', '>='); 0092 foreach ($funcs as $func) { 0093 if ($buggy && is_array($func)) { 0094 // :TRICKY: There are some compatibility issues and some 0095 // places where we need to error out 0096 $reflector = new ReflectionMethod($func[0], $func[1]); 0097 if (!$reflector->isStatic()) { 0098 throw new Exception( 0099 'HTML Purifier autoloader registrar is not compatible 0100 with non-static object methods due to PHP Bug #44144; 0101 Please do not use HTMLPurifier.autoload.php (or any 0102 file that includes this file); instead, place the code: 0103 spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\')) 0104 after your own autoloaders.' 0105 ); 0106 } 0107 // Suprisingly, spl_autoload_register supports the 0108 // Class::staticMethod callback format, although call_user_func doesn't 0109 if ($compat) { 0110 $func = implode('::', $func); 0111 } 0112 } 0113 spl_autoload_unregister($func); 0114 } 0115 spl_autoload_register($autoload); 0116 foreach ($funcs as $func) { 0117 spl_autoload_register($func); 0118 } 0119 } 0120 } 0121 } 0122 } 0123 0124 // vim: et sw=4 sts=4