Пример #1
0
 function get_javascripts()
 {
     global $Config;
     if (!empty($Config['use_jspacker']) and !empty($Config['cache_public_dir'])) {
         include_once 'lib/fckpacker.php';
         # good but not work with prototype.
         define('JS_PACKER', 'FCK_Packer/MoniWiki');
         $constProc = new FCKConstantProcessor();
         #$constProc->RemoveDeclaration = false ;
         #include_once('lib/jspacker.php'); # bad!
         #$packer = new JavaScriptPacker('', 0);
         #$packer->pack(); // init compressor
         #include_once('lib/jsmin.php'); # not work.
         $out = '';
         $packed = '';
         $pjs = array();
         $keys = array();
         foreach ($this->java_scripts as $k => $js) {
             if (!empty($js)) {
                 $keys[] = $k;
             }
         }
         if (empty($keys)) {
             return '';
         }
         $uniq = md5(implode(';', $keys));
         $cache = new Cache_text('js', array('ext' => 'html'));
         if ($cache->exists($uniq)) {
             foreach ($keys as $k) {
                 $this->java_scripts[$k] = '';
             }
             return $cache->fetch($uniq);
         }
         foreach ($this->java_scripts as $k => $js) {
             if ($js) {
                 if ($js[0] != '<') {
                     $async = '';
                     if (strpos($js, ',') !== false && substr($js, 0, 5) == 'async') {
                         $async = ' async';
                         $js = substr($js, 6);
                     }
                     if (preg_match('@^(http://|/)@', $js)) {
                         $out .= "<script{$async} type='text/javascript' src='{$js}'></script>\n";
                     } else {
                         if (file_exists('local/' . $js)) {
                             $fp = fopen('local/' . $js, 'r');
                             if (is_resource($fp)) {
                                 $_js = fread($fp, filesize('local/' . $js));
                                 fclose($fp);
                                 $packed .= '/* ' . $js . ' */' . "\n";
                                 #$packed.= JSMin::minify($_js);
                                 $packed .= FCKJavaScriptCompressor::Compress($_js, $constProc) . "\n";
                                 #$packed.= $packer->_pack($_js)."\n";
                                 $pjs[] = $k;
                             }
                         } else {
                             // is it exist ?
                             $js = $this->url_prefix . '/local/' . $js;
                             $out .= "<script{$async} type='text/javascript' src='{$js}'></script>\n";
                         }
                     }
                 } else {
                     //
                     $out .= $js;
                     if (0 and preg_match('/<script[^>]+(src=("|\')([^\\2]+)\\2)?[^>]*>(.*)<\\/script>\\s*$/s', $js, $m)) {
                         if (!empty($m[3])) {
                             $out .= $js;
                             #$out.="<script type='text/javascript' src='$js'></script>\n";
                         } else {
                             if (!empty($m[4])) {
                                 $packed .= '/* embeded ' . $k . '*/' . "\n";
                                 #$packed.= $packer->_pack($js)."\n";
                                 $packed .= FCKJavaScriptCompressor::Compress($m[4], $constProc) . "\n";
                                 #$packed.= JSMin::minify($js);
                                 $pjs[] = $k;
                             }
                         }
                     }
                 }
                 $this->java_scripts[$k] = '';
             }
         }
         $suniq = md5(implode(';', $pjs));
         $fc = new Cache_text('js', array('ext' => 'js', 'dir' => $Config['cache_public_dir']));
         $jsname = $fc->getKey($suniq, 0);
         $out .= '<script type="text/javascript" src="' . $Config['cache_public_url'] . '/' . $jsname . '"></script>' . "\n";
         $cache->update($uniq, $out);
         $ver = FCKJavaScriptCompressor::Revision();
         $header = '/* ' . JS_PACKER . ' ' . $ver . ' ' . md5($packed) . ' ' . date('Y-m-d H:i:s') . ' */' . "\n";
         # save real compressed js file.
         $fc->_save($jsname, $header . $packed);
         return $out;
     }
     $out = '';
     foreach ($this->java_scripts as $k => $js) {
         if ($js) {
             if ($js[0] != '<') {
                 $async = '';
                 if (strpos($js, ',') !== false && substr($js, 0, 5) == 'async') {
                     $async = ' async';
                     $js = substr($js, 6);
                 }
                 if (!preg_match('@^(http://|/)@', $js)) {
                     $js = $this->url_prefix . '/local/' . $js;
                 }
                 $out .= "<script{$async} type='text/javascript' src='{$js}'></script>\n";
             } else {
                 $out .= $js;
             }
             $this->java_scripts[$k] = '';
         }
     }
     return $out;
 }
Пример #2
0
 function CreateFile()
 {
     echo 'Packaging file ' . basename($this->_OutputPath) . "\n";
     // Extract the directory from the output file path.
     $destDir = dirname($this->_OutputPath);
     // Create the directory if it doesn't exist.
     if (!@is_dir($destDir)) {
         CreateDir($destDir);
     }
     // Create the StringBuilder that will hold the output data.
     $outputData = '';
     $uncompressedSize = 0;
     // Loop through the files.
     foreach ($this->_Files as $file) {
         // Read the file.
         $data = file_get_contents($file);
         // Strip the UTF-8 BOM, if available.
         $data = StripUtf8Bom($data);
         $dataSize = strlen($data);
         $uncompressedSize += $dataSize;
         echo '    Adding ' . basename($file) . "\n";
         // Compress (if needed) and process its contents.
         if ($this->CompactJavaScript) {
             $outputData .= FCKJavaScriptCompressor::Compress(FCKPreProcessor::Process($data), $this->ConstantsProcessor);
         } else {
             $outputData .= FCKPreProcessor::Process($data);
         }
         // Each file terminates with a CRLF, even if compressed.
         $outputData .= "\r\n";
     }
     // Replace global vars.
     if ($this->RenameGlobals) {
         $funcProcessor = new FCKFunctionProcessor($outputData, NULL, true);
         $outputData = $funcProcessor->Process();
     }
     // Write the output file.
     if (strlen($this->Header) > 0) {
         $outputData = $this->Header . "\r\n" . $outputData;
     }
     if (!SaveStringToFile($outputData, $this->_OutputPath, TRUE)) {
         ExitError('It was not possible to save the file "' . $this->_OutputPath . '".');
     }
     echo "\n";
     echo '    Number of files processed: ' . count($this->_Files) . "\n";
     echo '    Original size............: ' . number_format($uncompressedSize) . ' bytes' . "\n";
     echo '    Output file size.........: ' . number_format(strlen($outputData)) . ' bytes (' . round(strlen($outputData) / $uncompressedSize * 100, 2) . '% of original)' . "\n";
     echo "\n";
 }
Пример #3
0
 function _ProcessFunctionMatch($match)
 {
     // Creates an array with the parameters names ($match[1]).
     if (strlen(trim($match[1])) == 0) {
         $parameters = array();
     } else {
         $parameters = preg_split('/\\s*,\\s*/', trim($match[1]));
     }
     $hasfuncProcessor = isset($GLOBALS['funcProcessor']);
     if ($hasfuncProcessor != TRUE) {
         $GLOBALS['funcProcessor'] = new FCKFunctionProcessor($match[0], $parameters, false);
     } else {
         $GLOBALS['funcProcessor']->_Function = $match[0];
         $GLOBALS['funcProcessor']->_Parameters = $parameters;
     }
     $processed = $GLOBALS['funcProcessor']->Process();
     $processed = substr_replace($processed, '', 0, 8);
     $processed = FCKJavaScriptCompressor::_ProcessFunctions($processed);
     if ($hasfuncProcessor != TRUE) {
         unset($GLOBALS['funcProcessor']);
     }
     return 'function' . $processed;
 }