PluginProbe
Autoptimize / 2.1.2
Autoptimize v2.1.2
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classes / autoptimizeCache.php

autoptimizeCache.php in Autoptimize 2.1.2, at classes/autoptimizeCache.php

266 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 class autoptimizeCache {
5 private $filename;
6 private $mime;
7 private $cachedir;
8 private $delayed;
9
10 public function __construct($md5,$ext='php') {
11 $this->cachedir = AUTOPTIMIZE_CACHE_DIR;
12 $this->delayed = AUTOPTIMIZE_CACHE_DELAY;
13 $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP;
14 if($this->nogzip == false) {
15 $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.php';
16 } else {
17 if (in_array($ext, array("js","css"))) {
18 $this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext;
19 } else {
20 $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext;
21 }
22 }
23 }
24
25 public function check() {
26 if(!file_exists($this->cachedir.$this->filename)) {
27 // No cached file, sorry
28 return false;
29 }
30 // Cache exists!
31 return true;
32 }
33
34 public function retrieve() {
35 if($this->check()) {
36 if($this->nogzip == false) {
37 return file_get_contents($this->cachedir.$this->filename.'.none');
38 } else {
39 return file_get_contents($this->cachedir.$this->filename);
40 }
41 }
42 return false;
43 }
44
45 public function cache($code,$mime) {
46 if($this->nogzip == false) {
47 $file = ($this->delayed ? 'delayed.php' : 'default.php');
48 $phpcode = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'/config/'.$file);
49 $phpcode = str_replace(array('%%CONTENT%%','exit;'),array($mime,''),$phpcode);
50 file_put_contents($this->cachedir.$this->filename,$phpcode, LOCK_EX);
51 file_put_contents($this->cachedir.$this->filename.'.none',$code, LOCK_EX);
52 if(!$this->delayed) {
53 // Compress now!
54 file_put_contents($this->cachedir.$this->filename.'.deflate',gzencode($code,9,FORCE_DEFLATE), LOCK_EX);
55 file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP), LOCK_EX);
56 }
57 } else {
58 // Write code to cache without doing anything else
59 file_put_contents($this->cachedir.$this->filename,$code, LOCK_EX);
60 if (apply_filters('autoptimize_filter_cache_create_static_gzip', false)) {
61 // Create an additional cached gzip file
62 file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP), LOCK_EX);
63 }
64 }
65 }
66
67 public function getname() {
68 apply_filters('autoptimize_filter_cache_getname',AUTOPTIMIZE_CACHE_URL.$this->filename);
69 return $this->filename;
70 }
71
72 static function clearall() {
73 if(!autoptimizeCache::cacheavail()) {
74 return false;
75 }
76
77 // scan the cachedirs
78 foreach (array("","js","css") as $scandirName) {
79 $scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName);
80 }
81
82 // clear the cachedirs
83 foreach ($scan as $scandirName=>$scanneddir) {
84 $thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/";
85 foreach($scanneddir as $file) {
86 if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false && is_file($thisAoCacheDir.$file)) {
87 @unlink($thisAoCacheDir.$file);
88 }
89 }
90 }
91
92 @unlink(AUTOPTIMIZE_CACHE_DIR."/.htaccess");
93 delete_transient("autoptimize_stats");
94
95 // add cachepurged action
96 if (!function_exists('autoptimize_do_cachepurged_action')) {
97 function autoptimize_do_cachepurged_action() {
98 do_action("autoptimize_action_cachepurged");
99 }
100 }
101 add_action("shutdown","autoptimize_do_cachepurged_action",11);
102
103 // try to purge caching plugins cache-files?
104 include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizePageCacheFlush.php');
105 add_action("autoptimize_action_cachepurged","autoptimize_flush_pagecache",10,0);
106
107 return true;
108 }
109
110 static function stats() {
111 $AOstats=get_transient("autoptimize_stats");
112
113 if (empty($AOstats)) {
114 // Cache not available :(
115 if(!autoptimizeCache::cacheavail()) {
116 return 0;
117 }
118
119 // Count cached info
120 $count = 0;
121 $size = 0;
122
123 // scan the cachedirs
124 foreach (array("","js","css") as $scandirName) {
125 $scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName);
126 }
127
128 foreach ($scan as $scandirName=>$scanneddir) {
129 $thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/";
130 foreach($scanneddir as $file) {
131 if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false) {
132 if(is_file($thisAoCacheDir.$file)) {
133 if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false || strpos($file,'.img') !== false || strpos($file,'.txt') !== false )) {
134 $count++;
135 } elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false) {
136 $count++;
137 }
138 $size+=filesize($thisAoCacheDir.$file);
139 }
140 }
141 }
142 }
143 $AOstats=array($count,$size,time());
144 if ($count>100) {
145 set_transient("autoptimize_stats",$AOstats,HOUR_IN_SECONDS);
146 }
147 }
148 // print the number of instances
149 return $AOstats;
150 }
151
152 static function cacheavail() {
153 if(!defined('AUTOPTIMIZE_CACHE_DIR')) {
154 // We didn't set a cache
155 return false;
156 }
157
158 foreach (array("","js","css") as $checkDir) {
159 if(!autoptimizeCache::checkCacheDir(AUTOPTIMIZE_CACHE_DIR.$checkDir)) {
160 return false;
161 }
162 }
163
164 /** write index.html here to avoid prying eyes */
165 $indexFile=AUTOPTIMIZE_CACHE_DIR.'/index.html';
166 if(!is_file($indexFile)) {
167 @file_put_contents($indexFile,'<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>');
168 }
169
170 /** write .htaccess here to overrule wp_super_cache */
171 $htAccess=AUTOPTIMIZE_CACHE_DIR.'/.htaccess';
172 if(!is_file($htAccess)) {
173 /**
174 * create wp-content/AO_htaccess_tmpl with
175 * whatever htaccess rules you might need
176 * if you want to override default AO htaccess
177 */
178 $htaccess_tmpl=WP_CONTENT_DIR."/AO_htaccess_tmpl";
179 if (is_file($htaccess_tmpl)) {
180 $htAccessContent=file_get_contents($htaccess_tmpl);
181 } else if (is_multisite() || AUTOPTIMIZE_CACHE_NOGZIP == false) {
182 $htAccessContent='<IfModule mod_headers.c>
183 Header set Vary "Accept-Encoding"
184 Header set Cache-Control "max-age=10672000, must-revalidate"
185 </IfModule>
186 <IfModule mod_expires.c>
187 ExpiresActive On
188 ExpiresByType text/css A30672000
189 ExpiresByType text/javascript A30672000
190 ExpiresByType application/javascript A30672000
191 </IfModule>
192 <IfModule mod_deflate.c>
193 <FilesMatch "\.(js|css)$">
194 SetOutputFilter DEFLATE
195 </FilesMatch>
196 </IfModule>
197 <IfModule mod_authz_core.c>
198 <Files *.php>
199 Require all granted
200 </Files>
201 </IfModule>
202 <IfModule !mod_authz_core.c>
203 <Files *.php>
204 Order allow,deny
205 Allow from all
206 </Files>
207 </IfModule>';
208 } else {
209 $htAccessContent='<IfModule mod_headers.c>
210 Header set Vary "Accept-Encoding"
211 Header set Cache-Control "max-age=10672000, must-revalidate"
212 </IfModule>
213 <IfModule mod_expires.c>
214 ExpiresActive On
215 ExpiresByType text/css A30672000
216 ExpiresByType text/javascript A30672000
217 ExpiresByType application/javascript A30672000
218 </IfModule>
219 <IfModule mod_deflate.c>
220 <FilesMatch "\.(js|css)$">
221 SetOutputFilter DEFLATE
222 </FilesMatch>
223 </IfModule>
224 <IfModule mod_authz_core.c>
225 <Files *.php>
226 Require all denied
227 </Files>
228 </IfModule>
229 <IfModule !mod_authz_core.c>
230 <Files *.php>
231 Order deny,allow
232 Deny from all
233 </Files>
234 </IfModule>';
235 }
236 @file_put_contents($htAccess,$htAccessContent);
237 }
238
239 // All OK
240 return true;
241 }
242
243 static function checkCacheDir($dir) {
244 // Check and create if not exists
245 if(!file_exists($dir)) {
246 @mkdir($dir,0775,true);
247 if(!file_exists($dir)) {
248 return false;
249 }
250 }
251
252 // check if we can now write
253 if(!is_writable($dir)) {
254 return false;
255 }
256
257 // and write index.html here to avoid prying eyes
258 $indexFile=$dir.'/index.html';
259 if(!is_file($indexFile)) {
260 @file_put_contents($indexFile,'<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>');
261 }
262
263 return true;
264 }
265 }
266