Example #1
0
 /**
  * Loads and configures NetDNA widget to be used in WP Dashboards.
  * @param $widget_id
  * @param array $form_inputs
  */
 function widget_netdna($widget_id, $form_inputs = array())
 {
     $authorized = $this->authorized;
     $have_zone = $this->have_zone;
     $is_sealed = $this->_sealed;
     if ($authorized && $have_zone) {
         $zone_id = $this->_config->get_integer('cdn.netdna.zone_id');
         $zone_info = $this->api->get_pull_zone($zone_id);
         if ($zone_info) {
             $content_zone = $zone_info['name'];
             $summary = $this->api->get_stats_per_zone($zone_id);
             $filetypes = $this->api->get_list_of_file_types_per_zone($zone_id);
             $popular_files = $this->api->get_list_of_popularfiles_per_zone($zone_id);
             $popular_files = NetDNAPresentation::format_popular($popular_files);
             $popular_files = array_slice($popular_files, 0, 5);
             $account = $this->api->get_account();
             $account_status = NetDNAPresentation::get_account_status($account['status']);
             include W3TC_INC_WIDGET_DIR . '/netdna.php';
         } else {
             include W3TC_INC_WIDGET_DIR . '/netdna_signup.php';
         }
     } else {
         include W3TC_INC_WIDGET_DIR . '/netdna_signup.php';
     }
 }
Example #2
0
 /**
  * CDN tab
  *
  * @return void
  */
 function view()
 {
     $config = Dispatcher::config();
     $cdn_engine = $config->get_string('cdn.engine');
     if (Cdn_Util::is_engine_fsd($cdn_engine)) {
         do_action('w3tc_settings_cdn');
         return;
     }
     $cdn_enabled = $config->get_boolean('cdn.enabled');
     $cdn_mirror = Cdn_Util::is_engine_mirror($cdn_engine);
     $cdn_mirror_purge_all = Cdn_Util::can_purge_all($cdn_engine);
     $cdn_common = Dispatcher::component('Cdn_Core');
     $cdn = $cdn_common->get_cdn();
     $cdn_supports_header = $cdn->headers_support() == W3TC_CDN_HEADER_MIRRORING;
     $minify_enabled = $config->get_boolean('minify.enabled') && Util_Rule::can_check_rules() && $config->get_boolean('minify.rewrite') && (!$config->get_boolean('minify.auto') || Cdn_Util::is_engine_mirror($config->get_string('cdn.engine')));
     $cookie_domain = $this->get_cookie_domain();
     $set_cookie_domain = $this->is_cookie_domain_enabled();
     // Required for Update Media Query String button
     $browsercache_enabled = $config->get_boolean('browsercache.enabled');
     $browsercache_update_media_qs = $config->get_boolean('browsercache.cssjs.replace') || $config->get_boolean('browsercache.other.replace');
     if (in_array($cdn_engine, array('netdna', 'maxcdn'))) {
         $pull_zones = array();
         $authorization_key = $config->get_string("cdn.{$cdn_engine}.authorization_key");
         $zone_id = $config->get_integer("cdn.{$cdn_engine}.zone_id");
         $alias = $consumerkey = $consumersecret = '';
         if ($authorization_key) {
             $keys = explode('+', $authorization_key);
             if (sizeof($keys) == 3) {
                 list($alias, $consumerkey, $consumersecret) = $keys;
             }
         }
         $authorized = $authorization_key != '' && $alias && $consumerkey && $consumersecret;
         $have_zone = $zone_id != 0;
         if ($authorized) {
             require_once W3TC_LIB_NETDNA_DIR . '/NetDNA.php';
             try {
                 $api = new \NetDNA($alias, $consumerkey, $consumersecret);
                 $pull_zones = $api->get_zones_by_url(get_home_url());
             } catch (\Exception $ex) {
                 Util_Ui::error_box('<p>There is an error with your CDN settings: ' . $ex->getMessage() . '</p>');
             }
         }
     }
     include W3TC_INC_DIR . '/options/cdn.php';
 }
Example #3
0
 /**
  * CDN tab
  *
  * @return void
  */
 function view()
 {
     w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/ui.php');
     $cdn_enabled = $this->_config->get_boolean('cdn.enabled');
     $cdn_engine = $this->_config->get_string('cdn.engine');
     $cdn_mirror = w3_is_cdn_mirror($cdn_engine);
     $cdn_mirror_purge_all = w3_cdn_can_purge_all($cdn_engine);
     $cdn_common = w3_instance('W3_Plugin_CdnCommon');
     $cdn = $cdn_common->get_cdn();
     $cdn_supports_header = $cdn->headers_support() == W3TC_CDN_HEADER_MIRRORING;
     $cdn_supports_full_page_mirroring = $cdn->supports_full_page_mirroring();
     $minify_enabled = $this->_config->get_boolean('minify.enabled') && $this->_config->get_boolean('minify.rewrite') && (!$this->_config->get_boolean('minify.auto') || w3_is_cdn_mirror($this->_config->get_string('cdn.engine')));
     $cookie_domain = $this->get_cookie_domain();
     $set_cookie_domain = $this->is_cookie_domain_enabled();
     // Required for Update Media Query String button
     $browsercache_enabled = $this->_config->get_boolean('browsercache.enabled');
     $browsercache_update_media_qs = $this->_config->get_boolean('browsercache.cssjs.replace') || $this->_config->get_boolean('browsercache.other.replace');
     if (in_array($cdn_engine, array('netdna', 'maxcdn'))) {
         $pull_zones = array();
         $authorization_key = $this->_config->get_string("cdn.{$cdn_engine}.authorization_key");
         $zone_id = $this->_config->get_integer("cdn.{$cdn_engine}.zone_id");
         $alias = $consumerkey = $consumersecret = '';
         if ($authorization_key) {
             $keys = explode('+', $authorization_key);
             if (sizeof($keys) == 3) {
                 list($alias, $consumerkey, $consumersecret) = $keys;
             }
         }
         $authorized = $authorization_key != '' && $alias && $consumerkey && $consumersecret;
         $have_zone = $zone_id != 0;
         if ($authorized) {
             w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
             try {
                 $api = new NetDNA($alias, $consumerkey, $consumersecret);
                 $pull_zones = $api->get_zones_by_url(w3_get_home_url());
             } catch (Exception $ex) {
                 w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/ui.php');
                 w3_e_error_box('<p>There is an error with your CDN settings: ' . $ex->getMessage() . '</p>');
             }
         }
     }
     include W3TC_INC_DIR . '/options/cdn.php';
 }
 /**
  * Flushes CDN completely
  */
 function flush_all()
 {
     if (empty($this->api_key) || empty($this->zone_id)) {
         throw new \Exception(__('API key not specified.', 'w3-total-cache'));
     }
     if (!class_exists('NetDNA')) {
         require_once W3TC_LIB_NETDNA_DIR . '/NetDNA.php';
     }
     $api = \NetDNA::create($this->api_key);
     $api->cache_delete($this->zone_id);
 }
Example #5
0
 /**
  * Loads and configures NetDNA widget to be used in WP Dashboards.
  * @param $widget_id
  * @param array $form_inputs
  */
 function widget_maxcdn($widget_id, $form_inputs = array())
 {
     w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/ui.php');
     $authorized = $this->authorized;
     $have_zone = $this->have_zone;
     $error = '';
     $no_zone = $this->_config->get_integer('cdn.maxcdn.zone_id') == 0;
     $is_sealed = $this->_sealed;
     $pull_zones = array();
     $zone_info = false;
     if ($this->authorized && $this->have_zone) {
         $zone_id = $this->_config->get_integer('cdn.maxcdn.zone_id');
         try {
             $zone_info = $this->api->get_pull_zone($zone_id);
         } catch (Exception $ex) {
             $zone_info = false;
             $error = $ex->getMessage();
         }
         if ($zone_info) {
             $content_zone = $zone_info['name'];
             try {
                 $summary = $this->api->get_stats_per_zone($zone_id);
                 $filetypes = $this->api->get_list_of_file_types_per_zone($zone_id);
                 $popular_files = $this->api->get_list_of_popularfiles_per_zone($zone_id);
                 $popular_files = NetDNAPresentation::format_popular($popular_files);
                 $popular_files = array_slice($popular_files, 0, 5);
                 $account = $this->api->get_account();
                 $account_status = NetDNAPresentation::get_account_status($account['status']);
                 include W3TC_INC_WIDGET_DIR . '/maxcdn.php';
             } catch (Exception $ex) {
                 $error = $ex->getMessage();
                 try {
                     $pull_zones = $this->api->get_zones_by_url(home_url());
                 } catch (Exception $ex) {
                 }
                 include W3TC_INC_WIDGET_DIR . '/maxcdn_signup.php';
             }
         } else {
             try {
                 $pull_zones = $this->api->get_zones_by_url(home_url());
             } catch (Exception $ex) {
             }
             include W3TC_INC_WIDGET_DIR . '/maxcdn_signup.php';
         }
     } else {
         include W3TC_INC_WIDGET_DIR . '/maxcdn_signup.php';
     }
 }
 private function _w3tc_save_options_process()
 {
     $data = array('old_config' => $this->_config, 'response_query_string' => array(), 'response_actions' => array(), 'response_errors' => array(), 'response_notes' => array('config_save'));
     // if we are on extension settings page - stay on the same page
     if (Util_Request::get_string('page') == 'w3tc_extensions') {
         $data['response_query_string']['page'] = Util_Request::get_string('page');
         $data['response_query_string']['extension'] = Util_Request::get_string('extension');
         $data['response_query_string']['action'] = Util_Request::get_string('action');
     }
     $capability = apply_filters('w3tc_capability_config_save', 'manage_options');
     if (!current_user_can($capability)) {
         wp_die(__('You do not have the rights to perform this action.', 'w3-total-cache'));
     }
     /**
      * Read config
      * We should use new instance of WP_Config object here
      */
     $config = new Config();
     $this->read_request($config);
     if ($this->_page == 'w3tc_dashboard') {
         if (Util_Request::get_boolean('maxcdn')) {
             $config->set('cdn.enabled', true);
             $config->set('cdn.engine', 'maxcdn');
         }
     }
     /**
      * General tab
      */
     if ($this->_page == 'w3tc_general') {
         $file_nfs = Util_Request::get_boolean('file_nfs');
         $file_locking = Util_Request::get_boolean('file_locking');
         $config->set('pgcache.file.nfs', $file_nfs);
         $config->set('minify.file.nfs', $file_nfs);
         $config->set('dbcache.file.locking', $file_locking);
         $config->set('objectcache.file.locking', $file_locking);
         $config->set('pgcache.file.locking', $file_locking);
         $config->set('minify.file.locking', $file_locking);
         if (is_network_admin()) {
             if ($this->_config->get_boolean('common.force_master') !== $config->get_boolean('common.force_master')) {
                 // blogmap is wrong so empty it
                 @unlink(W3TC_CACHE_BLOGMAP_FILENAME);
                 $blogmap_dir = dirname(W3TC_CACHE_BLOGMAP_FILENAME) . '/' . basename(W3TC_CACHE_BLOGMAP_FILENAME, '.php') . '/';
                 if (@is_dir($blogmap_dir)) {
                     Util_File::rmdir($blogmap_dir);
                 }
             }
         }
         /**
          * Check permalinks for page cache
          */
         if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_generic' && !get_option('permalink_structure')) {
             $config->set('pgcache.enabled', false);
             $data['response_errors'][] = 'fancy_permalinks_disabled_pgcache';
         }
         if (!Util_Environment::is_w3tc_pro($this->_config)) {
             delete_transient('w3tc_license_status');
         }
     }
     /**
      * Minify tab
      */
     if ($this->_page == 'w3tc_minify' && !$this->_config->get_boolean('minify.auto')) {
         $js_groups = array();
         $css_groups = array();
         $js_files = Util_Request::get_array('js_files');
         $css_files = Util_Request::get_array('css_files');
         foreach ($js_files as $theme => $templates) {
             foreach ($templates as $template => $locations) {
                 foreach ((array) $locations as $location => $types) {
                     foreach ((array) $types as $files) {
                         foreach ((array) $files as $file) {
                             if (!empty($file)) {
                                 $js_groups[$theme][$template][$location]['files'][] = Util_Environment::normalize_file_minify($file);
                             }
                         }
                     }
                 }
             }
         }
         foreach ($css_files as $theme => $templates) {
             foreach ($templates as $template => $locations) {
                 foreach ((array) $locations as $location => $files) {
                     foreach ((array) $files as $file) {
                         if (!empty($file)) {
                             $css_groups[$theme][$template][$location]['files'][] = Util_Environment::normalize_file_minify($file);
                         }
                     }
                 }
             }
         }
         $config->set('minify.js.groups', $js_groups);
         $config->set('minify.css.groups', $css_groups);
         $js_theme = Util_Request::get_string('js_theme');
         $css_theme = Util_Request::get_string('css_theme');
         $data['response_query_string']['js_theme'] = $js_theme;
         $data['response_query_string']['css_theme'] = $css_theme;
     }
     /**
      * Browser Cache tab
      */
     if ($this->_page == 'w3tc_browsercache') {
         if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.no404wp') && !get_option('permalink_structure')) {
             $config->set('browsercache.no404wp', false);
             $data['response_errors'][] = 'fancy_permalinks_disabled_browsercache';
         }
         // todo: move to cdn module
         if (in_array($engine = $this->_config->get_string('cdn.engine'), array('netdna', 'maxcdn'))) {
             require_once W3TC_LIB_NETDNA_DIR . '/NetDNA.php';
             $keys = explode('+', $this->_config->get_string('cdn.' . $engine . '.authorization_key'));
             if (sizeof($keys) == 3) {
                 list($alias, $consumerkey, $consumersecret) = $keys;
                 try {
                     $api = new \NetDNA($alias, $consumerkey, $consumersecret);
                     $disable_cooker_header = $config->get_boolean('browsercache.other.nocookies') || $config->get_boolean('browsercache.cssjs.nocookies');
                     $api->update_pull_zone($this->_config->get_string('cdn.' . $engine . '.zone_id'), array('ignore_setcookie_header' => $disable_cooker_header));
                 } catch (\Exception $ex) {
                 }
             }
         }
     }
     /**
      * Mobile tab
      */
     if ($this->_page == 'w3tc_mobile') {
         $groups = Util_Request::get_array('mobile_groups');
         $mobile_groups = array();
         $cached_mobile_groups = array();
         foreach ($groups as $group => $group_config) {
             $group = strtolower($group);
             $group = preg_replace('~[^0-9a-z_]+~', '_', $group);
             $group = trim($group, '_');
             if ($group) {
                 $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default';
                 $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true;
                 $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : '';
                 $agents = isset($group_config['agents']) ? explode("\r\n", trim($group_config['agents'])) : array();
                 $mobile_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'agents' => $agents);
                 $cached_mobile_groups[$group] = $agents;
             }
         }
         /**
          * Allow plugins modify WPSC mobile groups
          */
         $cached_mobile_groups = apply_filters('cached_mobile_groups', $cached_mobile_groups);
         /**
          * Merge existent and delete removed groups
          */
         foreach ($mobile_groups as $group => $group_config) {
             if (isset($cached_mobile_groups[$group])) {
                 $mobile_groups[$group]['agents'] = (array) $cached_mobile_groups[$group];
             } else {
                 unset($mobile_groups[$group]);
             }
         }
         /**
          * Add new groups
          */
         foreach ($cached_mobile_groups as $group => $agents) {
             if (!isset($mobile_groups[$group])) {
                 $mobile_groups[$group] = array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => $agents);
             }
         }
         /**
          * Allow plugins modify W3TC mobile groups
          */
         $mobile_groups = apply_filters('w3tc_mobile_groups', $mobile_groups);
         /**
          * Sanitize mobile groups
          */
         foreach ($mobile_groups as $group => $group_config) {
             $mobile_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => array()), $group_config);
             $mobile_groups[$group]['agents'] = array_unique($mobile_groups[$group]['agents']);
             $mobile_groups[$group]['agents'] = array_map('strtolower', $mobile_groups[$group]['agents']);
             sort($mobile_groups[$group]['agents']);
         }
         $enable_mobile = false;
         foreach ($mobile_groups as $group_config) {
             if ($group_config['enabled']) {
                 $enable_mobile = true;
                 break;
             }
         }
         $config->set('mobile.enabled', $enable_mobile);
         $config->set('mobile.rgroups', $mobile_groups);
     }
     /**
      * Referrer tab
      */
     if ($this->_page == 'w3tc_referrer') {
         $groups = Util_Request::get_array('referrer_groups');
         $referrer_groups = array();
         foreach ($groups as $group => $group_config) {
             $group = strtolower($group);
             $group = preg_replace('~[^0-9a-z_]+~', '_', $group);
             $group = trim($group, '_');
             if ($group) {
                 $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default';
                 $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true;
                 $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : '';
                 $referrers = isset($group_config['referrers']) ? explode("\r\n", trim($group_config['referrers'])) : array();
                 $referrer_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'referrers' => $referrers);
             }
         }
         /**
          * Allow plugins modify W3TC referrer groups
          */
         $referrer_groups = apply_filters('w3tc_referrer_groups', $referrer_groups);
         /**
          * Sanitize mobile groups
          */
         foreach ($referrer_groups as $group => $group_config) {
             $referrer_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'referrers' => array()), $group_config);
             $referrer_groups[$group]['referrers'] = array_unique($referrer_groups[$group]['referrers']);
             $referrer_groups[$group]['referrers'] = array_map('strtolower', $referrer_groups[$group]['referrers']);
             sort($referrer_groups[$group]['referrers']);
         }
         $enable_referrer = false;
         foreach ($referrer_groups as $group_config) {
             if ($group_config['enabled']) {
                 $enable_referrer = true;
                 break;
             }
         }
         $config->set('referrer.enabled', $enable_referrer);
         $config->set('referrer.rgroups', $referrer_groups);
     }
     /**
      * CDN tab
      */
     if ($this->_page == 'w3tc_cdn') {
         $cdn_cnames = Util_Request::get_array('cdn_cnames');
         $cdn_domains = array();
         foreach ($cdn_cnames as $cdn_cname) {
             $cdn_cname = trim($cdn_cname);
             /**
              * Auto expand wildcard domain to 10 subdomains
              */
             $matches = null;
             if (preg_match('~^\\*\\.(.*)$~', $cdn_cname, $matches)) {
                 $cdn_domains = array();
                 for ($i = 1; $i <= 10; $i++) {
                     $cdn_domains[] = sprintf('cdn%d.%s', $i, $matches[1]);
                 }
                 break;
             }
             if ($cdn_cname) {
                 $cdn_domains[] = $cdn_cname;
             }
         }
         switch ($this->_config->get_string('cdn.engine')) {
             case 'ftp':
                 $config->set('cdn.ftp.domain', $cdn_domains);
                 break;
             case 's3':
             case 's3_compatible':
                 $config->set('cdn.s3.cname', $cdn_domains);
                 break;
             case 'cf':
                 $config->set('cdn.cf.cname', $cdn_domains);
                 break;
             case 'cf2':
                 $config->set('cdn.cf2.cname', $cdn_domains);
                 break;
             case 'rackspace_cdn':
                 $config->set('cdn.rackspace_cdn.domains', $cdn_domains);
                 break;
             case 'rscf':
                 $config->set('cdn.rscf.cname', $cdn_domains);
                 break;
             case 'azure':
                 $config->set('cdn.azure.cname', $cdn_domains);
                 break;
             case 'mirror':
                 $config->set('cdn.mirror.domain', $cdn_domains);
                 break;
             case 'maxcdn':
                 $config->set('cdn.maxcdn.domain', $cdn_domains);
                 break;
             case 'netdna':
                 $config->set('cdn.netdna.domain', $cdn_domains);
                 break;
             case 'cotendo':
                 $config->set('cdn.cotendo.domain', $cdn_domains);
                 break;
             case 'edgecast':
                 $config->set('cdn.edgecast.domain', $cdn_domains);
                 break;
             case 'att':
                 $config->set('cdn.att.domain', $cdn_domains);
                 break;
             case 'akamai':
                 $config->set('cdn.akamai.domain', $cdn_domains);
                 break;
             case 'highwinds':
                 $config->set('cdn.highwinds.host.domains', $cdn_domains);
                 break;
         }
     }
     $old_ext_settings = $this->_config->get_array('extensions.settings', array());
     $new_ext_settings = $old_ext_settings;
     $modified = false;
     $extensions = Extensions_Util::get_extensions($config);
     foreach ($extensions as $extension => $descriptor) {
         $request = Util_Request::get_as_array('extensions.settings.' . $extension . '.');
         if (count($request) > 0) {
             if (!isset($new_ext_settings[$extension])) {
                 $new_ext_settings[$extension] = array();
             }
             foreach ($request as $key => $value) {
                 if (!isset($old_ext_settings[$extension]) || !isset($old_ext_settings[$extension][$key]) || $old_ext_settings[$extension][$key] != $value) {
                     $new_ext_settings[$extension][$key] = $value;
                     $modified = true;
                 }
             }
         }
     }
     if ($modified) {
         $config->set("extensions.settings", $new_ext_settings);
     }
     $data['new_config'] = $config;
     $data = apply_filters('w3tc_save_options', $data);
     $config = $data['new_config'];
     do_action('w3tc_config_ui_save', $config, $this->_config);
     do_action("w3tc_config_ui_save-{$this->_page}", $config, $this->_config);
     Util_Admin::config_save($this->_config, $config);
     if ($this->_page == 'w3tc_cdn') {
         /**
          * Handle Set Cookie Domain
          */
         $set_cookie_domain_old = Util_Request::get_boolean('set_cookie_domain_old');
         $set_cookie_domain_new = Util_Request::get_boolean('set_cookie_domain_new');
         if ($set_cookie_domain_old != $set_cookie_domain_new) {
             if ($set_cookie_domain_new) {
                 if (!$this->enable_cookie_domain()) {
                     Util_Admin::redirect(array_merge($data['response_query_string'], array('w3tc_error' => 'enable_cookie_domain')));
                 }
             } else {
                 if (!$this->disable_cookie_domain()) {
                     Util_Admin::redirect(array_merge($data['response_query_string'], array('w3tc_error' => 'disable_cookie_domain')));
                 }
             }
         }
     }
     return array('query_string' => $data['response_query_string'], 'actions' => $data['response_actions'], 'errors' => $data['response_errors'], 'notes' => $data['response_notes']);
 }
 /**
  * Create NetDNA/MaxCDN pullzone
  */
 function action_cdn_create_netdna_maxcdn_pull_zone()
 {
     w3_require_once(W3TC_LIB_W3_DIR . '/Request.php');
     $type = W3_Request::get_string('type');
     $name = W3_Request::get_string('name');
     $label = W3_Request::get_string('label');
     w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
     $cdn_engine = $this->_config->get_string('cdn.engine');
     $authorization_key = $this->_config->get_string("cdn.{$cdn_engine}.authorization_key");
     $alias = $consumerkey = $consumersecret = '';
     if ($authorization_key) {
         $keys = explode('+', $authorization_key);
         if (sizeof($keys) == 3) {
             list($alias, $consumerkey, $consumersecret) = $keys;
         }
     }
     $api = new NetDNA($alias, $consumerkey, $consumersecret);
     $url = w3_get_home_url();
     $zone = array();
     $zone['name'] = $name;
     $zone['label'] = $label;
     $zone['url'] = $url;
     try {
         $response = $api->create_pull_zone($zone);
         try {
             $this->_config->set('cdn.enabled', true);
             if (!$this->_config->get_array("cdn.{$cdn_engine}.domain")) {
                 $this->_config->set("cdn.{$cdn_engine}.domain", array("{$name}.{$alias}.netdna-cdn.com"));
             }
             $this->_config->save();
         } catch (Exception $ex) {
         }
         echo json_encode(array('status' => 'success', 'message' => 'Pull Zone created.', 'temporary_url' => "{$name}.{$alias}.netdna-cdn.com", 'data' => $response));
     } catch (Exception $ex) {
         echo json_encode(array('status' => 'error', 'message' => $ex->getMessage()));
     }
 }
Example #8
0
 /**
  * Purge CDN completely
  * @param $results
  * @return bool
  */
 function purge_all(&$results)
 {
     if (empty($this->_config['authorization_key'])) {
         $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, __('Empty Authorization Key.', 'w3-total-cache'));
         return false;
     }
     if (empty($this->_config['alias']) || empty($this->_config['consumerkey']) || empty($this->_config['consumersecret'])) {
         $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, __('Malformed Authorization Key.', 'w3-total-cache'));
         return false;
     }
     if (!class_exists('NetDNA')) {
         w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
     }
     $api = new NetDNA($this->_config['alias'], $this->_config['consumerkey'], $this->_config['consumersecret']);
     $results = array();
     $local_path = $remote_path = '';
     $domain_is_valid = 0;
     $found_domain = false;
     try {
         if ($this->_config['zone_id'] != 0) {
             $zone_id = $this->_config['zone_id'];
         } else {
             $zone_id = $api->get_zone_id(w3_get_home_url());
         }
         if ($zone_id == 0) {
             $zone_id = $api->get_zone_id(w3_get_domain_url());
         }
         if ($zone_id == 0) {
             $zone_id = $api->get_zone_id(str_replace('://', '://www.', w3_get_domain_url()));
         }
         if ($zone_id == 0 || is_null($zone_id)) {
             if (w3_get_domain_url() == w3_get_home_url()) {
                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/')));
             } else {
                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s or %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/'), trim(w3_get_domain_url(), '/')));
             }
             return !$this->_is_error($results);
         }
         $pullzone = json_decode($api->get('/zones/pull.json/' . $zone_id));
         try {
             if (preg_match("(200|201)", $pullzone->code)) {
                 $custom_domains_full = json_decode($api->get('/zones/pull/' . $pullzone->data->pullzone->id . '/customdomains.json'));
                 $custom_domains = array();
                 foreach ($custom_domains_full->data->customdomains as $custom_domain_full) {
                     $custom_domains[] = $custom_domain_full->custom_domain;
                 }
                 $local_path = 'all';
                 $remote_path = 'all';
                 $domain_is_valid = 0;
                 $found_domain = false;
                 if ($pullzone->data->pullzone->name . '.' . $this->_config['alias'] . '.' . W3TC_CDN_NETDNA_URL === $this->get_domain($local_path) || in_array($this->get_domain($local_path), $custom_domains)) {
                     try {
                         $params = array('file' => '/' . $local_path);
                         $file_purge = json_decode($api->delete('/zones/pull.json/' . $pullzone->data->pullzone->id . '/cache'));
                         if (preg_match("(200|201)", $file_purge->code)) {
                             $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, __('OK', 'w3-total-cache'));
                         } else {
                             if (preg_match("(401|500)", $file_purge->code)) {
                                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('Failed with error code %s. Please check your alias, consumer key, and private key.', 'w3-total-cache'), $file_purge->code));
                             } else {
                                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('Failed with error code ', 'w3-total-cache') . $file_purge->code);
                             }
                         }
                         $found_domain = true;
                     } catch (CurlException $e) {
                         $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, sprintf(__('Unable to purge (%s).', 'w3-total-cache'), $e->getMessage()));
                     }
                 } else {
                     $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No registered CNAMEs match %s.', 'w3-total-cache'), $this->get_domain($local_path)));
                     return !$this->_is_error($results);
                 }
             } else {
                 if (preg_match("(401|500)", $pullzone->code)) {
                     $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('Failed with error code %s. Please check your alias, consumer key, and private key.', 'w3-total-cache'), $pullzone->code));
                 } else {
                     $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('Failed with error code ', 'w3-total-cache') . $pullzone->code);
                 }
             }
         } catch (CurlException $e) {
             $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, sprintf(__('Unable to purge (%s).', 'w3-total-cache'), $e->getMessage()));
         }
     } catch (CurlException $e) {
         $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, __('Failure to pull zone: ', 'w3-total-cache') . $e->getMessage());
     }
     if ($domain_is_valid > 0 && !$found_domain) {
         $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, __('No zones match custom domain.', 'w3-total-cache'));
     } elseif (!$found_domain) {
         $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s.', 'w3-total-cache'), trim(w3_get_home_url(), '/')));
     }
     return !$this->_is_error($results);
 }
Example #9
0
 /**
  * Options save action
  *
  * @return void
  */
 function action_save_options()
 {
     if (!current_user_can('manage_options')) {
         wp_die(__('You do not have the rights to perform this action.', 'w3-total-cache'));
     }
     /**
      * Redirect params
      */
     $params = array();
     /**
      * Store error message regarding permalink not enabled
      */
     $redirect_permalink_error = '';
     /**
      * Read config
      * We should use new instance of WP_Config object here
      */
     $config = new W3_Config();
     $this->read_request($config);
     $config_admin = new W3_ConfigAdmin();
     $this->read_request($config_admin);
     if ($this->_page == 'w3tc_dashboard') {
         if (W3_Request::get_boolean('maxcdn')) {
             $config->set('cdn.enabled', true);
             $config->set('cdn.engine', 'maxcdn');
         }
     }
     /**
      * General tab
      */
     if ($this->_page == 'w3tc_general') {
         $file_nfs = W3_Request::get_boolean('file_nfs');
         $file_locking = W3_Request::get_boolean('file_locking');
         $config->set('pgcache.file.nfs', $file_nfs);
         $config->set('minify.file.nfs', $file_nfs);
         $config->set('dbcache.file.locking', $file_locking);
         $config->set('objectcache.file.locking', $file_locking);
         $config->set('pgcache.file.locking', $file_locking);
         $config->set('minify.file.locking', $file_locking);
         if (is_network_admin()) {
             if ($this->_config->get_boolean('common.force_master') !== $config->get_boolean('common.force_master') || !w3_force_master() && $this->_config->get_boolean('common.force_master') && $config->get_boolean('common.force_master') || w3_force_master() && !$this->_config->get_boolean('common.force_master') && !$config->get_boolean('common.force_master')) {
                 @unlink(W3TC_CACHE_BLOGMAP_FILENAME);
                 $blogmap_dir = dirname(W3TC_CACHE_BLOGMAP_FILENAME) . '/' . basename(W3TC_CACHE_BLOGMAP_FILENAME, '.php') . '/';
                 if (@is_dir($blogmap_dir)) {
                     w3_rmdir($blogmap_dir);
                 }
             }
             if ($config->get_boolean('common.force_master')) {
                 $config_admin->set('common.visible_by_master_only', true);
             }
         }
         /**
          * Check permalinks for page cache
          */
         if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_generic' && !get_option('permalink_structure')) {
             $config->set('pgcache.enabled', false);
             $redirect_permalink_error = 'fancy_permalinks_disabled_pgcache';
         }
         /**
          * Get New Relic application id
          */
         if ($config->get_boolean('newrelic.enabled')) {
             $method = W3_Request::get_string('application_id_method');
             $newrelic_prefix = '';
             if (w3_is_network() && w3_get_blog_id() != 0) {
                 $newrelic_prefix = $this->_config->get_string('newrelic.appname_prefix');
             }
             if (($newrelic_api_key = $config->get_string('newrelic.api_key')) && !$config->get_string('newrelic.account_id')) {
                 $nerser = w3_instance('W3_NewRelicService');
                 $account_id = $nerser->get_account_id($newrelic_api_key);
                 $config->set('newrelic.account_id', $account_id);
             }
             if ($method == 'dropdown' && $config->get_string('newrelic.application_id')) {
                 $application_id = $config->get_string('newrelic.application_id');
                 if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) {
                     w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php');
                     $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id'));
                     $appname = $nerser->get_application_name($application_id);
                     $config->set('newrelic.appname', $appname);
                 }
             } else {
                 if ($method == 'manual' && $config->get_string('newrelic.appname')) {
                     if ($newrelic_prefix != '' && strpos($config->get_string('newrelic.appname'), $newrelic_prefix) === false) {
                         $application_name = $newrelic_prefix . $config->get_string('newrelic.appname');
                         $config->set('newrelic.appname', $application_name);
                     } else {
                         $application_name = $config->get_string('newrelic.appname');
                     }
                     if ($config->get_string('newrelic.api_key') && $config->get_string('newrelic.account_id')) {
                         w3_require_once(W3TC_LIB_W3_DIR . '/NewRelicService.php');
                         $nerser = new W3_NewRelicService($config->get_string('newrelic.api_key'), $config->get_string('newrelic.account_id'));
                         $application_id = $nerser->get_application_id($application_name);
                         if ($application_id) {
                             $config->set('newrelic.application_id', $application_id);
                         }
                     }
                 }
             }
         }
         if ($config->get_boolean('minify.enabled') && !$this->_config->get_boolean('minify.enabled') || $config->get_boolean('minify.enabled') && $config->get_boolean('browsercache.enabled') && !$this->_config->get_boolean('browsercache.enabled') || $config->get_boolean('minify.enabled') && $config->get_boolean('minify.auto') && !$this->_config->get_boolean('minify.auto') || $config->get_boolean('minify.enabled') && $config->get_string('minify.engine') != $this->_config->get_string('minify.engine')) {
             delete_transient('w3tc_minify_tested_filename_length');
         }
         if (!w3_is_pro($this->_config)) {
             delete_transient('w3tc_license_status');
         }
     }
     /**
      * Minify tab
      */
     if ($this->_page == 'w3tc_minify' && !$this->_config->get_boolean('minify.auto')) {
         $js_groups = array();
         $css_groups = array();
         $js_files = W3_Request::get_array('js_files');
         $css_files = W3_Request::get_array('css_files');
         foreach ($js_files as $theme => $templates) {
             foreach ($templates as $template => $locations) {
                 foreach ((array) $locations as $location => $types) {
                     foreach ((array) $types as $files) {
                         foreach ((array) $files as $file) {
                             if (!empty($file)) {
                                 $js_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file);
                             }
                         }
                     }
                 }
             }
         }
         foreach ($css_files as $theme => $templates) {
             foreach ($templates as $template => $locations) {
                 foreach ((array) $locations as $location => $files) {
                     foreach ((array) $files as $file) {
                         if (!empty($file)) {
                             $css_groups[$theme][$template][$location]['files'][] = w3_normalize_file_minify($file);
                         }
                     }
                 }
             }
         }
         $config->set('minify.js.groups', $js_groups);
         $config->set('minify.css.groups', $css_groups);
         $js_theme = W3_Request::get_string('js_theme');
         $css_theme = W3_Request::get_string('css_theme');
         $params = array_merge($params, array('js_theme' => $js_theme, 'css_theme' => $css_theme));
     }
     if ($this->_page == 'w3tc_minify') {
         if ($config->get_integer('minify.auto.filename_length') > 246) {
             $config->set('minify.auto.filename_length', 246);
         }
         delete_transient('w3tc_minify_tested_filename_length');
     }
     /**
      * Browser Cache tab
      */
     if ($this->_page == 'w3tc_browsercache') {
         if ($config->get_boolean('browsercache.enabled') && $config->get_boolean('browsercache.no404wp') && !get_option('permalink_structure')) {
             $config->set('browsercache.no404wp', false);
             $redirect_permalink_error = 'fancy_permalinks_disabled_browsercache';
         }
         $config->set('browsercache.timestamp', time());
         if (in_array($engine = $this->_config->get_string('cdn.engine'), array('netdna', 'maxcdn'))) {
             w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
             $keys = explode('+', $this->_config->get_string('cdn.' . $engine . '.authorization_key'));
             if (sizeof($keys) == 3) {
                 list($alias, $consumerkey, $consumersecret) = $keys;
                 try {
                     $api = new NetDNA($alias, $consumerkey, $consumersecret);
                     $disable_cooker_header = $config->get_boolean('browsercache.other.nocookies') || $config->get_boolean('browsercache.cssjs.nocookies');
                     $api->update_pull_zone($this->_config->get_string('cdn.' . $engine . '.zone_id'), array('ignore_setcookie_header' => $disable_cooker_header));
                 } catch (Exception $ex) {
                 }
             }
         }
     }
     /**
      * Mobile tab
      */
     if ($this->_page == 'w3tc_mobile') {
         $groups = W3_Request::get_array('mobile_groups');
         $mobile_groups = array();
         $cached_mobile_groups = array();
         foreach ($groups as $group => $group_config) {
             $group = strtolower($group);
             $group = preg_replace('~[^0-9a-z_]+~', '_', $group);
             $group = trim($group, '_');
             if ($group) {
                 $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default';
                 $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true;
                 $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : '';
                 $agents = isset($group_config['agents']) ? explode("\r\n", trim($group_config['agents'])) : array();
                 $mobile_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'agents' => $agents);
                 $cached_mobile_groups[$group] = $agents;
             }
         }
         /**
          * Allow plugins modify WPSC mobile groups
          */
         $cached_mobile_groups = apply_filters('cached_mobile_groups', $cached_mobile_groups);
         /**
          * Merge existent and delete removed groups
          */
         foreach ($mobile_groups as $group => $group_config) {
             if (isset($cached_mobile_groups[$group])) {
                 $mobile_groups[$group]['agents'] = (array) $cached_mobile_groups[$group];
             } else {
                 unset($mobile_groups[$group]);
             }
         }
         /**
          * Add new groups
          */
         foreach ($cached_mobile_groups as $group => $agents) {
             if (!isset($mobile_groups[$group])) {
                 $mobile_groups[$group] = array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => $agents);
             }
         }
         /**
          * Allow plugins modify W3TC mobile groups
          */
         $mobile_groups = apply_filters('w3tc_mobile_groups', $mobile_groups);
         /**
          * Sanitize mobile groups
          */
         foreach ($mobile_groups as $group => $group_config) {
             $mobile_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'agents' => array()), $group_config);
             $mobile_groups[$group]['agents'] = array_unique($mobile_groups[$group]['agents']);
             $mobile_groups[$group]['agents'] = array_map('strtolower', $mobile_groups[$group]['agents']);
             sort($mobile_groups[$group]['agents']);
         }
         $enable_mobile = false;
         foreach ($mobile_groups as $group_config) {
             if ($group_config['enabled']) {
                 $enable_mobile = true;
                 break;
             }
         }
         $config->set('mobile.enabled', $enable_mobile);
         $config->set('mobile.rgroups', $mobile_groups);
     }
     /**
      * Referrer tab
      */
     if ($this->_page == 'w3tc_referrer') {
         $groups = W3_Request::get_array('referrer_groups');
         $referrer_groups = array();
         foreach ($groups as $group => $group_config) {
             $group = strtolower($group);
             $group = preg_replace('~[^0-9a-z_]+~', '_', $group);
             $group = trim($group, '_');
             if ($group) {
                 $theme = isset($group_config['theme']) ? trim($group_config['theme']) : 'default';
                 $enabled = isset($group_config['enabled']) ? (bool) $group_config['enabled'] : true;
                 $redirect = isset($group_config['redirect']) ? trim($group_config['redirect']) : '';
                 $referrers = isset($group_config['referrers']) ? explode("\r\n", trim($group_config['referrers'])) : array();
                 $referrer_groups[$group] = array('theme' => $theme, 'enabled' => $enabled, 'redirect' => $redirect, 'referrers' => $referrers);
             }
         }
         /**
          * Allow plugins modify W3TC referrer groups
          */
         $referrer_groups = apply_filters('w3tc_referrer_groups', $referrer_groups);
         /**
          * Sanitize mobile groups
          */
         foreach ($referrer_groups as $group => $group_config) {
             $referrer_groups[$group] = array_merge(array('theme' => '', 'enabled' => true, 'redirect' => '', 'referrers' => array()), $group_config);
             $referrer_groups[$group]['referrers'] = array_unique($referrer_groups[$group]['referrers']);
             $referrer_groups[$group]['referrers'] = array_map('strtolower', $referrer_groups[$group]['referrers']);
             sort($referrer_groups[$group]['referrers']);
         }
         $enable_referrer = false;
         foreach ($referrer_groups as $group_config) {
             if ($group_config['enabled']) {
                 $enable_referrer = true;
                 break;
             }
         }
         $config->set('referrer.enabled', $enable_referrer);
         $config->set('referrer.rgroups', $referrer_groups);
     }
     /**
      * CDN tab
      */
     if ($this->_page == 'w3tc_cdn') {
         $cdn_cnames = W3_Request::get_array('cdn_cnames');
         $cdn_domains = array();
         foreach ($cdn_cnames as $cdn_cname) {
             $cdn_cname = trim($cdn_cname);
             /**
              * Auto expand wildcard domain to 10 subdomains
              */
             $matches = null;
             if (preg_match('~^\\*\\.(.*)$~', $cdn_cname, $matches)) {
                 $cdn_domains = array();
                 for ($i = 1; $i <= 10; $i++) {
                     $cdn_domains[] = sprintf('cdn%d.%s', $i, $matches[1]);
                 }
                 break;
             }
             if ($cdn_cname) {
                 $cdn_domains[] = $cdn_cname;
             }
         }
         switch ($this->_config->get_string('cdn.engine')) {
             case 'ftp':
                 $config->set('cdn.ftp.domain', $cdn_domains);
                 break;
             case 's3':
                 $config->set('cdn.s3.cname', $cdn_domains);
                 break;
             case 'cf':
                 $config->set('cdn.cf.cname', $cdn_domains);
                 break;
             case 'cf2':
                 $config->set('cdn.cf2.cname', $cdn_domains);
                 break;
             case 'rscf':
                 $config->set('cdn.rscf.cname', $cdn_domains);
                 break;
             case 'azure':
                 $config->set('cdn.azure.cname', $cdn_domains);
                 break;
             case 'mirror':
                 $config->set('cdn.mirror.domain', $cdn_domains);
                 break;
             case 'maxcdn':
                 $config->set('cdn.maxcdn.domain', $cdn_domains);
                 break;
             case 'netdna':
                 $config->set('cdn.netdna.domain', $cdn_domains);
                 break;
             case 'cotendo':
                 $config->set('cdn.cotendo.domain', $cdn_domains);
                 break;
             case 'edgecast':
                 $config->set('cdn.edgecast.domain', $cdn_domains);
                 break;
             case 'att':
                 $config->set('cdn.att.domain', $cdn_domains);
                 break;
             case 'akamai':
                 $config->set('cdn.akamai.domain', $cdn_domains);
                 break;
         }
     }
     w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/extensions.php');
     w3_extensions_admin_init();
     $all_extensions = w3_get_extensions($config);
     $old_extensions = $this->_config->get_array('extensions.settings', array());
     foreach ($all_extensions as $extension => $descriptor) {
         $extension_values = W3_Request::get_as_array('extensions.settings.');
         $extension_keys = array();
         $extension_settings = array();
         $tmp_grp = str_replace('.', '_', $extension) . '_';
         foreach ($extension_values as $key => $value) {
             if (strpos($key, $tmp_grp) !== false) {
                 $extension_settings[str_replace($tmp_grp, '', $key)] = $value;
             }
         }
         if ($extension_settings) {
             $old_extension_settings = isset($old_extensions[$extension]) ? $old_extensions[$extension] : array();
             if (!isset($old_extensions[$extension])) {
                 $old_extensions[$extension] = array();
             }
             $extension_keys[$extension] = apply_filters("w3tc_save_extension_settings-{$extension}", $extension_settings, $old_extension_settings);
             $new_settings = array_merge($old_extensions, $extension_keys);
             $config->set("extensions.settings", $new_settings);
             $old_extensions = $config->get_array('extensions.settings', array());
         }
     }
     //CloudFront does not support expires header. So disable it when its used
     if ($config->get_string('cdn.engine') == 'cf2') {
         $config->set('browsercache.cssjs.expires', false);
         $config->set('browsercache.html.expires', false);
         $config->set('browsercache.other.expires', false);
     }
     $config = apply_filters('w3tc_save_options', $config, $this->_config, $config_admin);
     $config = apply_filters("w3tc_save_options-{$this->_page}", $config, $this->_config, $config_admin);
     do_action('w3tc_saving_options', $config, $this->_config, $config_admin);
     do_action("w3tc_saving_options-{$this->_page}", $config, $this->_config, $config_admin);
     w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/admin.php');
     w3_config_save($this->_config, $config, $config_admin);
     switch ($this->_page) {
         case 'w3tc_cdn':
             /**
              * Handle Set Cookie Domain
              */
             $set_cookie_domain_old = W3_Request::get_boolean('set_cookie_domain_old');
             $set_cookie_domain_new = W3_Request::get_boolean('set_cookie_domain_new');
             if ($set_cookie_domain_old != $set_cookie_domain_new) {
                 if ($set_cookie_domain_new) {
                     if (!$this->enable_cookie_domain()) {
                         w3_admin_redirect(array_merge($params, array('w3tc_error' => 'enable_cookie_domain')));
                     }
                 } else {
                     if (!$this->disable_cookie_domain()) {
                         w3_admin_redirect(array_merge($params, array('w3tc_error' => 'disable_cookie_domain')));
                     }
                 }
             }
             break;
         case 'w3tc_general':
             break;
     }
     $notes[] = 'config_save';
     if ($redirect_permalink_error) {
         w3_admin_redirect(array('w3tc_error' => $redirect_permalink_error, 'w3tc_note' => 'config_save'));
     }
     w3_admin_redirect_with_custom_messages($params, null, $notes, true);
 }
Example #10
0
 /**
  * Purge CDN completely
  * @param $results
  * @return bool
  */
 function purge_all(&$results)
 {
     if (empty($this->_config['alias'])) {
         $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, 'Empty Alias.');
         return false;
     }
     if (empty($this->_config['consumerkey'])) {
         $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, 'Empty Consumer Key.');
         return false;
     }
     if (empty($this->_config['consumersecret'])) {
         $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, 'Empty Consumer Secret.');
         return false;
     }
     if (!class_exists('NetDNA')) {
         w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
     }
     $api = new NetDNA($this->_config['alias'], $this->_config['consumerkey'], $this->_config['consumersecret']);
     $results = array();
     $local_path = $remote_path = '';
     $domain_is_valid = 0;
     $found_domain = false;
     try {
         $customdomains = json_decode($api->get('/zones/pull.json'));
         if (preg_match("(200|201)", $customdomains->code)) {
             $local_path = 'all';
             $remote_path = 'all';
             $domain_is_valid = 0;
             $found_domain = false;
             foreach ($customdomains->data->pullzones as $zone) {
                 if ($zone->name . '.' . $this->_config['alias'] . '.' . W3TC_CDN_NETDNA_URL === $this->_config['domain'][0]) {
                     try {
                         $file_purge = json_decode($api->delete('/zones/pull.json/' . $zone->id . '/cache'));
                         if (preg_match("(200|201)", $customdomains->code)) {
                             $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_OK, 'OK');
                         } else {
                             if (preg_match("(401|500)", $file_purge->code)) {
                                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Failed with error code ' . $file_purge->code . '. Please check your alias, consumer key, and private key.');
                             } else {
                                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Failed with error code ' . $file_purge->code);
                             }
                         }
                         $found_domain = true;
                     } catch (CurlException $e) {
                         $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, sprintf('Unable to purge (%s).', $e->getMessage()));
                     }
                 } else {
                     $domain_is_valid++;
                 }
             }
         } else {
             if (preg_match("(401|500)", $customdomains->code)) {
                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Failed with error code ' . $customdomains->code . '. Please check your alias, consumer key, and private key.');
             } else {
                 $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'Failed with error code ' . $customdomains->code);
             }
         }
     } catch (CurlException $e) {
         $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_HALT, 'Failure to pull list of zones: ' . $e->getMessage());
     }
     if ($domain_is_valid > 0 && !$found_domain) {
         $results[] = $this->_get_result($local_path, $remote_path, W3TC_CDN_RESULT_ERROR, 'No zones matching custom domain.');
     }
     return !$this->_is_error($results);
 }
Example #11
0
 /**
  * Changes settings on MaxCDN/NetDNA site
  */
 function change_canonical_header()
 {
     if (in_array($cdn_engine = $this->_config->get_string('cdn.engine'), array('maxcdn', 'netdna'))) {
         w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
         $authorization_key = $this->_config->get_string("cdn.{$cdn_engine}.authorization_key");
         if ($authorization_key) {
             $keys = explode('+', $authorization_key);
             if (sizeof($keys) == 3) {
                 list($alias, $consumer_key, $consumer_secret) = $keys;
                 $api = new NetDNA($alias, $consumer_key, $consumer_secret);
                 $zone = array();
                 $zone_id = $this->_config->get_string("cdn.{$cdn_engine}.zone_id");
                 $zone['canonical_link_headers'] = $this->_config->get_boolean('cdn.canonical_header') ? 1 : 0;
                 try {
                     $api->update_pull_zone($zone_id, $zone);
                 } catch (Exception $ex) {
                 }
             }
         }
     }
 }
 public function w3tc_ajax_cdn_maxcdn_fsd_configure_zone()
 {
     $api_key = $_REQUEST['api_key'];
     $zone_id = Util_Request::get('zone_id', '');
     $zone = array('name' => Util_Request::get('name'), 'label' => Util_Request::get('name'), 'url' => Util_Request::get('url'), 'use_stale' => 1, 'queries' => 1, 'compress' => 1, 'backend_compress' => 1, 'dns_check' => Util_Request::get('dns_check'), 'ip' => Util_Request::get('ip'));
     $api = \NetDNA::create($api_key);
     try {
         if (empty($zone_id)) {
             $response = $api->create_pull_zone($zone);
             $zone_id = $response['id'];
         } else {
             $response = $api->update_pull_zone($zone_id, $zone);
         }
         $custom_domains = $api->get_custom_domains($zone_id);
         $custom_domain = Util_Request::get('custom_domain');
         $added = false;
         foreach ($custom_domains as $d) {
             if ($d['custom_domain'] == $custom_domain) {
                 $added = true;
                 break;
             }
         }
         if (!$added) {
             $api->create_custom_domain($zone_id, $custom_domain);
         }
     } catch (\Exception $ex) {
         $this->render_intro(array('api_key' => $api_key, 'error_message' => 'Failed to configure custom domain ' . $custom_domain . ': ' . $ex->getMessage()));
         exit;
     }
     $zone_domain = $response['tmp_url'];
     $c = Dispatcher::config();
     $c->set('cdn.maxcdn_fsd.api_key', $api_key);
     $c->set('cdn.maxcdn_fsd.zone_id', $zone_id);
     $c->set('cdn.maxcdn_fsd.zone_domain', $zone_domain);
     $c->save();
     $details = array('name' => $zone['name'], 'home_domain' => Util_Environment::home_url_host(), 'dns_cname_target' => $zone_domain);
     include W3TC_DIR . '/Cdn_MaxCdnFsd_Popup_View_Success.php';
     exit;
 }
Example #13
0
 /**
  * Create a NetDNA or MaxCDN pull zone automatically
  */
 function action_cdn_auto_create_netdna_maxcdn_pull_zone()
 {
     w3_require_once(W3TC_LIB_NETDNA_DIR . '/NetDNA.php');
     w3_require_once(W3TC_INC_FUNCTIONS_DIR . '/admin.php');
     $cdn_engine = W3_Request::get_string('type');
     $this->validate_cdnengine_is_netdna_maxcdn($cdn_engine);
     $authorization_key = W3_Request::get_string('authorization_key');
     $this->validate_authorization_key($authorization_key);
     $keys = explode('+', $authorization_key);
     list($alias, $consumerkey, $consumersecret) = $keys;
     $url = w3_get_home_url();
     try {
         $api = new NetDNA($alias, $consumerkey, $consumersecret);
         $disable_cooker_header = $this->_config->get_boolean('browsercache.other.nocookies') || $this->_config->get_boolean('browsercache.cssjs.nocookies');
         $zone = $api->create_default_pull_zone($url, null, null, array('ignore_setcookie_header' => $disable_cooker_header));
         $name = $zone['name'];
         $temporary_url = "{$name}.{$alias}.netdna-cdn.com";
         $test_result = -1;
         if (!$this->_config->get_array("cdn.{$cdn_engine}.domain")) {
             $test_result = $this->test_cdn_url($temporary_url) ? 1 : 0;
             $this->_config->set("cdn.{$cdn_engine}.zone_id", $zone['id']);
             if ($test_result) {
                 $this->_config->set("cdn.enabled", true);
             }
             $this->_config->set("cdn.{$cdn_engine}.domain", array($temporary_url));
         }
         $this->_config->save();
         $config_admin = w3_instance('W3_ConfigAdmin');
         $zones = $api->get_pull_zones();
         $zone_count = sizeof($zones);
         w3tc_make_track_call(array('type' => 'cdn', 'data' => array('cdn' => $cdn_engine, 'action' => 'zonecreation', 'creation' => 'manual', 'creationtime' => time(), 'signupclick' => $config_admin->get_integer('track.maxcdn_signup'), 'authorizeclick' => $config_admin->get_integer('track.maxcdn_authorize'), 'validationclick' => $config_admin->get_integer('track.maxcdn_validation'), 'total_zones' => $zone_count, 'test' => $test_result)));
         $result = array('result' => 'single', 'cnames' => array($temporary_url));
     } catch (Exception $ex) {
         $result = array('result' => 'error', 'message' => sprintf(__('Could not create default zone.' . $ex->getMessage(), 'w3-total-cache')));
     }
     echo json_encode($result);
     exit;
 }
 /**
  * Purge CDN completely
  *
  * @param unknown $results
  * @return bool
  */
 function purge_all(&$results)
 {
     if (empty($this->_config['authorization_key'])) {
         $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, __('Empty Authorization Key.', 'w3-total-cache'));
         return false;
     }
     if (empty($this->_config['alias']) || empty($this->_config['consumerkey']) || empty($this->_config['consumersecret'])) {
         $results = $this->_get_results(array(), W3TC_CDN_RESULT_HALT, __('Malformed Authorization Key.', 'w3-total-cache'));
         return false;
     }
     if (!class_exists('NetDNA')) {
         require_once W3TC_LIB_NETDNA_DIR . '/NetDNA.php';
     }
     $api = new \NetDNA($this->_config['alias'], $this->_config['consumerkey'], $this->_config['consumersecret']);
     $results = array();
     try {
         if ($this->_config['zone_id'] != 0) {
             $zone_id = $this->_config['zone_id'];
         } else {
             $zone_id = $api->get_zone_id(get_home_url());
         }
         if ($zone_id == 0) {
             $zone_id = $api->get_zone_id(Util_Environment::home_domain_root_url());
         }
         if ($zone_id == 0) {
             $zone_id = $api->get_zone_id(str_replace('://', '://www.', Util_Environment::home_domain_root_url()));
         }
         if ($zone_id == 0 || is_null($zone_id)) {
             if (Util_Environment::home_domain_root_url() == get_home_url()) {
                 $results[] = $this->_get_result('', '', W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s.', 'w3-total-cache'), trim(get_home_url(), '/')));
             } else {
                 $results[] = $this->_get_result('', '', W3TC_CDN_RESULT_ERROR, sprintf(__('No zones match site: %s or %s.', 'w3-total-cache'), trim(get_home_url(), '/'), trim(Util_Environment::home_domain_root_url(), '/')));
             }
             return !$this->_is_error($results);
         }
         $file_purge = json_decode($api->delete('/zones/pull.json/' . $zone_id . '/cache'));
         if (preg_match("(200|201)", $file_purge->code)) {
             $results[] = $this->_get_result('', '', W3TC_CDN_RESULT_OK, __('OK', 'w3-total-cache'));
         } else {
             if (preg_match("(401|500)", $file_purge->code)) {
                 $results[] = $this->_get_result('', '', W3TC_CDN_RESULT_ERROR, sprintf(__('Failed with error code %s. Please check your alias, consumer key, and private key.', 'w3-total-cache'), $file_purge->code));
             } else {
                 $results[] = $this->_get_result('', '', W3TC_CDN_RESULT_ERROR, __('Failed with error code ', 'w3-total-cache') . $file_purge->code);
             }
         }
     } catch (W3tcWpHttpException $e) {
         $results[] = $this->_get_result('', '', W3TC_CDN_RESULT_HALT, __('Failure to pull zone: ', 'w3-total-cache') . $e->getMessage());
     }
     return !$this->_is_error($results);
 }
<?php

require_once 'NetDNA.php';
$api = new NetDNA("my_alias", "consumer_key", "consumer_secret");
try {
    echo $api->get('/zones/pull.json');
} catch (CurlException $e) {
    print_r($e->getMessage());
    print_r($e->getHeaders());
}
Example #16
0
<?php

require_once 'netdnarws-php/NetDNA.php';
$config = json_decode(file_get_contents('./config.json'), true);
$config = $config['cdn'];
$zone_id = $config['zone_id'];
$parts = preg_split('/\\?reload=?/', $_SERVER['REQUEST_URI']);
if (!$parts) {
    header('400 Bad Request');
    echo $_SERVER['REQUEST_URI'] . ' is not a valid URI.';
    exit;
}
$file = $parts[0];
header('Content-Type: text/plain');
echo "Attempting to purge: {$zone_id}: {$file}.\n";
$api = new NetDNA('jquery', $config['consumer_key'], $config['consumer_secret']);
$result = $api->delete('/zones/pull.json/' . $zone_id . '/cache', array('file' => $file));
$result = json_decode($result, true);
if ($result['code'] !== 200) {
    echo 'Error reported: ' . print_r($result, true);
} else {
    echo "Done\n";
}