/** * Returns full urls for frontpage including older pages * @param int $limit_post_pages default is 10 * @return array */ function get_frontpage_urls($limit_post_pages = 10) { if (!isset($this->frontpage_urls[$limit_post_pages])) { $front_page = get_option('show_on_front'); $full_urls = array(); $home_path = w3_get_home_path(); $site_path = w3_get_site_path(); $full_urls[] = $this->home_url . '/'; if ($site_path != $home_path) { $full_urls[] = w3_get_site_url() . '/'; } if ($front_page == 'posts') { $full_urls = array_merge($full_urls, $this->get_older_pages($home_path, $limit_post_pages)); } $this->frontpage_urls[$limit_post_pages] = $full_urls; } return $this->frontpage_urls[$limit_post_pages]; }
/** * Remove script tags from the source * * @param string $content * @param array $files * @return void */ function remove_scripts(&$content, $files) { $regexps = array(); $home_url_regexp = w3_get_home_url_regexp(); $path = ''; if (w3_is_network() && !w3_is_subdomain_install()) { $path = ltrim(w3_get_home_path(), '/'); } foreach ($files as $file) { if ($path && strpos($file, $path) === 0) { $file = substr($file, strlen($path)); } $this->replaced_scripts[] = $file; if (w3_is_url($file) && !preg_match('~' . $home_url_regexp . '~i', $file)) { // external JS files $regexps[] = w3_preg_quote($file); } else { // local JS files $file = ltrim($file, '/'); if (ltrim(w3_get_site_path(), '/') && strpos($file, ltrim(w3_get_site_path(), '/')) === 0) { $file = str_replace(ltrim(w3_get_site_path(), '/'), '', $file); } $file = ltrim(preg_replace('~' . $home_url_regexp . '~i', '', $file), '/\\'); $regexps[] = '(' . $home_url_regexp . ')?/?' . w3_preg_quote($file); } } foreach ($regexps as $regexp) { $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content); } }
/** * Flushes post cache * * @param integer $post_id * @return boolean */ function flush_post($post_id = null) { if (!$post_id) { $post_id = $this->_detect_post_id(); } if ($post_id) { $uris = array(); $domain_url = w3_get_domain_url(); $feeds = $this->_config->get_array('pgcache.purge.feed.types'); if ($this->_config->get_boolean('pgcache.purge.terms') || $this->_config->get_boolean('pgcache.purge.feed.terms')) { $taxonomies = get_post_taxonomies($post_id); $terms = nxt_get_post_terms($post_id, $taxonomies); } switch (true) { case $this->_config->get_boolean('pgcache.purge.author'): case $this->_config->get_boolean('pgcache.purge.archive.daily'): case $this->_config->get_boolean('pgcache.purge.archive.monthly'): case $this->_config->get_boolean('pgcache.purge.archive.yearly'): case $this->_config->get_boolean('pgcache.purge.feed.author'): $post = get_post($post_id); } /** * Home URL */ if ($this->_config->get_boolean('pgcache.purge.home')) { $home_path = w3_get_home_path(); $site_path = w3_get_site_path(); $uris[] = $home_path; if ($site_path != $home_path) { $uris[] = $site_path; } } /** * Post URL */ if ($this->_config->get_boolean('pgcache.purge.post')) { $post_link = post_permalink($post_id); $post_uri = str_replace($domain_url, '', $post_link); $uris[] = $post_uri; } /** * Post comments URLs */ if ($this->_config->get_boolean('pgcache.purge.comments') && function_exists('get_comments_pagenum_link')) { $comments_number = get_comments_number($post_id); $comments_per_page = get_option('comments_per_page'); $comments_pages_number = @ceil($comments_number / $comments_per_page); for ($pagenum = 1; $pagenum <= $comments_pages_number; $pagenum++) { $comments_pagenum_link = $this->_get_comments_pagenum_link($post_id, $pagenum); $comments_pagenum_uri = str_replace($domain_url, '', $comments_pagenum_link); $uris[] = $comments_pagenum_uri; } } /** * Post author URLs */ if ($this->_config->get_boolean('pgcache.purge.author') && $post) { $posts_number = count_user_posts($post->post_author); $posts_per_page = get_option('posts_per_page'); $posts_pages_number = @ceil($posts_number / $posts_per_page); $author_link = get_author_link(false, $post->post_author); $author_uri = str_replace($domain_url, '', $author_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $author_pagenum_link = $this->_get_pagenum_link($author_uri, $pagenum); $author_pagenum_uri = str_replace($domain_url, '', $author_pagenum_link); $uris[] = $author_pagenum_uri; } } /** * Post terms URLs */ if ($this->_config->get_boolean('pgcache.purge.terms')) { $posts_per_page = get_option('posts_per_page'); foreach ($terms as $term) { $term_link = get_term_link($term, $term->taxonomy); $term_uri = str_replace($domain_url, '', $term_link); $posts_pages_number = @ceil($term->count / $posts_per_page); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $term_pagenum_link = $this->_get_pagenum_link($term_uri, $pagenum); $term_pagenum_uri = str_replace($domain_url, '', $term_pagenum_link); $uris[] = $term_pagenum_uri; } } } /** * Daily archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.daily') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $post_month = gmdate('m', $post_date); $post_day = gmdate('d', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year, $post_month, $post_day); $posts_pages_number = @ceil($posts_number / $posts_per_page); $day_link = get_day_link($post_year, $post_month, $post_day); $day_uri = str_replace($domain_url, '', $day_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $day_pagenum_link = $this->_get_pagenum_link($day_uri, $pagenum); $day_pagenum_uri = str_replace($domain_url, '', $day_pagenum_link); $uris[] = $day_pagenum_uri; } } /** * Monthly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.monthly') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $post_month = gmdate('m', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year, $post_month); $posts_pages_number = @ceil($posts_number / $posts_per_page); $month_link = get_month_link($post_year, $post_month); $month_uri = str_replace($domain_url, '', $month_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $month_pagenum_link = $this->_get_pagenum_link($month_uri, $pagenum); $month_pagenum_uri = str_replace($domain_url, '', $month_pagenum_link); $uris[] = $month_pagenum_uri; } } /** * Yearly archive URLs */ if ($this->_config->get_boolean('pgcache.purge.archive.yearly') && $post) { $post_date = strtotime($post->post_date); $post_year = gmdate('Y', $post_date); $posts_per_page = get_option('posts_per_page'); $posts_number = $this->_get_archive_posts_count($post_year); $posts_pages_number = @ceil($posts_number / $posts_per_page); $year_link = get_year_link($post_year); $year_uri = str_replace($domain_url, '', $year_link); for ($pagenum = 1; $pagenum <= $posts_pages_number; $pagenum++) { $year_pagenum_link = $this->_get_pagenum_link($year_uri, $pagenum); $year_pagenum_uri = str_replace($domain_url, '', $year_pagenum_link); $uris[] = $year_pagenum_uri; } } /** * Feed URLs */ if ($this->_config->get_boolean('pgcache.purge.feed.blog')) { foreach ($feeds as $feed) { $feed_link = get_feed_link($feed); $feed_uri = str_replace($domain_url, '', $feed_link); $uris[] = $feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.comments')) { foreach ($feeds as $feed) { $post_comments_feed_link = get_post_comments_feed_link($post_id, $feed); $post_comments_feed_uri = str_replace($domain_url, '', $post_comments_feed_link); $uris[] = $post_comments_feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.author') && $post) { foreach ($feeds as $feed) { $author_feed_link = get_author_feed_link($post->post_author, $feed); $author_feed_uri = str_replace($domain_url, '', $author_feed_link); $uris[] = $author_feed_uri; } } if ($this->_config->get_boolean('pgcache.purge.feed.terms')) { foreach ($terms as $term) { foreach ($feeds as $feed) { $term_feed_link = get_term_feed_link($term->term_id, $term->taxonomy, $feed); $term_feed_uri = str_replace($domain_url, '', $term_feed_link); $uris[] = $term_feed_uri; } } } /** * Flush cache */ if (count($uris)) { $cache =& $this->_get_cache(); $mobile_groups = $this->_get_mobile_groups(); $referrer_groups = $this->_get_referrer_groups(); $encryptions = $this->_get_encryptions(); $compressions = $this->_get_compressions(); foreach ($uris as $uri) { foreach ($mobile_groups as $mobile_group) { foreach ($referrer_groups as $referrer_group) { foreach ($encryptions as $encryption) { foreach ($compressions as $compression) { $page_key = $this->_get_page_key($uri, $mobile_group, $referrer_group, $encryption, $compression); $cache->delete($page_key); } } } } } /** * Purge varnish servers */ if ($this->_config->get_boolean('varnish.enabled')) { $varnish =& w3_instance('W3_Varnish'); foreach ($uris as $uri) { $varnish->purge($uri); } } } return true; } return false; }
/** * Returns absolute path to home directory * * Example: * * DOCUMENT_ROOT=/var/www/vhosts/domain.com * Install dir=/var/www/vhosts/domain.com/site/blog * home=http://domain.com/site * siteurl=http://domain.com/site/blog * return /var/www/vhosts/domain.com/site * * No trailing slash! * * @return string */ function w3_get_home_root() { if (w3_is_network()) { $path = w3_get_base_path(); } else { $path = w3_get_home_path(); } $home_root = w3_get_document_root() . $path; $home_root = realpath($home_root); $home_root = w3_path($home_root); return $home_root; }
/** * Generates rules for WP dir * * @param W3_Config $config * @return string */ private function rules_core_generate_apache($config) { $is_network = w3_is_network(); $base_path = w3_get_base_path(); $home_path = w3_get_home_path(); $rewrite_base = $is_network ? $base_path : $home_path; $cache_dir = w3_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')); 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 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/?\$ \$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(w3_get_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}'; } $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('w3_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('w3_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; $switch = " -" . ($config->get_boolean('pgcache.file.nfs') ? 'F' : 'f'); // support for GoDaddy servers configuration which uses // SUBDOMAIN_DOCUMENT_ROOT variable if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT']) && $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] != $_SERVER['DOCUMENT_ROOT']) { $document_root = '%{ENV:SUBDOMAIN_DOCUMENT_ROOT}'; } elseif (isset($_SERVER['PHP_DOCUMENT_ROOT']) && $_SERVER['PHP_DOCUMENT_ROOT'] != $_SERVER['DOCUMENT_ROOT']) { $document_root = '%{ENV:PHP_DOCUMENT_ROOT}'; } else { $document_root = '%{DOCUMENT_ROOT}'; } // 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; }
/** * 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; }
/** * Generates rules for WP dir * * @return string */ function generate_rules_core_apache() { $is_network = w3_is_network(); $is_vhost = w3_is_subdomain_install(); $base_path = w3_get_base_path(); $home_path = w3_get_home_path(); $rewrite_base = $is_network ? $base_path : $home_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 directives */ $rules = ''; $rules .= W3TC_MARKER_BEGIN_PGCACHE_CORE . "\n"; $rules .= "<IfModule mod_rewrite.c>\n"; $rules .= " RewriteEngine On\n"; $rules .= " RewriteBase " . $rewrite_base . "\n"; $rules .= " RewriteRule ^(.*\\/)?w3tc_rewrite_test\$ \$1?w3tc_rewrite_test=1 [L]\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 .= " RewriteCond %{HTTP_USER_AGENT} (" . implode('|', $mobile_agents) . ") [NC]\n"; $rules .= " RewriteRule .* " . $mobile_redirect . " [R,L]\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 .= " RewriteCond %{HTTP_COOKIE} w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ") [NC]\n"; $rules .= " RewriteRule .* " . $referrer_redirect . " [R,L]\n"; } } } /** * Network mode rules */ if ($is_network) { /** * Detect domain */ $rules .= " RewriteCond %{HTTP_HOST} ^(www\\.)?([a-z0-9\\-\\.]+\\.[a-z]+)\\.?(:[0-9]+)?\$\n"; $rules .= " RewriteRule .* - [E=W3TC_DOMAIN:%2]\n"; $replacement = '/w3tc-%{ENV:W3TC_DOMAIN}/'; /** * If VHOST is off, detect blogname from URI */ if (!$is_vhost) { $blognames = w3_get_blognames(); if (count($blognames)) { $rules .= " RewriteCond %{REQUEST_URI} ^" . $base_path . "(" . implode('|', array_map('w3_preg_quote', $blognames)) . ")/\n"; $rules .= " RewriteRule .* - [E=W3TC_BLOGNAME:%1.]\n"; $replacement = '/w3tc-%{ENV:W3TC_BLOGNAME}%{ENV:W3TC_DOMAIN}/'; } } $cache_dir = preg_replace('~/w3tc.*/~U', $replacement, $cache_dir, 1); } /** * Set mobile groups */ 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 .= " RewriteCond %{HTTP_USER_AGENT} (" . implode('|', $mobile_agents) . ") [NC]\n"; $rules .= " RewriteRule .* - [E=W3TC_UA:_" . $mobile_group . "]\n"; } } } /** * Set referrer groups */ 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 .= " RewriteCond %{HTTP_COOKIE} w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ") [NC]\n"; $rules .= " RewriteRule .* - [E=W3TC_REF:_" . $referrer_group . "]\n"; } } } /** * Set HTTPS */ if ($this->_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"; } $cache_path = str_replace(w3_get_document_root(), '', $cache_dir); /** * Set Accept-Encoding */ if ($this->_config->get_boolean('browsercache.enabled') && $this->_config->get_boolean('browsercache.html.compression')) { $rules .= " RewriteCond %{HTTP:Accept-Encoding} gzip\n"; $rules .= " RewriteRule .* - [E=W3TC_ENC:_gzip]\n"; } $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 hostname */ if ($this->_config->get_boolean('pgcache.check.domain')) { $use_cache_rules .= " RewriteCond %{HTTP_HOST} =" . w3_get_home_domain() . "\n"; } /** * Check permalink structure trailing slash */ if (substr($permalink_structure, -1) == '/') { $use_cache_rules .= " RewriteCond %{REQUEST_URI} \\/\$"; if (count($accept_uris)) { $use_cache_rules .= " [OR]\n"; $use_cache_rules .= " RewriteCond %{REQUEST_URI} (" . implode('|', $accept_uris) . ") [NC]\n"; } else { $use_cache_rules .= "\n"; } } /** * Don't accept rejected URIs */ $use_cache_rules .= " RewriteCond %{REQUEST_URI} !(" . implode('|', $reject_uris) . ")"; /** * Exclude files from rejected URIs list */ if (count($accept_files)) { $use_cache_rules .= " [NC,OR]\n RewriteCond %{REQUEST_URI} (" . implode('|', array_map('w3_preg_quote', $accept_files)) . ") [NC]\n"; } else { $use_cache_rules .= " [NC]\n"; } /** * Check for rejected cookies */ $use_cache_rules .= " RewriteCond %{HTTP_COOKIE} !(" . implode('|', array_map('w3_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('w3_preg_quote', $reject_user_agents)) . ") [NC]\n"; } /** * Make final rewrites for specific files */ $uri_pre = $cache_path . "/%{REQUEST_URI}/_index%{ENV:W3TC_UA}%{ENV:W3TC_REF}%{ENV:W3TC_SSL}"; $switch = " -" . ($this->_config->get_boolean('pgcache.file.nfs') ? 'F' : 'f'); $extensions = array('.html'); if ($this->_config->get_boolean('pgcache.cache.feed')) { $extensions[] = '.xml'; } foreach ($extensions as $ext) { $rules .= $use_cache_rules; $rules .= " RewriteCond \"%{DOCUMENT_ROOT}" . $uri_pre . $ext . "%{ENV:W3TC_ENC}\"" . $switch . "\n"; $rules .= " RewriteRule .* \"" . $uri_pre . $ext . "%{ENV:W3TC_ENC}\" [L]\n"; } $rules .= "</IfModule>\n"; $rules .= W3TC_MARKER_END_PGCACHE_CORE . "\n"; return $rules; }
/** * Queues file upload. * Links wp_cron call to do that by the end of request processing * * @param string $url * @return void */ function queue_upload_url($url) { /** * Get filesystem file name by url */ $home_url_ssl = w3_get_home_url_ssl(); if (substr($url, 0, strlen($home_url_ssl)) == $home_url_ssl) { if (!w3_is_multisite()) { $home_url_ssl = str_replace(trim(w3_get_home_path(), '/'), '', $home_url_ssl); } $path = str_replace($home_url_ssl, '', $url); $file_name = w3_get_document_root() . '/' . trim($path, '/'); } else { // unknown url for uploading return; } $document_root = w3_get_document_root() . '/'; /* * Define remote filename */ if (!substr($file_name, 0, strlen($document_root)) == $document_root) { // unexpected file name return; } $remote_file_name = $this->uri_to_cdn_uri($this->docroot_filename_to_uri(substr($file_name, strlen($document_root)))); /* * Queue uploading */ $this->queue_add($file_name, $remote_file_name, W3TC_CDN_COMMAND_UPLOAD, 'Pending'); if (!$this->_upload_scheduled) { wp_schedule_single_event(time() - 100, 'w3_cdn_cron_queue_process'); add_action('shutdown', 'wp_cron'); $this->_upload_scheduled = true; } }