| 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 |
function mmb_get_transient($option_name) |
| 80 |
{ |
| 81 |
if (trim($option_name) == '') { |
| 82 |
return FALSE; |
| 83 |
} |
| 84 |
|
| 85 |
global $wp_version, $_wp_using_ext_object_cache; |
| 86 |
|
| 87 |
if (version_compare($wp_version, '2.8.0', '<')) { |
| 88 |
return get_option($option_name); |
| 89 |
} else if (version_compare($wp_version, '3.0.0', '<')) { |
| 90 |
if (get_transient($option_name)) |
| 91 |
return get_transient($option_name); |
| 92 |
else |
| 93 |
return get_option('_transient_' . $option_name); |
| 94 |
} else { |
| 95 |
if (get_site_transient($option_name)) |
| 96 |
return get_site_transient($option_name); |
| 97 |
else |
| 98 |
return get_option('_site_transient_' . $option_name); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
function mmb_delete_transient($option_name) |
| 103 |
{ |
| 104 |
if (trim($option_name) == '') { |
| 105 |
return FALSE; |
| 106 |
} |
| 107 |
|
| 108 |
global $wp_version; |
| 109 |
|
| 110 |
if (version_compare($wp_version, '2.8.0', '<')) { |
| 111 |
delete_option($option_name); |
| 112 |
} else if (version_compare($wp_version, '3.0.0', '<')) { |
| 113 |
if (delete_transient($option_name)) |
| 114 |
delete_transient($option_name); |
| 115 |
else |
| 116 |
delete_option('_transient_' . $option_name); |
| 117 |
} else { |
| 118 |
if (delete_site_transient($option_name)) |
| 119 |
delete_site_transient($option_name); |
| 120 |
else |
| 121 |
delete_option('_site_transient_' . $option_name); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
function mmb_null_op_buffer($buffer) |
| 126 |
{ |
| 127 |
//do nothing |
| 128 |
if (!ob_get_level()) |
| 129 |
ob_start(array( |
| 130 |
$this, |
| 131 |
'mmb_null_op_buffer' |
| 132 |
)); |
| 133 |
return ''; |
| 134 |
} |
| 135 |
|
| 136 |
function _deleteTempDir($directory) |
| 137 |
{ |
| 138 |
if (substr($directory, -1) == "/") { |
| 139 |
$directory = substr($directory, 0, -1); |
| 140 |
} |
| 141 |
// $this->_log($directory); |
| 142 |
if (!file_exists($directory) || !is_dir($directory)) { |
| 143 |
return false; |
| 144 |
} elseif (!is_readable($directory)) { |
| 145 |
return false; |
| 146 |
} else { |
| 147 |
$directoryHandle = opendir($directory); |
| 148 |
|
| 149 |
while ($contents = readdir($directoryHandle)) { |
| 150 |
if ($contents != '.' && $contents != '..') { |
| 151 |
$path = $directory . "/" . $contents; |
| 152 |
|
| 153 |
if (is_dir($path)) { |
| 154 |
$this->_deleteTempDir($path); |
| 155 |
} else { |
| 156 |
unlink($path); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
closedir($directoryHandle); |
| 161 |
rmdir($directory); |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
function _is_ftp_writable_mmb() |
| 167 |
{ |
| 168 |
if (defined('FTP_PASS')) { |
| 169 |
return true; |
| 170 |
} else |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
function _set_worker_message_id($message_id = false) |
| 175 |
{ |
| 176 |
if ($message_id) { |
| 177 |
add_option('_action_message_id', $message_id) or update_option('_action_message_id', $message_id); |
| 178 |
return $message_id; |
| 179 |
} |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
function _get_worker_message_id() |
| 184 |
{ |
| 185 |
return (int) get_option('_action_message_id'); |
| 186 |
} |
| 187 |
|
| 188 |
function _set_master_public_key($public_key = false) |
| 189 |
{ |
| 190 |
if ($public_key && !get_option('_worker_public_key')) { |
| 191 |
add_option('_worker_public_key', base64_encode($public_key)); |
| 192 |
return true; |
| 193 |
} |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
function _get_master_public_key() |
| 198 |
{ |
| 199 |
if (!get_option('_worker_public_key')) |
| 200 |
return false; |
| 201 |
return base64_decode(get_option('_worker_public_key')); |
| 202 |
} |
| 203 |
|
| 204 |
function _get_master_referer() |
| 205 |
{ |
| 206 |
if (!get_option('_master_referer')) |
| 207 |
return false; |
| 208 |
return base64_decode(get_option('_master_referer')); |
| 209 |
} |
| 210 |
|
| 211 |
function _get_random_signature() |
| 212 |
{ |
| 213 |
if (!get_option('_worker_nossl_key')) |
| 214 |
return false; |
| 215 |
return base64_decode(get_option('_worker_nossl_key')); |
| 216 |
} |
| 217 |
|
| 218 |
function _set_random_signature($random_key = false) |
| 219 |
{ |
| 220 |
if ($random_key && !get_option('_worker_nossl_key')) { |
| 221 |
add_option('_worker_nossl_key', base64_encode($random_key)); |
| 222 |
return true; |
| 223 |
} |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
function _authenticate_message($data = false, $signature = false, $message_id = false) |
| 229 |
{ |
| 230 |
if (!$data && !$signature) { |
| 231 |
return array( |
| 232 |
'error' => 'Authentication failed.' |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
$current_message = $this->_get_worker_message_id(); |
| 237 |
|
| 238 |
if ((int) $current_message > (int) $message_id) |
| 239 |
return array( |
| 240 |
'error' => 'Invalid message recieved. You can try to reinstall worker plugin and re-add the site to your account.' |
| 241 |
); |
| 242 |
|
| 243 |
$pl_key = $this->_get_master_public_key(); |
| 244 |
if (!$pl_key) { |
| 245 |
return array( |
| 246 |
'error' => 'Authentication failed (public key). You can try to reinstall worker plugin and re-add the site to your account.' |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
if (function_exists('openssl_verify') && !$this->_get_random_signature()) { |
| 251 |
$verify = openssl_verify($data, $signature, $pl_key); |
| 252 |
if ($verify == 1) { |
| 253 |
$message_id = $this->_set_worker_message_id($message_id); |
| 254 |
return true; |
| 255 |
} else if ($verify == 0) { |
| 256 |
return array( |
| 257 |
'error' => 'Invalid message signature. You can try to reinstall worker plugin and re-add the site to your account.' |
| 258 |
); |
| 259 |
} else { |
| 260 |
return array( |
| 261 |
'error' => 'Command not successful! Please try again.' |
| 262 |
); |
| 263 |
} |
| 264 |
} else if ($this->_get_random_signature()) { |
| 265 |
if (md5($data . $this->_get_random_signature()) == $signature) { |
| 266 |
$message_id = $this->_set_worker_message_id($message_id); |
| 267 |
return true; |
| 268 |
} |
| 269 |
return array( |
| 270 |
'error' => 'Invalid message signature. You can try to reinstall the worker plugin and then re-add the site to your dashboard.' |
| 271 |
); |
| 272 |
} |
| 273 |
// no rand key - deleted in get_stat maybe |
| 274 |
else |
| 275 |
return array( |
| 276 |
'error' => 'Invalid message signature, try reinstalling worker plugin and re-adding the site to your dashboard.' |
| 277 |
); |
| 278 |
} |
| 279 |
|
| 280 |
function _check_if_user_exists($username = false) |
| 281 |
{ |
| 282 |
global $wpdb; |
| 283 |
if ($username) { |
| 284 |
require_once(ABSPATH . WPINC . '/registration.php'); |
| 285 |
include_once(ABSPATH . 'wp-includes/pluggable.php'); |
| 286 |
|
| 287 |
if (username_exists($username) == null) { |
| 288 |
return false; |
| 289 |
} |
| 290 |
$user = (array)get_userdatabylogin($username); |
| 291 |
if ($user[$wpdb->prefix.'user_level'] == 10 || isset($user[$wpdb->prefix.'capabilities']['administrator'])) { |
| 292 |
define('MMB_USER_CAPABILITIES', $user->wp_user_level); |
| 293 |
return true; |
| 294 |
} |
| 295 |
return false; |
| 296 |
} |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
function refresh_updates() |
| 301 |
{ |
| 302 |
if (rand(1, 3) == '2') { |
| 303 |
require_once(ABSPATH . WPINC . '/update.php'); |
| 304 |
wp_update_plugins(); |
| 305 |
wp_update_themes(); |
| 306 |
wp_version_check(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
function remove_http($url = '') |
| 311 |
{ |
| 312 |
if ($url == 'http://' OR $url == 'https://') { |
| 313 |
return $url; |
| 314 |
} |
| 315 |
return preg_replace('/^(http|https)\:\/\/(www.)?/i', '', $url); |
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
function mmb_get_error($error_object) |
| 320 |
{ |
| 321 |
if (!is_wp_error($error_object)) { |
| 322 |
return $error_object != '' ? $error_object : ' error occured.'; |
| 323 |
} else { |
| 324 |
$errors = array(); |
| 325 |
foreach ($error_object->error_data as $error_key => $error_string) { |
| 326 |
$errors[] = str_replace('_', ' ', ucfirst($error_key)) . ' - ' . $error_string; |
| 327 |
} |
| 328 |
return implode('<br />', $errors); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
} |
| 334 |
?> |