1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @subpackage System module |
5
|
|
|
* @category modules |
6
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi |
8
|
|
|
* @license MIT License, see license.txt |
9
|
|
|
*/ |
10
|
|
|
namespace cs\modules\System\api\Controller\admin; |
11
|
|
|
use |
12
|
|
|
cs\Cache as System_cache, |
13
|
|
|
cs\Config, |
14
|
|
|
cs\Event, |
15
|
|
|
cs\ExitException, |
16
|
|
|
cs\Language, |
17
|
|
|
cs\Permission, |
18
|
|
|
cs\Session, |
19
|
|
|
cs\modules\System\Packages_dependencies, |
20
|
|
|
cs\modules\System\Packages_manipulation; |
21
|
|
|
|
22
|
|
|
trait modules { |
23
|
|
|
/** |
24
|
|
|
* @param \cs\Request $Request |
25
|
|
|
* |
26
|
|
|
* @return mixed |
27
|
|
|
* |
28
|
|
|
* @throws ExitException |
29
|
|
|
*/ |
30
|
|
|
public static function admin_modules_get ($Request) { |
31
|
|
|
if ($Request->route_path(3)) { |
32
|
|
|
$route_path = $Request->route_path; |
33
|
|
|
switch ($route_path[3]) { |
34
|
|
|
/** |
35
|
|
|
* Get dependent packages for module |
36
|
|
|
*/ |
37
|
|
|
case 'dependent_packages': |
38
|
|
|
return static::get_dependent_packages_for_module($route_path[2]); |
39
|
|
|
/** |
40
|
|
|
* Get dependencies for module (packages, databases, storages) |
41
|
|
|
*/ |
42
|
|
|
case 'dependencies': |
43
|
|
|
return static::get_dependencies_for_module($route_path[2]); |
44
|
|
|
/** |
45
|
|
|
* Get dependencies for module during update |
46
|
|
|
*/ |
47
|
|
|
case 'update_dependencies': |
48
|
|
|
return static::get_update_dependencies_for_module($route_path[2]); |
49
|
|
|
/** |
50
|
|
|
* Get mapping of named module's databases to indexes of system databases |
51
|
|
|
*/ |
52
|
|
|
case 'db': |
53
|
|
|
return static::get_module_databases($route_path[2]); |
54
|
|
|
/** |
55
|
|
|
* Get mapping of named module's storages to indexes of system storages |
56
|
|
|
*/ |
57
|
|
|
case 'storage': |
58
|
|
|
return static::get_module_storages($route_path[2]); |
59
|
|
|
default: |
60
|
|
|
throw new ExitException(400); |
61
|
|
|
} |
62
|
|
|
} elseif ($Request->route_path(2) == 'default') { |
63
|
|
|
/** |
64
|
|
|
* Get current default module |
65
|
|
|
*/ |
66
|
|
|
return static::get_default_module(); |
67
|
|
|
} else { |
68
|
|
|
/** |
69
|
|
|
* Get array of modules in extended form |
70
|
|
|
*/ |
71
|
|
|
return static::get_modules_list(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
/** |
75
|
|
|
* @param string $module |
76
|
|
|
* |
77
|
|
|
* @return string[][] |
78
|
|
|
* |
79
|
|
|
* @throws ExitException |
80
|
|
|
*/ |
81
|
|
|
protected static function get_dependent_packages_for_module ($module) { |
82
|
|
|
if (!Config::instance()->module($module)) { |
83
|
|
|
throw new ExitException(404); |
84
|
|
|
} |
85
|
|
|
$meta_file = MODULES."/$module/meta.json"; |
86
|
|
|
return file_exists($meta_file) ? Packages_dependencies::get_dependent_packages(file_get_json($meta_file)) : []; |
87
|
|
|
} |
88
|
|
|
/** |
89
|
|
|
* @param string $module |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
* |
93
|
|
|
* @throws ExitException |
94
|
|
|
*/ |
95
|
|
|
protected static function get_dependencies_for_module ($module) { |
96
|
|
|
if (!Config::instance()->module($module)) { |
97
|
|
|
throw new ExitException(404); |
98
|
|
|
} |
99
|
|
|
$meta_file = MODULES."/$module/meta.json"; |
100
|
|
|
return file_exists($meta_file) ? Packages_dependencies::get_dependencies(file_get_json($meta_file)) : []; |
101
|
|
|
} |
102
|
|
|
/** |
103
|
|
|
* @param string $module |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
* |
107
|
|
|
* @throws ExitException |
108
|
|
|
*/ |
109
|
|
|
protected static function get_update_dependencies_for_module ($module) { |
110
|
|
|
if (!Config::instance()->module($module)) { |
111
|
|
|
throw new ExitException(404); |
112
|
|
|
} |
113
|
|
|
$tmp_location = TEMP.'/System/admin/'.Session::instance()->get_id().'.phar'; |
114
|
|
|
$tmp_dir = "phar://$tmp_location"; |
115
|
|
|
if ( |
116
|
|
|
!file_exists(MODULES."/$module/meta.json") || |
117
|
|
|
!file_exists("$tmp_dir/meta.json") |
118
|
|
|
) { |
119
|
|
|
throw new ExitException(400); |
120
|
|
|
} |
121
|
|
|
$new_meta = file_get_json("$tmp_dir/meta.json"); |
122
|
|
|
if (!static::is_same_module($new_meta, $module)) { |
123
|
|
|
throw new ExitException(Language::prefix('system_admin_modules_')->this_is_not_module_installer_file, 400); |
124
|
|
|
} |
125
|
|
|
return Packages_dependencies::get_dependencies($new_meta, true); |
126
|
|
|
} |
127
|
|
|
/** |
128
|
|
|
* @param array $meta |
129
|
|
|
* @param string $module |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
protected static function is_same_module ($meta, $module) { |
134
|
|
|
return $meta['category'] == 'modules' && $meta['package'] == $module; |
135
|
|
|
} |
136
|
|
|
/** |
137
|
|
|
* @param string $module |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
* |
141
|
|
|
* @throws ExitException |
142
|
|
|
*/ |
143
|
|
|
protected static function get_module_databases ($module) { |
144
|
|
|
$Config = Config::instance(); |
145
|
|
|
if (!isset($Config->components['modules'][$module]['db'])) { |
146
|
|
|
throw new ExitException(404); |
147
|
|
|
} |
148
|
|
|
return $Config->components['modules'][$module]['db']; |
149
|
|
|
} |
150
|
|
|
/** |
151
|
|
|
* @param string $module |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
* |
155
|
|
|
* @throws ExitException |
156
|
|
|
*/ |
157
|
|
|
protected static function get_module_storages ($module) { |
158
|
|
|
$Config = Config::instance(); |
159
|
|
|
if (!isset($Config->components['modules'][$module]['storage'])) { |
160
|
|
|
throw new ExitException(404); |
161
|
|
|
} |
162
|
|
|
return $Config->components['modules'][$module]['storage']; |
163
|
|
|
} |
164
|
|
|
protected static function get_modules_list () { |
165
|
|
|
$Config = Config::instance(); |
166
|
|
|
$modules_list = []; |
167
|
|
|
foreach ($Config->components['modules'] as $module_name => &$module_data) { |
168
|
|
|
$module = [ |
169
|
|
|
'active' => $module_data['active'], |
170
|
|
|
'name' => $module_name, |
171
|
|
|
'has_user_section' => file_exists_with_extension(MODULES."/$module_name/index", ['php', 'html', 'json']), |
172
|
|
|
'has_admin_section' => file_exists_with_extension(MODULES."/$module_name/admin/index", ['php', 'json']) |
173
|
|
|
]; |
174
|
|
|
/** |
175
|
|
|
* Check if API available |
176
|
|
|
*/ |
177
|
|
|
static::check_module_feature_availability($module, 'readme', 'api'); |
178
|
|
|
/** |
179
|
|
|
* Check if readme available |
180
|
|
|
*/ |
181
|
|
|
static::check_module_feature_availability($module, 'readme'); |
182
|
|
|
/** |
183
|
|
|
* Check if license available |
184
|
|
|
*/ |
185
|
|
|
static::check_module_feature_availability($module, 'license'); |
186
|
|
|
if (file_exists(MODULES."/$module_name/meta.json")) { |
187
|
|
|
$module['meta'] = file_get_json(MODULES."/$module_name/meta.json"); |
188
|
|
|
} |
189
|
|
|
$modules_list[] = $module; |
190
|
|
|
} |
191
|
|
|
return $modules_list; |
192
|
|
|
} |
193
|
|
|
/** |
194
|
|
|
* @param array $module |
195
|
|
|
* @param string $feature |
196
|
|
|
* @param string $dir |
197
|
|
|
*/ |
198
|
|
|
protected static function check_module_feature_availability (&$module, $feature, $dir = '') { |
199
|
|
|
/** |
200
|
|
|
* Check if feature available |
201
|
|
|
*/ |
202
|
|
|
$file = file_exists_with_extension(MODULES."/$module[name]/$dir/$feature", ['txt', 'html']); |
203
|
|
|
if ($file) { |
204
|
|
|
$module[$dir ?: $feature] = [ |
205
|
|
|
'type' => substr($file, -3) == 'txt' ? 'txt' : 'html', |
206
|
|
|
'content' => file_get_contents($file) |
207
|
|
|
]; |
208
|
|
|
} elseif ($dir && is_dir(MODULES."/$module[name]/$dir")) { |
209
|
|
|
$module[$dir ?: $feature] = []; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
/** |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
protected static function get_default_module () { |
216
|
|
|
return Config::instance()->core['default_module']; |
217
|
|
|
} |
218
|
|
|
/** |
219
|
|
|
* @param \cs\Request $Request |
220
|
|
|
* |
221
|
|
|
* @throws ExitException |
222
|
|
|
*/ |
223
|
|
|
public static function admin_modules_put ($Request) { |
224
|
|
|
if ($Request->route_path(3)) { |
225
|
|
|
$module = $Request->route_path[2]; |
226
|
|
|
switch ($Request->route_path[3]) { |
227
|
|
|
/** |
228
|
|
|
* Set mapping of named module's databases to indexes of system databases |
229
|
|
|
*/ |
230
|
|
|
case 'db': |
231
|
|
|
static::set_module_databases($Request, $module); |
232
|
|
|
break; |
233
|
|
|
/** |
234
|
|
|
* Set mapping of named module's storages to indexes of system storages |
235
|
|
|
*/ |
236
|
|
|
case 'storage': |
237
|
|
|
static::set_module_storages($Request, $module); |
238
|
|
|
break; |
239
|
|
|
default: |
240
|
|
|
throw new ExitException(400); |
241
|
|
|
} |
242
|
|
|
} elseif ($Request->route_path(2) == 'default') { |
243
|
|
|
/** |
244
|
|
|
* Set current default module |
245
|
|
|
*/ |
246
|
|
|
static::set_default_module($Request->data('module')); |
247
|
|
|
} else { |
248
|
|
|
throw new ExitException(400); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
/** |
252
|
|
|
* @param \cs\Request $Request |
253
|
|
|
* @param string $module |
254
|
|
|
* |
255
|
|
|
* @throws ExitException |
256
|
|
|
*/ |
257
|
|
|
protected static function set_module_databases ($Request, $module) { |
258
|
|
|
$Config = Config::instance(); |
259
|
|
|
$database_config = $Request->data('db'); |
260
|
|
|
if (!$database_config || !isset($Config->components['modules'][$module]['db'])) { |
261
|
|
|
throw new ExitException(404); |
262
|
|
|
} |
263
|
|
|
$Config->components['modules'][$module]['db'] = _int($database_config); |
264
|
|
|
static::admin_modules_save(); |
265
|
|
|
} |
266
|
|
|
/** |
267
|
|
|
* @param \cs\Request $Request |
268
|
|
|
* @param string $module |
269
|
|
|
* |
270
|
|
|
* @throws ExitException |
271
|
|
|
*/ |
272
|
|
|
protected static function set_module_storages ($Request, $module) { |
273
|
|
|
$Config = Config::instance(); |
274
|
|
|
$storage_config = $Request->data('storage'); |
275
|
|
|
if (!$storage_config || !isset($Config->components['modules'][$module]['storage'])) { |
276
|
|
|
throw new ExitException(404); |
277
|
|
|
} |
278
|
|
|
$Config->components['modules'][$module]['storage'] = _int($storage_config); |
279
|
|
|
static::admin_modules_save(); |
280
|
|
|
} |
281
|
|
|
/** |
282
|
|
|
* @param string $module |
283
|
|
|
* |
284
|
|
|
* @throws ExitException |
285
|
|
|
*/ |
286
|
|
|
protected static function set_default_module ($module) { |
287
|
|
|
$Config = Config::instance(); |
288
|
|
|
if (!Event::instance()->fire('admin/System/modules/default', ['name' => $module])) { |
289
|
|
|
throw new ExitException(500); |
290
|
|
|
} |
291
|
|
|
$Config->core['default_module'] = $module; |
292
|
|
|
static::admin_modules_save(); |
293
|
|
|
if ($Config->core['default_module'] != $module) { |
294
|
|
|
throw new ExitException(400); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
/** |
298
|
|
|
* Enable module |
299
|
|
|
* |
300
|
|
|
* Provides next events: |
301
|
|
|
* admin/System/modules/enable/before |
302
|
|
|
* ['name' => module_name] |
303
|
|
|
* |
304
|
|
|
* admin/System/modules/enable/after |
305
|
|
|
* ['name' => module_name] |
306
|
|
|
* |
307
|
|
|
* @param \cs\Request $Request |
308
|
|
|
* |
309
|
|
|
* @throws ExitException |
310
|
|
|
*/ |
311
|
|
|
public static function admin_modules_enable ($Request) { |
312
|
|
|
$Config = Config::instance(); |
313
|
|
|
$module = $Request->route_path(2); |
314
|
|
|
if (!$Config->module($module)->disabled()) { |
315
|
|
|
throw new ExitException(400); |
316
|
|
|
} |
317
|
|
|
if (!Event::instance()->fire('admin/System/modules/enable/before', ['name' => $module])) { |
318
|
|
|
throw new ExitException(500); |
319
|
|
|
} |
320
|
|
|
$Config->components['modules'][$module]['active'] = Config\Module_Properties::ENABLED; |
321
|
|
|
static::admin_modules_save(); |
322
|
|
|
Event::instance()->fire('admin/System/modules/enable/after', ['name' => $module]); |
323
|
|
|
static::admin_modules_cleanup(); |
324
|
|
|
} |
325
|
|
|
protected static function admin_modules_cleanup () { |
326
|
|
|
clean_pcache(); |
327
|
|
|
$Cache = System_cache::instance(); |
328
|
|
|
unset( |
329
|
|
|
$Cache->functionality, |
330
|
|
|
$Cache->languages |
331
|
|
|
); |
332
|
|
|
clean_classes_cache(); |
333
|
|
|
} |
334
|
|
|
/** |
335
|
|
|
* Disable module |
336
|
|
|
* |
337
|
|
|
* Provides next events: |
338
|
|
|
* admin/System/modules/disable/before |
339
|
|
|
* ['name' => module_name] |
340
|
|
|
* |
341
|
|
|
* admin/System/modules/disable/after |
342
|
|
|
* ['name' => module_name] |
343
|
|
|
* |
344
|
|
|
* @param \cs\Request $Request |
345
|
|
|
* |
346
|
|
|
* @throws ExitException |
347
|
|
|
*/ |
348
|
|
|
public static function admin_modules_disable ($Request) { |
349
|
|
|
$Config = Config::instance(); |
350
|
|
|
$module = $Request->route_path(2); |
351
|
|
|
if ( |
352
|
|
|
$module == Config::SYSTEM_MODULE || |
353
|
|
|
$Config->core['default_module'] == $module || |
354
|
|
|
!$Config->module($module)->enabled() |
355
|
|
|
) { |
356
|
|
|
throw new ExitException(400); |
357
|
|
|
} |
358
|
|
|
static::admin_modules_disable_internal($Config, $module); |
359
|
|
|
} |
360
|
|
|
/** |
361
|
|
|
* @param Config $Config |
362
|
|
|
* @param string $module |
363
|
|
|
* |
364
|
|
|
* @throws ExitException |
365
|
|
|
*/ |
366
|
|
|
protected static function admin_modules_disable_internal ($Config, $module) { |
367
|
|
|
if (!Event::instance()->fire('admin/System/modules/disable/before', ['name' => $module])) { |
368
|
|
|
throw new ExitException(500); |
369
|
|
|
} |
370
|
|
|
$Config->components['modules'][$module]['active'] = Config\Module_Properties::DISABLED; |
371
|
|
|
static::admin_modules_save(); |
372
|
|
|
Event::instance()->fire('admin/System/modules/disable/after', ['name' => $module]); |
373
|
|
|
static::admin_modules_cleanup(); |
374
|
|
|
} |
375
|
|
|
/** |
376
|
|
|
* Install module |
377
|
|
|
* |
378
|
|
|
* Provides next events: |
379
|
|
|
* admin/System/modules/install/before |
380
|
|
|
* ['name' => module_name] |
381
|
|
|
* |
382
|
|
|
* admin/System/modules/install/after |
383
|
|
|
* ['name' => module_name] |
384
|
|
|
* |
385
|
|
|
* @param \cs\Request $Request |
386
|
|
|
* |
387
|
|
|
* @throws ExitException |
388
|
|
|
*/ |
389
|
|
|
public static function admin_modules_install ($Request) { |
390
|
|
|
$Config = Config::instance(); |
391
|
|
|
$module = $Request->route_path(2); |
392
|
|
|
if (!$Config->module($module)->uninstalled()) { |
393
|
|
|
throw new ExitException(400); |
394
|
|
|
} |
395
|
|
|
if (!Event::instance()->fire('admin/System/modules/install/before', ['name' => $module])) { |
396
|
|
|
throw new ExitException(500); |
397
|
|
|
} |
398
|
|
|
$module_data = &$Config->components['modules'][$module]; |
399
|
|
|
$database_config = $Request->data('db'); |
400
|
|
|
if ($database_config) { |
401
|
|
|
$module_data['db'] = _int($database_config); |
402
|
|
|
Packages_manipulation::execute_sql_from_directory(MODULES."/$module/meta/install_db", $module_data['db']); |
403
|
|
|
} |
404
|
|
|
$storage_config = $Request->data('storage'); |
405
|
|
|
if ($storage_config) { |
406
|
|
|
$module_data['storage'] = _int($storage_config); |
407
|
|
|
} |
408
|
|
|
$module_data['active'] = Config\Module_Properties::DISABLED; |
409
|
|
|
static::admin_modules_save(); |
410
|
|
|
Event::instance()->fire('admin/System/modules/install/after', ['name' => $module]); |
411
|
|
|
static::admin_modules_cleanup(); |
412
|
|
|
} |
413
|
|
|
/** |
414
|
|
|
* Uninstall module |
415
|
|
|
* |
416
|
|
|
* Provides next events: |
417
|
|
|
* admin/System/modules/uninstall/before |
418
|
|
|
* ['name' => module_name] |
419
|
|
|
* |
420
|
|
|
* admin/System/modules/uninstall/after |
421
|
|
|
* ['name' => module_name] |
422
|
|
|
* |
423
|
|
|
* @param \cs\Request $Request |
424
|
|
|
* |
425
|
|
|
* @throws ExitException |
426
|
|
|
*/ |
427
|
|
|
public static function admin_modules_uninstall ($Request) { |
428
|
|
|
$Config = Config::instance(); |
429
|
|
|
$module = $Request->route_path(2); |
430
|
|
|
$modules = &$Config->components['modules']; |
431
|
|
|
/** |
432
|
|
|
* Do not allow to uninstall enabled module, it should be explicitly disabled first |
433
|
|
|
*/ |
434
|
|
|
if (!$Config->module($module)->disabled()) { |
435
|
|
|
throw new ExitException(400); |
436
|
|
|
} |
437
|
|
|
if (!Event::instance()->fire('admin/System/modules/uninstall/before', ['name' => $module])) { |
438
|
|
|
throw new ExitException(500); |
439
|
|
|
} |
440
|
|
|
$module_data = &$modules[$module]; |
441
|
|
|
if (isset($module_data['db'])) { |
442
|
|
|
Packages_manipulation::execute_sql_from_directory(MODULES."/$module/meta/uninstall_db", $module_data['db']); |
443
|
|
|
} |
444
|
|
|
static::delete_permissions_for_module($module); |
445
|
|
|
$modules[$module] = ['active' => Config\Module_Properties::UNINSTALLED]; |
446
|
|
|
static::admin_modules_save(); |
447
|
|
|
Event::instance()->fire('admin/System/modules/uninstall/after', ['name' => $module]); |
448
|
|
|
static::admin_modules_cleanup(); |
449
|
|
|
} |
450
|
|
|
/** |
451
|
|
|
* @param string $module |
452
|
|
|
*/ |
453
|
|
|
protected static function delete_permissions_for_module ($module) { |
454
|
|
|
$Permission = Permission::instance(); |
455
|
|
|
$permissions_ids = array_merge( |
456
|
|
|
$Permission->get(null, $module), |
457
|
|
|
$Permission->get(null, "admin/$module"), |
458
|
|
|
$Permission->get(null, "api/$module") |
459
|
|
|
); |
460
|
|
|
if (!empty($permissions_ids)) { |
461
|
|
|
$Permission->del( |
462
|
|
|
array_column($permissions_ids, 'id') |
463
|
|
|
); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
/** |
467
|
|
|
* Extract uploaded module |
468
|
|
|
* |
469
|
|
|
* @throws ExitException |
470
|
|
|
*/ |
471
|
|
|
public static function admin_modules_extract () { |
472
|
|
|
$Config = Config::instance(); |
473
|
|
|
$L = Language::prefix('system_admin_modules_'); |
474
|
|
|
$tmp_location = TEMP.'/System/admin/'.Session::instance()->get_id().'.phar'; |
475
|
|
|
$tmp_dir = "phar://$tmp_location"; |
476
|
|
|
if ( |
477
|
|
|
!file_exists($tmp_location) || |
478
|
|
|
!file_exists("$tmp_dir/meta.json") |
479
|
|
|
) { |
480
|
|
|
throw new ExitException(400); |
481
|
|
|
} |
482
|
|
|
$new_meta = file_get_json("$tmp_dir/meta.json"); |
483
|
|
|
if ($new_meta['category'] !== 'modules') { |
484
|
|
|
throw new ExitException($L->this_is_not_module_installer_file, 400); |
485
|
|
|
} |
486
|
|
|
if (!Packages_manipulation::install_extract(MODULES."/$new_meta[package]", $tmp_location)) { |
487
|
|
|
throw new ExitException($L->module_files_unpacking_error, 500); |
488
|
|
|
} |
489
|
|
|
$Config->components['modules'][$new_meta['package']] = ['active' => Config\Module_Properties::UNINSTALLED]; |
490
|
|
|
ksort($Config->components['modules'], SORT_STRING | SORT_FLAG_CASE); |
491
|
|
|
static::admin_modules_save(); |
492
|
|
|
static::admin_modules_cleanup(); |
493
|
|
|
} |
494
|
|
|
/** |
495
|
|
|
* Update module (or system if module name is System) |
496
|
|
|
* |
497
|
|
|
* @param \cs\Request $Request |
498
|
|
|
* |
499
|
|
|
* @throws ExitException |
500
|
|
|
*/ |
501
|
|
|
public static function admin_modules_update ($Request) { |
502
|
|
|
$module = $Request->route_path(2); |
503
|
|
|
if (!Config::instance()->module($module)) { |
504
|
|
|
throw new ExitException(404); |
505
|
|
|
} |
506
|
|
|
$tmp_location = TEMP.'/System/admin/'.Session::instance()->get_id().'.phar'; |
507
|
|
|
$tmp_dir = "phar://$tmp_location"; |
508
|
|
|
$module_dir = MODULES."/$module"; |
509
|
|
|
if ( |
510
|
|
|
!file_exists($tmp_location) || |
511
|
|
|
!file_exists("$module_dir/meta.json") || |
512
|
|
|
!file_exists("$tmp_dir/meta.json") |
513
|
|
|
) { |
514
|
|
|
throw new ExitException(400); |
515
|
|
|
} |
516
|
|
|
$existing_meta = file_get_json("$module_dir/meta.json"); |
517
|
|
|
$new_meta = file_get_json("$tmp_dir/meta.json"); |
518
|
|
|
if ($module == Config::SYSTEM_MODULE) { |
519
|
|
|
static::update_system($module, $existing_meta, $new_meta, $tmp_location); |
520
|
|
|
} else { |
521
|
|
|
static::update_module($module, $existing_meta, $new_meta, $tmp_location, $Request); |
522
|
|
|
} |
523
|
|
|
static::admin_modules_cleanup(); |
524
|
|
|
} |
525
|
|
|
/** |
526
|
|
|
* Provides next events: |
527
|
|
|
* admin/System/modules/update/before |
528
|
|
|
* ['name' => module_name] |
529
|
|
|
* |
530
|
|
|
* admin/System/modules/update/after |
531
|
|
|
* ['name' => module_name] |
532
|
|
|
* |
533
|
|
|
* @param string $module |
534
|
|
|
* @param array $existing_meta |
535
|
|
|
* @param array $new_meta |
536
|
|
|
* @param string $tmp_location |
537
|
|
|
* @param \cs\Request $Request |
538
|
|
|
* |
539
|
|
|
* @throws ExitException |
540
|
|
|
*/ |
541
|
|
|
protected static function update_module ($module, $existing_meta, $new_meta, $tmp_location, $Request) { |
542
|
|
|
$Config = Config::instance(); |
543
|
|
|
$L = Language::prefix('system_admin_modules_'); |
544
|
|
|
$module_dir = MODULES."/$module"; |
545
|
|
|
$enabled = $Config->module($module)->enabled(); |
546
|
|
|
// If module is currently enabled - disable it temporary |
547
|
|
|
if ($enabled) { |
548
|
|
|
static::admin_modules_disable_internal($Config, $module); |
549
|
|
|
} |
550
|
|
|
if (!static::is_same_module($new_meta, $module)) { |
551
|
|
|
throw new ExitException($L->this_is_not_module_installer_file, 400); |
552
|
|
|
} |
553
|
|
|
if (!Event::instance()->fire('admin/System/modules/update/before', ['name' => $module])) { |
554
|
|
|
throw new ExitException(500); |
555
|
|
|
} |
556
|
|
|
if (!is_writable($module_dir)) { |
557
|
|
|
throw new ExitException($L->cant_unpack_module_no_write_permissions, 500); |
558
|
|
|
} |
559
|
|
|
if (!Packages_manipulation::update_extract($module_dir, $tmp_location)) { |
560
|
|
|
throw new ExitException($L->module_files_unpacking_error, 500); |
561
|
|
|
} |
562
|
|
|
$module_data = $Config->components['modules'][$module]; |
563
|
|
|
// Run PHP update scripts and SQL queries if any |
564
|
|
|
Packages_manipulation::update_php_sql($module_dir, $existing_meta['version'], isset($module_data['db']) ? $module_data['db'] : null); |
565
|
|
|
Event::instance()->fire('admin/System/modules/update/after', ['name' => $module]); |
566
|
|
|
// If module was enabled before update - enable it back |
567
|
|
|
if ($enabled) { |
568
|
|
|
static::admin_modules_enable($Request); |
569
|
|
|
} |
570
|
|
|
} |
571
|
|
|
/** |
572
|
|
|
* Provides next events: |
573
|
|
|
* admin/System/modules/update_system/before |
574
|
|
|
* |
575
|
|
|
* admin/System/modules/update_system/after |
576
|
|
|
* |
577
|
|
|
* @param string $module |
578
|
|
|
* @param array $existing_meta |
579
|
|
|
* @param array $new_meta |
580
|
|
|
* @param string $tmp_location |
581
|
|
|
* |
582
|
|
|
* @throws ExitException |
583
|
|
|
*/ |
584
|
|
|
protected static function update_system ($module, $existing_meta, $new_meta, $tmp_location) { |
585
|
|
|
$Config = Config::instance(); |
586
|
|
|
$L = Language::prefix('system_admin_modules_'); |
587
|
|
|
$module_dir = MODULES."/$module"; |
588
|
|
|
/** |
589
|
|
|
* Temporary close site |
590
|
|
|
*/ |
591
|
|
|
$site_mode = $Config->core['site_mode']; |
592
|
|
|
$Config->core['site_mode'] = 0; |
593
|
|
|
static::admin_modules_save(); |
594
|
|
|
if (!static::is_same_module($new_meta, Config::SYSTEM_MODULE)) { |
595
|
|
|
throw new ExitException($L->this_is_not_system_installer_file, 400); |
596
|
|
|
} |
597
|
|
|
if (!Event::instance()->fire('admin/System/modules/update_system/before')) { |
598
|
|
|
throw new ExitException(500); |
599
|
|
|
} |
600
|
|
|
if (!Packages_manipulation::update_extract(DIR, $tmp_location, DIR.'/core', $module_dir)) { |
601
|
|
|
throw new ExitException($L->system_files_unpacking_error, 500); |
602
|
|
|
} |
603
|
|
|
$module_data = $Config->components['modules'][$module]; |
604
|
|
|
// Run PHP update scripts and SQL queries if any |
605
|
|
|
Packages_manipulation::update_php_sql($module_dir, $existing_meta['version'], isset($module_data['db']) ? $module_data['db'] : null); |
606
|
|
|
Event::instance()->fire('admin/System/modules/update_system/after'); |
607
|
|
|
/** |
608
|
|
|
* Restore previous site mode |
609
|
|
|
*/ |
610
|
|
|
$Config->core['site_mode'] = $site_mode; |
611
|
|
|
static::admin_modules_save(); |
612
|
|
|
static::admin_modules_cleanup(); |
613
|
|
|
} |
614
|
|
|
/** |
615
|
|
|
* Delete module completely |
616
|
|
|
* |
617
|
|
|
* @param \cs\Request $Request |
618
|
|
|
* |
619
|
|
|
* @throws ExitException |
620
|
|
|
*/ |
621
|
|
|
public static function admin_modules_delete ($Request) { |
622
|
|
|
$Config = Config::instance(); |
623
|
|
|
$module = $Request->route_path(2); |
624
|
|
|
if (!$Config->module($module)->uninstalled()) { |
625
|
|
|
throw new ExitException(400); |
626
|
|
|
} |
627
|
|
|
if (!rmdir_recursive(MODULES."/$module")) { |
628
|
|
|
throw new ExitException(500); |
629
|
|
|
} |
630
|
|
|
unset($Config->components['modules'][$module]); |
631
|
|
|
static::admin_modules_save(); |
632
|
|
|
} |
633
|
|
|
/** |
634
|
|
|
* Update information about present modules |
635
|
|
|
* |
636
|
|
|
* @throws ExitException |
637
|
|
|
*/ |
638
|
|
|
public static function admin_modules_update_list () { |
639
|
|
|
$Config = Config::instance(); |
640
|
|
|
/** |
641
|
|
|
* List of currently presented modules in file system |
642
|
|
|
*/ |
643
|
|
|
$modules_in_fs = get_files_list(MODULES, false, 'd'); |
644
|
|
|
$modules_list = array_fill_keys( |
645
|
|
|
$modules_in_fs, |
646
|
|
|
[ |
647
|
|
|
'active' => Config\Module_Properties::UNINSTALLED, |
648
|
|
|
'db' => [], |
649
|
|
|
'storage' => [] |
650
|
|
|
] |
651
|
|
|
); |
652
|
|
|
/** |
653
|
|
|
* Already known modules |
654
|
|
|
*/ |
655
|
|
|
$modules = &$Config->components['modules']; |
656
|
|
|
$known_modules = array_keys($modules); |
657
|
|
|
if ($modules_in_fs != $known_modules) { |
658
|
|
|
/** |
659
|
|
|
* Delete permissions of modules that are mot present anymore |
660
|
|
|
*/ |
661
|
|
|
foreach ($known_modules as $module) { |
662
|
|
|
if (!isset($modules_list[$module])) { |
663
|
|
|
static::delete_permissions_for_module($module); |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
unset($module); |
667
|
|
|
} |
668
|
|
|
unset($modules_in_fs, $known_modules); |
669
|
|
|
$modules = array_merge($modules_list, array_intersect_key($modules, $modules_list)); |
670
|
|
|
ksort($modules, SORT_STRING | SORT_FLAG_CASE); |
671
|
|
|
static::admin_modules_save(); |
672
|
|
|
static::admin_modules_cleanup(); |
673
|
|
|
} |
674
|
|
|
/** |
675
|
|
|
* Save changes |
676
|
|
|
* |
677
|
|
|
* @throws ExitException |
678
|
|
|
*/ |
679
|
|
|
protected static function admin_modules_save () { |
680
|
|
|
if (!Config::instance()->save()) { |
681
|
|
|
throw new ExitException(500); |
682
|
|
|
} |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|