Passed
Push — master ( fa0560...6b8bc5 )
by Mike
04:54 queued 31s
created

ServerEnvironment::test_post_request()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 9
nop 0
dl 0
loc 31
rs 8.9777
c 0
b 0
f 0
1
<?php
2
/**
3
 * Server information for status report.
4
 *
5
 * @package WooCommerce/Utilities
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Utilities;
9
10
/**
11
 * ServerEnvironment class.
12
 */
13
class ServerEnvironment {
14
	/**
15
	 * Get array of environment information. Includes thing like software
16
	 * versions, and various server settings.
17
	 *
18
	 * @return array
19
	 */
20
	public function get_environment_info() {
21
		$post_request     = $this->test_post_request();
22
		$get_request      = $this->test_get_request();
23
		$database_version = wc_get_server_database_version();
24
25
		// Return all environment info. Described by JSON Schema.
26
		return array(
27
			'home_url'                  => get_option( 'home' ),
28
			'site_url'                  => get_option( 'siteurl' ),
29
			'version'                   => WC()->version,
30
			'log_directory'             => \WC_LOG_DIR,
0 ignored issues
show
Bug introduced by
The constant WC_LOG_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
			'log_directory_writable'    => (bool) @fopen( \WC_LOG_DIR . 'test-log.log', 'a' ), // phpcs:ignore
32
			'wp_version'                => get_bloginfo( 'version' ),
33
			'wp_multisite'              => is_multisite(),
34
			'wp_memory_limit'           => $this->get_wp_memory_limit(),
35
			'wp_debug_mode'             => $this->is_constant_true( 'WP_DEBUG' ),
36
			'wp_cron'                   => ! $this->is_constant_true( 'DISABLE_WP_CRON' ),
37
			'language'                  => get_locale(),
38
			'external_object_cache'     => wp_using_ext_object_cache(),
39
			'server_info'               => $this->get_server_software(),
40
			'php_version'               => phpversion(),
41
			'php_post_max_size'         => wc_let_to_num( ini_get( 'post_max_size' ) ),
42
			'php_max_execution_time'    => ini_get( 'max_execution_time' ),
43
			'php_max_input_vars'        => ini_get( 'max_input_vars' ),
44
			'curl_version'              => $this->get_curl_version(),
45
			'suhosin_installed'         => extension_loaded( 'suhosin' ),
46
			'max_upload_size'           => wp_max_upload_size(),
47
			'mysql_version'             => $database_version['number'],
48
			'mysql_version_string'      => $database_version['string'],
49
			'default_timezone'          => date_default_timezone_get(),
50
			'fsockopen_or_curl_enabled' => $this->fsockopen_or_curl_enabled(),
51
			'soapclient_enabled'        => class_exists( 'SoapClient' ),
52
			'domdocument_enabled'       => class_exists( 'DOMDocument' ),
53
			'gzip_enabled'              => is_callable( 'gzopen' ),
54
			'mbstring_enabled'          => extension_loaded( 'mbstring' ),
55
			'remote_post_successful'    => $post_request['success'],
56
			'remote_post_response'      => $post_request['response'],
57
			'remote_get_successful'     => $get_request['success'],
58
			'remote_get_response'       => $get_request['response'],
59
		);
60
	}
61
62
	/**
63
	 * Test POST to an external server.
64
	 *
65
	 * @return array
66
	 */
67
	protected function test_post_request() {
68
		$post_response_code = get_transient( 'woocommerce_test_remote_post' );
69
70
		if ( false === $post_response_code || is_wp_error( $post_response_code ) ) {
71
			$response = wp_safe_remote_post(
72
				'https://www.paypal.com/cgi-bin/webscr',
73
				array(
74
					'timeout'     => 10,
75
					'user-agent'  => 'WooCommerce/' . WC()->version,
76
					'httpversion' => '1.1',
77
					'body'        => array(
78
						'cmd' => '_notify-validate',
79
					),
80
				)
81
			);
82
			if ( ! is_wp_error( $response ) ) {
83
				$post_response_code = $response['response']['code'];
84
			}
85
			set_transient( 'woocommerce_test_remote_post', $post_response_code, HOUR_IN_SECONDS );
86
		}
87
88
		if ( is_wp_error( $post_response_code ) ) {
89
			return array(
90
				'success'  => false,
91
				'response' => $post_response_code->get_error_message(),
92
			);
93
		}
94
95
		return array(
96
			'success'  => $post_response_code >= 200 && $post_response_code < 300,
97
			'response' => $post_response_code,
98
		);
99
	}
100
101
	/**
102
	 * Test GET to an external server.
103
	 *
104
	 * @return array
105
	 */
106
	protected function test_get_request() {
107
		$get_response_code = get_transient( 'woocommerce_test_remote_get' );
108
109
		if ( false === $get_response_code || is_wp_error( $get_response_code ) ) {
110
			$response = wp_safe_remote_get( 'https://woocommerce.com/wc-api/product-key-api?request=ping&network=' . ( is_multisite() ? '1' : '0' ) );
111
			if ( ! is_wp_error( $response ) ) {
112
				$get_response_code = $response['response']['code'];
113
			}
114
			set_transient( 'woocommerce_test_remote_get', $get_response_code, HOUR_IN_SECONDS );
115
		}
116
117
		if ( is_wp_error( $get_response_code ) ) {
118
			return array(
119
				'success'  => false,
120
				'response' => $get_response_code->get_error_message(),
121
			);
122
		}
123
124
		return array(
125
			'success'  => $get_response_code >= 200 && $get_response_code < 300,
126
			'response' => $get_response_code,
127
		);
128
	}
129
130
	/**
131
	 * Return if a constant is defined and true.
132
	 *
133
	 * @param string $name Constant name.
134
	 * @return bool
135
	 */
136
	protected function is_constant_true( $name ) {
137
		return defined( $name ) && (bool) constant( $name );
138
	}
139
140
	/**
141
	 * Return info about server software running.
142
	 *
143
	 * @return string
144
	 */
145
	protected function get_server_software() {
146
		return isset( $_SERVER['SERVER_SOFTWARE'] ) ? wc_clean( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : '';
147
	}
148
149
	/**
150
	 * Get CURL version running on server.
151
	 *
152
	 * @return string
153
	 */
154
	protected function get_curl_version() {
155
		$curl_version = '';
156
		if ( function_exists( 'curl_version' ) ) {
157
			$curl_version = curl_version();
158
			$curl_version = $curl_version['version'] . ', ' . $curl_version['ssl_version'];
159
		} elseif ( extension_loaded( 'curl' ) ) {
160
			$curl_version = __( 'cURL installed but unable to retrieve version.', 'woocommerce' );
161
		}
162
		return $curl_version;
163
	}
164
165
	/**
166
	 * Get WP memory limit.
167
	 *
168
	 * @return string
169
	 */
170
	protected function get_wp_memory_limit() {
171
		$wp_memory_limit = wc_let_to_num( WP_MEMORY_LIMIT );
172
		if ( function_exists( 'memory_get_usage' ) ) {
173
			$wp_memory_limit = max( $wp_memory_limit, wc_let_to_num( @ini_get( 'memory_limit' ) ) );
174
		}
175
		return (string) $wp_memory_limit;
176
	}
177
178
	/**
179
	 * See if modules are enabled.
180
	 *
181
	 * @return bool
182
	 */
183
	protected function fsockopen_or_curl_enabled() {
184
		return function_exists( 'fsockopen' ) || function_exists( 'curl_init' );
185
	}
186
187
}
188