PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.1.3
Adminify – White Label, Admin Menu Editor, Login Customizer v4.1.3
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.3, at Inc/Classes/ServerInfo.php

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