Passed
Push — devel-3.0 ( 30ba17...7f31cf )
by Rubén
03:41
created

PhpExtensionChecker   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 243
rs 10
c 0
b 0
f 0
wmc 20

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A checkExtensions() 0 3 1
A checkOpenSslAvailable() 0 3 1
A checkXmlAvailable() 0 3 1
A checkPharAvailable() 0 3 1
A checkPdoAvailable() 0 3 1
A checkCurlAvailable() 0 3 1
A getMissing() 0 5 1
A checkIsAvailable() 0 9 3
A checkMandatory() 0 11 3
A checkSimpleXmlAvailable() 0 3 1
A checkGdAvailable() 0 3 1
A checkLdapAvailable() 0 3 1
A checkGettextAvailable() 0 3 1
A checkMbstringAvailable() 0 3 1
A checkJsonAvailable() 0 3 1
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      http://syspass.org
7
 * @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Core;
26
27
use SP\Core\Exceptions\CheckException;
28
29
/**
30
 * Class PhpExtensionChecker
31
 *
32
 * @package SP\Core
33
 */
34
final class PhpExtensionChecker
35
{
36
    /**
37
     * Array of extensions needed by sysPass.
38
     *
39
     * true  -> required
40
     * false -> not required
41
     */
42
    const EXTENSIONS = [
43
        'ldap' => false,
44
        'curl' => false,
45
        'simplexml' => false,
46
        'libxml' => true,
47
        'phar' => false,
48
        'json' => true,
49
        'xml' => true,
50
        'pdo' => true,
51
        'zlib' => false,
52
        'gettext' => true,
53
        'openssl' => true,
54
        'pcre' => true,
55
        'session' => true,
56
        'mcrypt' => false,
57
        'gd' => false,
58
        'mbstring' => true,
59
        'pdo_mysql' => true,
60
        'fileinfo' => true
61
    ];
62
63
    const MSG_NOT_AVAILABLE = 'Oops, it seems that some extensions are not available: \'%s\'';
64
65
    /**
66
     * Available extensions
67
     *
68
     * @var array
69
     */
70
    private $available;
71
72
    /**
73
     * PhpExtensionChecker constructor.
74
     */
75
    public function __construct()
76
    {
77
        $this->checkExtensions();
78
    }
79
80
    /**
81
     * Check for available extensions
82
     */
83
    public function checkExtensions()
84
    {
85
        $this->available = array_intersect(array_keys(self::EXTENSIONS), array_map('strtolower', get_loaded_extensions()));
86
    }
87
88
    /**
89
     * Checks if the extension is installed
90
     *
91
     * @param bool $exception
92
     *
93
     * @return bool
94
     * @throws CheckException
95
     */
96
    public function checkCurlAvailable($exception = false)
97
    {
98
        return $this->checkIsAvailable('curl', $exception);
99
    }
100
101
    /**
102
     * Checks if the extension is installed
103
     *
104
     * @param string $extension
105
     * @param bool   $exception Throws an exception if the extension is not available
106
     *
107
     * @return bool
108
     * @throws CheckException
109
     */
110
    public function checkIsAvailable(string $extension, $exception = false)
111
    {
112
        $result = in_array(strtolower($extension), $this->available);
113
114
        if (!$result && $exception) {
115
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, $extension));
116
        }
117
118
        return $result;
119
    }
120
121
    /**
122
     * Checks if the extension is installed
123
     *
124
     * @param bool $exception
125
     *
126
     * @return bool
127
     * @throws CheckException
128
     */
129
    public function checkLdapAvailable($exception = false)
130
    {
131
        return $this->checkIsAvailable('ldap', $exception);
132
    }
133
134
    /**
135
     * Checks if the extension is installed
136
     *
137
     * @param bool $exception
138
     *
139
     * @return bool
140
     * @throws CheckException
141
     */
142
    public function checkSimpleXmlAvailable($exception = false)
143
    {
144
        return $this->checkIsAvailable('simplexml', $exception);
145
    }
146
147
    /**
148
     * Checks if the extension is installed
149
     *
150
     * @param bool $exception
151
     *
152
     * @return bool
153
     * @throws CheckException
154
     */
155
    public function checkXmlAvailable($exception = false)
156
    {
157
        return $this->checkIsAvailable('xml', $exception);
158
    }
159
160
    /**
161
     * Checks if the extension is installed
162
     *
163
     * @param bool $exception
164
     *
165
     * @return bool
166
     * @throws CheckException
167
     */
168
    public function checkPharAvailable($exception = false)
169
    {
170
        return $this->checkIsAvailable('phar', $exception);
171
    }
172
173
    /**
174
     * Checks if the extension is installed
175
     *
176
     * @param bool $exception
177
     *
178
     * @return bool
179
     * @throws CheckException
180
     */
181
    public function checkJsonAvailable($exception = false)
182
    {
183
        return $this->checkIsAvailable('json', $exception);
184
    }
185
186
    /**
187
     * Checks if the extension is installed
188
     *
189
     * @param bool $exception
190
     *
191
     * @return bool
192
     * @throws CheckException
193
     */
194
    public function checkPdoAvailable($exception = false)
195
    {
196
        return $this->checkIsAvailable('pdo', $exception);
197
    }
198
199
    /**
200
     * Checks if the extension is installed
201
     *
202
     * @param bool $exception
203
     *
204
     * @return bool
205
     * @throws CheckException
206
     */
207
    public function checkGettextAvailable($exception = false)
208
    {
209
        return $this->checkIsAvailable('gettext', $exception);
210
    }
211
212
    /**
213
     * Checks if the extension is installed
214
     *
215
     * @param bool $exception
216
     *
217
     * @return bool
218
     * @throws CheckException
219
     */
220
    public function checkOpenSslAvailable($exception = false)
221
    {
222
        return $this->checkIsAvailable('openssl', $exception);
223
    }
224
225
    /**
226
     * Checks if the extension is installed
227
     *
228
     * @param bool $exception
229
     *
230
     * @return bool
231
     * @throws CheckException
232
     */
233
    public function checkGdAvailable($exception = false)
234
    {
235
        return $this->checkIsAvailable('gd', $exception);
236
    }
237
238
    /**
239
     * Checks if the extension is installed
240
     *
241
     * @param bool $exception
242
     *
243
     * @return bool
244
     * @throws CheckException
245
     */
246
    public function checkMbstringAvailable($exception = false)
247
    {
248
        return $this->checkIsAvailable('mbstring', $exception);
249
    }
250
251
    /**
252
     * @throws CheckException
253
     */
254
    public function checkMandatory()
255
    {
256
        $missing = array_filter(self::EXTENSIONS, function ($v, $k) {
257
            return $v === true && !in_array($k, $this->available);
258
        }, ARRAY_FILTER_USE_BOTH);
259
260
        if (count($missing) > 0) {
261
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, implode(',', array_keys($missing))));
262
        }
263
264
        logger('Extensions checked', 'INFO');
265
    }
266
267
    /**
268
     * Returns missing extensions
269
     *
270
     * @return array
271
     */
272
    public function getMissing()
273
    {
274
        return array_filter(self::EXTENSIONS, function ($k) {
275
            return !in_array($k, $this->available);
276
        }, ARRAY_FILTER_USE_KEY);
277
    }
278
}