1 | |
2 | //////////////////////////////////////////////////////////////////////////////// |
3 | // |
4 | // File: ecrc.php: ECR Daemon Client Library |
5 | // Description: Php Module interface to ecrd (ECR Daemon). |
6 | // Author: Andrew@DeFaria.com |
7 | // Created: Tue Feb 15 09:40:57 PST 2005 |
8 | // Modified: |
9 | // Language: Php |
10 | // |
11 | // (c) Copyright 2005, LynuxWorks, all rights reserved. |
12 | // |
13 | //////////////////////////////////////////////////////////////////////////////// |
14 | require_once "Net/Socket.php"; |
15 | |
16 | define ("SERVER", "lynx12"); |
17 | define ("PORT", 1500); |
18 | |
19 | $ecrserver; |
20 | $verbose = $_REQUEST [verbose]; |
21 | $debug = $_REQUEST [debug]; |
22 | |
23 | function verbose ($msg) { |
24 | global $verbose; |
25 | |
26 | if ($verbose == 1) { |
27 | print "$msg<br>"; |
28 | } // if |
29 | } // verbose |
30 | |
31 | function debug ($msg) { |
32 | global $debug; |
33 | |
34 | if ($debug == 1) { |
35 | print "DEBUG: $msg<br>"; |
36 | } // if |
37 | } // debug |
38 | |
39 | function Connect ($host, $port = 1500) { |
40 | global $ecrserver; |
41 | |
42 | debug ("Connect ($host, $port)"); |
43 | |
44 | $ecrserver = ConnectToServer ($host, $port); |
45 | |
46 | if (is_object ($ecrserver)) { |
47 | verbose ("Connected to $host"); |
48 | SendServerAck ($ecrserver); |
49 | } // if |
50 | |
51 | return $ecrserver; |
52 | } // Connect |
53 | |
54 | function Disconnect () { |
55 | global $ecrserver; |
56 | global $command; |
57 | |
58 | $msg; |
59 | |
60 | if ($ecrserver) { |
61 | if ($command == "shutdown") { |
62 | $msg = "Disconnected from server - shutdown server"; |
63 | } else { |
64 | $command = "quit"; |
65 | $msg = "Disconnected from server"; |
66 | } // if |
67 | SendServerCmd ($ecrserver, $command); |
68 | GetServerAck ($ecrserver); |
69 | verbose ($msg); |
70 | $ecrserver->disconnect (); |
71 | } // if |
72 | } // Disconnect |
73 | |
74 | function GetECRRecord ($ecr) { |
75 | global $ecrserver; |
76 | |
77 | $fields; |
78 | |
79 | debug ("ENTER GetECRRecord ($ecr)"); |
80 | if (!$ecrserver) { |
81 | verbose ("Not connected to server yet!"); |
82 | verbose ("Attempting connection to $default_server..."); |
83 | if (!Connect (SERVER)) { |
84 | print "Unable to connect to server ". SERVER . "<br>"; |
85 | exit (1); |
86 | } // if |
87 | } // if |
88 | |
89 | SendServerCmd ($ecrserver, $ecr); |
90 | GetServerAck ($ecrserver); |
91 | |
92 | if ($ecr == "*") { |
93 | verbose ("Getting all ECRs"); |
94 | $fields = GetServerList ($ecrserver); |
95 | } else { |
96 | verbose ("Getting specific ECR $ecr"); |
97 | $fields = GetServerResponse ($ecrserver); |
98 | } // if |
99 | |
100 | SendServerAck ($ecrserver); |
101 | |
102 | return $fields; |
103 | } // GetECRRecord |
104 | |
105 | function Shutdown () { |
106 | global $command; |
107 | |
108 | verbose ("Sending disconnect command to server"); |
109 | $command = "quit"; |
110 | Disconnect (); |
111 | } // Shutdown |
112 | |
113 | function ConnectToServer ($host, $port = 1500) { |
114 | $socket = new Net_Socket (); |
115 | |
116 | debug ("Socket created... Attempting to connect to $host:$port"); |
117 | // create a tcp connection to the specified host and port |
118 | if (@$socket->connect ($host, $port) == 1) { |
119 | verbose ("Socket $socket connected"); |
120 | } else { |
121 | print "Unable to connect to server $host:$port!<br>"; |
122 | exit (1); |
123 | } // if |
124 | |
125 | return $socket; |
126 | } // ConnectToServer |
127 | |
128 | function SendServerAck ($server) { |
129 | $server->write ("ACK" . "\n"); |
130 | } // SendServerAck |
131 | |
132 | function GetServerAck ($server) { |
133 | while ($srvresp = $server->readLine ()) { |
134 | if ($srvresp == "ACK") { |
135 | return; |
136 | } // if |
137 | verbose ("Received $srvresp from server - expected ACK"); |
138 | } // while |
139 | } // GetServerAck |
140 | |
141 | function GetServerList ($server) { |
142 | $ecrs = array (); |
143 | |
144 | while ($srvresp = $server->readLine ()) { |
145 | if ($srvresp == "ACK") { |
146 | break; |
147 | } // if |
148 | |
149 | if (preg_match ("/ECR.*was not found/", $srvresp)) { |
150 | return; |
151 | } else { |
152 | array_push ($ecrs, $srvresp); |
153 | } // if |
154 | } // while |
155 | |
156 | return $ecrs; |
157 | } # GetServerList |
158 | |
159 | function GetServerResponse ($server) { |
160 | $fields; |
161 | |
162 | while ($srvresp = $server->readLine ()) { |
163 | if ($srvresp == "ACK") { |
164 | break; |
165 | } // if |
166 | |
167 | if (preg_match ("/ECR.*was not found/", $srvresp)) { |
168 | return; |
169 | } else { |
170 | preg_match ("/(^\w+):\s+(.*)/s", $srvresp, $matches); |
171 | $value = str_replace ("\\n", "\n", $matches [2]); |
172 | $fields {$matches [1]} = $value; |
173 | } // if |
174 | } // while |
175 | |
176 | return $fields; |
177 | } // GetServerResponse |
178 | |
179 | function SendServerCmd ($server, $command) { |
180 | $server->write ($command . "\n"); |
181 | } // SendServerCmd |
182 | ?> |