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

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 b3467f7 was c9cfd44, checked in by OpenGnSys Support Team <soporte-og@…>, 6 years ago

#915 adapt web console to use new refresh command in REST API

SocketHidra? "actualizar" has been replaced by POST /refresh.

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