/**
  * Called when new access token issued by api objects
  */
 public function _on_new_access_requested_api()
 {
     $r = Cdn_RackSpace_Api_Tokens::authenticate($this->_config['user_name'], $this->_config['api_key']);
     if (!isset($r['access_token']) || !isset($r['services'])) {
         throw new \Exception('Authentication failed');
     }
     $r['regions'] = Cdn_RackSpace_Api_Tokens::cdn_services_by_region($r['services']);
     if (!isset($r['regions'][$this->_config['region']])) {
         throw new \Exception('Region ' . $this->_config['region'] . ' not found');
     }
     $this->_access_state['access_token'] = $r['access_token'];
     $this->_access_state['access_region_descriptor'] = $r['regions'][$this->_config['region']];
     $this->_create_api(array($this, '_on_new_access_requested_second_time'));
     if (!empty($this->_new_access_state_callback)) {
         call_user_func($this->_new_access_state_callback, json_encode($this->_access_state));
     }
     return $this->_api;
 }
 private function _render_cdn_rackspace_regions($details)
 {
     $user_name = $details['user_name'];
     $api_key = $details['api_key'];
     try {
         $r = Cdn_RackSpace_Api_Tokens::authenticate($user_name, $api_key);
     } catch (\Exception $ex) {
         $details = array('user_name' => $user_name, 'api_key' => $api_key, 'error_message' => 'Can\'t authenticate: ' . $ex->getMessage());
         include W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Intro.php';
         exit;
     }
     $r['regions'] = Cdn_RackSpace_Api_Tokens::cdn_services_by_region($r['services']);
     $details['access_token'] = $r['access_token'];
     $details['region_descriptors'] = $r['regions'];
     // avoid fights with quotes, magic_quotes may break randomly
     $details['region_descriptors_serialized'] = strtr(json_encode($r['regions']), '"\\', '!^');
     include W3TC_DIR . '/Cdn_RackSpaceCdn_Popup_View_Regions.php';
     exit;
 }