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         require HTMLPURIFIER_PREFIX . '/' . $file;
00041         return true;
00042     }
00043     
00047     public static function getPath($class) {
00048         if (strncmp('HTMLPurifier', $class, 12) !== 0) return false;
00049         // Custom implementations
00050         if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
00051             $code = str_replace('_', '-', substr($class, 22));
00052             $file = 'HTMLPurifier/Language/classes/' . $code . '.php';
00053         } else {
00054             $file = str_replace('_', '/', $class) . '.php';
00055         }
00056         if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) return false;
00057         return $file;
00058     }
00059     
00063     public static function registerAutoload() {
00064         $autoload = array('HTMLPurifier_Bootstrap', 'autoload');
00065         if ( ($funcs = spl_autoload_functions()) === false ) {
00066             spl_autoload_register($autoload);
00067         } elseif (function_exists('spl_autoload_unregister')) {
00068             $compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
00069                       version_compare(PHP_VERSION, '5.1.0', '>=');
00070             foreach ($funcs as $func) {
00071                 if (is_array($func)) {
00072                     // :TRICKY: There are some compatibility issues and some
00073                     // places where we need to error out
00074                     $reflector = new ReflectionMethod($func[0], $func[1]);
00075                     if (!$reflector->isStatic()) {
00076                         throw new Exception('
00077                             HTML Purifier autoloader registrar is not compatible
00078                             with non-static object methods due to PHP Bug #44144;
00079                             Please do not use HTMLPurifier.autoload.php (or any
00080                             file that includes this file); instead, place the code:
00081                             spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
00082                             after your own autoloaders.
00083                         ');
00084                     }
00085                     // Suprisingly, spl_autoload_register supports the
00086                     // Class::staticMethod callback format, although call_user_func doesn't
00087                     if ($compat) $func = implode('::', $func);
00088                 }
00089                 spl_autoload_unregister($func);
00090             }
00091             spl_autoload_register($autoload);
00092             foreach ($funcs as $func) spl_autoload_register($func);
00093         }
00094     }
00095     
00096 }

Generated on Thu Jun 19 18:47:26 2008 for HTMLPurifier by  doxygen 1.5.3