Passed
Push — master ( 498725...4036bc )
by Paul
04:49
created

System::getPluginDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 10
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
			'plugin' => '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 getPluginDetails()
173
	{
174
		return [
175
			'Console size' => glsr( Console::class )->humanSize(),
176
			'Current version' => glsr()->version,
177
			'Previous version' => glsr( OptionManager::class )->get( 'version_upgraded_from' ),
178
		];
179
	}
180
181
	/**
182
	 * @return array
183
	 */
184
	public function getWordpressDetails()
185
	{
186
		global $wpdb;
187
		$theme = wp_get_theme();
188
		return [
189
			'Active Theme' => sprintf( '%s v%s', (string)$theme->Name, (string)$theme->Version ),
190
			'Home URL' => home_url(),
191
			'Language' => get_option( 'WPLANG', 'en_US' ),
192
			'Memory Limit' => WP_MEMORY_LIMIT,
193
			'Multisite' => var_export( is_multisite(), true ),
194
			'Page For Posts ID' => get_option( 'page_for_posts' ),
195
			'Page On Front ID' => get_option( 'page_on_front' ),
196
			'Permalink Structure' => get_option( 'permalink_structure', 'default' ),
197
			'Post Stati' => implode( ', ', get_post_stati() ),
198
			'Remote Post' => glsr( Cache::class )->getRemotePostTest(),
199
			'Show On Front' => get_option( 'show_on_front' ),
200
			'Site URL' => site_url(),
201
			'Timezone' => get_option( 'timezone_string' ),
202
			'Version' => get_bloginfo( 'version' ),
203
			'WP Debug' => var_export( defined( 'WP_DEBUG' ), true ),
204
			'WP Max Upload Size' => size_format( wp_max_upload_size() ),
205
			'WP Memory Limit' => WP_MEMORY_LIMIT,
206
		];
207
	}
208
209
	/**
210
	 * @return string
211
	 */
212
	protected function detectWebhostProvider()
213
	{
214
		$checks = [
215
			'.accountservergroup.com' => 'Site5',
216
			'.gridserver.com' => 'MediaTemple Grid',
217
			'.inmotionhosting.com' => 'InMotion Hosting',
218
			'.ovh.net' => 'OVH',
219
			'.pair.com' => 'pair Networks',
220
			'.stabletransit.com' => 'Rackspace Cloud',
221
			'.stratoserver.net' => 'STRATO',
222
			'.sysfix.eu' => 'SysFix.eu Power Hosting',
223
			'bluehost.com' => 'Bluehost',
224
			'DH_USER' => 'DreamHost',
225
			'Flywheel' => 'Flywheel',
226
			'ipagemysql.com' => 'iPage',
227
			'ipowermysql.com' => 'IPower',
228
			'localhost:/tmp/mysql5.sock' => 'ICDSoft',
229
			'mysqlv5' => 'NetworkSolutions',
230
			'PAGELYBIN' => 'Pagely',
231
			'secureserver.net' => 'GoDaddy',
232
			'WPE_APIKEY' => 'WP Engine',
233
		];
234
		foreach( $checks as $key => $value ) {
235
			if( !$this->isWebhostCheckValid( $key ))continue;
236
			return $value;
237
		}
238
		return implode( ',', array_filter( [DB_HOST, filter_input( INPUT_SERVER, 'SERVER_NAME' )] ));
239
	}
240
241
	/**
242
	 * @return string
243
	 */
244
	protected function getHostName()
245
	{
246
		return sprintf( '%s (%s)',
247
			$this->detectWebhostProvider(),
248
			glsr( Helper::class )->getIpAddress()
249
		);
250
	}
251
252
	/**
253
	 * @return array
254
	 */
255
	protected function getWordpressPlugins()
256
	{
257
		$plugins = get_plugins();
258
		$activePlugins = (array)get_option( 'active_plugins', [] );
259
		$inactive = $this->normalizePluginList( array_diff_key( $plugins, array_flip( $activePlugins )));
260
		$active = $this->normalizePluginList( array_diff_key( $plugins, $inactive ));
261
		return $active + $inactive;
262
	}
263
264
	/**
265
	 * @param string $title
266
	 * @return string
267
	 */
268
	protected function implode( $title, array $details )
269
	{
270
		$strings = ['['.$title.']'];
271
		$padding = max( array_map( 'strlen', array_keys( $details )) );
272
		$padding = max( [$padding, static::PAD] );
273
		foreach( $details as $key => $value ) {
274
			$strings[] = is_string( $key )
275
				? sprintf( '%s : %s', str_pad( $key.' ', $padding, '.' ), $value )
276
				: ' - '.$value;
277
		}
278
		return implode( PHP_EOL, $strings ).PHP_EOL.PHP_EOL;
279
	}
280
281
	/**
282
	 * @param string $key
283
	 * @return bool
284
	 */
285
	protected function isWebhostCheckValid( $key )
286
	{
287
		return defined( $key )
288
			|| filter_input( INPUT_SERVER, $key )
289
			|| strpos( filter_input( INPUT_SERVER, 'SERVER_NAME' ), $key ) !== false
290
			|| strpos( DB_HOST, $key ) !== false
291
			|| strpos( php_uname(), $key ) !== false;
292
	}
293
294
	/**
295
	 * @return array
296
	 */
297
	protected function normalizePluginList( array $plugins )
298
	{
299
		$plugins = array_map( function( $plugin ) {
300
			return sprintf( '%s v%s', $plugin['Name'], $plugin['Version'] );
301
		}, $plugins );
302
		natcasesort( $plugins );
303
		return array_flip( $plugins );
304
	}
305
}
306