Esempio n. 1
0
 /**
  * Constructor
  */
 function __construct()
 {
     $this->_blog_id = w3_get_blog_id();
     // defines $keys_admin with descriptors
     include W3TC_LIB_W3_DIR . '/ConfigKeys.php';
     $this->_keys = $keys_admin;
     $this->_aggregated_data_object = new W3_ConfigData($keys_admin);
     $this->_aggregated_data_object->set_defaults();
     // load master-config data
     $filename1 = $this->_get_config_filename(true);
     $this->_aggregated_data_object->read($filename1);
     // load blog-config data (or master if we are master)
     $filename2 = $this->_get_config_filename();
     $this->_data_object = new W3_ConfigData($keys_admin);
     // set defaults
     $this->_data_object->set_group($this->_aggregated_data_object->data);
     $data = W3_ConfigData::get_array_from_file($filename2);
     if (!is_null($data)) {
         foreach ($data as $key => $value) {
             if ($this->_writable_key($key)) {
                 $this->_data_object->set($key, $value);
             }
         }
     }
     // merge blog-specific config data to aggregated data
     if ($this->_blog_id > 0) {
         $this->_aggregated_data_object->set_group($this->_data_object->data);
     }
     $this->_data =& $this->_aggregated_data_object->data;
 }
Esempio n. 2
0
 /**
  * Reads legacy config file
  * @param int $blog_id
  * @param bool $force_master
  * @return array
  */
 public function get_imported_legacy_config_keys($blog_id, $force_master = false)
 {
     $suffix = '';
     if ($force_master) {
     } else {
         if ($blog_id > 0) {
             if (w3_is_network()) {
                 if (w3_is_subdomain_install()) {
                     $suffix = '-' . w3_get_domain(w3_get_host());
                 } else {
                     // try subdir blog
                     $request_uri = rtrim($_SERVER['REQUEST_URI'], '/');
                     $site_home_uri = w3_get_base_path();
                     if (substr($request_uri, 0, strlen($site_home_uri)) == $site_home_uri) {
                         $request_path_in_wp = '/' . substr($request_uri, strlen($site_home_uri));
                         $n = strpos($request_path_in_wp, '/', 1);
                         if ($n === false) {
                             $blog_path_in_wp = substr($request_path_in_wp, 1);
                         } else {
                             $blog_path_in_wp = substr($request_path_in_wp, 1, $n - 1);
                         }
                         $suffix = '-' . ($blog_path_in_wp != 'wp-admin' ? $blog_path_in_wp . '.' : '') . w3_get_domain(w3_get_host());
                     }
                 }
             }
         }
     }
     $filename = WP_CONTENT_DIR . '/w3-total-cache-config' . $suffix . '.php';
     $legacy_config = W3_ConfigData::get_array_from_file($filename);
     if (is_array($legacy_config) && isset($legacy_config['pgcache.engine']) && $legacy_config['pgcache.engine'] == 'file_pgcache') {
         $legacy_config['pgcache.engine'] = 'file_generic';
     }
     return $legacy_config;
 }
Esempio n. 3
0
 /**
  * @synopsis [--check] [--default-settings] [--preserve-cdn]
  * @subcommand total-cache
  *
  */
 function w3tc($args, $assoc_args)
 {
     if (!file_exists(WP_CONTENT_DIR . "/plugins/w3-total-cache/")) {
         WP_CLI::warning("W3 Total Cache plugin is not installed into plugins/w3-total-cache/. Skipping.");
         exit;
     }
     if (!is_plugin_active("w3-total-cache/w3-total-cache.php")) {
         WP_CLI::warning("W3 Total Cache plugin is not enabled. Skipping.");
         exit;
     }
     $w3tc_version = get_plugin_data(WP_CONTENT_DIR . "/plugins/w3-total-cache/w3-total-cache.php");
     $w3tc_version = $w3tc_version['Version'];
     $w3tc_master = $w3tc_version >= "0.9.2.6" ? WP_CONTENT_DIR . "/w3tc-config/master.php" : WP_CONTENT_DIR . "/w3-total-cache-config.php";
     if (!file_exists($w3tc_master)) {
         WP_CLI::warning("W3 Total Cache config file can not be found. Skipping.");
         exit;
     }
     if (isset($assoc_args['check'])) {
         $file = fopen($w3tc_master, "r");
         while (!feof($file)) {
             $line = fgets($file);
             if (preg_match('/pgcache\\.enabled\' => true/', $line)) {
                 $pgcache = true;
                 continue;
             }
             if (preg_match('/pgcache\\.engine\' => \'file\'/', $line)) {
                 $pgcache_method = "disk: basic";
                 continue;
             }
             if (preg_match('/pgcache\\.engine\' => \'file_generic\'/', $line)) {
                 $pgcache_method = "disk: enhanced";
                 continue;
             }
             if (preg_match('/pgcache\\.engine\' => \'apc\'/', $line)) {
                 $pgcache_method = "opcode: apc";
                 continue;
             }
             if (preg_match('/minify\\.enabled\' => true/', $line)) {
                 $minify = true;
                 continue;
             }
             if (preg_match('/dbcache\\.enabled\' => true/', $line)) {
                 $dbcache = true;
                 continue;
             }
             if (preg_match('/objectcache\\.enabled\' => true/', $line)) {
                 $objectcache = true;
                 continue;
             }
             if (preg_match('/objectcache\\.engine\' => \'file\'/', $line)) {
                 $objectcache_method = "disk";
                 continue;
             }
             if (preg_match('/objectcache\\.engine\' => \'apc\'/', $line)) {
                 $objectcache_method = "apc";
                 continue;
             }
             if (preg_match('/fragmentcache\\.enabled\' => true/', $line)) {
                 $fragmentcache = true;
                 continue;
             }
             if (preg_match('/fragmentcache\\.engine\' => \'file\'/', $line)) {
                 $fragmentcache_method = "disk";
                 continue;
             }
             if (preg_match('/fragmentcache\\.engine\' => \'apc\'/', $line)) {
                 $fragmentcache_method = "apc";
                 continue;
             }
             if (preg_match('/browsercache\\.enabled\' => true/', $line)) {
                 $browsercache = true;
                 continue;
             }
             if (preg_match('/cdn\\.enabled\' => true/', $line)) {
                 $cdn = true;
                 continue;
             }
         }
         fclose($file);
         echo "W3 Total Cache " . $w3tc_version . " (" . (file_exists("/var/run/apache2.pid") || file_exists("/run/apache2/apache2.pid") ? "apache+nginx" : "nginx+php5-fpm") . ")\n";
         echo "\nPage Cache: " . (isset($pgcache) ? "enabled (" . $pgcache_method . ")" : "-");
         echo "\nMinify: " . (isset($minify) ? "enabled" : "-");
         echo "\nDatabase Cache: " . (isset($dbcache) ? "enabled" : "-");
         echo "\nObject Cache: " . (isset($objectcache) ? "enabled (" . $objectcache_method . ")" : "-");
         echo "\nFragment Cache: " . (isset($fragmentcache) ? "enabled (" . $fragmentcache_method . ")" : "-");
         echo "\nBrowser Cache: " . (isset($browsercache) ? "enabled" : "-");
         echo "\nCDN: " . (isset($cdn) ? "enabled" : "-") . "\n";
     } else {
         if (isset($assoc_args['default-settings'])) {
             $siteurl = preg_replace("/https?:\\/\\/(www.)?/", "", home_url());
             if (strlen($siteurl) == 0 || !is_dir("/var/www/" . $siteurl)) {
                 WP_CLI::error("Unable to find site root. Skipping.");
                 return;
             }
             if (!is_writable("/var/www/" . $siteurl)) {
                 WP_CLI::error("Site root is not writable. Please check the permissions. Skipping.");
                 return;
             }
             $config = file_exists("/var/run/apache2.pid") || file_exists("/run/apache2/apache2.pid") ? "master_apache" : "master_nginx";
             if (file_exists("/tmp/" . $config)) {
                 WP_CLI::launch("rm /tmp/" . $config);
             }
             if (file_exists("/tmp/cdn_settings")) {
                 WP_CLI::launch("rm /tmp/cdn_settings");
             }
             $url = "https://tiagohillebrandt.eti.br/w3tc/" . $w3tc_version . "/" . $config;
             WP_CLI::launch("wget -q " . $url . " -P /tmp/ || echo -n");
             if (!file_exists("/tmp/" . $config)) {
                 WP_CLI::warning("W3TC version not compatible or server not available at this moment. Please import the settings manually.");
                 exit;
             }
             $old_config = null;
             if (isset($assoc_args['preserve-cdn'])) {
                 require_once W3TC_LIB_W3_DIR . "/ConfigData.php";
                 $cdn_url = "https://tiagohillebrandt.eti.br/w3tc/" . $w3tc_version . "/cdn_settings";
                 WP_CLI::launch("wget -q " . $cdn_url . " -P /tmp/ || echo -n");
                 if (!file_exists("/tmp/cdn_settings")) {
                     WP_CLI::warning("Not able to preserve the CDN settings. Skipping.");
                     exit;
                 }
                 $old_config = W3_ConfigData::get_array_from_file($w3tc_master);
             }
             $backup_file = preg_replace("/https?:\\/\\/(www.)?/", "", home_url());
             $backup_file = $backup_file . ".master.php";
             $backup_file = preg_replace("/\\//", "_", $backup_file);
             WP_CLI::launch("mv " . $w3tc_master . " /var/www/" . $siteurl . "/." . $backup_file);
             WP_CLI::line("Current settings backup: /var/www/" . $siteurl . "/." . $backup_file);
             WP_CLI::launch("rm " . WP_CONTENT_DIR . "/advanced-cache.php 2> /dev/null || echo -n");
             WP_CLI::launch("rm " . WP_CONTENT_DIR . "/db.php 2> /dev/null || echo -n");
             WP_CLI::launch("rm " . WP_CONTENT_DIR . "/object-cache.php 2> /dev/null || echo -n");
             WP_CLI::launch("rm -r " . WP_CONTENT_DIR . "/cache/* 2> /dev/null || echo -n");
             WP_CLI::launch("rm -r " . WP_CONTENT_DIR . "/w3tc/* 2> /dev/null || echo -n");
             WP_CLI::launch("mv /tmp/" . $config . " " . $w3tc_master);
             WP_CLI::launch("chown -R \$(stat -c %U " . WP_CONTENT_DIR . "):\$(stat -c %U " . WP_CONTENT_DIR . ") " . $w3tc_master);
             if (!is_null($old_config)) {
                 require_once W3TC_LIB_W3_DIR . "/Config.php";
                 $config = new W3_Config();
                 $cdn = fopen("/tmp/cdn_settings", "r");
                 while (!feof($cdn)) {
                     $property = trim(fgets($cdn));
                     $config->set($property, $old_config[$property]);
                 }
                 fclose($cdn);
                 $config->save();
             }
             WP_CLI::success("W3TC recommended settings have been imported.");
         }
     }
 }
Esempio n. 4
0
 /**
  * Reads config from file using "set" method to fill object with data.
  *
  * @param string $filename
  * @return boolean
  */
 function read($filename)
 {
     $config = W3_ConfigData::get_array_from_file($filename);
     if (is_null($config)) {
         return false;
     }
     foreach ($config as $key => $value) {
         $this->set($key, $value);
     }
     return true;
 }
Esempio n. 5
0
 /**
  * Store instance in master config.
  * @param W3_ConfigData $compiled_config
  */
 private function _put_instance_value_in_config($compiled_config)
 {
     if (!isset($compiled_config->data['common.instance_id']) || $compiled_config->data['common.instance_id'] == 0) {
         // read common.instance_id from master config
         /**
          * @var $keys
          */
         include W3TC_LIB_W3_DIR . '/ConfigKeys.php';
         $config2 = new W3_ConfigData($keys);
         $config2->read($this->_get_config_filename(true, false));
         if (isset($config2->data['common.instance_id']) && $config2->data['common.instance_id'] != 0) {
             $compiled_config->data['common.instance_id'] = $config2->data['common.instance_id'];
         }
     }
 }
 /**
  * Overloads the current ConfigWriter with found legacy values and saves to config file.
  */
 public function import_legacy_config_and_save()
 {
     if ($this->own_config_exists()) {
         return;
     }
     /**
      * defines $keys with descriptors
      * @var array $keys config keys
      */
     include W3TC_LIB_W3_DIR . '/ConfigKeys.php';
     $compiled_config = new W3_ConfigData($keys);
     $compiled_config->set_defaults();
     $config_admin = w3_instance('W3_ConfigAdmin');
     $data = $this->_import_legacy_config($compiled_config);
     if (!is_null($data)) {
         $master_data = array();
         if ($this->_blog_id != 0 && w3_is_network()) {
             $master_config = new W3_ConfigData($keys);
             $master_config->read($this->_get_config_filename(true));
             $master_data = $master_config->data;
         }
         foreach ($data as $key => $value) {
             if ($master_data && isset($master_data[$key]) && $master_data[$key] === $value) {
                 continue;
             }
             if (!$this->_key_sealed($key, $this->_data->data, $config_admin, $value)) {
                 $this->_data->set($key, $value);
             }
         }
         // Since configs don't exist create them.
         $this->save();
         $config_admin->save();
     }
 }