r1202 + all options have moved into the database
[racktables] / inc / gateways.php
CommitLineData
d33645ff
DO
1<?
2/*
3*
4* This file contains gateway functions for RackTables.
5* A gateway is an external executable, which provides
6* read-only or read-write access to some external entities.
7* Each gateway accepts its own list of command-line args
8* and then reads its stdin for requests. Each request consists
9* of one line and results in exactly one line of reply.
10* The replies must have the following syntax:
11* OK<space>any text up to the end of the line
12* ERR<space>any text up to the end of the line
13*
14*/
15
16
17// This function launches specified gateway with specified
18// command-line arguments and feeds it with the commands stored
19// in the second arg as array.
20// The answers are stored in another array, which is returned
21// by this function. In the case when a gateway cannot be found,
22// finishes prematurely or exits with non-zero return code,
23// a single-item array is returned with the only "ERR" record,
24// which explains the reason.
2da7c9b0 25function queryGateway ($gwname, $questions)
d33645ff
DO
26{
27 $execpath = "./gateways/{$gwname}/main";
d33645ff
DO
28 $dspec = array
29 (
30 0 => array ("pipe", "r"),
31 1 => array ("pipe", "w"),
32 2 => array ("file", "/dev/null", "a")
33 );
34 $pipes = array();
2da7c9b0 35 $gateway = proc_open ($execpath, $dspec, $pipes);
d33645ff
DO
36 if (!is_resource ($gateway))
37 return array ('ERR proc_open() failed in queryGateway()');
38
39// Dialogue starts. Send all questions.
40 foreach ($questions as $q)
41 fwrite ($pipes[0], "$q\n");
42 fclose ($pipes[0]);
43
44// Fetch replies.
2da7c9b0 45 $answers = array ();
d33645ff
DO
46 while (!feof($pipes[1]))
47 {
48 $a = fgets ($pipes[1]);
49 if (empty ($a))
50 continue;
075aeb88
DO
51 // Somehow I got a space appended at the end. Kick it.
52 $answers[] = trim ($a);
d33645ff
DO
53 }
54 fclose($pipes[1]);
55
56 $retval = proc_close ($gateway);
57 if ($retval != 0)
58 return array ("ERR gateway '${gwname}' returned ${retval}");
59 return $answers;
60}
61
62?>