]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/CheckModules.php
e30ea0ce49224ae15b3a74b21b0c2a7408330c33
[galette.git] / galette / lib / Galette / Core / CheckModules.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Required modules checking
7 *
8 * PHP version 5
9 *
10 * Copyright © 2007-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Core
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2012-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.1dev - 2012-03-12
36 */
37
38 namespace Galette\Core;
39
40 /**
41 * Required modules checking
42 *
43 * @category Core
44 * @name CheckModules
45 * @package Galette
46 * @author Johan Cwiklinski <johan@x-tnd.be>
47 * @copyright 2012-2014 The Galette Team
48 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
49 * @link http://galette.tuxfamily.org
50 * @since Available since 0.7.1dev - 2012-03-12
51 */
52 class CheckModules
53 {
54 private $good = array();
55 private $should = array();
56 private $missing = array();
57
58 private $modules = [
59 //name => required
60 'SimpleXML' => true,
61 'gd' => true,
62 'pdo' => true,
63 'curl' => false,
64 'tidy' => false,
65 'gettext' => false,
66 'mbstring' => true,
67 'openssl' => false,
68 'intl' => true,
69 'session' => true
70 ];
71
72
73 /**
74 * Constructor
75 *
76 * @param boolean $do Whether to do checks, defaults to true
77 */
78 public function __construct($do = true)
79 {
80 if ($do === true) {
81 $this->doCheck();
82 }
83 }
84
85 /**
86 * Check various modules and dispatch them beetween:
87 * - good: module that are present,
88 * - may: modules that may be present but are not,
89 * - should: modules that should be present but are not,
90 * - missing: required modules that are missing
91 *
92 * @param boolean $translated Use translations (default to true)
93 *
94 * @return void
95 */
96 public function doCheck($translated = true)
97 {
98 $string = ($translated ? _T("'%s' module") : "'%s' module");
99 foreach ($this->modules as $name => $required) {
100 if ($name == 'pdo') {
101 //one of mysql or pgsql driver must be present
102 $mstring = "either 'mysql' or 'pgsql' PDO driver";
103 if ($translated) {
104 $mstring = _T("either 'mysql' or 'pgsql' PDO driver");
105 }
106 if (!extension_loaded('pdo_mysql')
107 && !extension_loaded('pdo_pgsql')
108 ) {
109 $this->missing[] = $mstring;
110 } else {
111 $this->good[$name] = $mstring;
112 }
113 } else {
114 $mstring = str_replace('%s', $name, $string);
115 if (!extension_loaded($name)) {
116 if ($required) {
117 $this->missing[] = $mstring;
118 } else {
119 $this->should[] = $mstring;
120 }
121 } else {
122 $this->good[$name] = str_replace('%s', $name, $string);
123 }
124 }
125 }
126 }
127
128 /**
129 * HTML formatted results for checks
130 *
131 * @param boolean $translated Use translations (default to true)
132 *
133 * @return string
134 */
135 public function toHtml($translated = true)
136 {
137 $html = null;
138 $img_dir = null;
139 if (defined('GALETTE_THEME_DIR')) {
140 $img_dir = GALETTE_THEME_DIR . 'images/';
141 } else {
142 $img_dir = GALETTE_TPL_SUBDIR . 'images/';
143 }
144
145 if (count($this->missing) > 0) {
146 $ko = ($translated ? _T('Ko') : 'Ko');
147 foreach ($this->missing as $m) {
148 $html .= '<li><span>' . $m . '</span><span><img src="' .
149 $img_dir . 'icon-invalid.png" alt="' .
150 $ko . '"/></span></li>';
151 }
152 }
153
154 if (count($this->good) > 0) {
155 $ok = ($translated ? _T('Ok') : 'Ok');
156 foreach ($this->good as $m) {
157 $html .= '<li><span>' . $m . '</span><span><img src="' .
158 $img_dir . 'icon-valid.png" alt="' .
159 $ok . '"/></span></li>';
160 }
161 }
162
163 if (count($this->should) > 0) {
164 foreach ($this->should as $m) {
165 $html .= '<li><span>' . $m . '</span><span><img src="' .
166 $img_dir . 'icon-warning.png" alt=""' .
167 '/></span></li>';
168 }
169 }
170
171 return $html;
172 }
173
174 /**
175 * Check if it is ok to use Galette with current modules
176 *
177 * @return boolean
178 */
179 public function isValid()
180 {
181 return count($this->missing) === 0;
182 }
183
184 /**
185 * Check if a specific module is OK for that instance
186 *
187 * @param string $module Module name to check
188 *
189 * @return boolean
190 */
191 public function isGood($module)
192 {
193 return isset($this->good[$module]);
194 }
195
196 /**
197 * Retrieve good modules
198 *
199 * @return array
200 */
201 public function getGoods()
202 {
203 return $this->good;
204 }
205
206 /**
207 * Retrieve should modules
208 *
209 * @return array
210 */
211 public function getShoulds()
212 {
213 return $this->should;
214 }
215
216 /**
217 * Retrieve missing modules
218 *
219 * @return array
220 */
221 public function getMissings()
222 {
223 return $this->missing;
224 }
225 }