class-server-utils.php
1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace Smush\Core;
class Server_Utils {
/**
* @var string
*/
private $mysql_version;
public function get_server_type() {
if ( empty( $_SERVER['SERVER_SOFTWARE'] ) ) {
return '';
}
$server_software = wp_unslash( $_SERVER['SERVER_SOFTWARE'] );
if ( ! is_array( $server_software ) ) {
$server_software = array( $server_software );
}
$server_software = array_map( 'strtolower', $server_software );
$is_nginx = $this->array_has_needle( $server_software, 'nginx' );
if ( $is_nginx ) {
return 'nginx';
}
$is_apache = $this->array_has_needle( $server_software, 'apache' );
if ( $is_apache ) {
return 'apache';
}
return '';
}
public function get_memory_limit() {
if ( function_exists( 'ini_get' ) ) {
$memory_limit = ini_get( 'memory_limit' );
} else {
// Sensible default.
$memory_limit = '128M';
}
if ( ! $memory_limit || - 1 === $memory_limit ) {
// Unlimited, set to 32GB.
$memory_limit = '32000M';
}
return intval( $memory_limit ) * 1024 * 1024;
}
public function get_memory_usage() {
return memory_get_usage( true );
}
private function array_has_needle( $array, $needle ) {
foreach ( $array as $item ) {
if ( strpos( $item, $needle ) !== false ) {
return true;
}
}
return false;
}
public function get_mysql_version() {
if ( ! $this->mysql_version ) {
global $wpdb;
$this->mysql_version = $wpdb->db_version();
}
return $this->mysql_version;
}
public function get_max_execution_time() {
return (int) ini_get( 'max_execution_time' );
}
public function get_user_agent() {
return ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) : '';
}
}