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

662 lines 15.7 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 WP Memory Limit
410 $get_memory_limit = WP_MEMORY_LIMIT;
411 if ((int) WP_MEMORY_LIMIT > (int) @ini_get('memory_limit')) {
412 // WP Limit can't be greater than Server Limiit
413 $get_memory_limit = @ini_get('memory_limit');
414 }
415
416 $memory_limit_convert = $this->convert_memory_size($get_memory_limit);
417 $memory_limit_format = size_format($memory_limit_convert);
418 $memory_limit = $memory_limit_convert;
419
420 // Get Real Memory Usage
421 $get_memory_usage = $this->get_real_memory_usage();
422 $memory_usage_convert = round($get_memory_usage / 1024 / 1024);
423 $memory_usage_format = $memory_usage_convert . 'MB';
424 $memory_usage = $get_memory_usage;
425
426 if ($get_memory_usage != false && $get_memory_limit != false) {
427
428 // check memory limit is a numeric value
429 if (!is_numeric($memory_limit)) {
430 $memory_limit = 999;
431 }
432
433 $wp_mem_data = [
434 'MemLimit' => $memory_limit,
435 'MemLimitGet' => $get_memory_limit,
436 'MemLimitConvert' => $memory_limit_convert,
437 'MemLimitFormat' => $memory_limit_format,
438 'MemUsage' => $memory_usage,
439 'MemUsageGet' => $get_memory_usage,
440 'MemUsageConvert' => $memory_usage_convert,
441 'MemUsageFormat' => $memory_usage_format,
442 'MemUsageCalc' => round($memory_usage / $memory_limit * 100, 0),
443 ];
444
445 return $wp_mem_data;
446 }
447
448 return 'N/A';
449 }
450
451 /**
452 * Server Memory Usage
453 */
454 public function get_server_memory_usage()
455 {
456
457 // Get Server Memory Limit
458 $get_memory_limit = @ini_get('memory_limit');
459 $memory_limit_convert = $this->convert_memory_size($get_memory_limit);
460 $memory_limit_format = size_format($memory_limit_convert);
461 $memory_limit = $memory_limit_convert;
462
463 // Get Real Memory Usage
464 $get_memory_usage = $this->get_real_memory_usage();
465 $memory_usage_convert = round($get_memory_usage / 1024 / 1024);
466 $memory_usage_format = $memory_usage_convert . ' MB';
467 $memory_usage = $get_memory_usage;
468
469 if ($get_memory_usage != false && $get_memory_limit != false) {
470
471 // check memory limit is a numeric value
472 if (!is_numeric($memory_limit)) {
473 $memory_limit = 999;
474 }
475
476 $php_mem_data = [
477 'MemLimit' => $memory_limit,
478 'MemLimitGet' => $get_memory_limit,
479 'MemLimitConvert' => $memory_limit_convert,
480 'MemLimitFormat' => $memory_limit_format,
481 'MemUsage' => $memory_usage,
482 'MemUsageGet' => $get_memory_usage,
483 'MemUsageConvert' => $memory_usage_convert,
484 'MemUsageFormat' => $memory_usage_format,
485 'MemUsageCalc' => round($memory_usage / $memory_limit * 100, 0),
486 ];
487
488 return $php_mem_data;
489 }
490
491 return 'N/A';
492 }
493
494
495 public static function wp_memory_usage_percentage()
496 {
497 $memory_usage = (new ServerInfo())->get_wp_memory_usage();
498 $memory_usage_percentage = $memory_usage['MemUsageCalc'];
499 return $memory_usage_percentage;
500 }
501
502
503
504
505 /**
506 * Get Server Disk Size
507 */
508
509 public function get_server_disk_size($path = '/')
510 {
511 $os = '';
512 if (defined('PHP_OS')) {
513 $os = PHP_OS;
514 }
515
516 // Linux server
517 if ($os == 'Linux') {
518 $result = [];
519 $result['size'] = 0;
520 $result['free'] = 0;
521 $result['used'] = 0;
522
523 $lines = null;
524 exec(sprintf('df /P %s', $path), $lines);
525
526 foreach ($lines as $index => $line) {
527 if ($index != 1) {
528 continue;
529 }
530 $values = preg_split('/\s{1,}/', $line);
531 $result['size'] = round($values[1] / 1024 / 1024, 2);
532 $result['free'] = round($values[3] / 1024 / 1024, 2);
533 $result['used'] = round($values[2] / 1024 / 1024, 2);
534 $result['usage'] = round($result['used'] / $result['size'] * 100, 2);
535 break;
536 }
537
538 return $result;
539 }
540
541 return 'N/A';
542 }
543
544
545
546
547 /**
548 * CPU Load Average
549 *
550 * @return void
551 */
552
553 public function get_cpu_load_average()
554 {
555 $load = 'N/A';
556
557 // Check via PHP function
558 $avg = function_exists('sys_getloadavg') ? sys_getloadavg() : false;
559 if (!empty($avg) && is_array($avg) && 3 == count($avg)) {
560 $load = implode(', ', $avg);
561 }
562
563 return $load;
564 }
565
566 /**
567 * Get IP Address
568 *
569 * @return void
570 */
571 public function get_ip_address()
572 {
573 // Get ip address
574
575 $ip_address = isset($_SERVER['SERVER_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_ADDR'])) : '';
576 if (!$ip_address) {
577 $ip_address = isset($_SERVER['LOCAL_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['LOCAL_ADDR'])) : '';
578 }
579 return $ip_address;
580 }
581
582 /**
583 * Get WordPress Version
584 *
585 * @return void
586 */
587 public function get_wp_version()
588 {
589 global $wp_version;
590 return $wp_version;
591 }
592
593 public function check_limit()
594 {
595 $memory_limit = ini_get('memory_limit');
596 if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
597 if ($matches[2] == 'G') {
598 $memory_limit = $matches[1] . ' ' . 'GB'; // nnnG -> nnn GB
599 } elseif ($matches[2] == 'M') {
600 $memory_limit = $matches[1] . ' ' . 'MB'; // nnnM -> nnn MB
601 } elseif ($matches[2] == 'K') {
602 $memory_limit = $matches[1] . ' ' . 'KB'; // nnnK -> nnn KB
603 } elseif ($matches[2] == 'T') {
604 $memory_limit = $matches[1] . ' ' . 'TB'; // nnnT -> nnn TB
605 } elseif ($matches[2] == 'P') {
606 $memory_limit = $matches[1] . ' ' . 'PB'; // nnnP -> nnn PB
607 }
608 }
609 return $memory_limit;
610 }
611
612 public function format_filesize($bytes)
613 {
614 if (($bytes / pow(1024, 5)) > 1) {
615 return number_format_i18n(($bytes / pow(1024, 5)), 0) . ' ' . __('PB', 'adminify-serverinfo');
616 } elseif (($bytes / pow(1024, 4)) > 1) {
617 return number_format_i18n(($bytes / pow(1024, 4)), 0) . ' ' . __('TB', 'adminify-serverinfo');
618 } elseif (($bytes / pow(1024, 3)) > 1) {
619 return number_format_i18n(($bytes / pow(1024, 3)), 0) . ' ' . __('GB', 'adminify-serverinfo');
620 } elseif (($bytes / pow(1024, 2)) > 1) {
621 return number_format_i18n(($bytes / pow(1024, 2)), 0) . ' ' . __('MB', 'adminify-serverinfo');
622 } elseif ($bytes / 1024 > 1) {
623 return number_format_i18n($bytes / 1024, 0) . ' ' . __('KB', 'adminify-serverinfo');
624 } elseif ($bytes >= 0) {
625 return number_format_i18n($bytes, 0) . ' ' . __('bytes', 'adminify-serverinfo');
626 } else {
627 return __('Unknown', 'adminify-serverinfo');
628 }
629 }
630
631 public function format_filesize_kB($kiloBytes)
632 {
633 if (($kiloBytes / pow(1024, 4)) > 1) {
634 return number_format_i18n(($kiloBytes / pow(1024, 4)), 0) . ' ' . __('PB', 'adminify-serverinfo');
635 } elseif (($kiloBytes / pow(1024, 3)) > 1) {
636 return number_format_i18n(($kiloBytes / pow(1024, 3)), 0) . ' ' . __('TB', 'adminify-serverinfo');
637 } elseif (($kiloBytes / pow(1024, 2)) > 1) {
638 return number_format_i18n(($kiloBytes / pow(1024, 2)), 0) . ' ' . __('GB', 'adminify-serverinfo');
639 } elseif (($kiloBytes / 1024) > 1) {
640 return number_format_i18n($kiloBytes / 1024, 0) . ' ' . __('MB', 'adminify-serverinfo');
641 } elseif ($kiloBytes >= 0) {
642 return number_format_i18n($kiloBytes / 1, 0) . ' ' . __('KB', 'adminify-serverinfo');
643 } else {
644 return __('Unknown', 'adminify-serverinfo');
645 }
646 }
647
648 public function format_php_size($size)
649 {
650 if (!is_numeric($size)) {
651 if (strpos($size, 'M') !== false) {
652 $size = intval($size) * 1024 * 1024;
653 } elseif (strpos($size, 'K') !== false) {
654 $size = intval($size) * 1024;
655 } elseif (strpos($size, 'G') !== false) {
656 $size = intval($size) * 1024 * 1024 * 1024;
657 }
658 }
659 return is_numeric($size) ? $this->format_filesize($size) : $size;
660 }
661 }
662