PluginProbe
ManageWP Worker / 3.9.29
ManageWP Worker v3.9.29
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 / functions.php

functions.php in ManageWP Worker 3.9.29, at functions.php

571 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function mwp_autoload($class)
4 {
5 if (substr($class, 0, 8) === 'Dropbox_'
6 || substr($class, 0, 8) === 'Symfony_'
7 || substr($class, 0, 8) === 'Monolog_'
8 || substr($class, 0, 5) === 'Gelf_'
9 || substr($class, 0, 4) === 'MWP_'
10 || substr($class, 0, 4) === 'MMB_'
11 || substr($class, 0, 3) === 'S3_'
12 ) {
13 $file = dirname(__FILE__).'/src/'.str_replace('_', '/', $class).'.php';
14 if (file_exists($file)) {
15 include_once $file;
16 }
17 }
18 }
19
20 function mwp_register_autoload_google()
21 {
22 static $registered;
23
24 if ($registered) {
25 return;
26 } else {
27 $registered = true;
28 }
29
30 if (version_compare(PHP_VERSION, '5.3', '<')) {
31 spl_autoload_register('mwp_autoload_google');
32 } else {
33 spl_autoload_register('mwp_autoload_google', true, true);
34 }
35 }
36
37 function mwp_autoload_google($class)
38 {
39 if (substr($class, 0, 7) === 'Google_') {
40 $file = dirname(__FILE__).'/src/'.str_replace('_', '/', $class).'.php';
41 if (file_exists($file)) {
42 include_once $file;
43 }
44 }
45 }
46
47 function mwp_container()
48 {
49 static $container;
50
51 if ($container === null) {
52 $parameters = get_option('mwp_container_parameters', array());
53 $container = new MMB_Container($parameters);
54 }
55
56 return $container;
57 }
58
59 /**
60 * @return Monolog_Psr_LoggerInterface
61 */
62 function mwp_logger()
63 {
64 static $mwp_logger;
65 if (!get_option('mwp_debug_enable', false)) {
66 if ($mwp_logger === null) {
67 $mwp_logger = new Monolog_Logger('worker', array(new Monolog_Handler_NullHandler()));
68 }
69
70 return $mwp_logger;
71 }
72 if ($mwp_logger instanceof Monolog_Logger) {
73 return $mwp_logger;
74 }
75 if ($mwp_logger === null) {
76 $mwp_logger = true;
77 $logger = mwp_container()->getLogger();
78 Monolog_Registry::addLogger($logger, 'worker');
79
80 $errorHandler = new Monolog_ErrorHandler($logger);
81 $errorHandler->registerErrorHandler();
82 $errorHandler->registerExceptionHandler();
83 $errorHandler->registerFatalHandler(null, 1024);
84 }
85
86 return Monolog_Registry::getInstance('worker');
87 }
88
89 /**
90 * @param $appKey
91 * @param $appSecret
92 * @param $token
93 * @param $tokenSecret
94 *
95 * @return Dropbox_Client
96 */
97 function mwp_dropbox_oauth1_factory($appKey, $appSecret, $token, $tokenSecret)
98 {
99 $oauthToken ='OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.$appKey.'", oauth_token="'.$token.'", oauth_signature="'.$appSecret.'&'.$tokenSecret.'"';
100 $client = new Dropbox_Client($oauthToken, $token);
101
102 return $client;
103 }
104
105 function mwp_format_memory_limit($limit)
106 {
107 if ((string) (int) $limit === (string) $limit) {
108 // The number is numeric.
109 return mwp_format_bytes($limit);
110 }
111
112 $units = strtolower(substr($limit, -1));
113
114 if (!in_array($units, array('b', 'k', 'm', 'g'))) {
115 // Invalid size unit.
116 return $limit;
117 }
118
119 $number = substr($limit, 0, -1);
120
121 if ((string) (int) $number !== $number) {
122 // The number isn't numeric.
123 return $number;
124 }
125
126 switch ($units) {
127 case 'g':
128 return $number.' GB';
129 case 'm':
130 return $number.' MB';
131 case 'k':
132 return $number.' KB';
133 case 'b':
134 default:
135 return $number.' B';
136 }
137 }
138
139
140 function mwp_format_bytes($bytes)
141 {
142 $bytes = (int) $bytes;
143
144 if ($bytes > 1024 * 1024 * 1024) {
145 return round($bytes / 1024 / 1024 / 1024, 2).' GB';
146 } elseif ($bytes > 1024 * 1024) {
147 return round($bytes / 1024 / 1024, 2).' MB';
148 } elseif ($bytes > 1024) {
149 return round($bytes / 1024, 2).' KB';
150 }
151
152 return $bytes.' B';
153 }
154
155 function mwp_log_warnings()
156 {
157 // If mbstring.func_overload is set, it changes the behavior of the standard string functions in
158 // ways that makes external libraries like Dropbox break.
159 $mbstring_func_overload = ini_get("mbstring.func_overload");
160 if ($mbstring_func_overload & 2 == 2) {
161 mwp_logger()->warning('"mbstring.func_overload" changes the behavior of the standard string functions in ways that makes external libraries like Dropbox break');
162 }
163
164 if (strlen((string) PHP_INT_MAX) < 19) {
165 // Looks like we're running on a 32-bit build of PHP. This could cause problems because some of the numbers
166 // we use (file sizes, quota, etc) can be larger than 32-bit ints can handle.
167 mwp_logger()->warning("Some external libraries rely on 64-bit integers, but it looks like we're running on a version of PHP that doesn't support 64-bit integers (PHP_INT_MAX=".((string) PHP_INT_MAX).").");
168 }
169 }
170
171 function search_posts_by_term($params = false)
172 {
173
174 global $wpdb, $current_user;
175
176 $search_type = trim($params['search_type']);
177 $search_term = strtolower(trim($params['search_term']));
178 switch ($search_type) {
179 case 'page_post':
180 $num_posts = 10;
181 $num_content_char = 30;
182
183 $term_orig = trim($params['search_term']);
184
185 $term_base = addslashes(trim($params['search_term']));
186
187 $query = "SELECT *
188 FROM $wpdb->posts
189 WHERE $wpdb->posts.post_status = 'publish'
190 AND ($wpdb->posts.post_title LIKE '%$term_base%'
191 OR $wpdb->posts.post_content LIKE '%$term_base%')
192 ORDER BY $wpdb->posts.post_modified DESC
193 LIMIT 0, $num_posts
194 ";
195
196 $posts_array = $wpdb->get_results($query);
197
198 $ret_posts = array();
199
200 foreach ($posts_array as $post) {
201 //highlight searched term
202
203 if (substr_count(strtolower($post->post_title), strtolower($term_orig))) {
204 $str_position_start = strpos(strtolower($post->post_title), strtolower($term_orig));
205
206 $post->post_title = substr($post->post_title, 0, $str_position_start).'<b>'.
207 substr($post->post_title, $str_position_start, strlen($term_orig)).'</b>'.
208 substr($post->post_title, $str_position_start + strlen($term_orig));
209
210 }
211 $post->post_content = html_entity_decode($post->post_content);
212
213 $post->post_content = strip_tags($post->post_content);
214
215
216 if (substr_count(strtolower($post->post_content), strtolower($term_orig))) {
217 $str_position_start = strpos(strtolower($post->post_content), strtolower($term_orig));
218
219 $start = $str_position_start > $num_content_char ? $str_position_start - $num_content_char : 0;
220 $first_len = $str_position_start > $num_content_char ? $num_content_char : $str_position_start;
221
222 $start_substring = $start > 0 ? '...' : '';
223 $post->post_content = $start_substring.substr($post->post_content, $start, $first_len).'<b>'.
224 substr($post->post_content, $str_position_start, strlen($term_orig)).'</b>'.
225 substr($post->post_content, $str_position_start + strlen($term_orig), $num_content_char).'...';
226
227
228 } else {
229 $post->post_content = substr($post->post_content, 0, 50).'...';
230 }
231
232 $ret_posts[] = array(
233 'ID' => $post->ID,
234 'post_permalink' => get_permalink($post->ID),
235 'post_date' => $post->post_date,
236 'post_title' => $post->post_title,
237 'post_content' => $post->post_content,
238 'post_modified' => $post->post_modified,
239 'comment_count' => $post->comment_count,
240 );
241 }
242 mmb_response($ret_posts, true);
243 break;
244
245 case 'plugin':
246 $plugins = get_option('active_plugins');
247
248 if (!function_exists('get_plugin_data')) {
249 include_once(ABSPATH.'/wp-admin/includes/plugin.php');
250 }
251
252 $have_plugin = array();
253 foreach ($plugins as $plugin) {
254 $pl = WP_PLUGIN_DIR.'/'.$plugin;
255 $pl_extended = get_plugin_data($pl);
256 $pl_name = $pl_extended['Name'];
257 if (strpos(strtolower($pl_name), $search_term) > -1) {
258
259 $have_plugin[] = $pl_name;
260 }
261 }
262 if ($have_plugin) {
263 mmb_response($have_plugin, true);
264 } else {
265 mmb_response('Not found', false);
266 }
267 break;
268 case 'theme':
269 $theme = strtolower(get_option('stylesheet'));
270 $tm = ABSPATH.'wp-content/themes/'.$theme.'/style.css';
271 $tm_extended = get_theme_data($tm);
272 $tm_name = $tm_extended['Name'];
273 $have_theme = array();
274 if (strpos(strtolower($tm_name), $search_term) > -1) {
275 $have_theme[] = $tm_name;
276 mmb_response($have_theme, true);
277 } else {
278 mmb_response('Not found', false);
279 }
280 break;
281 default:
282 mmb_response('Not found', false);
283 }
284 }
285
286 function mmb_add_action($action = false, $callback = false)
287 {
288 if (!$action || !$callback) {
289 return;
290 }
291
292 global $mmb_actions;
293
294 if (!is_callable($callback)) {
295 wp_die('The provided argument is not a valid callback');
296 }
297
298 if (isset($mmb_actions[$action])) {
299 wp_die('Cannot redeclare ManageWP action "'.$action.'".');
300 }
301
302 $mmb_actions[$action] = $callback;
303 }
304
305 function mmb_get_extended_info($stats)
306 {
307 $params = get_option('mmb_stats_filter');
308 $filter = isset($params['plugins']['cleanup']) ? $params['plugins']['cleanup'] : array();
309 $stats['num_revisions'] = mmb_num_revisions($filter['revisions']);
310 //$stats['num_revisions'] = 5;
311 $stats['overhead'] = mmb_handle_overhead(false);
312 $stats['num_spam_comments'] = mmb_num_spam_comments();
313
314 return $stats;
315 }
316
317 /* Revisions */
318 function cleanup_delete_worker($params = array())
319 {
320 $revision_params = get_option('mmb_stats_filter');
321 $revision_filter = isset($revision_params['plugins']['cleanup']) ? $revision_params['plugins']['cleanup'] : array();
322
323 $params_array = explode('_', $params['actions']);
324 $return_array = array();
325
326 foreach ($params_array as $param) {
327 switch ($param) {
328 case 'revision':
329 if (mmb_delete_all_revisions($revision_filter['revisions'])) {
330 $return_array['revision'] = 'OK';
331 } else {
332 $return_array['revision_error'] = 'OK, nothing to do';
333 }
334 break;
335 case 'overhead':
336 if (mmb_handle_overhead(true)) {
337 $return_array['overhead'] = 'OK';
338 } else {
339 $return_array['overhead_error'] = 'OK, nothing to do';
340 }
341 break;
342 case 'comment':
343 if (mmb_delete_spam_comments()) {
344 $return_array['comment'] = 'OK';
345 } else {
346 $return_array['comment_error'] = 'OK, nothing to do';
347 }
348 break;
349 default:
350 break;
351 }
352
353 }
354
355 unset($params);
356
357 mmb_response($return_array, true);
358 }
359
360 function mmb_num_revisions($filter)
361 {
362 global $wpdb;
363
364 $allRevisions = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_type = 'revision'", ARRAY_A);
365
366 $revisionsToDelete = 0;
367 $revisionsToKeepCount = array();
368
369 if (isset($filter['num_to_keep']) && !empty($filter['num_to_keep'])) {
370 $num_rev = str_replace("r_", "", $filter['num_to_keep']);
371
372 foreach ($allRevisions as $revision) {
373 $revisionsToKeepCount[$revision['post_name']] = isset($revisionsToKeepCount[$revision['post_name']])
374 ? $revisionsToKeepCount[$revision['post_name']] + 1
375 : 1;
376
377 if ($revisionsToKeepCount[$revision['post_name']] > $num_rev) {
378 ++$revisionsToDelete;
379 }
380 }
381 } else {
382 $revisionsToDelete = count($allRevisions);
383 }
384
385 return $revisionsToDelete;
386 }
387
388 function mmb_select_all_revisions()
389 {
390 global $wpdb;
391 $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'revision'";
392 $revisions = $wpdb->get_results($sql);
393
394 return $revisions;
395 }
396
397 function mmb_delete_all_revisions($filter)
398 {
399 global $wpdb;
400 $where = '';
401 $keep = isset($filter['num_to_keep']) ? $filter['num_to_keep'] : false;
402 if ($keep) {
403 $num_rev = str_replace("r_", "", $keep);
404 $allRevisions = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_type = 'revision' ORDER BY post_date DESC", ARRAY_A);
405 $revisionsToKeep = array(0 => 0);
406 $revisionsToKeepCount = array();
407
408 foreach ($allRevisions as $revision) {
409 $revisionsToKeepCount[$revision['post_name']] = isset($revisionsToKeepCount[$revision['post_name']])
410 ? $revisionsToKeepCount[$revision['post_name']] + 1
411 : 1;
412
413 if ($revisionsToKeepCount[$revision['post_name']] <= $num_rev) {
414 $revisionsToKeep[] = $revision['ID'];
415 }
416 }
417
418 $notInQuery = join(', ', $revisionsToKeep);
419
420 $where = "AND a.ID NOT IN ({$notInQuery})";
421 }
422
423 $sql = "DELETE a,b,c FROM $wpdb->posts a LEFT JOIN $wpdb->term_relationships b ON (a.ID = b.object_id) LEFT JOIN $wpdb->postmeta c ON (a.ID = c.post_id) WHERE a.post_type = 'revision' {$where}";
424
425 $revisions = $wpdb->query($sql);
426
427 return $revisions;
428 }
429
430 function mmb_handle_overhead($clear = false)
431 {
432 /** @var wpdb $wpdb */
433 global $wpdb;
434 $query = 'SHOW TABLE STATUS';
435 $tables = $wpdb->get_results($query, ARRAY_A);
436 $total_gain = 0;
437 $table_string = '';
438 foreach ($tables as $table) {
439 if (isset($table['Engine']) && $table['Engine'] === 'MyISAM') {
440 if ($wpdb->base_prefix != $wpdb->prefix) {
441 if (preg_match('/^'.$wpdb->prefix.'*/Ui', $table['Name'])) {
442 if ($table['Data_free'] > 0) {
443 $total_gain += $table['Data_free'] / 1024;
444 $table_string .= $table['Name'].",";
445 }
446 }
447 } else {
448 if (preg_match('/^'.$wpdb->prefix.'[0-9]{1,20}_*/Ui', $table['Name'])) {
449 continue;
450 } else {
451 if ($table['Data_free'] > 0) {
452 $total_gain += $table['Data_free'] / 1024;
453 $table_string .= $table['Name'].",";
454 }
455 }
456 }
457 // @todo check if the cleanup was successful, if not, set a flag always skip innodb cleanup
458 //} elseif (isset($table['Engine']) && $table['Engine'] == 'InnoDB') {
459 // $innodb_file_per_table = $wpdb->get_results("SHOW VARIABLES LIKE 'innodb_file_per_table'");
460 // if (isset($innodb_file_per_table[0]->Value) && $innodb_file_per_table[0]->Value === "ON") {
461 // if ($table['Data_free'] > 0) {
462 // $total_gain += $table['Data_free'] / 1024;
463 // $table_string .= $table['Name'].",";
464 // }
465 // }
466 }
467 }
468
469 if ($clear) {
470 $table_string = substr($table_string, 0, strlen($table_string) - 1); //remove last ,
471 $table_string = rtrim($table_string);
472 $query = "OPTIMIZE TABLE $table_string";
473 $optimize = $wpdb->query($query);
474
475 return (bool)$optimize;
476 } else {
477 return round($total_gain, 3);
478 }
479 }
480
481
482 /* Spam Comments */
483 function mmb_num_spam_comments()
484 {
485 global $wpdb;
486 $sql = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 'spam'";
487 $num_spams = $wpdb->get_var($sql);
488
489 return $num_spams;
490 }
491
492 function mmb_delete_spam_comments()
493 {
494 global $wpdb;
495 $spam = 1;
496 $total = 0;
497 while (!empty($spam)) {
498 $getCommentIds = "SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = 'spam' LIMIT 200";
499 $spam = $wpdb->get_results($getCommentIds);
500 foreach ($spam as $comment) {
501 wp_delete_comment($comment->comment_ID, true);
502 }
503 $total += count($spam);
504 if (!empty($spam)) {
505 usleep(100000);
506 }
507 }
508
509 return $total;
510 }
511
512 function mmb_get_spam_comments()
513 {
514 global $wpdb;
515 $sql = "SELECT * FROM $wpdb->comments as a LEFT JOIN $wpdb->commentmeta as b WHERE a.comment_ID = b.comment_id AND a.comment_approved = 'spam'";
516 $spams = $wpdb->get_results($sql);
517
518 return $spams;
519 }
520
521 function mwp_is_nio_shell_available()
522 {
523 static $check;
524 if(isset($check)){
525 return $check;
526 }
527 try {
528 $process = new Symfony_Process_Process("cd .", dirname(__FILE__), array(), null, 1);
529 $process->run();
530 $check = $process->isSuccessful();
531 } catch (Exception $e) {
532 $check = false;
533 }
534 return $check;
535 }
536
537 function mwp_is_shell_available()
538 {
539 if (mwp_is_safe_mode()) {
540 return false;
541 }
542 if (!function_exists('proc_open') || !function_exists('escapeshellarg')) {
543 return false;
544 }
545
546 if (extension_loaded('suhosin') && $suhosin = ini_get('suhosin.executor.func.blacklist')) {
547 $suhosin = explode(',', $suhosin);
548 $blacklist = array_map('trim', $suhosin);
549 $blacklist = array_map('strtolower', $blacklist);
550 if (in_array('proc_open', $blacklist)) {
551 return false;
552 }
553 }
554
555 if (!mwp_is_nio_shell_available()) {
556 return false;
557 }
558
559 return true;
560 }
561
562 function mwp_is_safe_mode()
563 {
564 $value = ini_get("safe_mode");
565 if ((int) $value === 0 || strtolower($value) === "off") {
566 return false;
567 }
568
569 return true;
570 }
571