Passed
Branch monitor-2.5.x (8b654c)
by Tim
01:31
created

TestConfiguration::getAvailableApacheModulesCgi()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 12
nop 0
dl 0
loc 29
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\Modules\Monitor;
4
5
use \SimpleSAML\Modules\Monitor\DependencyInjection as DependencyInjection;
6
use \SimpleSAML_Configuration as ApplicationConfiguration;
7
use \SimpleSAML_Metadata_MetaDataStorageSource as MetaDataStorageSource;
8
	
9
10
final class TestConfiguration
11
{
12
    /**
13
     * @var ApplicationConfiguration
14
     */
15
    private $globalConfig;
16
17
    /**
18
     * @var ApplicationConfiguration
19
     */
20
    private $moduleConfig;
21
22
    /**
23
     * @var ApplicationConfiguration
24
     */
25
    private $authSourceConfig;
26
27
    /**
28
     * @var array
29
     */
30
    private $metadataConfig;
31
32
    /**
33
     * @var array
34
     */
35
    private $availableApacheModules;
36
37
    /**
38
     * @var array
39
     */
40
    private $availablePhpModules;
41
42
    /**
43
     * @var DependencyInjection
44
     */
45
    private $serverVars;
46
47
    /**
48
     * @var DependencyInjection
49
     */
50
    private $requestVars;
51
52
    /**
53
     * @param DependencyInjection $serverVars
54
     * @param DependencyInjection $requestVars
55
     */
56
    public function __construct($serverVars, $requestVars, $globalConfig, $authSourceConfig, $moduleConfig)
57
    {
58
        $this->serverVars = $serverVars;
59
        $this->requestVars = $requestVars;
60
61
        $this->setAuthsourceConfig($authSourceConfig);
62
        $this->setModuleConfig($moduleConfig);
63
        $this->setGlobalConfig($globalConfig);
64
        $this->setMetadataConfig();
65
        $this->setAvailableApacheModules();
66
        $this->setAvailablePhpModules();
67
    }
68
69
    /**
70
     * @param ApplicationConfiguration $authSourceConfig
71
     *
72
     * @return void
73
     */
74
    private function setAuthsourceConfig($authSourceConfig)
75
    {
76
        assert($authSourceConfig instanceof ApplicationConfiguration);
77
        $this->authSourceConfig = $authSourceConfig;
78
    }
79
80
    /**
81
     * @param ApplicationConfiguration $moduleConfig
82
     *
83
     * @return void
84
     */
85
    private function setModuleConfig($moduleConfig)
86
    {
87
        assert($moduleConfig instanceof ApplicationConfiguration);
88
        $this->moduleConfig = $moduleConfig;
89
    }
90
91
    /**
92
     * #param ApplicationConfiguration $globalConfig
93
     *
94
     * @return void
95
     */
96
    private function setGlobalConfig($globalConfig)
97
    {
98
        assert($globalConfig instanceof ApplicationConfiguration);
99
        $this->globalConfig = $globalConfig;
100
    }
101
102
    /**
103
     * @return void
104
     */
105
    private function setMetadataConfig()
106
    {
107
        $sets = $this->getAvailableMetadataSets();
108
        $sources = $this->globalConfig->getValue('metadata.sources');
109
        $handlers = MetaDataStorageSource::parseSources($sources);
110
        $metadata = [];
111
        if (!empty($sets)) {
112
            foreach ($handlers as $handler) {
113
                foreach ($sets as $set) {
114
                    $metadata[$set] = $handler->getMetadataSet($set);
115
                }
116
            }
117
        }
118
        assert(is_array($metadata));
119
        $this->metadataConfig = $metadata;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    protected function getAvailableMetadataSets()
126
    {
127
        $globalConfig = $this->getGlobalConfig();
128
        $sets = array();
129
        if ($globalConfig->getBoolean('enable.saml20-idp', false)) {
130
            $sets = array_merge($sets, array('saml20-idp-hosted', 'saml20-sp-remote', 'saml20-idp-remote'));
131
        }
132
        if ($globalConfig->getBoolean('enable.shib13-idp', false)) {
133
            $sets = array_merge($sets, array('shib13-idp-hosted', 'shib13-sp-hosted', 'shib13-sp-remote', 'shib13-idp-remote'));
134
        }
135
        if ($globalConfig->getBoolean('enable.adfs-idp', false)) {
136
            $sets = array_merge($sets, array('adfs-idp-hosted', 'adfs-sp-remote'));
137
        }
138
        if ($globalConfig->getBoolean('enable.wsfed-sp', false)) {
139
            $sets = array_merge($sets, array('wsfed-sp-hosted', 'wsfed-idp-remote'));
140
        }
141
        return $sets;
142
    }
143
144
    /**
145
     * @return void
146
     */
147
    private function setAvailableApacheModules()
148
    {
149
        // Determine available Apache-modules
150
        if (function_exists('apache_get_modules')) {
151
            $this->availableApacheModules = apache_get_modules();
152
        } else { // CGI-mode
153
            $this->availableApacheModules = $this->getAvailableApacheModulesCgi();
154
        }
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    private function getAvailableApacheModulesCgi()
161
    {
162
        $knownLocations = array(
163
            '/usr/sbin/httpd',
164
            '/usr/sbin/apache2',
165
            '/opt/rh/httpd24/root/usr/sbin/httpd'
166
        );
167
168
        $output = null;
169
        foreach ($knownLocations as $location) {
170
            if (file_exists($location)) {
171
                exec("$location -t -D DUMP_MODULES", $output);
172
                break;
173
            }
174
        }
175
176
        if ($output === null) {
177
            return array(); // Cannot determine available modules
178
        }
179
        array_shift($output);
180
181
        $modules = array();
182
        foreach ($output as $module) {
183
            $module = ltrim($module);
184
            if (($res = preg_replace('/(_module \((shared|static)\))/', '', $module)) !== $module) {
185
                $modules[] = 'mod_' . $res;
186
            } // else skip
187
        }
188
        return $modules;
189
    }
190
191
    /**
192
     * @return void
193
     */
194
    private function setAvailablePhpModules()
195
    {
196
        $this->availablePhpModules = array_merge(get_loaded_extensions(), get_loaded_extensions(true));
197
    }
198
199
    /**
200
     * @return array
201
     */
202
    public function getAvailableApacheModules()
203
    {
204
        return $this->availableApacheModules;
205
    }
206
207
    /**
208
     * @return array
209
     */
210
    public function getAvailablePhpModules()
211
    {
212
        return $this->availablePhpModules;
213
    }
214
215
    /**
216
     * @return DependencyInjection
217
     */
218
    public function getServerVars()
219
    {
220
        return $this->serverVars;
221
    }
222
223
    /**
224
     * @return DependencyInjection
225
     */
226
    public function getRequestVars()
227
    {
228
        return $this->requestVars;
229
    }
230
231
    /**
232
     * @return ApplicationConfiguration
233
     */
234
    public function getGlobalConfig()
235
    {
236
        assert($this->moduleConfig instanceof ApplicationConfiguration);
237
        return $this->globalConfig;
238
    }
239
240
    /**
241
     * @return ApplicationConfiguration
242
     */
243
    public function getModuleConfig()
244
    {
245
        assert($this->moduleConfig instanceof ApplicationConfiguration);
246
        return $this->moduleConfig;
247
    }
248
249
    /**
250
     * @return ApplicationConfiguration
251
     */
252
    public function getAuthSourceConfig()
253
    {
254
        assert($this->authSourceConfig instanceof ApplicationConfiguration);
255
        return $this->authSourceConfig;
256
    }
257
258
    /**
259
     * @return array
260
     */
261
    public function getMetadataConfig()
262
    {
263
        assert(is_array($this->metadataConfig));
264
        return $this->metadataConfig;
265
    }
266
}
267