fileCombinator.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. function deduceContentType ($fileToLoad)
  23. {
  24. $contentType = '';
  25. $fileExtension = pathinfo($fileToLoad, PATHINFO_EXTENSION);
  26. if ($fileExtension == 'css')
  27. {
  28. $contentType = 'text/css';
  29. }
  30. elseif ($fileExtension == 'js' )
  31. {
  32. $contentType = 'application/x-javascript';
  33. }
  34. return $contentType;
  35. }
  36. /* main() */
  37. $filesToLoad = explode('&', $_SERVER['QUERY_STRING']);
  38. $contentType = '';
  39. $responseBody = '';
  40. $servingYuiFile = false;
  41. foreach ($filesToLoad as $fileToLoad)
  42. {
  43. /* Assumes a request has only homogenous file types, which holds true for
  44. * the combined requests YUI makes.
  45. */
  46. if (empty($contentType))
  47. {
  48. $contentType = deduceContentType($fileToLoad);
  49. if (preg_match('/^yui/', $fileToLoad))
  50. {
  51. $servingYuiFile = true;
  52. }
  53. }
  54. $fileContents = file_get_contents('./' . $fileToLoad);
  55. if ($fileContents)
  56. {
  57. $responseBody .= $fileContents;
  58. }
  59. }
  60. header('Content-type: ' . $contentType);
  61. header('Content-Length: ' . strlen($responseBody));
  62. /* When we serve YUI files, make sure they're cached for a long time. */
  63. if( $servingYuiFile )
  64. {
  65. $validitySecs = 24 * 60 * 60; /* 1 day */
  66. header('Cache-Control: max-age=' . $validitySecs . ', must-revalidate, public');
  67. }
  68. echo $responseBody;
  69. ?>