| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class Powered_Cache_Config { |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Return an instance of the current class |
| 12 |
* |
| 13 |
* @since 1.0 |
| 14 |
* @since 1.1 initialize WP_Filesystem_Direct |
| 15 |
* @return Powered_Cache_Config |
| 16 |
*/ |
| 17 |
public static function factory() { |
| 18 |
static $instance = false; |
| 19 |
|
| 20 |
if ( ! $instance ) { |
| 21 |
$instance = new self(); |
| 22 |
global $powered_cache_fs; |
| 23 |
|
| 24 |
if ( ! $powered_cache_fs ) { |
| 25 |
include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
| 26 |
include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; |
| 27 |
|
| 28 |
$powered_cache_fs = new WP_Filesystem_Direct( new StdClass() ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
return $instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* placeholder |
| 37 |
* |
| 38 |
* @since 1.0 |
| 39 |
*/ |
| 40 |
public function __construct() { } |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Default options |
| 45 |
* |
| 46 |
* @since 1.0 |
| 47 |
* @return mixed|void |
| 48 |
*/ |
| 49 |
public function default_settings() { |
| 50 |
$settings = array( |
| 51 |
// basic options |
| 52 |
'enable_page_caching' => false, |
| 53 |
'object_cache' => 'off', |
| 54 |
'cache_mobile' => true, |
| 55 |
'cache_mobile_separate_file' => false, |
| 56 |
'loggedin_user_cache' => false, |
| 57 |
'ssl_cache' => false, |
| 58 |
'gzip_compression' => false, |
| 59 |
'cache_timeout' => 1440, |
| 60 |
'cache_location' => powered_cache_get_cache_dir(), |
| 61 |
// advanced options |
| 62 |
'remove_query_string' => false, |
| 63 |
'rejected_user_agents' => '', |
| 64 |
'rejected_cookies' => '', |
| 65 |
'rejected_uri' => '', |
| 66 |
'accepted_query_strings' => '', |
| 67 |
'purge_additional_pages' => '', |
| 68 |
// cdn |
| 69 |
'cdn_status' => false, |
| 70 |
'cdn_hostname' => array(), |
| 71 |
'cdn_zone' => array(), |
| 72 |
'cdn_rejected_files' => '', |
| 73 |
// skip extensions, we don't need default for them |
| 74 |
// misc |
| 75 |
'show_cache_message' => true, |
| 76 |
); |
| 77 |
|
| 78 |
|
| 79 |
return apply_filters( 'powered_cache_default_settings', $settings ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Generates advanced-cache.php |
| 84 |
* |
| 85 |
* @since 1.0 |
| 86 |
* @since 1.1 is_multisite control added |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function generate_advanced_cache_file() { |
| 90 |
global $powered_cache_fs; |
| 91 |
|
| 92 |
$file = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php'; |
| 93 |
|
| 94 |
|
| 95 |
$file_string = ''; |
| 96 |
|
| 97 |
/** |
| 98 |
* multisite setups should always have `advanced-cache.php` file |
| 99 |
*/ |
| 100 |
if ( true === powered_cache_get_option( 'enable_page_caching' ) || is_multisite() ) { |
| 101 |
$file_string = $this->advanced_cache_file_content(); |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! $powered_cache_fs->put_contents( $file, $file_string, FS_CHMOD_FILE ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Setup object-cache.php |
| 114 |
* |
| 115 |
* @since 1.0 |
| 116 |
* @param string $backend |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
public function setup_object_cache( $backend = 'off' ) { |
| 121 |
global $powered_cache_fs; |
| 122 |
|
| 123 |
$file = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php'; |
| 124 |
|
| 125 |
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
if ( 'off' === $backend ) { |
| 130 |
if ( $powered_cache_fs->exists( $file ) ) { |
| 131 |
$powered_cache_fs->delete( $file ); // remove object cache file |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
$file_string = $this->object_cache_file_content( $backend ); |
| 138 |
|
| 139 |
if ( ! $powered_cache_fs->put_contents( $file, $file_string, FS_CHMOD_FILE ) ) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Generate advanced-cache.php and define WP_CACHE |
| 149 |
* |
| 150 |
* @since 1.0 |
| 151 |
* |
| 152 |
* @param $status |
| 153 |
* |
| 154 |
* @return bool |
| 155 |
*/ |
| 156 |
public function setup_page_cache( $status ) { |
| 157 |
|
| 158 |
/** |
| 159 |
* Forcing multisite settings always true |
| 160 |
*/ |
| 161 |
if ( is_multisite() ) { |
| 162 |
$status = true; |
| 163 |
} |
| 164 |
|
| 165 |
Powered_Cache_Config::factory()->generate_advanced_cache_file(); |
| 166 |
Powered_Cache_Config::factory()->define_wp_cache( $status ); |
| 167 |
Powered_Cache_Config::factory()->configure_htaccess( $status ); |
| 168 |
|
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* object-cache.php contents |
| 175 |
* |
| 176 |
* @since 1.0 |
| 177 |
* @since 1.1 supports `POWERED_CACHE_OBJECT_CACHE_DROPIN` |
| 178 |
* @param $backend |
| 179 |
* @see Powered_Cache_Admin_Helper::object_cache_dropins |
| 180 |
* |
| 181 |
* @return mixed|void |
| 182 |
*/ |
| 183 |
public function object_cache_file_content( $backend ) { |
| 184 |
$string = '<?php ' . "\n"; |
| 185 |
$string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL; |
| 186 |
$string .= "define( 'POWERED_OBJECT_CACHE', true );" . PHP_EOL; |
| 187 |
$string .= "if ( ! @file_exists( WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'] . '.php' ) ) { return; }" . PHP_EOL; |
| 188 |
|
| 189 |
$object_caches = Powered_Cache_Admin_Helper::object_cache_dropins(); |
| 190 |
|
| 191 |
$string .= "\$GLOBALS['powered_cache_options'] = include( WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'] . '.php' );" . "\n\n"; |
| 192 |
$string .= 'if ( defined( \'POWERED_CACHE_OBJECT_CACHE_DROPIN\') && @file_exists( POWERED_CACHE_OBJECT_CACHE_DROPIN ) ) {' . PHP_EOL; |
| 193 |
$string .= "\t" . 'include( POWERED_CACHE_OBJECT_CACHE_DROPIN );' . PHP_EOL; |
| 194 |
$string .= '} elseif ( @file_exists( \'' . $object_caches[ $backend ] . '\' ) ) {' . PHP_EOL; |
| 195 |
$string .= "\t" . 'include( \'' . $object_caches[ $backend ] . '\' );' . PHP_EOL; |
| 196 |
$string .= '} else {' . PHP_EOL; |
| 197 |
$string .= "\t" . 'define( \'POWERED_OBJECT_CACHE_HAS_PROBLEM\', true );' . PHP_EOL; |
| 198 |
$string .= '}'; |
| 199 |
|
| 200 |
return apply_filters( 'powered_cache_object_cache_file_content', $string ); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* Prepare advanced-cache.php contents |
| 206 |
* |
| 207 |
* @since 1.0 |
| 208 |
* @since 1.1 supports `POWERED_CACHE_ADVANCED_CACHE_DROPIN` |
| 209 |
* @return mixed|void |
| 210 |
*/ |
| 211 |
public function advanced_cache_file_content(){ |
| 212 |
|
| 213 |
$string = '<?php ' . PHP_EOL; |
| 214 |
$string .= "defined( 'ABSPATH' ) || exit;" . PHP_EOL; |
| 215 |
$string .= "define( 'POWERED_CACHE_PAGE_CACHING', true );" . PHP_EOL; |
| 216 |
|
| 217 |
$string .= "\$config_file = WP_CONTENT_DIR . '/pc-config/config-' . \$_SERVER['HTTP_HOST'];" . PHP_EOL . PHP_EOL; |
| 218 |
|
| 219 |
$string .= "if ( is_multisite() && ( defined( 'SUBDOMAIN_INSTALL' ) && false === SUBDOMAIN_INSTALL ) ) {" . PHP_EOL; |
| 220 |
$string .= "\t" . "\$request_uri = explode( '/', ltrim( \$_SERVER['REQUEST_URI'], '/' ) );" . PHP_EOL; |
| 221 |
$string .= "\t" . "\$sub_site_name = \$request_uri[0];" . PHP_EOL; |
| 222 |
$string .= "\t" . "\$config_file .= '-'.\$sub_site_name;" . PHP_EOL; |
| 223 |
$string .= "}" . PHP_EOL; |
| 224 |
$string .= "\$config_file .= '.php';" . PHP_EOL; |
| 225 |
|
| 226 |
|
| 227 |
$string .= "if ( ! @file_exists( \$config_file ) ) { return; }" . PHP_EOL; |
| 228 |
// get config file |
| 229 |
$string .= "\$GLOBALS['powered_cache_options'] = include( \$config_file );" . PHP_EOL . PHP_EOL; |
| 230 |
// mobile cache varibales |
| 231 |
$string .= '$powered_cache_mobile_browsers = ' . var_export( powered_cache_mobile_browsers(), true ) . ";" . PHP_EOL; |
| 232 |
$string .= '$powered_cache_mobile_prefixes = ' . var_export( powered_cache_mobile_prefixes(), true ) . ";" . PHP_EOL; |
| 233 |
|
| 234 |
$string .= 'if ( defined( \'POWERED_CACHE_ADVANCED_CACHE_DROPIN\') && @file_exists( POWERED_CACHE_ADVANCED_CACHE_DROPIN ) ) {' . PHP_EOL; |
| 235 |
$string .= "\t" . 'include( POWERED_CACHE_ADVANCED_CACHE_DROPIN );' . PHP_EOL; |
| 236 |
$string .= '} elseif ( @file_exists( \'' . POWERED_CACHE_DROPIN_DIR . 'page-cache.php' . '\' ) ) {' . PHP_EOL; |
| 237 |
$string .= "\t" . 'include( \'' . POWERED_CACHE_DROPIN_DIR . 'page-cache.php' . '\' );' . PHP_EOL; |
| 238 |
$string .= '} else {' . PHP_EOL; |
| 239 |
$string .= "\t" . 'define( \'POWERED_CACHE_PAGE_CACHING_HAS_PROBLEM\', true );' . PHP_EOL; |
| 240 |
$string .= '}'; |
| 241 |
|
| 242 |
|
| 243 |
return apply_filters( 'powered_cache_advanced_cache_file_content', $string ); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Define WP_CACHE constant |
| 248 |
* |
| 249 |
* @since 1.0 |
| 250 |
* @param $status |
| 251 |
* |
| 252 |
* @return bool |
| 253 |
*/ |
| 254 |
public function define_wp_cache( $status ) { |
| 255 |
global $powered_cache_fs; |
| 256 |
$config_path = $this->find_wp_config_file(); |
| 257 |
|
| 258 |
if ( ! $config_path ) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
if ( defined( 'WP_CACHE' ) && WP_CACHE === $status ) { |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
$config_file_string = $powered_cache_fs->get_contents( $config_path ); |
| 268 |
|
| 269 |
// Config file is empty. Maybe couldn't read it? |
| 270 |
if ( empty( $config_file_string ) ) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
$config_file = preg_split( "#(\n|\r)#", $config_file_string ); |
| 275 |
$line_key = false; |
| 276 |
|
| 277 |
foreach ( $config_file as $key => $line ) { |
| 278 |
if ( ! preg_match( '/^\s*define\(\s*(\'|")([A-Z_]+)(\'|")(.*)/', $line, $match ) ) { |
| 279 |
continue; |
| 280 |
} |
| 281 |
|
| 282 |
if ( $match[2] == 'WP_CACHE' ) { |
| 283 |
$line_key = $key; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if ( $line_key !== false ) { |
| 288 |
unset( $config_file[ $line_key ] ); |
| 289 |
} |
| 290 |
|
| 291 |
$status_string = ( $status ) ? 'true' : 'false'; |
| 292 |
|
| 293 |
array_shift( $config_file ); |
| 294 |
array_unshift( $config_file, '<?php', "define( 'WP_CACHE', $status_string ); // Powered Cache" ); |
| 295 |
|
| 296 |
foreach ( $config_file as $key => $line ) { |
| 297 |
if ( '' === $line ) { |
| 298 |
unset( $config_file[$key] ); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
if ( ! $powered_cache_fs->put_contents( $config_path, implode( "\n\r", $config_file ), FS_CHMOD_FILE ) ) { |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
return true; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* seeking wp-config file |
| 312 |
* |
| 313 |
* @since 1.0 |
| 314 |
* |
| 315 |
* @return bool|string |
| 316 |
*/ |
| 317 |
public function find_wp_config_file() { |
| 318 |
global $powered_cache_fs; |
| 319 |
|
| 320 |
$file = '/wp-config.php'; |
| 321 |
|
| 322 |
for ( $i = 1; $i <= 3; $i ++ ) { |
| 323 |
if ( $i > 1 ) { |
| 324 |
$file = '/..' . $file; |
| 325 |
} |
| 326 |
|
| 327 |
if ( $powered_cache_fs->exists( untrailingslashit( ABSPATH ) . $file ) ) { |
| 328 |
$config_path = untrailingslashit( ABSPATH ) . $file; |
| 329 |
break; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
if ( ! isset( $config_path ) ) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
return $config_path; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Save settings to file |
| 342 |
* |
| 343 |
* @since 1.0 |
| 344 |
* @param $configuration |
| 345 |
* |
| 346 |
* @return bool |
| 347 |
*/ |
| 348 |
public function save_to_file( $configuration ) { |
| 349 |
global $powered_cache_fs; |
| 350 |
|
| 351 |
$config_dir = WP_CONTENT_DIR . '/pc-config'; |
| 352 |
|
| 353 |
$site_url_parts = parse_url( site_url() ); |
| 354 |
$config_file_name = $site_url_parts['host']; |
| 355 |
|
| 356 |
if ( is_multisite() && ( defined( 'SUBDOMAIN_INSTALL' ) && false === SUBDOMAIN_INSTALL ) ) { |
| 357 |
if ( is_main_site( get_current_blog_id() ) ) { |
| 358 |
$config_file_name .= '-blog'; |
| 359 |
} else { |
| 360 |
$subdir_name = ltrim( parse_url( get_site_url( get_current_blog_id() ), PHP_URL_PATH ), '/' ); |
| 361 |
$config_file_name .= '-' . $subdir_name; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
$config_file = $config_dir . '/config-' . $config_file_name . '.php'; |
| 367 |
|
| 368 |
$configuration['cache_location'] = powered_cache_get_cache_dir(); |
| 369 |
|
| 370 |
$powered_cache_fs->mkdir( $config_dir ); |
| 371 |
|
| 372 |
$config_file_string = '<?php ' . "\n\r" . "defined( 'ABSPATH' ) || exit;" . "\n\r" . 'return ' . var_export( $configuration, true ) . '; ' . "\n\r"; |
| 373 |
|
| 374 |
if ( ! $powered_cache_fs->put_contents( $config_file, $config_file_string, FS_CHMOD_FILE ) ) { |
| 375 |
return false; |
| 376 |
} |
| 377 |
|
| 378 |
return true; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Create .htaccess file based on current setting preferences |
| 383 |
* |
| 384 |
* @since 1.0 |
| 385 |
* @return bool |
| 386 |
*/ |
| 387 |
public function configure_htaccess( $enable = true ) { |
| 388 |
global $powered_cache_fs; |
| 389 |
$htaccess_file = get_home_path() . '.htaccess'; |
| 390 |
|
| 391 |
if ( $powered_cache_fs->is_writable( $htaccess_file ) ) { |
| 392 |
$contents = $powered_cache_fs->get_contents( $htaccess_file ); |
| 393 |
|
| 394 |
//clean up |
| 395 |
$contents = preg_replace( '/# BEGIN POWERED CACHE(.*)# END POWERED CACHE/is', '', $contents ); |
| 396 |
|
| 397 |
if ( false === $enable ) { |
| 398 |
return $powered_cache_fs->put_contents( $htaccess_file, $contents, FS_CHMOD_FILE ); |
| 399 |
} |
| 400 |
|
| 401 |
$rules = $this->htaccess_rules(); |
| 402 |
$contents = $rules . $contents; |
| 403 |
|
| 404 |
// Update the .htacces file |
| 405 |
if ( ! $powered_cache_fs->put_contents( $htaccess_file, $contents, FS_CHMOD_FILE ) ) { |
| 406 |
return false; |
| 407 |
} |
| 408 |
|
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
/** |
| 417 |
* Prepares .htaccess rules for the caching |
| 418 |
* |
| 419 |
* @since 1.1 |
| 420 |
* @return string $rules |
| 421 |
*/ |
| 422 |
public function htaccess_rules() { |
| 423 |
$rules = ''; |
| 424 |
|
| 425 |
/** |
| 426 |
* @since 1.1 $contents parameter removed |
| 427 |
*/ |
| 428 |
$rules .= apply_filters( 'powered_cache_pre_htaccess', '' ); |
| 429 |
|
| 430 |
$rules .= '# BEGIN POWERED CACHE' . PHP_EOL; |
| 431 |
|
| 432 |
// todo set option here. |
| 433 |
if ( apply_filters( 'powered_cache_browser_cache', true ) ) { |
| 434 |
$wp_mime_types = wp_get_mime_types(); |
| 435 |
$mime_types = array_flip( $wp_mime_types ); |
| 436 |
//mimes |
| 437 |
$rules .= '<IfModule mod_mime.c>' . PHP_EOL; |
| 438 |
foreach ( $mime_types as $mime_type => $ext ) { |
| 439 |
$ext_str = '.' . str_replace( '|', ' .', $ext ); |
| 440 |
$rules .= " AddType " . $mime_type . " " . $ext_str . PHP_EOL; |
| 441 |
} |
| 442 |
$rules .= "</IfModule>" . PHP_EOL; |
| 443 |
|
| 444 |
// set expire time |
| 445 |
|
| 446 |
$rules .= '<IfModule mod_expires.c>' . PHP_EOL; |
| 447 |
$rules .= " ExpiresActive On" . PHP_EOL; |
| 448 |
$rules .= ' ExpiresByType text/html "access plus 0 seconds"' . PHP_EOL; |
| 449 |
$rules .= ' ExpiresByType text/richtext "access plus 0 seconds"' . PHP_EOL; |
| 450 |
$rules .= ' ExpiresByType image/svg+xml "access plus 0 seconds"' . PHP_EOL; |
| 451 |
$rules .= ' ExpiresByType text/plain "access plus 0 seconds"' . PHP_EOL; |
| 452 |
$rules .= ' ExpiresByType text/xsd "access plus 0 seconds"' . PHP_EOL; |
| 453 |
$rules .= ' ExpiresByType text/xsl "access plus 0 seconds"' . PHP_EOL; |
| 454 |
$rules .= ' ExpiresByType text/xml "access plus 0 seconds"' . PHP_EOL; |
| 455 |
$rules .= ' ExpiresByType text/cache-manifest "access plus 0 seconds"' . PHP_EOL; |
| 456 |
|
| 457 |
foreach ( $mime_types as $mime_type => $ext ) { |
| 458 |
|
| 459 |
if ( in_array( $mime_type, array( 'text/html', 'text/richtext', 'image/svg+xml', 'text/plain', 'text/xsd', 'text/xsl', 'text/xml', 'text/cache-manifest' ) ) ) { |
| 460 |
continue; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Apache allow both format like A2592000 => "access plus 1 month" |
| 465 |
* A => access, M => Modified |
| 466 |
* |
| 467 |
* @see http://httpd.apache.org/docs/current/mod/mod_expires.html |
| 468 |
*/ |
| 469 |
if ( in_array( $mime_type, array( 'text/css', 'application/javascript' ) ) ) { |
| 470 |
$expiry_time = apply_filters( 'powered_cache_browser_cache_assets_lifespan', 'access plus 1 year' ); |
| 471 |
} else { |
| 472 |
$expiry_time = apply_filters( 'powered_cache_browser_cache_default_lifespan', 'access plus 1 month' ); |
| 473 |
} |
| 474 |
$rules .= ' ExpiresByType ' . $mime_type . ' "' . $expiry_time . '"' . PHP_EOL; |
| 475 |
} |
| 476 |
$rules .= "</IfModule>" . PHP_EOL; |
| 477 |
|
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
// gzip |
| 482 |
$rules .= '<IfModule mod_deflate.c>' . PHP_EOL; |
| 483 |
$rules .= ' <IfModule mod_headers.c>' . PHP_EOL; |
| 484 |
$rules .= ' Header append Vary User-Agent env=!dont-vary' . PHP_EOL; |
| 485 |
$rules .= ' </IfModule>' . PHP_EOL; |
| 486 |
$rules .= ' AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon application/json application/vnd.ms-access application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.presentation application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint application/x-shockwave-flash image/tiff application/x-font-ttf application/vnd.ms-opentype audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel' . PHP_EOL; |
| 487 |
$rules .= ' <IfModule mod_mime.c>' . PHP_EOL; |
| 488 |
$rules .= ' AddOutputFilter DEFLATE js css htm html xml' . PHP_EOL; |
| 489 |
$rules .= ' </IfModule>' . PHP_EOL; |
| 490 |
$rules .= '</IfModule>' . PHP_EOL; |
| 491 |
|
| 492 |
|
| 493 |
// remove etag |
| 494 |
$rules .= '<IfModule mod_headers.c>' . PHP_EOL; |
| 495 |
$rules .= 'Header unset ETag' . PHP_EOL; |
| 496 |
$rules .= '</IfModule>' . PHP_EOL . PHP_EOL; |
| 497 |
|
| 498 |
|
| 499 |
// rewrite |
| 500 |
|
| 501 |
$env_powered_cache_ua = ''; |
| 502 |
$env_powered_cache_ssl = ''; |
| 503 |
$env_powered_cache_enc = ''; |
| 504 |
|
| 505 |
|
| 506 |
$rewrite_base = network_home_url( '', 'relative' ); |
| 507 |
if ( empty( $rewrite_base ) ) { |
| 508 |
$rewrite_base = '/'; |
| 509 |
} |
| 510 |
|
| 511 |
$rules .= '<IfModule mod_rewrite.c>' . PHP_EOL; |
| 512 |
$rules .= ' RewriteEngine On' . PHP_EOL; |
| 513 |
$rules .= ' RewriteBase ' . $rewrite_base . PHP_EOL; |
| 514 |
|
| 515 |
|
| 516 |
if ( true === powered_cache_get_option( 'cache_mobile' ) && true === powered_cache_get_option( 'cache_mobile_separate_file' ) ) { |
| 517 |
$mobile_browsers = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_browsers() ) ), ' ' ); |
| 518 |
$mobile_prefixes = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_prefixes() ) ), ' ' ); |
| 519 |
// mobile env set |
| 520 |
$rules .= " RewriteCond %{HTTP_USER_AGENT} (" . $mobile_browsers . ") [NC]" . PHP_EOL; |
| 521 |
$rules .= " RewriteRule .* - [E=PC_UA:-mobile]" . PHP_EOL; |
| 522 |
$rules .= " RewriteCond %{HTTP_USER_AGENT} ^(" . $mobile_prefixes . ") [NC]" . PHP_EOL; |
| 523 |
$rules .= " RewriteRule .* - [E=PC_UA:-mobile]" . PHP_EOL; |
| 524 |
$env_powered_cache_ua = '%{ENV:PC_UA}'; |
| 525 |
} |
| 526 |
|
| 527 |
if ( true === powered_cache_get_option( 'ssl_cache' ) ) { |
| 528 |
$rules .= " RewriteCond %{HTTPS} =on" . PHP_EOL; |
| 529 |
$rules .= " RewriteRule .* - [E=PC_SSL:-https]" . PHP_EOL; |
| 530 |
$rules .= " RewriteCond %{SERVER_PORT} =443" . PHP_EOL; |
| 531 |
$rules .= " RewriteRule .* - [E=PC_SSL:-https]" . PHP_EOL; |
| 532 |
$env_powered_cache_ssl = '%{ENV:PC_SSL}'; |
| 533 |
} |
| 534 |
|
| 535 |
if ( true === powered_cache_get_option( 'gzip_compression' ) ) { |
| 536 |
$rules .= " RewriteCond %{HTTP:Accept-Encoding} gzip" . PHP_EOL; |
| 537 |
$rules .= " RewriteRule .* - [E=PC_ENC:_gzip]" . PHP_EOL; |
| 538 |
$env_powered_cache_enc = '%{ENV:PC_ENC}'; |
| 539 |
} |
| 540 |
|
| 541 |
$rules .= " RewriteCond %{REQUEST_METHOD} !=POST" . PHP_EOL; |
| 542 |
$rules .= ' RewriteCond %{QUERY_STRING} =""' . PHP_EOL; |
| 543 |
|
| 544 |
if ( substr( get_option( 'permalink_structure' ), - 1 ) == '/' ) { |
| 545 |
$rules .= ' RewriteCond %{REQUEST_URI} \/$' . PHP_EOL; |
| 546 |
} |
| 547 |
|
| 548 |
// Get root base |
| 549 |
$site_root = parse_url( site_url() ); |
| 550 |
$site_root = isset( $site_root['path'] ) ? trailingslashit( $site_root['path'] ) : ''; |
| 551 |
|
| 552 |
|
| 553 |
// reject user agent |
| 554 |
if ( false !== powered_cache_get_option( 'rejected_user_agents' ) ) { |
| 555 |
$rejected_user_agents = preg_split( '#(\r\n|\n|\r)#', powered_cache_get_option( 'rejected_user_agents' ) ); |
| 556 |
if ( ! empty( $rejected_user_agents ) ) { |
| 557 |
$rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(' . implode( '|', $rejected_user_agents ) . ').* [NC]' . PHP_EOL; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
// ignore cookies |
| 563 |
if ( false !== powered_cache_get_option( 'rejected_uri' ) ) { |
| 564 |
$cookies = preg_split( '#(\n|\r)#', powered_cache_get_option( 'rejected_uri' ) ); |
| 565 |
} |
| 566 |
$wp_cookies = array( 'wordpressuser_', 'wordpresspass_', 'wordpress_sec_', 'wordpress_logged_in_' ); |
| 567 |
if ( ! empty( $cookies ) ) { |
| 568 |
$wp_cookies = array_merge( $cookies, $wp_cookies ); |
| 569 |
} |
| 570 |
$rules .= ' RewriteCond %{HTTP:Cookie} !(' . implode( '|', $wp_cookies ) . ') [NC]' . PHP_EOL; |
| 571 |
|
| 572 |
// dont cache fbexternal |
| 573 |
$rules .= ' RewriteCond %{HTTP_USER_AGENT} !^(facebookexternalhit).* [NC]' . PHP_EOL; |
| 574 |
|
| 575 |
|
| 576 |
$cache_location = powered_cache_get_cache_dir(); |
| 577 |
$cache_location = untrailingslashit( $cache_location ) . '/powered-cache/'; |
| 578 |
if ( strpos( ABSPATH, $cache_location ) === false ) { |
| 579 |
// clean doc root |
| 580 |
$cache_path = str_replace( $_SERVER['DOCUMENT_ROOT'], '', $cache_location ); |
| 581 |
} else { |
| 582 |
$cache_path = $site_root . str_replace( ABSPATH, '', $cache_location ); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
if ( apply_filters( 'powered_cache_maybe_1and1_hosting', ( 0 === strpos( $_SERVER['DOCUMENT_ROOT'], '/kunden/homepage/' ) ) ) ) { |
| 587 |
$rules .= ' RewriteCond "' . str_replace( '/kunden/homepage/', '/', $cache_location ) . '%{HTTP_HOST}' . '%{REQUEST_URI}/index' . $env_powered_cache_ssl . $env_powered_cache_ua . '.html' . $env_powered_cache_enc . '" -f' . PHP_EOL; |
| 588 |
} else { |
| 589 |
$rules .= ' RewriteCond "%{DOCUMENT_ROOT}/' . ltrim( $cache_path, '/' ) . '%{HTTP_HOST}' . '%{REQUEST_URI}/index' . $env_powered_cache_ssl . $env_powered_cache_ua . '.html' . $env_powered_cache_enc . '" -f' . PHP_EOL; |
| 590 |
} |
| 591 |
$rules .= ' RewriteRule .* "' . $cache_path . '%{HTTP_HOST}' . '%{REQUEST_URI}/index' . $env_powered_cache_ssl . $env_powered_cache_ua . '.html' . $env_powered_cache_enc . '" [L]' . PHP_EOL; |
| 592 |
$rules .= '</IfModule>' . PHP_EOL; |
| 593 |
$rules .= '# END POWERED CACHE' . PHP_EOL; |
| 594 |
|
| 595 |
return $rules; |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
/** |
| 600 |
* Prepares nginx configuration |
| 601 |
* |
| 602 |
* @since 1.1 |
| 603 |
* @return string $contents nginx rules |
| 604 |
*/ |
| 605 |
public function nginx_rules() { |
| 606 |
$contents = ''; |
| 607 |
$contents .= '##### POWERED CACHE CONF #####' . PHP_EOL; |
| 608 |
$contents .= 'set $cache_uri $request_uri;' . PHP_EOL; |
| 609 |
$contents .= 'set $pc_ssl "";' . PHP_EOL; |
| 610 |
$contents .= 'set $pc_enc "";' . PHP_EOL; |
| 611 |
$contents .= 'set $pc_ua "";' . PHP_EOL . PHP_EOL; |
| 612 |
|
| 613 |
// post |
| 614 |
$contents .= '# POST requests and urls with a query string should always go to PHP' . PHP_EOL; |
| 615 |
$contents .= 'if ($request_method = POST) {' . PHP_EOL; |
| 616 |
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL; |
| 617 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 618 |
|
| 619 |
// query string |
| 620 |
$contents .= 'if ($query_string != "") {' . PHP_EOL; |
| 621 |
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL; |
| 622 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 623 |
|
| 624 |
// https |
| 625 |
$contents .= '# HTTPS' . PHP_EOL; |
| 626 |
$contents .= 'if ($https = "on") {' . PHP_EOL; |
| 627 |
$contents .= ' set $pc_ssl "-https";' . PHP_EOL; |
| 628 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 629 |
|
| 630 |
$contents .= '# Don\'t cache uris containing the following segments' . PHP_EOL; |
| 631 |
$contents .= 'if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {' . PHP_EOL; |
| 632 |
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL; |
| 633 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 634 |
|
| 635 |
$contents .= '# Don\'t use the cache for logged in users or recent commenters' . PHP_EOL; |
| 636 |
$contents .= 'if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|powered_cache_commented_posts") {' . PHP_EOL; |
| 637 |
$contents .= ' set $cache_uri \'null cache\';' . PHP_EOL; |
| 638 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 639 |
|
| 640 |
$contents .= 'if ($http_x_wap_profile) {' . PHP_EOL; |
| 641 |
$contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL; |
| 642 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 643 |
|
| 644 |
if ( true === powered_cache_get_option( 'cache_mobile' ) && true === powered_cache_get_option( 'cache_mobile_separate_file' ) ) { |
| 645 |
$mobile_browsers = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_browsers() ) ), ' ' ); |
| 646 |
$mobile_prefixes = addcslashes( implode( '|', preg_split( '/[\s*,\s*]*,+[\s*,\s*]*/', powered_cache_mobile_prefixes() ) ), ' ' ); |
| 647 |
|
| 648 |
$contents .= 'if ($http_user_agent ~* (' . $mobile_browsers . '))' . PHP_EOL; |
| 649 |
$contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL; |
| 650 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 651 |
|
| 652 |
$contents .= 'if ($http_user_agent ~* (' . $mobile_prefixes . '))' . PHP_EOL; |
| 653 |
$contents .= ' set $pc_ua \'-mobile\';' . PHP_EOL; |
| 654 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 655 |
} |
| 656 |
|
| 657 |
$contents .= 'location = /favicon.ico { log_not_found off; access_log off; }' . PHP_EOL; |
| 658 |
$contents .= 'location = /robots.txt { log_not_found off; access_log off; }' . PHP_EOL . PHP_EOL; |
| 659 |
|
| 660 |
$contents .= 'location ~* .(jpg|jpeg|png|gif|ico|css|js|woff|svg)$ {' . PHP_EOL; |
| 661 |
$contents .= ' expires 1M;' . PHP_EOL; |
| 662 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 663 |
|
| 664 |
$cache_suffix = 'html'; |
| 665 |
if ( true === powered_cache_get_option( 'gzip_compression' ) ) { |
| 666 |
$cache_suffix .= '_gzip'; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
$contents .= 'location / {' . PHP_EOL; |
| 671 |
$contents .= ' add_header X-Powered-Cache nginx;' . PHP_EOL; |
| 672 |
$contents .= ' try_files /wp-content/cache/powered-cache/$http_host/$cache_uri/index${pc_ssl}${pc_ua}.' . $cache_suffix . ' $uri $uri/ /index.php?$args;' . PHP_EOL; |
| 673 |
$contents .= '}' . PHP_EOL . PHP_EOL; |
| 674 |
|
| 675 |
|
| 676 |
return $contents; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
/** |
| 681 |
* Downloads configuration files |
| 682 |
* |
| 683 |
* @since 1.1 |
| 684 |
* |
| 685 |
* @param string $server type supports apache and nginx |
| 686 |
*/ |
| 687 |
public function download_rewrite_rules( $server ) { |
| 688 |
|
| 689 |
$rules = ''; |
| 690 |
$filename = 'conf'; |
| 691 |
|
| 692 |
if ( 'apache' === $server ) { |
| 693 |
$rules = $this->htaccess_rules(); |
| 694 |
$filename = '.htaccess_powered_cache'; |
| 695 |
} |
| 696 |
|
| 697 |
if ( 'nginx' === $server ) { |
| 698 |
$rules = $this->nginx_rules(); |
| 699 |
$filename = 'poweredcache.conf'; |
| 700 |
} |
| 701 |
|
| 702 |
nocache_headers(); |
| 703 |
@header( 'Content-Type: text/plain' ); |
| 704 |
@header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); |
| 705 |
@header( 'Content-Transfer-Encoding: binary' ); |
| 706 |
@header( 'Content-Length: ' . strlen( $rules ) ); |
| 707 |
@header( 'Connection: close' ); |
| 708 |
echo $rules; |
| 709 |
exit; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
} |
| 714 |
|