PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.1
Elementor Website Builder – more than just a page builder v3.8.1
4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 All 454 releases
elementor / modules / system-info / reporters / server.php

server.php in Elementor Website Builder – more than just a page builder 3.8.1, at modules/system-info/reporters/server.php

488 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\System_Info\Reporters;
3
4 use Elementor\Api;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 /**
11 * Elementor server environment report.
12 *
13 * Elementor system report handler class responsible for generating a report for
14 * the server environment.
15 *
16 * @since 1.0.0
17 */
18 class Server extends Base {
19
20 const KEY_PATH_WP_ROOT_DIR = 'wp_root';
21 const KEY_PATH_WP_CONTENT_DIR = 'wp_content';
22 const KEY_PATH_UPLOADS_DIR = 'uploads';
23 const KEY_PATH_ELEMENTOR_UPLOADS_DIR = 'elementor_uploads';
24 const KEY_PATH_HTACCESS_FILE = '.htaccess';
25
26 /**
27 * Get server environment reporter title.
28 *
29 * Retrieve server environment reporter title.
30 *
31 * @since 1.0.0
32 * @access public
33 *
34 * @return string Reporter title.
35 */
36 public function get_title() {
37 return 'Server Environment';
38 }
39
40 /**
41 * Get server environment report fields.
42 *
43 * Retrieve the required fields for the server environment report.
44 *
45 * @since 1.0.0
46 * @access public
47 *
48 * @return array Required report fields with field ID and field label.
49 */
50 public function get_fields() {
51 return [
52 'os' => 'Operating System',
53 'software' => 'Software',
54 'mysql_version' => 'MySQL version',
55 'php_version' => 'PHP Version',
56 'php_memory_limit' => 'PHP Memory Limit',
57 'php_max_input_vars' => 'PHP Max Input Vars',
58 'php_max_post_size' => 'PHP Max Post Size',
59 'gd_installed' => 'GD Installed',
60 'zip_installed' => 'ZIP Installed',
61 'write_permissions' => 'Write Permissions',
62 'elementor_library' => 'Elementor Library',
63 ];
64 }
65
66 /**
67 * Get server operating system.
68 *
69 * Retrieve the server operating system.
70 *
71 * @since 1.0.0
72 * @access public
73 *
74 * @return array {
75 * Report data.
76 *
77 * @type string $value Server operating system.
78 * }
79 */
80 public function get_os() {
81 return [
82 'value' => PHP_OS,
83 ];
84 }
85
86 /**
87 * Get server software.
88 *
89 * Retrieve the server software.
90 *
91 * @since 1.0.0
92 * @access public
93 *
94 * @return array {
95 * Report data.
96 *
97 * @type string $value Server software.
98 * }
99 */
100 public function get_software() {
101 return [
102 'value' => $_SERVER['SERVER_SOFTWARE'],
103 ];
104 }
105
106 /**
107 * Get PHP version.
108 *
109 * Retrieve the PHP version.
110 *
111 * @since 1.0.0
112 * @access public
113 *
114 * @return array {
115 * Report data.
116 *
117 * @type string $value PHP version.
118 * @type string $recommendation Minimum PHP version recommendation.
119 * @type bool $warning Whether to display a warning.
120 * }
121 */
122 public function get_php_version() {
123 $result = [
124 'value' => PHP_VERSION,
125 ];
126
127 if ( version_compare( $result['value'], '5.4', '<' ) ) {
128 $result['recommendation'] = _x( 'We recommend to use php 5.4 or higher', 'System Info', 'elementor' );
129
130 $result['warning'] = true;
131 }
132
133 return $result;
134 }
135
136 /**
137 * Get PHP memory limit.
138 *
139 * Retrieve the PHP memory limit.
140 *
141 * @return array {
142 * Report data.
143 *
144 * @type string $value PHP memory limit.
145 * @type string $recommendation Recommendation memory limit.
146 * @type bool $warning Whether to display a warning. True if the limit
147 * is below the recommended 128M, False otherwise.
148 * }
149 */
150 public function get_php_memory_limit() {
151 $result = [
152 'value' => (string) get_cfg_var( 'memory_limit' ),
153 ];
154
155 $min_recommended_memory = '128M';
156 $preferred_memory = '256M';
157
158 $memory_limit_bytes = wp_convert_hr_to_bytes( $result['value'] );
159
160 $min_recommended_bytes = wp_convert_hr_to_bytes( $min_recommended_memory );
161
162 if ( $memory_limit_bytes < $min_recommended_bytes ) {
163 $result['recommendation'] = sprintf(
164 /* translators: 1: Minimum recommended_memory, 2: Preferred memory, 3: WordPress wp-config memory documentation. */
165 _x( 'We recommend setting memory to at least %1$s. (%2$sMB or higher is preferred) For more information, read about <a href="%3$s">how to Increase memory allocated to PHP</a>.', 'System Info', 'elementor' ),
166 $min_recommended_memory,
167 $preferred_memory,
168 'https://go.elementor.com/wordpress-wp-config-memory/'
169 );
170
171 $result['warning'] = true;
172 }
173
174 return $result;
175 }
176
177 /**
178 * Get PHP `max_input_vars`.
179 *
180 * Retrieve the value of `max_input_vars` from `php.ini` configuration file.
181 *
182 * @since 1.0.0
183 * @access public
184 *
185 * @return array {
186 * Report data.
187 *
188 * @type string $value PHP `max_input_vars`.
189 * }
190 */
191 public function get_php_max_input_vars() {
192 return [
193 'value' => ini_get( 'max_input_vars' ),
194 ];
195 }
196
197 /**
198 * Get PHP `post_max_size`.
199 *
200 * Retrieve the value of `post_max_size` from `php.ini` configuration file.
201 *
202 * @since 1.0.0
203 * @access public
204 *
205 * @return array {
206 * Report data.
207 *
208 * @type string $value PHP `post_max_size`.
209 * }
210 */
211 public function get_php_max_post_size() {
212 return [
213 'value' => ini_get( 'post_max_size' ),
214 ];
215 }
216
217 /**
218 * Get GD installed.
219 *
220 * Whether the GD extension is installed.
221 *
222 * @since 1.0.0
223 * @access public
224 *
225 * @return array {
226 * Report data.
227 *
228 * @type string $value Yes if the GD extension is installed, No otherwise.
229 * @type bool $warning Whether to display a warning. True if the GD extension is installed, False otherwise.
230 * }
231 */
232 public function get_gd_installed() {
233 $gd_installed = extension_loaded( 'gd' );
234
235 return [
236 'value' => $gd_installed ? 'Yes' : 'No',
237 'warning' => ! $gd_installed,
238 ];
239 }
240
241 /**
242 * Get ZIP installed.
243 *
244 * Whether the ZIP extension is installed.
245 *
246 * @since 2.1.0
247 * @access public
248 *
249 * @return array {
250 * Report data.
251 *
252 * @type string $value Yes if the ZIP extension is installed, No otherwise.
253 * @type bool $warning Whether to display a warning. True if the ZIP extension is installed, False otherwise.
254 * }
255 */
256 public function get_zip_installed() {
257 $zip_installed = extension_loaded( 'zip' );
258
259 return [
260 'value' => $zip_installed ? 'Yes' : 'No',
261 'warning' => ! $zip_installed,
262 ];
263 }
264
265 /**
266 * Get MySQL version.
267 *
268 * Retrieve the MySQL version.
269 *
270 * @since 1.0.0
271 * @access public
272 *
273 * @return array {
274 * Report data.
275 *
276 * @type string $value MySQL version.
277 * }
278 */
279 public function get_mysql_version() {
280 global $wpdb;
281
282 $db_server_version = $wpdb->get_results( "SHOW VARIABLES WHERE `Variable_name` IN ( 'version_comment', 'innodb_version' )", OBJECT_K );
283
284 $db_server_version_string = $db_server_version['version_comment']->Value . ' v';
285
286 // On some hosts, `innodb_version` is empty, in PHP 8.1.
287 if ( isset( $db_server_version['innodb_version'] ) ) {
288 $db_server_version_string .= $db_server_version['innodb_version']->Value;
289 } else {
290 $db_server_version_string .= $wpdb->get_var( 'SELECT VERSION() AS version' );
291 }
292
293 return [
294 'value' => $db_server_version_string,
295 ];
296 }
297
298 /**
299 * Get write permissions.
300 * Check whether the required paths for have writing permissions.
301 *
302 * @since 1.9.0
303 * @access public
304 *
305 * @return array {
306 * Report data.
307 *
308 * @type string $value Writing permissions status.
309 * @type bool $warning Whether to display a warning. True if some required
310 * folders don't have writing permissions, False otherwise.
311 * }
312 */
313 public function get_write_permissions() : array {
314 $paths_to_check = [
315 static::KEY_PATH_WP_ROOT_DIR => $this->get_system_path( static::KEY_PATH_WP_ROOT_DIR ),
316 static::KEY_PATH_HTACCESS_FILE => $this->get_system_path( static::KEY_PATH_HTACCESS_FILE ),
317 static::KEY_PATH_UPLOADS_DIR => $this->get_system_path( static::KEY_PATH_UPLOADS_DIR ),
318 static::KEY_PATH_ELEMENTOR_UPLOADS_DIR => $this->get_system_path( static::KEY_PATH_ELEMENTOR_UPLOADS_DIR ),
319 ];
320
321 $paths_permissions = $this->get_paths_permissions( $paths_to_check );
322
323 $write_problems = [];
324
325 if ( ! $paths_permissions[ static::KEY_PATH_WP_ROOT_DIR ]['write'] ) {
326 $write_problems[] = 'WordPress root directory';
327 }
328
329 if ( ! $paths_permissions[ static::KEY_PATH_UPLOADS_DIR ]['write'] ) {
330 $write_problems[] = 'WordPress uploads directory';
331 }
332
333 if ( $paths_permissions[ self::KEY_PATH_ELEMENTOR_UPLOADS_DIR ]['exists'] && ! $paths_permissions[ self::KEY_PATH_ELEMENTOR_UPLOADS_DIR ]['write'] ) {
334 $write_problems[] = 'Elementor uploads directory';
335 }
336
337 if ( $paths_permissions[ self::KEY_PATH_HTACCESS_FILE ]['exists'] && ! $paths_permissions[ self::KEY_PATH_HTACCESS_FILE ]['write'] ) {
338 $write_problems[] = '.htaccess file';
339 }
340
341 if ( $write_problems ) {
342 $value = 'There are some writing permissions issues with the following directories/files:' . "\n\t\t - ";
343
344 $value .= implode( "\n\t\t - ", $write_problems );
345 } else {
346 $value = 'All right';
347 }
348
349 return [
350 'value' => $value,
351 'warning' => ! ! $write_problems,
352 ];
353 }
354
355 /**
356 * Check for elementor library connectivity.
357 *
358 * Check whether the remote elementor library is reachable.
359 *
360 * @since 1.0.0
361 * @access public
362 *
363 * @return array {
364 * Report data.
365 *
366 * @type string $value The status of elementor library connectivity.
367 * @type bool $warning Whether to display a warning. True if elementor
368 * * library is not reachable, False otherwise.
369 * }
370 */
371 public function get_elementor_library() {
372 $response = wp_remote_get(
373 Api::$api_info_url, [
374 'timeout' => 5,
375 'body' => [
376 // Which API version is used
377 'api_version' => ELEMENTOR_VERSION,
378 // Which language to return
379 'site_lang' => get_bloginfo( 'language' ),
380 ],
381 ]
382 );
383
384 if ( is_wp_error( $response ) ) {
385 return [
386 'value' => 'Not connected (' . $response->get_error_message() . ')',
387 'warning' => true,
388 ];
389 }
390
391 $http_response_code = wp_remote_retrieve_response_code( $response );
392
393 if ( 200 !== (int) $http_response_code ) {
394 $error_msg = 'HTTP Error (' . $http_response_code . ')';
395
396 return [
397 'value' => 'Not connected (' . $error_msg . ')',
398 'warning' => true,
399 ];
400 }
401
402 $info_data = json_decode( wp_remote_retrieve_body( $response ), true );
403
404 if ( empty( $info_data ) ) {
405 return [
406 'value' => 'Not connected (Returns invalid JSON)',
407 'warning' => true,
408 ];
409 }
410
411 return [
412 'value' => 'Connected',
413 ];
414 }
415
416 /**
417 * @param $paths [] Paths to check permissions.
418 * @return array []{exists: bool, read: bool, write: bool, execute: bool}
419 */
420 public function get_paths_permissions( $paths ) : array {
421 $permissions = [];
422
423 foreach ( $paths as $key_path => $path ) {
424 $permissions[ $key_path ] = $this->get_path_permissions( $path );
425 }
426
427 return $permissions;
428 }
429
430 /**
431 * Get path by path key.
432 *
433 * @param $path_key
434 * @return string
435 */
436 public function get_system_path( $path_key ) : string {
437 switch ( $path_key ) {
438 case static::KEY_PATH_WP_ROOT_DIR:
439 return ABSPATH;
440
441 case static::KEY_PATH_WP_CONTENT_DIR:
442 return WP_CONTENT_DIR;
443
444 case static::KEY_PATH_HTACCESS_FILE:
445 return file_exists( ABSPATH . '/.htaccess' ) ? ABSPATH . '/.htaccess' : '';
446
447 case static::KEY_PATH_UPLOADS_DIR:
448 return wp_upload_dir()['basedir'] ?? '';
449
450 case static::KEY_PATH_ELEMENTOR_UPLOADS_DIR:
451 if ( empty( wp_upload_dir()['basedir'] ) ) {
452 return '';
453 }
454
455 $elementor_uploads_dir = wp_upload_dir()['basedir'] . '/elementor';
456
457 return is_dir( $elementor_uploads_dir ) ? $elementor_uploads_dir : '';
458
459 default:
460 return '';
461 }
462 }
463
464 /**
465 * Check the permissions of a path.
466 *
467 * @param $path
468 * @return array{exists: bool, read: bool, write: bool, execute: bool}
469 */
470 public function get_path_permissions( $path ) : array {
471 if ( empty( $path ) ) {
472 return [
473 'exists' => false,
474 'read' => false,
475 'write' => false,
476 'execute' => false,
477 ];
478 }
479
480 return [
481 'exists' => true,
482 'read' => is_readable( $path ),
483 'write' => is_writeable( $path ),
484 'execute' => is_executable( $path ),
485 ];
486 }
487 }
488