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 $this->clientError(_('f parameter is not a valid path'),404);
53 $this->clientError(_('f parameter is required'),500);
61 return "\"" . crc32($this->file . $this->v) . "\"";
63 $stat = stat($this->file);
64 return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
68 function lastModified()
70 return filemtime($this->file);
73 function handle($args)
75 parent::handle($args);
77 $c = common_memcache();
79 $cacheKey = common_cache_key(MinifyPlugin::cacheKey . ':' . $this->file . '?v=' . empty($this->v)?'':$this->v);
80 $out = $c->get($cacheKey);
83 $out = $this->minify($this->file);
86 $c->set($cacheKey, $out);
89 $sec = session_cache_expire() * 60;
90 header('Cache-Control: public, max-age=' . $sec);
91 header('Pragma: public');
95 function minify($file)
97 $info = pathinfo($file);
98 switch(strtolower($info['extension'])){
100 $out = MinifyPlugin::minifyJs(file_get_contents($file));
101 header('Content-Type: ' . self::TYPE_JS);
105 $options['currentDir'] = dirname($file);
106 $options['docRoot'] = INSTALLDIR;
107 $out = MinifyPlugin::minifyCss(file_get_contents($file),$options);
108 header('Content-Type: ' . self::TYPE_CSS);
111 $this->clientError(_('File type not supported'),500);