Cheetah
ChWsbGzip.php
Go to the documentation of this file.
1 <?php
2 
8 class ChWsbGzip
9 {
10  var $_sType;
11  var $_bGzip;
14 
15  var $_sInFile;
16 
20 
21  function __construct($sFile)
22  {
23  $this->_iExpirationOffset = 3600 * 24 * 10;
24 
25  $this->_sInFile = CH_DIRECTORY_PATH_CACHE_PUBLIC . $sFile;
26  $this->_sOutFile = CH_DIRECTORY_PATH_CACHE_PUBLIC . $sFile . '.gz';
27 
28  $aMatches = array();
29  if(!preg_match("/^([a-z0-9_-]+)\.(js|css)$/", $sFile, $aMatches))
30  die();
31 
32  switch($aMatches[2]) {
33  case 'css':
34  $this->_sType = 'css';
35  break;
36  case 'js':
37  $this->_sType = 'javascript';
38  break;
39  }
40 
41  $this->_sOutContent = "";
42  $this->_sOutContentZipped = "";
43  }
44 
45  public static function load($sFile)
46  {
47  $oLoader = new ChWsbGzip($sFile);
48 
49  $oLoader->prepare();
50  $oLoader->read();
51  $oLoader->output();
52  }
53 
54  function prepare()
55  {
56  header("Content-type: text/" . $this->_sType);
57  header("Vary: Accept-Encoding");
58  header("Expires: " . gmdate("D, d M Y H:i:s", time() + $this->_iExpirationOffset) . " GMT");
59 
60  $encodings = array ();
61  if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
62  $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
63 
64  if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
65  $this->_sEncoding = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
66  $this->_bGzip = true;
67  }
68  }
69 
70  function read()
71  {
72  if($this->_bGzip && file_exists($this->_sOutFile))
73  $this->_sOutContentZipped = $this->getFileContents($this->_sOutFile);
74  else
75  $this->_sOutContent = $this->getFileContents($this->_sInFile);
76  }
77  function output()
78  {
79  if(!$this->_bGzip) {
81  return;
82  }
83 
84  header("Content-Encoding: " . $this->_sEncoding);
85  if(!empty($this->_sOutContentZipped)) {
87  return;
88  }
89 
90  if (!$this->_sOutContent)
91  return;
92 
93  $this->_sOutContentZipped = gzencode($this->_sOutContent, 9, FORCE_GZIP);
94  $this->putFileContents($this->_sOutFile, $this->_sOutContentZipped);
95 
97  }
98  function getFileContents($sPath)
99  {
100  $sPath = realpath($sPath);
101 
102  if(!$sPath || !@is_file($sPath))
103  return "";
104 
105  if(function_exists("file_get_contents"))
106  return @file_get_contents($sPath);
107 
108  $sContent = "";
109  if(!($rHandler = @fopen($sPath, "r")))
110  return "";
111 
112  while(!feof($rHandler))
113  $sContent .= fgets($rHandler);
114 
115  fclose($rHandler);
116 
117  return $sContent;
118  }
119 
120  function putFileContents($sPath, $sContent)
121  {
122  if (function_exists("file_put_contents"))
123  return @file_put_contents($sPath, $sContent);
124 
125  if(!($rHandler = @fopen($sPath, "wb")))
126  return 0;
127 
128  $iResult = (int)fwrite($rHandler, $sContent);
129  fclose($rHandler);
130 
131  return $iResult;
132  }
133 }
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
ChWsbGzip\prepare
prepare()
Definition: ChWsbGzip.php:54
ChWsbGzip\$_iExpirationOffset
$_iExpirationOffset
Definition: ChWsbGzip.php:13
ChWsbGzip\putFileContents
putFileContents($sPath, $sContent)
Definition: ChWsbGzip.php:120
ChWsbGzip\output
output()
Definition: ChWsbGzip.php:77
ChWsbGzip\$_bGzip
$_bGzip
Definition: ChWsbGzip.php:11
php
ChWsbGzip\read
read()
Definition: ChWsbGzip.php:70
ChWsbGzip\getFileContents
getFileContents($sPath)
Definition: ChWsbGzip.php:98
ChWsbGzip\$_sInFile
$_sInFile
Definition: ChWsbGzip.php:15
ChWsbGzip\$_sOutContent
$_sOutContent
Definition: ChWsbGzip.php:18
ChWsbGzip\$_sEncoding
$_sEncoding
Definition: ChWsbGzip.php:12
$sFile
$sFile
Definition: index.php:20
$sContent
$sContent
Definition: bottom_menu_compose.php:169
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
ChWsbGzip\__construct
__construct($sFile)
Definition: ChWsbGzip.php:21
ChWsbGzip\$_sOutFile
$_sOutFile
Definition: ChWsbGzip.php:17
ChWsbGzip
Definition: ChWsbGzip.php:9
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
ChWsbGzip\$_sOutContentZipped
$_sOutContentZipped
Definition: ChWsbGzip.php:19
ChWsbGzip\load
static load($sFile)
Definition: ChWsbGzip.php:45
ChWsbGzip\$_sType
$_sType
Definition: ChWsbGzip.php:10