glpi / hook.php @ 0d761a49
History | View | Annotate | Download (5.25 KB)
1 |
<?php
|
---|---|
2 |
|
3 |
require __DIR__ . DIRECTORY_SEPARATOR .'autoloader.php'; |
4 |
class VigiloHooks |
5 |
{ |
6 |
private $confdir; |
7 |
|
8 |
public function __construct($confdir = "/etc/vigilo/vigiconf/conf.d") |
9 |
{ |
10 |
spl_autoload_register('vigilo_autoloader');
|
11 |
$this->confdir = $confdir; |
12 |
} |
13 |
|
14 |
public function updateGroups() |
15 |
{ |
16 |
$host = new VigiloLocation(); |
17 |
$dirs = array($this->confdir, "groups", "managed"); |
18 |
$confdir = implode(DIRECTORY_SEPARATOR, $dirs); |
19 |
$file = $confdir . DIRECTORY_SEPARATOR . "groups.xml"; |
20 |
|
21 |
mkdir($confdir, 0770, true); |
22 |
$acc = ""; |
23 |
foreach ($dirs as $dir) { |
24 |
$acc .= DIRECTORY_SEPARATOR . $dir; |
25 |
chgrp($acc, "vigiconf"); |
26 |
} |
27 |
|
28 |
$res = file_put_contents($file, $host, LOCK_EX); |
29 |
if ($res !== false) { |
30 |
chgrp($file, "vigiconf"); |
31 |
chmod($file, 0660); |
32 |
} |
33 |
} |
34 |
|
35 |
public function add($computer) |
36 |
{ |
37 |
if ($computer->getField("is_template")==0) { |
38 |
global $DB; |
39 |
$template_id = PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($computer->getField("vigilo_template")); |
40 |
|
41 |
if(!empty($template_id)) { |
42 |
$query = "UPDATE glpi_computers |
43 |
SET vigilo_template = '" . PluginVigiloVigiloTemplate::getVigiloTemplateNameByID($computer->getField("vigilo_template")) . |
44 |
"' WHERE id = " . $computer->getField("id") . ";"; |
45 |
$DB->queryOrDie($query, "update vigilo_template field"); |
46 |
} |
47 |
|
48 |
$query = "UPDATE glpi_computers |
49 |
SET is_dynamic = ' 1
|
50 |
' WHERE id = " . $computer->getField("id") . ";"; |
51 |
$DB->queryOrDie($query, "update vigilo_template field"); |
52 |
$host = new VigiloHost($computer); |
53 |
$dirs = array($this->confdir, "hosts", "managed"); |
54 |
$confdir = implode(DIRECTORY_SEPARATOR, $dirs); |
55 |
$file = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml"; |
56 |
|
57 |
if (!file_exists($confdir)) { |
58 |
mkdir($confdir, 0770, true); |
59 |
} |
60 |
$acc = ""; |
61 |
foreach ($dirs as $dir) { |
62 |
$acc .= DIRECTORY_SEPARATOR . $dir; |
63 |
chgrp($acc, "vigiconf"); |
64 |
} |
65 |
|
66 |
$res = file_put_contents($file, $host, LOCK_EX); |
67 |
if ($res !== false) { |
68 |
chgrp($file, "vigiconf"); |
69 |
chmod($file, 0660); |
70 |
} |
71 |
} |
72 |
} |
73 |
|
74 |
public function delete($computer) |
75 |
{ |
76 |
$this->unmonitor($computer->fields["name"]); |
77 |
} |
78 |
|
79 |
public function update($computer) |
80 |
{ |
81 |
global $PLUGIN_HOOKS, $DB; |
82 |
if (isset($computer->oldvalues["name"])) { |
83 |
$this->unmonitor($computer->oldvalues["name"]); |
84 |
} |
85 |
$this->add($computer); |
86 |
} |
87 |
|
88 |
public function unmonitor($host) |
89 |
{ |
90 |
$dirs = array($this->confdir, "hosts", "managed", $host . ".xml"); |
91 |
unlink(implode(DIRECTORY_SEPARATOR, $dirs)); |
92 |
} |
93 |
|
94 |
public function manageComputerSoftwareVersion($computer_software_version) |
95 |
{ |
96 |
global $DB; |
97 |
$computer=new Computer(); |
98 |
$computer->getFromDB($computer_software_version->getField("computers_id")); |
99 |
$this->update($computer); |
100 |
} |
101 |
|
102 |
public function manageSoftwares($software) |
103 |
{ |
104 |
global $DB; |
105 |
$softwareVer=new SoftwareVersion(); |
106 |
$idSoftwareVersion=$softwareVer->find('softwares_id=' . $software->getID()); |
107 |
foreach ($idSoftwareVersion as $idVersion) { |
108 |
if ($idVersion['id']) { |
109 |
$computerVer=new Computer_SoftwareVersion(); |
110 |
$goodField='softwareversions_id=' . $idVersion['id']; |
111 |
$updateComp=$computerVer->find($goodField); |
112 |
foreach ($updateComp as $idComputer) { |
113 |
if ($idComputer['computers_id'] != -1) { |
114 |
$computer=new Computer(); |
115 |
$computer->getFromDB($idComputer['computers_id']); |
116 |
$this->update($computer); |
117 |
} |
118 |
} |
119 |
} |
120 |
} |
121 |
} |
122 |
|
123 |
public function manageDisks($disk) |
124 |
{ |
125 |
global $DB; |
126 |
$id=$disk->getField('computers_id'); |
127 |
$computer=new Computer(); |
128 |
$computer->getFromDB($id); |
129 |
$this->update($computer); |
130 |
} |
131 |
|
132 |
public function manageAddresses($address) |
133 |
{ |
134 |
global $DB; |
135 |
$id=$address->getField('mainitems_id'); |
136 |
$comp=new Computer(); |
137 |
$comp->getFromDB($id); |
138 |
$this->update($comp); |
139 |
} |
140 |
|
141 |
public function manageNetworks($network) |
142 |
{ |
143 |
global $DB; |
144 |
$id=$network->getField('items_id'); |
145 |
$comp=new Computer(); |
146 |
$comp->getFromDB($id); |
147 |
$this->update($comp); |
148 |
} |
149 |
|
150 |
public function plugin_vigilo_getAddSearchOptions($itemtype) |
151 |
{ |
152 |
$options = array(); |
153 |
if ($itemtype == 'Computer' or $itemtype == 'PluginVigiloComputer') |
154 |
{ |
155 |
$options['7007']['table'] = 'glpi_computers'; |
156 |
$options['7007']['field'] = 'vigilo_template'; |
157 |
$options['7007']['name'] = 'vigilo_template'; |
158 |
$options['7007']['massiveaction'] = 'TRUE'; |
159 |
$options['7007']['datatype'] = 'dropdown'; |
160 |
return $options; |
161 |
} |
162 |
} |
163 |
} |