source: admin/WebConsole/includes/restfunctions.php @ 1f43ddc

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

#986 Rename to ogserver

Step forward to rename all ogAdmServer references to ogServer

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