PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.1.17
Adminify – White Label, Admin Menu Editor, Login Customizer v4.1.17
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Inc / Classes / ServerInfo.php

ServerInfo.php in Adminify – White Label, Admin Menu Editor, Login Customizer 4.1.17, at Inc/Classes/ServerInfo.php

677 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPAdminify\Inc\Classes;
4
5 use WPAdminify\Inc\Utils;
6
7 // no direct access allowed
8 if (!defined('ABSPATH')) {
9 exit;
10 }
11 /**
12 * WPAdminify
13 * Admin Menu: Server Info
14 *
15 * @author Jewel Theme <support@jeweltheme.com>
16 */
17
18 class ServerInfo
19 {
20
21
22 /**
23 * SERVER CPU LOAD
24 * Get the server CPU load in percentage.
25 */
26
27 public function get_server_cpu_load_percentage()
28 {
29 $result = -1;
30 $lines = null;
31
32 $os = '';
33 if (defined('PHP_OS')) {
34 $os = PHP_OS;
35 }
36
37 // Linux server
38 if ($os == 'Linux') {
39 $checks = [];
40 foreach ([0, 1] as $i) {
41 $cmd = '/proc/stat';
42 $lines = [];
43 $fh = fopen($cmd, 'r');
44 if ($fh) {
45 while ($line = fgets($fh)) {
46 $lines[] = $line;
47 }
48 fclose($fh);
49 }
50 foreach ($lines as $line) {
51 $ma = [];
52 if (!preg_match('/^cpu (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)$/', $line, $ma)) {
53 continue;
54 }
55 $total = $ma[1] + $ma[2] + $ma[3] + $ma[4] + $ma[5] + $ma[6] + $ma[7] + $ma[8] + $ma[9];
56 // $totalCpu = $ma[1] + $ma[2] + $ma[3];
57 // $result = (100 / $total) * $totalCpu;
58 $ma['total'] = $total;
59 $checks[] = $ma;
60 break;
61 }
62 if ($i == 0) {
63 // Wait before checking again.
64 sleep(1);
65 }
66 }
67 // Idle - prev idle
68 $diffIdle = $checks[1][4] - $checks[0][4];
69 // Total - prev total
70 $diffTotal = $checks[1]['total'] - $checks[0]['total'];
71 // Usage in %
72 $diffUsage = round((1000 * ($diffTotal - $diffIdle) / $diffTotal + 5) / 10, 2);
73 $result = $diffUsage;
74
75 return (float) $result;
76 }
77
78 return 'N/A';
79 }
80
81 /**
82 * Convert Memory Size
83 *
84 * @param [type] $size
85 *
86 * @return void
87 */
88 public function convert_memory_size($size)
89 {
90 $l = substr($size, -1);
91 $ret = substr($size, 0, -1);
92
93 switch (strtoupper($l)) {
94 case 'P':
95 $ret *= 1024;
96 case 'T':
97 $ret *= 1024;
98 case 'G':
99 $ret *= 1024;
100 case 'M':
101 $ret *= 1024;
102 case 'K':
103 $ret *= 1024;
104 }
105
106 return $ret;
107 }
108
109 /**
110 * Get Server/WP Memory Limit
111 */
112
113 public function get_wp_memory_limit()
114 {
115 $memory_limit = (int) @ini_get('memory_limit') . ' MB' . ' (' . (int) WP_MEMORY_LIMIT . ' MB)';
116 if (@ini_get('memory_limit') == '-1') {
117 $memory_limit = '-1 / ' . esc_html__('Unlimited', 'adminify') . ' (' . (int) WP_MEMORY_LIMIT . ' MB)';
118 }
119
120 if ((int) WP_MEMORY_LIMIT < (int) @ini_get('memory_limit') && WP_MEMORY_LIMIT != '-1' || (int) WP_MEMORY_LIMIT < (int) @ini_get('memory_limit') && @ini_get('memory_limit') != '-1') {
121 /* translators: %s: Server PHP memory limit, e.g. "256 MB" */
122 $memory_limit .= ' <span class="warning"><span class="dashicons dashicons-warning"></span> ' . sprintf(__('The WP PHP Memory Limit is less than the %s Server PHP Memory Limit', 'adminify'), (int) @ini_get('memory_limit') . ' MB') . '!</span>';
123 }
124
125 return $memory_limit;
126 }
127
128 /**
129 * Get PHP Version
130 */
131 public function get_php_version()
132 {
133 $php_version = 'N/A';
134
135 if (function_exists('phpversion')) {
136 $php_version = phpversion();
137 }
138
139 if (defined('PHP_VERSION')) {
140 $php_version = PHP_VERSION;
141 }
142
143 if ($php_version != 'N/A' && version_compare($php_version, '7.3', '<')) {
144 /* translators: 1: Current PHP version, 2: Link to WordPress requirements page */
145 $php_version = '<span class="warning"><span class="dashicons dashicons-warning"></span> ' . sprintf(__('%1$s - Recommend PHP version of 7.3. See: %2$s', 'adminify'), esc_html($php_version), '<a href="https://wordpress.org/about/requirements/" target="_blank" rel="noopener">' . __('WordPress Requirements', 'adminify') . '</a>') . '</span>';
146 }
147
148 return $php_version;
149 }
150
151 /**
152 * Get PHP Version Only
153 * */
154 public function get_php_version_lite()
155 {
156 $php_version = 'N/A';
157
158 if (function_exists('phpversion')) {
159 $php_version = phpversion();
160 }
161
162 if (defined('PHP_VERSION')) {
163 $php_version = PHP_VERSION;
164 }
165
166 return $php_version;
167 }
168
169 /**
170 * Get Curl Version
171 *
172 * @return void
173 */
174 public function get_cURL_version()
175 {
176 $curl_version = 'N/A';
177
178 if (function_exists('curl_version')) {
179 $curl_version = curl_version();
180 $curl_version = $curl_version['version'] . ', ' . $curl_version['ssl_version'];
181 }
182
183 return $curl_version;
184 }
185
186 /**
187 * Get MySQL Version
188 *
189 * @return void
190 */
191 public function get_mysql_version()
192 {
193 global $wpdb;
194
195 // Short Version
196 // $db_version = $wpdb->db_server_info();
197
198 $db_version_dump = $wpdb->get_var('SELECT VERSION() AS version from DUAL');
199 if (preg_match('/\d+(?:\.\d+)+/', $db_version_dump, $matches)) {
200 $db_version = $matches[0]; // returning the first match
201 } else {
202 $db_version = __('N/A', 'adminify');
203 }
204 return $db_version;
205 }
206
207
208 /**
209 * Get Get Database Software Name
210 *
211 * @return void
212 */
213 public function get_db_software()
214 {
215 global $wpdb;
216 $db_software_query = $wpdb->get_row("SHOW VARIABLES LIKE 'version_comment'");
217 $db_software_dump = $db_software_query->Value;
218 if (!empty($db_software_dump)) {
219 $db_soft_array = explode(' ', trim($db_software_dump));
220 $db_software = $db_soft_array[0];
221 } else {
222 $db_software = __('N/A', 'adminify');
223 }
224
225 return $db_software;
226 }
227
228 /**
229 * Get WP Table Prefix
230 */
231 public function get_table_prefix()
232 {
233 global $wpdb;
234
235 $prefix = [
236 'tablePrefix' => $wpdb->prefix,
237 'tableBasePrefix' => $wpdb->base_prefix,
238 ];
239
240 return $prefix;
241 }
242
243
244 /**
245 * Get WP Timezone
246 */
247
248 public function get_wp_timezone()
249 {
250 $timezone = get_option('timezone_string'); // Direct value
251
252 // Create a UTC+- zone if no timezone string exists
253 if (empty($timezone)) {
254 // Current offset
255 $current_offset = get_option('gmt_offset');
256
257 // Plus offset
258 $timezone = 'UTC+' . $current_offset;
259
260 // No offset
261 if (0 == $current_offset) {
262 $timezone = 'UTC+0';
263 // Negative offset
264 } elseif ($current_offset < 0) {
265 $timezone = 'UTC' . $current_offset;
266 }
267
268 // Normalize
269 $timezone = str_replace(['.25', '.5', '.75'], [':15', ':30', ':45'], $timezone);
270 }
271
272 return $timezone;
273 }
274
275 /**
276 * Get Server/WP Total RAM
277 *
278 * @return void
279 */
280 public function get_server_total_ram()
281 {
282 $os = '';
283 if (defined('PHP_OS')) {
284 $os = PHP_OS;
285 }
286
287 $result = 0;
288
289 // Linux server
290 if ($os == 'Linux') {
291 $fh = fopen('/proc/meminfo', 'r');
292 if ($fh) {
293 while ($line = fgets($fh)) {
294 $pieces = [];
295 if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
296 $result = $pieces[1];
297 // KB to Bytes
298 $result = round($result / 1024 / 1024, 2);
299 break;
300 }
301 }
302 fclose($fh);
303 return $result;
304 }
305 }
306
307 return 'N/A';
308 }
309
310
311 /**
312 * Get Server/WP Free RAM
313 */
314 public function get_server_free_ram()
315 {
316 $os = '';
317 if (defined('PHP_OS')) {
318 $os = PHP_OS;
319 }
320
321 $result = 0;
322
323 // Linux server
324 if ($os == 'Linux') {
325 $fh = fopen('/proc/meminfo', 'r');
326 if ($fh) {
327 while ($line = fgets($fh)) {
328 $pieces = [];
329 if (preg_match('/^MemFree:\s+(\d+)\skB$/', $line, $pieces)) {
330 // KB to Bytes
331 $result = round($pieces[1] / 1024 / 1024, 2);
332 break;
333 }
334 }
335 fclose($fh);
336
337 return $result;
338 }
339 }
340
341 return 'N/A';
342 }
343
344 /**
345 * Get Server/WP RAM Details
346 *
347 * @return void
348 */
349 public function get_server_ram_details()
350 {
351 $os = '';
352 if (defined('PHP_OS')) {
353 $os = PHP_OS;
354 }
355
356 if ($os == 'Linux') {
357
358 $ram_data = '';
359 // Check if it readable
360 $fh = fopen('/proc/meminfo', 'r');
361 if ($fh) {
362 foreach (file('/proc/meminfo') as $ri) {
363 $m[strtok($ri, ':')] = strtok('');
364 }
365
366 $ram_total = round((int) $m['MemTotal'] / 1024 / 1024, 2);
367 $ram_available = round((int) $m['MemAvailable'] / 1024 / 1024, 2);
368 $ram_free = round((int) $m['MemFree'] / 1024 / 1024, 2);
369 $ram_buffers = round((int) $m['Buffers'] / 1024 / 1024, 2);
370 $ram_cached = round((int) $m['Cached'] / 1024 / 1024, 2);
371
372 $mem_kernel_app = round((100 - ($ram_buffers + $ram_cached + $ram_free) / $ram_total * 100), 2);
373 $mem_cached = round($ram_cached / $ram_total * 100, 2);
374 $mem_buffers = round($ram_buffers / $ram_total * 100, 2);
375
376 $ram_data = [
377 'MemTotal' => $ram_total,
378 'MemAvailable' => $ram_available,
379 'MemFree' => $ram_free,
380 'Buffers' => $ram_buffers,
381 'Cached' => $ram_cached,
382 'MemUsagePercentage' => round($mem_kernel_app + $mem_buffers + $mem_cached, 2), // Physical Memory
383 ];
384
385 return $ram_data;
386 }
387 }
388
389 return 'N/A';
390 }
391
392 /**
393 * Get Real Server Memory Usage
394 */
395 public function get_real_memory_usage()
396 {
397 $real_memory_usage = function_exists('memory_get_peak_usage') ? round(memory_get_peak_usage(true)) : 0;
398
399 if ($real_memory_usage) {
400 return $real_memory_usage;
401 }
402
403 return 'N/A';
404 }
405
406 /**
407 * WP Memory Usage
408 */
409 public function get_wp_memory_usage()
410 {
411 // Get PHP memory limit
412 $php_memory_limit = ini_get('memory_limit');
413
414 // Get WordPress memory limit if defined
415 $wordpress_memory_limit = defined('WP_MEMORY_LIMIT') ? WP_MEMORY_LIMIT : null;
416
417 // Determine the effective memory limit with PHP memory limit taking priority
418 $get_memory_limit = $php_memory_limit ? $php_memory_limit : $wordpress_memory_limit;
419
420
421 // Get WP Memory Limit
422 // $get_memory_limit = WP_MEMORY_LIMIT;
423 // if ((int) WP_MEMORY_LIMIT > (int) @ini_get('memory_limit')) {
424 // // WP Limit can't be greater than Server Limiit
425 // $get_memory_limit = @ini_get('memory_limit');
426 // }
427
428 $memory_limit_convert = $this->convert_memory_size($get_memory_limit);
429 $memory_limit_format = size_format($memory_limit_convert);
430 $memory_limit = $memory_limit_convert;
431
432 // Get Real Memory Usage
433 $get_memory_usage = $this->get_real_memory_usage();
434 $memory_usage_convert = round($get_memory_usage / 1024 / 1024);
435 $memory_usage_format = $memory_usage_convert . 'MB';
436 $memory_usage = $get_memory_usage;
437
438 if ($get_memory_usage != false && $get_memory_limit != false) {
439
440 // check memory limit is a numeric value
441 if (!is_numeric($memory_limit)) {
442 $memory_limit = 999;
443 }
444
445 $wp_mem_data = [
446 'MemLimit' => $memory_limit,
447 'MemLimitGet' => $get_memory_limit,
448 'MemLimitConvert' => $memory_limit_convert,
449 'MemLimitFormat' => $memory_limit_format,
450 'MemUsage' => $memory_usage,
451 'MemUsageGet' => $get_memory_usage,
452 'MemUsageConvert' => $memory_usage_convert,
453 'MemUsageFormat' => $memory_usage_format,
454 'MemUsageCalc' => round($memory_usage / $memory_limit * 100, 0),
455 ];
456
457 return $wp_mem_data;
458 }
459
460 return 'N/A';
461 }
462
463 /**
464 * Server Memory Usage
465 */
466 public function get_server_memory_usage()
467 {
468
469 // Get Server Memory Limit
470 $get_memory_limit = @ini_get('memory_limit');
471 $memory_limit_convert = $this->convert_memory_size($get_memory_limit);
472 $memory_limit_format = size_format($memory_limit_convert);
473 $memory_limit = $memory_limit_convert;
474
475 // Get Real Memory Usage
476 $get_memory_usage = $this->get_real_memory_usage();
477 $memory_usage_convert = round($get_memory_usage / 1024 / 1024);
478 $memory_usage_format = $memory_usage_convert . ' MB';
479 $memory_usage = $get_memory_usage;
480
481 if ($get_memory_usage != false && $get_memory_limit != false) {
482
483 // check memory limit is a numeric value
484 if (!is_numeric($memory_limit)) {
485 $memory_limit = 999;
486 }
487
488 $php_mem_data = [
489 'MemLimit' => $memory_limit,
490 'MemLimitGet' => $get_memory_limit,
491 'MemLimitConvert' => $memory_limit_convert,
492 'MemLimitFormat' => $memory_limit_format,
493 'MemUsage' => $memory_usage,
494 'MemUsageGet' => $get_memory_usage,
495 'MemUsageConvert' => $memory_usage_convert,
496 'MemUsageFormat' => $memory_usage_format,
497 'MemUsageCalc' => round($memory_usage / $memory_limit * 100, 0),
498 ];
499
500 return $php_mem_data;
501 }
502
503 return 'N/A';
504 }
505
506
507 public static function wp_memory_usage_percentage()
508 {
509 $memory_usage = (new ServerInfo())->get_wp_memory_usage();
510 $memory_usage_percentage = $memory_usage['MemUsageCalc'];
511 return $memory_usage_percentage;
512 }
513
514
515
516
517 /**
518 * Get Server Disk Size
519 */
520
521 public function get_server_disk_size($path = '/')
522 {
523 $os = '';
524 if (defined('PHP_OS')) {
525 $os = PHP_OS;
526 }
527
528 // Linux server
529 if ($os == 'Linux') {
530 $result = [];
531 $result['size'] = 0;
532 $result['free'] = 0;
533 $result['used'] = 0;
534
535 $lines = null;
536 // $path defaults to '/'. Escape it before composing the shell
537 // command so any future caller-supplied path can't break out
538 // into additional commands.
539 exec( sprintf( 'df /P %s', escapeshellarg( $path ) ), $lines );
540
541 foreach ($lines as $index => $line) {
542 if ($index != 1) {
543 continue;
544 }
545 $values = preg_split('/\s{1,}/', $line);
546 $result['size'] = round($values[1] / 1024 / 1024, 2);
547 $result['free'] = round($values[3] / 1024 / 1024, 2);
548 $result['used'] = round($values[2] / 1024 / 1024, 2);
549 $result['usage'] = round($result['used'] / $result['size'] * 100, 2);
550 break;
551 }
552
553 return $result;
554 }
555
556 return 'N/A';
557 }
558
559
560
561
562 /**
563 * CPU Load Average
564 *
565 * @return void
566 */
567
568 public function get_cpu_load_average()
569 {
570 $load = 'N/A';
571
572 // Check via PHP function
573 $avg = function_exists('sys_getloadavg') ? sys_getloadavg() : false;
574 if (!empty($avg) && is_array($avg) && 3 == count($avg)) {
575 $load = implode(', ', $avg);
576 }
577
578 return $load;
579 }
580
581 /**
582 * Get IP Address
583 *
584 * @return void
585 */
586 public function get_ip_address()
587 {
588 // Get ip address
589
590 $ip_address = isset($_SERVER['SERVER_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_ADDR'])) : '';
591 if (!$ip_address) {
592 $ip_address = isset($_SERVER['LOCAL_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['LOCAL_ADDR'])) : '';
593 }
594 return $ip_address;
595 }
596
597 /**
598 * Get WordPress Version
599 *
600 * @return void
601 */
602 public function get_wp_version()
603 {
604 global $wp_version;
605 return $wp_version;
606 }
607
608 public function check_limit()
609 {
610 $memory_limit = ini_get('memory_limit');
611 if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
612 if ($matches[2] == 'G') {
613 $memory_limit = $matches[1] . ' ' . 'GB'; // nnnG -> nnn GB
614 } elseif ($matches[2] == 'M') {
615 $memory_limit = $matches[1] . ' ' . 'MB'; // nnnM -> nnn MB
616 } elseif ($matches[2] == 'K') {
617 $memory_limit = $matches[1] . ' ' . 'KB'; // nnnK -> nnn KB
618 } elseif ($matches[2] == 'T') {
619 $memory_limit = $matches[1] . ' ' . 'TB'; // nnnT -> nnn TB
620 } elseif ($matches[2] == 'P') {
621 $memory_limit = $matches[1] . ' ' . 'PB'; // nnnP -> nnn PB
622 }
623 }
624 return $memory_limit;
625 }
626
627 public function format_filesize($bytes)
628 {
629 if (($bytes / pow(1024, 5)) > 1) {
630 return number_format_i18n(($bytes / pow(1024, 5)), 0) . ' ' . __('PB', 'adminify');
631 } elseif (($bytes / pow(1024, 4)) > 1) {
632 return number_format_i18n(($bytes / pow(1024, 4)), 0) . ' ' . __('TB', 'adminify');
633 } elseif (($bytes / pow(1024, 3)) > 1) {
634 return number_format_i18n(($bytes / pow(1024, 3)), 0) . ' ' . __('GB', 'adminify');
635 } elseif (($bytes / pow(1024, 2)) > 1) {
636 return number_format_i18n(($bytes / pow(1024, 2)), 0) . ' ' . __('MB', 'adminify');
637 } elseif ($bytes / 1024 > 1) {
638 return number_format_i18n($bytes / 1024, 0) . ' ' . __('KB', 'adminify');
639 } elseif ($bytes >= 0) {
640 return number_format_i18n($bytes, 0) . ' ' . __('bytes', 'adminify');
641 } else {
642 return __('Unknown', 'adminify');
643 }
644 }
645
646 public function format_filesize_kB($kiloBytes)
647 {
648 if (($kiloBytes / pow(1024, 4)) > 1) {
649 return number_format_i18n(($kiloBytes / pow(1024, 4)), 0) . ' ' . __('PB', 'adminify');
650 } elseif (($kiloBytes / pow(1024, 3)) > 1) {
651 return number_format_i18n(($kiloBytes / pow(1024, 3)), 0) . ' ' . __('TB', 'adminify');
652 } elseif (($kiloBytes / pow(1024, 2)) > 1) {
653 return number_format_i18n(($kiloBytes / pow(1024, 2)), 0) . ' ' . __('GB', 'adminify');
654 } elseif (($kiloBytes / 1024) > 1) {
655 return number_format_i18n($kiloBytes / 1024, 0) . ' ' . __('MB', 'adminify');
656 } elseif ($kiloBytes >= 0) {
657 return number_format_i18n($kiloBytes / 1, 0) . ' ' . __('KB', 'adminify');
658 } else {
659 return __('Unknown', 'adminify');
660 }
661 }
662
663 public function format_php_size($size)
664 {
665 if (!is_numeric($size)) {
666 if (strpos($size, 'M') !== false) {
667 $size = intval($size) * 1024 * 1024;
668 } elseif (strpos($size, 'K') !== false) {
669 $size = intval($size) * 1024;
670 } elseif (strpos($size, 'G') !== false) {
671 $size = intval($size) * 1024 * 1024 * 1024;
672 }
673 }
674 return is_numeric($size) ? $this->format_filesize($size) : $size;
675 }
676 }
677