Passed
Push — devel-3.0 ( bc80e8...31a38b )
by Rubén
03:32
created

PhpExtensionChecker::checkIsAvailable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
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
     * @throws CheckException
92
     */
93
    public function checkCurlAvailable()
94
    {
95
        if (!$this->checkIsAvailable('curl')) {
96
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'curl'));
97
        }
98
    }
99
100
    /**
101
     * Checks if the extension is installed
102
     *
103
     * @param $extension
104
     *
105
     * @return bool
106
     */
107
    public function checkIsAvailable(string $extension)
108
    {
109
        return in_array(strtolower($extension), $this->available);
110
    }
111
112
    /**
113
     * Checks if the extension is installed
114
     *
115
     * @throws CheckException
116
     */
117
    public function checkLdapAvailable()
118
    {
119
        if (!$this->checkIsAvailable('ldap')) {
120
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'ldap'));
121
        }
122
    }
123
124
    /**
125
     * Checks if the extension is installed
126
     *
127
     * @throws CheckException
128
     */
129
    public function checkSimpleXmlAvailable()
130
    {
131
        if (!$this->checkIsAvailable('simplexml')) {
132
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'simplexml'));
133
        }
134
    }
135
136
    /**
137
     * Checks if the extension is installed
138
     *
139
     * @throws CheckException
140
     */
141
    public function checkXmlAvailable()
142
    {
143
        if (!$this->checkIsAvailable('xml')) {
144
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'xml'));
145
        }
146
    }
147
148
    /**
149
     * Checks if the extension is installed
150
     *
151
     * @throws CheckException
152
     */
153
    public function checkPharAvailable()
154
    {
155
        if (!$this->checkIsAvailable('phar')) {
156
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'phar'));
157
        }
158
    }
159
160
    /**
161
     * Checks if the extension is installed
162
     *
163
     * @throws CheckException
164
     */
165
    public function checkJsonAvailable()
166
    {
167
        if (!$this->checkIsAvailable('json')) {
168
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'json'));
169
        }
170
    }
171
172
    /**
173
     * Checks if the extension is installed
174
     *
175
     * @throws CheckException
176
     */
177
    public function checkPdoAvailable()
178
    {
179
        if (!$this->checkIsAvailable('pdo')) {
180
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'pdo'));
181
        }
182
    }
183
184
    /**
185
     * Checks if the extension is installed
186
     *
187
     * @throws CheckException
188
     */
189
    public function checkGettextAvailable()
190
    {
191
        if (!$this->checkIsAvailable('gettext')) {
192
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'gettext'));
193
        }
194
    }
195
196
    /**
197
     * Checks if the extension is installed
198
     *
199
     * @throws CheckException
200
     */
201
    public function checkOpenSslAvailable()
202
    {
203
        if (!$this->checkIsAvailable('openssl')) {
204
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'openssl'));
205
        }
206
    }
207
208
    /**
209
     * Checks if the extension is installed
210
     *
211
     * @throws CheckException
212
     */
213
    public function checkGdAvailable()
214
    {
215
        if (!$this->checkIsAvailable('gd')) {
216
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'gd'));
217
        }
218
    }
219
220
    /**
221
     * Checks if the extension is installed
222
     *
223
     * @throws CheckException
224
     */
225
    public function checkMbstringAvailable()
226
    {
227
        if (!$this->checkIsAvailable('mbstring')) {
228
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, 'mbstring'));
229
        }
230
    }
231
232
    /**
233
     * @throws CheckException
234
     */
235
    public function checkMandatory()
236
    {
237
        $missing = array_filter(self::EXTENSIONS, function ($v, $k) {
238
            return $v === true && !in_array($k, $this->available);
239
        }, ARRAY_FILTER_USE_BOTH);
240
241
        if (count($missing) > 0) {
242
            throw new CheckException(sprintf(self::MSG_NOT_AVAILABLE, implode(',', array_keys($missing))));
243
        }
244
245
        logger('Extensions checked', 'INFO');
246
    }
247
}