1<?php 2/* 3 * e107 website system 4 * 5 * Copyright (C) 2008-2012 e107 Inc (e107.org) 6 * Released under the terms and conditions of the 7 * GNU General Public License (http://gnu.org). 8 * 9 * Javascript files consolidation script (gzip compression) 10 * 11 * $URL$ 12 * $Id$ 13 * 14 */ 15 16// prevent notices/warnings to break JS source 17error_reporting(0); 18 19//admin or front-end call 20if (strpos($_SERVER['QUERY_STRING'], '_admin') !== FALSE) 21{ 22 define('ADMIN_AREA', true); //force admin area 23} 24else 25{ 26 define('USER_AREA', true); //force user area 27} 28// no-browser-cache check 29if (strpos($_SERVER['QUERY_STRING'], '_nobcache') !== FALSE) 30{ 31 define('e_NOCACHE', true); //force no browser cache 32} 33else 34{ 35 define('e_NOCACHE', false); 36} 37 38// no-server-cache check 39if (strpos($_SERVER['QUERY_STRING'], '_nocache') !== FALSE) 40{ 41 define('e_NOSCACHE', true); //force no system cache 42} 43else 44{ 45 define('e_NOSCACHE', false); 46} 47 48if(!e_NOCACHE) session_cache_limiter('private'); 49 50$eJslibCacheDir = null; 51 52//output cache if available before calling the api 53e_jslib_cache_out(); 54 55//v0.8 - we need THEME defines here (do we?) - WE DON'T 56//$_E107 = array('no_forceuserupdate' => 1, 'no_online' => 1, 'no_menus' => 1, 'no_prunetmp' => 1); 57$_E107['minimal'] = true; 58 59//call jslib handler, render content 60require_once ("../../class2.php"); 61//require_once (e_HANDLER.'jslib_handler.php'); 62//$jslib = new e_jslib(); 63$jslib = e107::getObject('e_jslib', null, e_HANDLER.'jslib_handler.php'); 64$jslib->getContent(); 65 66exit; 67 68// 69// FUNCTIONS required for retrieveing cache without e107 API 70// 71 72/** 73 * Output cache file contents if available (doesn't require e107 API) 74 * 75 * @return void 76 */ 77function e_jslib_cache_out() 78{ 79 $encoding = e_jslib_browser_enc(); //NOTE - should be called first 80 $cacheFile = e_jslib_is_cache($encoding); 81 82 if ($cacheFile) 83 { 84 //kill any output buffering - better performance and 304 not modified requirement 85 while (@ob_end_clean()); 86 87 /* IT CAUSES GREAT TROUBLES ON SOME BROWSERS! 88 if (function_exists('date_default_timezone_set')) 89 { 90 date_default_timezone_set('UTC'); 91 } 92 93 // last modification time 94 $lmodified = filemtime($cacheFile); 95 96 // send last modified date 97 //header('Cache-Control: must-revalidate'); 98 //header('Last-modified: '.gmdate('r', $lmodified), true); 99 if($lmodified) header('Last-modified: '.gmdate("D, d M Y H:i:s", $lmodified).' GMT', true);*/ 100 101 // send content type and encoding 102 header('Content-type: text/javascript', true); 103 if ($encoding) 104 { 105 header('Content-Encoding: '.$encoding, true); 106 } 107 108 if (!e_NOCACHE) header("Cache-Control: must-revalidate", true); 109 110 /*// Expire header - 1 year 111 $time = time()+ 365 * 86400; 112 //header('Expires: '.gmdate('r', $time), true); 113 header('Expires: '.gmdate("D, d M Y H:i:s", $time).' GMT', true); 114 115 header('Cache-Control: must-revalidate', true); 116 117 // not modified check by last modified time - send 304 and exit 118 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lmodified) 119 { 120 header("HTTP/1.1 304 Not Modified", true); 121 exit; 122 }*/ 123 124 $page = @file_get_contents($cacheFile); 125 $etag = md5($page).($encoding ? '-'.$encoding : ''); 126 127 header('Content-Length: '.strlen($page), true); 128 header('ETag: '.$etag, true); 129 130 // not modified check by Etag 131 if (!e_NOCACHE && isset($_SERVER['HTTP_IF_NONE_MATCH'])) 132 { 133 $IF_NONE_MATCH = str_replace('"','',$_SERVER['HTTP_IF_NONE_MATCH']); 134 135 if($IF_NONE_MATCH == $etag || ($IF_NONE_MATCH == ($etag.'-'.$encoding))) 136 { 137 header('HTTP/1.1 304 Not Modified'); 138 exit(); 139 } 140 } 141 142 echo $page; 143 //TODO - debug 144 //@file_put_contents('cache/e_jslib_log', "----------\ncache used - ".$cacheFile."\n\n", FILE_APPEND); 145 exit; 146 } 147} 148 149/** 150 * Check jslib cache (doesn't require e107 API) 151 * 152 * @param string $encoding browser accepted encoding 153 * @return string cache filename on success or empty string otherwise 154 */ 155function e_jslib_is_cache($encoding) 156{ 157 //if(!e_NOSCACHE) return ''; 158 $cacheFile = e_jslib_cache_filename($encoding); 159 if (is_file($cacheFile) && is_readable($cacheFile)) 160 { 161 return $cacheFile; 162 } 163 164 return ''; 165} 166 167/** 168 * Detect browser accepted encoding (doesn't require e107 API) 169 * It'll always return empty string if '_nogzip' found in QUERY_STRING 170 * 171 * @return string encoding 172 */ 173function e_jslib_browser_enc() 174{ 175 //NEW - option to disable completely gzip compression 176 if(strpos($_SERVER['QUERY_STRING'], '_nogzip') !== false) 177 { 178 return ''; 179 } 180 //double-compression fix - thanks Topper 181 if (headers_sent() || ini_get('zlib.output_compression') || !isset($_SERVER["HTTP_ACCEPT_ENCODING"])) 182 { 183 $encoding = ''; 184 } 185 elseif (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'x-gzip') !== false) 186 { 187 $encoding = 'x-gzip'; 188 } 189 elseif (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') !== false) 190 { 191 $encoding = 'gzip'; 192 } 193 else 194 { 195 $encoding = ''; 196 } 197 198 return $encoding; 199} 200 201/** 202 * Creates cache filename (doesn't require e107 API) 203 * 204 * @param string $encoding 205 * @return string cache filename 206 */ 207function e_jslib_cache_filename($encoding = '') 208{ 209 $cacheDir = e_jslib_cache_path(); 210 $hash = $_SERVER['QUERY_STRING'] && $_SERVER['QUERY_STRING'] !== '_nogzip' ? md5(str_replace('_nogzip', '', $_SERVER['QUERY_STRING'])) : 'nomd5'; 211 $cacheFile = $cacheDir.'S_e_jslib'.($encoding ? '_'.$encoding : '').'_'.$hash.'.cache.php'; 212 213 return $cacheFile; 214} 215 216/** 217 * Retrieve cache system path (doesn't require e107 API) 218 * 219 * @return string path to cache folder 220 */ 221function e_jslib_cache_path() 222{ 223 global $eJslibCacheDir; 224 225 if(null === $eJslibCacheDir) 226 { 227 include('../../e107_config.php'); 228 229 if($CACHE_DIRECTORY) 230 { 231 $eJslibCacheDir = '../'.$CACHE_DIRECTORY.'content/'; 232 } 233 elseif (isset($E107_CONFIG) && isset($E107_CONFIG['CACHE_DIRECTORY'])) 234 { 235 $eJslibCacheDir = '../'.$E107_CONFIG['CACHE_DIRECTORY'].'content/'; 236 } 237 else $eJslibCacheDir = ''; 238 } 239 return $eJslibCacheDir; 240} 241?>