| 1 |
<?php |
| 2 |
|
| 3 |
// constants are slow, so we use as few as possible |
| 4 |
if (!defined('HTMLPURIFIER_PREFIX')) { |
| 5 |
define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..')); |
| 6 |
} |
| 7 |
|
| 8 |
// accommodations for versions earlier than 5.0.2 |
| 9 |
// borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <[email protected]> |
| 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"); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Bootstrap class that contains meta-functionality for HTML Purifier such as |
| 25 |
* the autoload function. |
| 26 |
* |
| 27 |
* @note |
| 28 |
* This class may be used without any other files from HTML Purifier. |
| 29 |
*/ |
| 30 |
class HTMLPurifier_Bootstrap |
| 31 |
{ |
| 32 |
|
| 33 |
/** |
| 34 |
* Autoload function for HTML Purifier |
| 35 |
* @param string $class Class to load |
| 36 |
* @return bool |
| 37 |
*/ |
| 38 |
public static function autoload($class) |
| 39 |
{ |
| 40 |
$file = HTMLPurifier_Bootstrap::getPath($class); |
| 41 |
if (!$file) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
// Technically speaking, it should be ok and more efficient to |
| 45 |
// just do 'require', but Antonio Parraga reports that with |
| 46 |
// Zend extensions such as Zend debugger and APC, this invariant |
| 47 |
// may be broken. Since we have efficient alternatives, pay |
| 48 |
// the cost here and avoid the bug. |
| 49 |
require_once HTMLPURIFIER_PREFIX . '/' . $file; |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the path for a specific class. |
| 55 |
* @param string $class Class path to get |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public static function getPath($class) |
| 59 |
{ |
| 60 |
if (strncmp('HTMLPurifier', $class, 12) !== 0) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
// Custom implementations |
| 64 |
if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) { |
| 65 |
$code = str_replace('_', '-', substr($class, 22)); |
| 66 |
$file = 'HTMLPurifier/Language/classes/' . $code . '.php'; |
| 67 |
} else { |
| 68 |
$file = str_replace('_', '/', $class) . '.php'; |
| 69 |
} |
| 70 |
if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
return $file; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* "Pre-registers" our autoloader on the SPL stack. |
| 78 |
*/ |
| 79 |
public static function registerAutoload() |
| 80 |
{ |
| 81 |
$autoload = array('HTMLPurifier_Bootstrap', 'autoload'); |
| 82 |
if (spl_autoload_functions() === false) { |
| 83 |
spl_autoload_register($autoload); |
| 84 |
} else { |
| 85 |
// prepend flag exists, no need for shenanigans |
| 86 |
spl_autoload_register($autoload, true, true); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// vim: et sw=4 sts=4 |