* cleanup
[citadel.git] / ctdlphp / ctdlprotocol.php
1 <?PHP
2
3 // $Id$
4 // 
5 // Implements various Citadel server commands.
6 //
7 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
8 // This program is released under the terms of the GNU General Public License.
9 //
10
11
12 //
13 // serv_gets() -- generic function to read one line of text from the server
14 //
15 function serv_gets() {
16         global $clientsocket;
17
18         $buf = fgets($clientsocket, 4096);              // Read line
19         $buf = substr($buf, 0, (strlen($buf)-1) );      // strip trailing LF
20         return $buf;
21 }
22
23
24 //
25 // serv_puts() -- generic function to write one line of text to the server
26 //
27 function serv_puts($buf) {
28         global $clientsocket;
29         
30         fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
31 }
32
33
34
35 //
36 // Identify this client, and the hostname where the user is, to Citadel.
37 //
38 function ctdl_iden() {
39         global $clientsocket;
40
41         serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
42         $buf = serv_gets();
43 }
44
45
46
47 //
48 // login_existing_user() -- attempt to login using a supplied username/password
49 // Returns an array with two variables:
50 // 0. TRUE or FALSE to determine success or failure
51 // 1. String error message (if relevant)
52 //
53 function login_existing_user($user, $pass) {
54         global $clientsocket;
55
56         serv_puts("USER " . $user);
57         $resp = serv_gets();
58         if (substr($resp, 0, 1) != "3") {
59                 return array(FALSE, substr($resp, 4));
60         }
61
62         serv_puts("PASS " . $pass);
63         $resp = serv_gets();
64         if (substr($resp, 0, 1) != "2") {
65                 return array(FALSE, substr($resp, 4));
66         }
67
68         $_SESSION["username"] = $user;
69         $_SESSION["password"] = $pass;
70         become_logged_in(substr($resp, 4));
71
72         return array(TRUE, "Login successful.  Have fun.");
73 }
74
75
76 //
77 // create_new_user() -- attempt to create a new user 
78 //                      using a supplied username/password
79 // Returns an array with two variables:
80 // 0. TRUE or FALSE to determine success or failure
81 // 1. String error message (if relevant)
82 //
83 function create_new_user($user, $pass) {
84         global $clientsocket;
85
86         serv_puts("NEWU " . $user);
87         $resp = serv_gets();
88         if (substr($resp, 0, 1) != "2") {
89                 return array(FALSE, substr($resp, 4));
90         }
91
92         serv_puts("SETP " . $pass);
93         $resp = serv_gets();
94         if (substr($resp, 0, 1) != "2") {
95                 return array(FALSE, substr($resp, 4));
96         }
97
98         $_SESSION["username"] = $user;
99         $_SESSION["password"] = $pass;
100         become_logged_in(substr($resp, 4));
101
102         return array(TRUE, "Login successful.  Have fun.");
103 }
104
105
106 //
107 // Code common to both existing-user and new-user logins
108 //
109 function become_logged_in($server_parms) {
110         $_SESSION["logged_in"] = 1;
111
112         $tok = strtok($server_parms, "|");
113         if ($tok) $thisline["username"] = $tok;
114
115         $tok = strtok("|");
116         if ($tok) $thisline["axlevel"] = $tok;
117                 
118         $tok = strtok("|");
119         if ($tok) $thisline["calls"] = $tok;
120                 
121         $tok = strtok("|");
122         if ($tok) $thisline["posts"] = $tok;
123                 
124         $tok = strtok("|");
125         if ($tok) $thisline["userflags"] = $tok;
126                 
127         $tok = strtok("|");
128         if ($tok) $thisline["usernum"] = $tok;
129                 
130         $tok = strtok("|");
131         if ($tok) $thisline["lastcall"] = $tok;
132                 
133         ctdl_goto("_BASEROOM_");
134 }
135
136
137
138 //
139 // Learn all sorts of interesting things about the Citadel server to
140 // which we are connected.
141 //
142 function ctdl_get_serv_info() {
143         serv_puts("INFO");
144         $buf = serv_gets();
145         if (substr($buf, 0, 1) == "1") {
146                 $i = 0;
147                 do {
148                         $buf = serv_gets();
149                         if ($i == 1) $_SESSION["serv_nodename"] = $buf;
150                         if ($i == 2) $_SESSION["serv_humannode"] = $buf;
151                         if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
152                         if ($i == 4) $_SESSION["serv_software"] = $buf;
153                         if ($i == 6) $_SESSION["serv_city"] = $buf;
154                         if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
155                         $i = $i + 1;
156                 } while (strcasecmp($buf, "000"));
157         }
158
159 }
160
161
162 //
163 // Display a system banner.  (Returns completed HTML.)
164 // (This is probably temporary because it outputs more or less finalized
165 // markup.  For now it's just usable.)
166 //
167 function ctdl_mesg($msgname) {
168         global $clientsocket;
169
170         $msgtext = "<DIV ALIGN=CENTER>\n";
171
172         serv_puts("MESG " . $msgname);
173         $response = serv_gets();
174
175         if (substr($response, 0, 1) == "1") {
176                 while (strcmp($buf = serv_gets(), "000")) {
177                         $msgtext .= "<TT>" . htmlspecialchars($buf)
178                                 . "</TT><BR>\n" ;
179                 }
180         }
181         else {
182                 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
183         }
184
185         $msgtext .= "</DIV>\n";
186         return($msgtext);
187 }
188
189
190 //
191 // Fetch the list of users currently logged in.
192 //
193 function ctdl_rwho() {
194         global $clientsocket;
195
196         serv_puts("RWHO");
197         $response = serv_gets();
198
199         if (substr($response, 0, 1) != "1") {
200                 return array(0, NULL);
201         }
202         
203         $all_lines = array();
204         $num_lines = 0;
205
206         while (strcmp($buf = serv_gets(), "000")) {
207
208                 $thisline = array();
209
210                 $tok = strtok($buf, "|");
211                 if ($tok) $thisline["session"] = $tok;
212
213                 $tok = strtok("|");
214                 if ($tok) $thisline["user"] = $tok;
215                 
216                 $tok = strtok("|");
217                 if ($tok) $thisline["room"] = $tok;
218                 
219                 $tok = strtok("|");
220                 if ($tok) $thisline["host"] = $tok;
221                 
222                 $tok = strtok("|");
223                 if ($tok) $thisline["client"] = $tok;
224
225                 // IGnore the rest of the fields for now.
226
227                 $num_lines = array_push($all_lines, $thisline);
228         }
229
230         return array($num_lines, $all_lines);
231
232 }
233
234
235 //
236 // Goto a room.
237 //
238 function ctdl_goto($to_where) {
239         
240         serv_puts("GOTO " . $to_where);
241         $response = serv_gets();
242
243         if (substr($response, 0, 1) == "2") {
244                 $_SESSION["room"] = strtok(substr($response, 4), "|");
245                 return array(TRUE, substr($response, 0, 3));
246         }
247
248         else {
249                 return array(FALSE, substr($response, 0, 3));
250         }
251
252 }
253
254
255
256 //
257 // Fetch the list of known rooms.
258 //
259 function ctdl_knrooms() {
260         global $clientsocket;
261
262         serv_puts("LKRA");
263         $response = serv_gets();
264
265         if (substr($response, 0, 1) != "1") {
266                 return array(0, NULL);
267         }
268         
269         $all_lines = array();
270         $num_lines = 0;
271
272         while (strcmp($buf = serv_gets(), "000")) {
273
274                 $thisline = array();
275
276                 $tok = strtok($buf, "|");
277                 if ($tok) $thisline["name"] = $tok;
278
279                 $tok = strtok("|");
280                 if ($tok) $thisline["flags"] = $tok;
281                 
282                 $tok = strtok("|");
283                 if ($tok) $thisline["floor"] = $tok;
284                 
285                 $tok = strtok("|");
286                 if ($tok) $thisline["order"] = $tok;
287                 
288                 $tok = strtok("|");
289                 if ($tok) $thisline["flags2"] = $tok;
290
291                 $tok = strtok("|");
292                 if ($tok) $thisline["access"] = $tok;
293
294                 if ($thisline["access"] & 8) {
295                         $thisline["hasnewmsgs"] = TRUE;
296                 }
297                 else {
298                         $thisline["hasnewmsgs"] = FALSE;
299                 }
300
301                 $num_lines = array_push($all_lines, $thisline);
302         }
303
304         return array($num_lines, $all_lines);
305
306 }
307
308
309
310 ?>