-
Notifications
You must be signed in to change notification settings - Fork 5
/
InstallUtils.php
544 lines (490 loc) · 21.1 KB
/
InstallUtils.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
<?php
namespace WebSharks\ZenCache\Pro;
/*
* Plugin activation hook.
*
* @since 150422 Rewrite.
*
* @attaches-to {@link \register_activation_hook()}
*/
$self->activate = function () use ($self) {
$self->setup(); // Ensure setup is complete.
if (!$self->options['enable']) {
return; // Nothing to do.
}
$self->addWpCacheToWpConfig();
$self->addWpHtaccess();
$self->addAdvancedCache();
$self->updateBlogPaths();
$self->autoClearCache();
};
/*
* Check current plugin version that is installed in WP.
*
* @since 150422 Rewrite.
*
* @attaches-to `admin_init` hook.
*/
$self->checkVersion = function () use ($self) {
$prev_version = $self->options['version'];
if (version_compare($prev_version, VERSION, '>=')) {
return; // Nothing to do; up-to-date.
}
$self->updateOptions(array('version' => VERSION));
new VsUpgrades($prev_version);
if ($self->options['enable']) {
$self->addWpCacheToWpConfig();
$self->addWpHtaccess();
$self->addAdvancedCache();
$self->updateBlogPaths();
}
$self->wipeCache(); // Fresh start now.
$self->enqueueMainNotice(sprintf(__('<strong>%1$s:</strong> detected a new version of itself. Recompiling w/ latest version... wiping the cache... all done :-)', SLUG_TD), esc_html(NAME)), array('push_to_top' => true));
};
/*
* Plugin deactivation hook.
*
* @since 150422 Rewrite.
*
* @attaches-to {@link \register_deactivation_hook()}
*/
$self->deactivate = function () use ($self) {
$self->setup(); // Ensure setup is complete.
$self->removeWpCacheFromWpConfig();
$self->removeWpHtaccess();
$self->removeAdvancedCache();
$self->clearCache();
};
/*
* Plugin uninstall hook.
*
* @since 150422 Rewrite.
*/
$self->uninstall = function () use ($self) {
$self->setup(); // Ensure setup is complete.
if (!defined('WP_UNINSTALL_PLUGIN')) {
return; // Disallow.
}
if (empty($GLOBALS[GLOBAL_NS.'_uninstalling'])) {
return; // Not uninstalling.
}
if (!current_user_can($self->uninstall_cap)) {
return; // Extra layer of security.
}
$self->removeWpCacheFromWpConfig();
$self->removeWpHtaccess();
$self->removeAdvancedCache();
$self->wipeCache();
if (!$self->options['uninstall_on_deletion']) {
return; // Nothing to do here.
}
$self->deleteAdvancedCache();
$self->deleteBaseDir();
if (is_multisite()) { // Main site CRON jobs.
switch_to_blog(get_current_site()->blog_id);
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_auto_cache');
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_cleanup');
restore_current_blog(); // Restore current blog.
} else { // Standard WP installation.
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_auto_cache');
wp_clear_scheduled_hook('_cron_'.GLOBAL_NS.'_cleanup');
}
$wpdb = $self->wpdb(); // WordPress DB.
$like = '%'.$wpdb->esc_like(GLOBAL_NS).'%';
if (is_multisite()) { // Site options for a network installation.
$wpdb->query('DELETE FROM `'.esc_sql($wpdb->sitemeta).'` WHERE `meta_key` LIKE \''.esc_sql($like).'\'');
switch_to_blog(get_current_site()->blog_id); // In case it started as a standard WP installation.
$wpdb->query('DELETE FROM `'.esc_sql($wpdb->options).'` WHERE `option_name` LIKE \''.esc_sql($like).'\'');
restore_current_blog(); // Restore current blog.
//
} else { // Standard WP installation.
$wpdb->query('DELETE FROM `'.esc_sql($wpdb->options).'` WHERE `option_name` LIKE \''.esc_sql($like).'\'');
}
};
/*
* Adds `define('WP_CACHE', TRUE);` to the `/wp-config.php` file.
*
* @since 150422 Rewrite.
*
* @return string The new contents of the updated `/wp-config.php` file;
* else an empty string if unable to add the `WP_CACHE` constant.
*/
$self->addWpCacheToWpConfig = function () use ($self) {
if (!$self->options['enable']) {
return ''; // Nothing to do.
}
if (!($wp_config_file = $self->findWpConfigFile())) {
return ''; // Unable to find `/wp-config.php`.
}
if (!is_readable($wp_config_file)) {
return ''; // Not possible.
}
if (!($wp_config_file_contents = file_get_contents($wp_config_file))) {
return ''; // Failure; could not read file.
}
if (!($wp_config_file_contents_no_whitespace = php_strip_whitespace($wp_config_file))) {
return ''; // Failure; file empty
}
if (preg_match('/\bdefine\s*\(\s*([\'"])WP_CACHE\\1\s*,\s*(?:\-?[1-9][0-9\.]*|TRUE|([\'"])(?:[^0\'"]|[^\'"]{2,})\\2)\s*\)\s*;/i', $wp_config_file_contents_no_whitespace)) {
return $wp_config_file_contents; // It's already in there; no need to modify this file.
}
if (!($wp_config_file_contents = $self->removeWpCacheFromWpConfig())) {
return ''; // Unable to remove previous value.
}
if (!($wp_config_file_contents = preg_replace('/^\s*(\<\?php|\<\?)\s+/i', '${1}'."\n"."define('WP_CACHE', TRUE);"."\n", $wp_config_file_contents, 1))) {
return ''; // Failure; something went terribly wrong here.
}
if (strpos($wp_config_file_contents, "define('WP_CACHE', TRUE);") === false) {
return ''; // Failure; unable to add; unexpected PHP code.
}
if (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS) {
return ''; // We may NOT edit any files.
}
if (!is_writable($wp_config_file)) {
return ''; // Not possible.
}
if (!file_put_contents($wp_config_file, $wp_config_file_contents)) {
return ''; // Failure; could not write changes.
}
return $wp_config_file_contents;
};
/*
* Removes `define('WP_CACHE', TRUE);` from the `/wp-config.php` file.
*
* @since 150422 Rewrite.
*
* @return string The new contents of the updated `/wp-config.php` file;
* else an empty string if unable to remove the `WP_CACHE` constant.
*/
$self->removeWpCacheFromWpConfig = function () use ($self) {
if (!($wp_config_file = $self->findWpConfigFile())) {
return ''; // Unable to find `/wp-config.php`.
}
if (!is_readable($wp_config_file)) {
return ''; // Not possible.
}
if (!($wp_config_file_contents = file_get_contents($wp_config_file))) {
return ''; // Failure; could not read file.
}
if (!($wp_config_file_contents_no_whitespace = php_strip_whitespace($wp_config_file))) {
return ''; // Failure; file empty
}
if (!preg_match('/([\'"])WP_CACHE\\1/i', $wp_config_file_contents_no_whitespace)) {
return $wp_config_file_contents; // Already gone.
}
if (preg_match('/\bdefine\s*\(\s*([\'"])WP_CACHE\\1\s*,\s*(?:0|FALSE|NULL|([\'"])0?\\2)\s*\)\s*;/i', $wp_config_file_contents_no_whitespace) && !is_writable($wp_config_file)) {
return $wp_config_file_contents; // It's already disabled, and since we can't write to this file let's let this slide.
}
if (!($wp_config_file_contents = preg_replace('/\bdefine\s*\(\s*([\'"])WP_CACHE\\1\s*,\s*(?:\-?[0-9\.]+|TRUE|FALSE|NULL|([\'"])[^\'"]*\\2)\s*\)\s*;/i', '', $wp_config_file_contents))) {
return ''; // Failure; something went terribly wrong here.
}
if (preg_match('/([\'"])WP_CACHE\\1/i', $wp_config_file_contents)) {
return ''; // Failure; perhaps the `/wp-config.php` file contains syntax we cannot remove safely.
}
if (defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS) {
return ''; // We may NOT edit any files.
}
if (!is_writable($wp_config_file)) {
return ''; // Not possible.
}
if (!file_put_contents($wp_config_file, $wp_config_file_contents)) {
return ''; // Failure; could not write changes.
}
return $wp_config_file_contents;
};
/*
* Checks to make sure the `advanced-cache.php` file still exists;
* and if it doesn't, the `advanced-cache.php` is regenerated automatically.
*
* @since 150422 Rewrite.
*
* @attaches-to `init` hook.
*
* @note This runs so that remote deployments which completely wipe out an
* existing set of website files (like the AWS Elastic Beanstalk does) will NOT cause ZenCache
* to stop functioning due to the lack of an `advanced-cache.php` file, which is generated by ZenCache.
*
* For instance, if you have a Git repo with all of your site files; when you push those files
* to your website to deploy them, you most likely do NOT have the `advanced-cache.php` file.
* ZenCache creates this file on its own. Thus, if it's missing (and QC is active)
* we simply regenerate the file automatically to keep ZenCache running.
*/
$self->checkAdvancedCache = function () use ($self) {
if (!$self->options['enable']) {
return; // Nothing to do.
}
if (!empty($_REQUEST[GLOBAL_NS])) {
return; // Skip on plugin actions.
}
$cache_dir = $self->cacheDir();
$advanced_cache_file = WP_CONTENT_DIR.'/advanced-cache.php';
$advanced_cache_check_file = $cache_dir.'/'.strtolower(SHORT_NAME).'-advanced-cache';
// Fixes zero-byte advanced-cache.php bug related to migrating from Quick Cache
// See: <https://github.com/websharks/zencache/issues/432>
// Also fixes a missing `define('WP_CACHE', TRUE)` bug related to migrating from Quick Cache
// See <https://github.com/websharks/zencache/issues/450>
if (!is_file($advanced_cache_check_file) || !is_file($advanced_cache_file) || filesize($advanced_cache_file) === 0) {
$self->addAdvancedCache();
$self->addWpCacheToWpConfig();
}
};
/*
* Creates and adds the `advanced-cache.php` file.
*
* @since 150422 Rewrite.
*
* @return bool|null `TRUE` on success. `FALSE` or `NULL` on failure.
* A special `NULL` return value indicates success with a single failure
* that is specifically related to the `[SHORT_NAME]-advanced-cache` file.
*/
$self->addAdvancedCache = function () use ($self) {
if (!$self->removeAdvancedCache()) {
return false; // Still exists.
}
$cache_dir = $self->cacheDir();
$advanced_cache_file = WP_CONTENT_DIR.'/advanced-cache.php';
$advanced_cache_check_file = $cache_dir.'/'.strtolower(SHORT_NAME).'-advanced-cache';
$advanced_cache_template = dirname(dirname(dirname(__FILE__))).'/templates/advanced-cache.txt';
if (is_file($advanced_cache_file) && !is_writable($advanced_cache_file)) {
return false; // Not possible to create.
}
if (!is_file($advanced_cache_file) && !is_writable(dirname($advanced_cache_file))) {
return false; // Not possible to create.
}
if (!is_file($advanced_cache_template) || !is_readable($advanced_cache_template)) {
return false; // Template file is missing; or not readable.
}
if (!($advanced_cache_contents = file_get_contents($advanced_cache_template))) {
return false; // Template file is missing; or is not readable.
}
$possible_advanced_cache_constant_key_values = array_merge(
$self->options, // The following additional keys are dynamic.
array('cache_dir' => $self->basePathTo($self->cache_sub_dir),
/*[pro strip-from="lite"]*/
'htmlc_cache_dir_public' => $self->basePathTo($self->htmlc_cache_sub_dir_public),
'htmlc_cache_dir_private' => $self->basePathTo($self->htmlc_cache_sub_dir_private),
/*[/pro]*/
)
);
foreach ($possible_advanced_cache_constant_key_values as $_option => $_value) {
$_value = (string) $_value; // Force string.
switch ($_option) {
case 'exclude_uris': // Converts to regex (caSe insensitive).
case 'exclude_refs': // Converts to regex (caSe insensitive).
case 'exclude_agents': // Converts to regex (caSe insensitive).
/*[pro strip-from="lite"]*/
case 'htmlc_css_exclusions': // Converts to regex (caSe insensitive).
case 'htmlc_js_exclusions': // Converts to regex (caSe insensitive).
/*[/pro]*/
$_value = "'".$self->escSq($self->lineDelimitedPatternsToRegex($_value))."'";
break; // Break switch handler.
/*[pro strip-from="lite"]*/
case 'version_salt': // This is PHP code; and we MUST validate syntax.
if ($_value && !is_wp_error($_response = wp_remote_post('http://phpcodechecker.com/api/', array('body' => array('code' => $_value))))
&& is_object($_response = json_decode(wp_remote_retrieve_body($_response))) && !empty($_response->errors) && strcasecmp($_response->errors, 'true') === 0
) {
$_value = ''; // PHP syntax errors; empty this.
$self->enqueueMainError(sprintf(__('<strong>%1$s</strong>: ignoring your Version Salt; it seems to contain PHP syntax errors.', SLUG_TD), esc_html(NAME)));
}
if (!$_value) {
$_value = "''"; // Use an empty string (default).
}
break; // Break switch handler.
/*[/pro]*/
default: // Default case handler.
$_value = "'".$self->escSq($_value)."'";
break; // Break switch handler.
}
$advanced_cache_contents = // Fill replacement codes.
str_ireplace(
array(
"'%%".GLOBAL_NS.'_'.$_option."%%'",
"'%%".GLOBAL_NS.'_'.preg_replace('/^cache_/i', '', $_option)."%%'",
),
$_value,
$advanced_cache_contents
);
}
unset($_option, $_value, $_values, $_response); // Housekeeping.
if (strpos(PLUGIN_FILE, WP_CONTENT_DIR) === 0) {
$plugin_file = "WP_CONTENT_DIR.'".$self->escSq(str_replace(WP_CONTENT_DIR, '', PLUGIN_FILE))."'";
} else {
$plugin_file = "'".$self->escSq(PLUGIN_FILE)."'"; // Full absolute path.
}
// Make it possible for the `advanced-cache.php` handler to find the plugin directory reliably.
$advanced_cache_contents = str_ireplace("'%%".GLOBAL_NS."_PLUGIN_FILE%%'", $plugin_file, $advanced_cache_contents);
// Ignore; this is created by ZenCache; and we don't need to obey in this case.
#if(defined('DISALLOW_FILE_MODS') && DISALLOW_FILE_MODS)
# return FALSE; // We may NOT edit any files.
if (!file_put_contents($advanced_cache_file, $advanced_cache_contents)) {
return false; // Failure; could not write file.
}
$cache_lock = $self->cacheLock(); // Lock cache.
if (!is_dir($cache_dir)) {
mkdir($cache_dir, 0775, true);
}
if (is_writable($cache_dir) && !is_file($cache_dir.'/.htaccess')) {
file_put_contents($cache_dir.'/.htaccess', $self->htaccess_deny);
}
if (!is_dir($cache_dir) || !is_writable($cache_dir) || !is_file($cache_dir.'/.htaccess') || !file_put_contents($advanced_cache_check_file, time())) {
$self->cacheUnlock($cache_lock); // Release.
return; // Special return value (NULL).
}
$self->cacheUnlock($cache_lock); // Release.
return true;
};
/*
* Removes the `advanced-cache.php` file.
*
* @since 150422 Rewrite.
*
* @return bool `TRUE` on success. `FALSE` on failure.
*
* @note The `advanced-cache.php` file is NOT actually deleted by this routine.
* Instead of deleting the file, we simply empty it out so that it's `0` bytes in size.
*
* The reason for this is to preserve any file permissions set by the site owner.
* If the site owner previously allowed this specific file to become writable, we don't want to
* lose that permission by deleting the file; forcing the site owner to do it all over again later.
*
* An example of where this is useful is when a site owner deactivates the QC plugin,
* but later they decide that QC really is the most awesome plugin in the world and they turn it back on.
*/
$self->removeAdvancedCache = function () use ($self) {
$advanced_cache_file = WP_CONTENT_DIR.'/advanced-cache.php';
if (!is_file($advanced_cache_file)) {
return true; // Already gone.
}
if (is_readable($advanced_cache_file) && filesize($advanced_cache_file) === 0) {
return true; // Already gone; i.e. it's empty already.
}
if (!is_writable($advanced_cache_file)) {
return false; // Not possible.
}
/* Empty the file only. This way permissions are NOT lost in cases where
a site owner makes this specific file writable for ZenCache. */
if (file_put_contents($advanced_cache_file, '') !== 0) {
return false; // Failure.
}
return true;
};
/*
* Deletes the `advanced-cache.php` file.
*
* @since 150422 Rewrite.
*
* @return bool `TRUE` on success. `FALSE` on failure.
*
* @note The `advanced-cache.php` file is deleted by this routine.
*/
$self->deleteAdvancedCache = function () use ($self) {
$cache_dir = $self->cacheDir();
$advanced_cache_file = WP_CONTENT_DIR.'/advanced-cache.php';
$advanced_cache_check_file = $cache_dir.'/'.strtolower(SHORT_NAME).'-advanced-cache';
if (is_file($advanced_cache_file)) {
if (!is_writable($advanced_cache_file) || !unlink($advanced_cache_file)) {
return false; // Not possible; or outright failure.
}
}
if (is_file($advanced_cache_check_file)) {
if (!is_writable($advanced_cache_check_file) || !unlink($advanced_cache_check_file)) {
return false; // Not possible; or outright failure.
}
}
return true; // Deletion success.
};
/*
* Checks to make sure the `[SHORT_NAME]-blog-paths` file still exists;
* and if it doesn't, the `[SHORT_NAME]-blog-paths` file is regenerated automatically.
*
* @since 150422 Rewrite.
*
* @attaches-to `init` hook.
*
* @note This runs so that remote deployments which completely wipe out an
* existing set of website files (like the AWS Elastic Beanstalk does) will NOT cause ZenCache
* to stop functioning due to the lack of a `[SHORT_NAME]-blog-paths` file, which is generated by ZenCache.
*
* For instance, if you have a Git repo with all of your site files; when you push those files
* to your website to deploy them, you most likely do NOT have the `[SHORT_NAME]-blog-paths` file.
* ZenCache creates this file on its own. Thus, if it's missing (and QC is active)
* we simply regenerate the file automatically to keep ZenCache running.
*/
$self->checkBlogPaths = function () use ($self) {
if (!$self->options['enable']) {
return; // Nothing to do.
}
if (!is_multisite()) {
return; // N/A.
}
if (!empty($_REQUEST[GLOBAL_NS])) {
return; // Skip on plugin actions.
}
$cache_dir = $self->cacheDir();
$blog_paths_file = $cache_dir.'/'.strtolower(SHORT_NAME).'-blog-paths';
if (!is_file($blog_paths_file)) {
$self->updateBlogPaths();
}
};
/*
* Creates and/or updates the `[SHORT_NAME]-blog-paths` file.
*
* @since 150422 Rewrite.
*
* @attaches-to `enable_live_network_counts` filter.
*
* @param mixed $enable_live_network_counts Optional, defaults to a `NULL` value.
*
* @return mixed The value of `$enable_live_network_counts` (passes through).
*
* @note While this routine is attached to a WP filter, we also call upon it directly at times.
*/
$self->updateBlogPaths = function ($enable_live_network_counts = null) use ($self) {
$value = $enable_live_network_counts; // This hook actually rides on a filter.
if (!$self->options['enable']) {
return $value; // Nothing to do.
}
if (!is_multisite()) {
return $value; // N/A.
}
$cache_dir = $self->cacheDir();
$blog_paths_file = $cache_dir.'/'.strtolower(SHORT_NAME).'-blog-paths';
$cache_lock = $self->cacheLock();
if (!is_dir($cache_dir)) {
mkdir($cache_dir, 0775, true);
}
if (is_writable($cache_dir) && !is_file($cache_dir.'/.htaccess')) {
file_put_contents($cache_dir.'/.htaccess', $self->htaccess_deny);
}
if (is_dir($cache_dir) && is_writable($cache_dir)) {
$paths = // Collect child `[/base]/path/`s from the WordPress database.
$self->wpdb()->get_col('SELECT `path` FROM `'.esc_sql($self->wpdb()->blogs)."` WHERE `deleted` <= '0'");
$host_base_token = $self->hostBaseToken(); // Pull this once only.
foreach ($paths as $_key => &$_path) {
if ($_path && $_path !== '/' && $host_base_token && $host_base_token !== '/') {
// Note that each `path` in the DB looks like: `[/base]/path/` (i.e., it includes base).
$_path = '/'.ltrim(preg_replace('/^'.preg_quote($host_base_token, '/').'/', '', $_path), '/');
}
if (!$_path || $_path === '/') {
unset($paths[$_key]); // Exclude main site.
}
}
unset($_key, $_path); // Housekeeping.
file_put_contents($blog_paths_file, serialize($paths));
}
$self->cacheUnlock($cache_lock); // Release.
return $value; // Pass through untouched (always).
};
/*
* Deletes base directory.
*
* @since 151002 Improving multisite compat.
*
* @return int Total files removed by this routine (if any).
*/
$self->deleteBaseDir = function () use ($self) {
$counter = 0; // Initialize.
@set_time_limit(1800); // @TODO Display a warning.
$counter += $self->deleteAllFilesDirsIn($self->wpContentBaseDirTo(''), true);
return $counter;
};