| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: ManageWP - Worker |
| 4 |
Plugin URI: http://managewp.com/ |
| 5 |
Description: Manage all your blogs from one dashboard |
| 6 |
Author: Prelovac Media |
| 7 |
Version: 3.9.0 |
| 8 |
Author URI: http://www.prelovac.com |
| 9 |
*/ |
| 10 |
|
| 11 |
/************************************************************* |
| 12 |
* |
| 13 |
* init.php |
| 14 |
* |
| 15 |
* Initialize the communication with master |
| 16 |
* |
| 17 |
* |
| 18 |
* Copyright (c) 2011 Prelovac Media |
| 19 |
* www.prelovac.com |
| 20 |
**************************************************************/ |
| 21 |
|
| 22 |
|
| 23 |
define('MMB_WORKER_VERSION', '3.9.0'); |
| 24 |
|
| 25 |
global $wpdb, $mmb_plugin_dir, $mmb_plugin_url; |
| 26 |
|
| 27 |
if (version_compare(PHP_VERSION, '5.0.0', '<')) // min version 5 supported |
| 28 |
exit("<p>ManageWP Worker plugin requires PHP 5 or higher.</p>"); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
global $wp_version; |
| 33 |
|
| 34 |
$mmb_wp_version = $wp_version; |
| 35 |
$mmb_plugin_dir = WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)); |
| 36 |
$mmb_plugin_url = WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)); |
| 37 |
|
| 38 |
$mmb_actions = array( |
| 39 |
'remove_site' => 'mmb_remove_site', |
| 40 |
'get_stats' => 'mmb_stats_get', |
| 41 |
'get_stats_notification' => 'mmb_get_stats_notification', |
| 42 |
'backup' => 'mmb_backup_now', |
| 43 |
'restore' => 'mmb_restore_now', |
| 44 |
'optimize_tables' => 'mmb_optimize_tables', |
| 45 |
'check_wp_version' => 'mmb_wp_checkversion', |
| 46 |
'create_post' => 'mmb_post_create', |
| 47 |
'update_worker' => 'mmb_update_worker_plugin', |
| 48 |
'change_comment_status' => 'mmb_change_comment_status', |
| 49 |
'change_post_status' => 'mmb_change_post_status', |
| 50 |
'get_comment_stats' => 'mmb_comment_stats_get', |
| 51 |
'install_addon' => 'mmb_install_addon', |
| 52 |
'do_upgrade' => 'mmb_do_upgrade', |
| 53 |
'add_link' => 'mmb_add_link', |
| 54 |
'email_backup' => 'mmb_email_backup', |
| 55 |
'check_backup_compat' => 'mmb_check_backup_compat', |
| 56 |
'execute_php_code' => 'mmb_execute_php_code' |
| 57 |
); |
| 58 |
|
| 59 |
require_once("$mmb_plugin_dir/helper.class.php"); |
| 60 |
require_once("$mmb_plugin_dir/core.class.php"); |
| 61 |
require_once("$mmb_plugin_dir/post.class.php"); |
| 62 |
require_once("$mmb_plugin_dir/comment.class.php"); |
| 63 |
require_once("$mmb_plugin_dir/stats.class.php"); |
| 64 |
require_once("$mmb_plugin_dir/backup.class.php"); |
| 65 |
require_once("$mmb_plugin_dir/installer.class.php"); |
| 66 |
require_once("$mmb_plugin_dir/link.class.php"); |
| 67 |
require_once("$mmb_plugin_dir/api.php"); |
| 68 |
|
| 69 |
require_once("$mmb_plugin_dir/plugins/search/search.php"); |
| 70 |
require_once("$mmb_plugin_dir/plugins/cleanup/cleanup.php"); |
| 71 |
|
| 72 |
//this is an exmaple plugin for extra_html element |
| 73 |
//require_once("$mmb_plugin_dir/plugins/extra_html_example/extra_html_example.php"); |
| 74 |
|
| 75 |
$mmb_core = new MMB_Core(); |
| 76 |
if( microtime(true) - (double)get_option('mwp_iframe_options_header') < 3600 ){ |
| 77 |
remove_action( 'admin_init', 'send_frame_options_header'); |
| 78 |
} |
| 79 |
|
| 80 |
add_action('init', 'mmb_parse_request'); |
| 81 |
|
| 82 |
if (function_exists('register_activation_hook')) |
| 83 |
register_activation_hook(__FILE__, array( |
| 84 |
$mmb_core, |
| 85 |
'install' |
| 86 |
)); |
| 87 |
|
| 88 |
if (function_exists('register_deactivation_hook')) |
| 89 |
register_deactivation_hook(__FILE__, array( |
| 90 |
$mmb_core, |
| 91 |
'uninstall' |
| 92 |
)); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
function mmb_parse_request() |
| 97 |
{ |
| 98 |
|
| 99 |
if (!isset($HTTP_RAW_POST_DATA)) { |
| 100 |
$HTTP_RAW_POST_DATA = file_get_contents('php://input'); |
| 101 |
} |
| 102 |
ob_start(); |
| 103 |
|
| 104 |
global $mmb_core, $mmb_actions, $new_actions; |
| 105 |
|
| 106 |
$data = base64_decode($HTTP_RAW_POST_DATA); |
| 107 |
if ($data) |
| 108 |
$num = @extract(unserialize($data)); |
| 109 |
|
| 110 |
if ($action) { |
| 111 |
global $wp_object_cache; |
| 112 |
|
| 113 |
if(!empty($wp_object_cache)) |
| 114 |
@$wp_object_cache->flush(); |
| 115 |
|
| 116 |
update_option('mwp_iframe_options_header', microtime(true)); |
| 117 |
// mmb_response($mmb_actions, false); |
| 118 |
if (!$mmb_core->check_if_user_exists($params['username'])) |
| 119 |
mmb_response('Username <b>' . $params['username'] . '</b> does not have administrator capabilities. Enter the correct username in the site options.', false); |
| 120 |
|
| 121 |
if ($action == 'add_site') { |
| 122 |
mmb_add_site($params); |
| 123 |
mmb_response('You should never see this.', false); |
| 124 |
} |
| 125 |
|
| 126 |
$auth = $mmb_core->authenticate_message($action . $id, $signature, $id); |
| 127 |
if ($auth === true) { |
| 128 |
if (array_key_exists($action, $mmb_actions) && function_exists($mmb_actions[$action])) |
| 129 |
call_user_func($mmb_actions[$action], $params); |
| 130 |
else |
| 131 |
mmb_response('Action "' . $action . '" does not exist.', false); |
| 132 |
} else { |
| 133 |
mmb_response($auth['error'], false); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
ob_end_clean(); |
| 139 |
} |
| 140 |
|
| 141 |
/* Main response function */ |
| 142 |
|
| 143 |
function mmb_response($response = false, $success = true) |
| 144 |
{ |
| 145 |
$return = array(); |
| 146 |
|
| 147 |
if (empty($response)) |
| 148 |
$return['error'] = 'Empty response.'; |
| 149 |
else if ($success) |
| 150 |
$return['success'] = $response; |
| 151 |
else |
| 152 |
$return['error'] = $response; |
| 153 |
|
| 154 |
if( !headers_sent() ){ |
| 155 |
ob_start("ob_gzhandler"); |
| 156 |
header('Content-Type: text/plain'); |
| 157 |
} |
| 158 |
exit("<MWPHEADER>" . base64_encode(serialize($return))."<ENDMWPHEADER>"); |
| 159 |
} |
| 160 |
|
| 161 |
function mmb_add_site($params) |
| 162 |
{ |
| 163 |
global $mmb_core; |
| 164 |
|
| 165 |
$num = extract($params); |
| 166 |
|
| 167 |
if ($num) { |
| 168 |
if (!get_option('_action_message_id') && !get_option('_worker_public_key')) { |
| 169 |
$public_key = base64_decode($public_key); |
| 170 |
|
| 171 |
if (function_exists('openssl_verify')) { |
| 172 |
$verify = openssl_verify($action . $id, base64_decode($signature), $public_key); |
| 173 |
if ($verify == 1) { |
| 174 |
$mmb_core->set_master_public_key($public_key); |
| 175 |
$mmb_core->set_worker_message_id($id); |
| 176 |
$mmb_core->get_stats_instance(); |
| 177 |
mmb_response($mmb_core->stats_instance->get_initial_stats(), true); |
| 178 |
} else if ($verify == 0) { |
| 179 |
mmb_response('Invalid message signature. Please contact us if you see this message often.', false); |
| 180 |
} else { |
| 181 |
mmb_response('Command not successful. Please try again.', false); |
| 182 |
} |
| 183 |
} else { |
| 184 |
if (!get_option('_worker_nossl_key')) { |
| 185 |
srand(); |
| 186 |
$random_key = md5(base64_encode($public_key) . rand(0, getrandmax())); |
| 187 |
|
| 188 |
$mmb_core->set_random_signature($random_key); |
| 189 |
$mmb_core->set_worker_message_id($id); |
| 190 |
$mmb_core->set_master_public_key($public_key); |
| 191 |
$mmb_core->get_stats_instance(); |
| 192 |
mmb_response($mmb_core->stats_instance->get_initial_stats(), true); |
| 193 |
} else |
| 194 |
mmb_response('Please deactivate & activate ManageWP Worker plugin on your site, then re-add the site to your dashboard.', false); |
| 195 |
} |
| 196 |
} else { |
| 197 |
mmb_response('Please deactivate & activate ManageWP Worker plugin on your site and re-add the site to your dashboard.', false); |
| 198 |
} |
| 199 |
} else { |
| 200 |
mmb_response('Invalid parameters received. Please try again.', false); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
function mmb_remove_site($params) |
| 205 |
{ |
| 206 |
extract($params); |
| 207 |
global $mmb_core; |
| 208 |
$mmb_core->uninstall(); |
| 209 |
|
| 210 |
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 211 |
$plugin_slug = basename(dirname(__FILE__)) . '/' . basename(__FILE__); |
| 212 |
|
| 213 |
if ($deactivate) { |
| 214 |
deactivate_plugins($plugin_slug, true); |
| 215 |
} |
| 216 |
|
| 217 |
if (!is_plugin_active($plugin_slug)) |
| 218 |
mmb_response(array( |
| 219 |
'deactivated' => 'Site removed successfully. <br /><br />ManageWP Worker plugin successfully deactivated.' |
| 220 |
), true); |
| 221 |
else |
| 222 |
mmb_response(array( |
| 223 |
'removed_data' => 'Site removed successfully. <br /><br /><b>ManageWP Worker plugin was not deactivated.</b>' |
| 224 |
), true); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
function mmb_stats_get($params) |
| 230 |
{ |
| 231 |
global $mmb_core; |
| 232 |
$mmb_core->get_stats_instance(); |
| 233 |
mmb_response($mmb_core->stats_instance->get($params), true); |
| 234 |
} |
| 235 |
function mmb_get_stats_notification($params) |
| 236 |
{ |
| 237 |
global $mmb_core; |
| 238 |
$mmb_core->get_stats_instance(); |
| 239 |
$stat = $mmb_core->stats_instance->get_stats_notification($params); |
| 240 |
mmb_response($stat, true); |
| 241 |
} |
| 242 |
|
| 243 |
//post |
| 244 |
function mmb_post_create($params) |
| 245 |
{ |
| 246 |
global $mmb_core; |
| 247 |
$mmb_core->get_post_instance(); |
| 248 |
$return = $mmb_core->post_instance->create($params); |
| 249 |
if (is_int($return)) |
| 250 |
mmb_response($return, true); |
| 251 |
else |
| 252 |
mmb_response($return, false); |
| 253 |
} |
| 254 |
function mmb_change_post_status($params) |
| 255 |
{ |
| 256 |
global $mmb_core; |
| 257 |
$mmb_core->get_post_instance(); |
| 258 |
$return = $mmb_core->post_instance->change_status($params); |
| 259 |
//mmb_response($return, true); |
| 260 |
|
| 261 |
} |
| 262 |
//comments |
| 263 |
function mmb_change_comment_status($params) |
| 264 |
{ |
| 265 |
global $mmb_core; |
| 266 |
$mmb_core->get_comment_instance(); |
| 267 |
$return = $mmb_core->comment_instance->change_status($params); |
| 268 |
//mmb_response($return, true); |
| 269 |
if ($return){ |
| 270 |
$mmb_core->get_stats_instance(); |
| 271 |
mmb_response($mmb_core->stats_instance->get_comments_stats($params), true); |
| 272 |
}else |
| 273 |
mmb_response('Comment not updated', false); |
| 274 |
} |
| 275 |
function mmb_comment_stats_get($params) |
| 276 |
{ |
| 277 |
global $mmb_core; |
| 278 |
$mmb_core->get_stats_instance(); |
| 279 |
mmb_response($mmb_core->stats_instance->get_comments_stats($params), true); |
| 280 |
} |
| 281 |
|
| 282 |
//backup |
| 283 |
function mmb_backup_now($params) |
| 284 |
{ |
| 285 |
global $mmb_core; |
| 286 |
|
| 287 |
$mmb_core->get_backup_instance(); |
| 288 |
$return = $mmb_core->backup_instance->backup($params); |
| 289 |
|
| 290 |
if (is_array($return) && array_key_exists('error', $return)) |
| 291 |
mmb_response($return['error'], false); |
| 292 |
else { |
| 293 |
mmb_response($return, true); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
function mmb_email_backup($params) |
| 298 |
{ |
| 299 |
global $mmb_core; |
| 300 |
$mmb_core->get_backup_instance(); |
| 301 |
$return = $mmb_core->backup_instance->email_backup($params); |
| 302 |
|
| 303 |
if (is_array($return) && array_key_exists('error', $return)) |
| 304 |
mmb_response($return['error'], false); |
| 305 |
else { |
| 306 |
mmb_response($return, true); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
function mmb_check_backup_compat($params) |
| 311 |
{ |
| 312 |
global $mmb_core; |
| 313 |
$mmb_core->get_backup_instance(); |
| 314 |
$return = $mmb_core->backup_instance->check_backup_compat($params); |
| 315 |
|
| 316 |
if (is_array($return) && array_key_exists('error', $return)) |
| 317 |
mmb_response($return['error'], false); |
| 318 |
else { |
| 319 |
mmb_response($return, true); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
function mmb_optimize_tables($params) |
| 324 |
{ |
| 325 |
global $mmb_core; |
| 326 |
$mmb_core->get_backup_instance(); |
| 327 |
$return = $mmb_core->backup_instance->optimize_tables(); |
| 328 |
if ($return) |
| 329 |
mmb_response($return, true); |
| 330 |
else |
| 331 |
mmb_response(false, false); |
| 332 |
} |
| 333 |
|
| 334 |
function mmb_restore_now($params) |
| 335 |
{ |
| 336 |
global $mmb_core; |
| 337 |
$mmb_core->get_backup_instance(); |
| 338 |
$return = $mmb_core->backup_instance->restore($params); |
| 339 |
if (is_array($return) && array_key_exists('error', $return)) |
| 340 |
mmb_response($return['error'], false); |
| 341 |
else |
| 342 |
mmb_response($return, true); |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
function mmb_update_worker_plugin($params) |
| 347 |
{ |
| 348 |
global $mmb_core; |
| 349 |
mmb_response($mmb_core->update_worker_plugin($params), true); |
| 350 |
} |
| 351 |
|
| 352 |
function mmb_wp_checkversion($params) |
| 353 |
{ |
| 354 |
include_once(ABSPATH . 'wp-includes/version.php'); |
| 355 |
global $mmb_wp_version, $mmb_core; |
| 356 |
mmb_response($mmb_wp_version, true); |
| 357 |
} |
| 358 |
|
| 359 |
function mmb_search_posts_by_term($params) |
| 360 |
{ |
| 361 |
global $mmb_core; |
| 362 |
$mmb_core->get_search_instance(); |
| 363 |
//$mmb_core->_log($params); |
| 364 |
|
| 365 |
$search_type = trim($params['search_type']); |
| 366 |
$search_term = strtolower(trim($params['search_term'])); |
| 367 |
|
| 368 |
switch ($search_type){ |
| 369 |
case 'page_post': |
| 370 |
$return = $mmb_core->search_instance->search_posts_by_term($params); |
| 371 |
if($return){ |
| 372 |
$return = serialize($return); |
| 373 |
mmb_response($return, true); |
| 374 |
}else{ |
| 375 |
mmb_response('No posts found', false); |
| 376 |
} |
| 377 |
break; |
| 378 |
|
| 379 |
case 'plugin': |
| 380 |
$plugins = get_option('active_plugins'); |
| 381 |
|
| 382 |
$have_plugin = false; |
| 383 |
foreach ($plugins as $plugin) { |
| 384 |
if(strpos($plugin, $search_term)>-1){ |
| 385 |
$have_plugin = true; |
| 386 |
} |
| 387 |
} |
| 388 |
if($have_plugin){ |
| 389 |
mmb_response(serialize($plugin), true); |
| 390 |
}else{ |
| 391 |
mmb_response(false, false); |
| 392 |
} |
| 393 |
break; |
| 394 |
case 'theme': |
| 395 |
$theme = strtolower(get_option('template')); |
| 396 |
if(strpos($theme, $search_term)>-1){ |
| 397 |
mmb_response($theme, true); |
| 398 |
}else{ |
| 399 |
mmb_response(false, false); |
| 400 |
} |
| 401 |
break; |
| 402 |
default: mmb_response(false, false); |
| 403 |
} |
| 404 |
$return = $mmb_core->search_instance->search_posts_by_term($params); |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
if ($return_if_true) { |
| 409 |
mmb_response($return_value, true); |
| 410 |
} else { |
| 411 |
mmb_response($return_if_false, false); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
function mmb_install_addon($params) |
| 416 |
{ |
| 417 |
global $mmb_core; |
| 418 |
$mmb_core->get_installer_instance(); |
| 419 |
$return = $mmb_core->installer_instance->install_remote_file($params); |
| 420 |
mmb_response($return, true); |
| 421 |
|
| 422 |
} |
| 423 |
function mmb_do_upgrade($params) |
| 424 |
{ |
| 425 |
global $mmb_core, $mmb_upgrading; |
| 426 |
$mmb_core->get_installer_instance(); |
| 427 |
$return = $mmb_core->installer_instance->do_upgrade($params); |
| 428 |
mmb_response($return, true); |
| 429 |
|
| 430 |
} |
| 431 |
|
| 432 |
function mmb_add_link($params) |
| 433 |
{ |
| 434 |
global $mmb_core; |
| 435 |
$mmb_core->get_link_instance(); |
| 436 |
$return = $mmb_core->link_instance->add_link($params); |
| 437 |
if (is_array($return) && array_key_exists('error', $return)) |
| 438 |
|
| 439 |
mmb_response($return['error'], false); |
| 440 |
else { |
| 441 |
mmb_response($return, true); |
| 442 |
} |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
function mmb_iframe_plugins_fix($update_actions) |
| 447 |
{ |
| 448 |
foreach($update_actions as $key => $action) |
| 449 |
{ |
| 450 |
$update_actions[$key] = str_replace('target="_parent"','',$action); |
| 451 |
} |
| 452 |
|
| 453 |
return $update_actions; |
| 454 |
|
| 455 |
} |
| 456 |
function mmb_execute_php_code($params) |
| 457 |
{ |
| 458 |
ob_start(); |
| 459 |
eval($params['code']); |
| 460 |
$return = ob_get_flush(); |
| 461 |
mmb_response(print_r($return, true), true); |
| 462 |
} |
| 463 |
|
| 464 |
add_filter('install_plugin_complete_actions','mmb_iframe_plugins_fix'); |
| 465 |
|
| 466 |
|
| 467 |
?> |
| 468 |
|