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