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

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