Skip to content Skip to sidebar Skip to footer

Include HTMLpurifier With Zend_Loader

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 u

Solution 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);
    }
}

Solution 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?


Solution 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);

Solution 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.


Post a Comment for "Include HTMLpurifier With Zend_Loader"