BxDolGzip.php 3.7 KB

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