1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<?php
namespace Garradin;
class Plugin
{
protected $id = null;
protected $config = null;
public function __construct($id)
{
$this->id = null;
}
public function getConfig($key = null)
{
if (is_null($this->config))
{
$db = DB::getInstance();
$this->config = $db->simpleQuerySingle('SELECT config FROM plugins WHERE id = ?;', false, $this->id);
$this->config = json_decode($this->config, true);
if (!is_array($this->config))
{
$this->config = [];
}
}
if (array_key_exists($key, $this->config))
{
return $this->config[$key];
}
return null;
}
public function setConfig($key, $value = null)
{
$this->getConfig();
if (is_null($value))
{
unset($this->config[$key]);
}
else
{
$this->config[$key] = $value;
}
$db = DB::getInstance();
$db->simpleUpdate('plugins', 'id = \'' . $this->id . '\'', ['config' => json_encode($this->config)]);
return true;
}
public function getInfos()
{
$db = DB::getInstance();
return $db->simpleStatementFetch('SELECT * FROM plugins WHERE id = ?;', $this->id);
}
static public function listInstalled()
{
$db = DB::getInstance();
return $db->simpleStatementFetchAssocKey('SELECT id, * FROM plugins ORDER BY nom;');
}
static public function listDownloaded()
{
$installed = self::listInstalled();
$list = [];
$dir = dir(PLUGINS_PATH);
|
|
|
>
|
>
|
>
>
|
>
|
|
<
<
|
|
<
|
|
|
|
>
|
>
>
>
>
>
|
|
<
<
|
|
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
<?php
namespace Garradin;
class Plugin
{
protected $id = null;
protected $plugin = null;
public function __construct($id)
{
$db = DB::getInstance();
$this->plugin = $db->simpleQuerySingle('SELECT * FROM plugins WHERE id = ?;', true, $id);
if (!$this->plugin)
{
throw new UserException('Ce plugin n\'existe pas ou n\'est pas installé correctement.');
}
$this->plugin['config'] = json_decode($this->plugin['config'], true);
if (!is_array($this->plugin['config']))
{
$this->plugin['config'] = [];
}
$this->id = $id;
}
public function getConfig($key = null)
{
if (is_null($key))
{
return $this->plugin['config'];
}
if (array_key_exists($key, $this->plugin['config']))
{
return $this->plugin['config'][$key];
}
return null;
}
public function setConfig($key, $value = null)
{
if (is_null($value))
{
unset($this->plugin['config'][$key]);
}
else
{
$this->plugin['config'][$key] = $value;
}
$db = DB::getInstance();
$db->simpleUpdate('plugins',
['config' => json_encode($this->plugin['config'])],
'id = \'' . $this->id . '\'');
return true;
}
public function getInfos($key = null)
{
if (is_null($key))
{
return $this->plugin;
}
if (array_key_exists($key, $this->plugin))
{
return $this->plugin[$key];
}
return null;
}
public function id()
{
return $this->id;
}
public function call($file)
{
$forbidden = ['install.php', 'infos.ini', 'upgrade.php', 'uninstall.php', 'signals.php'];
if (in_array($file, $forbidden))
{
throw new UserException('Le fichier ' . $file . ' ne peut être appelé par cette méthode.');
}
if (!file_exists('phar://' . PLUGINS_PATH . '/' . $this->id . '.phar/' . $file))
{
throw new UserException('Le fichier ' . $file . ' n\'existe pas dans le plugin ' . $this->id);
}
$plugin = $this;
global $tpl, $config, $user, $membres;
include 'phar://' . PLUGINS_PATH . '/' . $this->id . '.phar/' . $file;
}
public function uninstall()
{
if (file_exists('phar://' . PLUGINS_PATH . '/' . $this->id . '.phar/uninstall.php'))
{
include 'phar://' . PLUGINS_PATH . '/' . $this->id . '.phar/uninstall.php';
}
unlink(PLUGINS_PATH . '/' . $this->id . '.phar');
$db = DB::getInstance();
return $db->simpleExec('DELETE FROM plugins WHERE id = ?;', $this->id);
}
public function needUpgrade()
{
$infos = parse_ini_file('phar://' . PLUGINS_PATH . '/' . $this->id . '.phar/infos.ini', false);
if (version_compare($this->plugin['version'], $infos['version'], '!='))
return true;
return false;
}
public function upgrade()
{
if (file_exists('phar://' . PLUGINS_PATH . '/' . $this->id . '.phar/upgrade.php'))
{
include 'phar://' . PLUGINS_PATH . '/' . $this->id . '.phar/upgrade.php';
}
$db = DB::getInstance();
return $db->simpleUpdate('plugins',
'id = \''.$db->escapeString($this->id).'\'',
['version' => $infos['version']]);
}
static public function listInstalled()
{
$db = DB::getInstance();
return $db->simpleStatementFetchAssocKey('SELECT id, * FROM plugins ORDER BY nom;');
}
static public function listMenu()
{
$db = DB::getInstance();
return $db->simpleStatementFetchAssoc('SELECT id, nom FROM plugins WHERE menu = 1 ORDER BY nom;');
}
static public function listDownloaded()
{
$installed = self::listInstalled();
$list = [];
$dir = dir(PLUGINS_PATH);
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
$context = stream_context_create($context_options);
try {
$result = file_get_contents(PLUGINS_URL, NULL, $context);
}
catch (\Exception $e)
{
throw new UserException('Le téléchargement de la liste des plugins a échoué : ' . $e->getMessage())
}
Static_Cache::store('plugins_list', $result);
}
else
{
$result = Static_Cache::get('plugins_list');
|
|
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
$context = stream_context_create($context_options);
try {
$result = file_get_contents(PLUGINS_URL, NULL, $context);
}
catch (\Exception $e)
{
throw new UserException('Le téléchargement de la liste des plugins a échoué : ' . $e->getMessage());
}
Static_Cache::store('plugins_list', $result);
}
else
{
$result = Static_Cache::get('plugins_list');
|
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
$context = stream_context_create($context_options);
try {
copy($list[$id]['phar'], PLUGINS_PATH . '/' . $id . '.phar', $context);
}
catch (\Exception $e)
{
throw new \RuntimeException('Le téléchargement du plugin '.$id.' a échoué : ' . $e->getMessage())
}
if (!self::checkHash($id))
{
unlink(PLUGINS_PATH . '/' . $id . '.phar');
throw new \RuntimeException('L\'archive du plugin '.$id.' est corrompue (le hash SHA1 ne correspond pas).');
}
self::install($id, true);
return true;
}
static public function install($id, $official = true)
{
if ($official && !self::checkHash($id))
{
throw new \RuntimeException('L\'archive du plugin '.$id.' est corrompue (le hash SHA1 ne correspond pas).');
}
if (file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/install.php'))
{
include 'phar://' . PLUGINS_PATH . '/' . $id . '.phar/install.php';
}
$infos = parse_ini_file('phar://' . PLUGINS_PATH . '/' . $id . '.phar/infos.ini', false);
$db = DB::getInstance();
$db->simpleInsert('plugins', [
'id' => $id,
'officiel' => (int)(bool)$official,
'nom' => $infos['nom'],
'description'=> $infos['description'],
'auteur' => $infos['auteur'],
'url' => $infos['url'],
'version' => $infos['version'],
'menu' => (int)(bool)$infos['menu'],
'config' => '',
]);
}
static public function uninstall($id)
{
if (file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/uninstall.php'))
{
include 'phar://' . PLUGINS_PATH . '/' . $id . '.phar/uninstall.php';
}
$db = DB::getInstance();
return $db->simpleExec('DELETE FROM plugins WHERE id = ?;', $id);
}
static public function needUpgrade($id)
{
$infos = parse_ini_file('phar://' . PLUGINS_PATH . '/' . $id . '.phar/infos.ini', false);
$version = $db->simpleQuerySingle('SELECT version FROM plugins WHERE id = ?;', false, $id);
if (version_compare($version, $infos['version'], '!='))
return true;
return false;
}
static public function upgrade($id)
{
if (file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/upgrade.php'))
{
include 'phar://' . PLUGINS_PATH . '/' . $id . '.phar/upgrade.php';
}
$db = DB::getInstance();
return $db->simpleUpdate('plugins', 'id = \''.$db->escapeString($id).'\'', ['version' => $infos['version']]);
}
}
|
|
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
<
<
|
|
|
<
<
<
<
<
<
<
<
<
<
|
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
|
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
$context = stream_context_create($context_options);
try {
copy($list[$id]['phar'], PLUGINS_PATH . '/' . $id . '.phar', $context);
}
catch (\Exception $e)
{
throw new \RuntimeException('Le téléchargement du plugin '.$id.' a échoué : ' . $e->getMessage());
}
if (!self::checkHash($id))
{
unlink(PLUGINS_PATH . '/' . $id . '.phar');
throw new \RuntimeException('L\'archive du plugin '.$id.' est corrompue (le hash SHA1 ne correspond pas).');
}
self::install($id, true);
return true;
}
static public function install($id, $official = true)
{
if (!file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar'))
{
throw new \RuntimeException('Le plugin ' . $id . ' ne semble pas exister et ne peut donc être installé.');
}
if ($official && !self::checkHash($id))
{
throw new \RuntimeException('L\'archive du plugin '.$id.' est corrompue (le hash SHA1 ne correspond pas).');
}
if (!file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/infos.ini'))
{
throw new \RuntimeException('L\'archive '.$id.'.phar ne comporte pas de fichier infos.ini : est-ce un plugin Garradin ?');
}
if (!file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/index.php'))
{
throw new \RuntimeException('L\'archive '.$id.'.phar ne comporte pas de fichier index.php : est-ce un plugin Garradin ?');
}
$infos = parse_ini_file('phar://' . PLUGINS_PATH . '/' . $id . '.phar/infos.ini', false);
if ((bool)$infos['config'])
{
if (!file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/config.json'))
{
throw new \RuntimeException('L\'archive '.$id.'.phar ne comporte pas de fichier config.json
alors que le plugin nécessite le stockage d\'une configuration.');
}
if (!file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/config.php'))
{
throw new \RuntimeException('L\'archive '.$id.'.phar ne comporte pas de fichier config.php
alors que le plugin nécessite le stockage d\'une configuration.');
}
}
$db = DB::getInstance();
$db->simpleInsert('plugins', [
'id' => $id,
'officiel' => (int)(bool)$official,
'nom' => $infos['nom'],
'description'=> $infos['description'],
'auteur' => $infos['auteur'],
'url' => $infos['url'],
'version' => $infos['version'],
'menu' => (int)(bool)$infos['menu'],
'config' => $config,
]);
if (file_exists('phar://' . PLUGINS_PATH . '/' . $id . '.phar/install.php'))
{
include 'phar://' . PLUGINS_PATH . '/' . $id . '.phar/install.php';
}
return true;
}
}
|