douyaosi3164 2019-03-04 12:00
浏览 41

HTML Purifier和spl_autoload_register

Folders of my Page

system/ <-- offline files (classes, cronjobs, libraries ...)
online/ <-- contain all php scripts ( available online )

system/

classes <-- all my own classes inside.
... directories ...
libraries/htmlpurifier <- contain HTMLPurifier

I want to use spl_autoload_register()

<?php

define("DOC_ROOT", dirname(__DIR__));

function hTMLPurifier($class) {
 require DOC_ROOT . '/system/libraries/htmlpurifier/library/' . strtolower(str_replace('\\', '/', $class)) . '.php';
 }

 function myAutoload($class) {
 require DOC_ROOT . '/' . strtolower(str_replace('\\', '/', $class)) . '.php';
 }

spl_autoload_register('hTMLPurifier');
spl_autoload_register('myAutoload');


$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$clean_html = $purifier->purify($dirty_html);
?>

I get follow message.

 .../htmlpurifier_config.php): failed to open stream: No such file or directory in ...

If insert

  "require_once '/path/to/HTMLPurifier.auto.php';"

on Top

<?php

define("DOC_ROOT", dirname(__DIR__));
require_once '/path/to/HTMLPurifier.auto.php';

function myAutoload($class) {
  require DOC_ROOT . '/' . strtolower(str_replace('\\', '/', $class)) . '.php';
 }


 spl_autoload_register('myAutoload');


 $config = HTMLPurifier_Config::createDefault();
 $purifier = new HTMLPurifier($config);
 $clean_html = $purifier->purify($dirty_html);
 ?>

Everything is work.

I want to load all classes with spl_autoload_register() only without insert

"require_once '/path/to/HTMLPurifier.auto.php';" 

on top. How can, i do it???

  • 写回答

1条回答 默认 最新

  • duangan6636 2019-03-04 22:41
    关注

    HTML Purifier doesn't work with PHP namespaces, so substituting \\ with / won't net you the results you seek. Your autoload logic is trying to load the class HTMLPurifier_Config from the file htmlpurifier_config.php - so what you want is something like this:

    function autoloadHtmlPurifier($class) {
        require DOC_ROOT . '/system/libraries/htmlpurifier/library/'
            . str_replace('_', '/', $class) . '.php';
    }
    spl_autoload_register('autoloadHtmlPurifier');
    

    (Notice how this function doesn't use strtolower() and replaces _ with /.)

    But, looking at the HTML Purifier autoload implementation:

    public static function getPath($class)
    {
        if (strncmp('HTMLPurifier', $class, 12) !== 0) {
            return false;
        }
        // Custom implementations
        if (strncmp('HTMLPurifier_Language_', $class, 22) === 0) {
            $code = str_replace('_', '-', substr($class, 22));
            $file = 'HTMLPurifier/Language/classes/' . $code . '.php';
        } else {
            $file = str_replace('_', '/', $class) . '.php';
        }
        if (!file_exists(HTMLPURIFIER_PREFIX . '/' . $file)) {
            return false;
        }
        return $file;
     }
    

    ...there's a good chance the simple replacement will still not function correctly, because of the language class edge case. But you could try this:

    function autoloadHtmlPurifier($class) {
        require DOC_ROOT . '/system/libraries/htmlpurifier/library/'
            . str_replace('_', '/', $class) . '.php';
    }
    spl_autoload_register('autoloadHtmlPurifier');
    spl_autoload_register(array('HTMLPurifier_Bootstrap', 'autoload'));
    

    That uses your custom autoloader to load the HTMLPurifier_Bootstrap class, so you don't need to include it with require, but still have the benefit of using the canonical autoloader for HTML Purifier.

    Word of caution

    That said, in either case, the downside is that if HTML Purifier ever does switch to namespaces, your autoloader for it will break. I would really suggest you stick to the require_once(DOC_ROOT . '/system/libraries/htmlpurifier/library/HTMLPurifier.auto.php') approach, or install HTML Purifier using composer, which will take care of autoloading that library (and possibly others) for you:

    For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and start using the classes that those libraries provide without any extra work:

    require __DIR__ . '/vendor/autoload.php';
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP