/
ApacheDetector.php
136 lines (123 loc) · 3.32 KB
/
ApacheDetector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Concrete\Core\Service\Detector\HTTP;
use Concrete\Core\Http\Request;
use Concrete\Core\Service\Detector\DetectorInterface;
class ApacheDetector implements DetectorInterface
{
/**
* @var \Concrete\Core\Http\Request
*/
protected $request;
/**
* Class constructor.
*
* @param \Concrete\Core\Http\Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Service\Detector\DetectorInterface::detect()
*/
public function detect()
{
$result = null;
if (($result === null || $result === '') && $this->request->server->has('SERVER_SOFTWARE')) {
$version = $this->detectFromServer($this->request->server->get('SERVER_SOFTWARE'));
if ($version !== null) {
$result = $version;
}
}
if (($result === null || $result === '') && function_exists('apache_get_version')) {
$version = $this->detectFromSPL(@apache_get_version());
if ($version !== null) {
$result = $version;
}
}
if ($result === null || $result === '') {
ob_start();
phpinfo(INFO_MODULES);
$info = ob_get_contents();
ob_end_clean();
$result = $this->detectFromPHPInfo($info);
}
if (($result === null || $result === '')) {
$version = $this->detectFromSapiName(PHP_SAPI);
if ($version !== null) {
$result = $version;
}
}
return $result;
}
/**
* Detect from the SERVER_SOFTWARE key of the superglobal server array.
*
* @param string $value
*
* @return null|string
*/
private function detectFromServer($value)
{
$result = null;
if (is_string($value)) {
if (preg_match('/\bApache\/(\d+(\.\d+)+)/i', $value, $m)) {
$result = $m[1];
} elseif ($value === 'Apache') {
$result = '';
}
}
return $result;
}
/**
* Detect using the result of the SPL apache_get_version().
*
* @param string $value
*
* @return null|string
*/
private function detectFromSPL($value)
{
$result = null;
if (is_string($value)) {
if (preg_match('/\bApache\/(\d+(\.\d+)+)/i', $value, $m)) {
$result = $m[1];
} elseif ($value === 'Apache') {
$result = '';
}
}
return $result;
}
/**
* Detect using PHPInfo.
*
* @param string $value
*
* @return null|string
*/
private function detectFromPHPInfo($value)
{
$result = null;
if (is_string($value) && preg_match('/\bApache\/(\d+(\.\d+)+)/i', $value, $m)) {
$result = $m[1];
}
return $result;
}
/**
* Detect using PHP_SAPI/php_sapi_name.
*
* @param string $value
*
* @return null|string
*/
private function detectFromSapiName($sapiName)
{
$result = null;
if (is_string($sapiName) && preg_match('/^apache(\d)handler$/', $sapiName, $m)) {
$result = $m[1];
}
return $result;
}
}