Esempio n. 1
0
 /**
  * 
  * @return type
  */
 public static function getCombinedFile()
 {
     $aGet = self::getArray(array('f' => 'alnum', 'd' => 'int', 'i' => 'int', 'type' => 'word'));
     $iLifetime = (int) $aGet['d'] * 24 * 60 * 60;
     $aCache = JchPlatformCache::getCache($aGet['f'], $iLifetime);
     if ($aCache === FALSE) {
         die('File not found');
     }
     $aTimeMFile = JchPlatformUtility::RFC1123DateAdd($aCache['filemtime'], $aGet['d']);
     $sTimeMFile = $aTimeMFile['filemtime'] . ' GMT';
     $sExpiryDate = $aTimeMFile['expiry'] . ' GMT';
     $headers = array();
     if (function_exists('apache_request_headers')) {
         $headers = apache_request_headers();
     }
     if (!empty($headers) && isset($headers['If-Modified-Since']) && strtotime($headers['If-Modified-Since']) == strtotime($sTimeMFile)) {
         // Client's cache IS current, so we just respond '304 Not Modified'.
         header('HTTP/1.1 304 Not Modified');
         header('Connection: close');
         return;
     } else {
         header('Last-Modified: ' . $sTimeMFile);
     }
     $sFile = $aCache['file'][$aGet['i']];
     $sFile = JchOptimizeOutput::getCachedFile($sFile, $iLifetime);
     $aSpriteCss = $aCache['spritecss'];
     if ($aGet['type'] == 'css') {
         if (!empty($aSpriteCss)) {
             $sFile = str_replace($aSpriteCss['needles'], $aSpriteCss['replacements'], $sFile);
         }
         if (!empty($aCache['font-face'])) {
             $sFile = str_replace($aCache['font-face'], '', $sFile);
         }
         $oCssParser = new JchOptimizeCssParser();
         $sFile = $oCssParser->sortImports($sFile);
         if (function_exists('mb_convert_encoding')) {
             $sFile = '@charset "utf-8";' . $sFile;
         }
     }
     if ($aGet['type'] == 'css') {
         header('Content-type: text/css; charset=UTF-8');
     } elseif ($aGet['type'] == 'js') {
         header('Content-type: text/javascript; charset=UTF-8');
     }
     header('Expires: ' . $sExpiryDate);
     header('Cache-Control: Public');
     header('Vary: Accept-Encoding');
     $gzip = TRUE;
     if (isset($_SERVER['HTTP_USER_AGENT'])) {
         /* Facebook User Agent
          * facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)
          * LinkedIn User Agent
          * LinkedInBot/1.0 (compatible; Mozilla/5.0; Jakarta Commons-HttpClient/3.1 +http://www.linkedin.com)
          */
         $pattern = strtolower('/facebookexternalhit|LinkedInBot/x');
         if (preg_match($pattern, strtolower($_SERVER['HTTP_USER_AGENT']))) {
             $gzip = FALSE;
         }
     }
     if (isset($aGet['gz']) && $aGet['gz'] == 'gz' && $gzip) {
         $aSupported = array('x-gzip' => 'gz', 'gzip' => 'gz', 'deflate' => 'deflate');
         if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
             $aAccepted = array_map('trim', (array) explode(',', $_SERVER['HTTP_ACCEPT_ENCODING']));
             $aEncodings = array_intersect($aAccepted, array_keys($aSupported));
         } else {
             $aEncodings = array('gzip');
         }
         if (!empty($aEncodings)) {
             foreach ($aEncodings as $sEncoding) {
                 if ($aSupported[$sEncoding] == 'gz' || $aSupported[$sEncoding] == 'deflate') {
                     $sGzFile = gzencode($sFile, 4, $aSupported[$sEncoding] == 'gz' ? FORCE_GZIP : FORCE_DEFLATE);
                     if ($sGzFile === FALSE) {
                         continue;
                     }
                     header('Content-Encoding: ' . $sEncoding);
                     $sFile = $sGzFile;
                     break;
                 }
             }
         }
     }
     header('Content-Length: ' . strlen($sFile));
     echo $sFile;
 }