| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: ManageWP - Worker |
| 4 |
Plugin URI: https://managewp.com |
| 5 |
Description: ManageWP Worker plugin allows you to manage your WordPress sites from one dashboard. Visit <a href="https://managewp.com">ManageWP.com</a> for more information. |
| 6 |
Version: 4.1.0 |
| 7 |
Author: ManageWP |
| 8 |
Author URI: https://managewp.com |
| 9 |
License: GPL2 |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* This file is part of the ManageWP Worker plugin. |
| 14 |
* |
| 15 |
* (c) ManageWP LLC <contact@managewp.com> |
| 16 |
* |
| 17 |
* For the full copyright and license information, please view the LICENSE |
| 18 |
* file that was distributed with this source code. |
| 19 |
*/ |
| 20 |
|
| 21 |
if (!defined('ABSPATH')) { |
| 22 |
exit; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Handler for incomplete plugin installations. |
| 27 |
*/ |
| 28 |
if (!function_exists('mwp_fail_safe')): |
| 29 |
/** |
| 30 |
* Reserved memory for fatal error handling execution context. |
| 31 |
*/ |
| 32 |
$GLOBALS['mwp_reserved_memory'] = str_repeat(' ', 1024 * 20); |
| 33 |
/** |
| 34 |
* If we ever get only partially upgraded due to a server error or misconfiguration, |
| 35 |
* attempt to disable the plugin. |
| 36 |
*/ |
| 37 |
function mwp_fail_safe() |
| 38 |
{ |
| 39 |
$GLOBALS['mwp_reserved_memory'] = null; |
| 40 |
|
| 41 |
$lastError = error_get_last(); |
| 42 |
|
| 43 |
if (!$lastError || $lastError['type'] !== E_ERROR) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$activePlugins = get_option('active_plugins'); |
| 48 |
$workerIndex = array_search(plugin_basename(__FILE__), $activePlugins); |
| 49 |
if ($workerIndex === false) { |
| 50 |
// Plugin is not yet enabled, possibly in activation context. |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
$errorSource = realpath($lastError['file']); |
| 55 |
// We might be in eval() context. |
| 56 |
if (!$errorSource) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
// The only fatal error that we would get would be a 'Class 'X' not found in ...', so look out only for those messages. |
| 61 |
if (!preg_match('/^Class \'[^\']+\' not found$/', $lastError['message'])) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Only look for files that belong to this plugin. |
| 66 |
$pluginBase = realpath(dirname(__FILE__)); |
| 67 |
if (stripos($errorSource, $pluginBase) !== 0) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
unset($activePlugins[$workerIndex]); |
| 72 |
// Reset indexes. |
| 73 |
$activePlugins = array_values($activePlugins); |
| 74 |
update_option('active_plugins', $activePlugins); |
| 75 |
|
| 76 |
// We probably won't have access to the wp_mail function. |
| 77 |
$mailFn = function_exists('wp_mail') ? 'wp_mail' : 'mail'; |
| 78 |
$siteUrl = get_option('siteurl'); |
| 79 |
$title = sprintf("ManageWP Worker deactivated on %s", $siteUrl); |
| 80 |
$to = get_option('admin_email'); |
| 81 |
$brand = get_option('mwp_worker_brand'); |
| 82 |
if (!empty($brand['admin_email'])) { |
| 83 |
$to = $brand['admin_email']; |
| 84 |
} |
| 85 |
|
| 86 |
$fullError = print_r($lastError, 1); |
| 87 |
$workerSettings = get_option('wrksettings'); |
| 88 |
$userID = 0; |
| 89 |
if (!empty($workerSettings['dataown'])) { |
| 90 |
$userID = (int) $workerSettings['dataown']; |
| 91 |
} |
| 92 |
$body = sprintf("Worker deactivation due to an error. The site that was affected - %s. User email - %s (User ID: %s). Worker version: %s (%s). The error that caused this:\n<pre>%s</pre>", $siteUrl, $to, $userID, $GLOBALS['MMB_WORKER_VERSION'], $GLOBALS['MMB_WORKER_REVISION'], $fullError); |
| 93 |
$mailFn('dev@managewp.com', $title, $body); |
| 94 |
|
| 95 |
// If we're inside a cron scope, don't attempt to hide this error. |
| 96 |
if (defined('DOING_CRON') && DOING_CRON) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
// If we're inside a normal request scope retry the request so user doesn't have to see an ugly error page. |
| 101 |
if (!empty($_SERVER['REQUEST_URI'])) { |
| 102 |
$siteUrl .= $_SERVER['REQUEST_URI']; |
| 103 |
} |
| 104 |
if (headers_sent()) { |
| 105 |
// The headers are probably sent if the PHP configuration has the 'display_errors' directive enabled. In that case try a meta redirect. |
| 106 |
printf('<meta http-equiv="refresh" content="0; url=%s">', htmlspecialchars($siteUrl, ENT_QUOTES)); |
| 107 |
} else { |
| 108 |
header('Location: '.htmlspecialchars($siteUrl, ENT_QUOTES)); |
| 109 |
} |
| 110 |
exit; |
| 111 |
} |
| 112 |
|
| 113 |
register_shutdown_function('mwp_fail_safe'); |
| 114 |
endif; |
| 115 |
|
| 116 |
if (!class_exists('MwpWorkerResponder', false)): |
| 117 |
/** |
| 118 |
* We're not allowed to use lambda functions because this is PHP 5.2, so use a responder |
| 119 |
* class that's able to access the service container. |
| 120 |
*/ |
| 121 |
class MwpWorkerResponder |
| 122 |
{ |
| 123 |
|
| 124 |
private $container; |
| 125 |
|
| 126 |
private $responseSent = false; |
| 127 |
|
| 128 |
function __construct(MWP_ServiceContainer_Interface $container) |
| 129 |
{ |
| 130 |
$this->container = $container; |
| 131 |
} |
| 132 |
|
| 133 |
function callback(Exception $e = null, MWP_Http_ResponseInterface $response = null) |
| 134 |
{ |
| 135 |
if ($response !== null) { |
| 136 |
$responseEvent = new MWP_Event_MasterResponse($response); |
| 137 |
$this->container->getEventDispatcher()->dispatch(MWP_Event_Events::MASTER_RESPONSE, $responseEvent); |
| 138 |
$lastResponse = $responseEvent->getResponse(); |
| 139 |
|
| 140 |
if ($lastResponse !== null) { |
| 141 |
if (!$this->responseSent) { |
| 142 |
// This looks pretty ugly, but the "execute PHP" function handles fatal errors and wraps them |
| 143 |
// in a valid action response. That fatal error may also be handled by the global fatal error |
| 144 |
// handler, which also wraps the error in a response. We keep the state in this class, so we |
| 145 |
// don't send a worker response twice, first time as an action response, second time as a |
| 146 |
// global response. |
| 147 |
// If this is to be removed, simply remove fatal error handling from the "execute PHP" action. |
| 148 |
$lastResponse->send(); |
| 149 |
$this->responseSent = true; |
| 150 |
} |
| 151 |
exit; |
| 152 |
} |
| 153 |
} elseif ($e !== null) { |
| 154 |
// Exception is thrown and the response is empty. This should never happen, so don't try to hide it. |
| 155 |
throw $e; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @return callable |
| 161 |
*/ |
| 162 |
public function getCallback() |
| 163 |
{ |
| 164 |
return array($this, 'callback'); |
| 165 |
} |
| 166 |
} |
| 167 |
endif; |
| 168 |
|
| 169 |
if (!function_exists('mwp_container')): |
| 170 |
/** |
| 171 |
* @return MWP_ServiceContainer_Interface |
| 172 |
*/ |
| 173 |
function mwp_container() |
| 174 |
{ |
| 175 |
static $container; |
| 176 |
|
| 177 |
if ($container === null) { |
| 178 |
$parameters = (array) get_option('mwp_container_parameters', array()); |
| 179 |
$container = new MWP_ServiceContainer_Production(array( |
| 180 |
'worker_realpath' => __FILE__, |
| 181 |
'worker_basename' => 'worker/init.php', |
| 182 |
'worker_version' => $GLOBALS['MMB_WORKER_VERSION'], |
| 183 |
'worker_revision' => $GLOBALS['MMB_WORKER_REVISION'], |
| 184 |
) + $parameters); |
| 185 |
} |
| 186 |
|
| 187 |
return $container; |
| 188 |
} |
| 189 |
endif; |
| 190 |
|
| 191 |
if (!function_exists('mwp_init')): |
| 192 |
function mwp_init() |
| 193 |
{ |
| 194 |
// Ensure PHP version compatibility. |
| 195 |
if (version_compare(PHP_VERSION, '5.2', '<')) { |
| 196 |
trigger_error("ManageWP Worker plugin requires PHP 5.2 or higher.", E_USER_ERROR); |
| 197 |
exit; |
| 198 |
} |
| 199 |
|
| 200 |
// Register the autoloader that loads everything except the Google namespace. |
| 201 |
if (version_compare(PHP_VERSION, '5.3', '<')) { |
| 202 |
spl_autoload_register('mwp_autoload'); |
| 203 |
} else { |
| 204 |
// The prepend parameter was added in PHP 5.3.0 |
| 205 |
spl_autoload_register('mwp_autoload', true, true); |
| 206 |
} |
| 207 |
|
| 208 |
$GLOBALS['MMB_WORKER_VERSION'] = '4.1.0'; |
| 209 |
$GLOBALS['MMB_WORKER_REVISION'] = '2015-04-23 00:00:00'; |
| 210 |
$GLOBALS['mmb_plugin_dir'] = WP_PLUGIN_DIR.'/'.basename(dirname(__FILE__)); |
| 211 |
$GLOBALS['_mmb_item_filter'] = array(); |
| 212 |
$GLOBALS['mmb_core'] = $core = $GLOBALS['mmb_core_backup'] = new MMB_Core(); |
| 213 |
|
| 214 |
$siteUrl = function_exists('get_site_option') ? get_site_option('siteurl') : get_option('siteurl'); |
| 215 |
define('MMB_XFRAME_COOKIE', 'wordpress_'.md5($siteUrl).'_xframe'); |
| 216 |
|
| 217 |
define('MWP_BACKUP_DIR', WP_CONTENT_DIR.'/managewp/backups'); |
| 218 |
define('MWP_DB_DIR', MWP_BACKUP_DIR.'/mwp_db'); |
| 219 |
|
| 220 |
add_filter('mmb_stats_filter', 'mmb_get_extended_info'); |
| 221 |
add_action('plugins_loaded', 'mwp_return_core_reference', 1); |
| 222 |
add_filter('cron_schedules', 'mmb_more_reccurences'); |
| 223 |
add_action('mmb_remote_upload', 'mmb_call_scheduled_remote_upload'); |
| 224 |
add_action('mwp_datasend', 'mwp_datasend'); |
| 225 |
add_action('init', 'mmb_plugin_actions', 99999); |
| 226 |
add_filter('install_plugin_complete_actions', 'mmb_iframe_plugins_fix'); |
| 227 |
add_filter('comment_edit_redirect', 'mwb_edit_redirect_override'); |
| 228 |
|
| 229 |
// Datasend cron. |
| 230 |
if (!wp_next_scheduled('mwp_datasend')) { |
| 231 |
wp_schedule_event(time(), 'threehours', 'mwp_datasend'); |
| 232 |
} |
| 233 |
|
| 234 |
// Register updater hooks. |
| 235 |
MMB_Updater::register(); |
| 236 |
|
| 237 |
// Plugin management hooks. |
| 238 |
register_activation_hook(__FILE__, array($core, 'install')); |
| 239 |
register_deactivation_hook(__FILE__, array($core, 'deactivate')); |
| 240 |
register_uninstall_hook(dirname(__FILE__).'/functions.php', 'mwp_uninstall'); |
| 241 |
|
| 242 |
// Don't send the "X-Frame-Options: SAMEORIGIN" header if we're logging in inside an iframe. |
| 243 |
if (isset($_COOKIE[MMB_XFRAME_COOKIE])) { |
| 244 |
remove_action('admin_init', 'send_frame_options_header'); |
| 245 |
remove_action('login_init', 'send_frame_options_header'); |
| 246 |
} |
| 247 |
|
| 248 |
// Remove legacy scheduler. |
| 249 |
if (wp_next_scheduled('mwp_backup_tasks')) { |
| 250 |
wp_clear_scheduled_hook('mwp_backup_tasks'); |
| 251 |
} |
| 252 |
|
| 253 |
mwp_set_plugin_priority(); |
| 254 |
|
| 255 |
$request = MWP_Worker_Request::createFromGlobals(); |
| 256 |
$container = mwp_container(); |
| 257 |
$responder = new MwpWorkerResponder($container); |
| 258 |
|
| 259 |
$kernel = new MWP_Worker_Kernel($container); |
| 260 |
$kernel->handleRequest($request, $responder->getCallback(), true); |
| 261 |
} |
| 262 |
|
| 263 |
require_once dirname(__FILE__).'/functions.php'; |
| 264 |
|
| 265 |
mwp_init(); |
| 266 |
endif; |
| 267 |
|