PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/autoptimize/classes/autoptimizeCache.php

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