]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/minify.php
b7bee7c8815e572b6a4fa301a7b608fe951383df
[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 class MinifyAction extends Action
23 {
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';
29
30     var $file;
31     var $v;
32
33     function isReadOnly($args)
34     {
35         return true;
36     }
37
38     function prepare($args)
39     {
40         parent::prepare($args);
41         $this->v = $args['v'];
42
43         $f = $this->arg('f');
44         if(isset($f)) {
45             $this->file = INSTALLDIR.'/'.$f;
46             if(file_exists($this->file)) {
47                 return true;
48             } else {
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);
51                 return false;
52             }
53         }else{
54             // TRANS: Client error displayed when not providing parameter "f".
55             $this->clientError(_m('The parameter "f" is required but missing.'),500);
56             return false;
57         }
58     }
59
60     function etag()
61     {
62         if(isset($this->v)) {
63             return "\"" . crc32($this->file . $this->v) . "\"";
64         }else{
65             $stat = stat($this->file);
66             return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
67         }
68     }
69
70     function lastModified()
71     {
72         return filemtime($this->file);
73     }
74
75     function handle($args)
76     {
77         parent::handle($args);
78
79         $c = Cache::instance();
80         if (!empty($c)) {
81             $cacheKey = Cache::key(MinifyPlugin::cacheKey . ':' . $this->file . '?v=' . empty($this->v)?'':$this->v);
82             $out = $c->get($cacheKey);
83         }
84         if(empty($out)) {
85             $out = $this->minify($this->file);
86         }
87         if (!empty($c)) {
88             $c->set($cacheKey, $out);
89         }
90
91         $sec = session_cache_expire() * 60;
92         header('Cache-Control: public, max-age=' . $sec);
93         header('Pragma: public');
94         $this->raw($out);
95     }
96
97     function minify($file)
98     {
99         $info = pathinfo($file);
100         switch(strtolower($info['extension'])){
101             case 'js':
102                 $out = MinifyPlugin::minifyJs(file_get_contents($file));
103                 header('Content-Type: ' . self::TYPE_JS);
104                 break;
105             case 'css':
106                 $options = array();
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);
111                 break;
112             default:
113                 // TRANS: Client error displayed when trying to minify an unsupported file type.
114                 $this->clientError(_m('File type not supported.'),500);
115                 return false;
116         }
117         return $out;
118     }
119 }