예제 #1
0
/**
 * Returns blogname from URI
 *
 * @param string $uri
 * @return string
 */
function w3_get_blogname_from_uri($uri)
{
    $blogname = '';
    $matches = null;
    $uri = strtolower($uri);
    if (preg_match('~^/([a-z0-9-]+)/~', $uri, $matches)) {
        if (file_exists(W3TC_BLOGNAMES_PATH)) {
            // Get blognames from cache
            $blognames = w3_load_blognames();
        } elseif (isset($GLOBALS['wpdb'])) {
            // Get blognames from DB
            $blognames = w3_get_blognames();
        } else {
            $blognames = array();
        }
        if (is_array($blognames) && in_array($matches[1], $blognames)) {
            $blogname = $matches[1];
        }
    }
    return $blogname;
}
예제 #2
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;
 }