Completed
Push — master ( 12a73b...fbc3da )
by Tim
04:37 queued 03:16
created

sspmod_monitor_TestSuite_Modules::setRequiredApacheModules()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 8
nop 0
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 43 and the first side effect is on line 7.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace SimpleSAML\Module\monitor\TestSuite;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
7
final class Modules extends \SimpleSAML\Module\monitor\TestSuite
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
8
{
9
    private $requiredApacheModules = array();
10
    private $requiredPhpModules = array();
11
12
    // Important!!  Modules-names are handled case-sensitive!!
13
    private $storeApacheDependencies = array();
14
    private $storePhpDependencies = array(
15
        'memcache' => 'memcached|memcache',
16
        'phpsession' => 'session',
17
        'redis' => 'redis',
18
        'redissentinel' => 'redis',
19
        'riak:Store' => 'riak',
20
        'sql' => 'PDO'
21
    );
22
23
    private $moduleApacheDependencies = array(
24
        'negotiateext' => 'mod_auth_kerb|mod_auth_gssapi'
25
    );
26
    private $modulePhpDependencies = array(
27
        'authfacebook' => array('curl', 'json'),
28
        'authYubiKey' => 'curl',
29
// TODO: consent only requires pdo when database backend is used.. Should probably add this to required-list when processing metadata
30
//        'consent' => 'PDO',
31
        'consentAdmin' => 'PDO',
32
        'ldap' => 'ldap',
33
        'memcacheMonitor' => 'memcached|memcache',
34
        'negotiate' => 'krb5',
35
        'radius' => 'radius',
36
        'riak' => 'riak',
37
        'sqlauth' => 'PDO'
38
    );
39
40
    /*
41
     * @return void
42
     */
43
    protected function initialize()
44
    {
45
        $this->setRequiredModules();
46
    }
47
48
    /*
49
     * @return void
50
     */
51
    private function addRequiredApacheModule($module)
52
    {
53
        if (!in_array($module, $this->requiredApacheModules)) {
54
            $this->requiredApacheModules[] = $module;
55
        }
56
    }
57
58
    /*
59
     * @return void
60
     */
61
    private function addRequiredPhpModule($module)
62
    {
63
        if (!in_array($module, $this->requiredPhpModules)) {
64
            $this->requiredPhpModules[] = $module;
65
        }
66
    }
67
68
    /*
69
     * @return void
70
     */
71
    private function setRequiredModules()
72
    {
73
        $this->setRequiredApacheModules();
74
        $this->setRequiredPhpModules();
75
        $this->setRequiredSspModules();
76
    }
77
78
    /*
79
     * @return void
80
     */
81
    private function setRequiredApacheModules()
82
    {
83
        // Apache Modules
84
        if (\SimpleSAML\Utils\HTTP::isHTTPS()) {
85
            $this->addRequiredApacheModule('mod_ssl');
86
        }
87
        if (function_exists('apache_get_modules')) {
88
            $this->addRequiredApacheModule('mod_php|mod_php5');
89
        }
90
91
        // Determine extra required modules
92
        $monitor = $this->getMonitor();
93
        $globalConfig = $monitor->getGlobalConfig();
94
        $store = $globalConfig->getValue('store.type');
95
        if (array_key_exists($store, $this->storeApacheDependencies)) {
96
            $this->addRequiredApacheModule($this->storeApacheDependencies[$store]);
97
        }
98
    }
99
100
    /*
101
     * @return void
102
     */
103
    private function setRequiredPhpModules()
104
    {
105
        // PHP modules
106
        $composerFile = \SimpleSAML\Utils\System::resolvePath('composer.json');
107
        $composerData = file_get_contents($composerFile);
108
        $composer = json_decode($composerData, true);
109
        $composerRequired = $composer['require'];
110
111
        foreach ($composerRequired as $ext => $ver) {
112
            if (preg_match('/^ext-/', $ext)) {
113
                $this->addRequiredPhpModule(substr($ext, 4));
114
            }
115
        }
116
117
        // Determine extra required modules
118
        $monitor = $this->getMonitor();
119
        $globalConfig = $monitor->getGlobalConfig();
120
        $store = $globalConfig->getValue('store.type');
121
        if (array_key_exists($store, $this->storePhpDependencies)) {
122
            $this->addRequiredPhpModule($this->storePhpDependencies[$store]);
123
        }
124
    }
125
126
    /*
127
     * @return void
128
     */
129
    private function setRequiredSspModules()
130
    {
131
        $modules = \SimpleSAML\Module::getModules();
132
        foreach ($modules as $module) {
133
            if (\SimpleSAML\Module::isModuleEnabled($module)) {
134
                if (array_key_exists($module, $this->moduleApacheDependencies)) {
135
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->moduleApacheDependencies[$module]);
136
                    foreach ($dependencies as $dependency) {
137
                        $this->addRequiredApacheModule($dependency);
138
                    }
139
                }
140
                if (array_key_exists($module, $this->modulePhpDependencies)) {
141
                    $dependencies = \SimpleSAML\Utils\Arrays::Arrayize($this->modulePhpDependencies[$module]);
142
                    foreach ($dependencies as $dependency) {
143
                        $this->addRequiredPhpModule($dependency);
144
                    }
145
                }
146
            }
147
        }
148
    }
149
150
    /*
151
     * @return array
152
     */
153
    public function getAvailableApacheModules()
154
    {
155
        $monitor = $this->getMonitor();
156
        return $monitor->getAvailableApacheModules();
157
    }
158
159
    /*
160
     * @return array
161
     */
162
    public function getAvailablePhpModules()
163
    {
164
        $monitor = $this->getMonitor();
165
        return $monitor->getAvailablePhpModules();
166
    }
167
168
    /*
169
     * @return array<string,array>
170
     */
171
    private function getRequiredModules()
172
    {
173
        return array('Apache' => $this->requiredApacheModules, 'Php' => $this->requiredPhpModules);
174
    }
175
176
    /*
177
     * @return array<string,array>
178
     */
179
    private function getModuleDependencies()
180
    {
181
        return array('Apache' => $this->moduleApacheDependencies, 'Php' => $this->modulePhpDependencies);
182
    }
183
184
    /*
185
     * @return void
186
     */
187
    protected function invokeTestSuite()
188
    {
189
        $monitor = $this->getMonitor();
190
        $availableModules = array('Apache' => $monitor->getAvailableApacheModules(), 'Php' => $monitor->getAvailablePhpModules());
191
192
        $requiredModules = $this->getRequiredModules();
193
        $moduleDependencies = $this->getModuleDependencies();
194
        $output = array();
195
196
        // Test for the availability of required modules
197
        foreach ($availableModules as $category => $available) {
198
            if (is_null($available)) {
199
                $output[$category][] = array(State::SKIPPED, $category, implode(', ', $requiredModules[$category]), 'Unable to verify installed modules');
200
            } else {
201
                $class = '\SimpleSAML\Module\monitor\TestCase\Module\' . $category;
202
                $dependencies = array_key_exists($category, $moduleDependencies) ? $moduleDependencies[$category] : array();
203
                $required = array_key_exists($category, $requiredModules) ? $requiredModules[$category] : array();
204
205
                foreach ($required as $require) {
206
                    $test = new $class($this, array('module' => $require));
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING
Loading history...
207
                    $this->addTest($test);
208
209
                    $state = $test->getState();
210
                    if ($state !== State::OK) {
211
                        $missing = array();
212
                        while ($dependency = array_search($require, $dependencies)) {
213
                            if (\SimpleSAML\Module::isModuleEnabled($dependency)) {
214
                                $missing[] = $dependency;
215
                            }
216
                            unset($dependencies[$dependency]);
217
                        }
218
219
                        if (!empty($missing)) {
220
                            $output[$category][] = array($state, $category, $test->getModule(), 'Module not loaded; dependency for ' . implode(', ', $missing));
221
                        } else {
222
                            $output[$category][] = array($state, $category, $test->getModule(), 'Module not loaded');
223
                        }
224
                    }
225
                }
226
            }
227
        }
228
229
        $tests = $this->getTests();
230
        foreach ($availableModules as $category => $available) {
231
            $categories = array_fill(0, count($tests), $category);
232
            if (!isSet($output[$category])) {
233
                $modules = array_map(
234
                  function($test, $category) {
235
                    return ($test->getCategory() === $category) ? $test->getModule() : false;
236
                  }, $tests, $categories
237
                );
238
                $modules = array_diff($modules, array(false));
239
                $output[$category][] = array(State::OK, $category, implode(', ', $modules), "All required modules are loaded");
240
            }
241
            $this->addMessages($output[$category], $category);
242
        }
243
244
        $this->calculateState();
245
    }
246
}
247