| 1 |
<?php |
| 2 |
/************************************************************* |
| 3 |
* |
| 4 |
* helper.class.php |
| 5 |
* |
| 6 |
* Various 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 |
/** |
| 36 |
* Strips WP disallowed HTML and PHP tags from a string |
| 37 |
* |
| 38 |
* @param mixed $str |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
function _strip_tags($str) |
| 42 |
{ |
| 43 |
return strip_tags($str, '<address><a><abbr><acronym><area><b><big><blockquote><br><caption><cite><class><code><col><del><dd><div><dl><dt><em><font><h1><h2><h3><h4><h5><h6><hr><i><img><ins><kbd><li><map><ol><p><pre><q><s><span><strike><strong><sub><sup><table><tbody><td><tfoot><tr><tt><ul><var>'); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Filters a WordPress content (being comments, pages, posts etc) |
| 48 |
* |
| 49 |
* @param mixed $str |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
function _filter_content($str) |
| 53 |
{ |
| 54 |
return nl2br($this->_strip_tags($str)); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
function _escape(&$array) |
| 60 |
{ |
| 61 |
global $wpdb; |
| 62 |
|
| 63 |
if (!is_array($array)) { |
| 64 |
return ($wpdb->escape($array)); |
| 65 |
} else { |
| 66 |
foreach ((array) $array as $k => $v) { |
| 67 |
if (is_array($v)) { |
| 68 |
$this->_escape($array[$k]); |
| 69 |
} else if (is_object($v)) { |
| 70 |
//skip |
| 71 |
} else { |
| 72 |
$array[$k] = $wpdb->escape($v); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
function _base64_encode($str) |
| 79 |
{ |
| 80 |
// a plus sign can break the encoded string |
| 81 |
// if sent via URL |
| 82 |
return str_replace('+', '|', base64_encode($str)); |
| 83 |
} |
| 84 |
|
| 85 |
function _base64_decode($str) |
| 86 |
{ |
| 87 |
return base64_decode(str_replace('|', '+', $str)); |
| 88 |
} |
| 89 |
|
| 90 |
function _print_r($arr) |
| 91 |
{ |
| 92 |
if (is_string($arr)) |
| 93 |
$arr = array( |
| 94 |
$arr |
| 95 |
); |
| 96 |
echo '<pre>'; |
| 97 |
print_r($arr); |
| 98 |
echo '</pre>'; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Initializes the file system |
| 103 |
* |
| 104 |
*/ |
| 105 |
function _init_filesystem() |
| 106 |
{ |
| 107 |
global $wp_filesystem; |
| 108 |
|
| 109 |
if (!$wp_filesystem || !is_object($wp_filesystem)) { |
| 110 |
WP_Filesystem(); |
| 111 |
} |
| 112 |
|
| 113 |
if (!is_object($wp_filesystem)) |
| 114 |
return FALSE; |
| 115 |
|
| 116 |
return TRUE; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Gets transient based on WP version |
| 121 |
* |
| 122 |
* @global string $wp_version |
| 123 |
* @param string $option_name |
| 124 |
* @return mixed |
| 125 |
*/ |
| 126 |
function mmb_get_transient($option_name) |
| 127 |
{ |
| 128 |
if (trim($option_name) == '') { |
| 129 |
return FALSE; |
| 130 |
} |
| 131 |
|
| 132 |
global $wp_version, $_wp_using_ext_object_cache; |
| 133 |
|
| 134 |
if (version_compare($wp_version, '2.8.0', '<')){ |
| 135 |
return get_option($option_name); |
| 136 |
} |
| 137 |
else if (version_compare($wp_version, '3.0.0', '<')){ |
| 138 |
if(get_transient($option_name)) |
| 139 |
return get_transient($option_name); |
| 140 |
else |
| 141 |
return get_option('_transient_'.$option_name); |
| 142 |
} |
| 143 |
else { |
| 144 |
if(get_site_transient($option_name)) |
| 145 |
return get_site_transient($option_name); |
| 146 |
else |
| 147 |
return get_option('_site_transient_'.$option_name); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
function mmb_delete_transient($option_name) |
| 152 |
{ |
| 153 |
if (trim($option_name) == '') { |
| 154 |
return FALSE; |
| 155 |
} |
| 156 |
|
| 157 |
global $wp_version; |
| 158 |
|
| 159 |
if (version_compare($wp_version, '2.8.0', '<')){ |
| 160 |
delete_option($option_name); |
| 161 |
} |
| 162 |
else if (version_compare($wp_version, '3.0.0', '<')){ |
| 163 |
if(delete_transient($option_name)) |
| 164 |
delete_transient($option_name); |
| 165 |
else |
| 166 |
delete_option('_transient_'.$option_name); |
| 167 |
} |
| 168 |
else { |
| 169 |
if(delete_site_transient($option_name)) |
| 170 |
delete_site_transient($option_name); |
| 171 |
else |
| 172 |
delete_option('_site_transient_'.$option_name); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
function mmb_null_op_buffer($buffer) |
| 177 |
{ |
| 178 |
//do nothing |
| 179 |
if (!ob_get_level()) |
| 180 |
ob_start(array( |
| 181 |
$this, |
| 182 |
'mmb_null_op_buffer' |
| 183 |
)); |
| 184 |
return ''; |
| 185 |
} |
| 186 |
|
| 187 |
function _deleteTempDir($directory) |
| 188 |
{ |
| 189 |
if (substr($directory, -1) == "/") { |
| 190 |
$directory = substr($directory, 0, -1); |
| 191 |
} |
| 192 |
// $this->_log($directory); |
| 193 |
if (!file_exists($directory) || !is_dir($directory)) { |
| 194 |
return false; |
| 195 |
} elseif (!is_readable($directory)) { |
| 196 |
return false; |
| 197 |
} else { |
| 198 |
$directoryHandle = opendir($directory); |
| 199 |
|
| 200 |
while ($contents = readdir($directoryHandle)) { |
| 201 |
if ($contents != '.' && $contents != '..') { |
| 202 |
$path = $directory . "/" . $contents; |
| 203 |
|
| 204 |
if (is_dir($path)) { |
| 205 |
$this->_deleteTempDir($path); |
| 206 |
} else { |
| 207 |
unlink($path); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
closedir($directoryHandle); |
| 212 |
rmdir($directory); |
| 213 |
return true; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
function _is_ftp_writable_mmb() |
| 218 |
{ |
| 219 |
if (defined('FTP_PASS')) { |
| 220 |
return true; |
| 221 |
} else |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
function _set_worker_message_id($message_id = false) |
| 226 |
{ |
| 227 |
if ($message_id) { |
| 228 |
add_option('_action_message_id', $message_id) or update_option('_action_message_id', $message_id); |
| 229 |
return $message_id; |
| 230 |
} |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
function _get_worker_message_id() |
| 235 |
{ |
| 236 |
return (int) get_option('_action_message_id'); |
| 237 |
} |
| 238 |
|
| 239 |
function _set_master_public_key($public_key = false) |
| 240 |
{ |
| 241 |
if ($public_key && !get_option('_worker_public_key')) { |
| 242 |
add_option('_worker_public_key', base64_encode($public_key)); |
| 243 |
return true; |
| 244 |
} |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
function _get_master_public_key() |
| 249 |
{ |
| 250 |
if (!get_option('_worker_public_key')) |
| 251 |
return false; |
| 252 |
return base64_decode(get_option('_worker_public_key')); |
| 253 |
} |
| 254 |
|
| 255 |
function _get_master_referer(){ |
| 256 |
if (!get_option('_master_referer')) |
| 257 |
return false; |
| 258 |
return base64_decode(get_option('_master_referer')); |
| 259 |
} |
| 260 |
|
| 261 |
function _get_random_signature(){ |
| 262 |
if (!get_option('_worker_nossl_key')) |
| 263 |
return false; |
| 264 |
return base64_decode(get_option('_worker_nossl_key')); |
| 265 |
} |
| 266 |
|
| 267 |
function _set_random_signature($random_key = false){ |
| 268 |
if ($random_key && !get_option('_worker_nossl_key')){ |
| 269 |
add_option('_worker_nossl_key', base64_encode($random_key)); |
| 270 |
return true; |
| 271 |
} |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
function _authenticate_message($data = false, $signature = false, $message_id = false) |
| 277 |
{ |
| 278 |
if (!$data && !$signature) { |
| 279 |
return array( |
| 280 |
'error' => 'Authentication failed.' |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
$current_message = $this->_get_worker_message_id(); |
| 285 |
|
| 286 |
if ((int) $current_message > (int) $message_id) |
| 287 |
return array( |
| 288 |
'error' => 'Invalid message recieved. You can try to reinstall worker plugin and re-add the site to your account.' |
| 289 |
); |
| 290 |
|
| 291 |
$pl_key = $this->_get_master_public_key(); |
| 292 |
if (!$pl_key) { |
| 293 |
return array( |
| 294 |
'error' => 'Authentication failed (public key). You can try to reinstall worker plugin and re-add the site to your account.' |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
if( function_exists( 'openssl_verify' ) && !$this->_get_random_signature()){ |
| 299 |
$verify = openssl_verify($data, $signature, $pl_key); |
| 300 |
if ($verify == 1) { |
| 301 |
$message_id = $this->_set_worker_message_id($message_id); |
| 302 |
return true; |
| 303 |
} else if ($verify == 0) { |
| 304 |
return array( |
| 305 |
'error' => 'Invalid message signature. You can try to reinstall worker plugin and re-add the site to your account.' |
| 306 |
); |
| 307 |
} else { |
| 308 |
return array( |
| 309 |
'error' => 'Command not successful! Please try again.' |
| 310 |
); |
| 311 |
} |
| 312 |
} else if ($this->_get_random_signature()) { |
| 313 |
if(md5($data.$this->_get_random_signature()) == $signature ){ |
| 314 |
$message_id = $this->_set_worker_message_id($message_id); |
| 315 |
return true; |
| 316 |
} |
| 317 |
return array( |
| 318 |
'error' => 'Invalid message signature. You can try to reinstall the worker plugin and then re-add the site to your dashboard.' |
| 319 |
); |
| 320 |
} |
| 321 |
// no rand key - deleted in get_stat maybe |
| 322 |
else return array( |
| 323 |
'error' => 'Invalid message signature, try reinstalling worker plugin and re-adding the site to your dashboard.' |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
function _check_if_user_exists($username = false) |
| 328 |
{ |
| 329 |
if ($username) { |
| 330 |
require_once(ABSPATH . WPINC . '/registration.php'); |
| 331 |
include_once(ABSPATH . 'wp-includes/pluggable.php'); |
| 332 |
|
| 333 |
if (username_exists($username) == null) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
$user = get_userdatabylogin($username); |
| 337 |
if ($user->wp_user_level == 10 || isset($user->wp_capabilities['administrator'])) { |
| 338 |
define('MMB_USER_CAPABILITIES', $user->wp_user_level); |
| 339 |
return true; |
| 340 |
} |
| 341 |
return false; |
| 342 |
} |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
function refresh_updates() |
| 347 |
{ |
| 348 |
if (rand(1, 3) == '2') { |
| 349 |
require_once(ABSPATH . WPINC . '/update.php'); |
| 350 |
wp_update_plugins(); |
| 351 |
wp_update_themes(); |
| 352 |
wp_version_check(); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
function remove_http($url = '') |
| 357 |
{ |
| 358 |
if ($url == 'http://' OR $url == 'https://') { |
| 359 |
return $url; |
| 360 |
} |
| 361 |
return preg_replace('/^(http|https)\:\/\/(www.)?/i', '', $url); |
| 362 |
|
| 363 |
} |
| 364 |
|
| 365 |
function mmb_get_error($error_object){ |
| 366 |
if(!is_wp_error($error_object)){ |
| 367 |
return $error_object != '' ? $error_object : ' error occured.'; |
| 368 |
} |
| 369 |
else { |
| 370 |
$errors = array(); |
| 371 |
foreach($error_object->error_data as $error_key => $error_string){ |
| 372 |
$errors[] = str_replace('_', ' ', ucfirst($error_key)).' - '.$error_string; |
| 373 |
} |
| 374 |
return implode('<br />', $errors); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
} |
| 380 |
?> |