source: admin/WebConsole/includes/restfunctions.php @ 23dc851

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

#942 schedule commands (web)

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