source: admin/WebConsole/includes/restfunctions.php @ 58fc387

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

#942 Add weeks and week_days to the webconsole schedule (web)

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