Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / hook.php @ b204adb9

History | View | Annotate | Download (3.88 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
            $host       = new VigiloHost($computer);
39
            $dirs       = array($this->confdir, "hosts", "managed");
40
            $confdir    = implode(DIRECTORY_SEPARATOR, $dirs);
41
            $file     = $confdir . DIRECTORY_SEPARATOR . $host->getName() . ".xml";
42

    
43
            mkdir($confdir, 0770, true);
44
            $acc = "";
45
            foreach ($dirs as $dir) {
46
                $acc .= DIRECTORY_SEPARATOR . $dir;
47
                chgrp($acc, "vigiconf");
48
            }
49

    
50
            $res = file_put_contents($file, $host, LOCK_EX);
51
            if ($res !== false) {
52
                chgrp($file, "vigiconf");
53
                chmod($file, 0660);
54
            }
55
        }
56
    }
57

    
58
    public function delete($computer)
59
    {
60
        $this->unmonitor($computer->fields["name"]);
61
    }
62

    
63
    public function update($computer)
64
    {
65
        global $PLUGIN_HOOKS;
66
        if (isset($computer->oldvalues["name"])) {
67
            $this->unmonitor($computer->oldvalues["name"]);
68
        }
69

    
70
        $this->add($computer);
71
    }
72

    
73
    public function unmonitor($host)
74
    {
75
        $dirs = array($this->confdir, "hosts", "managed", $host . ".xml");
76
        unlink(implode(DIRECTORY_SEPARATOR, $dirs));
77
    }
78

    
79
    public function manageComputerSoftwareVersion($computer_software_version)
80
    {
81
        global $DB;
82
        $computer=new Computer();
83
        $computer->getFromDB($computer_software_version->getField("computers_id"));
84
        $this->update($computer);
85
    }
86

    
87
    public function manageSoftwares($software)
88
    {
89
        global $DB;
90
        $softwareVer=new SoftwareVersion();
91
        $idSoftwareVersion=$softwareVer->find('softwares_id=' . $software->getID());
92
        foreach ($idSoftwareVersion as $idVersion) {
93
            if ($idVersion['id']) {
94
                $computerVer=new Computer_SoftwareVersion();
95
                $goodField='softwareversions_id=' . $idVersion['id'];
96
                $updateComp=$computerVer->find($goodField);
97
                foreach ($updateComp as $idComputer) {
98
                    if ($idComputer['computers_id'] != -1) {
99
                        $computer=new Computer();
100
                        $computer->getFromDB($idComputer['computers_id']);
101
                        $this->update($computer);
102
                    }
103
                }
104
            }
105
        }
106
    }
107

    
108
    public function manageDisks($disk)
109
    {
110
        global $DB;
111
        $id=$disk->getField('computers_id');
112
        $computer=new Computer();
113
        $computer->getFromDB($id);
114
        $this->update($computer);
115
    }
116

    
117
    public function manageAddresses($address)
118
    {
119
        global $DB;
120
        $id=$address->getField('mainitems_id');
121
        $comp=new Computer();
122
        $comp->getFromDB($id);
123
        $this->update($comp);
124
    }
125

    
126
    public function manageNetworks($network)
127
    {
128
        global $DB;
129
        ;
130
        $id=$network->getField('items_id');
131
        $comp=new Computer();
132
        $comp->getFromDB($id);
133
        $this->update($comp);
134
    }
135
}