]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/minify.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / minify.php
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
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 // We bundle the minify library...
23 set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/minify/min/lib');
24
25 class MinifyAction extends Action
26 {
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';
32
33     const cacheKey = 'minify';
34
35     var $file;
36
37     function isReadOnly($args)
38     {
39         return true;
40     }
41
42     function prepare($args)
43     {
44         parent::prepare($args);
45         $this->v = $args['v'];
46
47         $f = $this->arg('f');
48         if(isset($f)) {
49             $this->file = INSTALLDIR.'/'.$f;
50             if(file_exists($this->file)) {
51                 return true;
52             } else {
53                 $this->clientError(_('f parameter is not a valid path'),404);
54                 return false;
55             }
56         }else{
57             $this->clientError(_('f parameter is required'),500);
58             return false;
59         }
60     }
61
62     function etag() 
63     {
64         if(isset($this->v)) {
65             return "\"" . crc32($this->file . $this->v) . "\"";
66         }else{
67             $stat = stat($this->file);
68             return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
69         }
70     }
71
72     function lastModified()
73     {
74         return filemtime($this->file);
75     }
76
77     function handle($args)
78     {
79         parent::handle($args);
80         
81         $c = common_memcache();
82         if (!empty($c)) {
83             $out = $c->get(common_cache_key(self::cacheKey . ':' . $this->file));
84         }
85         if(empty($out)) {
86             $out = $this->minify($this->file);
87         }
88         if (!empty($c)) {
89             $c->set(common_cache_key(self::cacheKey . ':' . $this->file), $out);
90         }
91
92         $sec = session_cache_expire() * 60;
93         header('Cache-Control: public, max-age=' . $sec);
94         header('Pragma: public');
95         $this->raw($out);
96     }
97
98     function minify($file)
99     {
100         $info = pathinfo($file);
101         switch(strtolower($info['extension'])){
102             case 'js':
103                 require_once('JSMin.php');
104                 $out = JSMin::minify(file_get_contents($file));
105                 header('Content-Type: ' . self::TYPE_JS);
106                 break;
107             case 'css':
108                 require_once('Minify/CSS.php');
109                 $options = array();
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);
114                 break;
115             default:
116                 $this->clientError(_('File type not supported'),500);
117                 return false;
118         }
119         return $out;
120     }
121 }
122