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 // We bundle the minify library...
23 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/minify/min/lib');
25 class MinifyAction extends Action
27 const TYPE_CSS = 'text/css';
28 const TYPE_HTML = 'text/html';
29 // there is some debate over the ideal JS Content-Type, but this is the
30 // Apache default and what Yahoo! uses..
31 const TYPE_JS = 'application/x-javascript';
33 const cacheKey = 'minify';
37 function isReadOnly($args)
42 function prepare($args)
44 parent::prepare($args);
45 $this->v = $args['v'];
49 $this->file = INSTALLDIR.'/'.$f;
50 if(file_exists($this->file)) {
53 $this->clientError(_('f parameter is not a valid path'),404);
57 $this->clientError(_('f parameter is required'),500);
65 return "\"" . crc32($this->file . $this->v) . "\"";
67 $stat = stat($this->file);
68 return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
72 function lastModified()
74 return filemtime($this->file);
77 function handle($args)
79 parent::handle($args);
81 $c = common_memcache();
83 $out = $c->get(common_cache_key(self::cacheKey . ':' . $this->file));
86 $out = $this->minify($this->file);
89 $c->set(common_cache_key(self::cacheKey . ':' . $this->file), $out);
92 $sec = session_cache_expire() * 60;
93 header('Cache-Control: public, max-age=' . $sec);
94 header('Pragma: public');
98 function minify($file)
100 $info = pathinfo($file);
101 switch(strtolower($info['extension'])){
103 require_once('JSMin.php');
104 $out = JSMin::minify(file_get_contents($file));
105 header('Content-Type: ' . self::TYPE_JS);
108 require_once('Minify/CSS.php');
110 $options['currentDir'] = dirname($file);
111 $options['docRoot'] = INSTALLDIR;
112 $out = Minify_CSS::minify(file_get_contents($file),$options);
113 header('Content-Type: ' . self::TYPE_CSS);
116 $this->clientError(_('File type not supported'),500);