/static/wp-content/plugins/w3-total-cache/inc/functions/rule.php

https://github.com/elleeott/WPOC-boilerplate · PHP · 239 lines · 121 code · 39 blank · 79 comment · 18 complexity · 7a87421ad0f801f8cccc56557efe4602 MD5 · raw file

  1. <?php
  2. /**
  3. * Check if WP permalink directives exists
  4. *
  5. * @return boolean
  6. */
  7. function w3_is_permalink_rules() {
  8. if ((w3_is_apache() || w3_is_litespeed()) && !w3_is_network()) {
  9. $path = w3_get_home_root() . '/.htaccess';
  10. return (($data = @file_get_contents($path)) && strstr($data, W3TC_MARKER_BEGIN_WORDPRESS) !== false);
  11. }
  12. return true;
  13. }
  14. /**
  15. * Returns nginx rules path
  16. *
  17. * @return string
  18. */
  19. function w3_get_nginx_rules_path() {
  20. $config = & w3_instance('W3_Config');
  21. $path = $config->get_string('config.path');
  22. if (!$path) {
  23. $path = w3_get_document_root() . '/nginx.conf';
  24. }
  25. return $path;
  26. }
  27. /**
  28. * Returns path of pagecache core rules file
  29. *
  30. * @return string
  31. */
  32. function w3_get_pgcache_rules_core_path() {
  33. switch (true) {
  34. case w3_is_apache():
  35. case w3_is_litespeed():
  36. return w3_get_home_root() . '/.htaccess';
  37. case w3_is_nginx():
  38. return w3_get_nginx_rules_path();
  39. }
  40. return false;
  41. }
  42. /**
  43. * Returns path of pgcache cache rules file
  44. *
  45. * @return string
  46. */
  47. function w3_get_pgcache_rules_cache_path() {
  48. switch (true) {
  49. case w3_is_apache():
  50. case w3_is_litespeed():
  51. return W3TC_CACHE_FILE_PGCACHE_DIR . '/.htaccess';
  52. case w3_is_nginx():
  53. return w3_get_nginx_rules_path();
  54. }
  55. return false;
  56. }
  57. /**
  58. * Returns path of browsercache cache rules file
  59. *
  60. * @return string
  61. */
  62. function w3_get_browsercache_rules_cache_path() {
  63. switch (true) {
  64. case w3_is_apache():
  65. case w3_is_litespeed():
  66. return w3_get_home_root() . '/.htaccess';
  67. case w3_is_nginx():
  68. return w3_get_nginx_rules_path();
  69. }
  70. return false;
  71. }
  72. /**
  73. * Returns path of browsercache no404wp rules file
  74. *
  75. * @return string
  76. */
  77. function w3_get_browsercache_rules_no404wp_path() {
  78. switch (true) {
  79. case w3_is_apache():
  80. case w3_is_litespeed():
  81. return w3_get_home_root() . '/.htaccess';
  82. case w3_is_nginx():
  83. return w3_get_nginx_rules_path();
  84. }
  85. return false;
  86. }
  87. /**
  88. * Returns path of minify rules file
  89. *
  90. * @return string
  91. */
  92. function w3_get_minify_rules_core_path() {
  93. switch (true) {
  94. case w3_is_apache():
  95. case w3_is_litespeed():
  96. return W3TC_CACHE_FILE_MINIFY_DIR . '/.htaccess';
  97. case w3_is_nginx():
  98. return w3_get_nginx_rules_path();
  99. }
  100. return false;
  101. }
  102. /**
  103. * Returns path of minify rules file
  104. *
  105. * @return string
  106. */
  107. function w3_get_minify_rules_cache_path() {
  108. switch (true) {
  109. case w3_is_apache():
  110. case w3_is_litespeed():
  111. return W3TC_CACHE_FILE_MINIFY_DIR . '/.htaccess';
  112. case w3_is_nginx():
  113. return w3_get_nginx_rules_path();
  114. }
  115. return false;
  116. }
  117. /**
  118. * Returns path of minify rules file
  119. *
  120. * @return string
  121. */
  122. function w3_get_cdn_rules_path() {
  123. switch (true) {
  124. case w3_is_apache():
  125. case w3_is_litespeed():
  126. return '.htaccess';
  127. case w3_is_nginx():
  128. return 'nginx.conf';
  129. }
  130. return false;
  131. }
  132. /**
  133. * Returns true if we can modify rules
  134. *
  135. * @param string $path
  136. * @return boolean
  137. */
  138. function w3_can_modify_rules($path) {
  139. if (w3_is_network()) {
  140. if (w3_is_apache() || w3_is_litespeed()) {
  141. switch ($path) {
  142. case w3_get_pgcache_rules_cache_path():
  143. case w3_get_minify_rules_core_path():
  144. case w3_get_minify_rules_cache_path():
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * Trim rules
  154. *
  155. * @param string $rules
  156. * @return string
  157. */
  158. function w3_trim_rules($rules) {
  159. $rules = trim($rules);
  160. if ($rules != '') {
  161. $rules .= "\n";
  162. }
  163. return $rules;
  164. }
  165. /**
  166. * Cleanup rewrite rules
  167. *
  168. * @param string $rules
  169. * @return string
  170. */
  171. function w3_clean_rules($rules) {
  172. $rules = preg_replace('~[\r\n]+~', "\n", $rules);
  173. $rules = preg_replace('~^\s+~m', '', $rules);
  174. $rules = w3_trim_rules($rules);
  175. return $rules;
  176. }
  177. /**
  178. * Erases text from start to end
  179. *
  180. * @param string $rules
  181. * @param string $start
  182. * @param string $end
  183. * @return string
  184. */
  185. function w3_erase_rules($rules, $start, $end) {
  186. $rules = preg_replace('~' . w3_preg_quote($start) . "\n.*?" . w3_preg_quote($end) . "\n*~s", '', $rules);
  187. $rules = w3_trim_rules($rules);
  188. return $rules;
  189. }
  190. /**
  191. * Check if rules exist
  192. *
  193. * @param string $rules
  194. * @param string $start
  195. * @param string $end
  196. * @return int
  197. */
  198. function w3_has_rules($rules, $start, $end) {
  199. return preg_match('~' . w3_preg_quote($start) . "\n.*?" . w3_preg_quote($end) . "\n*~s", $rules);
  200. }