/**
  * Generates rules for WP dir
  *
  * @param Config  $config
  * @return string
  */
 private function rules_core_generate_apache($config)
 {
     $rewrite_base = Util_Environment::network_home_url_uri();
     $cache_dir = Util_Environment::normalize_path(W3TC_CACHE_PAGE_ENHANCED_DIR);
     $permalink_structure = get_option('permalink_structure');
     $current_user = get_currentuserinfo();
     /**
      * 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'));
     Util_Rule::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);
     }
     Util_Rule::array_trim($reject_user_agents);
     /**
      * Generate directives
      */
     $env_W3TC_UA = '';
     $env_W3TC_REF = '';
     $env_W3TC_SSL = '';
     $env_W3TC_ENC = '';
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_PGCACHE_CORE . "\n";
     $rules .= "<IfModule mod_rewrite.c>\n";
     $rules .= "    RewriteEngine On\n";
     $rules .= "    RewriteBase " . $rewrite_base . "\n";
     if ($config->get_boolean('pgcache.debug')) {
         $rules .= "    RewriteRule ^(.*\\/)?w3tc_rewrite_test([0-9]+)/?\$ \$1?w3tc_rewrite_test=1 [L]\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 .= "    RewriteCond %{HTTP_USER_AGENT} (" . implode('|', $mobile_agents) . ") [NC]\n";
                 $rules .= "    RewriteRule .* " . $mobile_redirect . " [R,L]\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 .= "    RewriteCond %{HTTP_COOKIE} w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ") [NC]\n";
                 $rules .= "    RewriteRule .* " . $referrer_redirect . " [R,L]\n";
             }
         }
     }
     /**
      * Set 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 .= "    RewriteCond %{HTTP_USER_AGENT} (" . implode('|', $mobile_agents) . ") [NC]\n";
                 $rules .= "    RewriteRule .* - [E=W3TC_UA:_" . $mobile_group . "]\n";
                 $env_W3TC_UA = '%{ENV:W3TC_UA}';
             }
         }
     }
     /**
      * Set 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 .= "    RewriteCond %{HTTP_COOKIE} w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ") [NC]\n";
                 $rules .= "    RewriteRule .* - [E=W3TC_REF:_" . $referrer_group . "]\n";
                 $env_W3TC_REF = '%{ENV:W3TC_REF}';
             }
         }
     }
     /**
      * Set HTTPS
      */
     if ($config->get_boolean('pgcache.cache.ssl')) {
         $rules .= "    RewriteCond %{HTTPS} =on\n";
         $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";
         $rules .= "    RewriteCond %{SERVER_PORT} =443\n";
         $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";
         $env_W3TC_SSL = '%{ENV:W3TC_SSL}';
     }
     $cache_path = str_replace(Util_Environment::document_root(), '', $cache_dir);
     /**
      * Set Accept-Encoding
      */
     if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.html.compression')) {
         $rules .= "    RewriteCond %{HTTP:Accept-Encoding} gzip\n";
         $rules .= "    RewriteRule .* - [E=W3TC_ENC:_gzip]\n";
         $env_W3TC_ENC = '%{ENV:W3TC_ENC}';
     }
     $rules .= "    RewriteCond %{HTTP_COOKIE} w3tc_preview [NC]\n";
     $rules .= "    RewriteRule .* - [E=W3TC_PREVIEW:_preview]\n";
     $env_W3TC_PREVIEW = '%{ENV:W3TC_PREVIEW}';
     $use_cache_rules = '';
     /**
      * Don't accept POSTs
      */
     $use_cache_rules .= "    RewriteCond %{REQUEST_METHOD} !=POST\n";
     /**
      * Query string should be empty
      */
     $use_cache_rules .= "    RewriteCond %{QUERY_STRING} =\"\"\n";
     /**
      * Check permalink structure trailing slash
      */
     if (substr($permalink_structure, -1) == '/') {
         $use_cache_rules .= "    RewriteCond %{REQUEST_URI} \\/\$\n";
     }
     /**
      * Check for rejected cookies
      */
     $use_cache_rules .= "    RewriteCond %{HTTP_COOKIE} !(" . implode('|', array_map(array('\\W3TC\\Util_Environment', 'preg_quote'), $reject_cookies)) . ") [NC]\n";
     /**
      * Check for rejected user agents
      */
     if (count($reject_user_agents)) {
         $use_cache_rules .= "    RewriteCond %{HTTP_USER_AGENT} !(" . implode('|', array_map(array('\\W3TC\\Util_Environment', 'preg_quote'), $reject_user_agents)) . ") [NC]\n";
     }
     /**
      * Make final rewrites for specific files
      */
     $uri_prefix = $cache_path . '/%{HTTP_HOST}/%{REQUEST_URI}/' . '_index' . $env_W3TC_UA . $env_W3TC_REF . $env_W3TC_SSL . $env_W3TC_PREVIEW;
     $switch = " -" . ($config->get_boolean('pgcache.file.nfs') ? 'F' : 'f');
     $document_root = Util_Rule::apache_docroot_variable();
     // write rule to rewrite to .html file
     $ext = '.html';
     $rules .= $use_cache_rules;
     $rules .= "    RewriteCond \"" . $document_root . $uri_prefix . $ext . $env_W3TC_ENC . "\"" . $switch . "\n";
     $rules .= "    RewriteRule .* \"" . $uri_prefix . $ext . $env_W3TC_ENC . "\" [L]\n";
     $rules .= "</IfModule>\n";
     $rules .= W3TC_MARKER_END_PGCACHE_CORE . "\n";
     return $rules;
 }
 /**
  * Generates rules for WP dir
  *
  * @param Config  $config
  * @return string
  */
 private static function rules_generate_apache($config)
 {
     $rewrite_base = Util_Environment::network_home_url_uri();
     $a = Util_Environment::wp_upload_dir();
     $parse_url = @parse_url($a['baseurl']);
     if (empty($parse_url['path'])) {
         return '';
     }
     $uploads_path = $parse_url['path'];
     // cut off rewrite base since its already specified by rules
     if (substr($uploads_path, 0, strlen($rewrite_base)) == $rewrite_base) {
         $uploads_path_nobase = substr($uploads_path, strlen($rewrite_base));
     } else {
         $uploads_path_nobase = $uploads_path;
     }
     $rules = W3TC_MARKER_BEGIN_FEEDBURNER . "\n";
     $rules .= "<IfModule mod_rewrite.c>\n";
     $rules .= "    RewriteCond %{HTTP_USER_AGENT} FeedBurner\n";
     $rules .= "    RewriteRule ^{$uploads_path_nobase}/([0-9]+)/([0-9]+)/hotlink-ok/(.*)\$ {$uploads_path}/\$1/\$2/\$3 [L]\n";
     $rules .= "</IfModule>\n";
     $rules .= W3TC_MARKER_END_FEEDBURNER . "\n";
     return $rules;
 }
 /**
  * Returns server info
  *
  * @return array
  */
 private 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' => Util_Environment::blog_id(), 'home_domain_root_url' => Util_Environment::home_domain_root_url(), 'home_url_maybe_https' => Util_Environment::home_url_maybe_https(), 'site_path' => Util_Environment::site_path(), 'document_root' => Util_Environment::document_root(), 'site_root' => Util_Environment::site_root(), 'site_url_uri' => Util_Environment::site_url_uri(), 'home_url_host' => Util_Environment::home_url_host(), 'home_url_uri' => Util_Environment::home_url_uri(), 'network_home_url_uri' => Util_Environment::network_home_url_uri(), 'host_port' => Util_Environment::host_port(), 'host' => Util_Environment::host(), 'wp_config_path' => Util_Environment::wp_config_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) Util_Http::upload_info(), 'theme' => Util_Theme::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;
 }