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