Completed
Push — master ( e0fb57...7519fb )
by Tim
09:19 queued 07:48
created

TestConfiguration::setModuleConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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