]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/CheckModules.php
995fd0fb60fb79df19fe17654bd5ff63d4bdd3d1
[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 (
107 !extension_loaded('pdo_mysql')
108 && !extension_loaded('pdo_pgsql')
109 ) {
110 $this->missing[] = $mstring;
111 } else {
112 $this->good[$name] = $mstring;
113 }
114 } else {
115 $mstring = str_replace('%s', $name, $string);
116 if (!extension_loaded($name)) {
117 if ($required) {
118 $this->missing[] = $mstring;
119 } else {
120 $this->should[] = $mstring;
121 }
122 } else {
123 $this->good[$name] = str_replace('%s', $name, $string);
124 }
125 }
126 }
127 }
128
129 /**
130 * HTML formatted results for checks
131 *
132 * @param boolean $translated Use translations (default to true)
133 *
134 * @return string
135 */
136 public function toHtml($translated = true)
137 {
138 $html = null;
139 $img_dir = null;
140 if (defined('GALETTE_THEME_DIR')) {
141 $img_dir = GALETTE_THEME_DIR . 'images/';
142 } else {
143 $img_dir = GALETTE_TPL_SUBDIR . 'images/';
144 }
145
146 if (count($this->missing) > 0) {
147 $ko = ($translated ? _T('Ko') : 'Ko');
148 foreach ($this->missing as $m) {
149 $html .= '<li><span>' . $m . '</span><span><img src="' .
150 $img_dir . 'icon-invalid.png" alt="' .
151 $ko . '"/></span></li>';
152 }
153 }
154
155 if (count($this->good) > 0) {
156 $ok = ($translated ? _T('Ok') : 'Ok');
157 foreach ($this->good as $m) {
158 $html .= '<li><span>' . $m . '</span><span><img src="' .
159 $img_dir . 'icon-valid.png" alt="' .
160 $ok . '"/></span></li>';
161 }
162 }
163
164 if (count($this->should) > 0) {
165 foreach ($this->should as $m) {
166 $html .= '<li><span>' . $m . '</span><span><img src="' .
167 $img_dir . 'icon-warning.png" alt=""' .
168 '/></span></li>';
169 }
170 }
171
172 return $html;
173 }
174
175 /**
176 * Check if it is ok to use Galette with current modules
177 *
178 * @return boolean
179 */
180 public function isValid()
181 {
182 return count($this->missing) === 0;
183 }
184
185 /**
186 * Check if a specific module is OK for that instance
187 *
188 * @param string $module Module name to check
189 *
190 * @return boolean
191 */
192 public function isGood($module)
193 {
194 return isset($this->good[$module]);
195 }
196
197 /**
198 * Retrieve good modules
199 *
200 * @return array
201 */
202 public function getGoods()
203 {
204 return $this->good;
205 }
206
207 /**
208 * Retrieve should modules
209 *
210 * @return array
211 */
212 public function getShoulds()
213 {
214 return $this->should;
215 }
216
217 /**
218 * Retrieve missing modules
219 *
220 * @return array
221 */
222 public function getMissings()
223 {
224 return $this->missing;
225 }
226 }