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

675 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PXLBSAdminify\Inc\Classes;
4
5 // no direct access allowed
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9 /**
10 * PXLBSAdminify
11 * Admin Menu: Server Info
12 *
13 * @author Jewel Theme <support@jeweltheme.com>
14 */
15
16 class ServerInfo
17 {
18
19
20 /**
21 * SERVER CPU LOAD
22 * Get the server CPU load in percentage.
23 */
24
25 public function get_server_cpu_load_percentage()
26 {
27 $result = -1;
28 $lines = null;
29
30 $os = '';
31 if (defined('PHP_OS')) {
32 $os = PHP_OS;
33 }
34
35 // Linux server
36 if ($os == 'Linux') {
37 $checks = [];
38 foreach ([0, 1] as $i) {
39 $cmd = '/proc/stat';
40 $lines = [];
41 $fh = fopen($cmd, 'r'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- reading the /proc special path not supported by WP_Filesystem.
42 if ($fh) {
43 while ($line = fgets($fh)) {
44 $lines[] = $line;
45 }
46 fclose($fh); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- closing the /proc special path not supported by WP_Filesystem.
47 }
48 foreach ($lines as $line) {
49 $ma = [];
50 if (!preg_match('/^cpu (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)$/', $line, $ma)) {
51 continue;
52 }
53 $total = $ma[1] + $ma[2] + $ma[3] + $ma[4] + $ma[5] + $ma[6] + $ma[7] + $ma[8] + $ma[9];
54 // $totalCpu = $ma[1] + $ma[2] + $ma[3];
55 // $result = (100 / $total) * $totalCpu;
56 $ma['total'] = $total;
57 $checks[] = $ma;
58 break;
59 }
60 if ($i == 0) {
61 // Wait before checking again.
62 sleep(1);
63 }
64 }
65 // Idle - prev idle
66 $diffIdle = $checks[1][4] - $checks[0][4];
67 // Total - prev total
68 $diffTotal = $checks[1]['total'] - $checks[0]['total'];
69 // Usage in %
70 $diffUsage = round((1000 * ($diffTotal - $diffIdle) / $diffTotal + 5) / 10, 2);
71 $result = $diffUsage;
72
73 return (float) $result;
74 }
75
76 return 'N/A';
77 }
78
79 /**
80 * Convert Memory Size
81 *
82 * @param [type] $size
83 *
84 * @return void
85 */
86 public function convert_memory_size($size)
87 {
88 $l = substr($size, -1);
89 $ret = substr($size, 0, -1);
90
91 switch (strtoupper($l)) {
92 case 'P':
93 $ret *= 1024;
94 case 'T':
95 $ret *= 1024;
96 case 'G':
97 $ret *= 1024;
98 case 'M':
99 $ret *= 1024;
100 case 'K':
101 $ret *= 1024;
102 }
103
104 return $ret;
105 }
106
107 /**
108 * Get Server/WP Memory Limit
109 */
110
111 public function get_wp_memory_limit()
112 {
113 $memory_limit = (int) @ini_get('memory_limit') . ' MB' . ' (' . (int) WP_MEMORY_LIMIT . ' MB)';
114 if (@ini_get('memory_limit') == '-1') {
115 $memory_limit = '-1 / ' . esc_html__('Unlimited', 'adminify') . ' (' . (int) WP_MEMORY_LIMIT . ' MB)';
116 }
117
118 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') {
119 /* translators: %s: Server PHP memory limit, e.g. "256 MB" */
120 $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>';
121 }
122
123 return $memory_limit;
124 }
125
126 /**
127 * Get PHP Version
128 */
129 public function get_php_version()
130 {
131 $php_version = 'N/A';
132
133 if (function_exists('phpversion')) {
134 $php_version = phpversion();
135 }
136
137 if (defined('PHP_VERSION')) {
138 $php_version = PHP_VERSION;
139 }
140
141 if ($php_version != 'N/A' && version_compare($php_version, '7.3', '<')) {
142 /* translators: 1: Current PHP version, 2: Link to WordPress requirements page */
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'), esc_html($php_version), '<a href="https://wordpress.org/about/requirements/" target="_blank" rel="noopener">' . __('WordPress Requirements', 'adminify') . '</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'); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required for reading the live MySQL server version; not cached intentionally.
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');
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'"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required for reading the live MySQL server variable; not cached intentionally.
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');
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'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- reading the /proc special path not supported by WP_Filesystem.
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); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- closing the /proc special path not supported by WP_Filesystem.
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'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- reading the /proc special path not supported by WP_Filesystem.
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); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- closing the /proc special path not supported by WP_Filesystem.
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'); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- reading the /proc special path not supported by WP_Filesystem.
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 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 // $path defaults to '/'. Escape it before composing the shell
535 // command so any future caller-supplied path can't break out
536 // into additional commands.
537 exec( sprintf( 'df /P %s', escapeshellarg( $path ) ), $lines );
538
539 foreach ($lines as $index => $line) {
540 if ($index != 1) {
541 continue;
542 }
543 $values = preg_split('/\s{1,}/', $line);
544 $result['size'] = round($values[1] / 1024 / 1024, 2);
545 $result['free'] = round($values[3] / 1024 / 1024, 2);
546 $result['used'] = round($values[2] / 1024 / 1024, 2);
547 $result['usage'] = round($result['used'] / $result['size'] * 100, 2);
548 break;
549 }
550
551 return $result;
552 }
553
554 return 'N/A';
555 }
556
557
558
559
560 /**
561 * CPU Load Average
562 *
563 * @return void
564 */
565
566 public function get_cpu_load_average()
567 {
568 $load = 'N/A';
569
570 // Check via PHP function
571 $avg = function_exists('sys_getloadavg') ? sys_getloadavg() : false;
572 if (!empty($avg) && is_array($avg) && 3 == count($avg)) {
573 $load = implode(', ', $avg);
574 }
575
576 return $load;
577 }
578
579 /**
580 * Get IP Address
581 *
582 * @return void
583 */
584 public function get_ip_address()
585 {
586 // Get ip address
587
588 $ip_address = isset($_SERVER['SERVER_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_ADDR'])) : '';
589 if (!$ip_address) {
590 $ip_address = isset($_SERVER['LOCAL_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['LOCAL_ADDR'])) : '';
591 }
592 return $ip_address;
593 }
594
595 /**
596 * Get WordPress Version
597 *
598 * @return void
599 */
600 public function get_wp_version()
601 {
602 global $wp_version;
603 return $wp_version;
604 }
605
606 public function check_limit()
607 {
608 $memory_limit = ini_get('memory_limit');
609 if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
610 if ($matches[2] == 'G') {
611 $memory_limit = $matches[1] . ' ' . 'GB'; // nnnG -> nnn GB
612 } elseif ($matches[2] == 'M') {
613 $memory_limit = $matches[1] . ' ' . 'MB'; // nnnM -> nnn MB
614 } elseif ($matches[2] == 'K') {
615 $memory_limit = $matches[1] . ' ' . 'KB'; // nnnK -> nnn KB
616 } elseif ($matches[2] == 'T') {
617 $memory_limit = $matches[1] . ' ' . 'TB'; // nnnT -> nnn TB
618 } elseif ($matches[2] == 'P') {
619 $memory_limit = $matches[1] . ' ' . 'PB'; // nnnP -> nnn PB
620 }
621 }
622 return $memory_limit;
623 }
624
625 public function format_filesize($bytes)
626 {
627 if (($bytes / pow(1024, 5)) > 1) {
628 return number_format_i18n(($bytes / pow(1024, 5)), 0) . ' ' . __('PB', 'adminify');
629 } elseif (($bytes / pow(1024, 4)) > 1) {
630 return number_format_i18n(($bytes / pow(1024, 4)), 0) . ' ' . __('TB', 'adminify');
631 } elseif (($bytes / pow(1024, 3)) > 1) {
632 return number_format_i18n(($bytes / pow(1024, 3)), 0) . ' ' . __('GB', 'adminify');
633 } elseif (($bytes / pow(1024, 2)) > 1) {
634 return number_format_i18n(($bytes / pow(1024, 2)), 0) . ' ' . __('MB', 'adminify');
635 } elseif ($bytes / 1024 > 1) {
636 return number_format_i18n($bytes / 1024, 0) . ' ' . __('KB', 'adminify');
637 } elseif ($bytes >= 0) {
638 return number_format_i18n($bytes, 0) . ' ' . __('bytes', 'adminify');
639 } else {
640 return __('Unknown', 'adminify');
641 }
642 }
643
644 public function format_filesize_kB($kiloBytes)
645 {
646 if (($kiloBytes / pow(1024, 4)) > 1) {
647 return number_format_i18n(($kiloBytes / pow(1024, 4)), 0) . ' ' . __('PB', 'adminify');
648 } elseif (($kiloBytes / pow(1024, 3)) > 1) {
649 return number_format_i18n(($kiloBytes / pow(1024, 3)), 0) . ' ' . __('TB', 'adminify');
650 } elseif (($kiloBytes / pow(1024, 2)) > 1) {
651 return number_format_i18n(($kiloBytes / pow(1024, 2)), 0) . ' ' . __('GB', 'adminify');
652 } elseif (($kiloBytes / 1024) > 1) {
653 return number_format_i18n($kiloBytes / 1024, 0) . ' ' . __('MB', 'adminify');
654 } elseif ($kiloBytes >= 0) {
655 return number_format_i18n($kiloBytes / 1, 0) . ' ' . __('KB', 'adminify');
656 } else {
657 return __('Unknown', 'adminify');
658 }
659 }
660
661 public function format_php_size($size)
662 {
663 if (!is_numeric($size)) {
664 if (strpos($size, 'M') !== false) {
665 $size = intval($size) * 1024 * 1024;
666 } elseif (strpos($size, 'K') !== false) {
667 $size = intval($size) * 1024;
668 } elseif (strpos($size, 'G') !== false) {
669 $size = intval($size) * 1024 * 1024 * 1024;
670 }
671 }
672 return is_numeric($size) ? $this->format_filesize($size) : $size;
673 }
674 }
675