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

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

#942 Use both task_id and schedule_id in /schedule/get (web)

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