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