ff23a05b61b3df2e5553c7d117d323ebc60a45e3
[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 // serv_gets() -- generic function to read one line of text from the server
13 //
14 function serv_gets() {
15         global $clientsocket;
16
17         $buf = fgets($clientsocket, 4096);              // Read line
18         $buf = substr($buf, 0, (strlen($buf)-1) );      // strip trailing LF
19         return $buf;
20 }
21
22
23 //
24 // serv_puts() -- generic function to write one line of text to the server
25 //
26 function serv_puts($buf) {
27         global $clientsocket;
28         
29         fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
30         fflush($clientsocket);
31 }
32
33
34
35 //
36 // Identify ourselves to the Citadel server (do this once after connection)
37 //
38 function ctdl_iden() {
39         global $clientsocket;
40
41         // Identify client and hostname
42         serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
43         $buf = serv_gets();
44
45         // Also express our message format preferences
46         serv_puts("MSGP text/html|text/plain");
47         $buf = serv_gets();
48 }
49
50
51
52 //
53 // login_existing_user() -- attempt to login using a supplied username/password
54 // Returns an array with two variables:
55 // 0. TRUE or FALSE to determine success or failure
56 // 1. String error message (if relevant)
57 //
58 function login_existing_user($user, $pass) {
59         global $clientsocket;
60
61         serv_puts("USER " . $user);
62         $resp = serv_gets();
63         if (substr($resp, 0, 1) != "3") {
64                 return array(FALSE, substr($resp, 4));
65         }
66
67         serv_puts("PASS " . $pass);
68         $resp = serv_gets();
69         if (substr($resp, 0, 1) != "2") {
70                 return array(FALSE, substr($resp, 4));
71         }
72
73         $_SESSION["username"] = $user;
74         $_SESSION["password"] = $pass;
75         become_logged_in(substr($resp, 4));
76
77         return array(TRUE, "Login successful.  Have fun.");
78 }
79
80
81 //
82 // create_new_user() -- attempt to create a new user 
83 //                      using a supplied username/password
84 // Returns an array with two variables:
85 // 0. TRUE or FALSE to determine success or failure
86 // 1. String error message (if relevant)
87 //
88 function create_new_user($user, $pass) {
89         global $clientsocket;
90
91         serv_puts("NEWU " . $user);
92         $resp = serv_gets();
93         if (substr($resp, 0, 1) != "2") {
94                 return array(FALSE, substr($resp, 4));
95         }
96
97         serv_puts("SETP " . $pass);
98         $resp = serv_gets();
99         if (substr($resp, 0, 1) != "2") {
100                 return array(FALSE, substr($resp, 4));
101         }
102
103         $_SESSION["username"] = $user;
104         $_SESSION["password"] = $pass;
105         become_logged_in(substr($resp, 4));
106
107         return array(TRUE, "Login successful.  Have fun.");
108 }
109
110
111 //
112 // Code common to both existing-user and new-user logins
113 //
114 function become_logged_in($server_parms) {
115         $_SESSION["logged_in"] = 1;
116
117         $tok = strtok($server_parms, "|");
118         if ($tok) $thisline["username"] = $tok;
119
120         $tok = strtok("|");
121         if ($tok) $thisline["axlevel"] = $tok;
122                 
123         $tok = strtok("|");
124         if ($tok) $thisline["calls"] = $tok;
125                 
126         $tok = strtok("|");
127         if ($tok) $thisline["posts"] = $tok;
128                 
129         $tok = strtok("|");
130         if ($tok) $thisline["userflags"] = $tok;
131                 
132         $tok = strtok("|");
133         if ($tok) $thisline["usernum"] = $tok;
134                 
135         $tok = strtok("|");
136         if ($tok) $thisline["lastcall"] = $tok;
137                 
138         ctdl_goto("_BASEROOM_");
139 }
140
141
142
143 //
144 // Learn all sorts of interesting things about the Citadel server to
145 // which we are connected.
146 //
147 function ctdl_get_serv_info() {
148         serv_puts("INFO");
149         $buf = serv_gets();
150         if (substr($buf, 0, 1) == "1") {
151                 $i = 0;
152                 do {
153                         $buf = serv_gets();
154                         if ($i == 1) $_SESSION["serv_nodename"] = $buf;
155                         if ($i == 2) $_SESSION["serv_humannode"] = $buf;
156                         if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
157                         if ($i == 4) $_SESSION["serv_software"] = $buf;
158                         if ($i == 6) $_SESSION["serv_city"] = $buf;
159                         if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
160                         $i = $i + 1;
161                 } while (strcasecmp($buf, "000"));
162         }
163
164 }
165
166
167 //
168 // Display a system banner.  (Returns completed HTML.)
169 // (This is probably temporary because it outputs more or less finalized
170 // markup.  For now it's just usable.)
171 //
172 function ctdl_mesg($msgname) {
173         global $clientsocket;
174
175         $msgtext = "<DIV ALIGN=CENTER>\n";
176
177         serv_puts("MESG " . $msgname);
178         $response = serv_gets();
179
180         if (substr($response, 0, 1) == "1") {
181                 while (strcmp($buf = serv_gets(), "000")) {
182                         $msgtext .= "<TT>" . htmlspecialchars($buf)
183                                 . "</TT><BR>\n" ;
184                 }
185         }
186         else {
187                 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
188         }
189
190         $msgtext .= "</DIV>\n";
191         return($msgtext);
192 }
193
194
195 //
196 // Fetch the list of users currently logged in.
197 //
198 function ctdl_rwho() {
199         global $clientsocket;
200
201         serv_puts("RWHO");
202         $response = serv_gets();
203
204         if (substr($response, 0, 1) != "1") {
205                 return array(0, NULL);
206         }
207         
208         $all_lines = array();
209         $num_lines = 0;
210
211         while (strcmp($buf = serv_gets(), "000")) {
212
213                 $thisline = array();
214
215                 $tok = strtok($buf, "|");
216                 if ($tok) $thisline["session"] = $tok;
217
218                 $tok = strtok("|");
219                 if ($tok) $thisline["user"] = $tok;
220                 
221                 $tok = strtok("|");
222                 if ($tok) $thisline["room"] = $tok;
223                 
224                 $tok = strtok("|");
225                 if ($tok) $thisline["host"] = $tok;
226                 
227                 $tok = strtok("|");
228                 if ($tok) $thisline["client"] = $tok;
229
230                 // IGnore the rest of the fields for now.
231
232                 $num_lines = array_push($all_lines, $thisline);
233         }
234
235         return array($num_lines, $all_lines);
236
237 }
238
239
240 //
241 // Goto a room.
242 //
243 function ctdl_goto($to_where) {
244         
245         serv_puts("GOTO " . $to_where);
246         $response = serv_gets();
247
248         if (substr($response, 0, 1) == "2") {
249                 $_SESSION["room"] = strtok(substr($response, 4), "|");
250                 return array(TRUE, substr($response, 0, 3));
251         }
252
253         else {
254                 return array(FALSE, substr($response, 0, 3));
255         }
256
257 }
258
259
260
261 //
262 // Fetch the list of known rooms.
263 //
264 function ctdl_knrooms() {
265         global $clientsocket;
266
267         serv_puts("LKRA");
268         $response = serv_gets();
269
270         if (substr($response, 0, 1) != "1") {
271                 return array(0, NULL);
272         }
273         
274         $all_lines = array();
275         $num_lines = 0;
276
277         while (strcmp($buf = serv_gets(), "000")) {
278
279                 $thisline = array();
280
281                 $tok = strtok($buf, "|");
282                 if ($tok) $thisline["name"] = $tok;
283
284                 $tok = strtok("|");
285                 if ($tok) $thisline["flags"] = $tok;
286                 
287                 $tok = strtok("|");
288                 if ($tok) $thisline["floor"] = $tok;
289                 
290                 $tok = strtok("|");
291                 if ($tok) $thisline["order"] = $tok;
292                 
293                 $tok = strtok("|");
294                 if ($tok) $thisline["flags2"] = $tok;
295
296                 $tok = strtok("|");
297                 if ($tok) $thisline["access"] = $tok;
298
299                 if ($thisline["access"] & 8) {
300                         $thisline["hasnewmsgs"] = TRUE;
301                 }
302                 else {
303                         $thisline["hasnewmsgs"] = FALSE;
304                 }
305
306                 $num_lines = array_push($all_lines, $thisline);
307         }
308
309         return array($num_lines, $all_lines);
310
311 }
312
313
314 //
315 // Fetch the list of messages in this room.
316 // Returns: count, response, message array
317 //
318 function ctdl_msgs($mode, $count) {
319         global $clientsocket;
320
321         serv_puts("MSGS " . $mode . "|" . $count);
322         $response = serv_gets();
323
324         if (substr($response, 0, 1) != "1") {
325                 return array(0, substr($response, 4), NULL);
326         }
327         
328         $msgs = array();
329         $num_msgs = 0;
330
331         while (strcmp($buf = serv_gets(), "000")) {
332                 $num_msgs = array_push($msgs, $buf);
333         }
334
335         return array($num_msgs, substr($response, 4), $msgs);
336 }
337
338
339 // Load a message from the server.
340 function ctdl_fetch_message($msgnum) {
341         global $clientsocket;
342
343         serv_puts("MSG4 " . $msgnum);
344         $response = serv_gets();
345
346         if (substr($response, 0, 1) != "1") {
347                 return array(FALSE, substr($response, 4), NULL);
348         }
349
350         $fields = array();
351         while (strcmp($buf = serv_gets(), "000")) {
352                 if (substr($buf, 0, 4) == "text") {
353                         // We're in the text body.  New loop here.
354                         $fields["text"] = ctdl_msg4_from_server();
355                         return array(TRUE, substr($response, 4), $fields);
356                 }
357                 else {
358                         $fields[substr($buf, 0, 4)] = substr($buf, 5);
359                 }
360         }
361
362         // Message terminated prematurely (no text body)
363         return array(FALSE, substr($response, 4), $fields);
364 }
365
366 // Support function for ctdl_fetch_message().  This handles the text body
367 // portion of the message, converting various formats to HTML as
368 // appropriate.
369 function ctdl_msg4_from_server() {
370
371         $txt = "";
372         $msgformat = "text/plain";
373         $in_body = FALSE;
374
375         $previous_line = "";
376         while (strcmp($buf = serv_gets(), "000")) {
377                 if ($in_body == FALSE) {
378                         if (strlen($buf) == 0) {
379                                 $in_body = TRUE;
380                         }
381                         else {
382                                 if (!strncasecmp($buf, "content-type: ", 14)) {
383                                         $msgformat = substr($buf, 14);
384                                 }
385                         }
386                 }
387                 else {
388                         if (!strcasecmp($msgformat, "text/html")) {
389                                 $txt .= $buf;
390                         }
391                         else if (!strcasecmp($msgformat, "text/plain")) {
392                                 $txt .= "<TT>" . htmlspecialchars($buf) . "</TT><BR>\n" ;
393                         }
394                         else if (!strcasecmp($msgformat, "text/x-citadel-variformat")) {
395                                 if (substr($previous_line, 0, 1) == " ") {
396                                         $txt .= "<BR>\n" ;
397                                 }
398                                 $txt .= htmlspecialchars($buf);
399                         }
400                         else {
401                                 $txt .= htmlspecialchars($buf);
402                         }
403                         $previous_line = $buf;
404                 }
405         }
406
407         return($txt);
408 }
409
410
411 ?>