Passed
Push — monitor-3.0.x ( f85cdc...51d4ea )
by Tim
03:29
created

TestConfiguration::getServerVars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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