PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php/common.php

https://github.com/gcao/jsgameviewer4bbs
PHP | 135 lines | 84 code | 15 blank | 36 comment | 31 complexity | b850d056225b1861be22a8b25f38199c MD5 | raw file
  1. <?php
  2. /************************************************************************
  3. * CSS and Javascript Combinator 0.5
  4. * Copyright 2006 by Niels Leenheer
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. function combine($type, $elements, $base, $cache, $cachedir){
  26. // Determine last modification date of the files
  27. $lastmodified = 0;
  28. $s = "";
  29. while (list(,$element) = each($elements)) {
  30. $s .= $element . ',';
  31. $path = realpath($base . '/' . $element);
  32. $lastmodified = max($lastmodified, filemtime($path));
  33. }
  34. // Send Etag hash
  35. $hash = $lastmodified . '-' . md5($s);
  36. if (strlen($hash) > 60)
  37. $hash = substr($hash, 0, 60);
  38. header ("Etag: \"" . $hash . "\"");
  39. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
  40. stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) == '"' . $hash . '"')
  41. {
  42. // Return visit and no modifications, so do not send anything
  43. header ("HTTP/1.0 304 Not Modified");
  44. header ('Content-Length: 0');
  45. }
  46. else
  47. {
  48. // First time visit or files were modified
  49. if ($cache)
  50. {
  51. // Determine supported compression method
  52. $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
  53. $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
  54. // Determine used compression method
  55. $encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
  56. // Check for buggy versions of Internet Explorer
  57. if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&
  58. preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
  59. $version = floatval($matches[1]);
  60. if ($version < 6)
  61. $encoding = 'none';
  62. if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))
  63. $encoding = 'none';
  64. }
  65. // Try the cache first to see if the combined files were already generated
  66. $cachefile = 'cache-' . $hash . '.' . $type . ($encoding != 'none' ? '.' . $encoding : '');
  67. if (file_exists($cachedir . '/' . $cachefile)) {
  68. if ($fp = fopen($cachedir . '/' . $cachefile, 'rb')) {
  69. if ($encoding != 'none') {
  70. header ("Content-Encoding: " . $encoding);
  71. }
  72. header ("Content-Type: text/" . $type);
  73. header ("Content-Length: " . filesize($cachedir . '/' . $cachefile));
  74. fpassthru($fp);
  75. fclose($fp);
  76. exit;
  77. }
  78. }
  79. }
  80. // Get contents of the files
  81. $contents = '';
  82. reset($elements);
  83. while (list(,$element) = each($elements)) {
  84. $contents .= "\n\n";
  85. $path = realpath($base . '/' . $element);
  86. $file = @fopen($path, 'r');
  87. while (!feof($file)){
  88. $buffer = trim(fgets($file));
  89. if ($buffer{0} == '*' || ($buffer{0} == '/' &&
  90. ($buffer{1} == '*' || $buffer{1} == '/')))
  91. continue;
  92. $contents .= $buffer . "\n";
  93. }
  94. }
  95. // Send Content-Type
  96. header ("Content-Type: text/" . $type);
  97. if (isset($encoding) && $encoding != 'none')
  98. {
  99. // Send compressed contents
  100. $contents = gzencode($contents, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
  101. header ("Content-Encoding: " . $encoding);
  102. header ('Content-Length: ' . strlen($contents));
  103. echo $contents;
  104. }
  105. else
  106. {
  107. // Send regular contents
  108. header ('Content-Length: ' . strlen($contents));
  109. echo $contents;
  110. }
  111. // Store cache
  112. if ($cache) {
  113. if ($fp = fopen($cachedir . '/' . $cachefile, 'wb')) {
  114. fwrite($fp, $contents);
  115. fclose($fp);
  116. }
  117. }
  118. }
  119. }
  120. ?>