Passed
Push — master ( 6f6d9e...e32a3c )
by Paul
04:05
created

System::getReviewsDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 9
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
			'reviews' => 'Review Counts',
40
		];
41
		$systemInfo = array_reduce( array_keys( $details ), function( $carry, $key ) use( $details ) {
42
			$methodName = glsr( Helper::class )->buildMethodName( 'get-'.$key.'-details' );
43
			if( method_exists( $this, $methodName ) && $systemDetails = $this->$methodName() ) {
44
				return $carry.$this->implode( $details[$key], $systemDetails );
45
			}
46
			return $carry;
47
		});
48
		return trim( $systemInfo );
49
	}
50
51
	/**
52
	 * @return array
53
	 */
54
	public function getActivePluginDetails()
55
	{
56
		$plugins = get_plugins();
57
		$activePlugins = (array)get_option( 'active_plugins', [] );
58
		$inactive = array_diff_key( $plugins, array_flip( $activePlugins ));
59
		return $this->normalizePluginList( array_diff_key( $plugins, $inactive ));
60
	}
61
62
	/**
63
	 * @return array
64
	 */
65
	public function getBrowserDetails()
66
	{
67
		$browser = new Browser;
68
		$name = esc_attr( $browser->getName() );
69
		$userAgent = esc_attr( $browser->getUserAgent()->getUserAgentString() );
70
		$version = esc_attr( $browser->getVersion() );
71
		return [
72
			'Browser Name' => sprintf( '%s %s', $name, $version ),
73
			'Browser UA' => $userAgent,
74
		];
75
	}
76
77
	/**
78
	 * @return array
79
	 */
80
	public function getInactivePluginDetails()
81
	{
82
		$activePlugins = (array)get_option( 'active_plugins', [] );
83
		return $this->normalizePluginList( array_diff_key( get_plugins(), array_flip( $activePlugins )));
84
	}
85
86
	/**
87
	 * @return void|array
88
	 */
89
	public function getMuPluginDetails()
90
	{
91
		$plugins = array_merge(
92
			get_mu_plugins(),
93
			get_plugins( '/../'.basename( WPMU_PLUGIN_DIR ))
94
		);
95
		if( empty( $plugins ))return;
96
		return $this->normalizePluginList( $plugins );
97
	}
98
99
	/**
100
	 * @return void|array
101
	 */
102
	public function getMultisitePluginDetails()
103
	{
104
		if( !is_multisite() || empty( get_site_option( 'active_sitewide_plugins', [] )))return;
105
		return $this->normalizePluginList( wp_get_active_network_plugins() );
106
	}
107
108
	/**
109
	 * @return array
110
	 */
111
	public function getPhpDetails()
112
	{
113
		$displayErrors = ini_get( 'display_errors' )
114
			? 'On ('.ini_get( 'display_errors' ).')'
115
			: 'N/A';
116
		$intlSupport = extension_loaded( 'intl' )
117
			? phpversion( 'intl' )
118
			: 'false';
119
		return [
120
			'cURL' => var_export( function_exists( 'curl_init' ), true ),
121
			'Default Charset' => ini_get( 'default_charset' ),
122
			'Display Errors' => $displayErrors,
123
			'fsockopen' => var_export( function_exists( 'fsockopen' ), true ),
124
			'Intl' => $intlSupport,
125
			'Max Execution Time' => ini_get( 'max_execution_time' ),
126
			'Max Input Nesting Level' => ini_get( 'max_input_nesting_level' ),
127
			'Max Input Vars' => ini_get( 'max_input_vars' ),
128
			'Memory Limit' => ini_get( 'memory_limit' ),
129
			'Post Max Size' => ini_get( 'post_max_size' ),
130
			'Session Cookie Path' => esc_html( ini_get( 'session.cookie_path' )),
131
			'Session Name' => esc_html( ini_get( 'session.name' )),
132
			'Session Save Path' => esc_html( ini_get( 'session.save_path' )),
133
			'Session Use Cookies' => var_export( wp_validate_boolean( ini_get( 'session.use_cookies' )), true ),
134
			'Session Use Only Cookies' => var_export( wp_validate_boolean( ini_get( 'session.use_only_cookies' )), true ),
135
			'Upload Max Filesize' => ini_get( 'upload_max_filesize' ),
136
		];
137
	}
138
139
	/**
140
	 * @return array
141
	 */
142
	public function getReviewsDetails()
143
	{
144
		$counts = glsr( OptionManager::class )->get( 'counts', [] );
145
		$counts = glsr( Helper::class )->flattenArray( $counts );
146
		array_walk( $counts, function( &$ratings ) {
147
			$ratings = array_sum( $ratings ).' ('.implode( ', ', $ratings ).')';
148
		});
149
		ksort( $counts );
150
		return $counts;
151
	}
152
153
	/**
154
	 * @return array
155
	 */
156
	public function getServerDetails()
157
	{
158
		global $wpdb;
159
		return [
160
			'Host Name' => $this->getHostName(),
161
			'MySQL Version' => $wpdb->db_version(),
162
			'PHP Version' => PHP_VERSION,
163
			'Server Software' => filter_input( INPUT_SERVER, 'SERVER_SOFTWARE' ),
164
		];
165
	}
166
167
	/**
168
	 * @return array
169
	 */
170
	public function getSettingDetails()
171
	{
172
		$helper = glsr( Helper::class );
173
		$settings = glsr( OptionManager::class )->get( 'settings', [] );
174
		$settings = $helper->flattenArray( $settings, true );
175
		$settings = $this->purgeSettings( $settings );
176
		ksort( $settings );
177
		$details = [];
178
		foreach( $settings as $key => $value ) {
179
			if( $helper->startsWith( 'strings', $key ) && $helper->endsWith( 'id', $key ))continue;
180
			$value = htmlspecialchars( trim( preg_replace('/\s\s+/', '\\n', $value )), ENT_QUOTES, 'UTF-8' );
181
			$details[$key] = $value;
182
		}
183
		return $details;
184
	}
185
186
	/**
187
	 * @return array
188
	 */
189
	public function getPluginDetails()
190
	{
191
		return [
192
			'Console size' => glsr( Console::class )->humanSize( '0' ),
193
			'Current version' => glsr()->version,
194
			'Previous version' => glsr( OptionManager::class )->get( 'version_upgraded_from' ),
195
		];
196
	}
197
198
	/**
199
	 * @return array
200
	 */
201
	public function getWordpressDetails()
202
	{
203
		global $wpdb;
204
		$theme = wp_get_theme();
205
		return [
206
			'Active Theme' => sprintf( '%s v%s', (string)$theme->Name, (string)$theme->Version ),
207
			'Home URL' => home_url(),
208
			'Language' => get_locale(),
209
			'Memory Limit' => WP_MEMORY_LIMIT,
210
			'Multisite' => var_export( is_multisite(), true ),
211
			'Page For Posts ID' => get_option( 'page_for_posts' ),
212
			'Page On Front ID' => get_option( 'page_on_front' ),
213
			'Permalink Structure' => get_option( 'permalink_structure', 'default' ),
214
			'Post Stati' => implode( ', ', get_post_stati() ),
215
			'Remote Post' => glsr( Cache::class )->getRemotePostTest(),
216
			'Show On Front' => get_option( 'show_on_front' ),
217
			'Site URL' => site_url(),
218
			'Timezone' => get_option( 'timezone_string' ),
219
			'Version' => get_bloginfo( 'version' ),
220
			'WP Debug' => var_export( defined( 'WP_DEBUG' ), true ),
221
			'WP Max Upload Size' => size_format( wp_max_upload_size() ),
222
			'WP Memory Limit' => WP_MEMORY_LIMIT,
223
		];
224
	}
225
226
	/**
227
	 * @return string
228
	 */
229
	protected function detectWebhostProvider()
230
	{
231
		$checks = [
232
			'.accountservergroup.com' => 'Site5',
233
			'.gridserver.com' => 'MediaTemple Grid',
234
			'.inmotionhosting.com' => 'InMotion Hosting',
235
			'.ovh.net' => 'OVH',
236
			'.pair.com' => 'pair Networks',
237
			'.stabletransit.com' => 'Rackspace Cloud',
238
			'.stratoserver.net' => 'STRATO',
239
			'.sysfix.eu' => 'SysFix.eu Power Hosting',
240
			'bluehost.com' => 'Bluehost',
241
			'DH_USER' => 'DreamHost',
242
			'Flywheel' => 'Flywheel',
243
			'ipagemysql.com' => 'iPage',
244
			'ipowermysql.com' => 'IPower',
245
			'localhost:/tmp/mysql5.sock' => 'ICDSoft',
246
			'mysqlv5' => 'NetworkSolutions',
247
			'PAGELYBIN' => 'Pagely',
248
			'secureserver.net' => 'GoDaddy',
249
			'WPE_APIKEY' => 'WP Engine',
250
		];
251
		foreach( $checks as $key => $value ) {
252
			if( !$this->isWebhostCheckValid( $key ))continue;
253
			return $value;
254
		}
255
		return implode( ',', array_filter( [DB_HOST, filter_input( INPUT_SERVER, 'SERVER_NAME' )] ));
256
	}
257
258
	/**
259
	 * @return string
260
	 */
261
	protected function getHostName()
262
	{
263
		return sprintf( '%s (%s)',
264
			$this->detectWebhostProvider(),
265
			glsr( Helper::class )->getIpAddress()
266
		);
267
	}
268
269
	/**
270
	 * @return array
271
	 */
272
	protected function getWordpressPlugins()
273
	{
274
		$plugins = get_plugins();
275
		$activePlugins = (array)get_option( 'active_plugins', [] );
276
		$inactive = $this->normalizePluginList( array_diff_key( $plugins, array_flip( $activePlugins )));
277
		$active = $this->normalizePluginList( array_diff_key( $plugins, $inactive ));
278
		return $active + $inactive;
279
	}
280
281
	/**
282
	 * @param string $title
283
	 * @return string
284
	 */
285
	protected function implode( $title, array $details )
286
	{
287
		$strings = ['['.$title.']'];
288
		$padding = max( array_map( 'strlen', array_keys( $details )) );
289
		$padding = max( [$padding, static::PAD] );
290
		foreach( $details as $key => $value ) {
291
			$strings[] = is_string( $key )
292
				? sprintf( '%s : %s', str_pad( $key.' ', $padding, '.' ), $value )
293
				: ' - '.$value;
294
		}
295
		return implode( PHP_EOL, $strings ).PHP_EOL.PHP_EOL;
296
	}
297
298
	/**
299
	 * @param string $key
300
	 * @return bool
301
	 */
302
	protected function isWebhostCheckValid( $key )
303
	{
304
		return defined( $key )
305
			|| filter_input( INPUT_SERVER, $key )
306
			|| strpos( filter_input( INPUT_SERVER, 'SERVER_NAME' ), $key ) !== false
307
			|| strpos( DB_HOST, $key ) !== false
308
			|| strpos( php_uname(), $key ) !== false;
309
	}
310
311
	/**
312
	 * @return array
313
	 */
314
	protected function normalizePluginList( array $plugins )
315
	{
316
		$plugins = array_map( function( $plugin ) {
317
			return sprintf( '%s v%s', $plugin['Name'], $plugin['Version'] );
318
		}, $plugins );
319
		natcasesort( $plugins );
320
		return array_flip( $plugins );
321
	}
322
323
	/**
324
	 * @return array
325
	 */
326
	protected function purgeSettings( array $settings )
327
	{
328
		$keys = [
329
			'licenses.', 'submissions.recaptcha.key', 'submissions.recaptcha.secret',
330
		];
331
		array_walk( $settings, function( &$value, $setting ) use( $keys ) {
332
			foreach( $keys as $key ) {
333
				if( !glsr( Helper::class )->startsWith( $key, $setting ) || empty( $value ))continue;
334
				$value = str_repeat( '•', 13 );
335
				return;
336
			}
337
		});
338
		return $settings;
339
	}
340
}
341