minify.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. class MinifyAction extends Action
  21. {
  22. const TYPE_CSS = 'text/css';
  23. const TYPE_HTML = 'text/html';
  24. // there is some debate over the ideal JS Content-Type, but this is the
  25. // Apache default and what Yahoo! uses..
  26. const TYPE_JS = 'application/x-javascript';
  27. var $file;
  28. var $v;
  29. function isReadOnly($args)
  30. {
  31. return true;
  32. }
  33. function prepare($args)
  34. {
  35. parent::prepare($args);
  36. $this->v = $args['v'];
  37. $f = $this->arg('f');
  38. if(isset($f)) {
  39. $this->file = INSTALLDIR.'/'.$f;
  40. if(file_exists($this->file)) {
  41. return true;
  42. } else {
  43. // TRANS: Client error displayed when not providing a valid path in parameter "f".
  44. $this->clientError(_m('The parameter "f" is not a valid path.'),404);
  45. }
  46. }else{
  47. // TRANS: Client error displayed when not providing parameter "f".
  48. $this->clientError(_m('The parameter "f" is required but missing.'),500);
  49. }
  50. }
  51. function etag()
  52. {
  53. if(isset($this->v)) {
  54. return "\"" . crc32($this->file . $this->v) . "\"";
  55. }else{
  56. $stat = stat($this->file);
  57. return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
  58. }
  59. }
  60. function lastModified()
  61. {
  62. return filemtime($this->file);
  63. }
  64. function handle($args)
  65. {
  66. parent::handle($args);
  67. $c = Cache::instance();
  68. if (!empty($c)) {
  69. $cacheKey = Cache::key(MinifyPlugin::cacheKey . ':' . $this->file . '?v=' . empty($this->v)?'':$this->v);
  70. $out = $c->get($cacheKey);
  71. }
  72. if(empty($out)) {
  73. $out = $this->minify($this->file);
  74. }
  75. if (!empty($c)) {
  76. $c->set($cacheKey, $out);
  77. }
  78. $sec = session_cache_expire() * 60;
  79. header('Cache-Control: public, max-age=' . $sec);
  80. header('Pragma: public');
  81. $this->raw($out);
  82. }
  83. function minify($file)
  84. {
  85. $info = pathinfo($file);
  86. switch(strtolower($info['extension'])){
  87. case 'js':
  88. $out = MinifyPlugin::minifyJs(file_get_contents($file));
  89. header('Content-Type: ' . self::TYPE_JS);
  90. break;
  91. case 'css':
  92. $options = array();
  93. $options['currentDir'] = dirname($file);
  94. $options['docRoot'] = INSTALLDIR;
  95. $out = MinifyPlugin::minifyCss(file_get_contents($file),$options);
  96. header('Content-Type: ' . self::TYPE_CSS);
  97. break;
  98. default:
  99. // TRANS: Client error displayed when trying to minify an unsupported file type.
  100. $this->clientError(_m('File type not supported.'),500);
  101. }
  102. return $out;
  103. }
  104. }