Cheetah
ChJsGzipLoader.php
Go to the documentation of this file.
1 <?php
2 
9 {
16  function __construct ($sType, $p, $sJsDir = '', $sCacheDir = '')
17  {
18  $this->_sType = $sType;
19  $this->_p = $p;
20  $this->_a = array (); // array of js files
21  $this->_sJsDir = $sJsDir;
22 
23  $this->_sCacheDir = $sCacheDir;
24  $this->_bCache = $sCacheDir ? true : false;
25  $this->_sCacheFilename = '';
26 
27  $this->_bGzip = false; // gzip supported
28  $this->_sEnc = ''; // encoding
29 
30  $this->_c = ''; // content;
31  $this->_zc = ''; // gzip content;
32 
33  $this->sendheaders ();
34  $this->buildJsList ();
35  $this->checkEncoding ();
36  if ($this->cacheRead()) exit;
37  $this->readContent ();
38  $this->outputContent ();
39  }
40 
41  function buildJsList ()
42  {
43  if ( 'ja' == $this->_sType) {
44  foreach ($this->_p as $sJsFile) {
45  $this->_a[] = $this->_sJsDir . $sJsFile;
46  $this->_sCacheFilename .= $sJsFile;
47  }
48  $this->_sCacheFilename = md5 ($this->_sCacheFilename);
49  return;
50  }
51 
52  if ('d' == $this->_sType && is_dir ($this->_p)) {
53 
54  if (!($dh = opendir($this->_p))) return;
55 
56  while (($sJsFile = readdir($dh)) !== false) {
57  if (strtolower(substr($sJsFile, -3)) != '.js') continue;
58  $this->_a[] = $this->_p . $sJsFile;
59  $this->_sCacheFilename .= $sJsFile;
60  }
61 
62  $this->_sCacheFilename = md5 ($this->_sCacheFilename);
63 
64  closedir($dh);
65 
66  //print_r ($this->_a); exit;
67  } else {
68  die ("alert ('Wrong js directory')");
69  }
70  }
71 
75  function checkEncoding ()
76  {
77  $encodings = array ();
78  if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
79  $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
80 
81  if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression') && ini_get('output_handler') != 'ob_gzhandler') {
82  $this->_sEnc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
83  $this->_bGzip = true;
84  }
85  }
86 
87  function readContent ()
88  {
89  foreach ($this->_a as $sFile)
90  $this->_c .= $this->getFile($sFile);
91  }
92 
93  function outputContent ()
94  {
95  if (!$this->_bGzip) {
96  echo $this->_c;
97  return;
98  }
99 
100  header("Content-Encoding: " . $this->_sEnc);
101  $this->_cz = gzencode ($this->_c, 9, FORCE_GZIP);
102 
103  $this->cacheWrite();
104 
105  // Stream to client
106  echo $this->_cz;
107  }
108 
109  function getFile($s)
110  {
111  $path = realpath($s);
112 
113  if (!$path || !@is_file($s))
114  return "";
115 
116  if (function_exists("file_get_contents"))
117  return @file_get_contents($path);
118 
119  $content = "";
120  $fp = @fopen($path, "r");
121  if (!$fp)
122  return "";
123 
124  while (!feof($fp))
125  $content .= fgets($fp);
126 
127  fclose($fp);
128 
129  return $content;
130  }
131 
132  function putFile($s, $c)
133  {
134  if (function_exists("file_put_contents"))
135  return @file_put_contents($s, $c);
136 
137  $f = @fopen($s, "wb");
138  if ($f) {
139  fwrite($f, $c);
140  fclose($f);
141  }
142  }
143 
144  function sendheaders ()
145  {
146  header("Content-type: text/javascript");
147  header("Vary: Accept-Encoding"); // Handle proxies
148  header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
149  }
150 
151  function cacheRead()
152  {
153  if (!$this->_bGzip) return false;
154 
155  if (!$this->_bCache) return false;
156 
157  $fn = $this->_sCacheDir . $this->_sCacheFilename;
158 
159  if (!file_exists($fn)) return false;
160 
161  header("Content-Encoding: " . $this->_sEnc);
162 
163  echo $this->getFile($fn);
164 
165  return true;
166  }
167 
168  function cacheWrite()
169  {
170  if (!$this->_bCache) return;
171 
172  $fn = $this->_sCacheDir . $this->_sCacheFilename;
173 
174  $this->putFile($fn, $this->_cz);
175  }
176 }
header
</code > Be careful enabling this directive if you have a redirector script that does not use the< code > Location</code > HTTP header
Definition: URI.MungeResources.txt:10
true
if(!defined("TRUE_VAL")) define("TRUE_VAL" true
Definition: constants.inc.php:8
$f
global $f
Definition: callback.php:13
ChJsGzipLoader\readContent
readContent()
Definition: ChJsGzipLoader.php:87
ChJsGzipLoader\outputContent
outputContent()
Definition: ChJsGzipLoader.php:93
php
ChJsGzipLoader\buildJsList
buildJsList()
Definition: ChJsGzipLoader.php:41
ChJsGzipLoader\__construct
__construct($sType, $p, $sJsDir='', $sCacheDir='')
Definition: ChJsGzipLoader.php:16
exit
exit
Definition: cart.php:21
$sType
$sType
Definition: actions.inc.php:11
$sFile
$sFile
Definition: index.php:20
ChJsGzipLoader\sendheaders
sendheaders()
Definition: ChJsGzipLoader.php:144
$path
$path
Definition: header.inc.php:12
time
that in the case of a Adaptation or at a minimum such credit will if a credit for all contributing authors of the Adaptation or Collection then as part of these credits and in a manner at least as prominent as the credits for the other contributing authors For the avoidance of You may only use the credit required by this Section for the purpose of attribution in the manner set out above by exercising Your rights under this You may not implicitly or explicitly assert or imply any connection sponsorship or endorsement by the Original Licensor and or Attribution as of You or Your use of the without the express prior written permission of the Original Licensor and or Attribution Parties Except as otherwise agreed in writing by the Licensor or as may be otherwise permitted by applicable if You Distribute or Publicly Perform the Work either by itself or as part of any Adaptations or You must not modify or take other derogatory action in relation to the Work which would be prejudicial to the Original Author s honor or reputation Licensor agrees that in those in which any exercise of the right granted in modification or other derogatory action prejudicial to the Original Author s honor and the Licensor will waive or not as this to the fullest extent permitted by the applicable national to enable You to reasonably exercise Your right under Warranties and Disclaimer UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN LICENSOR OFFERS THE WORK AS IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE STATUTORY OR WITHOUT WARRANTIES OF FITNESS FOR A PARTICULAR OR THE ABSENCE OF LATENT OR OTHER OR THE PRESENCE OF ABSENCE OF WHETHER OR NOT DISCOVERABLE SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED SO SUCH EXCLUSION MAY NOT APPLY TO YOU Limitation on Liability EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES Termination This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License Individuals or entities who have received Adaptations or Collections from You under this will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses and will survive any termination of this License Subject to the above terms and the license granted here is Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time
Definition: license.txt:56
ChJsGzipLoader\getFile
getFile($s)
Definition: ChJsGzipLoader.php:109
$s
$s
Definition: embed.php:13
ChJsGzipLoader\putFile
putFile($s, $c)
Definition: ChJsGzipLoader.php:132
ChJsGzipLoader\checkEncoding
checkEncoding()
Definition: ChJsGzipLoader.php:75
ChJsGzipLoader
Definition: ChJsGzipLoader.php:9
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChJsGzipLoader\cacheRead
cacheRead()
Definition: ChJsGzipLoader.php:151
ChJsGzipLoader\cacheWrite
cacheWrite()
Definition: ChJsGzipLoader.php:168