api
2 days ago
background
2 days ago
backups
2 days ago
bulk
2 days ago
cache
2 days ago
cli
2 days ago
external
2 days ago
frontend
2 days ago
integrations
2 days ago
lazy-load
2 days ago
media
2 days ago
media-library
2 days ago
membership
2 days ago
modules
2 days ago
parser
2 days ago
photon
2 days ago
png2jpg
2 days ago
product-analytics
2 days ago
rating-notification
2 days ago
resize
2 days ago
security
2 days ago
smush
2 days ago
srcset
2 days ago
stats
2 days ago
threads
2 days ago
transform
2 days ago
class-abstract-settings-dto.php
2 days ago
class-activity-log-controller.php
2 days ago
class-animated-status-controller.php
2 days ago
class-array-utils.php
2 days ago
class-attachment-id-list.php
2 days ago
class-backup-size.php
2 days ago
class-configs.php
2 days ago
class-controller.php
2 days ago
class-core.php
2 days ago
class-cron-controller.php
2 days ago
class-deprecated-hooks.php
2 days ago
class-error-handler.php
2 days ago
class-file-system.php
2 days ago
class-file-utils.php
2 days ago
class-format-utils.php
2 days ago
class-helper.php
2 days ago
class-hub-connector.php
2 days ago
class-installer.php
2 days ago
class-keyword-exclusions.php
2 days ago
class-modules.php
2 days ago
class-multisite-utils.php
2 days ago
class-optimization-controller.php
2 days ago
class-optimizer.php
2 days ago
class-plugin-settings-watcher.php
2 days ago
class-rest.php
2 days ago
class-server-utils.php
2 days ago
class-settings-controller.php
2 days ago
class-settings-dto.php
2 days ago
class-settings-sanitizer.php
2 days ago
class-settings.php
2 days ago
class-shim.php
2 days ago
class-smush-file.php
2 days ago
class-stats.php
2 days ago
class-string-utils.php
2 days ago
class-time-utils.php
2 days ago
class-timer.php
2 days ago
class-upload-dir.php
2 days ago
class-url-utils.php
2 days ago
class-urls-exclusions.php
2 days ago
class-wp-query-utils.php
2 days ago
wp-compat.php
2 days ago
class-server-utils.php
238 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Smush\Core; |
| 4 | |
| 5 | class Server_Utils { |
| 6 | private static $firefox_agent = '#Firefox/(?<version>[0-9]{2,})#i'; |
| 7 | private static $ipad_iphone_agent = '#(?:iPad|iPhone)(.*)Version/(?<version>[0-9]{2,})#i'; |
| 8 | private static $safari_agent = '#Version/(?<version>[0-9]{2,})(?:.*)Safari#i'; |
| 9 | private static $msie_trident = '/MSIE|Trident/i'; |
| 10 | /** |
| 11 | * @var string |
| 12 | */ |
| 13 | private $mysql_version; |
| 14 | |
| 15 | private function browser_webp_support() { |
| 16 | return array( |
| 17 | self::$firefox_agent => array( 'version' => 66, 'operator' => '>' ), |
| 18 | self::$ipad_iphone_agent => array( 'version' => 14, 'operator' => '>=' ), |
| 19 | self::$safari_agent => array( 'version' => 14, 'operator' => '>=' ), |
| 20 | self::$msie_trident => false, |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | public function get_server_type() { |
| 25 | if ( empty( $_SERVER['SERVER_SOFTWARE'] ) ) { |
| 26 | return ''; |
| 27 | } |
| 28 | |
| 29 | $server_software = wp_unslash( $_SERVER['SERVER_SOFTWARE'] ); |
| 30 | if ( ! is_array( $server_software ) ) { |
| 31 | $server_software = array( $server_software ); |
| 32 | } |
| 33 | |
| 34 | $server_software = array_map( 'strtolower', $server_software ); |
| 35 | $is_nginx = $this->array_has_needle( $server_software, 'nginx' ); |
| 36 | if ( $is_nginx ) { |
| 37 | return 'nginx'; |
| 38 | } |
| 39 | |
| 40 | $is_apache = $this->array_has_needle( $server_software, 'apache' ); |
| 41 | if ( $is_apache ) { |
| 42 | return 'apache'; |
| 43 | } |
| 44 | |
| 45 | return ''; |
| 46 | } |
| 47 | |
| 48 | public function get_memory_limit() { |
| 49 | if ( function_exists( 'ini_get' ) ) { |
| 50 | $memory_limit = ini_get( 'memory_limit' ); |
| 51 | } else { |
| 52 | // Sensible default. |
| 53 | $memory_limit = '128M'; |
| 54 | } |
| 55 | |
| 56 | if ( ! $memory_limit || - 1 === (int) $memory_limit ) { |
| 57 | // Unlimited, set to 32GB. |
| 58 | $memory_limit = '32000M'; |
| 59 | } |
| 60 | |
| 61 | return intval( $memory_limit ) * 1024 * 1024; |
| 62 | } |
| 63 | |
| 64 | public function get_memory_usage() { |
| 65 | return memory_get_usage( true ); |
| 66 | } |
| 67 | |
| 68 | private function array_has_needle( $array, $needle ) { |
| 69 | foreach ( $array as $item ) { |
| 70 | if ( strpos( $item, $needle ) !== false ) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | public function get_mysql_version() { |
| 79 | if ( ! $this->mysql_version ) { |
| 80 | global $wpdb; |
| 81 | /** |
| 82 | * MariaDB version prefix 5.5.5- is not stripped when using $wpdb->db_version() to get the DB version: |
| 83 | * https://github.com/php/php-src/issues/7972 |
| 84 | */ |
| 85 | $this->mysql_version = $wpdb->get_var( 'SELECT VERSION()' ); |
| 86 | } |
| 87 | return $this->mysql_version; |
| 88 | } |
| 89 | |
| 90 | public function get_max_execution_time() { |
| 91 | return (int) ini_get( 'max_execution_time' ); |
| 92 | } |
| 93 | |
| 94 | public function get_user_agent() { |
| 95 | return ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) : ''; |
| 96 | } |
| 97 | |
| 98 | public function get_document_root() { |
| 99 | return ! empty( $_SERVER['DOCUMENT_ROOT'] ) ? wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) : ''; |
| 100 | } |
| 101 | |
| 102 | public function get_http_accept_header() { |
| 103 | if ( ! empty( $_SERVER['HTTP_ACCEPT'] ) ) { |
| 104 | return wp_unslash( $_SERVER['HTTP_ACCEPT'] ); |
| 105 | } |
| 106 | |
| 107 | if ( function_exists( 'apache_request_headers' ) ) { |
| 108 | $headers = apache_request_headers(); |
| 109 | if ( ! empty( $headers['Accept'] ) ) { |
| 110 | return $headers['Accept']; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return ''; |
| 115 | } |
| 116 | |
| 117 | public function browser_supports_nextgen_format( $format ) { |
| 118 | $http_accept = $this->get_http_accept_header(); |
| 119 | return ! empty( $http_accept ) && false !== strpos( $http_accept, $format ); |
| 120 | } |
| 121 | |
| 122 | public function browser_supports_webp() { |
| 123 | $http_accept = $this->get_http_accept_header(); |
| 124 | if ( ! empty( $http_accept ) && false !== strpos( $http_accept, 'webp' ) ) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | return $this->check_user_agent_version( $this->browser_webp_support() ); |
| 129 | } |
| 130 | |
| 131 | private function check_user_agent_version( $allowed, $default = false ) { |
| 132 | $user_agent = $this->get_user_agent(); |
| 133 | |
| 134 | foreach ( $allowed as $user_agent_regex => $data ) { |
| 135 | $version = isset( $data['version'] ) ? $data['version'] : 0; |
| 136 | $operator = isset( $data['operator'] ) ? $data['operator'] : ''; |
| 137 | |
| 138 | $matches = array(); |
| 139 | if ( preg_match( $user_agent_regex, $user_agent, $matches ) ) { |
| 140 | if ( $version && $operator && $matches['version'] ) { |
| 141 | return version_compare( (int) $matches['version'], $version, $operator ); |
| 142 | } else { |
| 143 | return $data; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return $default; |
| 149 | } |
| 150 | |
| 151 | public function get_request_uri() { |
| 152 | return rawurldecode( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); |
| 153 | } |
| 154 | |
| 155 | public function get_current_url() { |
| 156 | $protocol = is_ssl() ? 'https:' : 'http:'; |
| 157 | $domain = wp_parse_url( site_url(), PHP_URL_HOST ); |
| 158 | $path = wp_parse_url( $this->get_request_uri(), PHP_URL_PATH ); |
| 159 | $query = wp_parse_url( $this->get_request_uri(), PHP_URL_QUERY ); |
| 160 | |
| 161 | $url = $protocol . '//' . $domain . $path; |
| 162 | |
| 163 | if ( $query ) { |
| 164 | $url .= '?' . $query; |
| 165 | } |
| 166 | |
| 167 | return $url; |
| 168 | } |
| 169 | |
| 170 | public function get_request_method() { |
| 171 | if ( empty( $_SERVER['REQUEST_METHOD'] ) ) { |
| 172 | return ''; |
| 173 | } |
| 174 | return strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ); |
| 175 | } |
| 176 | |
| 177 | public function get_device_type() { |
| 178 | if ( ! $this->is_mobile() ) { |
| 179 | return 'desktop'; |
| 180 | } |
| 181 | |
| 182 | if ( $this->is_tablet() ) { |
| 183 | return 'tablet'; |
| 184 | } |
| 185 | |
| 186 | return 'mobile'; |
| 187 | } |
| 188 | |
| 189 | private function is_tablet() { |
| 190 | $user_agent = $this->get_user_agent(); |
| 191 | if ( empty( $user_agent ) ) { |
| 192 | return false; |
| 193 | } |
| 194 | /** |
| 195 | * It doesn't work with IpadOS due to this: |
| 196 | * https://stackoverflow.com/questions/62323230/how-can-i-detect-with-php-that-the-user-uses-an-ipad-when-my-user-agent-doesnt-c |
| 197 | */ |
| 198 | $tablet_pattern = '/(tablet|ipad|playbook|kindle|silk)/i'; |
| 199 | return preg_match( $tablet_pattern, $user_agent ); |
| 200 | } |
| 201 | |
| 202 | private function is_mobile() { |
| 203 | $user_agent = $this->get_user_agent(); |
| 204 | if ( empty( $user_agent ) ) { |
| 205 | return false; |
| 206 | } |
| 207 | // Do not use wp_is_mobile() since it doesn't detect ipad/tablet. |
| 208 | $mobile_patten = '/Mobile|iP(hone|od|ad)|Android|BlackBerry|tablet|IEMobile|Kindle|NetFront|Silk|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune|playbook/i'; |
| 209 | return preg_match( $mobile_patten, $user_agent ); |
| 210 | } |
| 211 | |
| 212 | public function is_function_supported( $function_name ) { |
| 213 | if ( ! function_exists( $function_name ) ) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | $disabled_functions = explode( ',', ini_get( 'disable_functions' ) ); |
| 218 | if ( in_array( $function_name, $disabled_functions ) ) { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | public function curl_multi_exec_available() { |
| 226 | if ( ! function_exists( 'curl_multi_exec' ) ) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | $disabled_functions = explode( ',', ini_get( 'disable_functions' ) ); |
| 231 | if ( in_array( 'curl_multi_exec', $disabled_functions ) ) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 |