| 1 |
<?php |
| 2 |
/************************************************************* |
| 3 |
* |
| 4 |
* helper.class.php |
| 5 |
* |
| 6 |
* Utility functions |
| 7 |
* |
| 8 |
* |
| 9 |
* Copyright (c) 2011 Prelovac Media |
| 10 |
* www.prelovac.com |
| 11 |
**************************************************************/ |
| 12 |
|
| 13 |
class MMB_Helper |
| 14 |
{ |
| 15 |
/** |
| 16 |
* A helper function to log data |
| 17 |
* |
| 18 |
* @param mixed $mixed |
| 19 |
*/ |
| 20 |
function _log($mixed) |
| 21 |
{ |
| 22 |
if (is_array($mixed)) { |
| 23 |
$mixed = print_r($mixed, 1); |
| 24 |
} else if (is_object($mixed)) { |
| 25 |
ob_start(); |
| 26 |
var_dump($mixed); |
| 27 |
$mixed = ob_get_clean(); |
| 28 |
} |
| 29 |
|
| 30 |
$handle = fopen(dirname(__FILE__) . '/log', 'a'); |
| 31 |
fwrite($handle, $mixed . PHP_EOL); |
| 32 |
fclose($handle); |
| 33 |
} |
| 34 |
|
| 35 |
function _escape(&$array) |
| 36 |
{ |
| 37 |
global $wpdb; |
| 38 |
|
| 39 |
if (!is_array($array)) { |
| 40 |
return ($wpdb->escape($array)); |
| 41 |
} else { |
| 42 |
foreach ((array) $array as $k => $v) { |
| 43 |
if (is_array($v)) { |
| 44 |
$this->_escape($array[$k]); |
| 45 |
} else if (is_object($v)) { |
| 46 |
//skip |
| 47 |
} else { |
| 48 |
$array[$k] = $wpdb->escape($v); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Initializes the file system |
| 56 |
* |
| 57 |
*/ |
| 58 |
function init_filesystem() |
| 59 |
{ |
| 60 |
global $wp_filesystem; |
| 61 |
|
| 62 |
if (!$wp_filesystem || !is_object($wp_filesystem)) { |
| 63 |
WP_Filesystem(); |
| 64 |
} |
| 65 |
|
| 66 |
if (!is_object($wp_filesystem)) |
| 67 |
return FALSE; |
| 68 |
|
| 69 |
return TRUE; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Gets transient based on WP version |
| 74 |
* |
| 75 |
* @global string $wp_version |
| 76 |
* @param string $option_name |
| 77 |
* @return mixed |
| 78 |
*/ |
| 79 |
|
| 80 |
function mmb_set_transient($option_name = false, $data = false){ |
| 81 |
|
| 82 |
if (!$option_name || !$data) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
if($this->mmb_multisite) |
| 86 |
return $this->mmb_set_sitemeta_transient($option_name, $data); |
| 87 |
|
| 88 |
global $wp_version; |
| 89 |
|
| 90 |
if (version_compare($wp_version, '2.7.9', '<=')) { |
| 91 |
update_option($option_name, $data); |
| 92 |
} else if (version_compare($wp_version, '2.9.9', '<=')) { |
| 93 |
update_option('_transient_' . $option_name, $data); |
| 94 |
} else { |
| 95 |
update_option('_site_transient_' . $option_name, $data); |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
function mmb_get_transient($option_name) |
| 100 |
{ |
| 101 |
if (trim($option_name) == '') { |
| 102 |
return FALSE; |
| 103 |
} |
| 104 |
if($this->mmb_multisite) |
| 105 |
return $this->mmb_get_sitemeta_transient($option_name); |
| 106 |
|
| 107 |
global $wp_version; |
| 108 |
|
| 109 |
|
| 110 |
if (version_compare($wp_version, '2.7.9', '<=')) { |
| 111 |
return get_option($option_name); |
| 112 |
} else if (version_compare($wp_version, '2.9.9', '<=')) { |
| 113 |
return get_option('_transient_' . $option_name); |
| 114 |
} else { |
| 115 |
return get_option('_site_transient_' . $option_name); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
function mmb_delete_transient($option_name) |
| 120 |
{ |
| 121 |
if (trim($option_name) == '') { |
| 122 |
return FALSE; |
| 123 |
} |
| 124 |
|
| 125 |
global $wp_version; |
| 126 |
|
| 127 |
if (version_compare($wp_version, '2.7.9', '<=')) { |
| 128 |
delete_option($option_name); |
| 129 |
} else if (version_compare($wp_version, '2.9.9', '<=')) { |
| 130 |
delete_option('_transient_' . $option_name); |
| 131 |
} else { |
| 132 |
delete_option('_site_transient_' . $option_name); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
function mmb_get_sitemeta_transient($option_name){ |
| 137 |
global $wpdb; |
| 138 |
$option_name = '_site_transient_'. $option_name; |
| 139 |
|
| 140 |
$result = $wpdb->get_var( $wpdb->prepare("SELECT `meta_value` FROM `{$wpdb->sitemeta}` WHERE meta_key = '{$option_name}' AND `site_id` = '{$this->mmb_multisite}' ")); |
| 141 |
$result = maybe_unserialize($result); |
| 142 |
return $result; |
| 143 |
} |
| 144 |
|
| 145 |
function mmb_set_sitemeta_transient($option_name, $option_value){ |
| 146 |
global $wpdb; |
| 147 |
$option_name = '_site_transient_'. $option_name; |
| 148 |
|
| 149 |
if($this->mmb_get_sitemeta_transient($option_name)){ |
| 150 |
$result = $wpdb->update( $wpdb->sitemeta, |
| 151 |
array( |
| 152 |
'meta_value' => maybe_serialize($option_value) |
| 153 |
), |
| 154 |
array( |
| 155 |
'meta_key' => $option_name, |
| 156 |
'site_id' => $this->mmb_multisite |
| 157 |
) |
| 158 |
); |
| 159 |
}else { |
| 160 |
$result = $wpdb->insert( $wpdb->sitemeta, |
| 161 |
array( |
| 162 |
'meta_key' => $option_name, |
| 163 |
'meta_value' => maybe_serialize($option_value), |
| 164 |
'site_id' => $this->mmb_multisite |
| 165 |
) |
| 166 |
); |
| 167 |
} |
| 168 |
return $result; |
| 169 |
} |
| 170 |
|
| 171 |
function delete_temp_dir($directory) |
| 172 |
{ |
| 173 |
if (substr($directory, -1) == "/") { |
| 174 |
$directory = substr($directory, 0, -1); |
| 175 |
} |
| 176 |
if (!file_exists($directory) || !is_dir($directory)) { |
| 177 |
return false; |
| 178 |
} elseif (!is_readable($directory)) { |
| 179 |
return false; |
| 180 |
} else { |
| 181 |
$directoryHandle = opendir($directory); |
| 182 |
|
| 183 |
while ($contents = readdir($directoryHandle)) { |
| 184 |
if ($contents != '.' && $contents != '..') { |
| 185 |
$path = $directory . "/" . $contents; |
| 186 |
|
| 187 |
if (is_dir($path)) { |
| 188 |
$this->delete_temp_dir($path); |
| 189 |
} else { |
| 190 |
unlink($path); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
closedir($directoryHandle); |
| 195 |
rmdir($directory); |
| 196 |
return true; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
function set_worker_message_id($message_id = false) |
| 201 |
{ |
| 202 |
if ($message_id) { |
| 203 |
add_option('_action_message_id', $message_id) or update_option('_action_message_id', $message_id); |
| 204 |
return $message_id; |
| 205 |
} |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
function get_worker_message_id() |
| 210 |
{ |
| 211 |
return (int) get_option('_action_message_id'); |
| 212 |
} |
| 213 |
|
| 214 |
function set_master_public_key($public_key = false) |
| 215 |
{ |
| 216 |
if ($public_key && !get_option('_worker_public_key')) { |
| 217 |
add_option('_worker_public_key', base64_encode($public_key)); |
| 218 |
return true; |
| 219 |
} |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
function get_master_public_key() |
| 224 |
{ |
| 225 |
if (!get_option('_worker_public_key')) |
| 226 |
return false; |
| 227 |
return base64_decode(get_option('_worker_public_key')); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
function get_random_signature() |
| 232 |
{ |
| 233 |
if (!get_option('_worker_nossl_key')) |
| 234 |
return false; |
| 235 |
return base64_decode(get_option('_worker_nossl_key')); |
| 236 |
} |
| 237 |
|
| 238 |
function set_random_signature($random_key = false) |
| 239 |
{ |
| 240 |
if ($random_key && !get_option('_worker_nossl_key')) { |
| 241 |
add_option('_worker_nossl_key', base64_encode($random_key)); |
| 242 |
return true; |
| 243 |
} |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
function authenticate_message($data = false, $signature = false, $message_id = false) |
| 249 |
{ |
| 250 |
if (!$data && !$signature) { |
| 251 |
return array( |
| 252 |
'error' => 'Authentication failed.' |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
$current_message = $this->get_worker_message_id(); |
| 257 |
|
| 258 |
if ((int) $current_message > (int) $message_id) |
| 259 |
return array( |
| 260 |
'error' => 'Invalid message recieved. You can try to reinstall worker plugin and re-add the site to your account.' |
| 261 |
); |
| 262 |
|
| 263 |
$pl_key = $this->get_master_public_key(); |
| 264 |
if (!$pl_key) { |
| 265 |
return array( |
| 266 |
'error' => 'Authentication failed (public key). You can try to reinstall worker plugin and re-add the site to your account.' |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
if (function_exists('openssl_verify') && !$this->get_random_signature()) { |
| 271 |
$verify = openssl_verify($data, $signature, $pl_key); |
| 272 |
if ($verify == 1) { |
| 273 |
$message_id = $this->set_worker_message_id($message_id); |
| 274 |
return true; |
| 275 |
} else if ($verify == 0) { |
| 276 |
return array( |
| 277 |
'error' => 'Invalid message signature. You can try to reinstall worker plugin and re-add the site to your account.' |
| 278 |
); |
| 279 |
} else { |
| 280 |
return array( |
| 281 |
'error' => 'Command not successful! Please try again.' |
| 282 |
); |
| 283 |
} |
| 284 |
} else if ($this->get_random_signature()) { |
| 285 |
if (md5($data . $this->get_random_signature()) == $signature) { |
| 286 |
$message_id = $this->set_worker_message_id($message_id); |
| 287 |
return true; |
| 288 |
} |
| 289 |
return array( |
| 290 |
'error' => 'Invalid message signature. You can try to reinstall the worker plugin and then re-add the site to your dashboard.' |
| 291 |
); |
| 292 |
} |
| 293 |
// no rand key - deleted in get_stat maybe |
| 294 |
else |
| 295 |
return array( |
| 296 |
'error' => 'Invalid message signature, try reinstalling worker plugin and re-adding the site to your dashboard.' |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
function _secure_data($data = false){ |
| 301 |
if($data == false) |
| 302 |
return false; |
| 303 |
|
| 304 |
$pl_key = $this->get_master_public_key(); |
| 305 |
if (!$pl_key) |
| 306 |
return false; |
| 307 |
|
| 308 |
$secure = ''; |
| 309 |
if( function_exists('openssl_public_decrypt') && !$this->get_random_signature()){ |
| 310 |
if(is_array($data) && !empty($data)){ |
| 311 |
foreach($data as $input){ |
| 312 |
openssl_public_decrypt($input, &$decrypted, $pl_key); |
| 313 |
$secure .= $decrypted; |
| 314 |
} |
| 315 |
} else { |
| 316 |
openssl_public_decrypt($input, &$decrypted, $pl_key); |
| 317 |
$secure = $decrypted; |
| 318 |
} |
| 319 |
return $secure; |
| 320 |
} |
| 321 |
return false; |
| 322 |
|
| 323 |
} |
| 324 |
|
| 325 |
function check_if_user_exists($username = false) |
| 326 |
{ |
| 327 |
global $wpdb; |
| 328 |
if ($username) { |
| 329 |
require_once(ABSPATH . WPINC . '/registration.php'); |
| 330 |
include_once(ABSPATH . 'wp-includes/pluggable.php'); |
| 331 |
|
| 332 |
if (username_exists($username) == null) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
$user = (array) get_userdatabylogin($username); |
| 336 |
|
| 337 |
if ($user[$wpdb->prefix . 'user_level'] == 10 || isset($user[$wpdb->prefix . 'capabilities']['administrator']) || |
| 338 |
(isset($user['caps']['administrator']) && $user['caps']['administrator'] == 1)){ |
| 339 |
define('MMB_USER_CAPABILITIES', $user->wp_user_level); |
| 340 |
return true; |
| 341 |
} |
| 342 |
return false; |
| 343 |
} |
| 344 |
return false; |
| 345 |
} |
| 346 |
|
| 347 |
function refresh_updates() |
| 348 |
{ |
| 349 |
if (rand(1, 3) == '2') { |
| 350 |
require_once(ABSPATH . WPINC . '/update.php'); |
| 351 |
wp_update_plugins(); |
| 352 |
wp_update_themes(); |
| 353 |
wp_version_check(); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
function remove_http($url = '') |
| 358 |
{ |
| 359 |
if ($url == 'http://' OR $url == 'https://') { |
| 360 |
return $url; |
| 361 |
} |
| 362 |
return preg_replace('/^(http|https)\:\/\/(www.)?/i', '', $url); |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
function mmb_get_error($error_object) |
| 367 |
{ |
| 368 |
if (!is_wp_error($error_object)) { |
| 369 |
return $error_object != '' ? $error_object : ''; |
| 370 |
} else { |
| 371 |
$errors = array(); |
| 372 |
foreach ($error_object->error_data as $error_key => $error_string) { |
| 373 |
$errors[] = str_replace('_', ' ', ucfirst($error_key)) . ': ' . $error_string; |
| 374 |
} |
| 375 |
return implode('<br />', $errors); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
function is_server_writable(){ |
| 380 |
if((!defined('FTP_HOST') || !defined('FTP_USER') || !defined('FTP_PASS')) && (get_filesystem_method(array(), false) != 'direct')) |
| 381 |
return false; |
| 382 |
else |
| 383 |
return true; |
| 384 |
} |
| 385 |
|
| 386 |
function mmb_download_url($url, $file_name) |
| 387 |
{ |
| 388 |
if (function_exists('fopen') && function_exists('ini_get') && ini_get('allow_url_fopen') == true && ($destination = @fopen($file_name, 'wb')) && ($source = @fopen($url, "r")) ) { |
| 389 |
|
| 390 |
|
| 391 |
while ($a = @fread($source, 1024* 1024)) { |
| 392 |
@fwrite($destination, $a); |
| 393 |
} |
| 394 |
|
| 395 |
fclose($source); |
| 396 |
fclose($destination); |
| 397 |
} else |
| 398 |
if (!fsockopen_download($url, $file_name)) |
| 399 |
die('Error downloading file ' . $url); |
| 400 |
return $file_name; |
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
?> |