PageRenderTime 69ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/W3/BrowserCacheAdminEnvironment.php

https://github.com/louieDA/bestilblomster
PHP | 795 lines | 521 code | 134 blank | 140 comment | 77 complexity | 853dc4ddff4a9de1963c00ea7e0765a7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0, BSD-3-Clause, AGPL-1.0
  1. <?php
  2. /**
  3. * W3 PgCache plugin - administrative interface
  4. */
  5. if (!defined('W3TC')) {
  6. die();
  7. }
  8. w3_require_once(W3TC_INC_DIR . '/functions/activation.php');
  9. w3_require_once(W3TC_INC_DIR . '/functions/file.php');
  10. w3_require_once(W3TC_INC_DIR . '/functions/rule.php');
  11. /**
  12. * Class W3_BrowserCacheAdminEnvironment
  13. */
  14. class W3_BrowserCacheAdminEnvironment {
  15. /**
  16. * Fixes environment in each wp-admin request
  17. *
  18. * @param W3_Config $config
  19. * @param bool $force_all_checks
  20. * @throws SelfTestExceptions
  21. **/
  22. public function fix_on_wpadmin_request($config, $force_all_checks) {
  23. $exs = new SelfTestExceptions();
  24. if ($config->get_boolean('config.check') || $force_all_checks) {
  25. if ($config->get_boolean('browsercache.enabled')) {
  26. $this->rules_cache_add($config, $exs);
  27. } else {
  28. $this->rules_cache_remove($exs);
  29. }
  30. if ($config->get_boolean('browsercache.enabled') &&
  31. $config->get_boolean('browsercache.no404wp')) {
  32. $this->rules_no404wp_add($config, $exs);
  33. } else {
  34. $this->rules_no404wp_remove($exs);
  35. }
  36. }
  37. if (count($exs->exceptions()) > 0)
  38. throw $exs;
  39. }
  40. /**
  41. * Fixes environment once event occurs
  42. * @throws SelfTestExceptions
  43. **/
  44. public function fix_on_event($config, $event, $old_config = null) {
  45. }
  46. /**
  47. * Fixes environment after plugin deactivation
  48. * @throws SelfTestExceptions
  49. */
  50. public function fix_after_deactivation() {
  51. $exs = new SelfTestExceptions();
  52. $this->rules_cache_remove($exs);
  53. $this->rules_no404wp_remove($exs);
  54. if (count($exs->exceptions()) > 0)
  55. throw $exs;
  56. }
  57. /**
  58. * Returns required rules for module
  59. *
  60. * @param W3_Config $config
  61. * @return array
  62. */
  63. public function get_required_rules($config) {
  64. if (!$config->get_boolean('browsercache.enabled'))
  65. return null;
  66. $rewrite_rules = array();
  67. $dispatcher = w3_instance('W3_Dispatcher');
  68. if ($dispatcher->should_browsercache_generate_rules_for_cdn($config)) {
  69. $domain = $dispatcher->get_cdn_domain();
  70. $cdn_rules_path = sprintf('ftp://%s/%s', $domain,
  71. w3_get_cdn_rules_path());
  72. $rewrite_rules[] = array(
  73. 'filename' => $cdn_rules_path,
  74. 'content' => $this->rules_cache_generate($config, true)
  75. );
  76. }
  77. $browsercache_rules_cache_path = w3_get_browsercache_rules_cache_path();
  78. $rewrite_rules[] = array(
  79. 'filename' => $browsercache_rules_cache_path,
  80. 'content' => $this->rules_cache_generate($config)
  81. );
  82. if ($config->get_boolean('browsercache.no404wp')) {
  83. $browsercache_rules_no404wp_path =
  84. w3_get_browsercache_rules_no404wp_path();
  85. $rewrite_rules[] = array(
  86. 'filename' => $browsercache_rules_no404wp_path,
  87. 'content' => $this->rules_no404wp_generate($config)
  88. );
  89. }
  90. return $rewrite_rules;
  91. }
  92. /**
  93. * Returns mime types
  94. *
  95. * @return array
  96. */
  97. public function get_mime_types() {
  98. return array(
  99. 'cssjs' => include W3TC_INC_DIR . '/mime/cssjs.php',
  100. 'html' => include W3TC_INC_DIR . '/mime/html.php',
  101. 'other' => include W3TC_INC_DIR . '/mime/other.php'
  102. );
  103. }
  104. /**
  105. * Generate rules for FTP upload
  106. *
  107. * @param W3_Config $config
  108. * @return string
  109. **/
  110. public function rules_cache_generate_for_ftp($config) {
  111. return $this->rules_cache_generate($config, true);
  112. }
  113. /**
  114. * rules cache
  115. **/
  116. /**
  117. * Writes cache rules
  118. *
  119. * @throws FilesystemOperationException with S/FTP form if it can't get the required filesystem credentials
  120. * @throws FileOperationException
  121. */
  122. private function rules_cache_add($config, $exs) {
  123. w3_add_rules($exs,
  124. w3_get_browsercache_rules_cache_path(),
  125. $this->rules_cache_generate($config),
  126. W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE,
  127. W3TC_MARKER_END_BROWSERCACHE_CACHE,
  128. array(
  129. W3TC_MARKER_BEGIN_MINIFY_CORE => 0,
  130. W3TC_MARKER_BEGIN_PGCACHE_CORE => 0,
  131. W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP => 0,
  132. W3TC_MARKER_BEGIN_WORDPRESS => 0,
  133. W3TC_MARKER_END_PGCACHE_CACHE => strlen(W3TC_MARKER_END_PGCACHE_CACHE) + 1,
  134. W3TC_MARKER_END_MINIFY_CACHE => strlen(W3TC_MARKER_END_MINIFY_CACHE) + 1
  135. )
  136. );
  137. }
  138. /**
  139. * Removes cache directives
  140. *
  141. * @throws FilesystemOperationException with S/FTP form if it can't get the required filesystem credentials
  142. * @throws FileOperationException
  143. */
  144. private function rules_cache_remove($exs) {
  145. w3_remove_rules($exs,
  146. w3_get_browsercache_rules_cache_path(),
  147. W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE,
  148. W3TC_MARKER_END_BROWSERCACHE_CACHE);
  149. }
  150. /**
  151. * Returns cache rules
  152. *
  153. * @param W3_Config $config
  154. * @param bool $cdnftp
  155. * @return string
  156. */
  157. public function rules_cache_generate($config, $cdnftp = false) {
  158. switch (true) {
  159. case w3_is_apache():
  160. case w3_is_litespeed():
  161. return $this->rules_cache_generate_apache($config);
  162. case w3_is_nginx():
  163. return $this->rules_cache_generate_nginx($config, $cdnftp);
  164. }
  165. return '';
  166. }
  167. /**
  168. * Returns cache rules
  169. *
  170. * @param W3_Config $config
  171. * @return string
  172. */
  173. private function rules_cache_generate_apache($config) {
  174. $mime_types2 = $this->get_mime_types();
  175. $cssjs_types = $mime_types2['cssjs'];
  176. $html_types = $mime_types2['html'];
  177. $other_types = $mime_types2['other'];
  178. $cssjs_expires = $config->get_boolean('browsercache.cssjs.expires');
  179. $html_expires = $config->get_boolean('browsercache.html.expires');
  180. $other_expires = $config->get_boolean('browsercache.other.expires');
  181. $cssjs_lifetime = $config->get_integer('browsercache.cssjs.lifetime');
  182. $html_lifetime = $config->get_integer('browsercache.html.lifetime');
  183. $other_lifetime = $config->get_integer('browsercache.other.lifetime');
  184. $compatibility = $config->get_boolean('pgcache.compatibility');
  185. $mime_types = array();
  186. if ($cssjs_expires && $cssjs_lifetime) {
  187. $mime_types = array_merge($mime_types, $cssjs_types);
  188. }
  189. if ($html_expires && $html_lifetime) {
  190. $mime_types = array_merge($mime_types, $html_types);
  191. }
  192. if ($other_expires && $other_lifetime) {
  193. $mime_types = array_merge($mime_types, $other_types);
  194. }
  195. $rules = '';
  196. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE . "\n";
  197. if (count($mime_types)) {
  198. $rules .= "<IfModule mod_mime.c>\n";
  199. foreach ($mime_types as $ext => $mime_type) {
  200. $extensions = explode('|', $ext);
  201. $rules .= " AddType " . $mime_type;
  202. foreach ($extensions as $extension) {
  203. $rules .= " ." . $extension;
  204. }
  205. $rules .= "\n";
  206. }
  207. $rules .= "</IfModule>\n";
  208. $rules .= "<IfModule mod_expires.c>\n";
  209. $rules .= " ExpiresActive On\n";
  210. if ($cssjs_expires && $cssjs_lifetime) {
  211. foreach ($cssjs_types as $mime_type) {
  212. $rules .= " ExpiresByType " . $mime_type . " A" . $cssjs_lifetime . "\n";
  213. }
  214. }
  215. if ($html_expires && $html_lifetime) {
  216. foreach ($html_types as $mime_type) {
  217. $rules .= " ExpiresByType " . $mime_type . " A" . $html_lifetime . "\n";
  218. }
  219. }
  220. if ($other_expires && $other_lifetime) {
  221. foreach ($other_types as $mime_type) {
  222. $rules .= " ExpiresByType " . $mime_type . " A" . $other_lifetime . "\n";
  223. }
  224. }
  225. $rules .= "</IfModule>\n";
  226. }
  227. $cssjs_compression = $config->get_boolean('browsercache.cssjs.compression');
  228. $html_compression = $config->get_boolean('browsercache.html.compression');
  229. $other_compression = $config->get_boolean('browsercache.other.compression');
  230. if ($cssjs_compression || $html_compression || $other_compression) {
  231. $compression_types = array();
  232. if ($cssjs_compression) {
  233. $compression_types = array_merge($compression_types, $cssjs_types);
  234. }
  235. if ($html_compression) {
  236. $compression_types = array_merge($compression_types, $html_types);
  237. }
  238. if ($other_compression) {
  239. $compression_types = array_merge($compression_types, array(
  240. //for some reason the 'other' types are not loaded from the 'other' file
  241. 'ico' => 'image/x-icon',
  242. 'json' => 'application/json'
  243. ));
  244. }
  245. $rules .= "<IfModule mod_deflate.c>\n";
  246. if ($compatibility) {
  247. $rules .= " <IfModule mod_setenvif.c>\n";
  248. $rules .= " BrowserMatch ^Mozilla/4 gzip-only-text/html\n";
  249. $rules .= " BrowserMatch ^Mozilla/4\\.0[678] no-gzip\n";
  250. $rules .= " BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html\n";
  251. $rules .= " BrowserMatch \\bMSI[E] !no-gzip !gzip-only-text/html\n";
  252. $rules .= " </IfModule>\n";
  253. }
  254. $rules .= " <IfModule mod_headers.c>\n";
  255. $rules .= " Header append Vary User-Agent env=!dont-vary\n";
  256. $rules .= " </IfModule>\n";
  257. if (version_compare($this->_get_server_version(), '2.3.7', '>=')) {
  258. $rules .= " <IfModule mod_filter.c>\n";
  259. }
  260. $rules .= " AddOutputFilterByType DEFLATE " . implode(' ', $compression_types) . "\n";
  261. $rules .= " <IfModule mod_mime.c>\n";
  262. $rules .= " # DEFLATE by extension\n";
  263. $rules .= " AddOutputFilter DEFLATE js css htm html xml\n";
  264. $rules .= " </IfModule>\n";
  265. if (version_compare($this->_get_server_version(), '2.3.7', '>=')) {
  266. $rules .= " </IfModule>\n";
  267. }
  268. $rules .= "</IfModule>\n";
  269. }
  270. foreach ($mime_types2 as $type => $extensions)
  271. $rules .= $this->_rules_cache_generate_apache_for_type($config,
  272. $extensions, $type);
  273. $rules .= W3TC_MARKER_END_BROWSERCACHE_CACHE . "\n";
  274. return $rules;
  275. }
  276. /**
  277. * Writes cache rules
  278. *
  279. * @param W3_Config $config
  280. * @param array $mime_types
  281. * @param string $section
  282. * @return string
  283. */
  284. private function _rules_cache_generate_apache_for_type($config, $mime_types,
  285. $section) {
  286. $is_disc_enhanced = $config->get_boolean('pgcache.enabled') &&
  287. $config->get_string('pgcache.engine') == 'file_generic';
  288. $cache_control = $config->get_boolean('browsercache.' . $section . '.cache.control');
  289. $etag = $config->get_boolean('browsercache.' . $section . '.etag');
  290. $w3tc = $config->get_boolean('browsercache.' . $section . '.w3tc');
  291. $unset_setcookie = $config->get_boolean('browsercache.' . $section . '.nocookies');
  292. $set_last_modified = $config->get_boolean('browsercache.' . $section . '.last_modified');
  293. $compatibility = $config->get_boolean('pgcache.compatibility');
  294. $extensions = array_keys($mime_types);
  295. // Remove ext from filesmatch if its the same as permalink extension
  296. $pext = strtolower(pathinfo(get_option('permalink_structure'), PATHINFO_EXTENSION));
  297. if ($pext) {
  298. $extensions = $this->_remove_extension_from_list($extensions, $pext);
  299. }
  300. $extensions_lowercase = array_map('strtolower', $extensions);
  301. $extensions_uppercase = array_map('strtoupper', $extensions);
  302. $rules = '';
  303. $headers_rules = '';
  304. if ($cache_control) {
  305. $cache_policy = $config->get_string('browsercache.' . $section . '.cache.policy');
  306. switch ($cache_policy) {
  307. case 'cache':
  308. $headers_rules .= " Header set Pragma \"public\"\n";
  309. $headers_rules .= " Header set Cache-Control \"public\"\n";
  310. break;
  311. case 'cache_public_maxage':
  312. $expires = $config->get_boolean('browsercache.' . $section . '.expires');
  313. $lifetime = $config->get_integer('browsercache.' . $section . '.lifetime');
  314. $headers_rules .= " Header set Pragma \"public\"\n";
  315. if ($expires)
  316. $headers_rules .= " Header append Cache-Control \"public\"\n";
  317. else
  318. $headers_rules .= " Header set Cache-Control \"max-age=" . $lifetime . ", public\"\n";
  319. break;
  320. case 'cache_validation':
  321. $headers_rules .= " Header set Pragma \"public\"\n";
  322. $headers_rules .= " Header set Cache-Control \"public, must-revalidate, proxy-revalidate\"\n";
  323. break;
  324. case 'cache_noproxy':
  325. $headers_rules .= " Header set Pragma \"public\"\n";
  326. $headers_rules .= " Header set Cache-Control \"private, must-revalidate\"\n";
  327. break;
  328. case 'cache_maxage':
  329. $expires = $config->get_boolean('browsercache.' . $section . '.expires');
  330. $lifetime = $config->get_integer('browsercache.' . $section . '.lifetime');
  331. $headers_rules .= " Header set Pragma \"public\"\n";
  332. if ($expires)
  333. $headers_rules .= " Header append Cache-Control \"public, must-revalidate, proxy-revalidate\"\n";
  334. else
  335. $headers_rules .= " Header set Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\"\n";
  336. break;
  337. case 'no_cache':
  338. $headers_rules .= " Header set Pragma \"no-cache\"\n";
  339. $headers_rules .= " Header set Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\"\n";
  340. break;
  341. }
  342. }
  343. if ($etag) {
  344. $rules .= " FileETag MTime Size\n";
  345. } else {
  346. if ($compatibility) {
  347. $rules .= " FileETag None\n";
  348. $headers_rules .= " Header unset ETag\n";
  349. }
  350. }
  351. if ($unset_setcookie)
  352. $headers_rules .= " Header unset Set-Cookie\n";
  353. if (!$set_last_modified)
  354. $headers_rules .= " Header unset Last-Modified\n";
  355. if ($w3tc)
  356. $headers_rules .= " Header set X-Powered-By \"" . W3TC_POWERED_BY . "\"\n";
  357. if (strlen($headers_rules) > 0) {
  358. $rules .= " <IfModule mod_headers.c>\n";
  359. $rules .= $headers_rules;
  360. $rules .= " </IfModule>\n";
  361. }
  362. if (strlen($rules) > 0) {
  363. $rules = "<FilesMatch \"\\.(" . implode('|',
  364. array_merge($extensions_lowercase, $extensions_uppercase)) .
  365. ")$\">\n" . $rules;
  366. $rules .= "</FilesMatch>\n";
  367. }
  368. return $rules;
  369. }
  370. /**
  371. * Returns cache rules
  372. *
  373. * @param W3_Config $config
  374. * @param bool $cdnftp
  375. * @return string
  376. */
  377. private function rules_cache_generate_nginx($config, $cdnftp = false) {
  378. $mime_types = $this->get_mime_types();
  379. $cssjs_types = $mime_types['cssjs'];
  380. $html_types = $mime_types['html'];
  381. $other_types = $mime_types['other'];
  382. $rules = '';
  383. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE . "\n";
  384. $cssjs_compression = $config->get_boolean('browsercache.cssjs.compression');
  385. $html_compression = $config->get_boolean('browsercache.html.compression');
  386. $other_compression = $config->get_boolean('browsercache.other.compression');
  387. if ($cssjs_compression || $html_compression || $other_compression) {
  388. $compression_types = array();
  389. if ($cssjs_compression) {
  390. $compression_types = array_merge($compression_types, $cssjs_types);
  391. }
  392. if ($html_compression) {
  393. $compression_types = array_merge($compression_types, $html_types);
  394. }
  395. if ($other_compression) {
  396. $compression_types = array_merge($compression_types, array(
  397. 'ico' => 'image/x-icon'
  398. ));
  399. }
  400. unset($compression_types['html|htm']);
  401. $rules .= "gzip on;\n";
  402. $rules .= "gzip_types " . implode(' ', $compression_types) . ";\n";
  403. }
  404. foreach ($mime_types as $type => $extensions)
  405. $this->_rules_cache_generate_nginx_for_type($config, $rules,
  406. $extensions, $type);
  407. $rules .= W3TC_MARKER_END_BROWSERCACHE_CACHE . "\n";
  408. return $rules;
  409. }
  410. /**
  411. * Adds cache rules for type to &$rules
  412. *
  413. * @param W3_Config $config
  414. * @param string $rules
  415. * @param array $mime_types
  416. * @param string $section
  417. * @param boolean $write_location
  418. * @param boolean $cdnftp
  419. * @return void
  420. */
  421. private function _rules_cache_generate_nginx_for_type($config, &$rules,
  422. $mime_types, $section, $write_location = false, $cdnftp = false) {
  423. $expires = $config->get_boolean('browsercache.' . $section . '.expires');
  424. $cache_control = $config->get_boolean('browsercache.' . $section . '.cache.control');
  425. $w3tc = $config->get_boolean('browsercache.' . $section . '.w3tc');
  426. if ($expires || $cache_control || $w3tc) {
  427. $lifetime = $config->get_integer('browsercache.' . $section . '.lifetime');
  428. $extensions = array_keys($mime_types);
  429. // Remove ext from filesmatch if its the same as permalink extension
  430. $pext = strtolower(pathinfo(get_option('permalink_structure'), PATHINFO_EXTENSION));
  431. if ($pext) {
  432. $extensions = $this->_remove_extension_from_list($extensions, $pext);
  433. }
  434. $rules .= "location ~ \\.(" . implode('|', $extensions) . ")$ {\n";
  435. if ($expires) {
  436. $rules .= " expires " . $lifetime . "s;\n";
  437. }
  438. if ($cache_control) {
  439. $cache_policy = $config->get_string('browsercache.cssjs.cache.policy');
  440. switch ($cache_policy) {
  441. case 'cache':
  442. $rules .= " add_header Pragma \"public\";\n";
  443. $rules .= " add_header Cache-Control \"public\";\n";
  444. break;
  445. case 'cache_public_maxage':
  446. $rules .= " add_header Pragma \"public\";\n";
  447. $rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n";
  448. break;
  449. case 'cache_validation':
  450. $rules .= " add_header Pragma \"public\";\n";
  451. $rules .= " add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n";
  452. break;
  453. case 'cache_noproxy':
  454. $rules .= " add_header Pragma \"public\";\n";
  455. $rules .= " add_header Cache-Control \"private, must-revalidate\";\n";
  456. break;
  457. case 'cache_maxage':
  458. $rules .= " add_header Pragma \"public\";\n";
  459. $rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n";
  460. break;
  461. case 'no_cache':
  462. $rules .= " add_header Pragma \"no-cache\";\n";
  463. $rules .= " add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n";
  464. break;
  465. }
  466. }
  467. $w3_dispatcher = w3_instance('W3_Dispatcher');
  468. $rules .= $w3_dispatcher->on_browsercache_rules_generation_for_section(
  469. $config, $cdnftp, $section);
  470. if ($w3tc) {
  471. $rules .= " add_header X-Powered-By \"" . W3TC_POWERED_BY . "\";\n";
  472. }
  473. if ($write_location) {
  474. $rules .= ' try_files $uri $uri/ $uri.html /index.php?$args;' . "\n";
  475. }
  476. $rules .= "}\n";
  477. }
  478. }
  479. /**
  480. * rules_no404wp
  481. **/
  482. /**
  483. * Writes no 404 by WP rules
  484. *
  485. * @param W3_Config $config
  486. * @param SelfTestExceptions $exs
  487. * @throws FilesystemOperationException with S/FTP form
  488. * @throws FileOperationException
  489. */
  490. private function rules_no404wp_add($config, $exs) {
  491. w3_add_rules($exs, w3_get_browsercache_rules_no404wp_path(),
  492. $this->rules_no404wp_generate($config),
  493. W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP,
  494. W3TC_MARKER_END_BROWSERCACHE_NO404WP,
  495. array(
  496. W3TC_MARKER_BEGIN_WORDPRESS => 0,
  497. W3TC_MARKER_END_PGCACHE_CORE =>
  498. strlen(W3TC_MARKER_END_PGCACHE_CORE) + 1,
  499. W3TC_MARKER_END_MINIFY_CORE =>
  500. strlen(W3TC_MARKER_END_MINIFY_CORE) + 1,
  501. W3TC_MARKER_END_BROWSERCACHE_CACHE =>
  502. strlen(W3TC_MARKER_END_BROWSERCACHE_CACHE) + 1,
  503. W3TC_MARKER_END_PGCACHE_CACHE =>
  504. strlen(W3TC_MARKER_END_PGCACHE_CACHE) + 1,
  505. W3TC_MARKER_END_MINIFY_CACHE =>
  506. strlen(W3TC_MARKER_END_MINIFY_CACHE) + 1
  507. )
  508. );
  509. }
  510. /**
  511. * Removes 404 directives
  512. *
  513. * @throws FilesystemOperationException with S/FTP form
  514. * @throws FileOperationException
  515. */
  516. private function rules_no404wp_remove($exs) {
  517. w3_remove_rules($exs,
  518. w3_get_browsercache_rules_no404wp_path(),
  519. W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP,
  520. W3TC_MARKER_END_BROWSERCACHE_NO404WP
  521. );
  522. }
  523. /**
  524. * Generate rules related to prevent for media 404 error by WP
  525. *
  526. * @param W3_Config $config
  527. * @return string
  528. */
  529. private function rules_no404wp_generate($config) {
  530. switch (true) {
  531. case w3_is_apache():
  532. case w3_is_litespeed():
  533. return $this->rules_no404wp_generate_apache($config);
  534. case w3_is_nginx():
  535. return $this->rules_no404wp_generate_nginx($config);
  536. }
  537. return false;
  538. }
  539. /**
  540. * Generate rules related to prevent for media 404 error by WP
  541. *
  542. * @param W3_Config $config
  543. * @return string
  544. */
  545. private function rules_no404wp_generate_apache($config) {
  546. $a = $this->get_mime_types();
  547. $cssjs_types = $a['cssjs'];
  548. $html_types = $a['html'];
  549. $other_types = $a['other'];
  550. $extensions = array_merge(array_keys($cssjs_types),
  551. array_keys($html_types), array_keys($other_types));
  552. $permalink_structure = get_option('permalink_structure');
  553. $permalink_structure_ext = ltrim(strrchr($permalink_structure, '.'),
  554. '.');
  555. if ($permalink_structure_ext != '') {
  556. foreach ($extensions as $index => $extension) {
  557. if (strstr($extension, $permalink_structure_ext) !== false) {
  558. $extensions[$index] = preg_replace('~\|?' .
  559. w3_preg_quote($permalink_structure_ext) .
  560. '\|?~', '', $extension);
  561. }
  562. }
  563. }
  564. $exceptions = $config->get_array('browsercache.no404wp.exceptions');
  565. $rules = '';
  566. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP . "\n";
  567. $rules .= "<IfModule mod_rewrite.c>\n";
  568. $rules .= " RewriteEngine On\n";
  569. $rules .= " RewriteCond %{REQUEST_FILENAME} !-f\n";
  570. $rules .= " RewriteCond %{REQUEST_FILENAME} !-d\n";
  571. if (count($exceptions)) {
  572. $rules .= " RewriteCond %{REQUEST_URI} !(" .
  573. implode('|', $exceptions) . ")\n";
  574. }
  575. $rules .= " RewriteCond %{REQUEST_FILENAME} \\.(" .
  576. implode('|', $extensions) . ")$ [NC]\n";
  577. $rules .= " RewriteRule .* - [L]\n";
  578. $rules .= "</IfModule>\n";
  579. $rules .= W3TC_MARKER_END_BROWSERCACHE_NO404WP . "\n";
  580. return $rules;
  581. }
  582. /**
  583. * Generate rules related to prevent for media 404 error by WP
  584. *
  585. * @param W3_Config $config
  586. * @return string
  587. */
  588. private function rules_no404wp_generate_nginx($config) {
  589. $a = $this->get_mime_types();
  590. $cssjs_types = $a['cssjs'];
  591. $html_types = $a['html'];
  592. $other_types = $a['other'];
  593. $extensions = array_merge(array_keys($cssjs_types),
  594. array_keys($html_types), array_keys($other_types));
  595. $permalink_structure = get_option('permalink_structure');
  596. $permalink_structure_ext =
  597. ltrim(strrchr($permalink_structure, '.'), '.');
  598. if ($permalink_structure_ext != '') {
  599. foreach ($extensions as $index => $extension) {
  600. if (strstr($extension, $permalink_structure_ext) !== false) {
  601. $extensions[$index] = preg_replace('~\|?' .
  602. w3_preg_quote($permalink_structure_ext) . '\|?~', '',
  603. $extension);
  604. }
  605. }
  606. }
  607. $exceptions = $config->get_array('browsercache.no404wp.exceptions');
  608. $rules = '';
  609. $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP . "\n";
  610. $rules .= "if (-f \$request_filename) {\n";
  611. $rules .= " break;\n";
  612. $rules .= "}\n";
  613. $rules .= "if (-d \$request_filename) {\n";
  614. $rules .= " break;\n";
  615. $rules .= "}\n";
  616. if (count($exceptions)) {
  617. $rules .= "if (\$request_uri ~ \"(" . implode('|', $exceptions) .
  618. ")\") {\n";
  619. $rules .= " break;\n";
  620. $rules .= "}\n";
  621. }
  622. $rules .= "if (\$request_uri ~* \\.(" . implode('|', $extensions) .
  623. ")$) {\n";
  624. $rules .= " return 404;\n";
  625. $rules .= "}\n";
  626. $rules .= W3TC_MARKER_END_BROWSERCACHE_NO404WP . "\n";
  627. return $rules;
  628. }
  629. /**
  630. * Returns the apache, nginx version
  631. * @return string
  632. */
  633. private function _get_server_version() {
  634. $sig= explode('/', $_SERVER['SERVER_SOFTWARE']);
  635. $temp = isset($sig[1]) ? explode(' ', $sig[1]) : array('0');
  636. $version = $temp[0];
  637. return $version;
  638. }
  639. /**
  640. * Takes an array of extensions single per row and/or extensions delimited by |
  641. * @param $extensions
  642. * @param $ext
  643. * @return array
  644. */
  645. private function _remove_extension_from_list($extensions, $ext) {
  646. for ($i = 0; $i < sizeof($extensions); $i++) {
  647. if ($extensions[$i] == $ext) {
  648. unset($extensions[$i]);
  649. return $extensions;
  650. } elseif (strpos($extensions[$i], $ext) !== false &&
  651. strpos($extensions[$i], '|') !== false) {
  652. $exts = explode('|', $extensions[$i]);
  653. $key = array_search($ext, $exts);
  654. unset($exts[$key]);
  655. $extensions[$i] = implode('|', $exts);
  656. return $extensions;
  657. }
  658. }
  659. return $extensions;
  660. }
  661. }