-
Notifications
You must be signed in to change notification settings - Fork 86
/
Cdn_BunnyCdn_Popup.php
279 lines (245 loc) · 9.36 KB
/
Cdn_BunnyCdn_Popup.php
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
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* File: Cdn_BunnyCdn_Popup.php
*
* @since 2.6.0
* @package W3TC
*/
namespace W3TC;
/**
* Class: Cdn_BunnyCdn_Popup
*
* @since 2.6.0
*/
class Cdn_BunnyCdn_Popup {
/**
* W3TC AJAX.
*
* @since 2.6.0
* @static
*
* @return void
*/
public static function w3tc_ajax() {
$o = new Cdn_BunnyCdn_Popup();
\add_action(
'w3tc_ajax_cdn_bunnycdn_intro',
array( $o, 'w3tc_ajax_cdn_bunnycdn_intro' )
);
\add_action(
'w3tc_ajax_cdn_bunnycdn_list_pull_zones',
array( $o, 'w3tc_ajax_cdn_bunnycdn_list_pull_zones' )
);
\add_action(
'w3tc_ajax_cdn_bunnycdn_configure_pull_zone',
array( $o, 'w3tc_ajax_cdn_bunnycdn_configure_pull_zone' )
);
\add_action(
'w3tc_ajax_cdn_bunnycdn_deauthorization',
array( $o, 'w3tc_ajax_cdn_bunnycdn_deauthorization' )
);
\add_action(
'w3tc_ajax_cdn_bunnycdn_deauthorize',
array( $o, 'w3tc_ajax_cdn_bunnycdn_deauthorize' )
);
}
/**
* W3TC AJAX: Render intro.
*
* @since 2.6.0
*
* @return void
*/
public function w3tc_ajax_cdn_bunnycdn_intro() {
$config = Dispatcher::config();
$account_api_key = $config->get_string( 'cdn.bunnycdn.account_api_key' );
// Ask for an account API key.
$this->render_intro(
array(
'account_api_key' => empty( $account_api_key ) ? null : $account_api_key,
)
);
}
/**
* W3TC AJAX: List pull zones.
*
* @since 2.6.0
*/
public function w3tc_ajax_cdn_bunnycdn_list_pull_zones() {
$account_api_key = Util_Request::get_string( 'account_api_key' );
$api = new Cdn_BunnyCdn_Api( array( 'account_api_key' => $account_api_key ) );
// Try to retrieve pull zones.
try {
$pull_zones = $api->list_pull_zones();
} catch ( \Exception $ex ) {
// Reauthorize: Ask for a new account API key.
$this->render_intro(
array(
'account_api_key' => empty( $account_api_key ) ? null : $account_api_key,
'error_message' => \esc_html( \__( 'Cannot list pull zones', 'w3-total-cache' ) . '; ' . $ex->getMessage() ),
)
);
}
// Save the account API key, if added or changed.
$config = Dispatcher::config();
if ( $config->get_string( 'cdn.bunnycdn.account_api_key' ) !== $account_api_key ) {
$config->set( 'cdn.bunnycdn.account_api_key', $account_api_key );
$config->save();
}
// Print the view.
$server_ip = ! empty( $_SERVER['SERVER_ADDR'] ) && \filter_var( \wp_unslash( $_SERVER['SERVER_ADDR'] ), FILTER_VALIDATE_IP ) ?
\filter_var( \wp_unslash( $_SERVER['SERVER_ADDR'] ), FILTER_SANITIZE_URL ) : null;
$details = array(
'pull_zones' => $pull_zones,
'suggested_origin_url' => \home_url(), // Suggested origin URL or IP.
'suggested_zone_name' => \substr( \str_replace( '.', '-', \parse_url( \home_url(), PHP_URL_HOST ) ), 0, 60 ), // Suggested pull zone name.
'pull_zone_id' => $config->get_integer( 'cdn.bunnycdn.pull_zone_id' ),
);
include W3TC_DIR . '/Cdn_BunnyCdn_Popup_View_Pull_Zones.php';
\wp_die();
}
/**
* W3TC AJAX: Configure pull zone.
*
* @since 2.6.0
*
* @see Cdn_BunnyCdn_Api::get_default_edge_rules()
*/
public function w3tc_ajax_cdn_bunnycdn_configure_pull_zone() {
$config = Dispatcher::config();
$account_api_key = $config->get_string( 'cdn.bunnycdn.account_api_key' );
$pull_zone_id = Util_Request::get_integer( 'pull_zone_id' );
$origin_url = Util_Request::get_string( 'origin_url' ); // Origin URL or IP.
$name = Util_Request::get_string( 'name' ); // Pull zone name.
$cdn_hostname = Util_Request::get_string( 'cdn_hostname' ); // Pull zone CDN hostname (system).
// If not selecting a pull zone. then create a new one.
if ( empty( $pull_zone_id ) ) {
$api = new Cdn_BunnyCdn_Api( array( 'account_api_key' => $account_api_key ) );
// Try to create a new pull zone.
try {
$response = $api->add_pull_zone(
array(
'Name' => $name, // The name/hostname for the pull zone where the files will be accessible; only letters, numbers, and dashes.
'OriginUrl' => $origin_url, // Origin URL or IP (with optional port number).
'CacheErrorResponses' => true, // If enabled, bunny.net will temporarily cache error responses (304+ HTTP status codes) from your servers for 5 seconds to prevent DDoS attacks on your origin. If disabled, error responses will be set to no-cache.
'DisableCookies' => false, // Determines if the Pull Zone should automatically remove cookies from the responses.
'EnableTLS1' => false, // TLS 1.0 was deprecated in 2018.
'EnableTLS1_1' => false, // TLS 1.1 was EOL's on March 31,2020.
'ErrorPageWhitelabel' => true, // Any bunny.net branding will be removed from the error page and replaced with a generic term.
'OriginHostHeader' => \parse_url( \home_url(), PHP_URL_HOST ), // Sets the host header that will be sent to the origin.
'UseStaleWhileUpdating' => true, // Serve stale content while updating. If Stale While Updating is enabled, cache will not be refreshed if the origin responds with a non-cacheable resource.
'UseStaleWhileOffline' => true, // Serve stale content if the origin is offline.
)
);
$pull_zone_id = (int) $response['Id'];
$name = $response['Name'];
$cdn_hostname = $response['Hostnames'][0]['Value'];
} catch ( \Exception $ex ) {
// Reauthorize: Ask for a new account API key.
$this->render_intro(
array(
'account_api_key' => empty( $account_api_key ) ? null : $account_api_key,
'error_message' => \esc_html( \__( 'Cannot select or add a pull zone', 'w3-total-cache' ) . '; ' . $ex->getMessage() ),
)
);
}
// Initialize an error messages array.
$error_messages = array();
// Add Edge Rules.
foreach ( Cdn_BunnyCdn_Api::get_default_edge_rules() as $edge_rule ) {
try {
$api->add_edge_rule( $edge_rule, $pull_zone_id );
} catch ( \Exception $ex ) {
$error_messages[] = sprintf(
// translators: 1: Edge Rule description/name.
\__( 'Could not add Edge Rule "%1$s".', 'w3-total-cache' ) . '; ',
\esc_html( $edge_rule['Description'] )
) . $ex->getMessage();
}
}
// Convert error messages array to a string.
$error_messages = \implode( "\r\n", $error_messages );
}
// Save configuration.
$config->set( 'cdn.bunnycdn.pull_zone_id', $pull_zone_id );
$config->set( 'cdn.bunnycdn.name', $name );
$config->set( 'cdn.bunnycdn.origin_url', $origin_url );
$config->set( 'cdn.bunnycdn.cdn_hostname', $cdn_hostname );
$config->save();
// Print success view.
include W3TC_DIR . '/Cdn_BunnyCdn_Popup_View_Configured.php';
\wp_die();
}
/**
* W3TC AJAX: Deauthorization form.
*
* @since 2.6.0
*/
public function w3tc_ajax_cdn_bunnycdn_deauthorization() {
$config = Dispatcher::config();
$origin_url = $config->get_string( 'cdn.bunnycdn.origin_url' ); // Origin URL or IP.
$name = $config->get_string( 'cdn.bunnycdn.name' ); // Pull zone name.
$cdn_hostname = $config->get_string( 'cdn.bunnycdn.cdn_hostname' ); // Pull zone CDN hostname.
$cdn_pull_zone_id = $config->get_integer( 'cdn.bunnycdn.pull_zone_id' ); // CDN pull zone id.
$cdnfsd_pull_zone_id = $config->get_integer( 'cdnfsd.bunnycdn.pull_zone_id' ); // CDN FSD pull zone id.
// Present details and ask to deauthorize and optionally delete the pull zone.
include W3TC_DIR . '/Cdn_BunnyCdn_Popup_View_Deauthorize.php';
\wp_die();
}
/**
* W3TC AJAX: Deauthorize.
*
* Deauthorize and optionally delete the pull zone.
*
* @since 2.6.0
*/
public function w3tc_ajax_cdn_bunnycdn_deauthorize() {
$config = Dispatcher::config();
$account_api_key = $config->get_string( 'cdn.bunnycdn.account_api_key' );
$cdn_pull_zone_id = $config->get_integer( 'cdn.bunnycdn.pull_zone_id' ); // CDN pull zone id.
$cdnfsd_pull_zone_id = $config->get_integer( 'cdnfsd.bunnycdn.pull_zone_id' ); // CDN FSD pull zone id.
$delete_pull_zone = Util_Request::get_string( 'delete_pull_zone' );
// Delete pull zone, if requested.
if ( 'yes' === $delete_pull_zone ) {
$api = new Cdn_BunnyCdn_Api( array( 'account_api_key' => $account_api_key ) );
// Try to delete pull zone.
try {
$api->delete_pull_zone( $cdn_pull_zone_id );
} catch ( \Exception $ex ) {
$delete_error_message = $ex->getMessage();
}
// If the same pull zone is used for FSD, then deauthorize that too.
if ( ! empty( $cdn_pull_zone_id ) && $cdn_pull_zone_id === $cdnfsd_pull_zone_id ) {
$config->set( 'cdnfsd.bunnycdn.pull_zone_id', null );
$config->set( 'cdnfsd.bunnycdn.name', null );
$config->set( 'cdnfsd.bunnycdn.origin_url', null );
$config->set( 'cdnfsd.bunnycdn.cdn_hostname', null );
}
}
$config->set( 'cdn.bunnycdn.pull_zone_id', null );
$config->set( 'cdn.bunnycdn.name', null );
$config->set( 'cdn.bunnycdn.origin_url', null );
$config->set( 'cdn.bunnycdn.cdn_hostname', null );
$config->save();
// Print success view.
include W3TC_DIR . '/Cdn_BunnyCdn_Popup_View_Deauthorized.php';
\wp_die();
}
/**
* Render intro.
*
* @since 2.6.0
* @access private
*
* @param array $details {
* Details for the modal.
*
* @type string $account_api_key Account API key.
* @type string $error_message Error message (optional).
* }
*/
private function render_intro( array $details ) {
include W3TC_DIR . '/Cdn_BunnyCdn_Popup_View_Intro.php';
\wp_die();
}
}