PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/media.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 142 lines | 87 code | 24 blank | 31 comment | 11 complexity | a2c96f2efa5e501f889241100a9e407a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Media Controller
  4. * A controller used to serve css, images and js files from the media directory.
  5. *
  6. * This class also compresses js and css files with gzip compression (if the browser supports it) and correctly handles
  7. * ETag/Last-Modified headers to prevent sending unmodified files to the client.
  8. *
  9. * //// GZIP Compression has been disabled in this controller and is handled
  10. * by settings in the config.php file where it can be enabled/disabled ////
  11. *
  12. * PHP version 5
  13. * LICENSE: This source file is subject to LGPL license
  14. * that is available through the world-wide-web at the following URI:
  15. * http://www.gnu.org/copyleft/lesser.html
  16. * @author Ushahidi Team <team@ushahidi.com>
  17. * @package Ushahidi - http://source.ushahididev.com
  18. * @subpackage Controllers
  19. * @copyright Ushahidi - http://www.ushahidi.com
  20. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  21. */
  22. class Media_Controller extends Controller {
  23. // Javascript URI
  24. public function js()
  25. {
  26. $this->_send();
  27. }
  28. // CSS URI
  29. public function css()
  30. {
  31. $this->_send();
  32. }
  33. // Image URI
  34. public function img()
  35. {
  36. $this->_send();
  37. }
  38. // Method retrieves file data via file_get_contents
  39. public function _send()
  40. {
  41. $gzip = false; // Enable/Disable GZip Compression
  42. $segments = $this->uri->segment_array(); // URI Segments
  43. $file = array_pop($segments);
  44. $file_path = implode("/", $segments);
  45. $pos = strrpos($file, '.');
  46. if ($pos === false)
  47. {
  48. $ext = '';
  49. }
  50. else
  51. {
  52. $ext = substr($file,$pos+1);
  53. $file = substr($file,0,$pos);
  54. }
  55. $file = $file_path."/".$file.".".$ext;
  56. if ( ! file_exists($file)) {
  57. // If the file doesn't exist, just pass an empty file.
  58. $file = false;
  59. $file_data = '';
  60. }
  61. else
  62. {
  63. $mtime = filemtime($file);
  64. $file_data = file_get_contents($file);
  65. }
  66. if ($ext == "css")
  67. { // Compress CSS data
  68. $file_data = $this->_css_compress($file_data);
  69. }
  70. // HTTP Headers
  71. $expiry_time = 613200; // 1 Week
  72. $mime = ($ext == 'css') ? 'text/css' : 'application/javascript';
  73. header('Content-type: '.$mime);
  74. header('Cache-Control: must-revalidate');
  75. header('Expires: '.gmdate("D, d M Y H:i:s", time() + $expiry_time).' GMT');
  76. if (isset($mtime))
  77. {
  78. header('ETag: '.$mtime);
  79. }
  80. header("Last-Modified: ".gmdate("D, d M Y H:i:s", $mtime)." GMT");
  81. $oldetag = isset($_SERVER['HTTP_IF_NONE_MATCH'])?trim($_SERVER['HTTP_IF_NONE_MATCH']):'';
  82. $oldmtime = isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])?$_SERVER['HTTP_IF_MODIFIED_SINCE']:'';
  83. $accencoding = isset($_SERVER['HTTP_ACCEPT_ENCODING'])?$_SERVER['HTTP_ACCEPT_ENCODING']:'';
  84. if (($oldmtime AND strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $oldmtime) OR $oldetag == $mtime)
  85. {
  86. header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
  87. }
  88. else
  89. {
  90. if (strpos($accencoding, 'gzip') !== false AND $gzip)
  91. {
  92. header('Content-Encoding: gzip');
  93. echo gzencode($file_data);
  94. }
  95. else echo $file_data;
  96. }
  97. }
  98. private function _css_compress($data)
  99. {
  100. // Remove comments
  101. $data = preg_replace('~/\*[^*]*\*+([^/][^*]*\*+)*/~', '', $data);
  102. // Replace all whitespace by single spaces
  103. $data = preg_replace('~\s+~', ' ', $data);
  104. // Remove needless whitespace
  105. $data = preg_replace('~ *+([{}+>:;,]) *~', '$1', trim($data));
  106. // Remove ; that closes last property of each declaration
  107. $data = str_replace(';}', '}', $data);
  108. // Remove empty CSS declarations
  109. $data = preg_replace('~[^{}]++\{\}~', '', $data);
  110. return $data;
  111. }
  112. private function _js_compress($data)
  113. {
  114. $packer = new JavaScriptPacker($data, $this->pack_js);
  115. return $packer->pack();
  116. }
  117. }
  118. ?>