Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

glpi / src / plugins / vigilo / inc / menu.class.php @ 945f4815

History | View | Annotate | Download (4.55 KB)

1
<?php
2

    
3
class PluginVigiloMenu extends CommonGLPI
4
{
5
    const TIMEOUT = 30;
6

    
7
    /**
8
     * Name of the type
9
     *
10
     * @param $nb  integer  number of item in the type (default 0)
11
    **/
12
    public static function getTypeName($nb = 0)
13
    {
14
        return 'Vigilo';
15
    }
16

    
17
    public static function canView()
18
    {
19
        return Session::haveRight("config", UPDATE);
20
    }
21

    
22
    public static function canCreate()
23
    {
24
        return false;
25
    }
26

    
27
    public static function getMenuName()
28
    {
29
        return static::getTypeName();
30
    }
31

    
32
    public static function getAdditionalMenuOptions()
33
    {
34
        return array();
35
    }
36

    
37
    public static function getAdditionalMenuContent()
38
    {
39
        return array();
40
    }
41

    
42
    public static function displayMenu($res, $pipes)
43
    {
44
        global $DB;
45

    
46
        $disabled = '';
47
        if (!is_resource($res)) {
48
            $disabled = 'disabled';
49
        }
50

    
51
        echo <<<HTML
52
<h1>Vigilo NMS</h1><form method="post" action="?itemtype=vigilo">
53
<textarea readonly='readonly' $disabled id='vigilo_deploy' style='display: block; width: 99%; height: 380px'>
54
HTML;
55

    
56
        $needs_deploy = false;
57
        $query = <<<SQL
58
SELECT `value`
59
FROM `glpi_plugin_vigilo_config`
60
WHERE `key` = 'needs_deploy';
61
SQL;
62

    
63
        $result = $DB->query($query);
64
        if ($result) {
65
            $needs_deploy = (int) $DB->result($result, 0, "value");
66
        }
67

    
68
        if (is_resource($res)) {
69
            ini_set("max_execution_time", 0);
70
            ignore_user_abort(true);
71
            set_time_limit(0);
72

    
73
            do {
74
                $read = $exc = $pipes;
75
                $write = array();
76

    
77
                $nb = stream_select($read, $write, $exc, static::TIMEOUT, 0);
78

    
79
                // Error
80
                if ($nb === false) {
81
                    echo "UNKNOWN ERROR\n";
82
                    break;
83
                }
84

    
85
                // Timeout
86
                if ($nb === 0) {
87
                    echo "ERROR: command timed out!\n";
88
                    break;
89
                }
90
                if (count($exc)) {
91
                    echo "UNKNOWN ERROR\n";
92
                    break;
93
                }
94

    
95
                foreach ($read as $stream) {
96
                    echo htmlspecialchars(fread($stream, 1024), ENT_HTML5 | ENT_QUOTES, "utf-8");
97
                };
98

    
99
                flush();
100
                if (feof($pipes[1])) {
101
                    break;
102
                }
103
            } while (1);
104

    
105
            $info = proc_get_status($res);
106
            if ($info === false) {
107
                echo "ERROR: could not determine process status\n";
108
                $info = array('exitcode' => null);
109
            } else {
110
                if ($info["signaled"]) {
111
                    echo "Command terminated by signal ${info['termsig']}\n";
112
                }
113
                if ($info["stopped"]) {
114
                    echo "Command stopped by signal ${info['stopsig']}\n";
115
                }
116
                echo "Command exited with return code ${info['exitcode']}\n";
117
            }
118
            proc_close($res);
119

    
120
            if (isset($info['exitcode']) && 0 === $info['exitcode']) {
121
                $query = "UPDATE `glpi_plugin_vigilo_config` SET `value` = 0 WHERE `key` = 'needs_deploy';";
122
                $DB->query($query);
123
            }
124
        } elseif ($needs_deploy) {
125
            echo __('Click on "Deploy the configuration" to apply changes.', "vigilo");
126
        } else {
127
            echo __("The configuration is already up-to-date.", "vigilo");
128
        }
129

    
130
        $force = empty($_POST['force']) ? '' : 'checked';
131
        $debug = empty($_POST['debug']) ? '' : 'checked';
132
        $debug_title = htmlspecialchars(__("Display debug and progress information for Vigilo", "vigilo"), ENT_XML1 | ENT_QUOTES, "utf-8");
133
        $debug_label = htmlspecialchars(__("Display debug information", "vigilo"), ENT_XML1 | ENT_QUOTES, "utf-8");
134
        $force_title = htmlspecialchars(__("Force a full deployment rather than an incremental one", "vigilo"), ENT_XML1 | ENT_QUOTES, "utf-8");
135
        $force_label = htmlspecialchars(__("Regenerate all files", "vigilo"), ENT_XML1 | ENT_QUOTES, "utf-8");
136
        $deploy_title = htmlspecialchars(__("Deploy the configuration", "vigilo"), ENT_XML1 | ENT_QUOTES, "utf-8");
137
        echo <<<HTML
138
</textarea>
139

140
<button type="submit" name="deploy" value="1">$deploy_title</button>
141

142
<label for="debug"><input name="debug" id="debug" value="1" type="checkbox" $debug title="$debug_title"/> $debug_label</label>
143

144
<label for="force"><input name="force" id="force" value="1" type="checkbox" $force title="$force_title"/> $force_label</label>
145
HTML;
146
        Html::closeForm();
147
    }
148
}