source: admin/WebConsole/includes/restfunctions.php @ 7bb39840

configure-oglivelgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since 7bb39840 was c9459a1, checked in by Natalia Serrano <natalia.serrano@…>, 17 months ago

Install new TLS certs or use those provided by the user

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