BxDolGzip.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. class BxDolGzip extends BxDol {
  9. var $_sType;
  10. var $_bGzip;
  11. var $_sEncoding;
  12. var $_iExpirationOffset;
  13. var $_sInFile;
  14. var $_sOutFile;
  15. var $_sOutContent;
  16. var $_sOutContentZipped;
  17. function BxDolGzip($sFile) {
  18. parent::BxDol();
  19. $this->_iExpirationOffset = 3600 * 24 * 10;
  20. $this->_sInFile = BX_DIRECTORY_PATH_CACHE_PUBLIC . $sFile;
  21. $this->_sOutFile = BX_DIRECTORY_PATH_CACHE_PUBLIC . $sFile . '.gz';
  22. $aMatches = array();
  23. if(!preg_match("/^([a-z0-9_-]+)\.(js|css)$/", $sFile, $aMatches))
  24. die('BxDolGzip: file is not js/css');
  25. switch($aMatches[2]) {
  26. case 'css':
  27. $this->_sType = 'css';
  28. break;
  29. case 'js':
  30. $this->_sType = 'javascript';
  31. break;
  32. }
  33. $this->_sOutContent = "";
  34. $this->_sOutContentZipped = "";
  35. }
  36. static function load($sFile) {
  37. $oLoader = new BxDolGzip($sFile);
  38. $oLoader->prepare();
  39. $oLoader->read();
  40. $oLoader->output();
  41. }
  42. function prepare() {
  43. header("Content-type: text/" . $this->_sType);
  44. header("Vary: Accept-Encoding");
  45. header("Expires: " . gmdate("D, d M Y H:i:s", time() + $this->_iExpirationOffset) . " GMT");
  46. $encodings = array ();
  47. if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
  48. $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
  49. if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
  50. $this->_sEncoding = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
  51. $this->_bGzip = true;
  52. }
  53. }
  54. function read() {
  55. if($this->_bGzip && file_exists($this->_sOutFile))
  56. $this->_sOutContentZipped = $this->getFileContents($this->_sOutFile);
  57. else
  58. $this->_sOutContent = $this->getFileContents($this->_sInFile);
  59. }
  60. function output() {
  61. if(!$this->_bGzip) {
  62. echo $this->_sOutContent;
  63. return;
  64. }
  65. header("Content-Encoding: " . $this->_sEncoding);
  66. if(!empty($this->_sOutContentZipped)) {
  67. echo $this->_sOutContentZipped;
  68. return;
  69. }
  70. if (!$this->_sOutContent)
  71. return;
  72. $this->_sOutContentZipped = gzencode($this->_sOutContent, 9, FORCE_GZIP);
  73. $this->putFileContents($this->_sOutFile, $this->_sOutContentZipped);
  74. echo $this->_sOutContentZipped;
  75. }
  76. function getFileContents($sPath) {
  77. $sPath = realpath($sPath);
  78. if(!$sPath || !@is_file($sPath))
  79. return "";
  80. if(function_exists("file_get_contents"))
  81. return @file_get_contents($sPath);
  82. $sContent = "";
  83. if(!($rHandler = @fopen($sPath, "r")))
  84. return "";
  85. while(!feof($rHandler))
  86. $sContent .= fgets($rHandler);
  87. fclose($rHandler);
  88. return $sContent;
  89. }
  90. function putFileContents($sPath, $sContent) {
  91. if (function_exists("file_put_contents"))
  92. return @file_put_contents($sPath, $sContent);
  93. if(!($rHandler = @fopen($sPath, "wb")))
  94. return 0;
  95. $iResult = (int)fwrite($rHandler, $sContent);
  96. fclose($rHandler);
  97. return $iResult;
  98. }
  99. }