| 1 |
<?php |
| 2 |
/** |
| 3 |
* Docket Cache. |
| 4 |
* |
| 5 |
* @author Nawawi Jamili |
| 6 |
* @license MIT |
| 7 |
* |
| 8 |
* @see https://github.com/nawawi/docket-cache |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Nawawi\DocketCache; |
| 12 |
|
| 13 |
\defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
final class Dropino extends Bepart |
| 16 |
{ |
| 17 |
private $path; |
| 18 |
public $wpcondir; |
| 19 |
public $condir; |
| 20 |
|
| 21 |
public function __construct($path) |
| 22 |
{ |
| 23 |
$this->path = wp_normalize_path($path); |
| 24 |
|
| 25 |
$this->wpcondir = \defined('WP_CONTENT_DIR') ? WP_CONTENT_DIR : ABSPATH.'wp-content'; |
| 26 |
$this->wpcondir = wp_normalize_path($this->wpcondir); |
| 27 |
|
| 28 |
$this->condir = \defined('DOCKET_CACHE_CONTENT_PATH') ? DOCKET_CACHE_CONTENT_PATH : $this->wpcondir; |
| 29 |
$this->condir = wp_normalize_path($this->condir); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* file. |
| 34 |
*/ |
| 35 |
public function resc() |
| 36 |
{ |
| 37 |
$dt = []; |
| 38 |
$dt['src'] = $this->path.'/includes/object-cache.php'; |
| 39 |
$dt['dst'] = $this->condir.'/object-cache.php'; |
| 40 |
$dt['wpdst'] = $this->wpcondir.'/object-cache.php'; |
| 41 |
|
| 42 |
// sync with includes/object-cache.php |
| 43 |
$dt['halt'] = $this->condir.'/.object-cache-delay.txt'; |
| 44 |
$dt['after'] = $this->condir.'/.object-cache-after-delay.txt'; |
| 45 |
|
| 46 |
return (object) $dt; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* exists. |
| 51 |
*/ |
| 52 |
public function exists() |
| 53 |
{ |
| 54 |
clearstatcache(); |
| 55 |
|
| 56 |
return @is_file($this->resc()->dst); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* meta. |
| 61 |
*/ |
| 62 |
private function meta($key) |
| 63 |
{ |
| 64 |
static $cache = []; |
| 65 |
|
| 66 |
if (isset($cache[$key])) { |
| 67 |
return $cache[$key]; |
| 68 |
} |
| 69 |
|
| 70 |
$cache['dropino'] = $this->plugin_meta($this->condir.'/object-cache.php'); |
| 71 |
$cache['plugin'] = $this->plugin_meta($this->path.'/includes/object-cache.php'); |
| 72 |
|
| 73 |
return $cache[$key]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* validate. |
| 78 |
*/ |
| 79 |
public function validate() |
| 80 |
{ |
| 81 |
if (!$this->exists()) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
if (0 !== strcmp(nwdcx_noscheme($this->meta('dropino')['PluginURI']), nwdcx_noscheme($this->meta('plugin')['PluginURI']))) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* is_valid. |
| 94 |
*/ |
| 95 |
public function is_outdated() |
| 96 |
{ |
| 97 |
return version_compare($this->meta('dropino')['Version'], $this->meta('plugin')['Version'], '<') || false === strpos($this->meta('dropino')['Version'], '.'); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* delay. |
| 102 |
*/ |
| 103 |
public function delay($seconds = 5) |
| 104 |
{ |
| 105 |
$time = time() + (int) $seconds; |
| 106 |
$file_delay = $this->resc()->halt; |
| 107 |
if ($this->put($file_delay, $time)) { |
| 108 |
@touch($file_delay, $time); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* undelay. |
| 114 |
*/ |
| 115 |
public function undelay() |
| 116 |
{ |
| 117 |
$file_delay = $this->resc()->halt; |
| 118 |
$after_delay = $this->resc()->after; |
| 119 |
if (@is_file($file_delay)) { |
| 120 |
@unlink($file_delay); |
| 121 |
} |
| 122 |
if (@is_file($after_delay)) { |
| 123 |
@unlink($after_delay); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* delay_expire. |
| 129 |
*/ |
| 130 |
public function delay_expire() |
| 131 |
{ |
| 132 |
$file_delay = $this->resc()->halt; |
| 133 |
$after_delay = $this->resc()->after; |
| 134 |
if (@is_file($file_delay) && time() > @filemtime($file_delay)) { |
| 135 |
@rename($file_delay, $after_delay); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* after_delay. |
| 141 |
*/ |
| 142 |
public function after_delay() |
| 143 |
{ |
| 144 |
$after_delay = $this->resc()->after; |
| 145 |
if (@is_file($after_delay)) { |
| 146 |
if (@unlink($after_delay)) { |
| 147 |
return $this->code_worker(['flush', 'preload']); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* install. |
| 156 |
*/ |
| 157 |
public function install($delay = false) |
| 158 |
{ |
| 159 |
if (nwdcx_construe('DISABLED')) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
$src = $this->resc()->src; |
| 164 |
$dst = $this->resc()->dst; |
| 165 |
|
| 166 |
if (is_writable(\dirname($dst))) { |
| 167 |
if ($delay) { |
| 168 |
$this->delay(); |
| 169 |
} |
| 170 |
|
| 171 |
if ($this->copy($src, $dst)) { |
| 172 |
// refresh |
| 173 |
$this->opcache_flush($dst); |
| 174 |
|
| 175 |
$this->multinet_active(true); |
| 176 |
|
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* uninstall. |
| 186 |
*/ |
| 187 |
public function uninstall($delay = false) |
| 188 |
{ |
| 189 |
if (nwdcx_construe('DISABLED')) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
$file = $this->resc()->dst; |
| 194 |
|
| 195 |
$this->undelay(); |
| 196 |
|
| 197 |
if (!@is_file($file)) { |
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
// remove flag file to trigger disable at admin interface |
| 202 |
$this->multinet_active(false); |
| 203 |
|
| 204 |
// dont remove drop-in if active on sub network. |
| 205 |
if ((!\defined('WP_CLI') || !WP_CLI) && $this->multinet_available()) { |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
$this->opcache_flush($file); |
| 210 |
|
| 211 |
if (is_writable($file) && @unlink($file)) { |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
public function multinet_install($hook) |
| 219 |
{ |
| 220 |
if (!is_multisite() || !nwdcx_network_multi()) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
if (!nwdcx_wpdb($wpdb)) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
$table = $wpdb->base_prefix.'sitemeta'; |
| 229 |
|
| 230 |
$network_id = null; |
| 231 |
|
| 232 |
$suppress = $wpdb->suppress_errors(true); |
| 233 |
|
| 234 |
$query = "SELECT `site_id`,`meta_key`,`meta_value` FROM `{$table}` WHERE `meta_key`='active_sitewide_plugins' ORDER BY site_id ASC LIMIT 100"; |
| 235 |
$results = $wpdb->get_results($query, ARRAY_A); |
| 236 |
if (!empty($results) && \is_array($results)) { |
| 237 |
while ($row = @array_shift($results)) { |
| 238 |
$site_id = $row['site_id']; |
| 239 |
$meta_key = $row['meta_key']; |
| 240 |
$meta_value = maybe_unserialize($row['meta_value']); |
| 241 |
|
| 242 |
if (null === $network_id) { |
| 243 |
$network_id = $site_id; |
| 244 |
} |
| 245 |
|
| 246 |
if (!empty($meta_value) && \is_array($meta_value) && !empty($meta_value[$hook])) { |
| 247 |
$this->multinet_active(true, $site_id); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if (null !== $network_id) { |
| 253 |
@file_put_contents($this->condir.'/.object-cache-network-main.txt', $network_id, \LOCK_EX); |
| 254 |
} |
| 255 |
|
| 256 |
$wpdb->suppress_errors($suppress); |
| 257 |
} |
| 258 |
|
| 259 |
private function multinet_clear_main($cleanup = false) |
| 260 |
{ |
| 261 |
$file = $this->condir.'/.object-cache-network-main.txt'; |
| 262 |
if (@is_file($file)) { |
| 263 |
@unlink($file); |
| 264 |
} |
| 265 |
|
| 266 |
if ($cleanup) { |
| 267 |
$file = $this->condir.'/.object-cache-network-multi.txt'; |
| 268 |
if (@is_file($file)) { |
| 269 |
@unlink($file); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
private function multinet_list() |
| 275 |
{ |
| 276 |
$list = []; |
| 277 |
$files = @glob($this->condir.'/.object-cache-network-*.txt', \GLOB_MARK | \GLOB_NOSORT); |
| 278 |
if (!empty($files) && \is_array($files)) { |
| 279 |
foreach ($files as $file) { |
| 280 |
$fx = basename($file); |
| 281 |
if (@preg_match('@^\.object-cache-network-(\d+)\.txt$@', $fx, $mm)) { |
| 282 |
$id = $mm[1]; |
| 283 |
$list[$id] = $file; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return !empty($list) ? $list : false; |
| 289 |
} |
| 290 |
|
| 291 |
public function multinet_available() |
| 292 |
{ |
| 293 |
$network_id = get_current_network_id(); |
| 294 |
$files = $this->multinet_list(); |
| 295 |
if (!empty($files) && \is_array($files)) { |
| 296 |
foreach ($files as $id => $file) { |
| 297 |
if ($id !== $network_id) { |
| 298 |
return true; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
public function multinet_clear($cache_path, $logfile) |
| 307 |
{ |
| 308 |
clearstatcache(); |
| 309 |
$this->multinet_clear_main(true); |
| 310 |
|
| 311 |
$files = $this->multinet_list(); |
| 312 |
if (!empty($files) && \is_array($files)) { |
| 313 |
foreach ($files as $network_id => $file) { |
| 314 |
$cachepath = $cache_path; |
| 315 |
if (@is_file($file)) { |
| 316 |
if (false === strpos($cache_path, '/network-')) { |
| 317 |
$cachepath = $cache_path.'/network-'.$network_id.'/'; |
| 318 |
} |
| 319 |
|
| 320 |
if (@is_dir($cachepath)) { |
| 321 |
$this->cachedir_flush($cachepath); |
| 322 |
} |
| 323 |
@unlink($file); |
| 324 |
} |
| 325 |
|
| 326 |
$ext = substr($logfile, -4); |
| 327 |
$fname = substr($logfile, 0, -4); |
| 328 |
$logfile = $fname.'-'.$network_id.$ext; |
| 329 |
if (@is_file($logfile)) { |
| 330 |
@unlink($logfile); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
public function multinet_tag($network_id = false) |
| 337 |
{ |
| 338 |
$network_id = empty($network_id) ? get_current_network_id() : $network_id; |
| 339 |
|
| 340 |
return sprintf('%s/.object-cache-network-%s.txt', $this->condir, $network_id); |
| 341 |
} |
| 342 |
|
| 343 |
public function multinet_active($status = false, $network_id = false) |
| 344 |
{ |
| 345 |
if (!is_multisite() || !nwdcx_network_multi()) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
$lock_file = $this->condir.'/.object-cache-network-multi.txt'; |
| 350 |
if (!@is_file($lock_file)) { |
| 351 |
@file_put_contents($lock_file, 1, \LOCK_EX); |
| 352 |
} |
| 353 |
|
| 354 |
if (empty($network_id)) { |
| 355 |
$this->multinet_clear_main(); |
| 356 |
$network_id = get_current_network_id(); |
| 357 |
} |
| 358 |
|
| 359 |
$file = $this->multinet_tag($network_id); |
| 360 |
|
| 361 |
clearstatcache(); |
| 362 |
|
| 363 |
$is_file = @is_file($file); |
| 364 |
if ($status) { |
| 365 |
if ($is_file) { |
| 366 |
return true; |
| 367 |
} |
| 368 |
|
| 369 |
return $this->put($file, $network_id); |
| 370 |
} |
| 371 |
|
| 372 |
if (!$is_file) { |
| 373 |
return true; |
| 374 |
} |
| 375 |
|
| 376 |
return @unlink($file); |
| 377 |
} |
| 378 |
|
| 379 |
public function multinet_me() |
| 380 |
{ |
| 381 |
if (!is_multisite()) { |
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
if (!nwdcx_network_multi()) { |
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
$file = $this->multinet_tag(); |
| 390 |
|
| 391 |
return @is_file($file); |
| 392 |
} |
| 393 |
|
| 394 |
public function is_alternative() |
| 395 |
{ |
| 396 |
if (0 === strcmp($this->wpcondir, $this->condir)) { |
| 397 |
return false; |
| 398 |
} |
| 399 |
|
| 400 |
return true; |
| 401 |
} |
| 402 |
} |
| 403 |
|