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

Last change on this file since c2bd1e9 was 4b6cd04, checked in by OpenGnSys Support Team <soporte-og@…>, 5 years ago

#988 Read API token from new json config

This patch reads configuration from the new json file instead of using
the legacy '.cfg' file.

  • Property mode set to 100644
File size: 20.5 KB
Line 
1
2<?php
3
4define('OG_REST_URL', 'http://127.0.0.1:8888/');
5
6define('GET', 1);
7define('POST', 2);
8define('CUSTOM', 3);
9
10define('OG_REST_CMD_CLIENTS', 'clients');
11define('OG_REST_CMD_WOL', 'wol');
12define('OG_REST_CMD_SESSION', 'session');
13define('OG_REST_CMD_RUN', 'shell/run');
14define('OG_REST_CMD_OUTPUT', 'shell/output');
15define('OG_REST_CMD_POWEROFF', 'poweroff');
16define('OG_REST_CMD_REBOOT', 'reboot');
17define('OG_REST_CMD_STOP', 'stop');
18define('OG_REST_CMD_REFRESH', 'refresh');
19define('OG_REST_CMD_HARDWARE', 'hardware');
20define('OG_REST_CMD_SOFTWARE', 'software');
21define('OG_REST_CMD_CREATE_IMAGE', 'image/create');
22define('OG_REST_CMD_RESTORE_IMAGE', 'image/restore');
23define('OG_REST_CMD_SETUP', 'setup');
24define('OG_REST_CMD_CREATE_BASIC_IMAGE', 'image/create/basic');
25define('OG_REST_CMD_CREATE_INCREMENTAL_IMAGE', 'image/create/incremental');
26define('OG_REST_CMD_RESTORE_BASIC_IMAGE', 'image/restore/basic');
27define('OG_REST_CMD_RESTORE_INCREMENTAL_IMAGE', 'image/restore/incremental');
28define('OG_REST_CMD_RUN_SCHEDULE', 'run/schedule');
29define('OG_REST_CMD_RUN_TASK', 'task/run');
30define('OG_REST_CMD_CREATE_SCHEDULE', 'schedule/create');
31define('OG_REST_CMD_DELETE_SCHEDULE', 'schedule/delete');
32define('OG_REST_CMD_UPDATE_SCHEDULE', 'schedule/update');
33define('OG_REST_CMD_GET_SCHEDULE', 'schedule/get');
34define('OG_REST_CMD_MODE', 'mode');
35
36define('OG_REST_PARAM_CLIENTS', 'clients');
37define('OG_REST_PARAM_ADDR', 'addr');
38define('OG_REST_PARAM_MAC', 'mac');
39define('OG_REST_PARAM_NETMASK', 'netmask');
40define('OG_REST_PARAM_DISK', 'disk');
41define('OG_REST_PARAM_PART', 'partition');
42define('OG_REST_PARAM_RUN', 'run');
43define('OG_REST_PARAM_TYPE', 'type');
44define('OG_REST_PARAM_STATE', 'state');
45define('OG_REST_PARAM_NAME', 'name');
46define('OG_REST_PARAM_REPOS', 'repository');
47define('OG_REST_PARAM_ID', 'id');
48define('OG_REST_PARAM_CODE', 'code');
49define('OG_REST_PARAM_PROFILE', 'profile');
50define('OG_REST_PARAM_CACHE', 'cache');
51define('OG_REST_PARAM_CACHE_SIZE', 'cache_size');
52define('OG_REST_PARAM_FILE_SYSTEM', 'filesystem');
53define('OG_REST_PARAM_SIZE', 'size');
54define('OG_REST_PARAM_FORMAT', 'format');
55define('OG_REST_PARAM_PARTITION_SETUP', 'partition_setup');
56define('OG_REST_PARAM_SYNC_PARAMS', 'sync_params');
57define('OG_REST_PARAM_SYNC', 'sync');
58define('OG_REST_PARAM_DIFF', 'diff');
59define('OG_REST_PARAM_REMOVE', 'remove');
60define('OG_REST_PARAM_COMPRESS', 'compress');
61define('OG_REST_PARAM_CLEANUP', 'cleanup');
62define('OG_REST_PARAM_CLEANUP_CACHE', 'cleanup_cache');
63define('OG_REST_PARAM_REMOVE_DST', 'remove_dst');
64define('OG_REST_PARAM_PATH', 'path');
65define('OG_REST_PARAM_DIFF_ID', 'diff_id');
66define('OG_REST_PARAM_DIFF_NAME', 'diff_name');
67define('OG_REST_PARAM_METHOD', 'method');
68define('OG_REST_PARAM_ECHO', 'echo');
69define('OG_REST_PARAM_TASK', 'task');
70define('OG_REST_PARAM_WHEN', 'when');
71define('OG_REST_PARAM_YEARS', 'years');
72define('OG_REST_PARAM_MONTHS', 'months');
73define('OG_REST_PARAM_WEEKS', 'weeks');
74define('OG_REST_PARAM_WEEK_DAYS', 'week_days');
75define('OG_REST_PARAM_DAYS', 'days');
76define('OG_REST_PARAM_HOURS', 'hours');
77define('OG_REST_PARAM_AM_PM', 'am_pm');
78define('OG_REST_PARAM_MINUTES', 'minutes');
79define('OG_REST_PARAM_MODE', 'mode');
80define('OG_REST_PARAM_SCOPE_NAME', 'scope_name');
81
82define('TYPE_COMMAND', 1);
83define('TYPE_PROCEDURE', 2);
84define('TYPE_TASK', 3);
85define('OG_SCHEDULE_COMMAND', 'command');
86define('OG_SCHEDULE_PROCEDURE', 'procedure');
87define('OG_SCHEDULE_TASK', 'task');
88
89$conf_file = json_decode(file_get_contents(__DIR__ . '/../../etc/ogserver.json'), true);
90define('OG_REST_API_TOKEN', 'Authorization: ' . $conf_file['rest']['api_token']);
91
92function common_request($command, $type, $data = null) {
93
94        $json = json_encode($data);
95
96        $service_url = OG_REST_URL.$command;
97
98        $curl = curl_init($service_url);
99        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
100        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
101                OG_REST_API_TOKEN,
102        ));
103
104        switch ($type) {
105                default:
106                case GET:
107                        break;
108                case POST:
109                        curl_setopt($curl, CURLOPT_POST, true);
110                        curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
111        }
112
113        $curl_response = curl_exec($curl);
114        $info = curl_getinfo($curl);
115
116        if ($curl_response === false || $info['http_code'] != 200) {
117                syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . print_r($info, TRUE));
118                return 0;
119        }
120
121        curl_close($curl);
122
123        syslog(LOG_INFO, 'response '.$command.' ok!');
124
125        return json_decode($curl_response, true);
126}
127
128
129function shell($case, $string_ips, $command) {
130
131        $ips = explode(';',$string_ips);
132
133        switch ($case) {
134                case 1:
135                        $data = array(OG_REST_PARAM_CLIENTS => $ips,
136                                      OG_REST_PARAM_RUN => $command,
137                                      OG_REST_PARAM_ECHO => true);
138                        $command = OG_REST_CMD_RUN;
139                        break;
140                default:
141                case 2:
142                        $data = array(OG_REST_PARAM_CLIENTS => $ips);
143                        $command = OG_REST_CMD_OUTPUT;
144                        break;
145                case 3:
146                        $decoded_cmds = rawurldecode(substr($command, 4));
147                        $data = array(OG_REST_PARAM_CLIENTS => $ips,
148                                      OG_REST_PARAM_RUN => $decoded_cmds,
149                                      OG_REST_PARAM_ECHO => false);
150                        $command = OG_REST_CMD_RUN;
151                        break;
152                case 4:
153                        $decoded_cmds = rawurldecode(substr($command, 4));
154                        $command = substr($decoded_cmds, 0, -1);
155                        $data = array(OG_REST_PARAM_CLIENTS => $ips,
156                                      OG_REST_PARAM_RUN => $command,
157                                      OG_REST_PARAM_ECHO => false);
158                        $command = OG_REST_CMD_RUN;
159        }
160
161        $result = common_request($command, POST,
162                $data)[OG_REST_PARAM_CLIENTS][0]['output'];
163
164        return (is_null($result) ? '1' : $result);
165}
166
167function clients($case, $ips) {
168
169        switch ($case) {
170                case 1:
171                        $type = POST;
172                        $data = array(OG_REST_PARAM_CLIENTS => $ips);
173                        break;
174                case 2:
175                        $type = GET;
176                        $data = null;
177                        break;
178        }
179
180        $result = common_request(OG_REST_CMD_CLIENTS, $type, $data);
181
182        $trama_notificacion = "";
183        if (isset($result[OG_REST_PARAM_CLIENTS])) {
184                foreach ($result[OG_REST_PARAM_CLIENTS] as $client) {
185                        $trama_notificacion .= $client[OG_REST_PARAM_ADDR].'/'.
186                                $client[OG_REST_PARAM_STATE].';';
187                }
188        }
189
190        return $trama_notificacion;
191}
192
193function wol($type_wol, $macs, $ips, $netmasks) {
194
195        switch ($type_wol) {
196                default:
197                case 1:
198                        $wol = 'broadcast';
199                        break;
200                case 2:
201                        $wol = 'unicast';
202        }
203
204        $clients = array();
205
206        for($i=0; $i<count($macs); $i++) {
207                $clients[] = array(OG_REST_PARAM_ADDR => $ips[$i],
208                                   OG_REST_PARAM_MAC => $macs[$i],
209                                   OG_REST_PARAM_NETMASK => $netmasks[$i]);
210        }
211
212        $data = array(OG_REST_PARAM_TYPE => $wol,
213                OG_REST_PARAM_CLIENTS => $clients);
214
215        common_request(OG_REST_CMD_WOL, POST, $data);
216}
217
218function session($string_ips, $params) {
219
220        preg_match_all('!\d{1}!', $params, $matches);
221
222        $ips = explode(';',$string_ips);
223        $disk = $matches[0][0];
224        $part = $matches[0][1];
225
226        $data = array(OG_REST_PARAM_CLIENTS => $ips,
227                OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part);
228
229        common_request(OG_REST_CMD_SESSION, POST, $data);
230}
231
232function set_mode($ip, $mode) {
233        $data = array(OG_REST_PARAM_CLIENTS => array($ip),
234                      OG_REST_PARAM_MODE => $mode);
235
236        common_request(OG_REST_CMD_MODE, POST, $data);
237}
238
239function create_image($string_ips, $params) {
240
241        preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches);
242
243        $ips = explode(';',$string_ips);
244        $disk = $matches[0][0];
245        $part = $matches[0][1];
246        $code = $matches[0][2];
247        $id = $matches[0][3];
248        $name = $matches[0][4];
249        $repos = $matches[0][5];
250
251        $data = array(OG_REST_PARAM_CLIENTS => $ips,
252                OG_REST_PARAM_DISK => $disk,
253                OG_REST_PARAM_PART => $part,
254                OG_REST_PARAM_CODE => $code,
255                OG_REST_PARAM_ID => $id,
256                OG_REST_PARAM_NAME => $name,
257                OG_REST_PARAM_REPOS => $repos);
258
259        common_request(OG_REST_CMD_CREATE_IMAGE, POST, $data);
260}
261
262function restore_image($string_ips, $params) {
263
264        preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches);
265
266        $ips = explode(';',$string_ips);
267        $disk = $matches[0][0];
268        $part = $matches[0][1];
269        $image_id = $matches[0][2];
270        $name = $matches[0][3];
271        $repos = $matches[0][4];
272        $profile = $matches[0][5];
273        $type = $matches[0][6];
274
275        $data = array(OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part,
276                OG_REST_PARAM_ID => $image_id, OG_REST_PARAM_NAME => $name,
277                OG_REST_PARAM_REPOS => $repos,
278                OG_REST_PARAM_PROFILE => $profile,
279                OG_REST_PARAM_TYPE => $type,
280                OG_REST_PARAM_CLIENTS => $ips);
281
282        common_request(OG_REST_CMD_RESTORE_IMAGE, POST, $data);
283}
284
285function create_basic_image($string_ips, $params) {
286
287        preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches);
288
289        $ips = explode(';',$string_ips);
290        $disk = $matches[0][0];
291        $part = $matches[0][1];
292        $code = $matches[0][2];
293        $image_id = $matches[0][3];
294        $name = $matches[0][4];
295        $repos = $matches[0][5];
296
297        $sync = $matches[0][7]; // Syncronization method
298
299        $diff = $matches[0][8]; // Send the whole file if there are differences
300        $remove = $matches[0][9]; // Delete files at destination that are not at source
301        $compress = $matches[0][10]; // Compress before sending
302
303        $cleanup = $matches[0][11]; // Delete image before creating it
304        $cache = $matches[0][12]; // Copy image to cache
305        $cleanup_cache = $matches[0][13]; // Delete image from cache before copying
306        $remove_dst = $matches[0][14]; // Dont delete files in destination
307
308        $data = array(OG_REST_PARAM_CLIENTS => $ips,
309                OG_REST_PARAM_DISK => $disk,
310                OG_REST_PARAM_PART => $part,
311                OG_REST_PARAM_CODE => $code,
312                OG_REST_PARAM_ID => $image_id,
313                OG_REST_PARAM_NAME => $name,
314                OG_REST_PARAM_REPOS => $repos,
315                OG_REST_PARAM_SYNC_PARAMS => array(
316                        OG_REST_PARAM_SYNC => $sync,
317                        OG_REST_PARAM_DIFF => $diff,
318                        OG_REST_PARAM_REMOVE => $remove,
319                        OG_REST_PARAM_COMPRESS => $compress,
320                        OG_REST_PARAM_CLEANUP => $cleanup,
321                        OG_REST_PARAM_CACHE => $cache,
322                        OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache,
323                        OG_REST_PARAM_REMOVE_DST => $remove_dst,
324                )
325        );
326
327        common_request(OG_REST_CMD_CREATE_BASIC_IMAGE, POST, $data);
328}
329
330function create_incremental_image($string_ips, $params) {
331
332        preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches);
333
334        $ips = explode(';',$string_ips);
335        $disk = $matches[0][0];
336        $part = $matches[0][1];
337        $id = $matches[0][2];
338        $name = $matches[0][3];
339        $repos = $matches[0][4];
340        $diff_id = $matches[0][5];
341        $diff_name = $matches[0][6];
342        $path = $matches[0][7];
343        $sync = $matches[0][8];
344        $diff = $matches[0][9];
345        $remove = $matches[0][10];
346        $compress = $matches[0][11];
347        $cleanup = $matches[0][12];
348        $cache = $matches[0][13];
349        $cleanup_cache = $matches[0][14];
350        $remove_dst = $matches[0][15];
351
352        $data = array(OG_REST_PARAM_CLIENTS => $ips,
353                OG_REST_PARAM_DISK => $disk,
354                OG_REST_PARAM_PART => $part,
355                OG_REST_PARAM_ID => $id,
356                OG_REST_PARAM_NAME => $name,
357                OG_REST_PARAM_REPOS => $repos,
358                OG_REST_PARAM_SYNC_PARAMS => array(
359                        OG_REST_PARAM_SYNC => $sync,
360                        OG_REST_PARAM_PATH => $path,
361                        OG_REST_PARAM_DIFF => $diff,
362                        OG_REST_PARAM_DIFF_ID => $diff_id,
363                        OG_REST_PARAM_DIFF_NAME => $diff_name,
364                        OG_REST_PARAM_REMOVE => $remove,
365                        OG_REST_PARAM_COMPRESS => $compress,
366                        OG_REST_PARAM_CLEANUP => $cleanup,
367                        OG_REST_PARAM_CACHE => $cache,
368                        OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache,
369                        OG_REST_PARAM_REMOVE_DST => $remove_dst)
370        );
371
372        common_request(OG_REST_CMD_CREATE_INCREMENTAL_IMAGE, POST, $data);
373}
374
375function restore_basic_image($string_ips, $params) {
376
377        preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches);
378
379        $ips = explode(';',$string_ips);
380        $disk = $matches[0][0];
381        $part = $matches[0][1];
382        $image_id = $matches[0][2];
383        $name = $matches[0][3];
384        $repos = $matches[0][4];
385        $profile = $matches[0][5];
386
387        $path = $matches[0][6];
388        $method = $matches[0][7];
389        $sync = $matches[0][8]; // Syncronization method
390
391        $type = $matches[0][9];
392
393        $diff = $matches[0][10]; // Send the whole file if there are differences
394        $remove = $matches[0][11]; // Delete files at destination that are not at source
395        $compress = $matches[0][12]; // Compress before sending
396
397        $cleanup = $matches[0][13]; // Delete image before creating it
398        $cache = $matches[0][14]; // Copy image to cache
399        $cleanup_cache = $matches[0][15]; // Delete image from cache before copying
400        $remove_dst = $matches[0][16]; // Dont delete files in destination
401
402        $data = array(OG_REST_PARAM_CLIENTS => $ips,
403                OG_REST_PARAM_DISK => $disk,
404                OG_REST_PARAM_PART => $part,
405                OG_REST_PARAM_ID => $image_id,
406                OG_REST_PARAM_NAME => $name,
407                OG_REST_PARAM_REPOS => $repos,
408                OG_REST_PARAM_PROFILE => $profile,
409                OG_REST_PARAM_TYPE => $type,
410                OG_REST_PARAM_SYNC_PARAMS => array(
411                        OG_REST_PARAM_PATH => $path,
412                        OG_REST_PARAM_METHOD => $method,
413                        OG_REST_PARAM_SYNC => $sync,
414                        OG_REST_PARAM_DIFF => $diff,
415                        OG_REST_PARAM_REMOVE => $remove,
416                        OG_REST_PARAM_COMPRESS => $compress,
417                        OG_REST_PARAM_CLEANUP => $cleanup,
418                        OG_REST_PARAM_CACHE => $cache,
419                        OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache,
420                        OG_REST_PARAM_REMOVE_DST => $remove_dst,
421                )
422        );
423
424        common_request(OG_REST_CMD_RESTORE_BASIC_IMAGE, POST, $data);
425}
426
427function restore_incremental_image($string_ips, $params) {
428
429        preg_match_all('/(?<=\=)[^\r]*(?=\r)?/', $params, $matches);
430
431        $ips = explode(';',$string_ips);
432        $disk = $matches[0][0];
433        $part = $matches[0][1];
434        $image_id = $matches[0][2];
435        $name = $matches[0][3];
436        $repos = $matches[0][4];
437        $profile = $matches[0][5];
438        $diff_id = $matches[0][6];
439        $diff_name = $matches[0][7];
440        $path = $matches[0][8];
441        $method = $matches[0][9];
442        $sync = $matches[0][10];
443        $type = $matches[0][11];
444        $diff = $matches[0][12];
445        $remove = $matches[0][13];
446        $compress = $matches[0][14];
447        $cleanup = $matches[0][15];
448        $cache = $matches[0][16];
449        $cleanup_cache = $matches[0][17];
450        $remove_dst = $matches[0][18];
451
452        $data = array(OG_REST_PARAM_CLIENTS => $ips,
453                OG_REST_PARAM_DISK => $disk,
454                OG_REST_PARAM_PART => $part,
455                OG_REST_PARAM_ID => $image_id,
456                OG_REST_PARAM_NAME => $name,
457                OG_REST_PARAM_REPOS => $repos,
458                OG_REST_PARAM_PROFILE => $profile,
459                OG_REST_PARAM_TYPE => $type,
460                OG_REST_PARAM_SYNC_PARAMS => array(
461                        OG_REST_PARAM_DIFF_ID => $diff_id,
462                        OG_REST_PARAM_DIFF_NAME => $diff_name,
463                        OG_REST_PARAM_PATH => $path,
464                        OG_REST_PARAM_METHOD => $method,
465                        OG_REST_PARAM_SYNC => $sync,
466                        OG_REST_PARAM_DIFF => $diff,
467                        OG_REST_PARAM_REMOVE => $remove,
468                        OG_REST_PARAM_COMPRESS => $compress,
469                        OG_REST_PARAM_CLEANUP => $cleanup,
470                        OG_REST_PARAM_CACHE => $cache,
471                        OG_REST_PARAM_CLEANUP_CACHE => $cleanup_cache,
472                        OG_REST_PARAM_REMOVE_DST => $remove_dst,
473                )
474        );
475
476        common_request(OG_REST_CMD_RESTORE_INCREMENTAL_IMAGE, POST, $data);
477}
478
479function poweroff($string_ips) {
480
481        $ips = explode(';',$string_ips);
482
483        $data = array(OG_REST_PARAM_CLIENTS => $ips);
484
485        common_request(OG_REST_CMD_POWEROFF, POST, $data);
486}
487
488function reboot($string_ips) {
489
490        $ips = explode(';',$string_ips);
491
492        $data = array(OG_REST_PARAM_CLIENTS => $ips);
493
494        common_request(OG_REST_CMD_REBOOT, POST, $data);
495}
496
497function stop($string_ips) {
498
499        $ips = explode(';',$string_ips);
500
501        $data = array(OG_REST_PARAM_CLIENTS => $ips);
502
503        common_request(OG_REST_CMD_STOP, POST, $data);
504}
505
506function refresh($string_ips) {
507
508        $ips = explode(';',$string_ips);
509
510        $data = array(OG_REST_PARAM_CLIENTS => $ips);
511
512        common_request(OG_REST_CMD_REFRESH, POST, $data);
513}
514
515function hardware($string_ips) {
516
517        $ips = explode(';',$string_ips);
518
519        $data = array(OG_REST_PARAM_CLIENTS => $ips);
520
521        common_request(OG_REST_CMD_HARDWARE, POST, $data);
522}
523
524function software($string_ips, $params) {
525
526        preg_match_all('/(?<=\=)(.*?)(?=\r)/', $params, $matches);
527
528        $ips = explode(';',$string_ips);
529        $disk = $matches[0][0];
530        $part = $matches[0][1];
531
532        $data = array(OG_REST_PARAM_CLIENTS => $ips,
533                OG_REST_PARAM_DISK => $disk,
534                OG_REST_PARAM_PART => $part);
535
536        common_request(OG_REST_CMD_SOFTWARE, POST, $data);
537}
538
539function setup($string_ips, $params) {
540
541        preg_match_all('/(?<=\=)(?!dis)(.*?)((?=\*)|(?=\r)|(?=\!)|(?=\%))/',
542                $params, $matches);
543
544        $ips = explode(';',$string_ips);
545        $disk = $matches[0][0];
546        $cache = $matches[0][2];
547        $cache_size = $matches[0][3];
548        $partition_number = array();
549        $partition_code = array();
550        $file_system = array();
551        $part_size = array();
552        $format = array();
553        for ($x = 0; $x < 4; $x++) {
554                $partition_number[$x] = $matches[0][4 + 5 * $x];
555                $partition_code[$x] = $matches[0][5 + 5 * $x];
556                $file_system[$x] = $matches[0][6 + 5 * $x];
557                $part_size[$x] = $matches[0][7 + 5 * $x];
558                $format[$x] = $matches[0][8 + 5 * $x];
559        }
560
561        $data = array(
562                OG_REST_PARAM_CLIENTS => $ips,
563                OG_REST_PARAM_DISK => $disk,
564                OG_REST_PARAM_CACHE => $cache,
565                OG_REST_PARAM_CACHE_SIZE => $cache_size,
566                OG_REST_PARAM_PARTITION_SETUP => array()
567        );
568
569        for ($i = 0; $i < sizeof($partition_number); $i++) {
570                $partition_setup = array(
571                        OG_REST_PARAM_PART => $partition_number[$i],
572                        OG_REST_PARAM_CODE => $partition_code[$i],
573                        OG_REST_PARAM_FILE_SYSTEM => $file_system[$i],
574                        OG_REST_PARAM_SIZE => $part_size[$i],
575                        OG_REST_PARAM_FORMAT => $format[$i]
576                );
577                array_push($data[OG_REST_PARAM_PARTITION_SETUP], $partition_setup);
578        }
579
580        common_request(OG_REST_CMD_SETUP, POST, $data);
581}
582
583function run_schedule($string_ips) {
584        $ips = explode(';',$string_ips);
585        $data = array(OG_REST_PARAM_CLIENTS => $ips);
586        common_request(OG_REST_CMD_RUN_SCHEDULE, POST, $data);
587}
588
589function run_task($task_id) {
590        $data = array(OG_REST_PARAM_TASK => $task_id);
591        return common_request(OG_REST_CMD_RUN_TASK, POST, $data);
592}
593
594function create_schedule($task_id, $type, $name, $years, $months, $weeks,
595                         $week_days, $days, $hours, $am_pm, $minutes) {
596        $type_string;
597
598        switch ($type) {
599        case TYPE_COMMAND:
600                $type_string = OG_SCHEDULE_COMMAND;
601                break;
602        case TYPE_PROCEDURE:
603                $type_string = OG_SCHEDULE_PROCEDURE;
604                break;
605        case TYPE_TASK:
606        default:
607                $type_string = OG_SCHEDULE_TASK;
608        }
609
610        $data = array (
611                OG_REST_PARAM_TASK => $task_id,
612                OG_REST_PARAM_TYPE => $type_string,
613                OG_REST_PARAM_NAME => $name,
614                OG_REST_PARAM_WHEN => array (
615                        OG_REST_PARAM_YEARS => intval($years),
616                        OG_REST_PARAM_MONTHS => intval($months),
617                        OG_REST_PARAM_WEEKS => intval($weeks),
618                        OG_REST_PARAM_WEEK_DAYS => intval($week_days),
619                        OG_REST_PARAM_DAYS => intval($days),
620                        OG_REST_PARAM_HOURS => intval($hours),
621                        OG_REST_PARAM_AM_PM => intval($am_pm),
622                        OG_REST_PARAM_MINUTES => intval($minutes)
623                )
624        );
625
626        return common_request(OG_REST_CMD_CREATE_SCHEDULE, POST, $data);
627}
628
629function delete_schedule($schedule_id) {
630
631        $data = array (
632                OG_REST_PARAM_ID => $schedule_id,
633        );
634
635        return common_request(OG_REST_CMD_DELETE_SCHEDULE, POST, $data);
636}
637
638function update_schedule($schedule_id, $task_id, $name, $years, $months, $days,
639                         $hours, $am_pm, $minutes) {
640
641        $data = array (
642                OG_REST_PARAM_ID => $schedule_id,
643                OG_REST_PARAM_TASK => $task_id,
644                OG_REST_PARAM_NAME => $name,
645                OG_REST_PARAM_WHEN => array (
646                        OG_REST_PARAM_YEARS => intval($years),
647                        OG_REST_PARAM_MONTHS => intval($months),
648                        OG_REST_PARAM_DAYS => intval($days),
649                        OG_REST_PARAM_HOURS => intval($hours),
650                        OG_REST_PARAM_AM_PM => intval($am_pm),
651                        OG_REST_PARAM_MINUTES => intval($minutes)
652                )
653        );
654
655        return common_request(OG_REST_CMD_UPDATE_SCHEDULE, POST, $data);
656}
657
658function get_schedule($task_id = null, $schedule_id = null) {
659        if (isset($task_id))
660                $data = array(OG_REST_PARAM_TASK => strval($task_id));
661        else if (isset($schedule_id))
662                $data = array(OG_REST_PARAM_ID => strval($schedule_id));
663        else
664                $data = null;
665
666        $result = common_request(OG_REST_CMD_GET_SCHEDULE, POST, $data);
667        return $result;
668}
669
670/*
671 * @function multiRequest.
672 * @param    URLs array (may include header and POST data), cURL options array.
673 * @return   Array of arrays with JSON requests and response codes.
674 * @warning  Default options: does not verifying certificate, connection timeout 200 ms.
675 * @Date     2015-10-14
676 */
677function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT_MS => 500)) {
678 
679  // array of curl handles
680  $curly = array();
681  // Data to be returned (response data and code)
682  $result = array();
683 
684  // multi handle
685  $mh = curl_multi_init();
686 
687  // loop through $data and create curl handles
688  // then add them to the multi-handle
689  foreach ($data as $id => $d) {
690 
691
692    $curly[$id] = curl_init();
693 
694    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
695    curl_setopt($curly[$id], CURLOPT_URL, $url);
696    // HTTP headers?
697    if (is_array($d) && !empty($d['header'])) {
698       curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']);
699    } else {
700       curl_setopt($curly[$id], CURLOPT_HEADER, 0);
701    }
702    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
703 
704    // post?
705    if (is_array($d)) {
706      if (!empty($d['post'])) {
707        curl_setopt($curly[$id], CURLOPT_POST, 1);
708        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
709      }
710    }
711
712    // extra options?
713    if (!empty($options)) {
714      curl_setopt_array($curly[$id], $options);
715    }
716 
717    curl_multi_add_handle($mh, $curly[$id]);
718  }
719 
720  // execute the handles
721  $running = null;
722  do {
723    curl_multi_exec($mh, $running);
724  } while($running > 0);
725 
726 
727  // Get content and HTTP code, and remove handles
728  foreach($curly as $id => $c) {
729    $result[$id]['data'] = curl_multi_getcontent($c);
730    $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE);
731    curl_multi_remove_handle($mh, $c);
732  }
733
734 // all done
735  curl_multi_close($mh);
736 
737  return $result;
738}
739
Note: See TracBrowser for help on using the repository browser.