glpi / hook.php @ 0d761a49
History | View | Annotate | Download (5.25 KB)
1 | efcf5ecf | Francois POIROTTE | <?php
|
---|---|---|---|
2 | |||
3 | a5c9df40 | Thibault Louet | require __DIR__ . DIRECTORY_SEPARATOR .'autoloader.php'; |
4 | efcf5ecf | Francois POIROTTE | class VigiloHooks |
5 | { |
||
6 | private $confdir; |
||
7 | |||
8 | a5c9df40 | Thibault Louet | public function __construct($confdir = "/etc/vigilo/vigiconf/conf.d") |
9 | efcf5ecf | Francois POIROTTE | { |
10 | bef4271a | Thibault Louet | spl_autoload_register('vigilo_autoloader');
|
11 | efcf5ecf | Francois POIROTTE | $this->confdir = $confdir; |
12 | } |
||
13 | |||
14 | a5c9df40 | Thibault Louet | public function updateGroups() |
15 | efcf5ecf | Francois POIROTTE | { |
16 | a5c9df40 | Thibault Louet | $host = new VigiloLocation(); |
17 | $dirs = array($this->confdir, "groups", "managed"); |
||
18 | efcf5ecf | Francois POIROTTE | $confdir = implode(DIRECTORY_SEPARATOR, $dirs); |
19 | a5c9df40 | Thibault Louet | $file = $confdir . DIRECTORY_SEPARATOR . "groups.xml"; |
20 | efcf5ecf | Francois POIROTTE | |
21 | 0d761a49 | Romain CHOLLET | mkdir($confdir, 0770, true); |
22 | efcf5ecf | Francois POIROTTE | $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 | a5c9df40 | Thibault Louet | public function add($computer) |
36 | { |
||
37 | if ($computer->getField("is_template")==0) { |
||
38 | 0d761a49 | Romain CHOLLET | 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 | a5c9df40 | Thibault Louet | $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 | 2ef80588 | Romain CHOLLET | if (!file_exists($confdir)) { |
58 | mkdir($confdir, 0770, true); |
||
59 | } |
||
60 | a5c9df40 | Thibault Louet | $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 | efcf5ecf | Francois POIROTTE | public function delete($computer) |
75 | { |
||
76 | a5c9df40 | Thibault Louet | $this->unmonitor($computer->fields["name"]); |
77 | efcf5ecf | Francois POIROTTE | } |
78 | |||
79 | public function update($computer) |
||
80 | { |
||
81 | 0d761a49 | Romain CHOLLET | global $PLUGIN_HOOKS, $DB; |
82 | efcf5ecf | Francois POIROTTE | if (isset($computer->oldvalues["name"])) { |
83 | a5c9df40 | Thibault Louet | $this->unmonitor($computer->oldvalues["name"]); |
84 | efcf5ecf | Francois POIROTTE | } |
85 | $this->add($computer); |
||
86 | } |
||
87 | |||
88 | a5c9df40 | Thibault Louet | public function unmonitor($host) |
89 | efcf5ecf | Francois POIROTTE | { |
90 | $dirs = array($this->confdir, "hosts", "managed", $host . ".xml"); |
||
91 | unlink(implode(DIRECTORY_SEPARATOR, $dirs)); |
||
92 | } |
||
93 | bef4271a | Thibault Louet | |
94 | b204adb9 | Romain CHOLLET | public function manageComputerSoftwareVersion($computer_software_version) |
95 | { |
||
96 | global $DB; |
||
97 | $computer=new Computer(); |
||
98 | $computer->getFromDB($computer_software_version->getField("computers_id")); |
||
99 | 0d761a49 | Romain CHOLLET | $this->update($computer); |
100 | b204adb9 | Romain CHOLLET | } |
101 | |||
102 | a5c9df40 | Thibault Louet | 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 | bef4271a | Thibault Louet | } |
131 | |||
132 | a5c9df40 | Thibault Louet | 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 | bef4271a | Thibault Louet | |
141 | a5c9df40 | Thibault Louet | 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 | bef4271a | Thibault Louet | } |
149 | 0d761a49 | Romain CHOLLET | |
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 | efcf5ecf | Francois POIROTTE | } |