forked from BoldGrid/w3-total-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cdn_Environment.php
442 lines (370 loc) · 11.8 KB
/
Cdn_Environment.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
namespace W3TC;
/**
* class Cdn_Environment
*/
class Cdn_Environment {
public function __construct() {
add_filter( 'w3tc_browsercache_rules_section_extensions',
array( $this, 'w3tc_browsercache_rules_section_extensions' ),
10, 3 );
add_filter( 'w3tc_browsercache_rules_section',
array( $this, 'w3tc_browsercache_rules_section' ),
10, 3 );
}
/**
* Fixes environment in each wp-admin request
*
* @param Config $config
* @param bool $force_all_checks
* @throws Util_Environment_Exceptions
*/
public function fix_on_wpadmin_request( $config, $force_all_checks ) {
$exs = new Util_Environment_Exceptions();
if ( $config->get_boolean( 'config.check' ) || $force_all_checks ) {
if ( $config->get_boolean( 'cdn.enabled' ) ) {
$this->rules_add( $config, $exs );
} else {
$this->rules_remove( $exs );
}
}
if ( count( $exs->exceptions() ) > 0 )
throw $exs;
}
/**
* Fixes environment once event occurs
*
* @param Config $config
* @param string $event
* @param Config|null $old_config
* @throws Util_Environment_Exceptions
*/
public function fix_on_event( $config, $event, $old_config = null ) {
if ( $config->get_boolean( 'cdn.enabled' ) &&
!Cdn_Util::is_engine_mirror( $config->get_string( 'cdn.engine' ) ) ) {
if ( $old_config != null &&
$config->get_integer( 'cdn.queue.interval' ) !=
$old_config->get_integer( 'cdn.queue.interval' ) ) {
$this->unschedule_queue_process();
}
if ( !wp_next_scheduled( 'w3_cdn_cron_queue_process' ) ) {
wp_schedule_event( time(),
'w3_cdn_cron_queue_process', 'w3_cdn_cron_queue_process' );
}
} else {
$this->unschedule_queue_process();
}
if ( $config->get_boolean( 'cdn.enabled' ) &&
$config->get_boolean( 'cdn.autoupload.enabled' ) &&
!Cdn_Util::is_engine_mirror( $config->get_string( 'cdn.engine' ) ) ) {
if ( $old_config != null &&
$config->get_integer( 'cdn.autoupload.interval' ) !=
$old_config->get_integer( 'cdn.autoupload.interval' ) ) {
$this->unschedule_upload();
}
if ( !wp_next_scheduled( 'w3_cdn_cron_upload' ) ) {
wp_schedule_event( time(),
'w3_cdn_cron_upload', 'w3_cdn_cron_upload' );
}
} else {
$this->unschedule_upload();
}
$exs = new Util_Environment_Exceptions();
if ( $config->get_boolean( 'cdn.enabled' ) ) {
try {
$this->handle_tables(
$event == 'activate' /* drop state on activation */,
true );
} catch ( \Exception $ex ) {
$exs->push( $ex );
}
}
if ( count( $exs->exceptions() ) > 0 )
throw $exs;
}
/**
* Fixes environment after plugin deactivation
*/
public function fix_after_deactivation() {
$exs = new Util_Environment_Exceptions();
$this->rules_remove( $exs );
$this->handle_tables( true, false );
if ( count( $exs->exceptions() ) > 0 )
throw $exs;
}
/**
* Returns required rules for module
*
* @param Config $config
* @return array|null
*/
function get_required_rules( $config ) {
if ( !$config->get_boolean( 'cdn.enabled' ) )
return null;
$rewrite_rules = array();
$rules = $this->rules_generate( $config );
if ( strlen( $rules ) > 0 ) {
if ( $config->get_string( 'cdn.engine' ) == 'ftp' ) {
$common = Dispatcher::component( 'Cdn_Core' );
$domain = $common->get_cdn()->get_domain();
$cdn_rules_path = sprintf( 'ftp://%s/%s', $domain,
Util_Rule::get_cdn_rules_path() );
$rewrite_rules[] = array(
'filename' => $cdn_rules_path,
'content' => $rules
);
}
$path = Util_Rule::get_browsercache_rules_cache_path();
$rewrite_rules[] = array(
'filename' => $path,
'content' => $rules
);
}
return $rewrite_rules;
}
/**
*
*
* @param Config $config
* @return array|null
*/
function get_instructions( $config ) {
if ( !$config->get_boolean( 'cdn.enabled' ) )
return null;
$instructions = array();
$instructions[] = array( 'title'=>__( 'CDN module: Required Database SQL', 'w3-total-cache' ),
'content' => $this->generate_table_sql(), 'area' => 'database' );
return $instructions;
}
/**
* Generate rules for FTP
*/
public function rules_generate_for_ftp( $config ) {
return $this->rules_generate( $config, true );
}
/**
* Create tables
*
* @param bool $drop
* @throws Util_Environment_Exception
*/
private function handle_tables( $drop, $create ) {
global $wpdb;
$tablename_queue = $wpdb->base_prefix . W3TC_CDN_TABLE_QUEUE;
$tablename_map = $wpdb->base_prefix . W3TC_CDN_TABLE_PATHMAP;
if ( $drop ) {
$sql = "DROP TABLE IF EXISTS `$tablename_queue`;";
$wpdb->query( $sql );
$sql = "DROP TABLE IF EXISTS `$tablename_map`;";
$wpdb->query( $sql );
}
if ( !$create ) {
return;
}
$charset_collate = '';
if ( ! empty( $wpdb->charset ) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty( $wpdb->collate ) )
$charset_collate .= " COLLATE $wpdb->collate";
$sql = "CREATE TABLE IF NOT EXISTS `$tablename_queue` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`local_path` varchar(500) NOT NULL DEFAULT '',
`remote_path` varchar(500) NOT NULL DEFAULT '',
`command` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '1 - Upload, 2 - Delete, 3 - Purge',
`last_error` varchar(150) NOT NULL DEFAULT '',
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `date` (`date`)
) $charset_collate;";
$wpdb->query( $sql );
if ( !$wpdb->result )
throw new Util_Environment_Exception( 'Can\'t create table ' .
$tablename_queue );
$sql = "
CREATE TABLE IF NOT EXISTS `$tablename_map` (
-- Relative file path.
-- For reference, not actually used for finding files.
path TEXT NOT NULL,
-- MD5 hash of remote path, used for finding files.
path_hash VARCHAR(32) CHARACTER SET ascii NOT NULL,
type tinyint(1) NOT NULL DEFAULT '0',
-- Google Drive: document identifier
remote_id VARCHAR(200) CHARACTER SET ascii,
PRIMARY KEY (path_hash),
KEY `remote_id` (`remote_id`)
) $charset_collate";
$wpdb->query( $sql );
if ( !$wpdb->result )
throw new Util_Environment_Exception( 'Can\'t create table ' .
$tablename_map );
}
private function generate_table_sql() {
global $wpdb;
$charset_collate = '';
if ( ! empty( $wpdb->charset ) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty( $wpdb->collate ) )
$charset_collate .= " COLLATE $wpdb->collate";
$sql = sprintf( 'DROP TABLE IF EXISTS `%s%s`;', $wpdb->base_prefix, W3TC_CDN_TABLE_QUEUE );
$sql .= "\n" . sprintf( "CREATE TABLE IF NOT EXISTS `%s%s` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`local_path` varchar(500) NOT NULL DEFAULT '',
`remote_path` varchar(500) NOT NULL DEFAULT '',
`command` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '1 - Upload, 2 - Delete, 3 - Purge',
`last_error` varchar(150) NOT NULL DEFAULT '',
`date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `date` (`date`)
) $charset_collate;", $wpdb->base_prefix, W3TC_CDN_TABLE_QUEUE );
return $sql;
}
/**
* schedules
*/
/**
* Unschedules cron events
*/
private function unschedule_queue_process() {
if ( wp_next_scheduled( 'w3_cdn_cron_queue_process' ) ) {
wp_clear_scheduled_hook( 'w3_cdn_cron_queue_process' );
}
}
/**
* Unschedule upload event
*/
private function unschedule_upload() {
if ( wp_next_scheduled( 'w3_cdn_cron_upload' ) ) {
wp_clear_scheduled_hook( 'w3_cdn_cron_upload' );
}
}
/*
* rules core modification
*/
/**
* Writes directives to WP .htaccess
*
* @param Config $config
* @param Util_Environment_Exceptions $exs
* @throws Util_WpFile_FilesystemOperationException with S/FTP form if it can't get the required filesystem credentials
*/
private function rules_add( $config, $exs ) {
Util_Rule::add_rules( $exs, Util_Rule::get_browsercache_rules_cache_path(),
$this->rules_generate( $config ),
W3TC_MARKER_BEGIN_CDN,
W3TC_MARKER_END_CDN,
array(
W3TC_MARKER_BEGIN_MINIFY_CORE => 0,
W3TC_MARKER_BEGIN_PGCACHE_CORE => 0,
W3TC_MARKER_BEGIN_BROWSERCACHE_CACHE => 0,
W3TC_MARKER_BEGIN_WORDPRESS => 0,
W3TC_MARKER_END_PGCACHE_CACHE => strlen( W3TC_MARKER_END_PGCACHE_CACHE ) + 1,
W3TC_MARKER_END_MINIFY_CACHE => strlen( W3TC_MARKER_END_MINIFY_CACHE ) + 1
)
);
}
/**
* Removes Page Cache core directives
*
* @param Util_Environment_Exceptions $exs
* @throws Util_WpFile_FilesystemOperationException with S/FTP form if it can't get the required filesystem credentials
*/
private function rules_remove( $exs ) {
Util_Rule::remove_rules( $exs,
Util_Rule::get_browsercache_rules_cache_path(),
W3TC_MARKER_BEGIN_CDN,
W3TC_MARKER_END_CDN );
}
/**
* Generates rules for WP dir
*
* @param Config $config
* @param bool $cdnftp
* @return string
*/
private function rules_generate( $config, $cdnftp = false ) {
if ( Util_Environment::is_nginx() ) {
$o = new Cdn_Environment_Nginx( $config );
return $o->generate( $cdnftp );
} elseif ( Util_Environment::is_litespeed() ) {
$o = new Cdn_Environment_LiteSpeed( $config );
return $o->generate( $cdnftp );
} else {
return $this->rules_generate_apache( $config, $cdnftp );
}
}
private function rules_generate_apache( $config, $cdnftp ) {
$rules = '';
if ( $config->get_boolean( 'cdn.canonical_header' ) ) {
$rules .= $this->canonical( $cdnftp,
$config->get_boolean( 'cdn.cors_header') );
}
if ( $config->get_boolean( 'cdn.cors_header') ) {
$rules .= $this->allow_origin( $cdnftp );
}
if ( strlen( $rules ) > 0 ) {
$rules =
W3TC_MARKER_BEGIN_CDN . "\n" .
$rules .
W3TC_MARKER_END_CDN . "\n";
}
return $rules;
}
private function canonical( $cdnftp = false, $cors_header = true ) {
$rules = '';
$mime_types = include W3TC_INC_DIR . '/mime/other.php';
$extensions = array_keys( $mime_types );
$extensions_lowercase = array_map( 'strtolower', $extensions );
$extensions_uppercase = array_map( 'strtoupper', $extensions );
$rules .= "<FilesMatch \"\\.(" . implode( '|',
array_merge( $extensions_lowercase, $extensions_uppercase ) ) . ")$\">\n";
$host = ( $cdnftp ? Util_Environment::home_url_host() : '%{HTTP_HOST}' );
$rules .= " <IfModule mod_rewrite.c>\n";
$rules .= " RewriteEngine On\n";
$rules .= " RewriteCond %{HTTPS} !=on\n";
$rules .= " RewriteRule .* - [E=CANONICAL:http://$host%{REQUEST_URI},NE]\n";
$rules .= " RewriteCond %{HTTPS} =on\n";
$rules .= " RewriteRule .* - [E=CANONICAL:https://$host%{REQUEST_URI},NE]\n";
$rules .= " </IfModule>\n";
$rules .= " <IfModule mod_headers.c>\n";
$rules .= ' Header set Link "<%{CANONICAL}e>; rel=\"canonical\""' . "\n";
$rules .= " </IfModule>\n";
$rules .= "</FilesMatch>\n";
return $rules;
}
/**
* Returns allow-origin rules
*/
private function allow_origin( $cdnftp = false ) {
$r = "<IfModule mod_headers.c>\n";
$r .= " Header set Access-Control-Allow-Origin \"*\"\n";
$r .= "</IfModule>\n";
if ( !$cdnftp )
return $r;
else
return
"<FilesMatch \"\.(ttf|ttc|otf|eot|woff|woff2|font.css)$\">\n" .
$r .
"</FilesMatch>\n";
}
public function w3tc_browsercache_rules_section_extensions(
$extensions, $config, $section ) {
if ( Util_Environment::is_nginx() ) {
$o = new Cdn_Environment_Nginx( $config );
$extensions = $o->w3tc_browsercache_rules_section_extensions(
$extensions, $section );
} elseif ( Util_Environment::is_litespeed() ) {
$o = new Cdn_Environment_LiteSpeed( $config );
$extensions = $o->w3tc_browsercache_rules_section_extensions(
$extensions, $section );
}
return $extensions;
}
public function w3tc_browsercache_rules_section( $section_rules, $config, $section ) {
if ( Util_Environment::is_litespeed() ) {
$o = new Cdn_Environment_LiteSpeed( $config );
$section_rules = $o->w3tc_browsercache_rules_section(
$section_rules, $section );
}
return $section_rules;
}
}