source: admin/WebConsole/includes/restfunctions.php @ e378505

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since e378505 was e378505, checked in by OpenGnSys Support Team <soporte-og@…>, 6 years ago

#915 The order of the json field is irrelevant in GET /clients

Either:

{ "addr" : "192.168.2.1", "state" : "OPG" }

or:

{ "state" : "OPG", "addr" : "192.168.2.1" }

should be accepted, this patch updates the web console parser to accept
both.

  • Property mode set to 100644
File size: 5.7 KB
Line 
1<?php
2
3define('OG_REST_URL', 'http://127.0.0.1:8888/');
4
5define('GET', 1);
6define('POST', 2);
7define('CUSTOM', 3);
8
9define('OG_REST_CMD_CLIENTS', 'clients');
10define('OG_REST_CMD_WOL', 'wol');
11define('OG_REST_CMD_SESSION', 'session');
12define('OG_REST_CMD_RUN', 'shell/run');
13define('OG_REST_CMD_OUTPUT', 'shell/output');
14define('OG_REST_CMD_POWEROFF', 'poweroff');
15define('OG_REST_CMD_REBOOT', 'reboot');
16define('OG_REST_CMD_STOP', 'stop');
17define('OG_REST_CMD_REFRESH', 'refresh');
18
19define('OG_REST_PARAM_CLIENTS', 'clients');
20define('OG_REST_PARAM_ADDR', 'addr');
21define('OG_REST_PARAM_MAC', 'mac');
22define('OG_REST_PARAM_DISK', 'disk');
23define('OG_REST_PARAM_PART', 'partition');
24define('OG_REST_PARAM_RUN', 'run');
25define('OG_REST_PARAM_TYPE', 'type');
26define('OG_REST_PARAM_STATE', 'state');
27
28function common_request($command, $type, $data = null, $custom = 'GET') {
29
30        $json = json_encode($data);
31
32        $service_url = OG_REST_URL.$command;
33
34        $curl = curl_init($service_url);
35        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
36
37        switch ($type) {
38                default:
39                case GET:
40                        break;
41                case POST:
42                        curl_setopt($curl, CURLOPT_POST, true);
43                        curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
44        }
45
46        $curl_response = curl_exec($curl);
47        $info = curl_getinfo($curl);
48
49        if ($curl_response === false || $info['http_code'] != 200) {
50                syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . var_export($info));
51                return 0;
52        }
53
54        curl_close($curl);
55
56        syslog(LOG_INFO, 'response '.$command.' ok!');
57
58        return json_decode($curl_response, true);
59}
60
61
62function shell($case, $string_ips, $command) {
63
64        $ips = explode(';',$string_ips);
65
66        switch ($case) {
67                case 1:
68                        $data = array(OG_REST_PARAM_CLIENTS => $ips,
69                                OG_REST_PARAM_RUN => $command);
70                        $command = OG_REST_CMD_RUN;
71                        break;
72                default:
73                case 2:
74                        $data = array(OG_REST_PARAM_CLIENTS => $ips);
75                        $command = OG_REST_CMD_OUTPUT;
76        }
77
78        $result = common_request($command, POST,
79                $data)[OG_REST_PARAM_CLIENTS][0]['output'];
80
81        return (is_null($result) ? '1' : $result);
82}
83
84function clients($case, $ips) {
85
86        switch ($case) {
87                case 1:
88                        $type = POST;
89                        $data = array(OG_REST_PARAM_CLIENTS => $ips);
90                        break;
91                case 2:
92                        $type = GET;
93                        break;
94        }
95
96        $result = common_request(OG_REST_CMD_CLIENTS, $type, $data);
97
98        foreach ($result[OG_REST_PARAM_CLIENTS] as $client) {
99                $trama_notificacion = $trama_notificacion.$client[OG_REST_PARAM_ADDR].'/'.
100                        $client[OG_REST_PARAM_STATE].';';
101        }
102
103        return $trama_notificacion;
104}
105
106function wol($type_wol, $macs, $ips) {
107
108        switch ($type_wol) {
109                default:
110                case 1:
111                        $wol = 'broadcast';
112                        break;
113                case 2:
114                        $wol = 'unicast';
115        }
116
117        $clients = array();
118
119        for($i=0; $i<count($macs); $i++) {
120                $clients[] = array(OG_REST_PARAM_ADDR => $ips[$i],
121                        OG_REST_PARAM_MAC => $macs[$i]);
122        }
123
124        $data = array(OG_REST_PARAM_TYPE => $wol,
125                OG_REST_PARAM_CLIENTS => $clients);
126
127        common_request(OG_REST_CMD_WOL, POST, $data);
128}
129
130function session($string_ips, $params) {
131
132        preg_match_all('!\d{1}!', $params, $matches);
133
134        $ips = explode(';',$string_ips);
135        $disk = $matches[0][0];
136        $part = $matches[0][1];
137
138        $data = array(OG_REST_PARAM_CLIENTS => $ips,
139                OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part);
140
141        common_request(OG_REST_CMD_SESSION, POST, $data);
142}
143
144function poweroff($string_ips) {
145
146        $ips = explode(';',$string_ips);
147
148        $data = array(OG_REST_PARAM_CLIENTS => $ips);
149
150        common_request(OG_REST_CMD_POWEROFF, POST, $data);
151}
152
153function reboot($string_ips) {
154
155        $ips = explode(';',$string_ips);
156
157        $data = array(OG_REST_PARAM_CLIENTS => $ips);
158
159        common_request(OG_REST_CMD_REBOOT, POST, $data);
160}
161
162function stop($string_ips) {
163
164        $ips = explode(';',$string_ips);
165
166        $data = array(OG_REST_PARAM_CLIENTS => $ips);
167
168        common_request(OG_REST_CMD_STOP, POST, $data);
169}
170
171function refresh($string_ips) {
172
173        $ips = explode(';',$string_ips);
174
175        $data = array(OG_REST_PARAM_CLIENTS => $ips);
176
177        common_request(OG_REST_CMD_REFRESH, POST, $data);
178}
179
180/*
181 * @function multiRequest.
182 * @param    URLs array (may include header and POST data), cURL options array.
183 * @return   Array of arrays with JSON requests and response codes.
184 * @warning  Default options: does not verifying certificate, connection timeout 200 ms.
185 * @Date     2015-10-14
186 */
187function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT_MS => 500)) {
188 
189  // array of curl handles
190  $curly = array();
191  // Data to be returned (response data and code)
192  $result = array();
193 
194  // multi handle
195  $mh = curl_multi_init();
196 
197  // loop through $data and create curl handles
198  // then add them to the multi-handle
199  foreach ($data as $id => $d) {
200 
201
202    $curly[$id] = curl_init();
203 
204    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
205    curl_setopt($curly[$id], CURLOPT_URL, $url);
206    // HTTP headers?
207    if (is_array($d) && !empty($d['header'])) {
208       curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']);
209    } else {
210       curl_setopt($curly[$id], CURLOPT_HEADER, 0);
211    }
212    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
213 
214    // post?
215    if (is_array($d)) {
216      if (!empty($d['post'])) {
217        curl_setopt($curly[$id], CURLOPT_POST, 1);
218        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
219      }
220    }
221
222    // extra options?
223    if (!empty($options)) {
224      curl_setopt_array($curly[$id], $options);
225    }
226 
227    curl_multi_add_handle($mh, $curly[$id]);
228  }
229 
230  // execute the handles
231  $running = null;
232  do {
233    curl_multi_exec($mh, $running);
234  } while($running > 0);
235 
236 
237  // Get content and HTTP code, and remove handles
238  foreach($curly as $id => $c) {
239    $result[$id]['data'] = curl_multi_getcontent($c);
240    $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE);
241    curl_multi_remove_handle($mh, $c);
242  }
243
244 // all done
245  curl_multi_close($mh);
246 
247  return $result;
248}
249
Note: See TracBrowser for help on using the repository browser.