public function w3tc_ajax_pagespeed_widgetdata()
 {
     if (Util_Request::get('cache') != 'no') {
         $response = get_transient('w3tc_pagespeed_widgetdata');
         $response = @json_decode($response, true);
         if (is_array($response) && isset($response['time']) && $response['time'] >= time() - 60) {
             echo json_encode($response);
             return;
         }
     }
     $config = Dispatcher::config();
     $key = $config->get_string('widget.pagespeed.key');
     $w3_pagespeed = new PageSpeed_Api($key);
     $r = $w3_pagespeed->analyze(get_home_url());
     if (!$r) {
         echo json_encode(array('error' => 'API call failed'));
         return;
     }
     $details = '<ul class="w3tc-widget-ps-rules">';
     foreach ($r['rules'] as $index => $rule) {
         if ($index >= 5) {
             break;
         }
         $details .= '<li class="w3tc-widget-ps-rule w3tc-widget-ps-priority-' . $rule['priority'] . '">' . '<div class="w3tc-widget-ps-icon"><div></div></div>' . '<p>' . $rule['name'] . '</p>' . '</li>';
     }
     $details .= '</ul>';
     $response = array('score' => $r['score'] . ' / 100', 'details' => $details, 'time' => time());
     set_transient('w3tc_pagespeed_widgetdata', json_encode($response), 60);
     echo json_encode($response);
 }
 public function w3tc_ajax_extension_cloudflare_zones_done()
 {
     $email = $_REQUEST['email'];
     $key = $_REQUEST['key'];
     $zone_id = Util_Request::get('zone_id');
     if (empty($zone_id)) {
         return $this->_render_extension_cloudflare_zones(array('email' => $email, 'key' => $key, 'error_message' => 'Please select zone'));
     }
     $zone_name = '';
     // get zone name
     try {
         $api = new Extension_CloudFlare_Api(array('email' => $email, 'key' => $key));
         $zone = $api->zone($zone_id);
         $zone_name = $zone['name'];
     } catch (\Exception $ex) {
         $details['error_message'] = 'Can\'t authenticate: ' . $ex->getMessage();
         include W3TC_DIR . '/Extension_CloudFlare_Popup_View_Intro.php';
         exit;
     }
     $c = Dispatcher::config();
     $c->set(array('cloudflare', 'email'), $email);
     $c->set(array('cloudflare', 'key'), $key);
     $c->set(array('cloudflare', 'zone_id'), $zone_id);
     $c->set(array('cloudflare', 'zone_name'), $zone_name);
     $c->save();
     delete_transient('w3tc_cloudflare_stats');
     $postfix = Util_Admin::custom_message_id(array(), array('extension_cloudflare_configuration_saved' => 'CloudFlare credentials are saved successfully'));
     echo 'Location admin.php?page=w3tc_extensions&extension=cloudflare&' . 'action=view&' . $postfix;
     exit;
 }
 public function w3tc_ajax_cdn_highwinds_configure_host()
 {
     $account_hash = $_REQUEST['account_hash'];
     $api_token = $_REQUEST['api_token'];
     $host = Util_Request::get('host', '');
     $details = array('account_hash' => $account_hash, 'api_token' => $api_token);
     $api = new Cdn_Highwinds_Api($account_hash, $api_token);
     try {
         if (empty($host)) {
             $host = $this->_create_host($api, $_REQUEST['host_new']);
         }
     } catch (\Exception $ex) {
         $api_hosts = $api->hosts();
         $details['hosts'] = $api_hosts['list'];
         $details['error_message'] = $ex->getMessage();
         include W3TC_DIR . '/Cdn_Highwinds_Popup_View_SelectHost.php';
         exit;
     }
     // try to obtain CNAMEs
     $c = Dispatcher::config();
     try {
         $scopes_response = $api->configure_scopes($host);
         $scope_id = 0;
         foreach ($scopes_response['list'] as $scope) {
             if ($scope['platform'] == 'CDS') {
                 $scope_id = $scope['id'];
             }
         }
         if ($scope_id <= 0) {
             throw new Exception('scope CDN hasnt been created');
         }
         $configuration = $api->configure_scope_get($host, $scope_id);
         if (isset($configuration['hostname'])) {
             $domains = array();
             foreach ($configuration['hostname'] as $d) {
                 $domains[] = $d['domain'];
             }
             $c->set('cdn.highwinds.host.domains', $domains);
         }
     } catch (\Exception $ex) {
     }
     $c->set('cdn.highwinds.account_hash', $account_hash);
     $c->set('cdn.highwinds.api_token', $api_token);
     $c->set('cdn.highwinds.host.hash_code', $host);
     $c->save();
     $postfix = Util_Admin::custom_message_id(array(), array('cdn_configuration_saved' => 'CDN credentials are saved successfully'));
     echo 'Location admin.php?page=w3tc_cdn&' . $postfix;
     exit;
 }
 public function w3tc_ajax_cdn_rackspace_services_done()
 {
     $user_name = $_REQUEST['user_name'];
     $api_key = $_REQUEST['api_key'];
     $access_token = $_REQUEST['access_token'];
     $access_region_descriptor = json_decode(strtr($_REQUEST['access_region_descriptor'], '!^', '"\\'), true);
     $region = $_REQUEST['region'];
     $service = Util_Request::get('service');
     if (!empty($service)) {
         $this->_render_service_actualize(array('user_name' => $user_name, 'api_key' => $api_key, 'access_token' => $access_token, 'access_region_descriptor_serialized' => strtr(json_encode($access_region_descriptor), '"\\', '!^'), 'region' => $region, 'service_id' => $service));
         exit;
     }
     $home_url = get_home_url();
     $parsed = parse_url($home_url);
     $is_https = $parsed['scheme'] == 'https';
     $details = array('user_name' => $user_name, 'api_key' => $api_key, 'access_token' => $access_token, 'access_region_descriptor_serialized' => strtr(json_encode($access_region_descriptor), '"\\', '!^'), 'region' => $region, 'name' => '', 'protocol' => $is_https ? 'https' : 'http', 'cname_http' => '', 'cname_http_style' => $is_https ? 'display: none' : '', 'cname_https_prefix' => '', 'cname_https_style' => $is_https ? '' : 'display: none', 'origin' => Util_Environment::home_url_host());
     include W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Service_Create.php';
     exit;
 }
 public function w3tc_ajax_cdn_rackspace_containers_done()
 {
     $user_name = $_REQUEST['user_name'];
     $api_key = $_REQUEST['api_key'];
     $access_token = $_REQUEST['access_token'];
     $access_region_descriptor = json_decode(strtr($_REQUEST['access_region_descriptor'], '!^', '"\\'), true);
     $region = $_REQUEST['region'];
     $container = Util_Request::get('container');
     $api_files = new Cdn_RackSpace_Api_CloudFiles(array('access_token' => $access_token, 'access_region_descriptor' => $access_region_descriptor, 'new_access_required' => ''));
     $api_cdn = new Cdn_RackSpace_Api_CloudFilesCdn(array('access_token' => $access_token, 'access_region_descriptor' => $access_region_descriptor, 'new_access_required' => ''));
     try {
         if (empty($container)) {
             $container_new = $_REQUEST['container_new'];
             if (empty($container_new)) {
                 throw new \Exception('Please select container');
             }
             $api_files->container_create($container_new);
             $api_cdn->container_cdn_enable($container_new);
             $container = $container_new;
         }
     } catch (\Exception $ex) {
         $containers = $api_cdn->containers();
         $details = array('user_name' => $user_name, 'api_key' => $api_key, 'access_token' => $access_token, 'access_region_descriptor_serialized' => strtr(json_encode($access_region_descriptor), '"\\', '!^'), 'region' => $region, 'containers' => $containers);
         $details['error_message'] = $ex->getMessage();
         include W3TC_DIR . '/Cdn_RackSpaceCloudFiles_Popup_View_Containers.php';
         exit;
     }
     $c = Dispatcher::config();
     $c->set('cdn.rscf.user', $user_name);
     $c->set('cdn.rscf.key', $api_key);
     $c->set('cdn.rscf.location', $region);
     $c->set('cdn.rscf.container', $container);
     $c->save();
     // reset calculated state
     $state = Dispatcher::config_state();
     $state->set('cdn.rackspace_cf.access_state', '');
     $state->save();
     $postfix = Util_Admin::custom_message_id(array(), array('cdn_configuration_saved' => 'CDN credentials are saved successfully'));
     echo 'Location admin.php?page=w3tc_cdn&' . $postfix;
     exit;
 }
 public function w3tc_ajax_newrelic_apply_configuration()
 {
     $api_key = $_REQUEST['api_key'];
     $monitoring_type = Util_Request::get('monitoring_type', 'apm');
     $apm_application_name = Util_Request::get('apm_application_name');
     $browser_application_id = Util_Request::get('browser_application_id');
     $c = Dispatcher::config();
     $c->set(array('newrelic', 'api_key'), $api_key);
     if ($monitoring_type == 'apm') {
         $c->set(array('newrelic', 'monitoring_type'), 'apm');
         $c->set(array('newrelic', 'apm.application_name'), $apm_application_name);
     } else {
         $c->set(array('newrelic', 'monitoring_type'), 'browser');
         $c->set(array('newrelic', 'browser.application_id'), $browser_application_id);
     }
     $c->save();
     // flush cached values on api key change to allow to reset it from ui
     // if something goes wrong
     update_option('w3tc_nr_account_id', '');
     update_option('w3tc_nr_application_id', '');
     $postfix = Util_Admin::custom_message_id(array(), array('newrelic_configuration_saved' => 'NewRelic configuration is saved successfully'));
     echo 'Location admin.php?page=w3tc_general&' . $postfix;
 }
 /**
  * Alters the active state of multiple extensions
  */
 public function change_extensions_status()
 {
     $extensions = Util_Request::get_array('checked');
     $action = Util_Request::get('action');
     if ('-1' == $action) {
         $action = Util_Request::get('action2');
     }
     // dropdown at bottom
     $message = '';
     if ('activate-selected' == $action) {
         foreach ($extensions as $extension) {
             if (Extensions_Util::activate_extension($extension, $this->_config)) {
                 $message .= '&activated=' . $extension;
             }
         }
         wp_redirect(Util_Ui::admin_url(sprintf('admin.php?page=w3tc_extensions%s', $message)));
     } elseif ('deactivate-selected' == $action) {
         foreach ($extensions as $extension) {
             if (Extensions_Util::deactivate_extension($extension, $this->_config)) {
                 $message .= '&deactivated=' . $extension;
             }
         }
         wp_redirect(Util_Ui::admin_url(sprintf('admin.php?page=w3tc_extensions%s', $message)));
     } else {
         wp_redirect(Util_Ui::admin_url('admin.php?page=w3tc_extensions'));
     }
 }
 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;
 }
 public function w3tc_ajax_cdn_cloudfront_fsd_configure_distribution()
 {
     $access_key = $_REQUEST['access_key'];
     $secret_key = $_REQUEST['secret_key'];
     $distribution_id = Util_Request::get('distribution_id', '');
     $origin_id = rand();
     $distribution = array('Comment' => Util_Request::get('distribution_comment'), 'Origins' => array('Quantity' => 1, 'Items' => array('Origin' => array('Id' => $origin_id, 'DomainName' => Util_Request::get('origin'), 'OriginPath' => '', 'CustomOriginConfig' => array('HTTPPort' => 80, 'HTTPSPort' => 443, 'OriginProtocolPolicy' => 'match-viewer')))), 'Aliases' => array('Quantity' => 1, 'Items' => array('CNAME' => Util_Request::get('alias'))), 'DefaultCacheBehavior' => array('TargetOriginId' => $origin_id, 'ForwardedValues' => array('QueryString' => 'true', 'Cookies' => array('Forward' => 'all'), 'Headers' => array('Quantity' => 1, 'Items' => array('Name' => 'Host'))), 'AllowedMethods' => array('Quantity' => 7, 'Items' => array('Method' => array('GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE')), 'CachedMethods' => array('Quantity' => 2, 'Items' => array('Method' => array('GET', 'HEAD')))), 'MinTTL' => 0));
     try {
         $api = new Cdn_CloudFrontFsd_Api($access_key, $secret_key);
         if (empty($distribution_id)) {
             $distribution['DefaultCacheBehavior']['TrustedSigners'] = array('Enabled' => 'false', 'Quantity' => 0);
             $distribution['DefaultCacheBehavior']['ViewerProtocolPolicy'] = 'allow-all';
             $response = $api->distribution_create($distribution);
             $distribution_id = $response['Id'];
         } else {
             $response = $api->distribution_update($distribution_id, $distribution);
         }
     } catch (\Exception $ex) {
         $this->render_intro(array('error_message' => 'Failed to configure distribution: ' . $ex->getMessage()));
         exit;
     }
     $distribution_domain = $response['DomainName'];
     $c = Dispatcher::config();
     $c->set('cdn.cloudfront_fsd.access_key', $access_key);
     $c->set('cdn.cloudfront_fsd.secret_key', $secret_key);
     $c->set('cdn.cloudfront_fsd.distribution_id', $distribution_id);
     $c->set('cdn.cloudfront_fsd.distribution_domain', $distribution_domain);
     $c->save();
     $details = array('name' => $distribution['Comment'], 'home_domain' => Util_Environment::home_url_host(), 'dns_cname_target' => $distribution_domain);
     include W3TC_DIR . '/Cdn_CloudFrontFsd_Popup_View_Success.php';
     exit;
 }
Example #10
0
 /**
  * Returns array value
  *
  * @param string  $key
  * @param array   $default
  * @return array
  */
 static function get_array($key, $default = array())
 {
     $value = Util_Request::get($key);
     if (is_array($value)) {
         return $value;
     } elseif ($value != '') {
         return preg_split("/[\r\n,;]+/", trim($value));
     }
     return $default;
 }