Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / hook.php @ 8b28e91d

History | View | Annotate | Download (6.4 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 addComputer($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

    
61
            $res = file_put_contents($file, $host, LOCK_EX);
62
            if ($res !== false) {
63
                chgrp($file, "vigiconf");
64
                chmod($file, 0660);
65
            }
66
        }
67
    }
68

    
69
    public function addNetworkEquipment($networkequipment)
70
    {
71
        if ($networkequipment->getField("is_template")==0) {
72
            global $DB;
73

    
74
            $host       = new VigiloNetworkEquipment($networkequipment);
75
            $dirs       = array($this->confdir, "hosts", "managed");
76
            $confdir    = implode(DIRECTORY_SEPARATOR, $dirs);
77
            $file       = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml";
78

    
79
            if (!file_exists($confdir)) {
80
                mkdir($confdir, 0770, true);
81
            }
82

    
83
            $res = file_put_contents($file, $host, LOCK_EX);
84
            if ($res !== false) {
85
                chgrp($file, "vigiconf");
86
                chmod($file, 0660);
87
            }
88
        }
89
    }
90

    
91
    public function delete($computer)
92
    {
93
        $this->unmonitor($computer->fields["name"]);
94
    }
95

    
96
    public function update($computer)
97
    {
98
        global $PLUGIN_HOOKS, $DB;
99
        if (isset($computer->oldvalues["name"])) {
100
            $this->unmonitor($computer->oldvalues["name"]);
101
        }
102
    }
103

    
104
    public function updateComputer($computer)
105
    {
106
        $this->update($computer);
107
        $this->addComputer($computer);
108
    }
109

    
110
    public function updateNetworkEquipment($networkEquipment)
111
    {
112
        $this->update($networkEquipment);
113
        $this->addNetworkEquipment($networkEquipment);
114
    }
115

    
116
    public function unmonitor($host)
117
    {
118
        $dirs = array($this->confdir, "hosts", "managed", $host . ".xml");
119
        unlink(implode(DIRECTORY_SEPARATOR, $dirs));
120
    }
121

    
122
    public function manageComputerSoftwareVersion($computer_software_version)
123
    {
124
        global $DB;
125
        $computer=new Computer();
126
        $computer->getFromDB($computer_software_version->getField("computers_id"));
127
        $this->updateComputer($computer);
128
    }
129

    
130
    public function manageSoftwares($software)
131
    {
132
        global $DB;
133
        $softwareVer=new SoftwareVersion();
134
        $idSoftwareVersion=$softwareVer->find('softwares_id=' . $software->getID());
135
        foreach ($idSoftwareVersion as $idVersion) {
136
            if ($idVersion['id']) {
137
                $computerVer=new Computer_SoftwareVersion();
138
                $goodField='softwareversions_id=' . $idVersion['id'];
139
                $updateComp=$computerVer->find($goodField);
140
                foreach ($updateComp as $idComputer) {
141
                    if ($idComputer['computers_id'] != -1) {
142
                        $computer=new Computer();
143
                        $computer->getFromDB($idComputer['computers_id']);
144
                        $this->updateComputer($computer);
145
                    }
146
                }
147
            }
148
        }
149
    }
150

    
151
    public function manageDisks($disk)
152
    {
153
        global $DB;
154
        $id=$disk->getField('computers_id');
155
        $computer=new Computer();
156
        $computer->getFromDB($id);
157
        $this->updateComputer($computer);
158
    }
159

    
160
    public function manageAddresses($address)
161
    {
162
        global $DB;
163
        $id=$address->getField('mainitems_id');
164
        $comp=new Computer();
165
        $comp->getFromDB($id);
166
        $this->updateComputer($comp);
167
    }
168

    
169
    public function manageNetworks($network)
170
    {
171
        global $DB;
172
        $id=$network->getField('items_id');
173
        $itemtype = $network->getField('itemtype');
174
        if ($itemtype === 'Computer') {
175
            $comp=new Computer();
176
            $comp->getFromDB($id);
177
            $this->updateComputer($comp);
178
        }
179
        else if ($itemtype === 'NetworkEquipment') {
180
            $ne=new NetworkEquipment();
181
            $ne->getFromDB($id);
182
            $this->updateNetworkEquipment($ne);
183
        }
184
    }
185

    
186
    public function plugin_vigilo_getAddSearchOptions($itemtype)
187
    {
188
        $options = array();
189
        if ($itemtype == 'Computer' or $itemtype == 'PluginVigiloComputer')
190
        {
191
            $options['7007']['table']          = 'glpi_computers';
192
            $options['7007']['field']          = 'vigilo_template';
193
            $options['7007']['name']           = 'vigilo_template';
194
            $options['7007']['massiveaction']  = 'TRUE';
195
            $options['7007']['datatype']       = 'dropdown';
196
            return $options;
197
        }
198
    }
199
}