/**
   * (non-PHPdoc)
   * @see acAssetOptimizer::compress()
   */
  public function compress($filePath)
  {
    $source = file_get_contents($filePath);

    $source = iconv('UTF-8', 'CP1252', $source);

    $packer = new Packer($source, 'None', true, false);

    $compressed = $packer->pack();

    $compressed = iconv('CP1252', 'UTF-8', $compressed);

    return $compressed;
  }
Example #2
0
    /**
     * build the boot function used for loading and decoding
     */
    private function _bootStrap($packed, $keywords)
    {
        $ENCODE = $this->_safeRegExp('$encode\\($count\\)');

        // $packed: the packed script
        $packed = "'" . $this->_escape($packed) . "'";

        // $ascii: base for encoding
        $ascii = min(count($keywords['sorted']), $this->_encoding);
        if ($ascii == 0) {
            $ascii = 1;
        }

        // $count: number of words contained in the script
        $count = count($keywords['sorted']);

        // $keywords: list of words contained in the script
        foreach ($keywords['protected'] as $i=>$value) {
          $keywords['sorted'][$i] = '';
        }
        // convert from a string to an array
        ksort($keywords['sorted']);
        $keywords = "'" . implode('|',$keywords['sorted']) . "'.split('|')";

        $encode = ($this->_encoding > 62) ? '_encode95' : $this->_getEncoder($ascii);
        $encode = $this->_getJSFunction($encode);
        $encode = preg_replace('/_encoding/','$ascii', $encode);
        $encode = preg_replace('/arguments\\.callee/','$encode', $encode);
        $inline = '\\$count' . ($ascii > 10 ? '.toString(\\$ascii)' : '');

        // $decode: code snippet to speed up decoding
        if ($this->_fastDecode) {
            // create the decoder
            $decode = $this->_getJSFunction('_decodeBody');
            if ($this->_encoding > 62) {
              $decode = preg_replace('/\\\\w/', '[\\xa1-\\xff]', $decode);
            } elseif ($ascii < 36) {
                // perform the encoding inline for lower ascii values
                $decode = preg_replace($ENCODE, $inline, $decode);
                // special case: when $count==0 there are no keywords. I want to keep
                //  the basic shape of the unpacking funcion so i'll frig the code...
            }
            if ($count == 0) {
                $decode = preg_replace($this->_safeRegExp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
            }
        }

        // boot function
        $unpack = $this->_getJSFunction('_unpack');

        if ($this->_fastDecode) {
            // insert the decoder
            $this->buffer = $decode;
            $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastDecode'), $unpack, 1);
        }

        $unpack = preg_replace('/"/', "'", $unpack);

        if ($this->_encoding > 62) { // high-ascii
            // get rid of the word-boundaries for regexp matches
            $unpack = preg_replace('/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/', '', $unpack);
        }
        if ($ascii > 36 || $this->_encoding > 62 || $this->_fastDecode) {
            // insert the encode function
            $this->buffer = $encode;
            $unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastEncode'), $unpack, 1);
        } else {
            // perform the encoding inline
            $unpack = preg_replace($ENCODE, $inline, $unpack);
        }
        // pack the boot function too
        $unpackPacker = new Packer($unpack, 0, false, true);
        $unpack = $unpackPacker->pack();

        // arguments
        $params = array($packed, $ascii, $count, $keywords);
        if ($this->_fastDecode) {
            $params[] = 0;
            $params[] = '{}';
        }
        $params = implode(',', $params);

        // the whole thing
        return 'eval(' . $unpack . '(' . $params . "))\n";
    }