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

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

#980 Update POST /mode to the new ogServer API

This patch selects the machines whose modes need to be changed using the
client's IP rather than their scope names.

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