Completed
Push — master ( 10354b...44551c )
by Devin
29:37 queued 13:08
created

system-info.php ➔ give_tools_sysinfo_get()   F

Complexity

Conditions 58
Paths > 20000

Size

Total Lines 270
Code Lines 163

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 58
eloc 163
nc 4294967295
nop 0
dl 0
loc 270
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * System Info
4
 *
5
 * These are functions
6
 *
7
 * @package     Give
8
 * @subpackage  Admin/System
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
11
 */
12
13
// Exit if accessed directly
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
19
/**
20
 * Display the system info tab
21
 *
22
 * @since       1.0
23
 * @return      void
24
 */
25
function give_system_info_callback() {
26
27
	if ( ! current_user_can( 'manage_give_settings' ) ) {
28
		return;
29
	}
30
	?>
31
	<textarea readonly="readonly" onclick="this.focus(); this.select()" id="system-info-textarea" name="give-sysinfo" title="To copy the system info, click below then press Ctrl + C (PC) or Cmd + C (Mac)."><?php echo give_tools_sysinfo_get(); ?></textarea>
32
	<p class="submit">
33
		<input type="hidden" name="give-action" value="download_sysinfo"/>
34
		<?php submit_button( 'Download System Info File', 'secondary', 'give-download-sysinfo', false ); ?>
35
	</p>
36
	<style>
37
		.give_forms_page_give-settings .give-submit-wrap {
38
			display: none; /* Hide Save settings button on System Info Tab (not needed) */
39
		}
40
	</style>
41
	<?php
42
}
43
44
45
/**
46
 * Allow Sessions for System Info Tab
47
 *
48
 * @description: In 1.3.6 we prevented sessions within wp-admin, this allows them and allows the system info to properly detect
49
 *
50
 * @since: 1.4
51
 *
52
 * @return bool
53
 */
54
function give_allow_sessions_for_sysinfo() {
55
	if ( is_admin() && ( isset( $_GET['page'] ) && isset( $_GET['tab'] ) ) && ( $_GET['tab'] == 'system_info' && $_GET['page'] == 'give-settings' ) ) {
56
		return true;
57
	}
58
}
59
60
add_filter( 'give_start_session', 'give_allow_sessions_for_sysinfo' );
61
62
63
/**
64
 * Get system info
65
 *
66
 * @since       1.0
67
 * @access      public
68
 * @global      object $wpdb Used to query the database using the WordPress Database API
69
 * @global      array $give_options Array of all Give options
70
 * @return      string $return A string containing the info to output
71
 */
72
function give_tools_sysinfo_get() {
73
	global $wpdb, $give_options;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
74
75
	if ( ! class_exists( 'Browser' ) ) {
76
		require_once GIVE_PLUGIN_DIR . 'includes/libraries/browser.php';
77
	}
78
79
	$browser = new Browser();
80
81
	// Get theme info
82
	if ( get_bloginfo( 'version' ) < '3.4' ) {
83
		$theme_data = wp_get_theme( get_stylesheet_directory() . '/style.css' );
84
		$theme      = $theme_data['Name'] . ' ' . $theme_data['Version'];
85
	} else {
86
		$theme_data = wp_get_theme();
87
		$theme      = $theme_data->Name . ' ' . $theme_data->Version;
88
	}
89
90
	// Try to identify the hosting provider
91
	$host = give_get_host();
92
93
	$return = '### Begin System Info ###' . "\n\n";
94
95
	// Start with the basics...
96
	$return .= '-- Site Info' . "\n\n";
97
	$return .= 'Site URL:                 ' . site_url() . "\n";
98
	$return .= 'Home URL:                 ' . home_url() . "\n";
99
	$return .= 'Multisite:                ' . ( is_multisite() ? 'Yes' : 'No' ) . "\n";
100
101
	$return = apply_filters( 'give_sysinfo_after_site_info', $return );
102
103
	// Can we determine the site's host?
104
	if ( $host ) {
105
		$return .= "\n" . '-- Hosting Provider' . "\n\n";
106
		$return .= 'Host:                     ' . $host . "\n";
107
108
		$return = apply_filters( 'give_sysinfo_after_host_info', $return );
109
	}
110
111
	// The local users' browser information, handled by the Browser class
112
	$return .= "\n" . '-- User Browser' . "\n\n";
113
	$return .= $browser;
114
115
	$return = apply_filters( 'give_sysinfo_after_user_browser', $return );
116
117
	// WordPress configuration
118
	$return .= "\n" . '-- WordPress Configuration' . "\n\n";
119
	$return .= 'Version:                  ' . get_bloginfo( 'version' ) . "\n";
120
	$return .= 'Language:                 ' . ( defined( 'WPLANG' ) && WPLANG ? WPLANG : 'en_US' ) . "\n";
121
	$return .= 'Permalink Structure:      ' . ( get_option( 'permalink_structure' ) ? get_option( 'permalink_structure' ) : 'Default' ) . "\n";
122
	$return .= 'Active Theme:             ' . $theme . "\n";
123
	$return .= 'Show On Front:            ' . get_option( 'show_on_front' ) . "\n";
124
125
	// Only show page specs if frontpage is set to 'page'
126
	if ( get_option( 'show_on_front' ) == 'page' ) {
127
		$front_page_id = get_option( 'page_on_front' );
128
		$blog_page_id  = get_option( 'page_for_posts' );
129
130
		$return .= 'Page On Front:            ' . ( $front_page_id != 0 ? get_the_title( $front_page_id ) . ' (#' . $front_page_id . ')' : 'Unset' ) . "\n";
131
		$return .= 'Page For Posts:           ' . ( $blog_page_id != 0 ? get_the_title( $blog_page_id ) . ' (#' . $blog_page_id . ')' : 'Unset' ) . "\n";
132
	}
133
134
	// Make sure wp_remote_post() is working
135
	$request['cmd'] = '_notify-validate';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$request was never initialized. Although not strictly required by PHP, it is generally a good practice to add $request = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
136
137
	$params = array(
138
		'sslverify'  => false,
139
		'timeout'    => 60,
140
		'user-agent' => 'Give/' . GIVE_VERSION,
141
		'body'       => $request
142
	);
143
144
	$response = wp_remote_post( 'https://www.paypal.com/cgi-bin/webscr', $params );
145
146
	if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) {
147
		$WP_REMOTE_POST = 'wp_remote_post() works';
148
	} else {
149
		$WP_REMOTE_POST = 'wp_remote_post() does not work';
150
	}
151
152
	$return .= 'Remote Post:              ' . $WP_REMOTE_POST . "\n";
153
	$return .= 'Table Prefix:             ' . 'Length: ' . strlen( $wpdb->prefix ) . '   Status: ' . ( strlen( $wpdb->prefix ) > 16 ? 'ERROR: Too long' : 'Acceptable' ) . "\n";
154
	$return .= 'Admin AJAX:               ' . ( give_test_ajax_works() ? 'Accessible' : 'Inaccessible' ) . "\n";
155
	$return .= 'WP_DEBUG:                 ' . ( defined( 'WP_DEBUG' ) ? WP_DEBUG ? 'Enabled' : 'Disabled' : 'Not set' ) . "\n";
156
	$return .= 'Memory Limit:             ' . WP_MEMORY_LIMIT . "\n";
157
	$return .= 'Registered Post Stati:    ' . implode( ', ', get_post_stati() ) . "\n";
158
159
	$return = apply_filters( 'give_sysinfo_after_wordpress_config', $return );
160
161
	// GIVE configuration
162
	$return .= "\n" . '-- Give Configuration' . "\n\n";
163
	$return .= 'Version:                  ' . GIVE_VERSION . "\n";
164
	$return .= 'Upgraded From:            ' . get_option( 'give_version_upgraded_from', 'None' ) . "\n";
165
	$return .= 'Test Mode:                ' . ( give_is_test_mode() ? "Enabled\n" : "Disabled\n" );
166
	$return .= 'Currency Code:            ' . give_get_currency() . "\n";
167
	$return .= 'Currency Position:        ' . give_get_option( 'currency_position', 'before' ) . "\n";
168
	$return .= 'Decimal Separator:        ' . give_get_option( 'decimal_separator', '.' ) . "\n";
169
	$return .= 'Thousands Separator:      ' . give_get_option( 'thousands_separator', ',' ) . "\n";
170
171
	$return = apply_filters( 'give_sysinfo_after_give_config', $return );
172
173
	// GIVE pages
174
	$return .= "\n" . '-- Give Page Configuration' . "\n\n";
175
	$return .= 'Success Page:             ' . ( ! empty( $give_options['success_page'] ) ? get_permalink( $give_options['success_page'] ) . "\n" : "Unset\n" );
176
	$return .= 'Failure Page:             ' . ( ! empty( $give_options['failure_page'] ) ? get_permalink( $give_options['failure_page'] ) . "\n" : "Unset\n" );
177
	$return .= 'Give Forms Slug:           ' . ( defined( 'GIVE_SLUG' ) ? '/' . GIVE_SLUG . "\n" : "/donations\n" );
178
179
	$return = apply_filters( 'give_sysinfo_after_give_pages', $return );
180
181
	// GIVE gateways
182
	$return .= "\n" . '-- Give Gateway Configuration' . "\n\n";
183
184
	$active_gateways = give_get_enabled_payment_gateways();
185
	if ( $active_gateways ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $active_gateways of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
186
		$default_gateway_is_active = give_is_gateway_active( give_get_default_gateway( null ) );
187
		if ( $default_gateway_is_active ) {
188
			$default_gateway = give_get_default_gateway( null );
189
			$default_gateway = $active_gateways[ $default_gateway ]['admin_label'];
190
		} else {
191
			$default_gateway = 'Test Payment';
192
		}
193
194
		$gateways = array();
195
		foreach ( $active_gateways as $gateway ) {
196
			$gateways[] = $gateway['admin_label'];
197
		}
198
199
		$return .= 'Enabled Gateways:         ' . implode( ', ', $gateways ) . "\n";
200
		$return .= 'Default Gateway:          ' . $default_gateway . "\n";
201
	} else {
202
		$return .= 'Enabled Gateways:         None' . "\n";
203
	}
204
205
	$return = apply_filters( 'give_sysinfo_after_give_gateways', $return );
206
207
	// GIVE Templates
208
	$dir = get_stylesheet_directory() . '/give_templates/*';
209
	if ( is_dir( $dir ) && ( count( glob( "$dir/*" ) ) !== 0 ) ) {
210
		$return .= "\n" . '-- Give Template Overrides' . "\n\n";
211
212
		foreach ( glob( $dir ) as $file ) {
213
			$return .= 'Filename:                 ' . basename( $file ) . "\n";
214
		}
215
216
		$return = apply_filters( 'give_sysinfo_after_give_templates', $return );
217
	}
218
219
	// Must-use plugins
220
	$muplugins = get_mu_plugins();
221
	if ( count( $muplugins > 0 ) ) {
222
		$return .= "\n" . '-- Must-Use Plugins' . "\n\n";
223
224
		foreach ( $muplugins as $plugin => $plugin_data ) {
225
			$return .= $plugin_data['Name'] . ': ' . $plugin_data['Version'] . "\n";
226
		}
227
228
		$return = apply_filters( 'give_sysinfo_after_wordpress_mu_plugins', $return );
229
	}
230
231
	// WordPress active plugins
232
	$return .= "\n" . '-- WordPress Active Plugins' . "\n\n";
233
234
	$plugins        = get_plugins();
235
	$active_plugins = get_option( 'active_plugins', array() );
236
237
	foreach ( $plugins as $plugin_path => $plugin ) {
238
		if ( ! in_array( $plugin_path, $active_plugins ) ) {
239
			continue;
240
		}
241
242
		$return .= $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
243
	}
244
245
	$return = apply_filters( 'give_sysinfo_after_wordpress_plugins', $return );
246
247
	// WordPress inactive plugins
248
	$return .= "\n" . '-- WordPress Inactive Plugins' . "\n\n";
249
250
	foreach ( $plugins as $plugin_path => $plugin ) {
251
		if ( in_array( $plugin_path, $active_plugins ) ) {
252
			continue;
253
		}
254
255
		$return .= $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
256
	}
257
258
	$return = apply_filters( 'give_sysinfo_after_wordpress_plugins_inactive', $return );
259
260
	if ( is_multisite() ) {
261
		// WordPress Multisite active plugins
262
		$return .= "\n" . '-- Network Active Plugins' . "\n\n";
263
264
		$plugins        = wp_get_active_network_plugins();
265
		$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
266
267
		foreach ( $plugins as $plugin_path ) {
268
			$plugin_base = plugin_basename( $plugin_path );
269
270
			if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
271
				continue;
272
			}
273
274
			$plugin = get_plugin_data( $plugin_path );
275
			$return .= $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
276
		}
277
278
		$return = apply_filters( 'give_sysinfo_after_wordpress_ms_plugins', $return );
279
	}
280
281
	// Server configuration (really just versioning)
282
	$return .= "\n" . '-- Webserver Configuration' . "\n\n";
283
	$return .= 'PHP Version:              ' . PHP_VERSION . "\n";
284
	$return .= 'MySQL Version:            ' . $wpdb->db_version() . "\n";
285
	$return .= 'Webserver Info:           ' . $_SERVER['SERVER_SOFTWARE'] . "\n";
286
287
	$return = apply_filters( 'give_sysinfo_after_webserver_config', $return );
288
289
	// PHP configs... now we're getting to the important stuff
290
	$return .= "\n" . '-- PHP Configuration' . "\n\n";
291
	$return .= 'Safe Mode:                ' . ( ini_get( 'safe_mode' ) ? 'Enabled' : 'Disabled' . "\n" );
292
	$return .= 'Memory Limit:             ' . ini_get( 'memory_limit' ) . "\n";
293
	$return .= 'Upload Max Size:          ' . ini_get( 'upload_max_filesize' ) . "\n";
294
	$return .= 'Post Max Size:            ' . ini_get( 'post_max_size' ) . "\n";
295
	$return .= 'Upload Max Filesize:      ' . ini_get( 'upload_max_filesize' ) . "\n";
296
	$return .= 'Time Limit:               ' . ini_get( 'max_execution_time' ) . "\n";
297
	$return .= 'Max Input Vars:           ' . ini_get( 'max_input_vars' ) . "\n";
298
	$return .= 'URL-aware fopen:          ' . ( ini_get( 'allow_url_fopen' ) ? 'On (' . ini_get( 'allow_url_fopen' ) . ')' : 'N/A' ) . "\n";
299
	$return .= 'Display Errors:           ' . ( ini_get( 'display_errors' ) ? 'On (' . ini_get( 'display_errors' ) . ')' : 'N/A' ) . "\n";
300
301
	$return = apply_filters( 'give_sysinfo_after_php_config', $return );
302
303
	// PHP extensions and such
304
	$return .= "\n" . '-- PHP Extensions' . "\n\n";
305
	$return .= 'cURL:                     ' . ( function_exists( 'curl_init' ) ? 'Supported' : 'Not Supported' ) . "\n";
306
307
	//cURL version
308
	if ( function_exists( 'curl_init' ) && function_exists( 'curl_version' ) ) {
309
		$curl_values = curl_version();
310
		$return .= 'cURL Version:             ' . $curl_values["version"] . "\n";
311
	}
312
	$return .= 'zlib:                     ' . ( function_exists( 'gzcompress' ) ? 'Supported' : 'Not Supported' ) . "\n";
313
	$return .= 'GD:                       ' . ( ( extension_loaded( 'gd' ) && function_exists( 'gd_info' ) ) ? 'Supported' : 'Not Supported' ) . "\n";
314
	$return .= 'fsockopen:                ' . ( function_exists( 'fsockopen' ) ? 'Supported' : 'Not Supported' ) . "\n";
315
	$return .= 'SOAP Client:              ' . ( class_exists( 'SoapClient' ) ? 'Installed' : 'Not Installed' ) . "\n";
316
	$return .= 'Suhosin:                  ' . ( extension_loaded( 'suhosin' ) ? 'Installed' : 'Not Installed' ) . "\n";
317
	$return .= 'DOM:                      ' . ( extension_loaded( 'dom' ) ? 'Installed' : 'Not Installed' ) . "\n";
318
	$return .= 'MBString:                 ' . ( extension_loaded( 'mbstring' ) ? 'Installed' : 'Not Installed' ) . "\n";
319
320
	$return = apply_filters( 'give_sysinfo_after_php_ext', $return );
321
322
	// Session stuff
323
	$return .= "\n" . '-- Session Configuration' . "\n\n";
324
	$return .= 'Give Use Sessions:        ' . ( defined( 'GIVE_USE_PHP_SESSIONS' ) && GIVE_USE_PHP_SESSIONS ? 'Enforced' : ( Give()->session->use_php_sessions() ? 'Enabled' : 'Disabled' ) ) . "\n";
325
	$return .= 'Session:                  ' . ( isset( $_SESSION ) ? 'Enabled' : 'Disabled' ) . "\n";
326
327
	// The rest of this is only relevant is session is enabled
328
	if ( isset( $_SESSION ) ) {
329
		$return .= 'Session Name:             ' . esc_html( ini_get( 'session.name' ) ) . "\n";
330
		$return .= 'Cookie Path:              ' . esc_html( ini_get( 'session.cookie_path' ) ) . "\n";
331
		$return .= 'Save Path:                ' . esc_html( ini_get( 'session.save_path' ) ) . "\n";
332
		$return .= 'Use Cookies:              ' . ( ini_get( 'session.use_cookies' ) ? 'On' : 'Off' ) . "\n";
333
		$return .= 'Use Only Cookies:         ' . ( ini_get( 'session.use_only_cookies' ) ? 'On' : 'Off' ) . "\n";
334
	}
335
336
	$return = apply_filters( 'give_sysinfo_after_session_config', $return );
337
338
	$return .= "\n" . '### End System Info ###';
339
340
	return $return;
341
}
342
343
344
/**
345
 * Generates a System Info download file
346
 *
347
 * @since       1.0
348
 * @return      void
349
 */
350
function give_tools_sysinfo_download() {
351
352
	if ( ! current_user_can( 'manage_give_settings' ) ) {
353
		return;
354
	}
355
356
	nocache_headers();
357
358
	header( 'Content-Type: text/plain' );
359
	header( 'Content-Disposition: attachment; filename="give-system-info.txt"' );
360
361
	echo wp_strip_all_tags( $_POST['give-sysinfo'] );
362
	give_die();
363
}
364
365
add_action( 'give_download_sysinfo', 'give_tools_sysinfo_download' );