Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / hook.php @ a5c9df40

History | View | Annotate | Download (3.6 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
        if (isset($computer->oldvalues["name"])) {
66
            $this->unmonitor($computer->oldvalues["name"]);
67
        }
68

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

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

    
78
    public function manageSoftwares($software)
79
    {
80
        global $DB;
81
        $softwareVer=new SoftwareVersion();
82
        $idSoftwareVersion=$softwareVer->find('softwares_id=' . $software->getID());
83
        foreach ($idSoftwareVersion as $idVersion) {
84
            if ($idVersion['id']) {
85
                $computerVer=new Computer_SoftwareVersion();
86
                $goodField='softwareversions_id=' . $idVersion['id'];
87
                $updateComp=$computerVer->find($goodField);
88
                foreach ($updateComp as $idComputer) {
89
                    if ($idComputer['computers_id'] != -1) {
90
                        $computer=new Computer();
91
                        $computer->getFromDB($idComputer['computers_id']);
92
                        $this->update($computer);
93
                    }
94
                }
95
            }
96
        }
97
    }
98

    
99
    public function manageDisks($disk)
100
    {
101
        global $DB;
102
        $id=$disk->getField('computers_id');
103
        $computer=new Computer();
104
        $computer->getFromDB($id);
105
        $this->update($computer);
106
    }
107

    
108
    public function manageAddresses($address)
109
    {
110
        global $DB;
111
        $id=$address->getField('mainitems_id');
112
        $comp=new Computer();
113
        $comp->getFromDB($id);
114
        $this->update($comp);
115
    }
116

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