Esempio n. 1
0
 /**
  * Reads legacy config file
  * @param int $blog_id
  * @param bool $force_master
  * @return array
  */
 public function get_imported_legacy_config_keys($blog_id, $force_master = false)
 {
     $suffix = '';
     if ($force_master) {
     } else {
         if ($blog_id > 0) {
             if (w3_is_network()) {
                 if (w3_is_subdomain_install()) {
                     $suffix = '-' . w3_get_domain(w3_get_host());
                 } else {
                     // try subdir blog
                     $request_uri = rtrim($_SERVER['REQUEST_URI'], '/');
                     $site_home_uri = w3_get_base_path();
                     if (substr($request_uri, 0, strlen($site_home_uri)) == $site_home_uri) {
                         $request_path_in_wp = '/' . substr($request_uri, strlen($site_home_uri));
                         $n = strpos($request_path_in_wp, '/', 1);
                         if ($n === false) {
                             $blog_path_in_wp = substr($request_path_in_wp, 1);
                         } else {
                             $blog_path_in_wp = substr($request_path_in_wp, 1, $n - 1);
                         }
                         $suffix = '-' . ($blog_path_in_wp != 'wp-admin' ? $blog_path_in_wp . '.' : '') . w3_get_domain(w3_get_host());
                     }
                 }
             }
         }
     }
     $filename = WP_CONTENT_DIR . '/w3-total-cache-config' . $suffix . '.php';
     $legacy_config = W3_ConfigData::get_array_from_file($filename);
     if (is_array($legacy_config) && isset($legacy_config['pgcache.engine']) && $legacy_config['pgcache.engine'] == 'file_pgcache') {
         $legacy_config['pgcache.engine'] = 'file_generic';
     }
     return $legacy_config;
 }
Esempio n. 2
0
 /**
  * Returns HTTP referrer value
  *
  * @return string
  */
 function get_http_referrer()
 {
     $http_referrer = '';
     if (isset($_COOKIE[W3TC_REFERRER_COOKIE_NAME])) {
         $http_referrer = $_COOKIE[W3TC_REFERRER_COOKIE_NAME];
     } elseif (isset($_SERVER['HTTP_REFERER'])) {
         $http_referrer = $_SERVER['HTTP_REFERER'];
         setcookie(W3TC_REFERRER_COOKIE_NAME, $http_referrer, 0, w3_get_base_path());
     }
     return $http_referrer;
 }
Esempio n. 3
0
 /**
  * Returns HTTP referrer value
  *
  * @return string
  */
 function get_http_referrer()
 {
     $http_referrer = '';
     if ($this->has_enabled_groups()) {
         if (isset($_COOKIE[W3TC_REFERRER_COOKIE_NAME])) {
             $http_referrer = $_COOKIE[W3TC_REFERRER_COOKIE_NAME];
         } elseif (isset($_SERVER['HTTP_REFERER'])) {
             $http_referrer = $_SERVER['HTTP_REFERER'];
             setcookie(W3TC_REFERRER_COOKIE_NAME, $http_referrer, 0, w3_get_base_path());
         }
     } elseif (isset($_COOKIE[W3TC_REFERRER_COOKIE_NAME])) {
         setcookie(W3TC_REFERRER_COOKIE_NAME, '', 1);
     }
     return $http_referrer;
 }
 /**
  * Rewrite a file relative URI as root relative
  *
  * <code>
  * Minify_CSS_UriRewriter::rewriteRelative(
  *       '../img/hello.gif'
  *     , '/home/user/www/css'  // path of CSS file
  *     , '/home/user/www'      // doc root
  * );
  * // returns '/img/hello.gif'
  *
  * // example where static files are stored in a symlinked directory
  * Minify_CSS_UriRewriter::rewriteRelative(
  *       'hello.gif'
  *     , '/var/staticFiles/theme'
  *     , '/home/user/www'
  *     , array('/home/user/www/static' => '/var/staticFiles')
  * );
  * // returns '/static/theme/hello.gif'
  * </code>
  *
  * @param string $uri file relative URI
  *
  * @param string $realCurrentDir realpath of the current file's directory.
  *
  * @param string $realDocRoot realpath of the site document root.
  *
  * @param array $symlinks (default = array()) If the file is stored in
  * a symlink-ed directory, provide an array of link paths to
  * real target paths, where the link paths "appear" to be within the document
  * root. E.g.:
  * <code>
  * array('/home/foo/www/not/real/path' => '/real/target/path') // unix
  * array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path')  // Windows
  * </code>
  *
  * @return string
  */
 private static function _rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
 {
     if ('/' === $uri[0]) {
         // root-relative
         return $uri;
     }
     // prepend path with current dir separator (OS-independent)
     $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
     self::$debugText .= "file-relative URI  : {$uri}\n" . "path prepended     : {$path}\n";
     // "unresolve" a symlink back to doc root
     foreach ($symlinks as $link => $target) {
         if (0 === strpos($path, $target)) {
             // replace $target with $link
             $path = $link . substr($path, strlen($target));
             self::$debugText .= "symlink unresolved : {$path}\n";
             break;
         }
     }
     // strip doc root
     $path = substr($path, strlen($realDocRoot));
     self::$debugText .= "docroot stripped   : {$path}\n";
     // fix to root-relative URI
     $uri = strtr($path, '/\\', '//');
     // remove /./ and /../ where possible
     $uri = str_replace('/./', '/', $uri);
     // inspired by patch from Oleg Cherniy
     do {
         $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
     } while ($changed);
     self::$debugText .= "traversals removed : {$uri}\n\n";
     $uri = preg_replace('~^' . w3_preg_quote(w3_get_base_path()) . '~', w3_get_site_path(), $uri);
     return $uri;
 }
Esempio n. 5
0
/**
 * Translates remote file to local file
 *
 * @param string $file
 * @return string
 */
function w3_translate_file($file)
{
    if (!w3_is_url($file)) {
        $file = '/' . ltrim($file, '/');
        $regexp = '~^' . w3_preg_quote(w3_get_site_path()) . '~';
        $file = preg_replace($regexp, w3_get_base_path(), $file);
        $file = ltrim($file, '/');
    }
    return $file;
}
 /**
  * Generates rules for WP dir
  *
  * @param W3_Config $config
  * @return string
  */
 private function rules_core_generate_nginx($config)
 {
     $is_network = w3_is_network();
     $base_path = w3_get_base_path();
     $cache_dir = w3_path(W3TC_CACHE_PAGE_ENHANCED_DIR);
     $permalink_structure = get_option('permalink_structure');
     /**
      * Auto reject cookies
      */
     $reject_cookies = array('comment_author', 'wp-postpass');
     if ($config->get_string('pgcache.engine') == 'file_generic') {
         $reject_cookies[] = 'w3tc_logged_out';
     }
     /**
      * Reject cache for logged in users
      * OR
      * Reject cache for roles if any
      */
     if ($config->get_boolean('pgcache.reject.logged')) {
         $reject_cookies = array_merge($reject_cookies, array('wordpress_logged_in'));
     } elseif ($config->get_boolean('pgcache.reject.logged_roles')) {
         $new_cookies = array();
         foreach ($config->get_array('pgcache.reject.roles') as $role) {
             $new_cookies[] = 'w3tc_logged_' . md5(NONCE_KEY . $role);
         }
         $reject_cookies = array_merge($reject_cookies, $new_cookies);
     }
     /**
      * Custom config
      */
     $reject_cookies = array_merge($reject_cookies, $config->get_array('pgcache.reject.cookie'));
     w3_array_trim($reject_cookies);
     $reject_user_agents = $config->get_array('pgcache.reject.ua');
     if ($config->get_boolean('pgcache.compatibility')) {
         $reject_user_agents = array_merge(array(W3TC_POWERED_BY), $reject_user_agents);
     }
     w3_array_trim($reject_user_agents);
     /**
      * Generate rules
      */
     $env_w3tc_ua = '';
     $env_w3tc_ref = '';
     $env_w3tc_ssl = '';
     $env_w3tc_ext = '';
     $env_w3tc_enc = '';
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_PGCACHE_CORE . "\n";
     if ($config->get_boolean('pgcache.debug')) {
         $rules .= "rewrite ^(.*\\/)?w3tc_rewrite_test/?\$ \$1?w3tc_rewrite_test=1 last;\n";
     }
     /**
      * Check for mobile redirect
      */
     if ($config->get_boolean('mobile.enabled')) {
         $mobile_groups = $config->get_array('mobile.rgroups');
         foreach ($mobile_groups as $mobile_group => $mobile_config) {
             $mobile_enabled = isset($mobile_config['enabled']) ? (bool) $mobile_config['enabled'] : false;
             $mobile_agents = isset($mobile_config['agents']) ? (array) $mobile_config['agents'] : '';
             $mobile_redirect = isset($mobile_config['redirect']) ? $mobile_config['redirect'] : '';
             if ($mobile_enabled && count($mobile_agents) && $mobile_redirect) {
                 $rules .= "if (\$http_user_agent ~* \"(" . implode('|', $mobile_agents) . ")\") {\n";
                 $rules .= "    rewrite .* " . $mobile_redirect . " last;\n";
                 $rules .= "}\n";
             }
         }
     }
     /**
      * Check for referrer redirect
      */
     if ($config->get_boolean('referrer.enabled')) {
         $referrer_groups = $config->get_array('referrer.rgroups');
         foreach ($referrer_groups as $referrer_group => $referrer_config) {
             $referrer_enabled = isset($referrer_config['enabled']) ? (bool) $referrer_config['enabled'] : false;
             $referrer_referrers = isset($referrer_config['referrers']) ? (array) $referrer_config['referrers'] : '';
             $referrer_redirect = isset($referrer_config['redirect']) ? $referrer_config['redirect'] : '';
             if ($referrer_enabled && count($referrer_referrers) && $referrer_redirect) {
                 $rules .= "if (\$http_cookie ~* \"w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ")\") {\n";
                 $rules .= "    rewrite .* " . $referrer_redirect . " last;\n";
                 $rules .= "}\n";
             }
         }
     }
     /**
      * Don't accept POSTs
      */
     $rules .= "set \$w3tc_rewrite 1;\n";
     $rules .= "if (\$request_method = POST) {\n";
     $rules .= "    set \$w3tc_rewrite 0;\n";
     $rules .= "}\n";
     /**
      * Query string should be empty
      */
     $rules .= "if (\$query_string != \"\") {\n";
     $rules .= "    set \$w3tc_rewrite 0;\n";
     $rules .= "}\n";
     /**
      * Check permalink structure trailing slash
      */
     if (substr($permalink_structure, -1) == '/') {
         $rules .= "if (\$request_uri !~ \\/\$) {\n";
         $rules .= "    set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     }
     /**
      * Check for rejected cookies
      */
     $rules .= "if (\$http_cookie ~* \"(" . implode('|', array_map('w3_preg_quote', $reject_cookies)) . ")\") {\n";
     $rules .= "    set \$w3tc_rewrite 0;\n";
     $rules .= "}\n";
     /**
      * Check for rejected user agents
      */
     if (count($reject_user_agents)) {
         $rules .= "if (\$http_user_agent ~* \"(" . implode('|', array_map('w3_preg_quote', $reject_user_agents)) . ")\") {\n";
         $rules .= "    set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     }
     /**
      * Check mobile groups
      */
     if ($config->get_boolean('mobile.enabled')) {
         $mobile_groups = array_reverse($config->get_array('mobile.rgroups'));
         foreach ($mobile_groups as $mobile_group => $mobile_config) {
             $mobile_enabled = isset($mobile_config['enabled']) ? (bool) $mobile_config['enabled'] : false;
             $mobile_agents = isset($mobile_config['agents']) ? (array) $mobile_config['agents'] : '';
             $mobile_redirect = isset($mobile_config['redirect']) ? $mobile_config['redirect'] : '';
             if ($mobile_enabled && count($mobile_agents) && !$mobile_redirect) {
                 $rules .= "set \$w3tc_ua \"\";\n";
                 $rules .= "if (\$http_user_agent ~* \"(" . implode('|', $mobile_agents) . ")\") {\n";
                 $rules .= "    set \$w3tc_ua _" . $mobile_group . ";\n";
                 $rules .= "}\n";
                 $env_w3tc_ua = "\$w3tc_ua";
             }
         }
     }
     /**
      * Check referrer groups
      */
     if ($config->get_boolean('referrer.enabled')) {
         $referrer_groups = array_reverse($config->get_array('referrer.rgroups'));
         foreach ($referrer_groups as $referrer_group => $referrer_config) {
             $referrer_enabled = isset($referrer_config['enabled']) ? (bool) $referrer_config['enabled'] : false;
             $referrer_referrers = isset($referrer_config['referrers']) ? (array) $referrer_config['referrers'] : '';
             $referrer_redirect = isset($referrer_config['redirect']) ? $referrer_config['redirect'] : '';
             if ($referrer_enabled && count($referrer_referrers) && !$referrer_redirect) {
                 $rules .= "set \$w3tc_ref \"\";\n";
                 $rules .= "if (\$http_cookie ~* \"w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ")\") {\n";
                 $rules .= "    set \$w3tc_ref _" . $referrer_group . ";\n";
                 $rules .= "}\n";
                 $env_w3tc_ref = "\$w3tc_ref";
             }
         }
     }
     if ($config->get_boolean('pgcache.cache.ssl')) {
         $rules .= "set \$w3tc_ssl \"\";\n";
         $rules .= "if (\$scheme = https) {\n";
         $rules .= "    set \$w3tc_ssl _ssl;\n";
         $rules .= "}\n";
         $env_w3tc_ssl = "\$w3tc_ssl";
     }
     if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.html.compression')) {
         $rules .= "set \$w3tc_enc \"\";\n";
         $rules .= "if (\$http_accept_encoding ~ gzip) {\n";
         $rules .= "    set \$w3tc_enc _gzip;\n";
         $rules .= "}\n";
         $env_w3tc_enc = "\$w3tc_enc";
     }
     $cache_path = str_replace(w3_get_document_root(), '', $cache_dir);
     $uri_prefix = $cache_path . "/\$http_host/" . "\$request_uri/_index" . $env_w3tc_ua . $env_w3tc_ref . $env_w3tc_ssl;
     if (!$config->get_boolean('pgcache.cache.nginx_handle_xml')) {
         $env_w3tc_ext = '.html';
         $rules .= "if (!-f \"\$document_root" . $uri_prefix . ".html" . $env_w3tc_enc . "\") {\n";
         $rules .= "  set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     } else {
         $env_w3tc_ext = "\$w3tc_ext";
         $rules .= "set \$w3tc_ext \"\";\n";
         $rules .= "if (-f \"\$document_root" . $uri_prefix . ".html" . $env_w3tc_enc . "\") {\n";
         $rules .= "    set \$w3tc_ext .html;\n";
         $rules .= "}\n";
         $rules .= "if (-f \"\$document_root" . $uri_prefix . ".xml" . $env_w3tc_enc . "\") {\n";
         $rules .= "    set \$w3tc_ext .xml;\n";
         $rules .= "}\n";
         $rules .= "if (\$w3tc_ext = \"\") {\n";
         $rules .= "  set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     }
     $rules .= "if (\$w3tc_rewrite = 1) {\n";
     $rules .= "    rewrite .* \"" . $uri_prefix . $env_w3tc_ext . $env_w3tc_enc . "\" last;\n";
     $rules .= "}\n";
     $rules .= W3TC_MARKER_END_PGCACHE_CORE . "\n";
     return $rules;
 }
Esempio n. 7
0
 /**
  * Returns server info
  *
  * @return array
  */
 function get_server_info()
 {
     global $wp_version, $wp_db_version, $wpdb;
     $wordpress_plugins = get_plugins();
     $wordpress_plugins_active = array();
     foreach ($wordpress_plugins as $wordpress_plugin_file => $wordpress_plugin) {
         if (is_plugin_active($wordpress_plugin_file)) {
             $wordpress_plugins_active[$wordpress_plugin_file] = $wordpress_plugin;
         }
     }
     $mysql_version = $wpdb->get_var('SELECT VERSION()');
     $mysql_variables_result = (array) $wpdb->get_results('SHOW VARIABLES', ARRAY_N);
     $mysql_variables = array();
     foreach ($mysql_variables_result as $mysql_variables_row) {
         $mysql_variables[$mysql_variables_row[0]] = $mysql_variables_row[1];
     }
     $server_info = array('w3tc' => array('version' => W3TC_VERSION, 'server' => !empty($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : 'Unknown', 'dir' => W3TC_DIR, 'cache_dir' => W3TC_CACHE_DIR, 'blog_id' => w3_get_blog_id(), 'document_root' => w3_get_document_root(), 'home_root' => w3_get_home_root(), 'site_root' => w3_get_site_root(), 'base_path' => w3_get_base_path(), 'home_path' => w3_get_home_path(), 'site_path' => w3_get_site_path()), 'wp' => array('version' => $wp_version, 'db_version' => $wp_db_version, 'abspath' => ABSPATH, 'home' => get_option('home'), 'siteurl' => get_option('siteurl'), 'email' => get_option('admin_email'), 'upload_info' => (array) w3_upload_info(), 'theme' => w3tc_get_current_theme(), 'wp_cache' => defined('WP_CACHE') && WP_CACHE ? 'true' : 'false', 'plugins' => $wordpress_plugins_active), 'mysql' => array('version' => $mysql_version, 'variables' => $mysql_variables));
     return $server_info;
 }
Esempio n. 8
0
 /**
  * Generates rules for WP dir
  *
  * @return string
  */
 function generate_rules_core_nginx()
 {
     $is_network = w3_is_network();
     $is_vhost = w3_is_subdomain_install();
     $base_path = w3_get_base_path();
     $cache_dir = w3_path(W3TC_CACHE_FILE_PGCACHE_DIR);
     $permalink_structure = get_option('permalink_structure');
     /**
      * Auto reject cookies
      */
     $reject_cookies = array('comment_author', 'wp-postpass');
     /**
      * Auto reject URIs
      */
     $reject_uris = array('\\/wp-admin\\/', '\\/xmlrpc.php', '\\/wp-(app|cron|login|register|mail)\\.php');
     /**
      * Reject cache for logged in users
      */
     if ($this->_config->get_boolean('pgcache.reject.logged')) {
         $reject_cookies = array_merge($reject_cookies, array('wordpress_[a-f0-9]+', 'wordpress_logged_in'));
     }
     /**
      * Reject cache for home page
      */
     if (!$this->_config->get_boolean('pgcache.cache.home')) {
         $reject_uris[] = '^(\\/|\\/index.php)$';
     }
     /**
      * Reject cache for feeds
      */
     if (!$this->_config->get_boolean('pgcache.cache.feed')) {
         $reject_uris[] = '\\/feed\\/';
     }
     /**
      * Custom config
      */
     $reject_cookies = array_merge($reject_cookies, $this->_config->get_array('pgcache.reject.cookie'));
     $reject_uris = array_merge($reject_uris, $this->_config->get_array('pgcache.reject.uri'));
     $reject_uris = array_map('w3_parse_path', $reject_uris);
     $reject_user_agents = array_merge(array(W3TC_POWERED_BY), $this->_config->get_array('pgcache.reject.ua'));
     $accept_uris = $this->_config->get_array('pgcache.accept.uri');
     $accept_files = $this->_config->get_array('pgcache.accept.files');
     /**
      * Generate rules
      */
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_PGCACHE_CORE . "\n";
     $rules .= "rewrite ^(.*\\/)?w3tc_rewrite_test\$ \$1?w3tc_rewrite_test=1 last;\n";
     /**
      * Check for mobile redirect
      */
     if ($this->_config->get_boolean('mobile.enabled')) {
         $mobile_groups = $this->_config->get_array('mobile.rgroups');
         foreach ($mobile_groups as $mobile_group => $mobile_config) {
             $mobile_enabled = isset($mobile_config['enabled']) ? (bool) $mobile_config['enabled'] : false;
             $mobile_agents = isset($mobile_config['agents']) ? (array) $mobile_config['agents'] : '';
             $mobile_redirect = isset($mobile_config['redirect']) ? $mobile_config['redirect'] : '';
             if ($mobile_enabled && count($mobile_agents) && $mobile_redirect) {
                 $rules .= "if (\$http_user_agent ~* \"(" . implode('|', $mobile_agents) . ")\") {\n";
                 $rules .= "    rewrite .* " . $mobile_redirect . " last;\n";
                 $rules .= "}\n";
             }
         }
     }
     /**
      * Check for referrer redirect
      */
     if ($this->_config->get_boolean('referrer.enabled')) {
         $referrer_groups = $this->_config->get_array('referrer.rgroups');
         foreach ($referrer_groups as $referrer_group => $referrer_config) {
             $referrer_enabled = isset($referrer_config['enabled']) ? (bool) $referrer_config['enabled'] : false;
             $referrer_referrers = isset($referrer_config['referrers']) ? (array) $referrer_config['referrers'] : '';
             $referrer_redirect = isset($referrer_config['redirect']) ? $referrer_config['redirect'] : '';
             if ($referrer_enabled && count($referrer_referrers) && $referrer_redirect) {
                 $rules .= "if (\$http_cookie ~* \"w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ")\") {\n";
                 $rules .= "    rewrite .* " . $referrer_redirect . " last;\n";
                 $rules .= "}\n";
             }
         }
     }
     /**
      * Don't accept POSTs
      */
     $rules .= "set \$w3tc_rewrite 1;\n";
     $rules .= "if (\$request_method = POST) {\n";
     $rules .= "    set \$w3tc_rewrite 0;\n";
     $rules .= "}\n";
     /**
      * Query string should be empty
      */
     $rules .= "if (\$query_string != \"\") {\n";
     $rules .= "    set \$w3tc_rewrite 0;\n";
     $rules .= "}\n";
     /**
      * Check hostname
      */
     if ($this->_config->get_boolean('pgcache.check.domain')) {
         $rules .= "if (\$http_host != \"" . w3_get_home_domain() . "\") {\n";
         $rules .= "    set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     }
     /**
      * Check permalink structure trailing slash
      */
     if (substr($permalink_structure, -1) == '/') {
         if (!count($accept_uris)) {
             $rules .= "if (\$request_uri !~ \\/\$) {\n";
             $rules .= "    set \$w3tc_rewrite 0;\n";
             $rules .= "}\n";
         } else {
             $rules .= "set \$w3tc_rewrite2 1;\n";
             $rules .= "if (\$request_uri !~ \\/\$) {\n";
             $rules .= "    set \$w3tc_rewrite2 0;\n";
             $rules .= "}\n";
             $rules .= "if (\$request_uri ~* \"(" . implode('|', $accept_uris) . ")\") {\n";
             $rules .= "    set \$w3tc_rewrite2 1;\n";
             $rules .= "}\n";
             $rules .= "if (\$w3tc_rewrite2 != 1) {\n";
             $rules .= "    set \$w3tc_rewrite 0;\n";
             $rules .= "}\n";
         }
     }
     /**
      * Check for rejected URIs
      */
     if (!count($accept_files)) {
         $rules .= "if (\$request_uri ~* \"(" . implode('|', $reject_uris) . "\") {\n";
         $rules .= "    set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     } else {
         $rules .= "set \$w3tc_rewrite3 1;\n";
         $rules .= "if (\$request_uri ~* \"(" . implode('|', $reject_uris) . ")\") {\n";
         $rules .= "    set \$w3tc_rewrite3 0;\n";
         $rules .= "}\n";
         $rules .= "if (\$request_uri ~* \"(" . implode('|', array_map('w3_preg_quote', $accept_files)) . ")\") {\n";
         $rules .= "    set \$w3tc_rewrite3 1;\n";
         $rules .= "}\n";
         $rules .= "if (\$w3tc_rewrite3 != 1) {\n";
         $rules .= "    set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     }
     /**
      * Check for rejected cookies
      */
     $rules .= "if (\$http_cookie ~* \"(" . implode('|', array_map('w3_preg_quote', $reject_cookies)) . ")\") {\n";
     $rules .= "    set \$w3tc_rewrite 0;\n";
     $rules .= "}\n";
     /**
      * Check for rejected user agents
      */
     if (count($reject_user_agents)) {
         $rules .= "if (\$http_user_agent ~* \"(" . implode('|', array_map('w3_preg_quote', $reject_user_agents)) . ")\") {\n";
         $rules .= "    set \$w3tc_rewrite 0;\n";
         $rules .= "}\n";
     }
     /**
      * Network mode rules
      */
     if ($is_network) {
         /**
          * Detect domain
          */
         $rules .= "set \$w3tc_domain \"\";\n";
         $rules .= "if (\$http_host ~ ^(www\\.)?([a-z0-9\\-\\.]+\\.[a-z]+)\\.?(:[0-9]+)?\$) {\n";
         $rules .= "    set \$w3tc_domain \$2;\n";
         $rules .= "}\n";
         $replacement = '/w3tc-$w3tc_domain/';
         /**
          * If VHOST is off, detect blogname from URI
          */
         if (!$is_vhost) {
             $blognames = w3_get_blognames();
             if (count($blognames)) {
                 $rules .= "set \$w3tc_blogname \"\";\n";
                 $rules .= "if (\$request_uri ~ ^" . $base_path . "(" . implode('|', array_map('w3_preg_quote', $blognames)) . ")/) {\n";
                 $rules .= "    set \$w3tc_blogname \$1.;\n";
                 $rules .= "}\n";
                 $replacement = '/w3tc-$w3tc_blogname$w3tc_domain/';
             }
         }
         $cache_dir = preg_replace('~/w3tc.*?/~', $replacement, $cache_dir, 1);
     }
     /**
      * Check mobile groups
      */
     $rules .= "set \$w3tc_ua \"\";\n";
     if ($this->_config->get_boolean('mobile.enabled')) {
         $mobile_groups = array_reverse($this->_config->get_array('mobile.rgroups'));
         foreach ($mobile_groups as $mobile_group => $mobile_config) {
             $mobile_enabled = isset($mobile_config['enabled']) ? (bool) $mobile_config['enabled'] : false;
             $mobile_agents = isset($mobile_config['agents']) ? (array) $mobile_config['agents'] : '';
             $mobile_redirect = isset($mobile_config['redirect']) ? $mobile_config['redirect'] : '';
             if ($mobile_enabled && count($mobile_agents) && !$mobile_redirect) {
                 $rules .= "if (\$http_user_agent ~* \"(" . implode('|', $mobile_agents) . ")\") {\n";
                 $rules .= "    set \$w3tc_ua _" . $mobile_group . ";\n";
                 $rules .= "}\n";
             }
         }
     }
     /**
      * Check referrer groups
      */
     $rules .= "set \$w3tc_ref \"\";\n";
     if ($this->_config->get_boolean('referrer.enabled')) {
         $referrer_groups = array_reverse($this->_config->get_array('referrer.rgroups'));
         foreach ($referrer_groups as $referrer_group => $referrer_config) {
             $referrer_enabled = isset($referrer_config['enabled']) ? (bool) $referrer_config['enabled'] : false;
             $referrer_referrers = isset($referrer_config['referrers']) ? (array) $referrer_config['referrers'] : '';
             $referrer_redirect = isset($referrer_config['redirect']) ? $referrer_config['redirect'] : '';
             if ($referrer_enabled && count($referrer_referrers) && !$referrer_redirect) {
                 $rules .= "if (\$http_cookie ~* \"w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ")\") {\n";
                 $rules .= "    set \$w3tc_ref _" . $referrer_group . ";\n";
                 $rules .= "}\n";
             }
         }
     }
     $rules .= "set \$w3tc_ssl \"\";\n";
     if ($this->_config->get_boolean('pgcache.cache.ssl')) {
         $rules .= "if (\$scheme = https) {\n";
         $rules .= "    set \$w3tc_ssl _ssl;\n";
         $rules .= "}\n";
     }
     $rules .= "set \$w3tc_enc \"\";\n";
     if ($this->_config->get_boolean('browsercache.enabled') && $this->_config->get_boolean('browsercache.html.compression')) {
         $rules .= "if (\$http_accept_encoding ~ gzip) {\n";
         $rules .= "    set \$w3tc_enc _gzip;\n";
         $rules .= "}\n";
     }
     $cache_path = str_replace(w3_get_document_root(), '', $cache_dir);
     $rules .= "set \$w3tc_ext \"\";\n";
     $rules .= "if (-f \"\$document_root" . $cache_path . "/\$request_uri/_index\$w3tc_ua\$w3tc_ref\$w3tc_ssl.html\$w3tc_enc\") {\n";
     $rules .= "    set \$w3tc_ext .html;\n";
     $rules .= "}\n";
     if ($this->_config->get_boolean('pgcache.cache.feed')) {
         $rules .= "if (-f \"\$document_root" . $cache_path . "/\$request_uri/_index\$w3tc_ua\$w3tc_ref\$w3tc_ssl.xml\$w3tc_enc\") {\n";
         $rules .= "    set \$w3tc_ext .xml;\n";
         $rules .= "}\n";
     }
     $rules .= "if (\$w3tc_ext = \"\") {\n";
     $rules .= "  set \$w3tc_rewrite 0;\n";
     $rules .= "}\n";
     $rules .= "if (\$w3tc_rewrite = 1) {\n";
     $rules .= "    rewrite .* \"" . $cache_path . "/\$request_uri/_index\$w3tc_ua\$w3tc_ref\$w3tc_ssl\$w3tc_ext\$w3tc_enc\" last;\n";
     $rules .= "}\n";
     $rules .= W3TC_MARKER_END_PGCACHE_CORE . "\n";
     return $rules;
 }
 /**
  * Reads legacy config file
  *
  * @param object $compiled_config
  * @return array
  */
 private function _import_legacy_config($compiled_config)
 {
     $suffix = '';
     if ($this->_blog_id > 0) {
         if (w3_is_network()) {
             if (w3_is_subdomain_install()) {
                 $suffix = '-' . w3_get_domain(w3_get_host());
             } else {
                 // try subdir blog
                 $request_uri = rtrim($_SERVER['REQUEST_URI'], '/');
                 $site_home_uri = w3_get_base_path();
                 if (substr($request_uri, 0, strlen($site_home_uri)) == $site_home_uri) {
                     $request_path_in_wp = '/' . substr($request_uri, strlen($site_home_uri));
                     $n = strpos($request_path_in_wp, '/', 1);
                     if ($n === false) {
                         $blog_path_in_wp = substr($request_path_in_wp, 1);
                     } else {
                         $blog_path_in_wp = substr($request_path_in_wp, 1, $n - 1);
                     }
                     $suffix = '-' . ($blog_path_in_wp != 'wp-admin' ? $blog_path_in_wp . '.' : '') . w3_get_domain(w3_get_host());
                 }
             }
         }
     }
     $filename = WP_CONTENT_DIR . '/w3-total-cache-config' . $suffix . '.php';
     return $compiled_config->get_array_from_file($filename);
 }