1
|
|
|
<?php |
2
|
|
|
namespace core\modules; |
3
|
|
|
use core\App; |
4
|
|
|
|
5
|
|
|
class GestionModule { |
6
|
|
|
private $id_module; |
7
|
|
|
private $url; |
8
|
|
|
private $nom; |
9
|
|
|
private $version; |
10
|
|
|
private $online_version; |
11
|
|
|
private $icone; |
12
|
|
|
private $url_telechargement; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------// |
16
|
|
|
//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------// |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
20
|
|
|
public function getIdModule() { |
21
|
|
|
return $this->id_module; |
22
|
|
|
} |
23
|
|
|
public function getUrl() { |
24
|
|
|
return $this->url; |
25
|
|
|
} |
26
|
|
|
public function getNom() { |
27
|
|
|
return $this->nom; |
28
|
|
|
} |
29
|
|
|
public function getVersion() { |
30
|
|
|
return $this->version; |
31
|
|
|
} |
32
|
|
|
public function getOnlineVersion() { |
33
|
|
|
return $this->online_version; |
34
|
|
|
} |
35
|
|
|
public function getIcone() { |
36
|
|
|
return $this->icone; |
37
|
|
|
} |
38
|
|
|
public function getUrlTelechargement() { |
39
|
|
|
return $this->url_telechargement; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* récupere la liste des modules activé utilisé pour toutes les pages |
44
|
|
|
*/ |
45
|
|
|
public function getListeModuleActiver() { |
46
|
|
|
$dbc = App::getDb(); |
47
|
|
|
|
48
|
|
|
$query = $dbc->query("SELECT * FROM module WHERE activer=1 AND installer=1"); |
49
|
|
|
|
50
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
51
|
|
|
$id_module = []; |
52
|
|
|
$url = []; |
53
|
|
|
$nom = []; |
54
|
|
|
$version = []; |
55
|
|
|
$icone = []; |
56
|
|
|
|
57
|
|
|
foreach ($query as $obj) { |
58
|
|
|
$id_module[] = $obj->ID_module; |
59
|
|
|
$url[] = $obj->url; |
60
|
|
|
$nom[] = $obj->nom_module; |
61
|
|
|
$version[] = $obj->version; |
62
|
|
|
$icone[] = $obj->icone; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->setListeModuleActiver($id_module, $url, $version, $nom, $icone); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param null $systeme |
71
|
|
|
* recupere la listes des modules ajouter par un autre admin |
72
|
|
|
* fonction utilisée uniquement dans la config |
73
|
|
|
*/ |
74
|
|
|
public function getListeModule($systeme = 0) { |
75
|
|
|
$dbc = App::getDb(); |
76
|
|
|
|
77
|
|
|
$query = $dbc->query("SELECT * FROM module WHERE systeme=".$dbc->quote($systeme)); |
78
|
|
|
|
79
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
80
|
|
|
$id_module = []; |
81
|
|
|
$url = []; |
82
|
|
|
$nom = []; |
83
|
|
|
$version = []; |
84
|
|
|
$url_telechargement = []; |
85
|
|
|
|
86
|
|
|
foreach ($query as $obj) { |
87
|
|
|
$id_module[] = $obj->ID_module; |
88
|
|
|
$url[] = $obj->url; |
89
|
|
|
$nom[] = $obj->nom_module; |
90
|
|
|
$version[] = $obj->version; |
91
|
|
|
$url_telechargement[] = $obj->url_telechargement; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->setListeModuleActiver($id_module, $url, $version, $nom, null, $url_telechargement); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $nom_module |
100
|
|
|
* @return bool |
101
|
|
|
* permets de savoir si un module est installé ou non |
102
|
|
|
*/ |
103
|
|
|
public static function getModuleInstaller($nom_module) { |
104
|
|
|
$dbc = App::getDb(); |
105
|
|
|
|
106
|
|
|
$query = $dbc->query("SELECT * FROM module WHERE nom_module = ".$dbc->quote($nom_module)); |
107
|
|
|
|
108
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
109
|
|
|
$installer = 0; |
110
|
|
|
|
111
|
|
|
foreach ($query as $obj) { |
112
|
|
|
$installer = $obj->installer; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $installer; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param $nom_module |
121
|
|
|
* @return boolean|null |
122
|
|
|
* return true si le module est activer sinon false |
123
|
|
|
*/ |
124
|
|
|
public static function getModuleActiver($nom_module) { |
125
|
|
|
$dbc = App::getDb(); |
126
|
|
|
|
127
|
|
|
$query = $dbc->query("SELECT activer FROM module WHERE nom_module = ".$dbc->quote($nom_module)); |
128
|
|
|
|
129
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
130
|
|
|
foreach ($query as $obj) { |
131
|
|
|
if ($obj->activer == 1) { |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
else { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $nom_module |
143
|
|
|
* @return boolean|null |
144
|
|
|
* fonction qui permet de savoir si un module est à jour ou non |
145
|
|
|
* si a jour renvoi true sinon renvoi false |
146
|
|
|
*/ |
147
|
|
|
public static function getModuleAJour($nom_module) { |
148
|
|
|
$dbc = App::getDb(); |
149
|
|
|
|
150
|
|
|
$query = $dbc->query("SELECT mettre_jour FROM module WHERE nom_module = ".$dbc->quote($nom_module)); |
151
|
|
|
|
152
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
153
|
|
|
foreach ($query as $obj) { |
154
|
|
|
return $obj->mettre_jour; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* fonction qui se lance à chaquer fois que l'on ouvre l'admin |
161
|
|
|
* permet de tester si tous les modules présent sur le site sont bien à jour |
162
|
|
|
*/ |
163
|
|
|
public function getCheckModuleVersion() { |
164
|
|
|
$dbc = App::getDb(); |
165
|
|
|
$today = date("Y-m-d"); |
166
|
|
|
$today_o = new \DateTime($today); |
167
|
|
|
|
168
|
|
|
$query = $dbc->query("SELECT next_check_version, version, url_telechargement, mettre_jour, delete_old_version, ID_module FROM module"); |
169
|
|
|
|
170
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
171
|
|
|
foreach ($query as $obj) { |
172
|
|
|
if ($obj->next_check_version == null) { |
173
|
|
|
//si pas de version a checker, cela veut dire qu'on vient d'installer le module |
174
|
|
|
//du coup on met le next_check aa la semaine pro |
175
|
|
|
$set_next = true; |
176
|
|
|
} |
177
|
|
|
else if (($obj->next_check_version <= $today) && ($obj->mettre_jour != 1)) { |
178
|
|
|
//avant tout on regarde si on doit delete une vieille version |
179
|
|
|
if ($obj->delete_old_version == 1) { |
180
|
|
|
$import = new ImportModule(); |
181
|
|
|
$import->setSupprimerOldModule($obj->ID_module); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
//on recupere le nom du dossier + extention |
185
|
|
|
$explode = explode(".", $obj->url_telechargement); |
186
|
|
|
array_pop($explode); |
187
|
|
|
|
188
|
|
|
$version_txt = implode(".", $explode)."_version.txt"; |
189
|
|
|
|
190
|
|
|
if (file_get_contents($version_txt) !== "") { |
191
|
|
|
|
192
|
|
|
//online pour bdd |
193
|
|
|
$version_online_txt = file_get_contents($version_txt); |
194
|
|
|
|
195
|
|
|
$version_online = floatval($version_online_txt); |
196
|
|
|
$version_site = floatval($obj->version); |
197
|
|
|
|
198
|
|
|
//la version sur le serveur de telechargement est plus récente, on va donc proposer |
199
|
|
|
//en passant la valeur update a 1 dans la table module pour ce module |
200
|
|
|
// au client de mettre a jour sa version sinon on met la next check a la semaine pro |
201
|
|
|
if ($version_online > $version_site) { |
202
|
|
|
$value = [ |
203
|
|
|
"update" => 1, |
204
|
|
|
"online_version" => $version_online_txt, |
205
|
|
|
"id_module" => $obj->ID_module |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
//on met la notification admin à 1 |
209
|
|
|
$dbc->query("UPDATE notification SET admin=1 WHERE ID_notification=1"); |
210
|
|
|
|
211
|
|
|
$dbc->prepare("UPDATE module SET mettre_jour=:update, online_version=:online_version WHERE ID_module=:id_module", $value); |
212
|
|
|
|
213
|
|
|
$set_next = true; |
214
|
|
|
} |
215
|
|
|
else { |
216
|
|
|
$set_next = true; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if ((isset($set_next)) && ($set_next === true)) { |
222
|
|
|
$value = [ |
223
|
|
|
"next_check" => $today_o->add(new \DateInterval("P1W"))->format("Y-m-d"), |
224
|
|
|
"id_module" => $obj->ID_module |
225
|
|
|
]; |
226
|
|
|
|
227
|
|
|
$dbc->prepare("UPDATE module SET next_check_version=:next_check WHERE ID_module=:id_module", $value); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getListeModuleMettreJour() { |
234
|
|
|
$dbc = App::getDb(); |
235
|
|
|
|
236
|
|
|
$query = $dbc->query("SELECT * FROM module WHERE mettre_jour=1"); |
237
|
|
|
|
238
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
239
|
|
|
$nom_module = []; |
240
|
|
|
$version = []; |
241
|
|
|
$online_version = []; |
242
|
|
|
|
243
|
|
|
foreach ($query as $obj) { |
244
|
|
|
$nom_module[] = $obj->nom_module; |
245
|
|
|
$version[] = $obj->version; |
246
|
|
|
$online_version[] = $obj->online_version; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$this->setListeModuleMettreJour($nom_module, $version, $online_version); |
250
|
|
|
|
251
|
|
|
return true; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
//-------------------------- FIN GETTER ----------------------------------------------------------------------------// |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
258
|
|
|
private function setListeModuleActiver($id_module, $url, $version, $nom, $icone = null, $url_telechargement = null) { |
259
|
|
|
$this->id_module = $id_module; |
260
|
|
|
$this->url = $url; |
261
|
|
|
$this->nom = $nom; |
262
|
|
|
$this->version = $version; |
263
|
|
|
$this->icone = $icone; |
264
|
|
|
$this->url_telechargement = $url_telechargement; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
private function setListeModuleMettreJour($nom_module, $version, $online_version) { |
268
|
|
|
$this->nom = $nom_module; |
269
|
|
|
$this->version = $version; |
270
|
|
|
$this->online_version = $online_version; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param $activer |
275
|
|
|
* @param $url |
276
|
|
|
* fonction qui permet d'activer || désactiver un module |
277
|
|
|
*/ |
278
|
|
|
public static function setActiverDesactiverModule($activer, $url) { |
|
|
|
|
279
|
|
|
$dbc = App::getDb(); |
280
|
|
|
|
281
|
|
|
$value = array( |
282
|
|
|
"activer" => $activer, |
283
|
|
|
"url" => $url |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
$dbc->prepare("UPDATE module SET activer=:activer WHERE url=:url", $value); |
287
|
|
|
} |
288
|
|
|
//-------------------------- FIN SETTER ----------------------------------------------------------------------------// |
289
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.