-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathCdnEngine_Mirror_RackSpaceCdn.php
More file actions
267 lines (233 loc) · 6.73 KB
/
Copy pathCdnEngine_Mirror_RackSpaceCdn.php
File metadata and controls
267 lines (233 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* File: CdnEngine_Mirror_RackSpaceCdn.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class CdnEngine_Mirror_RackSpaceCdn
*
* Rackspace CDN (pull) engine
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
*/
class CdnEngine_Mirror_RackSpaceCdn extends CdnEngine_Mirror {
/**
* Access state
*
* @var array
*/
private $_access_state;
/**
* Service ID
*
* @var string
*/
private $_service_id;
/**
* Domains
*
* @var array
*/
private $_domains;
/**
* CDN RackSpace API object
*
* @var Cdn_RackSpace_Api_Cdn
*/
private $_api;
/**
* New access state callback
*
* @var callable
*/
private $_new_access_state_callback;
/**
* Initializes the CdnEngine_Mirror_RackSpaceCdn instance with configuration parameters.
*
* @param array $w3tc_config Configuration settings for the RackSpace CDN service.
*
* @return void
*/
public function __construct( $w3tc_config = array() ) {
$w3tc_config = array_merge(
array(
'user_name' => '',
'api_key' => '',
'region' => '',
'service_id' => '',
'service_access_url' => '',
'service_protocol' => 'http',
'domains' => array(),
'access_state' => '',
'new_access_state_callback' => '',
),
$w3tc_config
);
$this->_service_id = $w3tc_config['service_id'];
$this->_new_access_state_callback = $w3tc_config['new_access_state_callback'];
// init access state.
$this->_access_state = @json_decode( $w3tc_config['access_state'], true );
if ( ! is_array( $this->_access_state ) ) {
$this->_access_state = array();
}
$this->_access_state = array_merge(
array(
'access_token' => '',
'access_region_descriptor' => array(),
),
$this->_access_state
);
// cnames.
if ( 'https' !== $w3tc_config['service_protocol'] && ! empty( $w3tc_config['domains'] ) ) {
$this->_domains = (array) $w3tc_config['domains'];
} else {
$this->_domains = array( $w3tc_config['service_access_url'] );
}
// form 'ssl' parameter based on service protocol.
if ( 'https' === $w3tc_config['service_protocol'] ) {
$w3tc_config['ssl'] = 'enabled';
} else {
$w3tc_config['ssl'] = 'disabled';
}
parent::__construct( $w3tc_config );
$this->_create_api( array( $this, '_on_new_access_requested_api' ) );
}
/**
* Creates the API instance for RackSpace CDN.
*
* @param callable $new_access_required_callback_api Callback for handling access renewal.
*
* @return void
*/
private function _create_api( $new_access_required_callback_api ) {
$this->_api = new Cdn_RackSpace_Api_Cdn(
array(
'access_token' => $this->_access_state['access_token'],
'access_region_descriptor' => $this->_access_state['access_region_descriptor'],
'new_access_required' => $new_access_required_callback_api,
)
);
}
/**
* Handles the process of requesting new access tokens and region descriptors via the API.
*
* @return Cdn_RackSpace_Api_Cdn The API instance with updated access credentials.
*
* @throws \Exception If authentication or region retrieval fails.
*/
public function _on_new_access_requested_api() {
$w3tc_r = Cdn_RackSpace_Api_Tokens::authenticate( $this->_config['user_name'], $this->_config['api_key'] );
if ( ! isset( $w3tc_r['access_token'] ) || ! isset( $w3tc_r['services'] ) ) {
throw new \Exception( \esc_html__( 'Authentication failed.', 'w3-total-cache' ) );
}
$w3tc_r['regions'] = Cdn_RackSpace_Api_Tokens::cdn_services_by_region( $w3tc_r['services'] );
if ( ! isset( $w3tc_r['regions'][ $this->_config['region'] ] ) ) {
throw new \Exception(
\esc_html(
sprintf(
// translators: 1: Region name.
\__( 'Region %1$s not found.', 'w3-total-cache' ),
$this->_config['region']
)
)
);
}
$this->_access_state['access_token'] = $w3tc_r['access_token'];
$this->_access_state['access_region_descriptor'] = $w3tc_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, wp_json_encode( $this->_access_state ) );
}
return $this->_api;
}
/**
* Handles the fallback case when the first authentication attempt fails.
*
* @return void
*
* @throws \Exception Always throws an exception indicating authentication failure.
*/
private function _on_new_access_requested_second_time() {
throw new \Exception( \esc_html__( 'Authentication failed', 'w3-total-cache' ) );
}
/**
* Purges a list of files from the RackSpace CDN.
*
* @param array $files Array of file descriptors to purge.
* @param array $results Reference to an array for storing purge results.
*
* @return bool True on success, false if there were errors during purging.
*/
public function purge( $files, &$results ) {
$results = array();
try {
foreach ( $files as $w3tc_file ) {
$w3tc_url = $this->_format_url( $w3tc_file['remote_path'] );
$this->_api->purge( $this->_service_id, $w3tc_url );
$results[] = $this->_get_result( '', '', W3TC_CDN_RESULT_OK, 'OK' );
}
} catch ( \Exception $e ) {
$results[] = $this->_get_result(
'',
'',
W3TC_CDN_RESULT_HALT,
\__( 'Failed to purge: ', 'w3-total-cache' ) . $e->getMessage()
);
}
return ! $this->_is_error( $results );
}
/**
* Retrieves the current set of domains associated with the CDN service.
*
* @return array List of domains.
*/
public function get_domains() {
return $this->_domains;
}
/**
* Retrieves the domains associated with the RackSpace service.
*
* @return array List of domains configured in the service.
*/
public function service_domains_get() {
$w3tc_service = $this->_api->service_get( $this->_service_id );
$domains = array();
if ( isset( $w3tc_service['domains'] ) ) {
foreach ( $w3tc_service['domains'] as $d ) {
$domains[] = $d['domain'];
}
}
return $domains;
}
/**
* Updates the domains associated with the RackSpace service.
*
* @param array $domains List of new domains to set.
*
* @return void
*/
public function service_domains_set( $domains ) {
$w3tc_value = array();
foreach ( $domains as $d ) {
$v = array( 'domain' => $d );
if ( 'https' === $this->_config['service_protocol'] ) {
$v['protocol'] = 'https';
}
$w3tc_value[] = $v;
}
$this->_api->service_set(
$this->_service_id,
array(
array(
'op' => 'replace',
'path' => '/domains',
'value' => $w3tc_value,
),
)
);
}
}