| 1 |
<?php |
| 2 |
/************************************************************ |
| 3 |
* This plugin was modified by Revmakx * |
| 4 |
* Copyright (c) 2012 Revmakx * |
| 5 |
* www.revmakx.com * |
| 6 |
* * |
| 7 |
************************************************************/ |
| 8 |
/************************************************************* |
| 9 |
* |
| 10 |
* helper.class.php |
| 11 |
* |
| 12 |
* Utility functions |
| 13 |
* |
| 14 |
* |
| 15 |
* Copyright (c) 2011 Prelovac Media |
| 16 |
* www.prelovac.com |
| 17 |
**************************************************************/ |
| 18 |
if ( ! defined('ABSPATH') ) |
| 19 |
die(); |
| 20 |
|
| 21 |
if(!defined('MMB_WORKER_VERSION')) |
| 22 |
define('MMB_WORKER_VERSION', '0'); |
| 23 |
|
| 24 |
class IWP_MMB_Helper |
| 25 |
{ |
| 26 |
/** |
| 27 |
* A helper function to log data |
| 28 |
* |
| 29 |
* @param mixed $mixed |
| 30 |
*/ |
| 31 |
function _log($mixed) |
| 32 |
{ |
| 33 |
if (is_array($mixed)) { |
| 34 |
$mixed = print_r($mixed, 1); |
| 35 |
} else if (is_object($mixed)) { |
| 36 |
ob_start(); |
| 37 |
var_dump($mixed); |
| 38 |
$mixed = ob_get_clean(); |
| 39 |
} |
| 40 |
|
| 41 |
//$handle = fopen(dirname(__FILE__) . '/log', 'a'); |
| 42 |
//fwrite($handle, $mixed . PHP_EOL); |
| 43 |
//fclose($handle); |
| 44 |
} |
| 45 |
|
| 46 |
function _escape(&$array) |
| 47 |
{ |
| 48 |
global $wpdb; |
| 49 |
|
| 50 |
if (!is_array($array)) { |
| 51 |
return ($wpdb->escape($array)); |
| 52 |
} else { |
| 53 |
foreach ((array) $array as $k => $v) { |
| 54 |
if (is_array($v)) { |
| 55 |
$this->_escape($array[$k]); |
| 56 |
} else if (is_object($v)) { |
| 57 |
//skip |
| 58 |
} else { |
| 59 |
$array[$k] = $wpdb->escape($v); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Initializes the file system |
| 67 |
* |
| 68 |
*/ |
| 69 |
function init_filesystem() |
| 70 |
{ |
| 71 |
global $wp_filesystem; |
| 72 |
|
| 73 |
if (!$wp_filesystem || !is_object($wp_filesystem)) { |
| 74 |
WP_Filesystem(); |
| 75 |
} |
| 76 |
|
| 77 |
if (!is_object($wp_filesystem)) |
| 78 |
return FALSE; |
| 79 |
|
| 80 |
return TRUE; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* |
| 85 |
* Check if function exists or not on `suhosin` black list |
| 86 |
* |
| 87 |
*/ |
| 88 |
|
| 89 |
function iwp_mmb_get_user_info( $user_info = false, $info = 'login' ){ |
| 90 |
|
| 91 |
if($user_info === false) |
| 92 |
return false; |
| 93 |
|
| 94 |
if( strlen( trim( $user_info ) ) == 0) |
| 95 |
return false; |
| 96 |
|
| 97 |
|
| 98 |
global $wp_version; |
| 99 |
if (version_compare($wp_version, '3.2.2', '<=')){ |
| 100 |
return get_userdatabylogin( $user_info ); |
| 101 |
} else { |
| 102 |
return iwp_mmb_get_user_by( $info, $user_info ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* |
| 108 |
* Call action item filters |
| 109 |
* |
| 110 |
*/ |
| 111 |
|
| 112 |
function iwp_mmb_parse_action_params( $key = '', $params = null, $call_object = null ){ |
| 113 |
|
| 114 |
global $_iwp_mmb_item_filter; |
| 115 |
$call_object = $call_object !== null ? $call_object : $this; |
| 116 |
$return = array(); |
| 117 |
|
| 118 |
if(isset($_iwp_mmb_item_filter[$key]) && !empty($_iwp_mmb_item_filter[$key])){ |
| 119 |
if( isset($params['item_filter']) && !empty($params['item_filter'])){ |
| 120 |
foreach($params['item_filter'] as $_items){ |
| 121 |
if(!empty($_items)){ |
| 122 |
foreach($_items as $_item){ |
| 123 |
if(in_array($_item[0], $_iwp_mmb_item_filter[$key])){ |
| 124 |
$_item[1] = isset($_item[1]) ? $_item[1] : array(); |
| 125 |
$return = call_user_func(array( &$call_object, 'get_'.$_item[0]), $return, $_item[1]); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return $return; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* |
| 138 |
* Check if function exists or not on `suhosin` black list |
| 139 |
* |
| 140 |
*/ |
| 141 |
|
| 142 |
function iwp_mmb_function_exists($function_callback){ |
| 143 |
|
| 144 |
if(!function_exists($function_callback)) |
| 145 |
return false; |
| 146 |
|
| 147 |
$disabled = explode(', ', @ini_get('disable_functions')); |
| 148 |
if (in_array($function_callback, $disabled)) |
| 149 |
return false; |
| 150 |
|
| 151 |
if (extension_loaded('suhosin')) { |
| 152 |
$suhosin = @ini_get("suhosin.executor.func.blacklist"); |
| 153 |
if (empty($suhosin) == false) { |
| 154 |
$suhosin = explode(',', $suhosin); |
| 155 |
$blacklist = array_map('trim', $suhosin); |
| 156 |
$blacklist = array_map('strtolower', $blacklist); |
| 157 |
if(in_array($function_callback, $blacklist)) |
| 158 |
return false; |
| 159 |
} |
| 160 |
} |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Gets transient based on WP version |
| 166 |
* |
| 167 |
* @global string $wp_version |
| 168 |
* @param string $option_name |
| 169 |
* @return mixed |
| 170 |
*/ |
| 171 |
|
| 172 |
function iwp_mmb_set_transient($option_name = false, $data = false){ |
| 173 |
|
| 174 |
if (!$option_name || !$data) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
if($this->iwp_mmb_multisite) |
| 178 |
return $this->iwp_mmb_set_sitemeta_transient($option_name, $data); |
| 179 |
|
| 180 |
global $wp_version; |
| 181 |
|
| 182 |
if (version_compare($wp_version, '2.7.9', '<=')) { |
| 183 |
update_option($option_name, $data); |
| 184 |
} else if (version_compare($wp_version, '2.9.9', '<=')) { |
| 185 |
update_option('_transient_' . $option_name, $data); |
| 186 |
} else { |
| 187 |
update_option('_site_transient_' . $option_name, $data); |
| 188 |
} |
| 189 |
|
| 190 |
} |
| 191 |
function iwp_mmb_get_transient($option_name) |
| 192 |
{ |
| 193 |
global $wp_version; |
| 194 |
|
| 195 |
if (trim($option_name) == '') { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
if (version_compare($wp_version, '3.4', '>')) { |
| 200 |
return get_site_transient($option_name); |
| 201 |
} |
| 202 |
|
| 203 |
if (!empty($this->iwp_mmb_multisite)) { |
| 204 |
return $this->iwp_mmb_get_sitemeta_transient($option_name); |
| 205 |
} |
| 206 |
|
| 207 |
$transient = get_option('_site_transient_'.$option_name); |
| 208 |
|
| 209 |
return apply_filters("site_transient_".$option_name, $transient); |
| 210 |
} |
| 211 |
|
| 212 |
function iwp_mmb_delete_transient($option_name) |
| 213 |
{ |
| 214 |
if (trim($option_name) == '') { |
| 215 |
return FALSE; |
| 216 |
} |
| 217 |
|
| 218 |
global $wp_version; |
| 219 |
|
| 220 |
if (version_compare($wp_version, '2.7.9', '<=')) { |
| 221 |
delete_option($option_name); |
| 222 |
} else if (version_compare($wp_version, '2.9.9', '<=')) { |
| 223 |
delete_option('_transient_' . $option_name); |
| 224 |
} else { |
| 225 |
delete_option('_site_transient_' . $option_name); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
function iwp_mmb_get_sitemeta_transient($option_name){ |
| 230 |
global $wpdb; |
| 231 |
$option_name = '_site_transient_'. $option_name; |
| 232 |
|
| 233 |
$result = $wpdb->get_var( $wpdb->prepare("SELECT `meta_value` FROM `{$wpdb->sitemeta}` WHERE meta_key = %s AND `site_id` = %s", $option_name, $this->iwp_mmb_multisite)); |
| 234 |
$result = maybe_unserialize($result); |
| 235 |
return $result; |
| 236 |
} |
| 237 |
|
| 238 |
function iwp_mmb_set_sitemeta_transient($option_name, $option_value){ |
| 239 |
global $wpdb; |
| 240 |
$option_name = '_site_transient_'. $option_name; |
| 241 |
|
| 242 |
if($this->iwp_mmb_get_sitemeta_transient($option_name)){ |
| 243 |
$result = $wpdb->update( $wpdb->sitemeta, |
| 244 |
array( |
| 245 |
'meta_value' => maybe_serialize($option_value) |
| 246 |
), |
| 247 |
array( |
| 248 |
'meta_key' => $option_name, |
| 249 |
'site_id' => $this->iwp_mmb_multisite |
| 250 |
) |
| 251 |
); |
| 252 |
}else { |
| 253 |
$result = $wpdb->insert( $wpdb->sitemeta, |
| 254 |
array( |
| 255 |
'meta_key' => $option_name, |
| 256 |
'meta_value' => maybe_serialize($option_value), |
| 257 |
'site_id' => $this->iwp_mmb_multisite |
| 258 |
) |
| 259 |
); |
| 260 |
} |
| 261 |
return $result; |
| 262 |
} |
| 263 |
|
| 264 |
function delete_temp_dir($directory) |
| 265 |
{ |
| 266 |
if (substr($directory, -1) == "/") { |
| 267 |
$directory = substr($directory, 0, -1); |
| 268 |
} |
| 269 |
if (!file_exists($directory) || !is_dir($directory)) { |
| 270 |
return false; |
| 271 |
} elseif (!is_readable($directory)) { |
| 272 |
return false; |
| 273 |
} else { |
| 274 |
$directoryHandle = opendir($directory); |
| 275 |
|
| 276 |
while ($contents = readdir($directoryHandle)) { |
| 277 |
if ($contents != '.' && $contents != '..') { |
| 278 |
$path = $directory . "/" . $contents; |
| 279 |
|
| 280 |
if (is_dir($path)) { |
| 281 |
$this->delete_temp_dir($path); |
| 282 |
} else { |
| 283 |
unlink($path); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
closedir($directoryHandle); |
| 288 |
rmdir($directory); |
| 289 |
return true; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
function set_client_message_id($message_id = false) |
| 294 |
{ |
| 295 |
if ($message_id) { |
| 296 |
if (is_multisite()) { |
| 297 |
global $wpdb; |
| 298 |
$blogIDs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 299 |
foreach ($blogIDs as $blogID) { |
| 300 |
update_blog_option($blogID, 'iwp_client_action_message_id', $message_id); |
| 301 |
} |
| 302 |
return true; |
| 303 |
} else { |
| 304 |
update_option('iwp_client_action_message_id', $message_id); |
| 305 |
return $message_id; |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
function get_client_message_id() |
| 313 |
{ |
| 314 |
return (int) get_option('iwp_client_action_message_id'); |
| 315 |
} |
| 316 |
|
| 317 |
function set_admin_panel_public_key($public_key = false) |
| 318 |
{ |
| 319 |
|
| 320 |
if (is_multisite()) { |
| 321 |
global $wpdb; |
| 322 |
$blogIDs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 323 |
foreach ($blogIDs as $blogID) { |
| 324 |
update_blog_option($blogID, 'iwp_client_public_key', base64_encode($public_key)); |
| 325 |
} |
| 326 |
return true; |
| 327 |
} else { |
| 328 |
if ($public_key && !get_option('iwp_client_public_key')) { |
| 329 |
add_option('iwp_client_public_key', base64_encode($public_key)); |
| 330 |
return true; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
function get_admin_panel_public_key() |
| 338 |
{ |
| 339 |
if (!get_option('iwp_client_public_key')) |
| 340 |
return false; |
| 341 |
return base64_decode(get_option('iwp_client_public_key')); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
function get_random_signature() |
| 346 |
{ |
| 347 |
if (!get_option('iwp_client_nossl_key')) |
| 348 |
return false; |
| 349 |
return base64_decode(get_option('iwp_client_nossl_key')); |
| 350 |
} |
| 351 |
|
| 352 |
function set_random_signature($random_key = false) |
| 353 |
{ |
| 354 |
if ($random_key && !get_option('iwp_client_nossl_key')) { |
| 355 |
add_option('iwp_client_nossl_key', base64_encode($random_key)); |
| 356 |
return true; |
| 357 |
} |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
function authenticate_message($data = false, $signature = false, $signature_new = '', $message_id = false) |
| 363 |
{ |
| 364 |
if (!$data && !$signature) { |
| 365 |
return array( |
| 366 |
'error' => 'Authentication failed.', 'error_code' => 'authentication_failed' |
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
$current_message = $this->get_client_message_id(); |
| 371 |
|
| 372 |
if(isset($_GET['auto_login'])){//temp fix for stopping reuse of open admin url |
| 373 |
if ((int) $current_message >= (int) $message_id) |
| 374 |
return array( |
| 375 |
'error' => 'Invalid message recieved.', 'error_code' => 'invalid_message_received' |
| 376 |
); |
| 377 |
} |
| 378 |
|
| 379 |
$pl_key = $this->get_admin_panel_public_key(); |
| 380 |
if (!$pl_key) { |
| 381 |
return array( |
| 382 |
'error' => 'Authentication failed. Deactivate and activate the InfiniteWP Client plugin on this site, then remove the website from your InfiniteWP Admin Panel and add it again.', 'error_code' => 'authentication_failed_reactive_and_readd_the_site' |
| 383 |
); |
| 384 |
} |
| 385 |
|
| 386 |
if (checkOpenSSL() && !$this->get_random_signature()) { |
| 387 |
$verify = 0; |
| 388 |
if(!empty($signature_new) && defined('OPENSSL_ALGO_SHA256')){ |
| 389 |
$verify = openssl_verify($data, $signature_new, $pl_key, OPENSSL_ALGO_SHA256); |
| 390 |
}elseif (!empty($signature)) { |
| 391 |
$verify = openssl_verify($data, $signature, $pl_key); |
| 392 |
} |
| 393 |
if ($verify == 1) { |
| 394 |
$message_id = $this->set_client_message_id($message_id); |
| 395 |
return true; |
| 396 |
} else if ($verify == 0) { |
| 397 |
return array( |
| 398 |
'error' => 'Invalid message signature. Deactivate and activate the InfiniteWP Client plugin on this site, then remove the website from your InfiniteWP Admin Panel and add it again.', 'error_code' => 'invalid_message_signature_openssl' |
| 399 |
); |
| 400 |
} else { |
| 401 |
return array( |
| 402 |
'error' => 'Command not successful! Please try again.', 'error_code' => 'command_not_successful' |
| 403 |
); |
| 404 |
} |
| 405 |
} else if ($this->get_random_signature()) { |
| 406 |
|
| 407 |
if (md5($data . $this->get_random_signature()) === $signature) { |
| 408 |
$message_id = $this->set_client_message_id($message_id); |
| 409 |
return true; |
| 410 |
} |
| 411 |
return array( |
| 412 |
'error' => 'Invalid message signature. Deactivate and activate the InfiniteWP Client plugin on this site, then remove the website from your InfiniteWP Admin Panel and add it again.', 'error_code' => 'invalid_message_signature_random_signature' |
| 413 |
); |
| 414 |
} |
| 415 |
// no rand key - deleted in get_stat maybe |
| 416 |
else |
| 417 |
return array( |
| 418 |
'error' => 'Invalid message signature. Deactivate and activate the InfiniteWP Client plugin on this site, then remove the website from your InfiniteWP Admin Panel and add it again.', 'error_code' => 'invalid_message_signature' |
| 419 |
); |
| 420 |
} |
| 421 |
|
| 422 |
function _secure_data($data = false){ |
| 423 |
if($data == false) |
| 424 |
return false; |
| 425 |
|
| 426 |
$pl_key = $this->get_admin_panel_public_key(); |
| 427 |
if (!$pl_key) |
| 428 |
return false; |
| 429 |
|
| 430 |
$secure = ''; |
| 431 |
if( function_exists('openssl_public_decrypt') && !$this->get_random_signature()){ |
| 432 |
if(is_array($data) && !empty($data)){ |
| 433 |
foreach($data as $input){ |
| 434 |
openssl_public_decrypt($input, $decrypted, $pl_key); |
| 435 |
$secure .= $decrypted; |
| 436 |
} |
| 437 |
} else { |
| 438 |
openssl_public_decrypt($data, $decrypted, $pl_key); |
| 439 |
$secure = $decrypted; |
| 440 |
} |
| 441 |
return $secure; |
| 442 |
} |
| 443 |
return false; |
| 444 |
|
| 445 |
} |
| 446 |
|
| 447 |
function check_if_user_exists($username = false) |
| 448 |
{ |
| 449 |
global $wpdb; |
| 450 |
if ($username) { |
| 451 |
if( !function_exists('username_exists') ) |
| 452 |
include_once(ABSPATH . WPINC . '/registration.php'); |
| 453 |
// if( !function_exists('get_user_by') ) |
| 454 |
// include_once(ABSPATH . 'wp-includes/pluggable.php'); |
| 455 |
|
| 456 |
// if (username_exists($username) == null) { |
| 457 |
// return false; |
| 458 |
// } |
| 459 |
|
| 460 |
$user = (array) $this->iwp_mmb_get_user_info( $username ); |
| 461 |
if ((isset($user[$wpdb->base_prefix . 'user_level']) && $user[$wpdb->base_prefix . 'user_level'] == 10) || isset($user[$wpdb->base_prefix . 'capabilities']['administrator']) || |
| 462 |
(isset($user['caps']['administrator']) && $user['caps']['administrator'] == 1)){ |
| 463 |
return true; |
| 464 |
} |
| 465 |
return false; |
| 466 |
} |
| 467 |
return false; |
| 468 |
} |
| 469 |
|
| 470 |
function refresh_updates() |
| 471 |
{ |
| 472 |
if (rand(1, 3) == '2') { |
| 473 |
require_once(ABSPATH . WPINC . '/update.php'); |
| 474 |
wp_update_plugins(); |
| 475 |
wp_update_themes(); |
| 476 |
wp_version_check(); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
function remove_http($url = '') |
| 481 |
{ |
| 482 |
if ($url == 'http://' OR $url == 'https://') { |
| 483 |
return $url; |
| 484 |
} |
| 485 |
return preg_replace('/^(http|https)\:\/\/(www.)?/i', '', $url); |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
function iwp_mmb_get_error($error_object) |
| 490 |
{ |
| 491 |
if (!is_wp_error($error_object)) { |
| 492 |
return $error_object != '' ? $error_object : ''; |
| 493 |
} else { |
| 494 |
$errors = array(); |
| 495 |
if(!empty($error_object->error_data)) { |
| 496 |
foreach ($error_object->error_data as $error_key => $error_string) { |
| 497 |
$errors[] = str_replace('_', ' ', ucfirst($error_key)) . ': ' . $error_string; |
| 498 |
} |
| 499 |
} elseif (!empty($error_object->errors)){ |
| 500 |
foreach ($error_object->errors as $error_key => $err) { |
| 501 |
$errors[] = 'Error: '.str_replace('_', ' ', strtolower($error_key)); |
| 502 |
} |
| 503 |
} |
| 504 |
return implode('<br />', $errors); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
function is_server_writable(){ |
| 509 |
if((!defined('FTP_HOST') || !defined('FTP_USER') || !defined('FTP_PASS')) && (get_filesystem_method(array(), false) != 'direct')) |
| 510 |
return false; |
| 511 |
else |
| 512 |
return true; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
function define_ftp_constants($params){ |
| 517 |
|
| 518 |
if (!$this->is_server_writable()) { |
| 519 |
$ftp_details = unserialize($params['account_info']); |
| 520 |
if (empty($ftp_details)) { |
| 521 |
return true; |
| 522 |
} |
| 523 |
if (!defined('FS_METHOD')) { |
| 524 |
define( 'FS_METHOD', 'ftpext' ); |
| 525 |
} |
| 526 |
if (!defined('FTP_BASE')) { |
| 527 |
define( 'FTP_BASE', $ftp_details['remoteFolder'] ); |
| 528 |
} |
| 529 |
if (!defined('FTP_USER')) { |
| 530 |
define( 'FTP_USER', $ftp_details['hostUserName'] ); |
| 531 |
} |
| 532 |
if (!defined('FTP_PASS')) { |
| 533 |
define( 'FTP_PASS', $ftp_details['hostPassword'] ); |
| 534 |
} |
| 535 |
if (!defined('FTP_HOST')) { |
| 536 |
define( 'FTP_HOST', $ftp_details['hostName'] ); |
| 537 |
} |
| 538 |
if (!defined('FTP_SSL')) { |
| 539 |
define( 'FTP_SSL', $ftp_details['hostSSL'] ); |
| 540 |
} |
| 541 |
} |
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
function iwp_mmb_download_url($url, $file_name) |
| 546 |
{ |
| 547 |
if (function_exists('fopen') && function_exists('ini_get') && ini_get('allow_url_fopen') == true && ($destination = @fopen($file_name, 'wb')) && ($source = @fopen($url, "r")) ) { |
| 548 |
|
| 549 |
|
| 550 |
while ($a = @fread($source, 1024* 1024)) { |
| 551 |
@fwrite($destination, $a); |
| 552 |
} |
| 553 |
|
| 554 |
fclose($source); |
| 555 |
fclose($destination); |
| 556 |
} else |
| 557 |
if (!fsockopen_download($url, $file_name)) |
| 558 |
die('Error downloading file ' . $url); |
| 559 |
return $file_name; |
| 560 |
} |
| 561 |
} |
| 562 |
?> |