Completed
Push — master ( e5b4c9...74bb7f )
by Tim
03:31 queued 01:27
created

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