/**
  * Fixes environment once event occurs
  *
  * @param Config  $config
  * @param string  $event
  * @param Config|null $old_config
  * @throws Util_Environment_Exceptions
  */
 public function fix_on_event($config, $event, $old_config = null)
 {
     if ($config->get_boolean('cdn.enabled') && !Cdn_Util::is_engine_mirror($config->get_string('cdn.engine'))) {
         if ($old_config != null && $config->get_integer('cdn.queue.interval') != $old_config->get_integer('cdn.queue.interval')) {
             $this->unschedule_queue_process();
         }
         if (!wp_next_scheduled('w3_cdn_cron_queue_process')) {
             wp_schedule_event(time(), 'w3_cdn_cron_queue_process', 'w3_cdn_cron_queue_process');
         }
     } else {
         $this->unschedule_queue_process();
     }
     if ($config->get_boolean('cdn.enabled') && $config->get_boolean('cdn.autoupload.enabled') && !Cdn_Util::is_engine_mirror($config->get_string('cdn.engine'))) {
         if ($old_config != null && $config->get_integer('cdn.autoupload.interval') != $old_config->get_integer('cdn.autoupload.interval')) {
             $this->unschedule_upload();
         }
         if (!wp_next_scheduled('w3_cdn_cron_upload')) {
             wp_schedule_event(time(), 'w3_cdn_cron_upload', 'w3_cdn_cron_upload');
         }
     } else {
         $this->unschedule_upload();
     }
     $exs = new Util_Environment_Exceptions();
     if ($config->get_boolean('cdn.enabled')) {
         try {
             $this->table_create($event == 'activate');
         } catch (\Exception $ex) {
             $exs->push($ex);
         }
     }
     if (count($exs->exceptions()) > 0) {
         throw $exs;
     }
 }
Exemple #2
0
 function __construct($login = null)
 {
     if ($login != null) {
         $path = '/users/' . self::escape_login($login);
         $this->login = $login;
         $config = new Config();
         $success = $this->first_name = $config->get_string($path . '/first_name');
         $success &= $this->last_name = $config->get_string($path . '/last_name');
         $success &= $this->roles = $config->get_array($path . '/roles');
         $success &= $this->password_hash = $config->get_string($path . '/password_hash');
         if ($success !== true) {
             return false;
         }
     }
 }
 /**
  * Returns required rules for module
  *
  * @param Config  $config
  * @return array
  */
 public static function get_required_rules($rewrite_rules, $config)
 {
     if (!$config->get_boolean('pgcache.enabled') || $config->get_string('pgcache.engine') != 'file_generic') {
         return $rewrite_rules;
     }
     $pgcache_rules_core_path = Util_Rule::get_pgcache_rules_core_path();
     $rewrite_rules[] = array('filename' => $pgcache_rules_core_path, 'content' => self::rules_generate($config), 'priority' => 1000);
     return $rewrite_rules;
 }
 protected function value_with_disabled($option_id, $disabled, $value_when_disabled)
 {
     if ($disabled) {
         echo 'value="' . esc_attr($value_when_disabled) . '" ';
         echo 'disabled="disabled" ';
     } else {
         echo 'value="' . esc_attr($this->_config->get_string($option_id)) . '" ';
     }
 }
 /**
  *
  *
  * @param Config  $config
  * @param Config  $old_config
  */
 function possible_state_change($config, $old_config)
 {
     if ($old_config->get_string('plugin.license_key') != '' && $config->get_string('plugin.license_key') == '') {
         $result = edd_w3edge_w3tc_deactivate_license($old_config->get_string('plugin.license_key'));
         if ($result) {
             $this->site_inactivated = true;
         }
         delete_transient('w3tc_license_status');
     } elseif ($old_config->get_string('plugin.license_key') == '' && $config->get_string('plugin.license_key') != '') {
         $result = edd_w3edge_w3tc_activate_license($config->get_string('plugin.license_key'), W3TC_VERSION);
         if ($result) {
             $this->site_activated = true;
             $config->set('common.track_usage', true);
         }
         delete_transient('w3tc_license_status');
     } elseif ($old_config->get_string('plugin.license_key') != $config->get_string('plugin.license_key')) {
         $result = edd_w3edge_w3tc_activate_license($config->get_string('plugin.license_key'), W3TC_VERSION);
         if ($result) {
             $this->site_activated = true;
         }
         delete_transient('w3tc_license_status');
     }
 }
 /**
  * Generates rules
  *
  * @param Config  $config
  * @return string
  */
 function rules_core_generate_nginx($config)
 {
     $cache_dir = Util_Rule::filename_to_uri(W3TC_CACHE_MINIFY_DIR);
     $first_regex_var = '$1';
     // for subdir - need to count subdir in url
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         // take into accont case when whole subdir wpmu is installed in subdir
         $home_uri = network_home_url('', 'relative');
         if (substr($cache_dir, 0, strlen($home_uri)) == $home_uri) {
             $cache_dir = $home_uri . '([a-z0-9]+/)?' . substr($cache_dir, strlen($home_uri));
         } else {
             $cache_dir = '(/[a-z0-9]+)?' . $cache_dir;
         }
         $first_regex_var = '$2';
     }
     $minify_uri = rtrim(network_site_url('', 'relative'), '/') . '/';
     $engine = $config->get_string('minify.engine');
     $browsercache = $config->get_boolean('browsercache.enabled');
     $compression = $browsercache && $config->get_boolean('browsercache.cssjs.compression') && !defined('W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF');
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_MINIFY_CORE . "\n";
     if ($engine == 'file') {
         $rules .= "set \$w3tc_enc \"\";\n";
         if ($compression) {
             $rules .= "if (\$http_accept_encoding ~ gzip) {\n";
             $rules .= "    set \$w3tc_enc .gzip;\n";
             $rules .= "}\n";
         }
         $rules .= "if (-f \$request_filename\$w3tc_enc) {\n";
         $rules .= "    rewrite (.*) \$1\$w3tc_enc break;\n";
         $rules .= "}\n";
     }
     $rules .= "rewrite ^{$cache_dir}/ {$minify_uri}index.php last;\n";
     $rules .= W3TC_MARKER_END_MINIFY_CORE . "\n";
     return $rules;
 }
Exemple #7
0
/**
 * Get the currently active IP, masked to make it not change when the last
 * octet or two change, for use in session cookies and such
 *
 * @param Config $config
 * @return string
 */
function get_session_ip(Config $config)
{
    $mask = $config->get_string("session_hash_mask", "255.255.0.0");
    $addr = $_SERVER['REMOTE_ADDR'];
    $addr = inet_ntop(inet_pton($addr) & inet_pton($mask));
    return $addr;
}
 /**
  * Generates directives for file cache dir
  *
  * @param Config  $config
  * @return string
  */
 private function rules_cache_generate_nginx($config)
 {
     $cache_dir = Util_Rule::filename_to_uri(W3TC_CACHE_MINIFY_DIR);
     $browsercache = $config->get_boolean('browsercache.enabled');
     $compression = $browsercache && $config->get_boolean('browsercache.cssjs.compression');
     $expires = $browsercache && $config->get_boolean('browsercache.cssjs.expires');
     $lifetime = $browsercache ? $config->get_integer('browsercache.cssjs.lifetime') : 0;
     $cache_control = $browsercache && $config->get_boolean('browsercache.cssjs.cache.control');
     $w3tc = $browsercache && $config->get_integer('browsercache.cssjs.w3tc');
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_MINIFY_CACHE . "\n";
     $common_rules = '';
     if ($expires) {
         $common_rules .= "    expires modified " . $lifetime . "s;\n";
     }
     if ($w3tc) {
         $common_rules .= "    add_header X-Powered-By \"" . Util_Environment::w3tc_header() . "\";\n";
     }
     if ($compression) {
         $common_rules .= "    add_header Vary \"Accept-Encoding\";\n";
     }
     if ($cache_control) {
         $cache_policy = $config->get_string('browsercache.cssjs.cache.policy');
         switch ($cache_policy) {
             case 'cache':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"public\";\n";
                 break;
             case 'cache_public_maxage':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n";
                 break;
             case 'cache_validation':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n";
                 break;
             case 'cache_noproxy':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"private, must-revalidate\";\n";
                 break;
             case 'cache_maxage':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n";
                 break;
             case 'no_cache':
                 $common_rules .= "    add_header Pragma \"no-cache\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n";
                 break;
         }
     }
     $rules .= "location ~ " . $cache_dir . ".*\\.js\$ {\n";
     $rules .= "    types {}\n";
     $rules .= "    default_type application/x-javascript;\n";
     $rules .= $common_rules;
     $rules .= "}\n";
     $rules .= "location ~ " . $cache_dir . ".*\\.css\$ {\n";
     $rules .= "    types {}\n";
     $rules .= "    default_type text/css;\n";
     $rules .= $common_rules;
     $rules .= "}\n";
     if ($compression) {
         $rules .= "location ~ " . $cache_dir . ".*js\\.gzip\$ {\n";
         $rules .= "    gzip off;\n";
         $rules .= "    types {}\n";
         $rules .= "    default_type application/x-javascript;\n";
         $rules .= $common_rules;
         $rules .= "    add_header Content-Encoding gzip;\n";
         $rules .= "}\n";
         $rules .= "location ~ " . $cache_dir . ".*css\\.gzip\$ {\n";
         $rules .= "    gzip off;\n";
         $rules .= "    types {}\n";
         $rules .= "    default_type text/css;\n";
         $rules .= $common_rules;
         $rules .= "    add_header Content-Encoding gzip;\n";
         $rules .= "}\n";
     }
     $rules .= W3TC_MARKER_END_MINIFY_CACHE . "\n";
     return $rules;
 }
 /**
  * Adds cache rules for type to &$rules
  *
  * @param Config  $config
  * @param string  $rules
  * @param array   $mime_types
  * @param string  $section
  * @return void
  */
 private function _rules_cache_generate_nginx_for_type($config, &$rules, $mime_types, $section)
 {
     $expires = $config->get_boolean('browsercache.' . $section . '.expires');
     $cache_control = $config->get_boolean('browsercache.' . $section . '.cache.control');
     $w3tc = $config->get_boolean('browsercache.' . $section . '.w3tc');
     if ($expires || $cache_control || $w3tc) {
         $lifetime = $config->get_integer('browsercache.' . $section . '.lifetime');
         $extensions = array_keys($mime_types);
         // Remove ext from filesmatch if its the same as permalink extension
         $pext = strtolower(pathinfo(get_option('permalink_structure'), PATHINFO_EXTENSION));
         if ($pext) {
             $extensions = $this->_remove_extension_from_list($extensions, $pext);
         }
         $rules .= "location ~ \\.(" . implode('|', $extensions) . ")\$ {\n";
         if ($expires) {
             $rules .= "    expires " . $lifetime . "s;\n";
         }
         $add_header_rules = '';
         if ($cache_control) {
             $cache_policy = $config->get_string('browsercache.' . $section . '.cache.policy');
             switch ($cache_policy) {
                 case 'cache':
                     $add_header_rules .= "    add_header Pragma \"public\";\n";
                     $add_header_rules .= "    add_header Cache-Control \"public\";\n";
                     break;
                 case 'cache_public_maxage':
                     $add_header_rules .= "    add_header Pragma \"public\";\n";
                     $add_header_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n";
                     break;
                 case 'cache_validation':
                     $add_header_rules .= "    add_header Pragma \"public\";\n";
                     $add_header_rules .= "    add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n";
                     break;
                 case 'cache_noproxy':
                     $add_header_rules .= "    add_header Pragma \"public\";\n";
                     $add_header_rules .= "    add_header Cache-Control \"private, must-revalidate\";\n";
                     break;
                 case 'cache_maxage':
                     $add_header_rules .= "    add_header Pragma \"public\";\n";
                     $add_header_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n";
                     break;
                 case 'no_cache':
                     $add_header_rules .= "    add_header Pragma \"no-cache\";\n";
                     $add_header_rules .= "    add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n";
                     break;
             }
         }
         if ($w3tc) {
             $add_header_rules .= "    add_header X-Powered-By \"" . Util_Environment::w3tc_header($config) . "\";\n";
         }
         $rules .= $add_header_rules;
         $rules .= Dispatcher::on_browsercache_rules_generation_for_section($config, false, $section, $add_header_rules);
         if (!$config->get_boolean('browsercache.no404wp')) {
             $wp_uri = network_home_url('', 'relative');
             $wp_uri = rtrim($wp_uri, '/');
             $rules .= '    try_files $uri $uri/ $uri.html ' . $wp_uri . '/index.php?$args;' . "\n";
         }
         $rules .= "}\n";
     }
 }
 /**
  * Generates rules for WP dir
  *
  * @param Config  $config
  * @return string
  */
 private function rules_core_generate_nginx($config)
 {
     $is_network = Util_Environment::is_wpmu();
     $cache_dir = Util_Environment::normalize_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'));
     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 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([0-9]+)/?\$ \$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(array('\\W3TC\\Util_Environment', '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(array('\\W3TC\\Util_Environment', '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'));
         $set_ua_var = true;
         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) {
                 if ($set_ua_var) {
                     $rules .= "set \$w3tc_ua \"\";\n";
                     $set_ua_var = false;
                 }
                 $rules .= "if (\$http_user_agent ~* \"(" . implode('|', $mobile_agents) . ")\") {\n";
                 $rules .= "    set \$w3tc_ua _" . $mobile_group . ";\n";
                 $rules .= "}\n";
                 $env_w3tc_ua = "\$w3tc_ua";
             }
         }
     }
     /**
      * Check for preview cookie
      */
     $rules .= "set \$w3tc_preview \"\";\n";
     $rules .= "if (\$http_cookie ~* \"(w3tc_preview)\") {\n";
     $rules .= "    set \$w3tc_preview _preview;\n";
     $rules .= "}\n";
     $env_w3tc_preview = "\$w3tc_preview";
     /**
      * Check referrer groups
      */
     if ($config->get_boolean('referrer.enabled')) {
         $referrer_groups = array_reverse($config->get_array('referrer.rgroups'));
         $set_ref_var = true;
         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) {
                 if ($set_ref_var) {
                     $rules .= "set \$w3tc_ref \"\";\n";
                     $set_ref_var = false;
                 }
                 $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(Util_Environment::document_root(), '', $cache_dir);
     $uri_prefix = $cache_path . "/\$http_host/" . "\$request_uri/_index" . $env_w3tc_ua . $env_w3tc_ref . $env_w3tc_ssl . $env_w3tc_preview;
     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;
 }
 /**
  * Generates directives for file cache dir
  *
  * @param Config  $config
  * @return string
  */
 private function rules_cache_generate_nginx($config)
 {
     $cache_root = Util_Environment::normalize_path(W3TC_CACHE_PAGE_ENHANCED_DIR);
     $cache_dir = rtrim(str_replace(Util_Environment::document_root(), '', $cache_root), '/');
     if (Util_Environment::is_wpmu()) {
         $cache_dir = preg_replace('~/w3tc.*?/~', '/w3tc.*?/', $cache_dir, 1);
     }
     $browsercache = $config->get_boolean('browsercache.enabled');
     $compression = $browsercache && $config->get_boolean('browsercache.html.compression');
     $expires = $browsercache && $config->get_boolean('browsercache.html.expires');
     $lifetime = $browsercache ? $config->get_integer('browsercache.html.lifetime') : 0;
     $cache_control = $browsercache && $config->get_boolean('browsercache.html.cache.control');
     $w3tc = $browsercache && $config->get_integer('browsercache.html.w3tc');
     $common_rules = '';
     if ($expires) {
         $common_rules .= "    expires modified " . $lifetime . "s;\n";
     }
     if ($w3tc) {
         $common_rules .= "    add_header X-Powered-By \"" . Util_Environment::w3tc_header() . "\";\n";
     }
     if ($expires) {
         $common_rules .= "    add_header Vary \"Accept-Encoding, Cookie\";\n";
     }
     if ($cache_control) {
         $cache_policy = $config->get_string('browsercache.html.cache.policy');
         switch ($cache_policy) {
             case 'cache':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"public\";\n";
                 break;
             case 'cache_public_maxage':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n";
                 break;
             case 'cache_validation':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n";
                 break;
             case 'cache_noproxy':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"private, must-revalidate\";\n";
                 break;
             case 'cache_maxage':
                 $common_rules .= "    add_header Pragma \"public\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n";
                 break;
             case 'no_cache':
                 $common_rules .= "    add_header Pragma \"no-cache\";\n";
                 $common_rules .= "    add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n";
                 break;
         }
     }
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_PGCACHE_CACHE . "\n";
     $rules .= "location ~ " . $cache_dir . ".*html\$ {\n";
     $rules .= $common_rules;
     $rules .= "}\n";
     if ($compression) {
         $rules .= "location ~ " . $cache_dir . ".*gzip\$ {\n";
         $rules .= "    gzip off;\n";
         $rules .= "    types {}\n";
         $rules .= "    default_type text/html;\n";
         $rules .= $common_rules;
         $rules .= "    add_header Content-Encoding gzip;\n";
         $rules .= "}\n";
     }
     $rules .= W3TC_MARKER_END_PGCACHE_CACHE . "\n";
     return $rules;
 }
Exemple #12
0
 /**
  * Save config, can't decline save process. (difference from action_save)
  *
  * Do some actions on config keys update
  * Used in several places such as:
  *
  * 1. common config save
  * 2. import settings
  *
  * @param Config  $current_config
  * @param Config  $new_config
  * @return bool
  * @throws Exception
  */
 public static function config_save($current_config, $new_config)
 {
     $master_config = $new_config->is_master() ? $new_config : Dispatcher::config_master();
     if ($master_config->get_integer('common.instance_id', 0) == 0) {
         $master_config->set('common.instance_id', mt_rand());
         if (!$new_config->is_master()) {
             $master_config->save();
         }
     }
     $old_config = new Config();
     $browsercache_dependencies = array();
     if ($new_config->get_boolean('browsercache.enabled')) {
         $browsercache_dependencies = array_merge($browsercache_dependencies, array('browsercache.rewrite', 'browsercache.cssjs.replace', 'browsercache.html.replace', 'browsercache.other.replace'));
         if ($new_config->get_boolean('browsercache.cssjs.replace')) {
             $browsercache_dependencies = array_merge($browsercache_dependencies, array('browsercache.cssjs.compression', 'browsercache.cssjs.expires', 'browsercache.cssjs.lifetime', 'browsercache.cssjs.cache.control', 'browsercache.cssjs.cache.policy', 'browsercache.cssjs.etag', 'browsercache.cssjs.w3tc'));
         }
         if ($new_config->get_boolean('browsercache.html.replace')) {
             $browsercache_dependencies = array_merge($browsercache_dependencies, array('browsercache.html.compression', 'browsercache.html.expires', 'browsercache.html.lifetime', 'browsercache.html.cache.control', 'browsercache.html.cache.policy', 'browsercache.html.etag', 'browsercache.html.w3tc'));
         }
         if ($new_config->get_boolean('browsercache.other.replace')) {
             $browsercache_dependencies = array_merge($browsercache_dependencies, array('browsercache.other.compression', 'browsercache.other.expires', 'browsercache.other.lifetime', 'browsercache.other.cache.control', 'browsercache.other.cache.policy', 'browsercache.other.etag', 'browsercache.other.w3tc'));
         }
         $old_bc_dependencies_values = array();
         $new_bc_dependencies_values = array();
         foreach ($browsercache_dependencies as $key) {
             $old_bc_dependencies_values[] = $old_config->get($key);
             $new_bc_dependencies_values[] = $new_config->get($key);
         }
         if (serialize($old_bc_dependencies_values) != serialize($new_bc_dependencies_values)) {
             $state_note = Dispatcher::config_state_note();
             $state_note->set('common.show_note.flush_statics_needed', true);
         }
     }
     /**
      * Show need empty page cache notification
      */
     $cache_flush = Dispatcher::component('CacheFlush');
     if ($cache_flush->flushable_posts()) {
         $pgcache_dependencies = array_merge($browsercache_dependencies, array('pgcache.debug', 'pgcache.cache.query', 'pgcache.cache.home', 'pgcache.cache.feed', 'pgcache.cache.nginx_handle_xml', 'pgcache.cache.ssl', 'pgcache.cache.404', 'pgcache.cache.headers', 'pgcache.compatibility', 'pgcache.remove_charset', 'pgcache.accept.uri', 'pgcache.accept.files', 'pgcache.accept.qs', 'pgcache.late_init', 'pgcache.mirrors.enabled', 'pgcache.reject.front_page', 'pgcache.reject.logged', 'pgcache.reject.logged_roles', 'pgcache.reject.uri', 'pgcache.reject.ua', 'pgcache.reject.cookie', 'pgcache.reject.request_head', 'dbcache.enabled', 'objectcache.enabled', 'minify.enabled', 'mobile.enabled', 'referrer.enabled'));
         if ($new_config->get_boolean('pgcache.mirrors.enabled')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('pgcache.mirrors.home_urls'));
         }
         if ($new_config->get_boolean('dbcache.enabled')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('dbcache.debug'));
         }
         if ($new_config->get_boolean('objectcache.enabled')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('objectcache.debug'));
         }
         if ($new_config->get_boolean('minify.enabled')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('minify.auto', 'minify.debug', 'minify.rewrite', 'minify.html.enable', 'minify.html.engine', 'minify.html.inline.css', 'minify.html.inline.js', 'minify.html.strip.crlf', 'minify.html.comments.ignore', 'minify.css.enable', 'minify.css.engine', 'minify.css.groups', 'minify.js.enable', 'minify.js.engine', 'minify.js.groups', 'minify.htmltidy.options.clean', 'minify.htmltidy.options.hide-comments', 'minify.htmltidy.options.wrap', 'minify.reject.logged', 'minify.reject.ua', 'minify.reject.uri'));
         }
         /**
          *
          *
          * @var W3_ModuleStatus $modules
          */
         $modules = Dispatcher::component('ModuleStatus');
         if ($modules->is_running('cdn')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('cdn.enabled', 'cdn.debug', 'cdn.engine', 'cdn.uploads.enable', 'cdn.includes.enable', 'cdn.includes.files', 'cdn.theme.enable', 'cdn.theme.files', 'cdn.minify.enable', 'cdn.custom.enable', 'cdn.custom.files', 'cdn.ftp.domain', 'cdn.ftp.ssl', 'cdn.s3.cname', 'cdn.s3.ssl', 'cdn.cf.cname', 'cdn.cf.ssl', 'cdn.cf2.cname', 'cdn.cf2.ssl', 'cdn.rscf.cname', 'cdn.rscf.ssl', 'cdn.azure.cname', 'cdn.azure.ssl', 'cdn.mirror.domain', 'cdn.mirror.ssl', 'cdn.netdna.domain', 'cdn.netdna.ssl', 'cdn.cotendo.domain', 'cdn.cotendo.ssl', 'cdn.edgecast.domain', 'cdn.edgecast.ssl', 'cdn.att.domain', 'cdn.att.ssl', 'cdn.reject.logged_roles', 'cdn.reject.roles', 'cdn.reject.ua', 'cdn.reject.uri', 'cdn.reject.files'));
         } elseif ($old_config->get_boolean('cdn.enabled') && !$new_config->get_boolean('cdn.enabled')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('cdn.enabled'));
         }
         if ($new_config->get_boolean('mobile.enabled')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('mobile.rgroups'));
         }
         if ($new_config->get_boolean('referrer.enabled')) {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('referrer.rgroups'));
         }
         if ($new_config->get_boolean('browsercache.enabled') && $new_config->get_string('pgcache.engine') == 'file_generic') {
             $pgcache_dependencies = array_merge($pgcache_dependencies, array('browsercache.html.last_modified', 'browsercache.other.last_modified'));
         }
         $old_pgcache_dependencies_values = array();
         $new_pgcache_dependencies_values = array();
         foreach ($pgcache_dependencies as $pgcache_dependency) {
             $old_pgcache_dependencies_values[] = $old_config->get($pgcache_dependency);
             $new_pgcache_dependencies_values[] = $new_config->get($pgcache_dependency);
         }
         if (serialize($old_pgcache_dependencies_values) != serialize($new_pgcache_dependencies_values)) {
             $state_note = Dispatcher::config_state_note();
             $state_note->set('common.show_note.flush_posts_needed', true);
         }
     }
     /**
      * Show need empty minify notification
      */
     if ($current_config->get_boolean('minify.enabled') && $new_config->get_boolean('minify.enabled') && ($new_config->get_boolean('minify.css.enable') && ($new_config->get_boolean('minify.auto') || count($new_config->get_array('minify.css.groups'))) || $new_config->get_boolean('minify.js.enable') && ($new_config->get_boolean('minify.auto') || count($new_config->get_array('minify.js.groups'))))) {
         $minify_dependencies = array_merge($browsercache_dependencies, array('minify.auto', 'minify.debug', 'minify.options', 'minify.symlinks', 'minify.css.enable', 'minify.js.enable'));
         if ($new_config->get_boolean('minify.css.enable') && ($new_config->get_boolean('minify.auto') || count($new_config->get_array('minify.css.groups')))) {
             $minify_dependencies = array_merge($minify_dependencies, array('minify.css.engine', 'minify.css.combine', 'minify.css.strip.comments', 'minify.css.strip.crlf', 'minify.css.imports', 'minify.css.groups', 'minify.yuicss.path.java', 'minify.yuicss.path.jar', 'minify.yuicss.options.line-break', 'minify.csstidy.options.remove_bslash', 'minify.csstidy.options.compress_colors', 'minify.csstidy.options.compress_font-weight', 'minify.csstidy.options.lowercase_s', 'minify.csstidy.options.optimise_shorthands', 'minify.csstidy.options.remove_last_;', 'minify.csstidy.options.case_properties', 'minify.csstidy.options.sort_properties', 'minify.csstidy.options.sort_selectors', 'minify.csstidy.options.merge_selectors', 'minify.csstidy.options.discard_invalid_properties', 'minify.csstidy.options.css_level', 'minify.csstidy.options.preserve_css', 'minify.csstidy.options.timestamp', 'minify.csstidy.options.template'));
         }
         if ($new_config->get_boolean('minify.js.enable') && ($new_config->get_boolean('minify.auto') || count($new_config->get_array('minify.js.groups')))) {
             $minify_dependencies = array_merge($minify_dependencies, array('minify.js.engine', 'minify.js.combine.header', 'minify.js.combine.body', 'minify.js.combine.footer', 'minify.js.strip.comments', 'minify.js.strip.crlf', 'minify.js.groups', 'minify.yuijs.path.java', 'minify.yuijs.path.jar', 'minify.yuijs.options.line-break', 'minify.yuijs.options.nomunge', 'minify.yuijs.options.preserve-semi', 'minify.yuijs.options.disable-optimizations', 'minify.ccjs.path.java', 'minify.ccjs.path.jar', 'minify.ccjs.options.compilation_level', 'minify.ccjs.options.formatting'));
         }
         /**
          *
          *
          * @var W3_ModuleStatus $modules
          */
         $modules = Dispatcher::component('ModuleStatus');
         if ($modules->is_running('cdn')) {
             $minify_dependencies = array_merge($minify_dependencies, array('cdn.engine', 'cdn.enabled'));
         } elseif ($old_config->get_boolean('cdn.enabled') && !$new_config->get_boolean('cdn.enabled')) {
             $minify_dependencies = array_merge($minify_dependencies, array('cdn.enabled'));
         }
         $old_minify_dependencies_values = array();
         $new_minify_dependencies_values = array();
         foreach ($minify_dependencies as $minify_dependency) {
             $old_minify_dependencies_values[] = $old_config->get($minify_dependency);
             $new_minify_dependencies_values[] = $new_config->get($minify_dependency);
         }
         if (serialize($old_minify_dependencies_values) != serialize($new_minify_dependencies_values)) {
             $state_note = Dispatcher::config_state_note();
             $state_note->set('minify.show_note.need_flush', true);
         }
     }
     if ($new_config->get_boolean('cdn.enabled') && !Cdn_Util::is_engine_mirror($new_config->get_string('cdn.engine'))) {
         /**
          * Show notification when CDN enabled
          */
         if (!$old_config->get_boolean('cdn.enabled')) {
             $state = Dispatcher::config_state();
             $state->set('cdn.show_note_cdn_upload', true);
             $state->save();
         }
         /**
          * Show notification when Browser Cache settings changes
          */
         $cdn_dependencies = array('browsercache.enabled');
         if ($new_config->get_boolean('cdn.enabled')) {
             $cdn_dependencies = array('browsercache.cssjs.compression', 'browsercache.cssjs.expires', 'browsercache.cssjs.lifetime', 'browsercache.cssjs.cache.control', 'browsercache.cssjs.cache.policy', 'browsercache.cssjs.etag', 'browsercache.cssjs.w3tc', 'browsercache.html.compression', 'browsercache.html.expires', 'browsercache.html.lifetime', 'browsercache.html.cache.control', 'browsercache.html.cache.policy', 'browsercache.html.etag', 'browsercache.html.w3tc', 'browsercache.other.compression', 'browsercache.other.expires', 'browsercache.other.lifetime', 'browsercache.other.cache.control', 'browsercache.other.cache.policy', 'browsercache.other.etag', 'browsercache.other.w3tc');
         }
         $old_cdn_dependencies_values = array();
         $new_cdn_dependencies_values = array();
         foreach ($cdn_dependencies as $cdn_dependency) {
             $old_cdn_dependencies_values[] = $old_config->get($cdn_dependency);
             $new_cdn_dependencies_values[] = $new_config->get($cdn_dependency);
         }
         if (serialize($old_cdn_dependencies_values) != serialize($new_cdn_dependencies_values)) {
             $state = Dispatcher::config_state();
             $state->set('cdn.show_note_cdn_reupload', true);
             $state->save();
         }
     }
     /**
      * Show need empty object cache notification
      */
     if ($current_config->get_boolean('objectcache.enabled')) {
         $objectcache_dependencies = array('objectcache.groups.global', 'objectcache.groups.nonpersistent');
         $old_objectcache_dependencies_values = array();
         $new_objectcache_dependencies_values = array();
         foreach ($objectcache_dependencies as $objectcache_dependency) {
             $old_objectcache_dependencies_values[] = $old_config->get($objectcache_dependency);
             $new_objectcache_dependencies_values[] = $new_config->get($objectcache_dependency);
         }
         if (serialize($old_objectcache_dependencies_values) != serialize($new_objectcache_dependencies_values)) {
             $state_note = Dispatcher::config_state_note();
             $state_note->set('objectcache.show_note.flush_needed', true);
         }
     }
     do_action('w3tc_saved_options', $new_config);
     /**
      * Save config
      */
     try {
         $new_config->save();
     } catch (\Exception $ex) {
         // try to fix environment, it potentially can be fixed silently
         // dont show error here, it will be called again later
         // in admin_notices
         try {
             $environment = Dispatcher::component('Root_Environment');
             $environment->fix_in_wpadmin($new_config);
         } catch (\Exception $ex) {
         }
         // retry save process and complain now on failure
         try {
             $new_config->save();
         } catch (\Exception $ex) {
             throw new \Exception('<strong>Can\'t change configuration</strong>: ' . $ex->getMessage());
         }
     }
     $w3_plugin_cdn = Dispatcher::component('Cdn_Core_Admin');
     /**
      * Empty caches on engine change or cache enable/disable
      */
     if ($old_config->get_string('pgcache.engine') != $new_config->get_string('pgcache.engine') || $old_config->get_string('pgcache.enabled') != $new_config->get_string('pgcache.enabled')) {
         $pgcacheflush = Dispatcher::component('PgCache_Flush');
         $v = $pgcacheflush->flush();
     }
     if ($old_config->get_string('dbcache.engine') != $new_config->get_string('dbcache.engine') || $old_config->get_string('dbcache.enabled') != $new_config->get_string('dbcache.enabled')) {
         w3tc_dbcache_flush();
     }
     if ($old_config->get_string('objectcache.engine') != $new_config->get_string('objectcache.engine') || $old_config->get_string('objectcache.enabled') != $new_config->get_string('objectcache.enabled')) {
         w3tc_objectcache_flush();
     }
     if ($old_config->get_string('minify.engine') != $new_config->get_string('minify.engine') || $old_config->get_string('minify.enabled') != $new_config->get_string('minify.enabled')) {
         w3tc_minify_flush();
     }
     /**
      * Update CloudFront CNAMEs
      */
     $update_cf_cnames = false;
     if ($new_config->get_boolean('cdn.enabled') && in_array($new_config->get_string('cdn.engine'), array('cf', 'cf2'))) {
         if ($new_config->get_string('cdn.engine') == 'cf') {
             $old_cnames = $old_config->get_array('cdn.cf.cname');
             $new_cnames = $new_config->get_array('cdn.cf.cname');
         } else {
             $old_cnames = $old_config->get_array('cdn.cf2.cname');
             $new_cnames = $new_config->get_array('cdn.cf2.cname');
         }
         if (count($old_cnames) != count($new_cnames) || count(array_diff($old_cnames, $new_cnames))) {
             $update_cf_cnames = true;
         }
     }
     /**
      * Refresh config
      */
     $current_config->load();
     /**
      * React to config changes
      */
     $environment = Dispatcher::component('Root_Environment');
     $environment->fix_on_event($new_config, 'config_change', $old_config);
     /**
      * Update support us option
      */
     Generic_AdminLinks::link_update($current_config);
     /**
      * Auto upload browsercache files to CDN
      */
     if ($new_config->get_boolean('cdn.enabled') && $new_config->get_string('cdn.engine') == 'ftp') {
         Util_Admin::cdn_delete_browsercache($current_config);
         Util_Admin::cdn_upload_browsercache($current_config);
     }
     /**
      * Update CloudFront CNAMEs
      */
     if ($update_cf_cnames) {
         $error = null;
         $w3_plugin_cdn->update_cnames($error);
     }
     return true;
 }
 /**
  *
  *
  * @param Config  $config
  * @return bool
  */
 public static function is_w3tc_enterprise($config = null)
 {
     $result = false;
     if ($config) {
         $result = $config->get_string('plugin.type') == 'enterprise' || defined('W3TC_ENTERPRISE') && W3TC_ENTERPRISE;
     }
     return $result;
 }
 /**
  *
  *
  * @param Config  $config
  */
 private function _setup($config)
 {
     $this->authorized = $config->get_string('cdn.netdna.authorization_key') != '' && $config->get_string('cdn.engine') == 'netdna';
     $keys = explode('+', $config->get_string('cdn.netdna.authorization_key'));
     $this->authorized = $this->authorized && sizeof($keys) == 3;
     $this->have_zone = $config->get_string('cdn.netdna.zone_id') != 0;
 }
Exemple #15
0
 /**
  * If BrowserCache should generate rules specific for CDN. Used with CDN FTP
  *
  * @param Config  $config
  * @return boolean;
  */
 public static function should_browsercache_generate_rules_for_cdn($config)
 {
     if ($config->get_boolean('cdn.enabled') && $config->get_string('cdn.engine') == 'ftp') {
         $cdncommon = Dispatcher::component('Cdn_Core');
         $cdn = $cdncommon->get_cdn();
         $domain = $cdn->get_domain();
         if ($domain) {
             return true;
         }
     }
     return false;
 }
Exemple #16
0
define("VERSION", 'trunk');
require_once "core/util.inc.php";
version_check();
sanitise_environment();
check_cli();
// load base files
$files = array_merge(glob("core/*.php"), glob("ext/*/main.php"));
foreach ($files as $filename) {
    require_once $filename;
}
// connect to database
$database = new Database();
$database->db->fnExecute = '_count_execs';
$config = new Config($database);
// load the theme parts
$_theme = $config->get_string("theme", "default");
if (!file_exists("themes/{$_theme}")) {
    $_theme = "default";
}
require_once "themes/{$_theme}/page.class.php";
require_once "themes/{$_theme}/layout.class.php";
require_once "themes/{$_theme}/themelet.class.php";
$themelets = glob("ext/*/theme.php");
foreach ($themelets as $filename) {
    require_once $filename;
}
$custom_themelets = glob("themes/{$_theme}/*.theme.php");
if ($custom_themelets) {
    $m = array();
    foreach ($custom_themelets as $filename) {
        if (preg_match("/themes\\/{$_theme}\\/(.*)\\.theme\\.php/", $filename, $m) && array_contains($themelets, "ext/{$m[1]}/theme.php")) {