Truncate alt text from src if it's too long.
[htmlpurifier.git] / library / HTMLPurifier / Bootstrap.php
blob559f61a232024524fbf2b193f081cc93c245b434
1 <?php
3 // constants are slow, so we use as few as possible
4 if (!defined('HTMLPURIFIER_PREFIX')) {
5 define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));
8 // accomodations for versions earlier than 5.0.2
9 // borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net>
10 if (!defined('PHP_EOL')) {
11 switch (strtoupper(substr(PHP_OS, 0, 3))) {
12 case 'WIN':
13 define('PHP_EOL', "\r\n");
14 break;
15 case 'DAR':
16 define('PHP_EOL', "\r");
17 break;
18 default:
19 define('PHP_EOL', "\n");
23 /**
24 * Bootstrap class that contains meta-functionality for HTML Purifier such as
25 * the autoload function.
27 * @note
28 * This class may be used without any other files from HTML Purifier.
30 class HTMLPurifier_Bootstrap
33 /**
34 * Autoload function for HTML Purifier
35 * @param $class Class to load
37 public static function autoload($class) {
38 $file = HTMLPurifier_Bootstrap::getPath($class);
39 if (!$file) return false;
40 require HTMLPURIFIER_PREFIX . '/' . $file;
41 return true;
44 /**
45 * Returns the path for a specific class.
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';
56 if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) return false;
57 return $file;
60 /**
61 * "Pre-registers" our autoloader on the SPL stack.
63 public static function registerAutoload() {
64 $autoload = array('HTMLPurifier_Bootstrap', 'autoload');
65 if ( ($funcs = spl_autoload_functions()) === false ) {
66 spl_autoload_register($autoload);
67 } elseif (function_exists('spl_autoload_unregister')) {
68 $compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
69 version_compare(PHP_VERSION, '5.1.0', '>=');
70 foreach ($funcs as $func) {
71 if (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 ');
85 // Suprisingly, spl_autoload_register supports the
86 // Class::staticMethod callback format, although call_user_func doesn't
87 if ($compat) $func = implode('::', $func);
89 spl_autoload_unregister($func);
91 spl_autoload_register($autoload);
92 foreach ($funcs as $func) spl_autoload_register($func);
98 // vim: et sw=4 sts=4