W3TCCdnEngine_Mirror_RackSpaceCdn

Rackspace CDN (pull) engine.

Defined (1)

The class is defined in the following location(s).

/CdnEngine_Mirror_RackSpaceCdn.php  
  1. class CdnEngine_Mirror_RackSpaceCdn extends CdnEngine_Mirror { 
  2. private $_access_state; 
  3. private $_service_id; 
  4. private $_domains; 
  5. private $_api; 
  6. private $_new_access_state_callback; 
  7.  
  8.  
  9.  
  10. function __construct( $config = array() ) { 
  11. $config = array_merge( array( 
  12. 'user_name' => '',  
  13. 'api_key' => '',  
  14. 'region' => '',  
  15. 'service_id' => '',  
  16. 'service_access_url' => '',  
  17. 'service_protocol' => 'http',  
  18. 'domains' => array(),  
  19. 'access_state' => '',  
  20. 'new_access_state_callback' => '' 
  21. ), $config ); 
  22.  
  23. $this->_service_id = $config['service_id']; 
  24. $this->_new_access_state_callback = $config['new_access_state_callback']; 
  25.  
  26. // init access state 
  27. $this->_access_state = @json_decode( $config['access_state'], true ); 
  28. if ( !is_array( $this->_access_state ) ) 
  29. $this->_access_state = array(); 
  30. $this->_access_state = array_merge( array( 
  31. 'access_token' => '',  
  32. 'access_region_descriptor' => array() 
  33. ), $this->_access_state ); 
  34.  
  35. // cnames 
  36. if ( $config['service_protocol'] != 'https' && !empty( $config['domains'] ) ) 
  37. $this->_domains = (array)$config['domains']; 
  38. else 
  39. $this->_domains = array( $config['service_access_url'] ); 
  40.  
  41. // form 'ssl' parameter based on service protocol 
  42. if ( $config['service_protocol'] == 'https' ) 
  43. $config['ssl'] = 'enabled'; 
  44. else 
  45. $config['ssl'] = 'disabled'; 
  46.  
  47. parent::__construct( $config ); 
  48. $this->_create_api( array( $this, '_on_new_access_requested_api' ) ); 
  49.  
  50.  
  51.  
  52. private function _create_api( $new_access_required_callback_api ) { 
  53. $this->_api = new Cdn_RackSpace_Api_Cdn( array( 
  54. 'access_token' => $this->_access_state['access_token'],  
  55. 'access_region_descriptor' => $this->_access_state['access_region_descriptor'],  
  56. 'new_access_required' => $new_access_required_callback_api ) ); 
  57.  
  58.  
  59.  
  60. /** 
  61. * Called when new access token issued by api objects 
  62. */ 
  63. public function _on_new_access_requested_api() { 
  64. $r = Cdn_RackSpace_Api_Tokens::authenticate( $this->_config['user_name'],  
  65. $this->_config['api_key'] ); 
  66. if ( !isset( $r['access_token'] ) || !isset( $r['services'] ) ) 
  67. throw new \Exception( 'Authentication failed' ); 
  68. $r['regions'] = Cdn_RackSpace_Api_Tokens::cdn_services_by_region( 
  69. $r['services'] ); 
  70.  
  71. if ( !isset( $r['regions'][$this->_config['region']] ) ) 
  72. throw new \Exception( 'Region ' . $this->_config['region'] . ' not found' ); 
  73.  
  74. $this->_access_state['access_token'] = $r['access_token']; 
  75. $this->_access_state['access_region_descriptor'] = 
  76. $r['regions'][$this->_config['region']]; 
  77.  
  78. $this->_create_api( array( $this, '_on_new_access_requested_second_time' ) ); 
  79.  
  80. if ( !empty( $this->_new_access_state_callback ) ) 
  81. call_user_func( $this->_new_access_state_callback,  
  82. json_encode( $this->_access_state ) ); 
  83.  
  84. return $this->_api; 
  85.  
  86.  
  87.  
  88. private function _on_new_access_requested_second_time() { 
  89. throw new \Exception( 'Authentication failed' ); 
  90.  
  91.  
  92.  
  93. /** 
  94. * Purges remote files 
  95. * @param array $files 
  96. * @param array $results 
  97. * @return boolean 
  98. */ 
  99. function purge( $files, &$results ) { 
  100. $results = array(); 
  101.  
  102. try { 
  103. foreach ( $files as $file ) { 
  104. $url = $this->_format_url( $file['remote_path'] ); 
  105. $this->_api->purge( $this->_service_id, $url ); 
  106.  
  107. $results[] = $this->_get_result( '', '', W3TC_CDN_RESULT_OK, 'OK' ); 
  108. } catch ( \Exception $e ) { 
  109. $results[] = $this->_get_result( '', '', W3TC_CDN_RESULT_HALT,  
  110. __( 'Failed to purge: ', 'w3-total-cache' ) . $e->getMessage() ); 
  111.  
  112. return !$this->_is_error( $results ); 
  113.  
  114.  
  115.  
  116. public function get_domains() { 
  117. return $this->_domains; 
  118.  
  119.  
  120.  
  121. public function service_domains_get() { 
  122. $service = $this->_api->service_get( $this->_service_id ); 
  123.  
  124. $domains = array(); 
  125.  
  126. if ( isset( $service['domains'] ) ) { 
  127. foreach ( $service['domains'] as $d ) 
  128. $domains[] = $d['domain']; 
  129.  
  130. return $domains; 
  131.  
  132.  
  133.  
  134. public function service_domains_set( $domains ) { 
  135. $value = array(); 
  136. foreach ( $domains as $d ) { 
  137. $v = array( 'domain' => $d ); 
  138. if ( $this->_config['service_protocol'] == 'https' ) 
  139. $v['protocol'] = 'https'; 
  140.  
  141. $value[] = $v; 
  142.  
  143. $this->_api->service_set( $this->_service_id,  
  144. array( array( 
  145. 'op' => 'replace',  
  146. 'path' => '/domains',  
  147. 'value' => $value ) ) );