PluginProbe
ManageWP Worker / 3.9.6
ManageWP Worker v3.9.6
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.6, at helper.class.php

428 lines 12.5 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 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 *
74 * Check if function exists or not on `suhosin` black list
75 *
76 */
77
78 function mmb_function_exists($function_callback){
79
80 if(!function_exists($function_callback))
81 return false;
82
83 if (extension_loaded('suhosin')) {
84 $suhosin = @ini_get("suhosin.executor.func.blacklist");
85 if (empty($suhosin) == false) {
86 $suhosin = explode(',', $suhosin);
87 $blacklist = array_map('trim', $suhosin);
88 $blacklist = array_map('strtolower', $blacklist);
89 if(in_array($function_callback, $blacklist))
90 return false;
91 }
92 }
93 return true;
94 }
95
96 /**
97 * Gets transient based on WP version
98 *
99 * @global string $wp_version
100 * @param string $option_name
101 * @return mixed
102 */
103
104 function mmb_set_transient($option_name = false, $data = false){
105
106 if (!$option_name || !$data) {
107 return false;
108 }
109 if($this->mmb_multisite)
110 return $this->mmb_set_sitemeta_transient($option_name, $data);
111
112 global $wp_version;
113
114 if (version_compare($wp_version, '2.7.9', '<=')) {
115 update_option($option_name, $data);
116 } else if (version_compare($wp_version, '2.9.9', '<=')) {
117 update_option('_transient_' . $option_name, $data);
118 } else {
119 update_option('_site_transient_' . $option_name, $data);
120 }
121
122 }
123 function mmb_get_transient($option_name)
124 {
125 if (trim($option_name) == '') {
126 return FALSE;
127 }
128 if($this->mmb_multisite)
129 return $this->mmb_get_sitemeta_transient($option_name);
130
131 global $wp_version;
132
133
134 if (version_compare($wp_version, '2.7.9', '<=')) {
135 return get_option($option_name);
136 } else if (version_compare($wp_version, '2.9.9', '<=')) {
137 return get_option('_transient_' . $option_name);
138 } else {
139 return get_option('_site_transient_' . $option_name);
140 }
141 }
142
143 function mmb_delete_transient($option_name)
144 {
145 if (trim($option_name) == '') {
146 return FALSE;
147 }
148
149 global $wp_version;
150
151 if (version_compare($wp_version, '2.7.9', '<=')) {
152 delete_option($option_name);
153 } else if (version_compare($wp_version, '2.9.9', '<=')) {
154 delete_option('_transient_' . $option_name);
155 } else {
156 delete_option('_site_transient_' . $option_name);
157 }
158 }
159
160 function mmb_get_sitemeta_transient($option_name){
161 global $wpdb;
162 $option_name = '_site_transient_'. $option_name;
163
164 $result = $wpdb->get_var( $wpdb->prepare("SELECT `meta_value` FROM `{$wpdb->sitemeta}` WHERE meta_key = '{$option_name}' AND `site_id` = '{$this->mmb_multisite}' "));
165 $result = maybe_unserialize($result);
166 return $result;
167 }
168
169 function mmb_set_sitemeta_transient($option_name, $option_value){
170 global $wpdb;
171 $option_name = '_site_transient_'. $option_name;
172
173 if($this->mmb_get_sitemeta_transient($option_name)){
174 $result = $wpdb->update( $wpdb->sitemeta,
175 array(
176 'meta_value' => maybe_serialize($option_value)
177 ),
178 array(
179 'meta_key' => $option_name,
180 'site_id' => $this->mmb_multisite
181 )
182 );
183 }else {
184 $result = $wpdb->insert( $wpdb->sitemeta,
185 array(
186 'meta_key' => $option_name,
187 'meta_value' => maybe_serialize($option_value),
188 'site_id' => $this->mmb_multisite
189 )
190 );
191 }
192 return $result;
193 }
194
195 function delete_temp_dir($directory)
196 {
197 if (substr($directory, -1) == "/") {
198 $directory = substr($directory, 0, -1);
199 }
200 if (!file_exists($directory) || !is_dir($directory)) {
201 return false;
202 } elseif (!is_readable($directory)) {
203 return false;
204 } else {
205 $directoryHandle = opendir($directory);
206
207 while ($contents = readdir($directoryHandle)) {
208 if ($contents != '.' && $contents != '..') {
209 $path = $directory . "/" . $contents;
210
211 if (is_dir($path)) {
212 $this->delete_temp_dir($path);
213 } else {
214 unlink($path);
215 }
216 }
217 }
218 closedir($directoryHandle);
219 rmdir($directory);
220 return true;
221 }
222 }
223
224 function set_worker_message_id($message_id = false)
225 {
226 if ($message_id) {
227 add_option('_action_message_id', $message_id) or update_option('_action_message_id', $message_id);
228 return $message_id;
229 }
230 return false;
231 }
232
233 function get_worker_message_id()
234 {
235 return (int) get_option('_action_message_id');
236 }
237
238 function set_master_public_key($public_key = false)
239 {
240 if ($public_key && !get_option('_worker_public_key')) {
241 add_option('_worker_public_key', base64_encode($public_key));
242 return true;
243 }
244 return false;
245 }
246
247 function get_master_public_key()
248 {
249 if (!get_option('_worker_public_key'))
250 return false;
251 return base64_decode(get_option('_worker_public_key'));
252 }
253
254
255 function get_random_signature()
256 {
257 if (!get_option('_worker_nossl_key'))
258 return false;
259 return base64_decode(get_option('_worker_nossl_key'));
260 }
261
262 function set_random_signature($random_key = false)
263 {
264 if ($random_key && !get_option('_worker_nossl_key')) {
265 add_option('_worker_nossl_key', base64_encode($random_key));
266 return true;
267 }
268 return false;
269 }
270
271
272 function authenticate_message($data = false, $signature = false, $message_id = false)
273 {
274 if (!$data && !$signature) {
275 return array(
276 'error' => 'Authentication failed.'
277 );
278 }
279
280 $current_message = $this->get_worker_message_id();
281
282 if ((int) $current_message > (int) $message_id)
283 return array(
284 'error' => 'Invalid message recieved. You can try to reinstall worker plugin and re-add the site to your account.'
285 );
286
287 $pl_key = $this->get_master_public_key();
288 if (!$pl_key) {
289 return array(
290 'error' => 'Authentication failed (public key). You can try to reinstall worker plugin and re-add the site to your account.'
291 );
292 }
293
294 if (function_exists('openssl_verify') && !$this->get_random_signature()) {
295 $verify = openssl_verify($data, $signature, $pl_key);
296 if ($verify == 1) {
297 $message_id = $this->set_worker_message_id($message_id);
298 return true;
299 } else if ($verify == 0) {
300 return array(
301 'error' => 'Invalid message signature. You can try to reinstall worker plugin and re-add the site to your account.'
302 );
303 } else {
304 return array(
305 'error' => 'Command not successful! Please try again.'
306 );
307 }
308 } else if ($this->get_random_signature()) {
309 if (md5($data . $this->get_random_signature()) == $signature) {
310 $message_id = $this->set_worker_message_id($message_id);
311 return true;
312 }
313 return array(
314 'error' => 'Invalid message signature. You can try to reinstall the worker plugin and then re-add the site to your dashboard.'
315 );
316 }
317 // no rand key - deleted in get_stat maybe
318 else
319 return array(
320 'error' => 'Invalid message signature, try reinstalling worker plugin and re-adding the site to your dashboard.'
321 );
322 }
323
324 function _secure_data($data = false){
325 if($data == false)
326 return false;
327
328 $pl_key = $this->get_master_public_key();
329 if (!$pl_key)
330 return false;
331
332 $secure = '';
333 if( function_exists('openssl_public_decrypt') && !$this->get_random_signature()){
334 if(is_array($data) && !empty($data)){
335 foreach($data as $input){
336 openssl_public_decrypt($input, &$decrypted, $pl_key);
337 $secure .= $decrypted;
338 }
339 } else {
340 openssl_public_decrypt($input, &$decrypted, $pl_key);
341 $secure = $decrypted;
342 }
343 return $secure;
344 }
345 return false;
346
347 }
348
349 function check_if_user_exists($username = false)
350 {
351 global $wpdb;
352 if ($username) {
353 require_once(ABSPATH . WPINC . '/registration.php');
354 include_once(ABSPATH . 'wp-includes/pluggable.php');
355
356 if (username_exists($username) == null) {
357 return false;
358 }
359 $user = (array) get_userdatabylogin($username);
360
361 if ($user[$wpdb->prefix . 'user_level'] == 10 || isset($user[$wpdb->prefix . 'capabilities']['administrator']) ||
362 (isset($user['caps']['administrator']) && $user['caps']['administrator'] == 1)){
363 define('MMB_USER_CAPABILITIES', $user->wp_user_level);
364 return true;
365 }
366 return false;
367 }
368 return false;
369 }
370
371 function refresh_updates()
372 {
373 if (rand(1, 3) == '2') {
374 require_once(ABSPATH . WPINC . '/update.php');
375 wp_update_plugins();
376 wp_update_themes();
377 wp_version_check();
378 }
379 }
380
381 function remove_http($url = '')
382 {
383 if ($url == 'http://' OR $url == 'https://') {
384 return $url;
385 }
386 return preg_replace('/^(http|https)\:\/\/(www.)?/i', '', $url);
387
388 }
389
390 function mmb_get_error($error_object)
391 {
392 if (!is_wp_error($error_object)) {
393 return $error_object != '' ? $error_object : '';
394 } else {
395 $errors = array();
396 foreach ($error_object->error_data as $error_key => $error_string) {
397 $errors[] = str_replace('_', ' ', ucfirst($error_key)) . ': ' . $error_string;
398 }
399 return implode('<br />', $errors);
400 }
401 }
402
403 function is_server_writable(){
404 if((!defined('FTP_HOST') || !defined('FTP_USER') || !defined('FTP_PASS')) && (get_filesystem_method(array(), false) != 'direct'))
405 return false;
406 else
407 return true;
408 }
409
410 function mmb_download_url($url, $file_name)
411 {
412 if (function_exists('fopen') && function_exists('ini_get') && ini_get('allow_url_fopen') == true && ($destination = @fopen($file_name, 'wb')) && ($source = @fopen($url, "r")) ) {
413
414
415 while ($a = @fread($source, 1024* 1024)) {
416 @fwrite($destination, $a);
417 }
418
419 fclose($source);
420 fclose($destination);
421 } else
422 if (!fsockopen_download($url, $file_name))
423 die('Error downloading file ' . $url);
424 return $file_name;
425 }
426
427 }
428 ?>