Include HTMLpurifier with Zend_Loader

て烟熏妆下的殇ゞ 提交于 2019-12-20 15:30:47

问题


I want to use the HTMLpurifier in combination with the Zend Framework. I would love to load the Class and its files with the Zend_Loader. How would you include it? Would you just use the HTMLPurifier.auto.php or do you know a better way of doing it?


回答1:


I use HTML Purifier as a filter in my Zend Framework project. Here's an altered version of my class:

require_once 'HTMLPurifier.includes.php';
require_once 'HTMLPurifier.autoload.php';

class My_Filter_HtmlPurifier implements Zend_Filter_Interface
{
    protected $_htmlPurifier = null;

    public function __construct($options = null)
    {
        // set up configuration
        $config = HTMLPurifier_Config::createDefault();
        $config->set('HTML.DefinitionID', 'My Filter');
        $config->set('HTML.DefinitionRev', 1); // increment when configuration changes
        // $config->set('Cache.DefinitionImpl', null); // comment out after finalizing the config

        // Doctype
        $config->set('HTML.Doctype', 'XHTML 1.0 Transitional');

        // configure caching
        $cachePath = APPLICATION_PATH . '/../cache/htmlpurifier';
        if (!is_dir($cachePath)) {
            mkdir($cachePath, 0755, true);
        }
        $cachePath = realpath($cachePath);
        $config->set('Cache.SerializerPath', $cachePath);

        if (!is_null($options)) {
            //$config = HTMLPurifier_Config::createDefault();
            foreach ($options as $option) {
                $config->set($option[0], $option[1], $option[2]);
            }
        }

        $this->_htmlPurifier = new HTMLPurifier($config);
    }

    public function filter($value)
    {
        return $this->_htmlPurifier->purify($value);
    }
}



回答2:


Unless I'm misunderstanding the question (Or HTMLpurifier). If you have Zend_Loader running and it's set to autoload.

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

Or something to that effect. Put the HTMLpurifier class in your library directory. I'm just not sure on it's actual class name.

You can just put the class file in the library directory and call it by it's name, or maybe toss it in a misc package.

Examples

// SITE/library/Zend/Auth.php
class Zend_Auth
{
}

// SITE/library/htmlpurifier.php
class htmlpurifier
{
}

// SITE/library/misc/htmlpurifier.php
class Misc_HTMLpurifier
{
}

Make sense?




回答3:


you can register an autoloader class using the Zend_Loader class. when you call the registerAutoLoad() method without any parameters, you are actually registering Zend_Loader itself as an autoloader. so:

Zend_Loader::registerAutoLoad();

// equals to: Zend_Loader::registerAutoLoad('Zend_Loader'),true);

Zend_Loader tries to load classes using Zend Framework's naming convention, which is like this:

  • each class is defined in a separate file
  • each class name begins with a capitalized letter
  • underlines in class name, means a directory level.

so if 'Zend_Loader' is the name of a class, it is defined in the file 'Loader.php' in 'Zend' directory in your path. to you PHP can file load this class from file Zend/Loader.php

if your classes follow this naming convention, they can be automatically loaded using the same autoloader. else, you need to define your own autoloader. write an autoloader class winch can extend Zend_Loader, and define the loading functionality so that it will load classes with other naming conventions. then register your own autoloader with Zend_Loader. like this:

Zend_Loader::registerAutoLoad('myLoader',true);



回答4:


I've put the contents of library of the archive of HTMLPurifier in my library path. So I have this directory structure :

library/
  HTMLPurifier\
  HTMLPurifier.auto.php
  ...
  HTMLPurifier.safe-includes.php

And then I put this on top of the file where I'm using the HTMLPurifier :

require_once 'HTMLPurifier.safe-includes.php';

Ugly, but it's working.



来源:https://stackoverflow.com/questions/646827/include-htmlpurifier-with-zend-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!