-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
plugin.class.php
2209 lines (1934 loc) · 67.4 KB
/
plugin.class.php
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
255
256
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* ---------------------------------------------------------------------
* GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2015-2018 Teclib' and contributors.
*
* http://glpi-project.org
*
* based on GLPI - Gestionnaire Libre de Parc Informatique
* Copyright (C) 2003-2014 by the INDEPNET Development Team.
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* GLPI is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GLPI. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
*/
/**
* Based on cacti plugin system
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
use Psr\SimpleCache\CacheInterface;
class Plugin extends CommonDBTM {
// Class constant : Plugin state
const ANEW = 0;
const ACTIVATED = 1;
const NOTINSTALLED = 2;
const TOBECONFIGURED = 3;
const NOTACTIVATED = 4;
const TOBECLEANED = 5;
const NOTUPDATED = 6;
static $rightname = 'config';
/**
* Plugin init state.
*
* @var boolean
*/
private static $plugins_init = false;
/**
* Loaded plugin list, indexed by their ID.
*
* @var null|string[]
*/
private static $loaded_plugins;
static function getTypeName($nb = 0) {
return _n('Plugin', 'Plugins', $nb);
}
static function getMenuName() {
return static::getTypeName(Session::getPluralNumber());
}
static function getMenuContent() {
global $CFG_GLPI;
$menu = [];
if (static::canView()) {
$menu['title'] = self::getMenuName();
$menu['page'] = '/front/plugin.php';
}
if (count($menu)) {
return $menu;
}
return false;
}
/**
* Retrieve an item from the database using its directory
*
* @param string $dir directory of the plugin
*
* @return boolean
**/
function getFromDBbyDir($dir) {
return $this->getFromDBByCrit([$this->getTable() . '.directory' => $dir]);
}
/**
* Init plugins list.
*
* @return void
**/
function init() {
global $DB;
self::$plugins_init = false;
self::$loaded_plugins = [];
if (!isset($DB) || !$DB->connected) {
// Cannot init plugins list if DB is not connected
self::$plugins_init = true;
return;
}
$this->checkStates();
$plugins = $this->find(['state' => self::ACTIVATED]);
self::$plugins_init = true;
if (count($plugins)) {
foreach ($plugins as $ID => $plug) {
$this->setLoaded($ID, $plug['directory']);
}
}
}
/**
* Are plugin initialized (Plugin::Init() called)
*
* @return boolean
*
* @deprecated 9.4.1
*/
public static function hasBeenInit() {
Toolbox::deprecated();
return self::$plugins_init;
}
/**
* Init a plugin including setup.php file
* launching plugin_init_NAME function after checking compatibility
*
* @param $name Name of hook to use
* @param $withhook boolean to load hook functions (false by default)
*
* @return nothing
**/
static function load($name, $withhook = false) {
global $LOADED_PLUGINS;
if (file_exists(GLPI_ROOT . "/plugins/$name/setup.php")) {
include_once(GLPI_ROOT . "/plugins/$name/setup.php");
if (!isset($LOADED_PLUGINS[$name])) {
self::loadLang($name);
$function = "plugin_init_$name";
if (function_exists($function)) {
$function();
$LOADED_PLUGINS[$name] = $name;
}
}
}
if ($withhook
&& file_exists(GLPI_ROOT . "/plugins/$name/hook.php")) {
include_once(GLPI_ROOT . "/plugins/$name/hook.php");
}
}
/**
* Load lang file for a plugin
*
* @param $name Name of hook to use
* @param $forcelang force a specific lang (default '')
* @param $coretrytoload lang trying to be load from core (default '')
*
* @return nothing
**/
static function loadLang($name, $forcelang = '', $coretrytoload = '') {
// $LANG needed : used when include lang file
global $CFG_GLPI, $LANG, $TRANSLATE;
// For compatibility for plugins using $LANG
$trytoload = 'en_GB';
if (isset($_SESSION['glpilanguage'])) {
$trytoload = $_SESSION["glpilanguage"];
}
// Force to load a specific lang
if (!empty($forcelang)) {
$trytoload = $forcelang;
}
// If not set try default lang file
if (empty($trytoload)) {
$trytoload = $CFG_GLPI["language"];
}
if (empty($coretrytoload)) {
$coretrytoload = $trytoload;
}
$dir = GLPI_ROOT . "/plugins/$name/locales/";
$translation_included = false;
// New localisation system
if (file_exists($dir.$CFG_GLPI["languages"][$trytoload][1])) {
$TRANSLATE->addTranslationFile('gettext',
$dir.$CFG_GLPI["languages"][$trytoload][1],
$name, $coretrytoload);
$translation_included = true;
} else if (!empty($CFG_GLPI["language"])
&& file_exists($dir.$CFG_GLPI["languages"][$CFG_GLPI["language"]][1])) {
$TRANSLATE->addTranslationFile('gettext',
$dir.$CFG_GLPI["languages"][$CFG_GLPI["language"]][1],
$name, $coretrytoload);
$translation_included = true;
} else if (file_exists($dir."en_GB.mo")) {
$TRANSLATE->addTranslationFile('gettext',
$dir."en_GB.mo",
$name, $coretrytoload);
$translation_included = true;
}
if (!$translation_included) {
if (file_exists($dir.$trytoload.'.php')) {
include ($dir.$trytoload.'.php');
} else if (isset($CFG_GLPI["language"])
&& file_exists($dir.$CFG_GLPI["language"].'.php')) {
include ($dir.$CFG_GLPI["language"].'.php');
} else if (file_exists($dir . "en_GB.php")) {
include ($dir . "en_GB.php");
} else if (file_exists($dir . "fr_FR.php")) {
include ($dir . "fr_FR.php");
}
}
}
/**
* Check plugins states and detect new plugins.
*
* @param boolean $scan_inactive_and_new_plugins
*
* @return void
*/
public function checkStates($scan_inactive_and_new_plugins = false) {
$directories = [];
// Add known plugins to the check list
$condition = $scan_inactive_and_new_plugins ? [] : ['state' => self::ACTIVATED];
$known_plugins = $this->find($condition);
foreach ($known_plugins as $plugin) {
$directories[] = $plugin['directory'];
}
if ($scan_inactive_and_new_plugins) {
// Add found directories to the check list
$plugins_directory = GLPI_ROOT."/plugins";
$directory_handle = opendir($plugins_directory);
while (false !== ($filename = readdir($directory_handle))) {
if (!in_array($filename, ['.svn', '.', '..'])
&& is_dir($plugins_directory . DIRECTORY_SEPARATOR . $filename)) {
$directories[] = $filename;
}
}
}
// Prevent duplicated checks
$directories = array_unique($directories);
// Check all directories from the checklist
foreach ($directories as $directory) {
$this->checkPluginState($directory);
}
}
/**
* Check plugin state.
*
* @param string $directory
*
* return void
*/
public function checkPluginState($directory) {
$plugin = new self();
$is_already_known = $plugin->getFromDBByCrit(['directory' => $directory]);
$informations = $this->getInformationsFromDirectory($directory);
if (empty($informations)) {
if (!$is_already_known) {
// Plugin is not known and we are unable to load informations, we ignore it
return;
}
// Plugin is known but we are unable to load informations, it should be cleaned
if ($plugin->fields['state'] == self::TOBECLEANED) {
// unless its state is already self::TOBECLEANED
return;
}
trigger_error(
sprintf(
'Unable to load plugin "%s" informations. Its state has been changed to "To be cleaned".',
$directory
),
E_USER_WARNING
);
$this->update(
[
'id' => $plugin->fields['id'],
'state' => self::TOBECLEANED,
]
);
return;
}
if (!$is_already_known && array_key_exists('oldname', $informations)) {
// Plugin not known but was named differently before, we try to load state using old name
$is_already_known = $plugin->getFromDBByCrit(['directory' => $informations['oldname']]);
}
if (!$is_already_known) {
// Plugin not known, add it in DB
$this->add(
array_merge(
$informations,
[
'state' => self::NOTINSTALLED,
'directory' => $directory,
]
)
);
return;
}
if ($informations['version'] != $plugin->fields['version']
|| $directory != $plugin->fields['directory']) {
// Plugin known version differs from informations or plugin has been renamed,
// update informations in database
$input = $informations;
$input['id'] = $plugin->fields['id'];
$input['directory'] = $directory;
if (!in_array($plugin->fields['state'], [self::ANEW, self::NOTINSTALLED])) {
// mark it as 'updatable' unless it was not installed
trigger_error(
sprintf(
'Plugin "%s" version changed. It has been deactivated as its update process has to be launched.',
$directory
),
E_USER_WARNING
);
$input['state'] = self::NOTUPDATED;
}
$this->update($input);
$this->setUnloadedByName($directory);
// reset menu
if (isset($_SESSION['glpimenu'])) {
unset($_SESSION['glpimenu']);
}
return;
}
if (self::ACTIVATED !== (int)$plugin->fields['state']) {
// Plugin is not activated, nothing to do
return;
}
// Check that active state of plugin can be kept
$usage_ok = true;
// Check compatibility
ob_start();
if (!$this->checkVersions($directory)) {
$usage_ok = false;
}
ob_end_clean();
// Check prerequisites
if ($usage_ok) {
$function = 'plugin_' . $directory . '_check_prerequisites';
if (function_exists($function)) {
ob_start();
if (!$function()) {
$usage_ok = false;
}
ob_end_clean();
}
}
// Check configuration
if ($usage_ok) {
$function = 'plugin_' . $directory . '_check_config';
if (!function_exists($function) || !$function()) {
$usage_ok = false;
}
}
if (!$usage_ok) {
// Deactivate if not usable
trigger_error(
sprintf(
'Plugin "%s" prerequisites are not matched. It has been deactivated.',
$directory
),
E_USER_WARNING
);
$this->unactivate($plugin->fields['id']);
}
}
/**
* Get list of all plugins
*
* @param array $fields Fields to retrieve
* @param array $order Query ORDER clause
*
* @return array
*/
public function getList(array $fields = [], array $order = ['name', 'directory']) {
global $DB;
$query = [
'FROM' => $this->getTable()
];
if (count($fields) > 0) {
$query['FIELDS'] = $fields;
}
if (count($order) > 0) {
$query['ORDER'] = $order;
}
$iterator = $DB->request($query);
return iterator_to_array($iterator, false);
}
/**
* uninstall a plugin
*
* @param $ID ID of the plugin
**/
function uninstall($ID) {
$message = '';
$type = ERROR;
if ($this->getFromDB($ID)) {
CronTask::Unregister($this->fields['directory']);
self::load($this->fields['directory'], true);
FieldUnicity::deleteForItemtype($this->fields['directory']);
Link_Itemtype::deleteForItemtype($this->fields['directory']);
// Run the Plugin's Uninstall Function first
$function = 'plugin_' . $this->fields['directory'] . '_uninstall';
if (function_exists($function)) {
self::setLoaded('temp', $this->fields['directory']); // For autoloader
$function();
self::setUnloaded('temp');
} else {
Session::addMessageAfterRedirect(
sprintf(__('Plugin %1$s has no uninstall function!'), $this->fields['name']),
true,
WARNING
);
}
$this->update([
'id' => $ID,
'state' => self::NOTINSTALLED,
]);
$this->setUnloadedByName($this->fields['directory']);
$type = INFO;
$message = sprintf(__('Plugin %1$s has been uninstalled!'), $this->fields['name']);
} else {
$message = sprintf(__('Plugin %1$s not found!'), $ID);
}
Session::addMessageAfterRedirect(
$message,
true,
$type
);
}
/**
* Install a plugin
*
* @param int $ID ID of the plugin
* @param array $params Additionnal params to pass to install hook.
*
* @return void
*
* @since 9.5.0 Added $param parameter
**/
function install($ID, array $params = []) {
global $DB;
$message = '';
$type = ERROR;
if ($this->getFromDB($ID)) {
// Clear locale cache to prevent errors while reloading plugin locales
$translation_cache = Config::getCache('cache_trans', 'core', true);
if ($translation_cache instanceof CacheInterface) {
$translation_cache->clear();
}
self::load($this->fields['directory'], true);
$install_function = 'plugin_' . $this->fields['directory'] . '_install';
if (function_exists($install_function)) {
$this->setLoaded('temp', $this->fields['directory']); // For autoloader
$DB->disableTableCaching(); //prevents issues on table/fieldExists upgrading from old versions
if ($install_function($params)) {
$type = INFO;
$check_function = 'plugin_' . $this->fields['directory'] . '_check_config';
if (function_exists($check_function)) {
if ($check_function()) {
$this->update(['id' => $ID,
'state' => self::NOTACTIVATED]);
$message = sprintf(__('Plugin %1$s has been installed!'), $this->fields['name']);
$message .= '<br/><br/>' . str_replace(
'%activate_link',
Html::getSimpleForm(static::getFormURL(), ['action' => 'activate'],
mb_strtolower(_x('button', 'Enable')), ['id' => $ID], '', 'class="pointer"'),
__('Do you want to %activate_link it?')
);
} else {
$this->update(['id' => $ID,
'state' => self::TOBECONFIGURED]);
$message = sprintf(__('Plugin %1$s has been installed and must be configured!'), $this->fields['name']);
}
}
}
$this->setUnloaded('temp');
} else {
$type = WARNING;
$message = sprintf(__('Plugin %1$s has no install function!'), $this->fields['name']);
}
} else {
$message = sprintf(__('Plugin %1$s not found!'), $ID);
}
Session::addMessageAfterRedirect(
$message,
true,
$type
);
}
/**
* activate a plugin
*
* @param $ID ID of the plugin
*
* @return boolean about success
**/
function activate($ID) {
global $PLUGIN_HOOKS;
if ($this->getFromDB($ID)) {
self::load($this->fields['directory'], true);
// No activation if not CSRF compliant
if (!isset($PLUGIN_HOOKS['csrf_compliant'][$this->fields['directory']])
|| !$PLUGIN_HOOKS['csrf_compliant'][$this->fields['directory']]) {
Session::addMessageAfterRedirect(
sprintf(__('Plugin %1$s is not CSRF compliant!'), $this->fields['name']),
true,
ERROR
);
return false;
}
// Enable autoloader early, during activation process
$this->setLoaded($ID, $this->fields['directory']);
$function = 'plugin_' . $this->fields['directory'] . '_check_prerequisites';
if (function_exists($function)) {
ob_start();
$do_activate = $function();
$msg = '';
if (!$do_activate) {
$msg = '<span class="error">' . ob_get_contents() . '</span>';
}
ob_end_clean();
if (!$do_activate) {
$this->setUnloaded($ID);
Session::addMessageAfterRedirect(
sprintf(__('Plugin %1$s has no check function!'), $this->fields['name']),
true,
ERROR
);
return false;
}
}
$function = 'plugin_' . $this->fields['directory'] . '_check_config';
if (function_exists($function)) {
if ($function()) {
$this->update(['id' => $ID,
'state' => self::ACTIVATED]);
// Initialize session for the plugin
if (isset($PLUGIN_HOOKS['init_session'][$this->fields['directory']])
&& is_callable($PLUGIN_HOOKS['init_session'][$this->fields['directory']])) {
call_user_func($PLUGIN_HOOKS['init_session'][$this->fields['directory']]);
}
// Initialize profile for the plugin
if (isset($PLUGIN_HOOKS['change_profile'][$this->fields['directory']])
&& is_callable($PLUGIN_HOOKS['change_profile'][$this->fields['directory']])) {
call_user_func($PLUGIN_HOOKS['change_profile'][$this->fields['directory']]);
}
// reset menu
if (isset($_SESSION['glpimenu'])) {
unset($_SESSION['glpimenu']);
}
Session::addMessageAfterRedirect(
sprintf(__('Plugin %1$s has been activated!'), $this->fields['name']),
true,
INFO
);
return true;
}
} // exists _check_config
// Failure so remove it
$this->setUnloaded($ID);
} // getFromDB
Session::addMessageAfterRedirect(
sprintf(__('Plugin %1$s not found!'), $ID),
true,
ERROR
);
return false;
}
/**
* unactivate a plugin
*
* @param $ID ID of the plugin
*
* @return boolean
**/
function unactivate($ID) {
if ($this->getFromDB($ID)) {
$this->update([
'id' => $ID,
'state' => self::NOTACTIVATED
]);
$this->setUnloadedByName($this->fields['directory']);
// reset menu
if (isset($_SESSION['glpimenu'])) {
unset($_SESSION['glpimenu']);
}
Session::addMessageAfterRedirect(
sprintf(__('Plugin %1$s has been deactivated!'), $this->fields['name']),
true,
INFO
);
return true;
}
Session::addMessageAfterRedirect(
sprintf(__('Plugin %1$s not found!'), $ID),
true,
ERROR
);
return false;
}
/**
* unactivate all activated plugins for update process
**/
function unactivateAll() {
global $DB;
$DB->update(
$this->getTable(), [
'state' => self::NOTACTIVATED
], [
'state' => self::ACTIVATED
]
);
self::$loaded_plugins = [];
// reset menu
if (isset($_SESSION['glpimenu'])) {
unset($_SESSION['glpimenu']);
}
}
/**
* clean a plugin
*
* @param $ID ID of the plugin
**/
function clean($ID) {
if ($this->getFromDB($ID)) {
// Clean crontask after "hard" remove
CronTask::Unregister($this->fields['directory']);
$this->delete(['id' => $ID]);
$this->setUnloadedByName($this->fields['directory']);
}
}
/**
* is a plugin activated
*
* @param $plugin plugin directory
**/
function isActivated($plugin) {
if ($this->isPluginLoaded($plugin)) {
// If plugin is loaded, it is because it is active. No need to query DB on this case.
return true;
}
// If plugin is not loaded, check on DB as plugins may have not been loaded yet.
if ($this->getFromDBbyDir($plugin)) {
return ($this->fields['state'] == self::ACTIVATED);
}
return false;
}
/**
* is a plugin installed
*
* @param $plugin plugin directory
**/
function isInstalled($plugin) {
if ($this->isPluginLoaded($plugin)) {
// If plugin is loaded, it is because it is installed and active. No need to query DB on this case.
return true;
}
// If plugin is not loaded, check on DB as plugins may have not been loaded yet.
if ($this->getFromDBbyDir($plugin)) {
return (($this->fields['state'] == self::ACTIVATED)
|| ($this->fields['state'] == self::TOBECONFIGURED)
|| ($this->fields['state'] == self::NOTACTIVATED));
}
}
/**
* Migrate itemtype from integer (0.72) to string (0.80)
*
* @param $types array of (num=>name) of type manage by the plugin
* @param $glpitables array of GLPI table name used by the plugin
* @param $plugtables array of Plugin table name which have an itemtype
*
* @return nothing
**/
static function migrateItemType($types = [], $glpitables = [], $plugtables = []) {
global $DB;
$typetoname = [0 => "",// For tickets
1 => "Computer",
2 => "NetworkEquipment",
3 => "Printer",
4 => "Monitor",
5 => "Peripheral",
6 => "Software",
7 => "Contact",
8 => "Supplier",
9 => "Infocom",
10 => "Contract",
11 => "CartridgeItem",
12 => "DocumentType",
13 => "Document",
14 => "KnowbaseItem",
15 => "User",
16 => "Ticket",
17 => "ConsumableItem",
18 => "Consumable",
19 => "Cartridge",
20 => "SoftwareLicense",
21 => "Link",
22 => "State",
23 => "Phone",
24 => "Device",
25 => "Reminder",
26 => "Stat",
27 => "Group",
28 => "Entity",
29 => "ReservationItem",
30 => "AuthMail",
31 => "AuthLDAP",
32 => "OcsServer",
33 => "RegistryKey",
34 => "Profile",
35 => "MailCollector",
36 => "Rule",
37 => "Transfer",
38 => "SavedSearch",
39 => "SoftwareVersion",
40 => "Plugin",
41 => "Item_Disk",
42 => "NetworkPort",
43 => "TicketFollowup",
44 => "Budget"];
// Filter tables that does not exists or does not contains an itemtype field.
// This kind of case exist when current method is called from plugins that based their
// logic on an old GLPI datamodel that may have changed upon time.
// see https://github.com/pluginsGLPI/order/issues/111
$glpitables = array_filter(
$glpitables,
function ($table) use ($DB) {
return $DB->tableExists($table) && $DB->fieldExists($table, 'itemtype');
}
);
//Add plugins types
$typetoname = self::doHookFunction("migratetypes", $typetoname);
foreach ($types as $num => $name) {
$typetoname[$num] = $name;
foreach ($glpitables as $table) {
$DB->updateOrDie(
$table, [
'itemtype' => $name,
], [
'itemtype' => $num
],
"update itemtype of table $table for $name"
);
}
}
if (in_array('glpi_infocoms', $glpitables)) {
$entities = getAllDatasFromTable('glpi_entities');
$entities[0] = "Root";
foreach ($types as $num => $name) {
$itemtable = getTableForItemType($name);
if (!$DB->tableExists($itemtable)) {
// Just for security, shouldn't append
continue;
}
$do_recursive = false;
if ($DB->fieldExists($itemtable, 'is_recursive')) {
$do_recursive = true;
}
foreach ($entities as $entID => $val) {
if ($do_recursive) {
// Non recursive ones
$sub_query = new \QuerySubQuery([
'SELECT' => 'id',
'FROM' => $itemtable,
'WHERE' => [
'entities_id' => $entID,
'is_recursive' => 0
]
]);
$DB->updateOrDie(
'glpi_infocoms', [
'entities_id' => $entID,
'is_recursive' => 0
], [
'itemtype' => $name,
'items_id' => $sub_query
],
"update entities_id and is_recursive=0 in glpi_infocoms for $name"
);
// Recursive ones
$sub_query = new \QuerySubQuery([
'SELECT' => 'id',
'FROM' => $itemtable,
'WHERE' => [
'entities_id' => $entID,
'is_recursive' => 1
]
]);
$DB->updateOrDie(
'glpi_infocoms', [
'entities_id' => $entID,
'is_recursive' => 1
], [
'itemtype' => $name,
'items_id' => $sub_query
],
"update entities_id and is_recursive=1 in glpi_infocoms for $name"
);
} else {
$sub_query = new \QuerySubQuery([
'SELECT' => 'id',
'FROM' => $itemtable,
'WHERE' => [
'entities_id' => $entID,
]
]);
$DB->updateOrDie(
'glpi_infocoms', [
'entities_id' => $entID
], [
'itemtype' => $name,
'items_id' => $sub_query
],
"update entities_id in glpi_infocoms for $name"
);
}
} // each entity
} // each plugin type
}
foreach ($typetoname as $num => $name) {
foreach ($plugtables as $table) {
$DB->updateOrDie(
$table, [
'itemtype' => $name
], [
'itemtype' => $num
],
"update itemtype of table $table for $name"
);
}
}
}
/**
* @param $width
**/
function showSystemInformations($width) {
// No need to translate, this part always display in english (for copy/paste to forum)
echo "\n<tr class='tab_bg_2'><th>Plugins list</th></tr>";
echo "<tr class='tab_bg_1'><td><pre>\n \n";
$plug = new Plugin();
$pluglist = $plug->find([], "name, directory");
foreach ($pluglist as $plugin) {
$msg = substr(str_pad($plugin['directory'], 30), 0, 20).
" Name: ".Toolbox::substr(str_pad($plugin['name'], 40), 0, 30).
" Version: ".str_pad($plugin['version'], 10).
" State: ";
switch ($plugin['state']) {
case self::ANEW :
$msg .= 'New';
break;
case self::ACTIVATED :
$msg .= 'Enabled';
break;
case self::NOTINSTALLED :
$msg .= 'Not installed';
break;