-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathuninstall.class.php
More file actions
1114 lines (953 loc) · 35.7 KB
/
uninstall.class.php
File metadata and controls
1114 lines (953 loc) · 35.7 KB
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
/**
* -------------------------------------------------------------------------
* Uninstall plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Uninstall.
*
* Uninstall 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.
*
* Uninstall 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 Uninstall. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2015-2023 by Teclib'.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/uninstall
* -------------------------------------------------------------------------
*/
use Glpi\Asset\Asset_PeripheralAsset;
use Glpi\Features\AssignableItemInterface;
use function Safe\preg_grep;
/**
* -------------------------------------------------------------------------
* Uninstall plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Uninstall.
*
* Uninstall 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.
*
* Uninstall 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 Uninstall. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2015-2023 by Teclib'.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/uninstall
* -------------------------------------------------------------------------
*/
class PluginUninstallUninstall extends CommonDBTM
{
public const PLUGIN_UNINSTALL_TRANSFER_NAME = "plugin_uninstall";
public static $rightname = "uninstall:profile";
public static function getTypeName($nb = 0)
{
return __("Item's Lifecycle", 'uninstall');
}
/**
* @since version 0.85
*
* @see CommonDBTM::showMassiveActionsSubForm()
**/
public static function showMassiveActionsSubForm(MassiveAction $ma)
{
/** @var array $UNINSTALL_TYPES */
global $UNINSTALL_TYPES;
foreach ($ma->getItems() as $itemtype => $data) {
if (!in_array($itemtype, $UNINSTALL_TYPES)) {
return false;
}
}
if ($ma->getAction() === 'uninstall') {
$uninst = new PluginUninstallUninstall();
$uninst->dropdownUninstallModels(
"model_id",
$_SESSION["glpiID"],
$_SESSION["glpiactive_entity"],
);
echo " "
. Html::submit(_x('button', 'Post'), ['name' => 'massiveaction']);
return true;
}
return false;
}
/**
* @since version 0.85
*
* @see CommonDBTM::processMassiveActionsForOneItemtype()
**/
public static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
{
/**
* @var array $CFG_GLPI
*/
global $CFG_GLPI;
if ($ma->getAction() === "uninstall") {
$itemtype = $ma->getItemtype(false);
foreach ($ids as $id) {
if ($item->getFromDB($id)) {
//Session::addMessageAfterRedirect(sprintf(__s('Form duplicated: %s', 'formcreator'), $item->getName()));
$_SESSION['glpi_uninstalllist'][$itemtype][$id] = $id;
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
}
}
Html::redirect($CFG_GLPI['root_doc'] . '/plugins/uninstall/front/action.php?device_type=' . $itemtype . "&model_id=" . $_POST["model_id"]);
}
}
/**
* Do uninstall process on a single item
*/
private static function doOneUninstall(PluginUninstallModel $model, Transfer $transfer, CommonDBTM $item, array $options = []): void
{
/** @var array $UNINSTALL_DIRECT_CONNECTIONS_TYPE */
global $UNINSTALL_DIRECT_CONNECTIONS_TYPE;
$id = $item->fields['id'];
$type = $options['type'] ?? $item::getType();
$location = $options['location'] ?? '';
$plug = new Plugin();
//First clean object and change location and status if needed
$entity = $item->fields["entities_id"];
$input = [];
$input["id"] = $id;
$input["entities_id"] = $entity;
$fields = [];
//Hook to perform actions before item is being uninstalled
$item->fields['_uninstall_event'] = $model->getID();
$item->fields['_action'] = 'uninstall';
Plugin::doHook("plugin_uninstall_before", $item);
if ($model->fields['raz_glpiinventory'] == 1) {
self::deleteGlpiInventoryLink($item);
}
$input['is_dynamic'] = $item->fields['is_dynamic']; #to prevent locked field
//--------------------//
//Direct connections //
//------------------//
if (in_array($type, $UNINSTALL_DIRECT_CONNECTIONS_TYPE)) {
$conn = new Asset_PeripheralAsset();
$conn->deleteByCriteria(['itemtype' => $type,
'items_id' => $id,
], true);
}
//--------------------//
//-- Common fields --//
//------------------//
//RAZ contact
if ($item->isField('contact') && ($model->fields["raz_contact"] == 1)) {
$fields["contact"] = '';
}
//RAZ contact number
if ($item->isField('contact') && $model->fields["raz_contact_num"] == 1 && $item->isField('contact_num')) {
$fields["contact_num"] = '';
}
//RAZ user
if (($model->fields["raz_user"] == 1) && $item->isField('users_id')) {
$fields["users_id"] = 0;
}
//RAZ status
if (($model->fields["states_id"] > 0) && $item->isField('states_id')) {
$fields["states_id"] = $model->fields["states_id"];
}
//RAZ machine's name
if ($item->isField('name') && ($model->fields["raz_name"] == 1)) {
$fields["name"] = '';
}
if ($item->isField('locations_id')) {
if ($location == '') {
$location = 0;
}
switch ($location) {
case -1:
break;
default:
$fields["locations_id"] = $location;
break;
}
}
if (
$item->isField('groups_id')
|| ($item instanceof AssignableItemInterface)
) {
$nbgroup = countElementsInTableForEntity(
"glpi_groups",
$entity,
['id' => $model->fields['groups_id']],
);
if (
($model->fields["groups_action"] === 'set')
&& ($nbgroup == 1 || $model->fields["groups_id"] == 0)
) {
// If a new group is defined and if the group is accessible in the object's entity
$fields["groups_id"] = $model->fields["groups_id"];
}
}
//------------------------------//
//-- Computer specific fields --//
//------------------------------//
if ($type == 'Computer') {
//RAZ all OS related informations
if (
$model->fields["raz_os"] == 1
&& Item_OperatingSystem::countForItem($item)
) {
$os = new Item_OperatingSystem();
$os->deleteByCriteria(['itemtype' => 'Computer',
'items_id' => $item->fields['id'],
], true);
$fields["autoupdatesystems_id"] = 0;
}
if ($plug->isActivated('ocsinventoryng') && ($item->fields["is_dynamic"] && ($model->fields["remove_from_ocs"] || $model->fields["delete_ocs_link"]))) {
$input["is_dynamic"] = 0;
}
//RAZ network
if ($item->isField('networks_id') && ($model->fields["raz_network"] == 1)) {
$fields["networks_id"] = 0;
}
}
//RAZ IPs from all the network cards
if ($model->fields["raz_ip"] == 1) {
self::razPortInfos($type, $id);
// For NetworkEquiment
if ($item->isField('ip')) {
$fields['ip'] = '';
}
if ($item->isField('mac')) {
$fields['mac'] = '';
}
}
foreach ($fields as $name => $value) {
if (
$item->getField($name) == NOT_AVAILABLE
|| ($item->getField($name) != $value)
) {
$input[$name] = $value;
}
}
$item->dohistory = true;
$item->update($input);
if ($model->fields["raz_budget"] == 1) {
$infocom_id = self::getInfocomPresentForDevice($type, $id);
if ($infocom_id > 0) {
$infocom = new Infocom();
$tmp["id"] = $infocom_id;
$tmp["budgets_id"] = 0;
$infocom->dohistory = false;
$infocom->update($tmp);
}
}
if ($model->fields["raz_domain"]) {
$domain_item = new Domain_Item();
$domain_item->cleanDBonItemDelete($type, $id);
}
//Delete machine from glpi_ocs_link
if ($type == 'Computer') {
//Delete computer's volumes
self::purgeComputerVolumes($id);
//Delete computer antivirus
if ($model->fields["raz_antivirus"] == 1) {
self::purgeComputerAntivirus($id);
}
if ($model->fields["raz_history"] == 1) {
//Delete history related to software
self::deleteHistory($id, false);
} elseif ($model->fields["raz_soft_history"] == 1) {
//Delete history related to software
self::deleteHistory($id, true);
}
if ($plug->isActivated('ocsinventoryng')) {
//Delete computer from OCS
if ($model->fields["remove_from_ocs"] == 1) {
self::deleteComputerInOCSByGlpiID($id);
}
//Delete link in glpi_ocs_link
if ($model->fields["delete_ocs_link"] || $model->fields["remove_from_ocs"]) {
self::deleteOcsLink($id);
}
}
//Should never happend that transfer_id = 0, but just in case
if ($model->fields["transfers_id"] > 0) {
$transfer->moveItems(
[$type => [$id => $id]],
$entity,
$transfer->fields,
);
}
}
if ($plug->isActivated('fusioninventory') && $model->fields['raz_fusioninventory'] == 1) {
self::deleteFusionInventoryLink($type, $id);
}
if ($plug->isActivated('fields') && $model->fields['raz_plugin_fields'] == 1) {
self::deletePluginFieldsLink($type, $id);
}
//Plugin hook after uninstall
Plugin::doHook("plugin_uninstall_after", $item);
}
public static function uninstall($type, $model_id, $tab_ids, $location)
{
new Plugin();
//Get the model
$model = new PluginUninstallModel();
$model->getConfig($model_id);
//Then destroy all the connexions
$transfer = new Transfer();
$transfer->getFromDB($model->fields["transfers_id"]);
echo "<div class='center'>";
echo "<table class='tab_cadre_fixe'><tr><th>" . __s('Uninstall', 'uninstall') . "</th></tr>";
echo "<tr class='tab_bg_2'><td>";
$count = 0;
$tot = count($tab_ids[$type]);
$message = __s('Please wait, uninstallation is running...', 'uninstall');
foreach ($tab_ids[$type] as $id => $value) {
$count++;
if (class_exists($type) && is_a($type, CommonDBTM::class, true)) {
$item = new $type();
$item->getFromDB($id);
self::doOneUninstall($model, $transfer, $item, [
'type' => $type,
'location' => $location,
]);
$percent = (int) (($count / $tot) * 100);
Html::getProgressBar($percent, $message);
//Add line in machine's history to say that machine was uninstalled
self::addUninstallLog([
'itemtype' => $type,
'items_id' => $id,
'models_id' => $model_id,
]);
}
}
echo "</td></tr>";
echo "</table></div>";
}
/**
* Do the configured uninstall action for the item related to the stale agent being cleaned.
*/
public static function doStaleAgentUninstall(CommonDBTM $item): void
{
$stale_agents_uninstall = Config::getConfigurationValue('plugin:uninstall', 'stale_agents_uninstall');
$model = new PluginUninstallModel();
$model->getConfig($stale_agents_uninstall);
$transfer = new Transfer();
$transfer->getFromDB($model->fields["transfers_id"]);
self::doOneUninstall($model, $transfer, $item);
}
/**
* Function to uninstall an object
*
* @param int $computers_id the computer's ID in GLPI
*
* @return void
**/
public static function deleteOcsLink($computers_id)
{
if (class_exists('PluginOcsinventoryngOcslink')) {
$link = new PluginOcsinventoryngOcslink();
$link->dohistory = false;
$link->deleteByCriteria(['computers_id' => $computers_id]);
}
if (class_exists('PluginOcsinventoryngRegistryKey')) {
$reg = new PluginOcsinventoryngRegistryKey();
$reg->deleteByCriteria(['computers_id' => $computers_id]);
}
}
public static function deleteRegistryKeys($computers_id)
{
if (class_exists('PluginOcsinventoryngRegistryKey')) {
$key = new PluginOcsinventoryngRegistryKey();
$key->deleteByCriteria(['computers_id' => $computers_id]);
}
}
/**
* Remove a computer in the OCS database
*
* @param $computer_id the computer's ID in GLPI
*
* @return void
**/
public static function deleteComputerInOCSByGlpiID($computer_id)
{
/** @var DBmysql $DB */
global $DB;
$iterator = $DB->request([
'FROM' => 'glpi_plugin_ocsinventoryng_ocslinks',
'WHERE' => ['computers_id' => $computer_id],
]);
if (count($iterator) === 1) {
$data = $iterator->current();
self::deleteComputerInOCS($data["ocsid"], $data["plugin_ocsinventoryng_ocsservers_id"]);
self::addUninstallLog([
'itemtype' => 'Computer',
'items_id' => $computer_id,
'action' => 'removeFromOCS',
'ocs_id' => $data["ocsid"],
]);
}
}
public static function deleteComputerInOCS($ocs_id, $ocs_server_id)
{
/** @var DBmysql $DB */
global $DB;
if (class_exists('PluginOcsinventoryngOcsServer')) {
$DBocs = PluginOcsinventoryngOcsServer::getDBocs($ocs_server_id)->getDB();
//First try to remove all the network ports
$query = "DELETE
FROM `netmap`
WHERE `MAC` IN (SELECT `MACADDR`
FROM `networks`
WHERE `networks`.`HARDWARE_ID` = '" . $ocs_id . "')";
$DBocs->query($query);
$tables = ["accesslog", "accountinfo", "bios", "controllers", "devices", "drives",
"download_history", "download_servers", "groups_cache", "inputs",
"memories", "modems", "monitors", "networks", "ports", "printers",
"registry", "slots", "softwares", "sounds", "storages", "videos",
];
foreach ($tables as $table) {
if (self::ocsTableExists($ocs_server_id, $table)) {
$query = "DELETE
FROM `" . $table . "`
WHERE `hardware_id` = '" . $ocs_id . "'";
$DBocs->query($query);
}
}
$query = "DELETE
FROM `hardware`
WHERE `ID` = '" . $ocs_id . "'";
$DBocs->query($query);
}
}
public static function ocsTableExists($ocs_server_id, $tablename)
{
if (class_exists('PluginOcsinventoryngOcsServer')) {
$dbClient = PluginOcsinventoryngOcsServer::getDBocs($ocs_server_id);
if (
class_exists('PluginOcsinventoryngOcsDbClient')
&& !($dbClient instanceof PluginOcsinventoryngOcsDbClient)
) {
return false;
}
$DBocs = $dbClient->getDB();
return $DBocs->tableExists($tablename);
}
return false;
}
/**
* Delete information related to the Fields plugin
*
* @param $itemtype the asset type
* @param $items_id the asset's ID in GLPI
*
*/
public static function deletePluginFieldsLink($itemtype, $items_id)
{
if (class_exists('PluginFieldsContainer') && (class_exists($itemtype) && is_a($itemtype, CommonDBTM::class, true))) {
$item = new $itemtype();
$item->getFromDB($items_id);
PluginFieldsContainer::preItemPurge($item);
}
}
/**
* Function to remove FusionInventory information for an asset
*
* @param $itemtype the asset type
* @param $items_id the asset's ID in GLPI
*
* @return void
**/
public static function deleteFusionInventoryLink($itemtype, $items_id)
{
if (class_exists('PluginFusioninventoryAgent') && function_exists('plugin_pre_item_purge_fusioninventory') && (class_exists($itemtype) && is_a($itemtype, CommonDBTM::class, true))) {
$item = new $itemtype();
$item->getFromDB($items_id);
$agent = new PluginFusioninventoryAgent();
$agents = $agent->getAgentsFromComputers([$items_id]);
// clean item associated to agents
plugin_pre_item_purge_fusioninventory($item);
if ($itemtype === 'Computer') {
// remove agent(s)
foreach ($agents as $current_agent) {
$agent->deleteByCriteria(['id' => $current_agent['id']], true);
}
if (class_exists('PluginFusioninventoryComputerLicenseInfo')) {
// remove licences
$pfComputerLicenseInfo = new PluginFusioninventoryComputerLicenseInfo();
$pfComputerLicenseInfo->deleteByCriteria(['computers_id' => $items_id]);
}
}
}
}
/**
* Function to remove GLPI Inventory information for an asset
*
* @param CommonDBTM $item
*
* @return void
**/
public static function deleteGlpiInventoryLink($item)
{
/** @var DBmysql $DB */
global $DB;
$plug = new Plugin();
if ($plug->isActivated('glpiinventory') && function_exists('plugin_pre_item_purge_glpiinventory')) {
// let glpi-inventory to clean item if needed (agent / collect etc ..)
plugin_pre_item_purge_glpiinventory($item);
} else {
$agent = new Agent();
$agent->deleteByCriteria(
[
'itemtype' => $item->getType(),
'items_id' => $item->getID(),
],
true,
);
}
// Purge dynamic computer items
$computer_item = new Asset_PeripheralAsset();
$computer_item->deleteByCriteria(
[
'items_id_asset' => $item->getID(),
'itemtype_asset' => $item->getType(),
'is_dynamic' => 1,
],
true,
);
// purge lock manually because related computer is not purged
$lockedfield = new Lockedfield();
if ($lockedfield->isHandled($item)) {
$lockedfield->itemDeleted();
}
// manage networkname
$networkport = new NetworkPort();
$db_networkport = $networkport->find(["itemtype" => $item->getType(), "items_id" => $item->getID()]);
foreach (array_keys($db_networkport) as $networkport_id) {
$DB->update(
"glpi_networknames",
[
'is_deleted' => 0,
'is_dynamic' => 0,
],
[
"itemtype" => "NetworkPort",
"items_id" => $networkport_id,
'is_dynamic' => 1,
],
);
}
// unlock item relations
$RELATION = getDbRelations();
if (isset($RELATION[$item->getTable()])) {
foreach ($RELATION[$item->getTable()] as $tablename => $fields) {
if ($tablename[0] == '_') {
$tablename = ltrim((string) $tablename, '_');
}
$sub_itemtype = getItemTypeForTable($tablename);
$sub_item = getItemForItemtype($sub_itemtype);
if ($sub_item === false || !$sub_item->maybeDynamic()) {
continue;
}
if (in_array($sub_item::class, [Agent::class, Asset_PeripheralAsset::class, Lockedfield::class, NetworkName::class])) {
// Specific handling
continue;
}
foreach ($fields as $field) {
if (is_array($field)) {
// Relation based on 'itemtype'/'items_id' (polymorphic relationship)
if ($sub_item instanceof IPAddress && in_array('mainitemtype', $field) && in_array('mainitems_id', $field)) {
// glpi_ipaddresses relationship that does not respect naming conventions
$itemtype_field = 'mainitemtype';
$items_id_field = 'mainitems_id';
} else {
$itemtype_matches = preg_grep('/^itemtype/', $field);
$items_id_matches = preg_grep('/^items_id/', $field);
$itemtype_field = reset($itemtype_matches);
$items_id_field = reset($items_id_matches);
}
$DB->update(
$tablename,
[
'is_deleted' => 0,
'is_dynamic' => 0,
],
[
$items_id_field => $item->getID(),
$itemtype_field => $item->getType(),
'is_dynamic' => 1,
],
);
} else {
// Relation based on single foreign key
$DB->update(
$tablename,
[
'is_deleted' => 0,
'is_dynamic' => 0,
],
[
$field => $item->getID(),
'is_dynamic' => 1,
],
);
}
}
}
}
//remove is_dynamic from asset
if ($item->maybeDynamic()) {
$DB->update(
Computer::getTable(),
['is_dynamic' => false],
['id' => $item->getID()],
);
//reload
$item->getFromDB($item->getID());
}
}
public static function purgeComputerVolumes($computers_id)
{
$disk = new Item_Disk();
$disk->dohistory = false;
$disk->deleteByCriteria(['items_id' => $computers_id, 'itemtype' => 'Computer']);
}
/**
* Remove antivirus information
* @since 2.3.0
*
* @param integer $computers_id the computer ID
*/
public static function purgeComputerAntivirus($computers_id)
{
$antivirus = new ItemAntivirus();
$antivirus->dohistory = false;
$antivirus->deleteByCriteria(['items_id' => $computers_id, 'itemtype' => Computer::class], true);
}
/**
* Remove all the computer software's history
*
* @param int $computer_id the computer's ID in GLPI
* @param bool $only_history (true by default)
*
* @return void
**/
public static function deleteHistory($computer_id, $only_history = true)
{
/** @var DBmysql $DB */
global $DB;
$criteria = [
'itemtype' => 'Computer',
'items_id' => $computer_id,
];
if ($only_history) {
$criteria['linked_action'] = [
Log::HISTORY_INSTALL_SOFTWARE,
Log::HISTORY_UNINSTALL_SOFTWARE,
];
}
$DB->delete('glpi_logs', $criteria);
}
/**
* @param $params array with theses options
* - 'itemtype'
* - 'items_id'
* - 'action' (default 'uninstall'
* - 'ocs_id' (default null)
* - 'models_id'
**/
public static function addUninstallLog($params = [])
{
// merge default paramaters
$params = array_merge([
'itemtype' => null,
'items_id' => null,
'action' => 'uninstall',
'ocs_id' => null,
'models_id' => null,
], $params);
$changes[0] = 0;
$changes[1] = "";
$model = new PluginUninstallModel();
if (isset($params['models_id'])) {
$model->getConfig($params['models_id']);
}
switch ($params['action']) {
case 'uninstall':
$changes[2] = __s('Item is now uninstalled', 'uninstall');
if (isset($params['models_id'])) {
$changes[2] = sprintf(
__s('Item is now uninstalled with model %s', 'uninstall'),
$model->getName(),
);
}
break;
case 'replaced_by':
$changes[2] = __s('Item replaced by a new one', 'uninstall');
if (isset($params['models_id'])) {
$changes[2] = sprintf(
__s('Item replaced by a new one with model %s', 'uninstall'),
$model->getName(),
);
}
break;
case 'replace':
$changes[2] = __s('Item replacing an old one', 'uninstall');
break;
case 'removeFromOCS':
$changes[2] = addslashes(sprintf(
__s('%1$s %2$s'),
__s('Removed from OCSNG with ID', 'uninstall'),
$params['ocs_id'],
));
break;
}
Log::history(
$params['items_id'],
$params['itemtype'],
$changes,
self::class,
Log::HISTORY_PLUGIN,
);
}
/**
* Get an history entry message
*
* @param $data Array from glpi_logs table
*
* @since GLPI version 0.84
*
* @return string
**/
public static function getHistoryEntry($data)
{
if ($data['linked_action'] - Log::HISTORY_PLUGIN === 0) {
return $data['new_value'];
}
return '';
}
/**
* @param $create (true by default)
* @return int
*/
public static function getUninstallTransferModelID($create = true)
{
/** @var DBmysql $DB */
global $DB;
$iterator = $DB->request([
'FROM' => 'glpi_transfers',
'WHERE' => ['name' => self::PLUGIN_UNINSTALL_TRANSFER_NAME],
]);
if (count($iterator) === 0) {
if ($create) {
$transfer = new Transfer();
$input["name"] = self::PLUGIN_UNINSTALL_TRANSFER_NAME;
$input["keep_networklink"] = 2;
$input["keep_history"] = 1;
$input["keep_devices"] = 1;
$input["keep_infocoms"] = 1;
$input["keep_enterprises"] = 1;
$input["keep_contacts"] = 1;
$input["keep_contracts"] = 1;
$input["keep_documents"] = 1;
$id = $transfer->add($input);
} else {
$id = 0;
}
} else {
$data = $iterator->current();
$id = $data['id'];
}
return $id;
}
/**
* @param $type
* @param $ID
**/
public static function getInfocomPresentForDevice($type, $ID)
{
/** @var DBmysql $DB */
global $DB;
$it = $DB->request([
'SELECT' => ['id'],
'FROM' => 'glpi_infocoms',
'WHERE' => [
'itemtype' => $type,
'items_id' => $ID,
],
]);
if (count($it) > 0) {
return $it->current()['id'];
}
return 0;
}
/**
* @param $ID
* @param $item
* @param $user_id
**/
public static function showFormUninstallation($ID, $item, $user_id)
{
/**
* @var array $CFG_GLPI
*/
global $CFG_GLPI;
$type = $item->getType();
echo "<form action='" . $CFG_GLPI['root_doc'] . "/plugins/uninstall/front/action.php'
method='post'>";
echo Html::hidden('device_type', ['value' => $type]);
echo "<table class='tab_cadre_fixe' cellpadding='5'>";
echo "<tr><th colspan='3'>" . __s("Apply model", 'uninstall') . "</th></tr>";
echo "<tr class='tab_bg_1'><td>" . __s("Model") . "</td><td>";
if (class_exists($type) && is_a($type, CommonDBTM::class, true)) {
$item = new $type();
$item->getFromDB($ID);
$rand = self::dropdownUninstallModels(
"model_id",
$_SESSION["glpiID"],
$item->fields["entities_id"],
);
echo "</td></tr>";
$params = ['templates_id' => '__VALUE__',
'entity' => $item->fields["entities_id"],
'users_id' => $_SESSION["glpiID"],
];
Ajax::updateItemOnSelectEvent(
'dropdown_model_id' . $rand,
"show_objects",
$CFG_GLPI['root_doc'] . "/plugins/uninstall/ajax/locations.php",
$params,
);
}
echo "<tr class='tab_bg_1'><td>" . __s("Item's location after applying model", "uninstall") . "</td>";
echo "<td><span id='show_objects'>\n" . Dropdown::EMPTY_VALUE . "</span></td>\n";
echo "</tr>";
echo "<tr class='tab_bg_1 center'><td colspan='3'>";
echo "<input type='submit' name='uninstall' value=\"" . _sx('button', 'Post') . "\"
class='submit'>";
echo "<input type='hidden' name='id' value='" . $ID . "'>";
echo "</td></tr>";
echo "</table>";
Html::closeForm();
}
/**
* @param $type
* @param $items_id
**/
public static function razPortInfos($type, $items_id)
{
/** @var DBmysql $DB */
global $DB;
$nn = new NetworkName();
$conn = new NetworkPort_NetworkPort();
$vlan = new NetworkPort_Vlan();
$crit = [
'FROM' => 'glpi_networkports',
'WHERE' => [