* ctdlprotocol.php: known room list fetch now sets subscript "hasnewmsgs"
[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         ctdl_goto("_BASEROOM_");
112 }
113
114
115
116 //
117 // Learn all sorts of interesting things about the Citadel server to
118 // which we are connected.
119 //
120 function ctdl_get_serv_info() {
121         serv_puts("INFO");
122         $buf = serv_gets();
123         if (substr($buf, 0, 1) == "1") {
124                 $i = 0;
125                 do {
126                         $buf = serv_gets();
127                         if ($i == 1) $_SESSION["serv_nodename"] = $buf;
128                         if ($i == 2) $_SESSION["serv_humannode"] = $buf;
129                         if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
130                         if ($i == 4) $_SESSION["serv_software"] = $buf;
131                         if ($i == 6) $_SESSION["serv_city"] = $buf;
132                         if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
133                         $i = $i + 1;
134                 } while (strcasecmp($buf, "000"));
135         }
136
137 }
138
139
140 //
141 // Display a system banner.  (Returns completed HTML.)
142 // (This is probably temporary because it outputs more or less finalized
143 // markup.  For now it's just usable.)
144 //
145 function ctdl_mesg($msgname) {
146         global $clientsocket;
147
148         $msgtext = "<DIV ALIGN=CENTER>\n";
149
150         serv_puts("MESG " . $msgname);
151         $response = serv_gets();
152
153         if (substr($response, 0, 1) == "1") {
154                 while (strcmp($buf = serv_gets(), "000")) {
155                         $msgtext .= "<TT>" . htmlspecialchars($buf)
156                                 . "</TT><BR>\n" ;
157                 }
158         }
159         else {
160                 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
161         }
162
163         $msgtext .= "</DIV>\n";
164         return($msgtext);
165 }
166
167
168 //
169 // Fetch the list of users currently logged in.
170 //
171 function ctdl_rwho() {
172         global $clientsocket;
173
174         serv_puts("RWHO");
175         $response = serv_gets();
176
177         if (substr($response, 0, 1) != "1") {
178                 return array(0, NULL);
179         }
180         
181         $all_lines = array();
182         $num_lines = 0;
183
184         while (strcmp($buf = serv_gets(), "000")) {
185
186                 $thisline = array();
187
188                 $tok = strtok($buf, "|");
189                 if ($tok) $thisline["session"] = $tok;
190
191                 $tok = strtok("|");
192                 if ($tok) $thisline["user"] = $tok;
193                 
194                 $tok = strtok("|");
195                 if ($tok) $thisline["room"] = $tok;
196                 
197                 $tok = strtok("|");
198                 if ($tok) $thisline["host"] = $tok;
199                 
200                 $tok = strtok("|");
201                 if ($tok) $thisline["client"] = $tok;
202
203                 // IGnore the rest of the fields for now.
204
205                 $num_lines = array_push($all_lines, $thisline);
206         }
207
208         return array($num_lines, $all_lines);
209
210 }
211
212
213 //
214 // Goto a room.
215 //
216 function ctdl_goto($to_where) {
217         
218         serv_puts("GOTO " . $to_where);
219         $response = serv_gets();
220
221         if (substr($response, 0, 1) == "2") {
222                 $_SESSION["room"] = strtok(substr($response, 4), "|");
223                 return array(TRUE, substr($response, 0, 3));
224         }
225
226         else {
227                 return array(FALSE, substr($response, 0, 3));
228         }
229
230 }
231
232
233
234 //
235 // Fetch the list of known rooms.
236 //
237 function ctdl_knrooms() {
238         global $clientsocket;
239
240         serv_puts("LKRA");
241         $response = serv_gets();
242
243         if (substr($response, 0, 1) != "1") {
244                 return array(0, NULL);
245         }
246         
247         $all_lines = array();
248         $num_lines = 0;
249
250         while (strcmp($buf = serv_gets(), "000")) {
251
252                 $thisline = array();
253
254                 $tok = strtok($buf, "|");
255                 if ($tok) $thisline["name"] = $tok;
256
257                 $tok = strtok("|");
258                 if ($tok) $thisline["flags"] = $tok;
259                 
260                 $tok = strtok("|");
261                 if ($tok) $thisline["floor"] = $tok;
262                 
263                 $tok = strtok("|");
264                 if ($tok) $thisline["order"] = $tok;
265                 
266                 $tok = strtok("|");
267                 if ($tok) $thisline["flags2"] = $tok;
268
269                 $tok = strtok("|");
270                 if ($tok) $thisline["access"] = $tok;
271
272                 if ($thisline["access"] & 8) {
273                         $thisline["hasnewmsgs"] = TRUE;
274                 }
275                 else {
276                         $thisline["hasnewmsgs"] = FALSE;
277                 }
278
279                 $num_lines = array_push($all_lines, $thisline);
280         }
281
282         return array($num_lines, $all_lines);
283
284 }
285
286
287
288 ?>