GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#936)
by Tom
02:44
created

Backup_Utilities::is_exec_available()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
rs 8.439
cc 6
eloc 11
nc 5
nop 0
1
<?php
2
3
namespace HM\BackUpWordPress;
4
5
/**
6
 * A set of Backup Utility functions
7
 */
8
class Backup_Utilities {
9
10
	/**
11
	 * Checks whether Safe Mode is currently on
12
	 *
13
	 * @param  string  $ini_get_callback By default we use `ini_get` to check for
14
	 *                                   the Safe Mode setting but this can be
15
	 *                                   overridden for testing purposes.
16
	 *
17
	 * @return boolean                   Whether Safe Mode is on or off.
18
	 */
19
	public static function is_safe_mode_on( $ini_get_callback = 'ini_get' ) {
20
21
		$safe_mode = @call_user_func( $ini_get_callback, 'safe_mode' );
22
23
		if ( $safe_mode && strtolower( $safe_mode ) !== 'off' ) {
1 ignored issue
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $safe_mode && str...($safe_mode) !== 'off';.
Loading history...
24
			return true;
25
		}
26
27
		return false;
28
29
	}
30
31
	/**
32
	 * Check whether it's possible to use `exec`.
33
	 *
34
	 * @return boolean [description]
35
	 */
36
	public static function is_exec_available() {
37
38
		// You can't use exec if Safe Mode is on.
39
		if ( self::is_safe_mode_on() ) {
40
			return false;
41
		}
42
43
		// Check if exec is specifically disabled
44
		if ( self::is_function_disabled( 'exec' ) ) {
45
			return false;
46
		}
47
48
		// Some servers seem to disable escapeshellcmd / escapeshellarg separately to exec, in
49
		// that instance we don't want to use exec as it's insecure
50
		if ( self::is_function_disabled( 'escapeshellcmd' ) || self::is_function_disabled( 'escapeshellarg' ) ) {
51
			return false;
52
		}
53
54
		// Can we issue a simple echo command?
55
		exec( 'echo backupwordpress', $output, $return );
56
57
		if ( $return !== 0 ) {
1 ignored issue
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($return !== 0);.
Loading history...
58
			return false;
59
		}
60
61
		return true;
62
63
	}
64
65
	/**
66
	 * Check whether a PHP function has been disabled.
67
	 *
68
	 * @param  string  $function         The function you want to test for.
69
	 * @param  string  $ini_get_callback By default we check with ini_get, but
70
	 *                                   it's possible to overridde this for
71
	 *                                   testing purposes.
72
	 *
73
	 * @return boolean                   Whether the function is disabled or not.
74
	 */
75
	public static function is_function_disabled( $function, $ini_get_callback = 'ini_get' ) {
76
77
		// Suhosin stores it's disabled functions in `suhosin.executor.func.blacklist`
78
		$suhosin_blacklist = array_map( 'trim', explode( ',', @call_user_func( $ini_get_callback, 'suhosin.executor.func.blacklist' ) ) );
79
80
		// PHP supports disabling functions by adding them to `disable_functions` in php.ini.
81
		$disabled_functions = array_map( 'trim', explode( ',', @call_user_func( $ini_get_callback, 'disable_functions' ) ) );
82
83
		if ( in_array( $function, array_merge( $suhosin_blacklist, $disabled_functions ) ) ) {
1 ignored issue
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return in_array($functio... $disabled_functions));.
Loading history...
84
			return true;
85
		}
86
87
		return false;
88
89
	}
90
91
	/**
92
	 * Attempt to work out path to a cli executable.
93
	 *
94
	 * @param  array $paths An array of paths to check against.
95
	 *
96
	 * @return string|false        The path to the executable.
97
	 */
98
	public static function get_executable_path( $paths ) {
99
100
		if ( ! self::is_exec_available() ) {
101
			return false;
102
		}
103
104
		$paths = array_map( 'wp_normalize_path', $paths );
105
106
		foreach ( $paths as $path ) {
107
108
			$output = $result = 0;
109
110
			/**
111
			 * Attempt to call `--version` on each path, the one which works
112
			 * must be the correct path.
113
			 *
114
			 * We pipe STDERR to /dev/null so we don't leak errors.
115
			 */
116
			exec( escapeshellarg( $path ) . ' --version ' . ignore_stderr(), $output, $result );
117
118
			// If the command executed successfully then this must be the correct path
119
			if ( $result === 0 ) {
120
				return $path;
121
			}
122
123
		}
124
125
		return false;
126
127
	}
128
129
}
130