| 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 $mmb_wp_version, $_wp_using_ext_object_cache; |
| 86 |
|
| 87 |
if (version_compare($mmb_wp_version, '2.8.0', '<')) { |
| 88 |
return get_option($option_name); |
| 89 |
} else if (version_compare($mmb_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 $mmb_wp_version; |
| 109 |
|
| 110 |
if (version_compare($mmb_wp_version, '2.8.0', '<')) { |
| 111 |
delete_option($option_name); |
| 112 |
} else if (version_compare($mmb_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 delete_temp_dir($directory) |
| 126 |
{ |
| 127 |
if (substr($directory, -1) == "/") { |
| 128 |
$directory = substr($directory, 0, -1); |
| 129 |
} |
| 130 |
if (!file_exists($directory) || !is_dir($directory)) { |
| 131 |
return false; |
| 132 |
} elseif (!is_readable($directory)) { |
| 133 |
return false; |
| 134 |
} else { |
| 135 |
$directoryHandle = opendir($directory); |
| 136 |
|
| 137 |
while ($contents = readdir($directoryHandle)) { |
| 138 |
if ($contents != '.' && $contents != '..') { |
| 139 |
$path = $directory . "/" . $contents; |
| 140 |
|
| 141 |
if (is_dir($path)) { |
| 142 |
$this->delete_temp_dir($path); |
| 143 |
} else { |
| 144 |
unlink($path); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
closedir($directoryHandle); |
| 149 |
rmdir($directory); |
| 150 |
return true; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
function set_worker_message_id($message_id = false) |
| 155 |
{ |
| 156 |
if ($message_id) { |
| 157 |
add_option('_action_message_id', $message_id) or update_option('_action_message_id', $message_id); |
| 158 |
return $message_id; |
| 159 |
} |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
function get_worker_message_id() |
| 164 |
{ |
| 165 |
return (int) get_option('_action_message_id'); |
| 166 |
} |
| 167 |
|
| 168 |
function set_master_public_key($public_key = false) |
| 169 |
{ |
| 170 |
if ($public_key && !get_option('_worker_public_key')) { |
| 171 |
add_option('_worker_public_key', base64_encode($public_key)); |
| 172 |
return true; |
| 173 |
} |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
function get_master_public_key() |
| 178 |
{ |
| 179 |
if (!get_option('_worker_public_key')) |
| 180 |
return false; |
| 181 |
return base64_decode(get_option('_worker_public_key')); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
function get_random_signature() |
| 186 |
{ |
| 187 |
if (!get_option('_worker_nossl_key')) |
| 188 |
return false; |
| 189 |
return base64_decode(get_option('_worker_nossl_key')); |
| 190 |
} |
| 191 |
|
| 192 |
function set_random_signature($random_key = false) |
| 193 |
{ |
| 194 |
if ($random_key && !get_option('_worker_nossl_key')) { |
| 195 |
add_option('_worker_nossl_key', base64_encode($random_key)); |
| 196 |
return true; |
| 197 |
} |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
function authenticate_message($data = false, $signature = false, $message_id = false) |
| 203 |
{ |
| 204 |
if (!$data && !$signature) { |
| 205 |
return array( |
| 206 |
'error' => 'Authentication failed.' |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
$current_message = $this->get_worker_message_id(); |
| 211 |
|
| 212 |
if ((int) $current_message > (int) $message_id) |
| 213 |
return array( |
| 214 |
'error' => 'Invalid message recieved. You can try to reinstall worker plugin and re-add the site to your account.' |
| 215 |
); |
| 216 |
|
| 217 |
$pl_key = $this->get_master_public_key(); |
| 218 |
if (!$pl_key) { |
| 219 |
return array( |
| 220 |
'error' => 'Authentication failed (public key). You can try to reinstall worker plugin and re-add the site to your account.' |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
if (function_exists('openssl_verify') && !$this->get_random_signature()) { |
| 225 |
$verify = openssl_verify($data, $signature, $pl_key); |
| 226 |
if ($verify == 1) { |
| 227 |
$message_id = $this->set_worker_message_id($message_id); |
| 228 |
return true; |
| 229 |
} else if ($verify == 0) { |
| 230 |
return array( |
| 231 |
'error' => 'Invalid message signature. You can try to reinstall worker plugin and re-add the site to your account.' |
| 232 |
); |
| 233 |
} else { |
| 234 |
return array( |
| 235 |
'error' => 'Command not successful! Please try again.' |
| 236 |
); |
| 237 |
} |
| 238 |
} else if ($this->get_random_signature()) { |
| 239 |
if (md5($data . $this->get_random_signature()) == $signature) { |
| 240 |
$message_id = $this->set_worker_message_id($message_id); |
| 241 |
return true; |
| 242 |
} |
| 243 |
return array( |
| 244 |
'error' => 'Invalid message signature. You can try to reinstall the worker plugin and then re-add the site to your dashboard.' |
| 245 |
); |
| 246 |
} |
| 247 |
// no rand key - deleted in get_stat maybe |
| 248 |
else |
| 249 |
return array( |
| 250 |
'error' => 'Invalid message signature, try reinstalling worker plugin and re-adding the site to your dashboard.' |
| 251 |
); |
| 252 |
} |
| 253 |
|
| 254 |
function check_if_user_exists($username = false) |
| 255 |
{ |
| 256 |
global $wpdb; |
| 257 |
if ($username) { |
| 258 |
require_once(ABSPATH . WPINC . '/registration.php'); |
| 259 |
include_once(ABSPATH . 'wp-includes/pluggable.php'); |
| 260 |
|
| 261 |
if (username_exists($username) == null) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
$user = (array) get_userdatabylogin($username); |
| 265 |
if ($user[$wpdb->prefix . 'user_level'] == 10 || isset($user[$wpdb->prefix . 'capabilities']['administrator'])) { |
| 266 |
define('MMB_USER_CAPABILITIES', $user->wp_user_level); |
| 267 |
return true; |
| 268 |
} |
| 269 |
return false; |
| 270 |
} |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
function refresh_updates() |
| 275 |
{ |
| 276 |
if (rand(1, 3) == '2') { |
| 277 |
require_once(ABSPATH . WPINC . '/update.php'); |
| 278 |
wp_update_plugins(); |
| 279 |
wp_update_themes(); |
| 280 |
wp_version_check(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
function remove_http($url = '') |
| 285 |
{ |
| 286 |
if ($url == 'http://' OR $url == 'https://') { |
| 287 |
return $url; |
| 288 |
} |
| 289 |
return preg_replace('/^(http|https)\:\/\/(www.)?/i', '', $url); |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
function mmb_get_error($error_object) |
| 294 |
{ |
| 295 |
if (!is_wp_error($error_object)) { |
| 296 |
return $error_object != '' ? $error_object : ' error occured.'; |
| 297 |
} else { |
| 298 |
$errors = array(); |
| 299 |
foreach ($error_object->error_data as $error_key => $error_string) { |
| 300 |
$errors[] = str_replace('_', ' ', ucfirst($error_key)) . ': ' . $error_string; |
| 301 |
} |
| 302 |
return implode('<br />', $errors); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
} |
| 308 |
?> |