]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/CheckModules.php
Remove 'svn' lines
[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 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7.1dev - 2012-03-12
35 */
36
37 namespace Galette\Core;
38
39 /**
40 * Required modules checking
41 *
42 * @category Core
43 * @name CheckModules
44 * @package Galette
45 * @author Johan Cwiklinski <johan@x-tnd.be>
46 * @copyright 2012-2014 The Galette Team
47 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
48 * @link http://galette.tuxfamily.org
49 * @since Available since 0.7.1dev - 2012-03-12
50 */
51 class CheckModules
52 {
53 private $good = array();
54 private $should = array();
55 private $missing = array();
56
57 private $modules = [
58 //name => required
59 'SimpleXML' => true,
60 'gd' => true,
61 'pdo' => true,
62 'curl' => false,
63 'tidy' => false,
64 'gettext' => false,
65 'mbstring' => true,
66 'openssl' => false,
67 'intl' => true,
68 'session' => true
69 ];
70
71
72 /**
73 * Constructor
74 *
75 * @param boolean $do Whether to do checks, defaults to true
76 */
77 public function __construct($do = true)
78 {
79 if ($do === true) {
80 $this->doCheck();
81 }
82 }
83
84 /**
85 * Check various modules and dispatch them beetween:
86 * - good: module that are present,
87 * - may: modules that may be present but are not,
88 * - should: modules that should be present but are not,
89 * - missing: required modules that are missing
90 *
91 * @param boolean $translated Use translations (default to true)
92 *
93 * @return void
94 */
95 public function doCheck($translated = true)
96 {
97 $string = ($translated ? _T("'%s' module") : "'%s' module");
98 foreach ($this->modules as $name => $required) {
99 if ($name == 'pdo') {
100 //one of mysql or pgsql driver must be present
101 $mstring = "either 'mysql' or 'pgsql' PDO driver";
102 if ($translated) {
103 $mstring = _T("either 'mysql' or 'pgsql' PDO driver");
104 }
105 if (
106 !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 }