Skip to content

Commit 352fa0a

Browse files
author
Debug Bill
committed
Almost ready for version 0.1.0. Just some more testing
1 parent 55702eb commit 352fa0a

File tree

11 files changed

+733
-573
lines changed

11 files changed

+733
-573
lines changed

hook.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*/
3434
function plugin_glpi2mdt_install() {
3535
global $DB;
36+
$dbversion = 1;
3637

3738
// Global plugin settings
3839
if (!TableExists("glpi_plugin_glpi2mdt_parameters")) {
@@ -52,7 +53,7 @@ function plugin_glpi2mdt_install() {
5253

5354
$query = "INSERT INTO `glpi_plugin_glpi2mdt_parameters`
5455
(`id`, `parameter`, `scope`, `value_num`, `is_deleted`)
55-
VALUES (1, 'database_version', 'global', 1, false)";
56+
VALUES (1, 'DBVersion', 'global', $dbversion, false)";
5657
$DB->query($query) or die("error updating glpi_plugin_glpi2mdt_parameters ". $DB->error());
5758
}
5859

@@ -69,7 +70,7 @@ function plugin_glpi2mdt_install() {
6970
KEY `is_in_sync` (`is_in_sync`),
7071
KEY `type` (`type`),
7172
KEY `category` (`category`)
72-
) ENGINE=MyISAM AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
73+
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
7374

7475
$DB->query($query) or die("error creating glpi_plugin_glpi2mdt_settings ". $DB->error());
7576
}
@@ -240,6 +241,21 @@ function plugin_glpi2mdt_install() {
240241
CronTask::Register('PluginGlpi2mdtCrontask', 'expireOSInstallFlag', 300,
241242
array('mode' => 2, 'allowmode' => 3, 'logs_lifetime' => 30,
242243
'comment' => 'Daily task checking for updates'));
244+
245+
// Update database if necessary
246+
$DB->query("UPDATE glpi_plugin_glpi2mdt_parameters SET parameter='DBVersion' WHERE scope='global' AND parameter='database_version';");
247+
$result = $DB->query("SELECT sum(value_num) as version FROM glpi_plugin_glpi2mdt_parameters WHERE scope='global' AND parameter='DBVersion'");
248+
if ($DB->numrows($result) == 0) {
249+
die(__("Glpi2mdt database is corrupted. Please uninstall and reinstall the plugin", 'glpi2mdt'));
250+
}
251+
if ($DB->numrows($result) == 1) {
252+
$currentdbversion = $DB->fetch_array($result)['version'];
253+
}
254+
255+
// Upgrade to version 2 of the database
256+
if ($currentdbversion == 1) {
257+
return true;
258+
}
243259
return true;
244260
}
245261

@@ -263,6 +279,7 @@ function plugin_glpi2mdt_uninstall() {
263279
return true;
264280
}
265281

282+
266283
/**
267284
* This function is called by GLPI when an update is made to a computer
268285
* It triggers an update of MDT just in case...

inc/computer.class.php

Lines changed: 93 additions & 111 deletions
Large diffs are not rendered by default.

inc/config.class.php

Lines changed: 173 additions & 170 deletions
Large diffs are not rendered by default.

inc/crontask.class.php

Lines changed: 100 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,30 @@
3838

3939

4040
/**
41-
* Glpi2mdtcrontasks class
41+
* Glpi2mdtcrontask class
4242
**/
4343
class PluginGlpi2mdtCronTask extends PluginGlpi2mdtMdt {
4444

4545
/**
46-
* Check if new version is available
47-
*
48-
* @param $auto boolean: check done by cron ? (if not display result)
49-
* (default true)
50-
* @param $messageafterredirect boolean: use message after redirect instead of display
51-
* (default false)
52-
*
53-
* @return string explaining the result
46+
* Check if new version is available
47+
*
48+
* @param $auto boolean: check done by cron ? (if not display result)
49+
* (default true)
50+
* @param $messageafterredirect boolean: use message after redirect instead of display
51+
* (default false)
52+
*
53+
* @return integer if started in cron mode. Outputs HTML data otherwise
54+
* >0 : done
55+
* <0 : to be run again (not finished)
56+
* 0 : nothing to be done
5457
**/
5558
static function cronCheckGlpi2mdtUpdate($task, $cron=true, $messageafterredirect=false) {
5659
$currentversion = PLUGIN_GLPI2MDT_VERSION;
5760
global $DB;
58-
// if (!$auto
59-
// && !Session::haveRight('backup', Backup::CHECKUPDATE)) {
60-
// return false;
61-
// }
61+
62+
// if (!$cron && !Session::haveRight('backup', Backup::CHECKUPDATE)) {
63+
// return false;
64+
// }
6265

6366
//parse github releases (get last version number)
6467
$error = "";
@@ -74,14 +77,14 @@ static function cronCheckGlpi2mdtUpdate($task, $cron=true, $messageafterredirect
7477
$latest_version = array_pop($released_tags);
7578
// Did we get something? Maybe not if the server has no internet access...
7679
if (strlen(trim($latest_version)) == 0) {
77-
if (!$auto) {
80+
if ($cron) {
81+
$task->log($error);
82+
} else {
7883
if ($messageafterredirect) {
7984
Session::addMessageAfterRedirect($error, true, ERROR);
8085
} else {
81-
echo "<div class='center'>$error</div>";
86+
return $error;
8287
}
83-
} else {
84-
return $error;
8588
}
8689
} else {
8790
// Build a unique ID which will differentiate platforms (dev, prod..) behind the same public IP
@@ -94,6 +97,7 @@ static function cronCheckGlpi2mdtUpdate($task, $cron=true, $messageafterredirect
9497
$query = "SELECT value_char FROM glpi_plugin_glpi2mdt_parameters
9598
WHERE is_deleted=false AND scope='global' AND parameter='ReportUsage'";
9699
if ($DB->fetch_assoc($DB->query($query))['value_char'] == 'YES') {
100+
$CO = $globalconfig['Mode'];
97101
$AP = $DB->fetch_row($DB->query("SELECT count(*) FROM glpi_plugin_glpi2mdt_applications WHERE is_deleted=false"))[0];
98102
$AG = $DB->fetch_row($DB->query("SELECT count(*) FROM glpi_plugin_glpi2mdt_application_groups WHERE is_deleted=false"))[0];
99103
$TS = $DB->fetch_row($DB->query("SELECT count(*) FROM glpi_plugin_glpi2mdt_task_sequences WHERE is_deleted=false"))[0];
@@ -102,50 +106,27 @@ static function cronCheckGlpi2mdtUpdate($task, $cron=true, $messageafterredirect
102106
$MO = $DB->fetch_row($DB->query("SELECT count(*) FROM glpi_plugin_glpi2mdt_models WHERE is_deleted=false"))[0];
103107
$PK = 0; //$DB->fetch_row($DB->query("SELECT count(*) FROM glpi_plugin_glpi2mdt_packages WHERE is_deleted=false"))[0];
104108
$ST = $DB->fetch_row($DB->query("SELECT count(*) FROM glpi_plugin_glpi2mdt_settings"))[0];
105-
$gets = $gets."&AP=$AP&AG=$AG&TS=$TS&TG=$TG&RO=$RO&MO=$MO&PK=$PK&ST=$ST";
109+
$gets = $gets."&CO=$CO&&AP=$AP&AG=$AG&TS=$TS&TG=$TG&RO=$RO&MO=$MO&PK=$PK&ST=$ST";
106110
}
107111
Toolbox::getURLContent("https://glpi2mdt.thauvin.org/report.php".$gets);
108-
$newversion = sprintf(__('A new version of plugin glpi2mdt is available: v%s'), $latest_version);
109-
$uptodate = sprintf(__('You have the latest available version of glpi2mdti: v%s'), $latest_version);
110-
$repository = __('You will find it on GitHub.com.');
112+
$task->log("https://glpi2mdt.thauvin.org/report.php".$gets);
111113
$query = "INSERT INTO glpi_plugin_glpi2mdt_parameters
112114
(`parameter`, `scope`, `value_char`, `is_deleted`)
113115
VALUES ('LatestVersion', 'global', '$latest_version', false)
114116
ON DUPLICATE KEY UPDATE value_char='$latest_version', value_num=NULL, is_deleted=false";
115117
$DB->queryOrDie($query, "Database error");
116118
if (version_compare($currentversion, $latest_version, '<')) {
117-
118-
if (!$auto) {
119-
if ($messageafterredirect) {
120-
Session::addMessageAfterRedirect($newversion);
121-
Session::addMessageAfterRedirect($repository);
122-
} else {
123-
echo "<div class='center'>".$newversion."</div>";
124-
echo "<div class='center'>".$repository."</div>";
125-
}
126-
127-
} else {
128-
if ($messageafterredirect) {
129-
Session::addMessageAfterRedirect($newversion);
130-
} else {
131-
return $newversion;
132-
}
133-
}
134-
119+
$message = sprintf(__('A new version of plugin glpi2mdt is available: v%s'), $latest_version);
135120
} else {
136-
if (!$auto) {
137-
if ($messageafterredirect) {
138-
Session::addMessageAfterRedirect($uptodate);
139-
} else {
140-
echo "<div class='center'>".$uptodate."</div>";
141-
}
142-
121+
$message = sprintf(__('You have the latest available version of glpi2mdti: v%s'), $latest_version);
122+
}
123+
if ($cron) {
124+
$task->log($message);
125+
} else {
126+
if ($messageafterredirect) {
127+
Session::addMessageAfterRedirect($message);
143128
} else {
144-
if ($messageafterredirect) {
145-
Session::addMessageAfterRedirect($uptodate);
146-
} else {
147-
return $uptodate;
148-
}
129+
return $message;
149130
}
150131
}
151132
}
@@ -429,15 +410,78 @@ function cronUpdateBaseconfigFromMDT($task, $cron=true) {
429410

430411
/**
431412
* Task to synchronize data between MDT and GLPI in Master-Master mode
413+
* Can be used atomically to update one machine, or globally by cron
432414
*
433415
* @param $task Object of CronTask class for log / stat
416+
* @param $id; computer ID to update
434417
*
435418
* @return integer
436419
* >0 : done
437420
* <0 : to be run again (not finished)
438421
* 0 : nothing to be done
439422
*/
440-
static function cronSyncMasterMaster($task) {
423+
static function cronSyncMasterMaster($task, $id=0) {
424+
global $DB;
425+
$MDT = new PluginGlpi2mdtMdt;
426+
$globalconfig = $MDT->globalconfig;
427+
if ($globalconfig['Mode'] <> "Master") {
428+
$task->log("This cron task runs only in Master-Master mode");
429+
return 0;
430+
}
431+
// Build array of valid variables
432+
$variables = $globalconfig['variables'];
433+
if ($id > 0) {
434+
$mdt = $MDT->getMdtIds($id);
435+
$mdtids = "AND c.".$mdt['mdtids'];
436+
$arraymdtids = $mdt['arraymdtids'];
437+
}
438+
439+
//GET all computers and settings
440+
$query="SELECT * FROM dbo.ComputerIdentity c, dbo.Settings s WHERE c.id=s.id $mdtids";
441+
$result = $MDT->queryOrDie($query, "Cannot retreive computers from MDT");
442+
if (isset($task)) {
443+
$task->setVolume($MDT->numrows($result));
444+
$task->log("Computer entries found in MDT database");
445+
}
446+
$correspondances = 0;
447+
$fields = 0;
448+
$previousid = 0;
449+
while ($row = $MDT->fetch_array($result)) {
450+
// Find correspondance in GLPI
451+
$query = "SELECT c.id as id FROM glpi_computers c, glpi_networkports n
452+
WHERE c.id=n.items_id AND n.itemtype='Computer' AND n.instantiation_type='NetworkPortEthernet'
453+
AND c.is_deleted=FALSE AND n.is_deleted=false AND c.name = '".$row['Description']."'
454+
AND UPPER(n.mac)='".$row['MacAddress']."' AND serial='".$row['SerialNumber']."' AND otherserial='".
455+
$row['AssetTag']."' AND uuid='".$row['UUID']."' ORDER BY c.id";
456+
$glpi = $DB->queryOrDie($query, "Can't find correspondance in GLPI");
457+
if ($DB->numrows($glpi) == 1) {
458+
$array = $DB->fetch_array($glpi);
459+
$id = $array['id'];
460+
// Mark settings that may have to be deleted only if first iteration on this computer
461+
if ($previousid<>$id) {
462+
$DB->query("UPDATE glpi_plugin_glpi2mdt_settings SET is_in_sync=false WHERE type='C' AND category='C' AND id=$id");
463+
}
464+
$previousid = $id;
465+
$correspondances += 1;
466+
// Update GLPI with data from MDT
467+
foreach ($row as $key=>$value) {
468+
if (isset($variables[$key]) AND $value <>'' AND $value<>null) {
469+
$DB->queryOrDie("INSERT INTO glpi_plugin_glpi2mdt_settings (ID, type, category, `key`, `value`, `is_in_sync`)
470+
VALUES ($id, 'C', 'C', '$key', '$value', true)
471+
ON DUPLICATE KEY UPDATE `key`='$key', value='$value', is_in_sync=true;", "Can't insert setting");
472+
$fields += 1;
473+
}
474+
}
475+
$DB->query("DELETE FROM glpi_plugin_glpi2mdt_settings WHERE type='C' AND category='C' AND is_in_sync=false AND id=$id");
476+
}
477+
}
478+
if (isset($task)) {
479+
$task->setVolume($correspondances);
480+
$task->log("computers updated in GLPI");
481+
$task->setVolume($fields);
482+
$task->log("settings updated in GLPI");
483+
return 1;
484+
}
441485
return 0;
442486
}
443487

@@ -469,50 +513,15 @@ function cronExpireOSInstallFlag($task) {
469513
while ($row=$DB->fetch_array($result)) {
470514
$nb += 1;
471515
$id = $row['id'];
472-
473-
// Get data for current computer
474-
$query = "SELECT name, uuid, serial, otherserial FROM glpi_computers WHERE id=$id AND is_deleted=false";
475-
$result = $DB->query($query) or $task->log("Database error: ". $DB->error()."<br><br>".$query);
476-
$common = $DB->fetch_array($result);
477-
478-
// Build list of IDs of existing records in MDT bearing same name, uuid, serial or mac adresses
479-
// as the computer being updated (this might clean up other bogus entries and remove duplicate names
480-
$uuid = $common['uuid'];
481-
$name = $common['name'];
482-
$serial = $common['serial'];
483-
$assettag = $common['otherserial'];
484-
485-
// Build list of mac addresses to search for
486-
$result = $DB->query("SELECT UPPER(n.mac) as mac
487-
FROM glpi_computers c, glpi_networkports n
488-
WHERE c.id=$id AND c.id=n.items_id AND itemtype='Computer'
489-
AND n.instantiation_type='NetworkPortEthernet' AND n.mac<>''
490-
AND c.is_deleted=FALSE AND n.is_deleted=false")
491-
or $task->log("Database error: ". $DB->error()."<br><br>".$query);
492-
$macs="MacAddress IN (";
493-
unset($values);
494-
while ($line = $DB->fetch_array($result)) {
495-
$mac = $line['mac'];
496-
$macs=$macs."'".$mac."', ";
497-
}
498-
$macs = substr($macs, 0, -2).") ";
499-
if ($macs == "MacAddress IN ()") {
500-
$macs='false';
501-
}
516+
$ids = $this->GetMdtIDs($id);
502517
// Cancel installation flag directly into MDT and MSSQL
503-
$query = "UPDATE dbo.Settings
504-
SET OSInstall='NO'
505-
FROM dbo.ComputerIdentity i, dbo.Settings s
506-
WHERE i.id=s.id AND s.type='C'
507-
AND ((UUID<>'' AND UUID='$uuid')
508-
OR (Description<>'' AND Description='$name')
509-
OR (SerialNumber<>'' AND SerialNumber='$serial')
510-
OR $macs)";
518+
$query = "UPDATE dbo.Settings SET OSInstall=''
519+
FROM dbo.Settings WHERE type='C' AND ".$ids['mdtids'].";";
511520
$MDT->query($query) or $task->log("Can't reset OSInstall flag<br><br>".$query."<br><br>".$MDT->dberror());
512521

513522
// Do the same now on GLPI database
514523
$query = "DELETE FROM glpi_plugin_glpi2mdt_settings WHERE type='C' AND category='C'
515-
AND id=$id AND (`key`='OSInstall' OR `key`='OSInstallExpire');";
524+
AND id=$id AND ((`key`='OSInstall' AND values='YES') OR `key`='OSInstallExpire');";
516525
$DB->query($query) or $task->log("Database error: ". $DB->error()."<br><br>".$query);
517526
}
518527
$task->log('record(s) expired');

0 commit comments

Comments
 (0)