feulf / raintpl3

The easiest Template Engine for PHP
https://feulf.github.io/raintpl
258 stars 57 forks source link

Compress plugin somehow broken #133

Open dhardtke opened 10 years ago

dhardtke commented 10 years ago

Hi,

@rainphp you broke the Compress plugin apparently, and I wanted it to have the ability to only compress if debug is false, so here is yours from a previous raintpl version with the debug thingy:

<?php
namespace Rain\Tpl\Plugin;

class Compress extends \Rain\Tpl\Plugin {

    protected $hooks = array('afterDraw'), $cache_dir;

    /**
     * Function called in the hook afterDraw
     * @param \ArrayAccess $context
     */
    public function afterDraw(\ArrayAccess $context) {
        // get the cache directory
        $this->cache_dir  = $context->conf['cache_dir'];

        $html = $context->code;

        if(!$context->conf['debug'])
            $html = $this->compressHTML($html);

        // save the compressed code
        $context->code = $html;
    }

    /**
     * Compress the HTML
     * @param type $html
     * @return type
     */
    protected function compressHTML($html) {

        // Set PCRE recursion limit to sane value = STACKSIZE / 500
        // ini_set("pcre.recursion_limit", "524"); // 256KB stack. Win32 Apache
        ini_set("pcre.recursion_limit", "16777");  // 8MB stack. *nix
        $re = '%# Collapse whitespace everywhere but in blacklisted elements.
                (?>             # Match all whitespans other than single space.
                [^\S ]\s*     # Either one [\t\r\n\f\v] and zero or more ws,
                | \s{2,}        # or two or more consecutive-any-whitespace.
                ) # Note: The remaining regex consumes no text at all...
                (?=             # Ensure we are not in a blacklist tag.
                [^<]*+        # Either zero or more non-"<" {normal*}
                (?:           # Begin {(special normal*)*} construct
                <           # or a < starting a non-blacklist tag.
                (?!/?(?:textarea|pre|script)\b)
                [^<]*+      # more non-"<" {normal*}
                )*+           # Finish "unrolling-the-loop"
                (?:           # Begin alternation group.
                <           # Either a blacklist start tag.
                (?>textarea|pre|script)\b
                | \z          # or end of file.
                )             # End alternation group.
                )  # If we made it here, we are not in a blacklist tag.
                %Six';
        $html = preg_replace($re, " ", $html);
        if ($html === null)
            exit("PCRE Error! File too big.\n");
        return $html;
    }

    public function configure( $setting, $value ){
        $this->conf[$setting] = self::$configure[$setting] = $value;
    }

    public function configureLocal( $setting, $value ){
        $this->conf[$setting] = $value;
    }

}
feulf commented 10 years ago

Thanks for reporting it, I'll check the code soon

feulf commented 10 years ago

please review