<?php
/**
 * Goweb Sitemanager 5 Framework
 *
 * @version    5.0.25
 * @category   Goweb
 * @package    Goweb_View_Helper
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */

/**
 * Abstract class for extension
 */
goweb::loadClass('Zend_View_Helper_Abstract');

require_once 'External/htmlpurifier/HTMLPurifier.auto.php';

/**
 *
 * @version    5.0.25
 * @category   Goweb
 * @package    Goweb_View_Helper_Purify
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */

class Goweb_View_Helper_Purify extends Zend_View_Helper_Abstract {

    /**
     * HTMLPurifier class
     *
     * @var HTMLPurifier
     */
    protected $_htmlPurifier = null;

    /**
     * Purify html
     *
     * @param String $value
     * @return String
     */
    public function purify($value){
        if (is_null($this->_htmlPurifier)){
            ini_set('max_execution_time', 300);
            $config = HTMLPurifier_Config::createDefault();
            if (!is_dir(CACHE_PATH . '/htmlpurifier')){
                @mkdir(CACHE_PATH . '/htmlpurifier');
                @chmod(CACHE_PATH . '/htmlpurifier', 0777);
            }
            $config->set('Cache.SerializerPath', CACHE_PATH . '/htmlpurifier');
            $config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
            $config->set('AutoFormat.Linkify', true);
            $config->set('AutoFormat.AutoParagraph', true);
            $config->set('AutoFormat.RemoveEmpty', true);
            $config->set('HTML.BlockWrapper', 'div');
            $config->set('HTML.TidyLevel', 'heavy');
            $config->set('HTML.Trusted', true);
            $config->set('Core.RemoveInvalidImg', true);
            $this->_htmlPurifier = new HTMLPurifier($config);
        }
        $value = $this->_htmlPurifier->purify($value);
        if (extension_loaded('tidy')) {
            $config = array(
                'accessibility-check'           => 3,
                'drop-proprietary-attributes'   => true,
                'drop-empty-paras'              => true,
                'hide-comments'                 => true,
                'drop-font-tags'                => true,
                'show-body-only'                => true,
                'indent'                        => true,
                'output-xhtml'                  => true,
                'wrap'                          => 200
            );

            // Tidy
            $tidy = new tidy;
            $tidy->parseString($value, $config, str_replace('-', '', CHARSET));
            $tidy->cleanRepair();

            // Output
            $value =  $tidy;
        }
        return $value;
    }
}