-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplxMinifyCache.php
executable file
·246 lines (221 loc) · 6.73 KB
/
plxMinifyCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Plugin plxMinifyCache
*
* @package PLX
* @version 1.4
* @date 20/04/2013
* @author i M@N, Stephane F.
**/
class plxMinifyCache extends plxPlugin {
/**
* Constructeur de la classe plxMinifyCache
*
* @param default_lang langue par défaut utilisée par PluXml
* @return null
* @authors i M@N, Stephane F.
**/
public function __construct($default_lang) {
# Appel du constructeur de la classe plxPlugin (obligatoire)
parent::__construct($default_lang);
# droits pour accéder à la page config.php du plugin
$this->setConfigProfil(PROFIL_ADMIN);
# Autorisation d'accès à l'administration du plugin
$this->setAdminProfil(PROFIL_ADMIN);
# Déclarations des hooks
$this->addHook('IndexEnd', 'IndexEnd');
}
/**
* Méthode appelée dans l'administration pour calculer la taille du cache
*
* @return human readable size
* @author Rommel Santor : http://rommelsantor.com/
**/
public function size_readable($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
/**
* Méthode appelée dans l'administration pour lister le cache
*
* @return null
* @author i M@N, Stephane F.
**/
public function plxMinifyCacheList() {
$cache_size = 0;
/* list cache dir */
if (extension_loaded('glob')) {
$cached = glob(PLX_ROOT."cache/*.html");
foreach ($cached as $file) {
$cache_size += filesize(PLX_ROOT."cache/".$file);
echo(basename($file)).'<br>';
}
echo 'total : '.$this->size_readable($cache_size, $decimals = 2);
unset($cached);
}
else {
if($cached = opendir(PLX_ROOT."cache/")) {
while(($file = readdir($cached))!== false) {
if( $file == '.' || $file == '..' )
continue;
if(strtolower(strrchr($file,'.')==".html")) {
$cache_size += filesize(PLX_ROOT."cache/".$file);
echo(basename(PLX_ROOT."cache/".$file)).'<br>';
}
}
echo 'total : '.$this->size_readable($cache_size, $decimals = 2);
closedir($cached);
}
}
}
/**
* Méthode appelée dans l'administration pour nettoyer le cache
*
* @return null
* @author i M@N, Stephane F.
**/
public function plxMinifyCacheClean() {
/* clean cache dir */
if (extension_loaded('glob')) {
$cached = glob(PLX_ROOT."cache/*.html");
foreach ($cached as $file) {
unlink($file);
}
unset($cached);
}
else {
if($cached = opendir(PLX_ROOT."cache/")) {
while(($file = readdir($cached))!== false) {
if( $file == '.' || $file == '..' )
continue;
if(strtolower(strrchr($file,'.')==".html")) {
unlink(PLX_ROOT."cache/".$file);
}
}
closedir($cached);
}
}
return plxMsg::Info($this->getLang('L_CACHE_CLEANED'));
}
/**
* Méthode appelée quand on active le plugin : pour créer le répertoire de cache
*
* @return null
* @author i M@N
**/
public function OnActivate() {
/* cache dir check */
if (!is_dir(PLX_ROOT."cache/")) {
mkdir(PLX_ROOT."cache/");
}
$plxMotor = plxMotor::getInstance();
if (version_compare($plxMotor->version, "5.1.7", ">=")) {
if (!file_exists(PLX_ROOT."data/configuration/plugins/plxMinifyCache.xml")) {
if (!copy(PLX_PLUGINS."plxMinifyCache/parameters.xml", PLX_ROOT."data/configuration/plugins/plxMinifyCache.xml")) {
return plxMsg::Error(L_SAVE_ERR.' '.PLX_PLUGINS."plxMinifyCache/parameters.xml");
}
}
}
}
/**
* Méthode appelée quand on désactive le plugin : pour nettoyer le cache
*
* @return null
* @author i M@N, Stephane F.
**/
public function OnDeactivate() {
/* clean cache dir */
if (extension_loaded('glob')) {
$cached = glob(PLX_ROOT."cache/*.html");
foreach ($cached as $file) {
unlink($file);
}
unset($cached);
}
else {
if($cached = opendir(PLX_ROOT."cache/")) {
while(($file = readdir($cached))!== false) {
if( $file == '.' || $file == '..' )
continue;
if(strtolower(strrchr($file,'.')==".html")) {
unlink(PLX_ROOT."cache/".$file);
}
}
closedir($cached);
}
}
}
/**
* Méthode qui gère le cache de la sortie écran
*
* @return stdio
* @author i M@N, Stephane F.
**/
public function IndexEnd() {
$string = '
$plxShow = plxShow::getInstance();
$minify = explode(",","'.$this->getParam("minify").'");
#$output = print_r($minify); exit; #debug
include_once(PLX_PLUGINS."plxMinifyCache/lib/HTML.php");
if (in_array("css",$minify)) {
include_once(PLX_PLUGINS."plxMinifyCache/lib/CSS.php");
include_once(PLX_PLUGINS."plxMinifyCache/lib/Compressor.php");
}
if (in_array("javascript",$minify)) {
include_once(PLX_PLUGINS."plxMinifyCache/lib/JSMin.php");
include_once(PLX_PLUGINS."plxMinifyCache/lib/CommentPreserver.php");
}
$output = preg_replace("@ {2,}|\t+@is", "", $output);
$output = preg_replace("@<br />@is", "<br>", $output);
$output = preg_replace("@<hr />@is", "<hr>", $output);
#$options = array(
## "cssMinifier" => array("Minify_CSS", "minify"),
# "jsMinifier" => array("JSMin", "minify"),
# "jsCleanComments",
# "xhtml" => true
# );
$options = array("xhtml" => true); # set Minify_HTML::minify options
if (in_array("javascript",$minify)) { # set jsMinifier::minify options
$options[jsMinifier][0] = "JSMin";
$options[jsMinifier][1] = "minify";
$options[0] = "jsCleanComments";
}
$output = Minify_HTML::minify($output, $options);
if (in_array("css",$minify)) { # set cssMinifier::minify options
$options = array("compress" => true,
"preserveComments" => false);
$output = Minify_CSS::minify($output, $options);
}
#$output = print_r($options);exit; #debug
$output = preg_replace("@\n@is", " ", $output); # suppression de retour chariot
#$output = print_r($plxShow->mode());exit; #debug
$exclude = explode(",","'.$this->getParam("exclude").'");
#$output = print_r($exclude); exit; #debug
if ((!in_array($plxShow->mode(),$exclude)) AND ($_SERVER["REQUEST_METHOD"] != "POST")) {
$delay = "'.$this->getParam("delay").'";
$cache = PLX_ROOT."cache/cache_".md5($_SERVER["QUERY_STRING"]).".html";
$expire = time() - $delay; # 3600 (1 hr)
if(@is_file($cache) AND filemtime($cache) > $expire) {
$expire_offset = $delay;
header("Expires: ".gmdate("D, d M Y H:i:s", time() + $expire_offset)." GMT");
header("Cache-Control: private, must-revalidate, proxy-revalidate, post-check=10, pre-check=60, max-age=".$expire_offset);
header("Pragma: no-cache");
$gzip = substr_count($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") ? "ob_gzhandler" : "";
ob_start($gzip);
readfile($cache);
# ob_end_flush();
exit;
} else {
$output .= "<!-- cached ".$_SERVER["QUERY_STRING"]." ".date("Y-m-d H:i:s")." -->";
file_put_contents($cache, $output);
}
}
else {
$output .= "<!-- minified ".date("Y-m-d H:i:s")." -->";
}
';
echo '<?php '.$string.' ?>';
}
}
?>