forked from DebugBill/glpi2mdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.php
325 lines (282 loc) · 15.5 KB
/
hook.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
<?php
/*
-------------------------------------------------------------------------
glpi2mdt plugin for GLPI
Copyright (C) 2017 by the glpi2mdt Development Team.
https://github.com/RandomFrenchAdmin/glpi2mdt
-------------------------------------------------------------------------
LICENSE
This file is part of glpi2mdt.
glpi2mdt 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 3 of the License, or
(at your option) any later version.
glpi2mdt 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 glpi2mdt. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
/**
* Plugin install process, create databases and crontasks
*
* @return boolean
*/
function plugin_glpi2mdt_install() {
global $DB;
$dbversion = 1;
// Global plugin settings
if (!$DB->tableExists("glpi_plugin_glpi2mdt_parameters")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_parameters` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parameter` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`scope` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'global',
`value_num` int(11) DEFAULT NULL,
`value_char` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_deleted` boolean NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `Constraint` (`parameter`,`scope`),
INDEX `is_deleted` (`is_deleted` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_parameters ". $DB->error());
$query = "INSERT INTO `glpi_plugin_glpi2mdt_parameters`
(`id`, `parameter`, `scope`, `value_num`, `is_deleted`)
VALUES (1, 'DBVersion', 'global', $dbversion, false)";
$DB->query($query) or die("error updating glpi_plugin_glpi2mdt_parameters ". $DB->error());
}
// Individual settings for computers, models and roles
if (!$DB->tableExists("glpi_plugin_glpi2mdt_settings")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_settings` (
`id` int(11) NOT NULL auto_increment,
`category` varchar(1) COLLATE utf8_unicode_ci NOT NULL,
`type` varchar(1) COLLATE utf8_unicode_ci NOT NULL,
`key` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`value` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_in_sync` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`, `type`,`category`,`key`),
KEY `is_in_sync` (`is_in_sync`),
KEY `type` (`type`),
KEY `category` (`category`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_settings ". $DB->error());
}
// Available roles extracted from MDT database
if (!$DB->tableExists("glpi_plugin_glpi2mdt_roles")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_roles` (
`id` int(11) NOT NULL,
`role` varchar(255) collate utf8_unicode_ci default NULL,
`is_deleted` boolean NOT NULL default true,
`is_in_sync` boolean NOT NULL default false,
PRIMARY KEY (`id`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_roles ". $DB->error());
}
// Available applications, extracted from XML file on installation share
if (!$DB->tableExists("glpi_plugin_glpi2mdt_applications")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_applications` (
`guid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`shortname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`version` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`hide` boolean NOT NULL DEFAULT false,
`enable` boolean NOT NULL DEFAULT true,
`is_deleted` boolean NOT NULL DEFAULT false,
`is_in_sync` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`guid`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_applications ". $DB->error());
}
if (!$DB->tableExists("glpi_plugin_glpi2mdt_application_groups")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_application_groups` (
`guid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`hide` boolean NOT NULL DEFAULT false,
`enable` boolean NOT NULL DEFAULT '1',
`is_deleted` boolean NOT NULL DEFAULT false,
`is_in_sync` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`guid`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_application_groups ". $DB->error());
}
if (!$DB->tableExists("glpi_plugin_glpi2mdt_application_group_links")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_application_group_links` (
`group_guid` varchar(38) COLLATE utf8_unicode_ci NOT NULL,
`application_guid` varchar(38) COLLATE utf8_unicode_ci NOT NULL,
`is_deleted` boolean NOT NULL DEFAULT false,
`is_in_sync` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`group_guid`, `application_guid`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_application_group_links ". $DB->error());
}
// Available OS, extracted from OS folder on installation share
if (!$DB->tableExists("glpi_plugin_glpi2mdt_operating_systems")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_operating_systems` (
`id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`guid` varchar(38) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`enable` boolean NOT NULL DEFAULT true,
`is_deleted` boolean NOT NULL DEFAULT false,
`is_in_sync` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`id`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_operating_systems ". $DB->error());
}
// Available task sequences, extracted from XML file on installation share
if (!$DB->tableExists("glpi_plugin_glpi2mdt_task_sequences")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_task_sequences` (
`id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`guid` varchar(38) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`hide` boolean NOT NULL DEFAULT false,
`enable` boolean NOT NULL DEFAULT true,
`is_deleted` boolean NOT NULL DEFAULT false,
`is_in_sync` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`id`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_task_sequences ". $DB->error());
}
if (!$DB->tableExists("glpi_plugin_glpi2mdt_task_sequence_groups")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_task_sequence_groups` (
`guid` varchar(38) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`hide` boolean NOT NULL DEFAULT false,
`enable` boolean NOT NULL DEFAULT true,
`is_deleted` boolean NOT NULL DEFAULT false,
`is_in_sync` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`guid`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_task_sequence_groups ". $DB->error());
}
if (!$DB->tableExists("glpi_plugin_glpi2mdt_task_sequence_group_links")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_task_sequence_group_links` (
`group_guid` varchar(38) COLLATE utf8_unicode_ci NOT NULL,
`sequence_guid` varchar(38) COLLATE utf8_unicode_ci NOT NULL,
`is_deleted` boolean NOT NULL DEFAULT '0',
`is_in_sync` boolean NOT NULL DEFAULT '1',
PRIMARY KEY (`group_guid`, `sequence_guid`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_task_sequence_group_links ". $DB->error());
}
// Make and Models association
if (!$DB->tableExists("glpi_plugin_glpi2mdt_models")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_models` (
`id` int(11) NOT NULL,
`make` varchar(50) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`tech_code` varchar(50) NOT NULL,
`is_in_sync` tinyint(4) NOT NULL DEFAULT '1',
`is_deleted` tinyint(4) NOT NULL DEFAULT '0',
`glpi_plugin_glpi2mdt_modelscol` varchar(45) DEFAULT NULL,
PRIMARY KEY (`make`, `tech_code`),
UNIQUE KEY (`id`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_models ". $DB->error());
}
// All valid parameters for MDT objects
if (!$DB->tableExists("glpi_plugin_glpi2mdt_descriptions")) {
$query = "CREATE TABLE `glpi_plugin_glpi2mdt_descriptions` (
`column_name` varchar(255) collate utf8_unicode_ci NOT NULL,
`category_order` integer collate utf8_unicode_ci NOT NULL default 0,
`category` varchar(255) default '',
`description` varchar(255) collate utf8_unicode_ci default '',
`is_deleted` boolean NOT NULL DEFAULT false,
`is_in_sync` boolean NOT NULL DEFAULT true,
PRIMARY KEY (`column_name`),
INDEX `is_deleted` (`is_deleted` ASC),
INDEX `is_in_sync`(`is_in_sync` ASC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_descriptions ". $DB->error());
}
// Remove cron tasks
Crontask::Unregister('Glpi2mdtCrontask');
// Create or update crontask for checking new plugin updates
CronTask::Register('PluginGlpi2mdtCrontask', 'checkGlpi2mdtUpdate', (3600 * 24),
array('mode' => 2, 'allowmode' => 3, 'logs_lifetime' => 30,
'comment' => 'Daily task checking for updates'));
// Create or update crontask for updating base data from MDT files and database
CronTask::Register('PluginGlpi2mdtCrontask', 'updateBaseconfigFromMDT', 300,
array('mode' => 2, 'allowmode' => 3, 'logs_lifetime' => 30,
'comment' => 'Update base data from MDT XML files and MS-SQL DB'));
// Create or update crontask for syncrhonizing data between MDT and GLPI (Master-Master & Strict modes)
CronTask::Register('PluginGlpi2mdtCrontask', 'syncMasterAndStrict', 3600,
array('mode' => 2, 'allowmode' => 3, 'logs_lifetime' => 30,
'comment' => 'Synchronize data between MDT and GLPI in Master-Master and Strict modes'));
// Create or update crontask for disabling "OS Install" flag when expired
CronTask::Register('PluginGlpi2mdtCrontask', 'expireOSInstallFlag', 300,
array('mode' => 2, 'allowmode' => 3, 'logs_lifetime' => 30,
'comment' => 'Disable "OS Install" flag when expired'));
// Update database if necessary
$DB->query("UPDATE glpi_plugin_glpi2mdt_parameters SET parameter='DBVersion' WHERE scope='global' AND parameter='database_version';");
$result = $DB->query("SELECT sum(value_num) as version FROM glpi_plugin_glpi2mdt_parameters WHERE scope='global' AND parameter='DBVersion'");
if ($DB->numrows($result) == 0) {
die(__("Glpi2mdt database is corrupted. Please uninstall and reinstall the plugin", 'glpi2mdt'));
}
if ($DB->numrows($result) == 1) {
$currentdbversion = $DB->fetchAssoc($result)['version'];
}
// Upgrade to version 2 of the database
if ($currentdbversion == 1) {
return true;
}
return true;
}
/**
* Plugin uninstall process
*
* @return boolean
*/
function plugin_glpi2mdt_uninstall() {
global $DB;
// Plugin tables deletion
$tables = ["glpi_plugin_glpi2mdt_application_group_links",
"glpi_plugin_glpi2mdt_application_groups",
"glpi_plugin_glpi2mdt_applications",
"glpi_plugin_glpi2mdt_descriptions",
"glpi_plugin_glpi2mdt_models",
"glpi_plugin_glpi2mdt_operating_systems",
"glpi_plugin_glpi2mdt_parameters",
"glpi_plugin_glpi2mdt_roles",
"glpi_plugin_glpi2mdt_settings",
"glpi_plugin_glpi2mdt_task_sequence_group_links",
"glpi_plugin_glpi2mdt_task_sequence_groups",
"glpi_plugin_glpi2mdt_task_sequences"];
foreach ($tables as $table) {
$DB->query("DROP TABLE IF EXISTS `$table`;");
}
// Remove cron tasks
CronTask::Unregister('Glpi2mdtCrontask');
return true;
}
/**
* This function is called by GLPI when an update is made to a computer
* It triggers an update of MDT just in case...
*
* @param $item, object reference to a computer
* @return nothing
*/
function updateMDT($item) {
$id = $item->getID();
$computer = new PluginGlpi2mdtComputer;
$computer->updateMDT($id);
unset($computer);
}