source: admin/WebConsole/includes/restfunctions.php @ 953594a3

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

#1001 Remove trailing \r and \n in some scripts

We find that scripts sent by "Assistants"/"Asistentes" are losing the
last character and scripts send by "Run Script"/"Ejecutar Script" have a
trailing line feed (\n).

This is happening because shell() function, which calls "/shell/run" of
ogServer REST API, expects scripts send by "Asistentes" and "Run Script"
to have two trailing characters and removes them. But "Assistants" only
put one trailing char (\r) and "Run Script" put three trailing chars
(\n\n\r).

To avoid problems when running scripts without the last char or with
trailing line feed this patch updates WebConsole? to:

1) "Assistants" no longer put one trailing character at the end

of the script

2) "Run Script" no longer puts three trailing characters at the

end of the script

3) shell() expects no trailing chars in scripts sent by

"Assistants" and "Run Script".

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