Passed
Push — master ( da7150...31d720 )
by Paul
04:54
created

System::isWebhostCheckValid()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 5
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\Cache;
7
use GeminiLabs\SiteReviews\Database\OptionManager;
8
use GeminiLabs\SiteReviews\Helper;
9
use Sinergi\BrowserDetector\Browser;
10
11
class System
12
{
13
	const PAD = 40;
14
15
	/**
16
	 * @return string
17
	 */
18
	public function __toString()
19
	{
20
		return $this->get();
21
	}
22
23
	/**
24
	 * @return string
25
	 */
26
	public function get()
27
	{
28
		$details = [
29
			'version' => 'Plugin Details',
30
			'browser' => 'Browser Details',
31
			'server' => 'Server Details',
32
			'php' => 'PHP Configuration',
33
			'wordpress' => 'WordPress Configuration',
34
			'mu-plugin' => 'Must-Use Plugins',
35
			'multisite-plugin' => 'Network Active Plugins',
36
			'active-plugin' => 'Active Plugins',
37
			'inactive-plugin' => 'Inactive Plugins',
38
			'setting' => 'Plugin Settings',
39
		];
40
		$systemInfo = array_reduce( array_keys( $details ), function( $carry, $key ) use( $details ) {
41
			$methodName = glsr( Helper::class )->buildMethodName( 'get-'.$key.'-details' );
42
			if( method_exists( $this, $methodName ) && $systemDetails = $this->$methodName() ) {
43
				return $carry.$this->implode( $details[$key], $systemDetails );
44
			}
45
			return $carry;
46
		});
47
		return trim( $systemInfo );
48
	}
49
50
	/**
51
	 * @return array
52
	 */
53
	public function getActivePluginDetails()
54
	{
55
		$plugins = get_plugins();
56
		$activePlugins = (array)get_option( 'active_plugins', [] );
57
		$inactive = array_diff_key( $plugins, array_flip( $activePlugins ));
58
		return $this->normalizePluginList( array_diff_key( $plugins, $inactive ));
59
	}
60
61
	/**
62
	 * @return array
63
	 */
64
	public function getBrowserDetails()
65
	{
66
		$browser = new Browser;
67
		$name = esc_attr( $browser->getName() );
68
		$userAgent = esc_attr( $browser->getUserAgent()->getUserAgentString() );
69
		$version = esc_attr( $browser->getVersion() );
70
		return [
71
			'Browser Name' => sprintf( '%s %s', $name, $version ),
72
			'Browser UA' => $userAgent,
73
		];
74
	}
75
76
	/**
77
	 * @return array
78
	 */
79
	public function getInactivePluginDetails()
80
	{
81
		$activePlugins = (array)get_option( 'active_plugins', [] );
82
		return $this->normalizePluginList( array_diff_key( get_plugins(), array_flip( $activePlugins )));
83
	}
84
85
	/**
86
	 * @return void|array
87
	 */
88
	public function getMuPluginDetails()
89
	{
90
		$plugins = array_merge(
91
			get_mu_plugins(),
92
			get_plugins( '/../'.basename( WPMU_PLUGIN_DIR ))
93
		);
94
		if( empty( $plugins ))return;
95
		return $this->normalizePluginList( $plugins );
96
	}
97
98
	/**
99
	 * @return void|array
100
	 */
101
	public function getMultisitePluginDetails()
102
	{
103
		if( !is_multisite() || empty( get_site_option( 'active_sitewide_plugins', [] )))return;
104
		return $this->normalizePluginList( wp_get_active_network_plugins() );
105
	}
106
107
	/**
108
	 * @return array
109
	 */
110
	public function getPhpDetails()
111
	{
112
		$displayErrors = ini_get( 'display_errors' )
113
			? 'On ('.ini_get( 'display_errors' ).')'
114
			: 'N/A';
115
		return [
116
			'cURL' => var_export( function_exists( 'curl_init' ), true ),
117
			'Default Charset' => ini_get( 'default_charset' ),
118
			'Display Errors' => $displayErrors,
119
			'fsockopen' => var_export( function_exists( 'fsockopen' ), true ),
120
			'Max Execution Time' => ini_get( 'max_execution_time' ),
121
			'Max Input Nesting Level' => ini_get( 'max_input_nesting_level' ),
122
			'Max Input Vars' => ini_get( 'max_input_vars' ),
123
			'Memory Limit' => ini_get( 'memory_limit' ),
124
			'Post Max Size' => ini_get( 'post_max_size' ),
125
			'Session Cookie Path' => esc_html( ini_get( 'session.cookie_path' )),
126
			'Session Name' => esc_html( ini_get( 'session.name' )),
127
			'Session Save Path' => esc_html( ini_get( 'session.save_path' )),
128
			'Session Use Cookies' => var_export( wp_validate_boolean( ini_get( 'session.use_cookies' )), true ),
129
			'Session Use Only Cookies' => var_export( wp_validate_boolean( ini_get( 'session.use_only_cookies' )), true ),
130
			'Upload Max Filesize' => ini_get( 'upload_max_filesize' ),
131
		];
132
	}
133
134
	/**
135
	 * @return array
136
	 */
137
	public function getServerDetails()
138
	{
139
		global $wpdb;
140
		return [
141
			'Host Name' => $this->getHostName(),
142
			'MySQL Version' => $wpdb->db_version(),
143
			'PHP Version' => PHP_VERSION,
144
			'Server Software' => filter_input( INPUT_SERVER, 'SERVER_SOFTWARE' ),
145
		];
146
	}
147
148
	/**
149
	 * @return array
150
	 */
151
	public function getSettingDetails()
152
	{
153
		$helper = glsr( Helper::class );
154
		$settings = glsr( OptionManager::class )->get( 'settings', [] );
155
		$settings = $helper->flattenArray( $settings, true );
156
		foreach( ['submissions.recaptcha.key', 'submissions.recaptcha.secret'] as $key ) {
157
			if( empty( $settings[$key] ))continue;
158
			$settings[$key] = str_repeat( '*', 10 );
159
		}
160
		$details = [];
161
		foreach( $settings as $key => $value ) {
162
			if( $helper->startsWith( 'strings', $key ) && $helper->endsWith( 'id', $key ))continue;
163
			$value = htmlspecialchars( trim( preg_replace('/\s\s+/', '\\n', $value )), ENT_QUOTES, 'UTF-8' );
164
			$details[$key] = $value;
165
		}
166
		return $details;
167
	}
168
169
	/**
170
	 * @return array
171
	 */
172
	public function getVersionDetails()
173
	{
174
		return [
175
			'Current version' => glsr()->version,
176
			'Previous version' => glsr( OptionManager::class )->get( 'version_upgraded_from' ),
177
		];
178
	}
179
180
	/**
181
	 * @return array
182
	 */
183
	public function getWordpressDetails()
184
	{
185
		global $wpdb;
186
		$theme = wp_get_theme();
187
		return [
188
			'Active Theme' => sprintf( '%s v%s', (string)$theme->Name, (string)$theme->Version ),
189
			'Home URL' => home_url(),
190
			'Language' => get_option( 'WPLANG', 'en_US' ),
191
			'Memory Limit' => WP_MEMORY_LIMIT,
192
			'Multisite' => var_export( is_multisite(), true ),
193
			'Page For Posts ID' => get_option( 'page_for_posts' ),
194
			'Page On Front ID' => get_option( 'page_on_front' ),
195
			'Permalink Structure' => get_option( 'permalink_structure', 'default' ),
196
			'Post Stati' => implode( ', ', get_post_stati() ),
197
			'Remote Post' => glsr( Cache::class )->getRemotePostTest(),
198
			'Show On Front' => get_option( 'show_on_front' ),
199
			'Site URL' => site_url(),
200
			'Timezone' => get_option( 'timezone_string' ),
201
			'Version' => get_bloginfo( 'version' ),
202
			'WP Debug' => var_export( defined( 'WP_DEBUG' ), true ),
203
			'WP Max Upload Size' => size_format( wp_max_upload_size() ),
204
			'WP Memory Limit' => WP_MEMORY_LIMIT,
205
		];
206
	}
207
208
	/**
209
	 * @return string
210
	 */
211
	protected function detectWebhostProvider()
212
	{
213
		$checks = [
214
			'.accountservergroup.com' => 'Site5',
215
			'.gridserver.com' => 'MediaTemple Grid',
216
			'.inmotionhosting.com' => 'InMotion Hosting',
217
			'.ovh.net' => 'OVH',
218
			'.pair.com' => 'pair Networks',
219
			'.stabletransit.com' => 'Rackspace Cloud',
220
			'.stratoserver.net' => 'STRATO',
221
			'.sysfix.eu' => 'SysFix.eu Power Hosting',
222
			'bluehost.com' => 'Bluehost',
223
			'DH_USER' => 'DreamHost',
224
			'Flywheel' => 'Flywheel',
225
			'ipagemysql.com' => 'iPage',
226
			'ipowermysql.com' => 'IPower',
227
			'localhost:/tmp/mysql5.sock' => 'ICDSoft',
228
			'mysqlv5' => 'NetworkSolutions',
229
			'PAGELYBIN' => 'Pagely',
230
			'secureserver.net' => 'GoDaddy',
231
			'WPE_APIKEY' => 'WP Engine',
232
		];
233
		foreach( $checks as $key => $value ) {
234
			if( !$this->isWebhostCheckValid( $key ))continue;
235
			return $value;
236
		}
237
		return implode( ',', array_filter( [DB_HOST, filter_input( INPUT_SERVER, 'SERVER_NAME' )] ));
238
	}
239
240
	/**
241
	 * @return string
242
	 */
243
	protected function getHostName()
244
	{
245
		return sprintf( '%s (%s)',
246
			$this->detectWebhostProvider(),
247
			glsr( Helper::class )->getIpAddress()
248
		);
249
	}
250
251
	/**
252
	 * @return array
253
	 */
254
	protected function getWordpressPlugins()
255
	{
256
		$plugins = get_plugins();
257
		$activePlugins = (array)get_option( 'active_plugins', [] );
258
		$inactive = $this->normalizePluginList( array_diff_key( $plugins, array_flip( $activePlugins )));
259
		$active = $this->normalizePluginList( array_diff_key( $plugins, $inactive ));
260
		return $active + $inactive;
261
	}
262
263
	/**
264
	 * @param string $title
265
	 * @return string
266
	 */
267
	protected function implode( $title, array $details )
268
	{
269
		$strings = ['['.$title.']'];
270
		$padding = max( array_map( 'strlen', array_keys( $details )) );
271
		$padding = max( [$padding, static::PAD] );
272
		foreach( $details as $key => $value ) {
273
			$strings[] = is_string( $key )
274
				? sprintf( '%s : %s', str_pad( $key.' ', $padding, '.' ), $value )
275
				: ' - '.$value;
276
		}
277
		return implode( PHP_EOL, $strings ).PHP_EOL.PHP_EOL;
278
	}
279
280
	/**
281
	 * @param string $key
282
	 * @return string
283
	 */
284
	protected function isWebhostCheckValid( $key )
285
	{
286
		return defined( $key )
0 ignored issues
show
Bug Best Practice introduced by
The expression return defined($key) || ...name(), $key) !== false returns the type boolean which is incompatible with the documented return type string.
Loading history...
287
			|| filter_input( INPUT_SERVER, $key )
288
			|| strpos( filter_input( INPUT_SERVER, 'SERVER_NAME' ), $key ) !== false
289
			|| strpos( DB_HOST, $key ) !== false
290
			|| strpos( php_uname(), $key ) !== false;
291
	}
292
293
	/**
294
	 * @return array
295
	 */
296
	protected function normalizePluginList( array $plugins )
297
	{
298
		$plugins = array_map( function( $plugin ) {
299
			return sprintf( '%s v%s', $plugin['Name'], $plugin['Version'] );
300
		}, $plugins );
301
		natcasesort( $plugins );
302
		return array_flip( $plugins );
303
	}
304
}
305