-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathConfigCompiler.php
476 lines (388 loc) · 14.4 KB
/
ConfigCompiler.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
namespace W3TC;
class ConfigCompiler {
private $_blog_id;
private $_preview;
private $_data;
private $_keys;
/**
* Returns true is key is not modifiable anymore
* Placed here to limit class loading
*
* @return bool
*/
static public function child_key_sealed( $key, $master_data, $child_data ) {
if ( isset( $master_data['common.force_master'] ) &&
(boolean)$master_data['common.force_master'] )
return true;
// affects rules which are global, not possible to overload
if ( $key == 'pgcache.engine' &&
( $master_data['pgcache.engine'] == 'file_generic' ) )
return true;
if ( in_array( $key, array(
'minify.rewrite',
'browsercache.rewrite',
'version' ) ) )
return true;
include W3TC_DIR . '/ConfigKeys.php';
// key which marks overloads is always editable
$overloads_postfix = '.configuration_overloaded';
// extension settings sealing
// e.g. array('newrelic' , '...') is controlled
// by 'newrelic.configuration_overloaded']
$block_key = ( is_array( $key ) ? $key[0] : $key );
if ( isset( $child_data[$block_key] ) &&
isset( $child_data[$block_key . $overloads_postfix] ) &&
(boolean)$child_data[$block_key . $overloads_postfix] )
return false;
if ( !is_array( $key ) ) {
if ( substr( $key, strlen( $key ) - strlen( $overloads_postfix ),
strlen( $overloads_postfix ) ) == $overloads_postfix )
return false;
// default sealing
foreach ( $overloading_keys_scope as $i ) {
$overloading_key = $i['key'];
// check if this key is allowed by overloading-mark key
if ( substr( $key, 0, strlen( $i['prefix'] ) ) == $i['prefix'] ) {
if ( !isset( $child_data[$overloading_key] ) )
return true;
if ( (boolean)$child_data[$overloading_key] )
return false;
}
}
}
return true;
}
/**
* Reads config from file and returns it's content as array (or null)
* Stored in this class to limit class loading
*/
static private function util_array_from_file_legacy_v2( $filename ) {
if ( file_exists( $filename ) && is_readable( $filename ) ) {
// including file directly instead of read+eval causes constant
// problems with APC, ZendCache, and WSOD in a case of
// broken config file
$content = @file_get_contents( $filename );
$config = @json_decode( $content, true );
if ( is_array( $config ) )
return $config;
}
return null;
}
public function __construct( $blog_id, $preview ) {
$this->_blog_id = $blog_id;
$this->_preview = $preview;
include W3TC_DIR . '/ConfigKeys.php';
$this->_keys = $keys;
// move _date to initial state
foreach ( $this->_keys as $key => $value ) {
$this->_data[$key] = $value['default'];
}
$this->_data['version'] = W3TC_VERSION;
$this->set_dynamic_defaults();
}
public function load( $data = null ) {
// apply data from master config
if ( is_null( $data ) ) {
$data = Config::util_array_from_storage( 0, $this->_preview );
}
if ( is_null( $data ) && $this->_preview ) {
// try to read production data when preview not available
$data = Config::util_array_from_storage( 0, false );
}
// try to get legacy v2 data
if ( is_null( $data ) ) {
$master_filename = Config::util_config_filename_legacy_v2( 0,
$this->_preview );
$data = self::util_array_from_file_legacy_v2( $master_filename );
}
if ( is_array( $data ) ) {
$data = $this->upgrade( $data );
foreach ( $data as $key => $value )
$this->_data[$key] = $value;
}
if ( $this->is_master() )
return;
// apply child config
$data = Config::util_array_from_storage( $this->_blog_id,
$this->_preview );
if ( is_null( $data ) && $this->_preview ) {
// try to read production data when preview not available
$data = Config::util_array_from_storage( $this->_blog_id,
false );
}
// try to get legacy v2 data
if ( is_null( $data ) ) {
$child_filename = Config::util_config_filename_legacy_v2(
$this->_blog_id, $this->_preview );
$data = self::util_array_from_file_legacy_v2( $child_filename );
}
if ( is_array( $data ) ) {
$data = $this->upgrade( $data );
foreach ( $data as $key => $value ) {
if ( !ConfigCompiler::child_key_sealed( $key, $this->_data, $data ) )
$this->_data[$key] = $value;
}
}
}
public function apply_data( $data ) {
foreach ( $data as $key => $value )
$this->_data[$key] = $value;
}
public function get_data() {
return $this->_data;
}
public function save() {
$data = array(
'version' => $this->_data['version']
);
if ( $this->is_master() ) {
foreach ( $this->_data as $key => $value )
$data[$key] = $this->_data[$key];
} else {
// write only overwrited keys
$master = new ConfigCompiler( 0, $this->_preview );
$master->load();
foreach ( $this->_data as $key => $value ) {
if ( !ConfigCompiler::child_key_sealed( $key, $master->_data,
$this->_data ) )
$data[$key] = $this->_data[$key];
}
}
ConfigUtil::save_item( $this->_blog_id, $this->_preview, $data );
}
/**
* Returns true if we edit master config
*
* @return boolean
*/
private function is_master() {
return $this->_blog_id <= 0;
}
private function set_dynamic_defaults() {
if ( empty( $this->_data['stats.access_log.webserver'] ) ) {
if ( Util_Environment::is_nginx() ) {
$this->_data['stats.access_log.webserver'] = 'nginx';
$this->_data['stats.access_log.format'] = '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
} else {
$this->_data['stats.access_log.webserver'] = 'apache';
}
}
}
/**
* Apply new default values when version changes
*/
private function upgrade( $file_data ) {
if ( !isset( $file_data['version'] ) ) {
$file_data['version'] = '0.0.0';
}
if ( !function_exists( 'bb2_start' ) ) {
$file_data['pgcache.bad_behavior_path'] = '';
} else {
if ( file_exists( WP_PLUGIN_DIR . '/bad-behavior/bad-behavior-generic.php' ) ) {
$bb_file = WP_PLUGIN_DIR . '/bad-behavior/bad-behavior-generic.php';
} elseif ( file_exists( WP_PLUGIN_DIR . '/Bad-Behavior/bad-behavior-generic.php' ) ) {
$bb_file = WP_PLUGIN_DIR . '/Bad-Behavior/bad-behavior-generic.php';
} else {
$bb_file = false;
}
if ( $bb_file ) {
$file_data['pgcache.bad_behavior_path'] = $bb_file;
}
}
//
// changes in 0.15.2
//
if ( version_compare( $file_data['version'], '0.15.2', '<' ) ) {
if ( isset( $file_data['minify.js.combine.header'] ) && $file_data['minify.js.combine.header'] ) {
$file_data['minify.js.method'] = 'combine';
}
if ( isset( $file_data['minify.css.combine'] ) && $file_data['minify.css.combine'] ) {
$file_data['minify.css.method'] = 'combine';
}
}
//
// changes in 0.13
//
if ( version_compare( $file_data['version'], '0.12.0', '<=' ) ) {
if ( empty( $file_data['lazyload.exclude'] ) ) {
$file_data['lazyload.exclude'] = array();
}
if ( !in_array( 'skip_lazy', $file_data['lazyload.exclude'] ) ) {
$file_data['lazyload.exclude'][] = 'skip_lazy';
}
}
//
// changes in 0.9.7
//
if ( isset( $file_data['cdnfsd.enabled'] ) &&
$file_data['cdnfsd.enabled'] == '1' &&
empty( $file_data['cdnfsd.engine'] ) ) {
$file_data['cdnfsd.enabled'] = '0';
}
//
// changes in 0.9.6
//
if ( !isset( $file_data['cdn.cors_header'] ) ) {
$file_data['cdn.cors_header'] = true;
}
//
// changes in 0.9.5
//
if ( !isset( $file_data['extensions.active_frontend'] ) ||
!is_array( $file_data['extensions.active_frontend'] ) )
$file_data['extensions.active_frontend'] = array();
if ( version_compare( $file_data['version'], '0.9.5', '<' ) ) {
// dont show minify tips if already enabled
if ( isset( $file_data['minify.enabled'] ) &&
$file_data['minify.enabled'] == 'true' &&
function_exists( 'get_option' ) ) {
$cs = Dispatcher::config_state();
$cs->set( 'minify.hide_minify_help', true );
$cs->save();
}
$file_data['pgcache.mirrors.enabled'] = true;
// map regions in rackspace
if ( isset( $file_data['cdn.rscf.location'] ) ) {
if ( $file_data['cdn.rscf.location'] == 'uk' )
$file_data['cdn.rscf.location'] = 'LON';
if ( $file_data['cdn.rscf.location'] == 'us' )
$file_data['cdn.rscf.location'] = 'ORD';
}
// change filenames
$active = array();
if ( isset( $file_data['extensions.active'] ) &&
is_array( $file_data['extensions.active'] ) ) {
if ( isset( $file_data['extensions.active']['cloudflare'] ) )
$active['cloudflare'] = 'w3-total-cache/Extension_CloudFlare_Plugin.php';
if ( isset( $file_data['extensions.active']['genesis.theme'] ) )
$active['genesis.theme'] = 'w3-total-cache/Extension_Genesis_Plugin.php';
if ( isset( $file_data['extensions.active']['wordpress-seo'] ) )
$active['wordpress-seo'] = 'w3-total-cache/Extension_WordPressSeo_Plugin.php';
}
$file_data['extensions.active'] = $active;
$active_frontend = array();
foreach ( $active as $key => $value )
$active_frontend[$key] = '*';
$file_data['extensions.active_frontend'] = $active_frontend;
// keep those active by default
$file_data['extensions.active']['newrelic'] =
'w3-total-cache/Extension_NewRelic_Plugin.php';
$file_data['extensions.active']['fragmentcache'] =
'w3-total-cache/Extension_FragmentCache_Plugin.php';
}
// newrelic settings - migrate to extension
if ( isset( $file_data['newrelic.enabled'] ) &&
$file_data['newrelic.enabled'] ) {
// make new relic extension enabled
if ( !isset( $file_data['extensions.active_frontend']['newrelic'] ) )
$file_data['extensions.active_frontend']['newrelic'] ='*';
}
if ( !isset( $file_data['newrelic'] ) ||
!is_array( $file_data['newrelic'] ) )
$file_data['newrelic'] = array(
'monitoring_type' => 'apm'
);
$this->_set_if_exists( $file_data, 'newrelic.api_key',
'newrelic', 'api_key' );
$this->_set_if_exists( $file_data, 'newrelic.appname',
'newrelic', 'apm.application_name' );
$this->_set_if_exists( $file_data, 'newrelic.accept.logged_roles',
'newrelic', 'accept.logged_roles' );
$this->_set_if_exists( $file_data, 'newrelic.accept.roles',
'newrelic', 'accept.roles' );
$this->_set_if_exists( $file_data, 'newrelic.use_php_function',
'newrelic', 'use_php_function' );
$this->_set_if_exists( $file_data, 'newrelic.cache_time',
'newrelic', 'cache_time' );
$this->_set_if_exists( $file_data, 'newrelic.enable_xmit',
'newrelic', 'enable_xmit' );
$this->_set_if_exists( $file_data, 'newrelic.include_rum',
'newrelic', 'include_rum' );
// extensions - kept in separate key now
$this->_set_if_exists_extension( $file_data, 'cloudflare' );
$this->_set_if_exists_extension( $file_data, 'genesis.theme' );
// fragmentcache to extension
if ( isset( $file_data['fragmentcache.enabled'] ) &&
$file_data['fragmentcache.enabled'] ) {
// make new relic extension enabled
if ( !isset( $file_data['extensions.active_frontend']['fragmentcache'] ) )
$file_data['extensions.active_frontend']['fragmentcache'] = '*';
}
$this->_set_if_exists( $file_data, 'fragmentcache.debug',
'fragmentcache', 'debug' );
$this->_set_if_exists( $file_data, 'fragmentcache.engine',
'fragmentcache', 'engine' );
$this->_set_if_exists( $file_data, 'fragmentcache.file.gc',
'fragmentcache', 'file.gc' );
$this->_set_if_exists( $file_data, 'fragmentcache.file.locking',
'fragmentcache', 'file.locking' );
$this->_set_if_exists( $file_data, 'fragmentcache.memcached.servers',
'fragmentcache', 'memcached.servers' );
$this->_set_if_exists( $file_data, 'fragmentcache.memcached.persistent',
'fragmentcache', 'memcached.persistent' );
$this->_set_if_exists( $file_data, 'fragmentcache.memcached.aws_autodiscovery',
'fragmentcache', 'memcached.aws_autodiscovery' );
$this->_set_if_exists( $file_data, 'fragmentcache.memcached.username',
'fragmentcache', 'memcached.username' );
$this->_set_if_exists( $file_data, 'fragmentcache.memcached.password',
'fragmentcache', 'memcached.password' );
$this->_set_if_exists( $file_data, 'fragmentcache.memcached.binary_protocol',
'fragmentcache', 'memcached.binary_protocol' );
$this->_set_if_exists( $file_data, 'fragmentcache.redis.persistent',
'fragmentcache', 'redis.persistent' );
$this->_set_if_exists( $file_data, 'fragmentcache.redis.servers',
'fragmentcache', 'redis.servers' );
$this->_set_if_exists( $file_data, 'fragmentcache.redis.verify_tls_certificates',
'fragmentcache', 'redis.verify_tls_certificates' );
$this->_set_if_exists( $file_data, 'fragmentcache.redis.password',
'fragmentcache', 'redis.password' );
$this->_set_if_exists( $file_data, 'fragmentcache.redis.dbid',
'fragmentcache', 'redis.dbid' );
$this->_set_if_exists( $file_data, 'fragmentcache.lifetime',
'fragmentcache', 'lifetime' );
// new options, separated old one. implemented in 0.9.5.3
if ( isset( $file_data['browsercache.cssjs.replace'] ) &&
!isset( $file_data['browsercache.cssjs.querystring'] ) ) {
$file_data['browsercache.cssjs.querystring'] = $file_data['browsercache.cssjs.replace'];
}
if ( isset( $file_data['browsercache.other.replace'] ) &&
!isset( $file_data['browsercache.other.querystring'] ) ) {
$file_data['browsercache.other.querystring'] = $file_data['browsercache.other.replace'];
}
//
// changes in 0.9.5.4
//
if ( isset( $file_data['cdn.engine'] ) ) {
if ( $file_data['cdn.engine'] == 'cloudfront_fsd' ) {
$file_data['cdnfsd.engine'] = 'cloudfront';
$file_data['cdnfsd.enabled'] = $file_data['cdn.enabled'];
if ( isset( $file_data['cdn.cloudfront_fsd.access_key'] ) ) {
$file_data['cdnfsd.cloudfront.access_key'] =
$file_data['cdn.cloudfront_fsd.access_key'];
$file_data['cdnfsd.cloudfront.distribution_domain'] =
$file_data['cdn.cloudfront_fsd.distribution_domain'];
$file_data['cdnfsd.cloudfront.secret_key'] =
$file_data['cdn.cloudfront_fsd.secret_key'];
$file_data['cdnfsd.cloudfront.distribution_id'] =
$file_data['cdn.cloudfront_fsd.distribution_id'];
}
}
}
$file_data['version'] = W3TC_VERSION;
return $file_data;
}
private function _set_if_exists_extension( &$a, $extension ) {
if ( isset( $a['extensions.settings'] ) &&
isset( $a['extensions.settings'][$extension] ) ) {
$a[$extension] = $a['extensions.settings'][$extension];
unset( $a['extensions.settings'][$extension] );
}
}
private function _set_if_exists( &$a, $old_key, $new_key0, $new_key1 ) {
if ( isset( $a[$old_key] ) ) {
$a[$new_key0][$new_key1] = $a[$old_key];
unset( $a[$old_key] );
}
}
}