2008-01-27 01:54:41 +00:00
|
|
|
<?php
|
|
|
|
|
2008-02-11 00:27:35 +00:00
|
|
|
// constants are slow, so we use as few as possible
|
2008-02-11 00:15:04 +00:00
|
|
|
if (!defined('HTMLPURIFIER_PREFIX')) {
|
|
|
|
define('HTMLPURIFIER_PREFIX', realpath(dirname(__FILE__) . '/..'));
|
|
|
|
}
|
2008-02-10 22:29:01 +00:00
|
|
|
|
2008-02-11 00:27:35 +00:00
|
|
|
// accomodations for versions earlier than 5.0.2
|
|
|
|
// borrowed from PHP_Compat, LGPL licensed, by Aidan Lister <aidan@php.net>
|
|
|
|
if (!defined('PHP_EOL')) {
|
|
|
|
switch (strtoupper(substr(PHP_OS, 0, 3))) {
|
|
|
|
case 'WIN':
|
|
|
|
define('PHP_EOL', "\r\n");
|
|
|
|
break;
|
|
|
|
case 'DAR':
|
|
|
|
define('PHP_EOL', "\r");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
define('PHP_EOL', "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-27 01:54:41 +00:00
|
|
|
/**
|
|
|
|
* Bootstrap class that contains meta-functionality for HTML Purifier such as
|
|
|
|
* the autoload function.
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
* This class may be used without any other files from HTML Purifier.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_Bootstrap
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 01:54:41 +00:00
|
|
|
/**
|
|
|
|
* Autoload function for HTML Purifier
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $class Class to load
|
|
|
|
* @return bool
|
2008-01-27 01:54:41 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public static function autoload($class)
|
|
|
|
{
|
2008-01-27 01:54:41 +00:00
|
|
|
$file = HTMLPurifier_Bootstrap::getPath($class);
|
2013-07-16 11:56:14 +00:00
|
|
|
if (!$file) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-31 09:48:28 +00:00
|
|
|
// Technically speaking, it should be ok and more efficient to
|
|
|
|
// just do 'require', but Antonio Parraga reports that with
|
|
|
|
// Zend extensions such as Zend debugger and APC, this invariant
|
|
|
|
// may be broken. Since we have efficient alternatives, pay
|
|
|
|
// the cost here and avoid the bug.
|
|
|
|
require_once HTMLPURIFIER_PREFIX . '/' . $file;
|
2008-01-27 01:54:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 01:54:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the path for a specific class.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $class Class path to get
|
|
|
|
* @return string
|
2008-01-27 01:54:41 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public static function getPath($class)
|
|
|
|
{
|
|
|
|
if (strncmp('HTMLPurifier', $class, 12) !== 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-27 05:31:06 +00:00
|
|
|
// Custom implementations
|
2008-01-27 01:54:41 +00:00
|
|
|
if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
|
2008-05-16 01:49:33 +00:00
|
|
|
$code = str_replace('_', '-', substr($class, 22));
|
|
|
|
$file = 'HTMLPurifier/Language/classes/' . $code . '.php';
|
|
|
|
} else {
|
|
|
|
$file = str_replace('_', '/', $class) . '.php';
|
2008-01-27 01:54:41 +00:00
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-05-16 01:49:33 +00:00
|
|
|
return $file;
|
2008-01-27 01:54:41 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-02-18 01:11:17 +00:00
|
|
|
/**
|
|
|
|
* "Pre-registers" our autoloader on the SPL stack.
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public static function registerAutoload()
|
|
|
|
{
|
2008-02-18 01:11:17 +00:00
|
|
|
$autoload = array('HTMLPurifier_Bootstrap', 'autoload');
|
2013-07-16 11:56:14 +00:00
|
|
|
if (($funcs = spl_autoload_functions()) === false) {
|
2008-02-18 01:11:17 +00:00
|
|
|
spl_autoload_register($autoload);
|
2008-02-18 02:33:50 +00:00
|
|
|
} elseif (function_exists('spl_autoload_unregister')) {
|
2012-05-02 15:07:24 +00:00
|
|
|
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
|
|
|
|
// prepend flag exists, no need for shenanigans
|
|
|
|
spl_autoload_register($autoload, true, true);
|
|
|
|
} else {
|
|
|
|
$buggy = version_compare(PHP_VERSION, '5.2.11', '<');
|
|
|
|
$compat = version_compare(PHP_VERSION, '5.1.2', '<=') &&
|
|
|
|
version_compare(PHP_VERSION, '5.1.0', '>=');
|
|
|
|
foreach ($funcs as $func) {
|
|
|
|
if ($buggy && is_array($func)) {
|
|
|
|
// :TRICKY: There are some compatibility issues and some
|
|
|
|
// places where we need to error out
|
|
|
|
$reflector = new ReflectionMethod($func[0], $func[1]);
|
|
|
|
if (!$reflector->isStatic()) {
|
2013-07-16 11:56:14 +00:00
|
|
|
throw new Exception(
|
|
|
|
'HTML Purifier autoloader registrar is not compatible
|
2012-05-02 15:07:24 +00:00
|
|
|
with non-static object methods due to PHP Bug #44144;
|
|
|
|
Please do not use HTMLPurifier.autoload.php (or any
|
|
|
|
file that includes this file); instead, place the code:
|
|
|
|
spl_autoload_register(array(\'HTMLPurifier_Bootstrap\', \'autoload\'))
|
2013-07-16 11:56:14 +00:00
|
|
|
after your own autoloaders.'
|
|
|
|
);
|
2012-05-02 15:07:24 +00:00
|
|
|
}
|
|
|
|
// Suprisingly, spl_autoload_register supports the
|
|
|
|
// Class::staticMethod callback format, although call_user_func doesn't
|
2013-07-16 11:56:14 +00:00
|
|
|
if ($compat) {
|
|
|
|
$func = implode('::', $func);
|
|
|
|
}
|
2008-02-18 02:33:50 +00:00
|
|
|
}
|
2012-05-02 15:07:24 +00:00
|
|
|
spl_autoload_unregister($func);
|
2008-02-18 02:33:50 +00:00
|
|
|
}
|
2012-05-02 15:07:24 +00:00
|
|
|
spl_autoload_register($autoload);
|
2013-07-16 11:56:14 +00:00
|
|
|
foreach ($funcs as $func) {
|
|
|
|
spl_autoload_register($func);
|
|
|
|
}
|
2008-02-18 02:33:50 +00:00
|
|
|
}
|
2008-02-18 01:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-27 01:54:41 +00:00
|
|
|
}
|
2008-12-06 09:24:59 +00:00
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|