1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of tenside/core. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Schiffler <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* This project is provided in good faith and hope to be usable by anyone. |
12
|
|
|
* |
13
|
|
|
* @package tenside/core |
14
|
|
|
* @author Christian Schiffler <[email protected]> |
15
|
|
|
* @copyright 2015 Christian Schiffler <[email protected]> |
16
|
|
|
* @license https://github.com/tenside/core/blob/master/LICENSE MIT |
17
|
|
|
* @link https://github.com/tenside/core |
18
|
|
|
* @filesource |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Tenside\Core\Util; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class provides methods to check if certain functions have been disabled in PHP or are callable. |
25
|
|
|
*/ |
26
|
|
|
class FunctionAvailabilityCheck |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Cache. |
30
|
|
|
* |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
private static $blackListSuhosin; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Cache. |
37
|
|
|
* |
38
|
|
|
* @var string[] |
39
|
|
|
*/ |
40
|
|
|
private static $blackListPhpIni; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if function is defined. |
45
|
|
|
* |
46
|
|
|
* @param string $function The function to test. |
47
|
|
|
* |
48
|
|
|
* @param string|null $extension The optional name of an php extension providing said function. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public static function isFunctionEnabled($function, $extension = null) |
53
|
|
|
{ |
54
|
|
|
return |
55
|
|
|
(null === $extension || extension_loaded($extension)) |
56
|
|
|
&& !static::isFunctionBlacklistedInPhpIni($function) |
57
|
|
|
&& !static::isFunctionBlacklistedInSuhosin($function) |
58
|
|
|
&& static::isFunctionDefined($function); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if function is defined. |
63
|
|
|
* |
64
|
|
|
* @param string $function The function to test. |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public static function isFunctionDefined($function) |
69
|
|
|
{ |
70
|
|
|
return function_exists($function); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if function is blacklisted in Suhosin. |
75
|
|
|
* |
76
|
|
|
* @param string $function The function to test. |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public static function isFunctionBlacklistedInSuhosin($function) |
81
|
|
|
{ |
82
|
|
|
if (!extension_loaded('suhosin')) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!isset(self::$blackListSuhosin)) { |
87
|
|
|
self::$blackListSuhosin = static::prepareList(ini_get('suhosin.executor.func.blacklist')); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return static::isFunctionsMentionedInList($function, self::$blackListSuhosin); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if method is blacklisted in Suhosin. |
95
|
|
|
* |
96
|
|
|
* @param string $function The function to test. |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public static function isFunctionBlacklistedInPhpIni($function) |
101
|
|
|
{ |
102
|
|
|
if (!isset(self::$blackListPhpIni)) { |
103
|
|
|
self::$blackListPhpIni = static::prepareList(ini_get('disable_functions')); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return static::isFunctionsMentionedInList($function, self::$blackListPhpIni); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check if a function is mentioned in the passed (comma separated) list. |
111
|
|
|
* |
112
|
|
|
* @param string $function The function to test. |
113
|
|
|
* |
114
|
|
|
* @param string[] $list The function list. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public static function isFunctionsMentionedInList($function, $list) |
119
|
|
|
{ |
120
|
|
|
if (empty($list)) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return (false !== array_search($function, $list)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Explode a list. |
129
|
|
|
* |
130
|
|
|
* @param string $list The list. |
131
|
|
|
* |
132
|
|
|
* @return string[] |
133
|
|
|
*/ |
134
|
|
|
private static function prepareList($list) |
135
|
|
|
{ |
136
|
|
|
return array_map('strtolower', array_map('trim', explode(',', $list, -1))); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: