3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 class MinifyAction extends Action
24 const TYPE_CSS = 'text/css';
25 const TYPE_HTML = 'text/html';
26 // there is some debate over the ideal JS Content-Type, but this is the
27 // Apache default and what Yahoo! uses..
28 const TYPE_JS = 'application/x-javascript';
33 function isReadOnly($args)
38 function prepare($args)
40 parent::prepare($args);
41 $this->v = $args['v'];
45 $this->file = INSTALLDIR.'/'.$f;
46 if(file_exists($this->file)) {
49 // TRANS: Client error displayed when not providing a valid path in parameter "f".
50 $this->clientError(_m('The parameter "f" is not a valid path.'),404);
54 // TRANS: Client error displayed when not providing parameter "f".
55 $this->clientError(_m('The parameter "f" is required but missing.'),500);
63 return "\"" . crc32($this->file . $this->v) . "\"";
65 $stat = stat($this->file);
66 return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
70 function lastModified()
72 return filemtime($this->file);
75 function handle($args)
77 parent::handle($args);
79 $c = Cache::instance();
81 $cacheKey = Cache::key(MinifyPlugin::cacheKey . ':' . $this->file . '?v=' . empty($this->v)?'':$this->v);
82 $out = $c->get($cacheKey);
85 $out = $this->minify($this->file);
88 $c->set($cacheKey, $out);
91 $sec = session_cache_expire() * 60;
92 header('Cache-Control: public, max-age=' . $sec);
93 header('Pragma: public');
97 function minify($file)
99 $info = pathinfo($file);
100 switch(strtolower($info['extension'])){
102 $out = MinifyPlugin::minifyJs(file_get_contents($file));
103 header('Content-Type: ' . self::TYPE_JS);
107 $options['currentDir'] = dirname($file);
108 $options['docRoot'] = INSTALLDIR;
109 $out = MinifyPlugin::minifyCss(file_get_contents($file),$options);
110 header('Content-Type: ' . self::TYPE_CSS);
113 // TRANS: Client error displayed when trying to minify an unsupported file type.
114 $this->clientError(_m('File type not supported.'),500);