HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/Bootstrap.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // constants are slow, so we use as few as possible
00004 if (!defined('HTMLPURIFIER_PREFIX')) {
00005     define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));
00006 }
00007 
00008 // accomodations for versions earlier than 5.0.2
00009 // borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net>
00010 if (!defined('PHP_EOL')) {
00011     switch (strtoupper(substr(PHP_OS, 0, 3))) {
00012         case 'WIN':
00013             define('PHP_EOL', "\r\n");
00014             break;
00015         case 'DAR':
00016             define('PHP_EOL', "\r");
00017             break;
00018         default:
00019             define('PHP_EOL', "\n");
00020     }
00021 }
00022 
00030 class HTMLPurifier_Bootstrap
00031 {
00032 
00037     public static function autoload($class) {
00038         $file = HTMLPurifier_Bootstrap::getPath($class);
00039         if (!$file) return false;
00040         // Technically speaking, it should be ok and more efficient to
00041         // just do 'require', but Antonio Parraga reports that with
00042         // Zend extensions such as Zend debugger and APC, this invariant
00043         // may be broken.  Since we have efficient alternatives, pay
00044         // the cost here and avoid the bug.
00045         require_once HTMLPURIFIER_PREFIX . '/' . $file;
00046         return true;
00047     }
00048 
00052     public static function getPath($class) {
00053         if (strncmp('HTMLPurifier', $class, 12) !== 0) return false;
00054         // Custom implementations
00055         if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
00056             $code = str_replace('_', '-', substr($class, 22));
00057             $file = 'HTMLPurifier/Language/classes/' . $code . '.php';
00058         } else {
00059             $file = str_replace('_', '/', $class) . '.php';
00060         }
00061         if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) return false;
00062         return $file;
00063     }
00064 
00068     public static function registerAutoload() {
00069         $autoload = array('HTMLPurifier_Bootstrap', 'autoload');
00070         if ( ($funcs = spl_autoload_functions()) === false ) {
00071             spl_autoload_register($autoload);
00072         } elseif (function_exists('spl_autoload_unregister')) {
00073             $buggy  = version_compare(PHP_VERSION, '5.2.11', '<');
00074             $compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
00075                       version_compare(PHP_VERSION, '5.1.0', '>=');
00076             foreach ($funcs as $func) {
00077                 if ($buggy && is_array($func)) {
00078                     // :TRICKY: There are some compatibility issues and some
00079                     // places where we need to error out
00080                     $reflector = new ReflectionMethod($func[0], $func[1]);
00081                     if (!$reflector->isStatic()) {
00082                         throw new Exception('
00083                             HTML Purifier autoloader registrar is not compatible
00084                             with non-static object methods due to PHP Bug #44144;
00085                             Please do not use HTMLPurifier.autoload.php (or any
00086                             file that includes this file); instead, place the code:
00087                             spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
00088                             after your own autoloaders.
00089                         ');
00090                     }
00091                     // Suprisingly, spl_autoload_register supports the
00092                     // Class::staticMethod callback format, although call_user_func doesn't
00093                     if ($compat) $func = implode('::', $func);
00094                 }
00095                 spl_autoload_unregister($func);
00096             }
00097             spl_autoload_register($autoload);
00098             foreach ($funcs as $func) spl_autoload_register($func);
00099         }
00100     }
00101 
00102 }
00103 
00104 // vim: et sw=4 sts=4