PluginProbe
ManageWP Worker / 3.9.24
ManageWP Worker v3.9.24
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / helper.class.php

helper.class.php in ManageWP Worker 3.9.24, at helper.class.php

562 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 define('MWP_SHOW_LOG', false);
14
15 class MMB_Helper
16 {
17 /**
18 * A helper function to log data
19 *
20 * @param mixed $mixed
21 */
22 function _log($mixed)
23 {
24 if(defined('MWP_SHOW_LOG') && MWP_SHOW_LOG == true){
25 if (is_array($mixed)) {
26 $mixed = print_r($mixed, 1);
27 } else if (is_object($mixed)) {
28 ob_start();
29 var_dump($mixed);
30 $mixed = ob_get_clean();
31 }
32
33 $md5 = get_option('mwp_log_md5');
34 if ($md5 === false) {
35 $md5 = md5(date('jS F Y h:i:s A'));
36 update_option('mwp_log_md5', $md5);
37 }
38 $handle = fopen(dirname(__FILE__) . '/log_'.$md5, 'a');
39 fwrite($handle, $mixed . PHP_EOL);
40 fclose($handle);
41 }
42 }
43
44 function _escape(&$array)
45 {
46 global $wpdb;
47
48 if (!is_array($array)) {
49 return ($wpdb->escape($array));
50 } else {
51 foreach ((array) $array as $k => $v) {
52 if (is_array($v)) {
53 $this->_escape($array[$k]);
54 } else if (is_object($v)) {
55 //skip
56 } else {
57 $array[$k] = $wpdb->escape($v);
58 }
59 }
60 }
61 }
62
63 /**
64 * Initializes the file system
65 *
66 */
67 function init_filesystem()
68 {
69 global $wp_filesystem;
70
71 if (!$wp_filesystem || !is_object($wp_filesystem)) {
72 WP_Filesystem();
73 }
74
75 if (!is_object($wp_filesystem))
76 return FALSE;
77
78 return TRUE;
79 }
80
81 /**
82 *
83 * Check if function exists or not on `suhosin` black list
84 *
85 */
86
87 function mmb_get_user_info( $user_info = false, $info = 'login' ){
88
89 if($user_info === false)
90 return false;
91
92 if( strlen( trim( $user_info ) ) == 0)
93 return false;
94
95
96 global $wp_version;
97 if (version_compare($wp_version, '3.2.2', '<=')){
98 return get_userdatabylogin( $user_info );
99 } else {
100 return get_user_by( $info, $user_info );
101 }
102 }
103
104 /**
105 *
106 * Call action item filters
107 *
108 */
109
110 function mmb_parse_action_params( $key = '', $params = null, $call_object = null ){
111
112 global $_mmb_item_filter;
113 $call_object = $call_object !== null ? $call_object : $this;
114 $return = array();
115
116 if(isset($_mmb_item_filter[$key]) && !empty($_mmb_item_filter[$key])){
117 if( isset($params['item_filter']) && !empty($params['item_filter'])){
118 foreach($params['item_filter'] as $_items){
119 if(!empty($_items)){
120 foreach($_items as $_item){
121 if(isset($_item[0]) && in_array($_item[0], $_mmb_item_filter[$key])){
122 $_item[1] = isset($_item[1]) ? $_item[1] : array();
123 $return = call_user_func(array( &$call_object, 'get_'.$_item[0]), $return, $_item[1]);
124 }
125 }
126 }
127 }
128 }
129 }
130
131 return $return;
132 }
133
134 /**
135 *
136 * Check if function exists or not on `suhosin` black list
137 *
138 */
139
140 function mmb_function_exists($function_callback){
141
142 if(!function_exists($function_callback))
143 return false;
144
145 $disabled = explode(', ', @ini_get('disable_functions'));
146 if (in_array($function_callback, $disabled))
147 return false;
148
149 if (extension_loaded('suhosin')) {
150 $suhosin = @ini_get("suhosin.executor.func.blacklist");
151 if (empty($suhosin) == false) {
152 $suhosin = explode(',', $suhosin);
153 $blacklist = array_map('trim', $suhosin);
154 $blacklist = array_map('strtolower', $blacklist);
155 if(in_array($function_callback, $blacklist))
156 return false;
157 }
158 }
159 return true;
160 }
161
162 /**
163 * Gets transient based on WP version
164 *
165 * @global string $wp_version
166 * @param string $option_name
167 * @return mixed
168 */
169
170 function mmb_set_transient($option_name = false, $data = false){
171
172 if (!$option_name || !$data) {
173 return false;
174 }
175 if($this->mmb_multisite)
176 return $this->mmb_set_sitemeta_transient($option_name, $data);
177
178 global $wp_version;
179
180 if (version_compare($wp_version, '2.7.9', '<=')) {
181 update_option($option_name, $data);
182 } else if (version_compare($wp_version, '2.9.9', '<=')) {
183 update_option('_transient_' . $option_name, $data);
184 } else {
185 update_option('_site_transient_' . $option_name, $data);
186 }
187
188 }
189 function mmb_get_transient($option_name)
190 {
191 if (trim($option_name) == '') {
192 return FALSE;
193 }
194 if($this->mmb_multisite)
195 return $this->mmb_get_sitemeta_transient($option_name);
196
197 global $wp_version;
198
199 $transient = array();
200
201 if (version_compare($wp_version, '2.7.9', '<=')) {
202 return get_option($option_name);
203 } else if (version_compare($wp_version, '2.9.9', '<=')) {
204 $transient = get_option('_transient_' . $option_name);
205 return apply_filters("transient_".$option_name, $transient);
206 } else {
207 $transient = get_option('_site_transient_' . $option_name);
208 return apply_filters("site_transient_".$option_name, $transient);
209 }
210 }
211
212 function 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 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->mmb_multisite));
234 $result = maybe_unserialize($result);
235 return $result;
236 }
237
238 function mmb_set_sitemeta_transient($option_name, $option_value){
239 global $wpdb;
240 $option_name = '_site_transient_'. $option_name;
241
242 if($this->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->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->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_worker_message_id($message_id = false)
294 {
295 if ($message_id) {
296 add_option('_action_message_id', $message_id) or update_option('_action_message_id', $message_id);
297 return $message_id;
298 }
299 return false;
300 }
301
302 function get_worker_message_id()
303 {
304 return (int) get_option('_action_message_id');
305 }
306
307 function set_master_public_key($public_key = false)
308 {
309 if ($public_key && !get_option('_worker_public_key')) {
310 add_option('_worker_public_key', base64_encode($public_key));
311 return true;
312 }
313 return false;
314 }
315
316 function get_master_public_key()
317 {
318 if (!get_option('_worker_public_key'))
319 return false;
320 return base64_decode(get_option('_worker_public_key'));
321 }
322
323
324 function get_random_signature()
325 {
326 if (!get_option('_worker_nossl_key'))
327 return false;
328 return base64_decode(get_option('_worker_nossl_key'));
329 }
330
331 function set_random_signature($random_key = false)
332 {
333 if ($random_key && !get_option('_worker_nossl_key')) {
334 add_option('_worker_nossl_key', base64_encode($random_key));
335 return true;
336 }
337 return false;
338 }
339
340
341 function authenticate_message($data = false, $signature = false, $message_id = false)
342 {
343 if (!$data && !$signature) {
344 return array(
345 'error' => 'Authentication failed.'
346 );
347 }
348
349 $current_message = $this->get_worker_message_id();
350
351 if ((int) $current_message >= (int) $message_id)
352 return array(
353 'error' => 'Invalid message recieved. Deactivate and activate the ManageWP Worker plugin on this site, then re-add it to your ManageWP account.'
354 );
355
356 $pl_key = $this->get_master_public_key();
357 if (!$pl_key) {
358 return array(
359 'error' => 'Authentication failed. Deactivate and activate the ManageWP Worker plugin on this site, then re-add it to your ManageWP account.'
360 );
361 }
362
363 if (function_exists('openssl_verify') && !$this->get_random_signature()) {
364 $verify = openssl_verify($data, $signature, $pl_key);
365 if ($verify == 1) {
366 $message_id = $this->set_worker_message_id($message_id);
367 return true;
368 } else if ($verify == 0) {
369 return array(
370 'error' => 'Invalid message signature. Deactivate and activate the ManageWP Worker plugin on this site, then re-add it to your ManageWP account.'
371 );
372 } else {
373 return array(
374 'error' => 'Command not successful! Please try again.'
375 );
376 }
377 } else if ($this->get_random_signature()) {
378 if (md5($data . $this->get_random_signature()) === $signature) {
379 $message_id = $this->set_worker_message_id($message_id);
380 return true;
381 }
382 return array(
383 'error' => 'Invalid message signature. Deactivate and activate the ManageWP Worker plugin on this site, then re-add it to your ManageWP account.'
384 );
385 }
386 // no rand key - deleted in get_stat maybe
387 else
388 return array(
389 'error' => 'Invalid message signature. Deactivate and activate the ManageWP Worker plugin on this site, then re-add it to your ManageWP account.'
390 );
391 }
392
393 function get_secure_hash(){
394
395 $pl_key = $this->get_master_public_key();
396 if ( empty($pl_key) )
397 $pl_key = $this->get_random_signature();
398
399 if( !empty($pl_key) )
400 return md5(base64_encode($pl_key));
401
402 return false;
403 }
404
405 function _secure_data($data = false){
406 if($data == false)
407 return false;
408
409 $pl_key = $this->get_master_public_key();
410 if (!$pl_key)
411 return false;
412
413 $secure = '';
414 if( function_exists('openssl_public_decrypt') && !$this->get_random_signature() ){
415 if(is_array($data) && !empty($data)){
416 foreach($data as $input){
417 openssl_public_decrypt($input, $decrypted, $pl_key);
418 $secure .= $decrypted;
419 }
420 } else if ( is_string( $data ) ) {
421 openssl_public_decrypt($data, $decrypted, $pl_key);
422 $secure = $decrypted;
423 } else {
424 $secure = $data;
425 }
426 return $secure;
427 }
428 return false;
429
430 }
431
432 function encrypt_data( $data = false, $single = false ){
433 if( empty($data) )
434 return $data;
435
436 $pl_key = $this->get_master_public_key();
437 if ( !$pl_key )
438 return false;
439
440 $data = serialize( $data );
441 $crypted = '';
442 if( function_exists('openssl_public_encrypt') && !$this->get_random_signature() ){
443 $length = strlen( $data );
444 if( $length > 100 ){
445 for($i = 0; $i <= $length + 100; $i = $i+100){
446 $input = substr($data, $i, 100);
447 openssl_public_encrypt($input, $crypt, $pl_key);
448 $crypted .= base64_encode($crypt).'::';
449 }
450 } else
451 openssl_public_encrypt($input, $crypted, $pl_key);
452 } else {
453 $crypted = base64_encode($data);
454 }
455
456 return $crypted;
457
458 }
459
460 function check_if_user_exists($username = false)
461 {
462 global $wpdb;
463 if ($username) {
464 if( !function_exists('username_exists') )
465 include_once(ABSPATH . WPINC . '/registration.php');
466
467 include_once(ABSPATH . 'wp-includes/pluggable.php');
468
469 if (username_exists($username) == null) {
470 return false;
471 }
472
473 $user = (array) $this->mmb_get_user_info( $username );
474 if ((isset($user[$wpdb->prefix . 'user_level']) && $user[$wpdb->prefix . 'user_level'] == 10) || isset($user[$wpdb->prefix . 'capabilities']['administrator']) ||
475 (isset($user['caps']['administrator']) && $user['caps']['administrator'] == 1)){
476 return true;
477 }
478 return false;
479 }
480 return false;
481 }
482
483 function refresh_updates()
484 {
485 if (rand(1, 3) == '2') {
486 require_once(ABSPATH . WPINC . '/update.php');
487 wp_update_plugins();
488 wp_update_themes();
489 wp_version_check();
490 }
491 }
492
493 function remove_http($url = '')
494 {
495 if ($url == 'http://' OR $url == 'https://') {
496 return $url;
497 }
498 return preg_replace('/^(http|https)\:\/\/(www.)?/i', '', $url);
499
500 }
501
502 function mmb_get_error($error_object)
503 {
504 if (!is_wp_error($error_object)) {
505 return $error_object != '' ? $error_object : '';
506 } else {
507 $errors = array();
508 if(!empty($error_object->error_data)) {
509 foreach ($error_object->error_data as $error_key => $error_string) {
510 $errors[] = str_replace('_', ' ', ucfirst($error_key)) . ': ' . $error_string;
511 }
512 } elseif (!empty($error_object->errors)){
513 foreach ($error_object->errors as $error_key => $err) {
514 $errors[] = 'Error: '.str_replace('_', ' ', strtolower($error_key));
515 }
516 }
517 return implode('<br />', $errors);
518 }
519 }
520
521 function is_server_writable(){
522
523 if((!defined('FTP_HOST') || !defined('FTP_USER') || !defined('FTP_PASS')) && (get_filesystem_method(array(), false) != 'direct'))
524 return false;
525 else
526 return true;
527 }
528
529 function mmb_download_url($url, $file_name)
530 {
531 if (function_exists('fopen') && function_exists('ini_get') && ini_get('allow_url_fopen') == true && ($destination = @fopen($file_name, 'wb')) && ($source = @fopen($url, "r")) ) {
532
533 while ($a = @fread($source, 1024* 1024)) {
534 @fwrite($destination, $a);
535 }
536
537 fclose($source);
538 fclose($destination);
539 } else
540 if (!fsockopen_download($url, $file_name))
541 die('Error downloading file ' . $url);
542 return $file_name;
543 }
544
545 function return_bytes($val) {
546 $val = trim($val);
547 $last = strtolower($val[strlen($val)-1]);
548 switch($last) {
549 // The 'G' modifier is available since PHP 5.1.0
550 case 'g':
551 $val *= 1024;
552 case 'm':
553 $val *= 1024;
554 case 'k':
555 $val *= 1024;
556 }
557
558 return $val;
559 }
560
561 }
562 ?>