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

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 f4e69d5 was 0b97d96, checked in by Ramón M. Gómez <ramongomez@…>, 6 years ago

#834: Setting a default value for some PHP variables to avoid warnings.

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