Test Failed
Push — master ( a95317...fac2f0 )
by Paul
03:50
created

System   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 285
ccs 0
cts 213
cp 0
rs 8.8
c 0
b 0
f 0
wmc 36

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getBrowserDetails() 0 9 1
A getVersionDetails() 0 5 1
A getWordpressDetails() 0 22 1
A getMuPluginDetails() 0 8 2
A getServerDetails() 0 8 1
A __toString() 0 3 1
A getHostName() 0 5 1
B getSettingDetails() 0 16 6
A getWordpressPlugins() 0 7 1
A get() 0 22 3
A implode() 0 11 3
A getPhpDetails() 0 21 2
C detectWebhostProvider() 0 33 7
A getInactivePluginDetails() 0 4 1
A getActivePluginDetails() 0 6 1
A getMultisitePluginDetails() 0 4 3
A normalizePluginList() 0 7 1
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 );
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( 'translations', $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
		$serverName = filter_input( INPUT_SERVER, 'SERVER_NAME' );
234
		foreach( $checks as $key => $value ) {
235
			if( defined( $key )
236
				|| filter_input( INPUT_SERVER, $key )
237
				|| strpos( $serverName, $key ) !== false
238
				|| strpos( DB_HOST, $key ) !== false
239
				|| strpos( php_uname(), $key ) !== false ) {
240
				return $value;
241
			}
242
		}
243
		return implode( ',', array_filter( [DB_HOST, $serverName] ));
244
	}
245
246
	/**
247
	 * @return string
248
	 */
249
	protected function getHostName()
250
	{
251
		return sprintf( '%s (%s)',
252
			$this->detectWebhostProvider(),
253
			glsr( Helper::class )->getIpAddress()
254
		);
255
	}
256
257
	/**
258
	 * @return array
259
	 */
260
	protected function getWordpressPlugins()
261
	{
262
		$plugins = get_plugins();
263
		$activePlugins = (array)get_option( 'active_plugins', [] );
264
		$inactive = $this->normalizePluginList( array_diff_key( $plugins, array_flip( $activePlugins )));
265
		$active = $this->normalizePluginList( array_diff_key( $plugins, $inactive ));
266
		return $active + $inactive;
267
	}
268
269
	/**
270
	 * @param string $title
271
	 * @return string
272
	 */
273
	protected function implode( $title, array $details )
274
	{
275
		$strings = ['['.$title.']'];
276
		$padding = max( array_map( 'strlen', array_keys( $details )) );
277
		$padding = max( [$padding, static::PAD] );
278
		foreach( $details as $key => $value ) {
279
			$strings[] = is_string( $key )
280
				? sprintf( '%s : %s', str_pad( $key.' ', $padding, '.' ), $value )
281
				: ' - '.$value;
282
		}
283
		return implode( PHP_EOL, $strings ).PHP_EOL.PHP_EOL;
284
	}
285
286
	/**
287
	 * @return array
288
	 */
289
	protected function normalizePluginList( array $plugins )
290
	{
291
		$plugins = array_map( function( $plugin ) {
292
			return sprintf( '%s v%s', $plugin['Name'], $plugin['Version'] );
293
		}, $plugins );
294
		natcasesort( $plugins );
295
		return array_flip( $plugins );
296
	}
297
}
298