32d0030450e1504604ed78e55d5a955c94ae3927
[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 // login_existing_user() -- attempt to login using a supplied username/password
35 // Returns an array with two variables:
36 // 0. TRUE or FALSE to determine success or failure
37 // 1. String error message (if relevant)
38 //
39 function login_existing_user($user, $pass) {
40         global $clientsocket;
41
42         serv_puts("USER " . $user);
43         $resp = serv_gets();
44         if (substr($resp, 0, 1) != "3") {
45                 return array(FALSE, substr($resp, 4));
46         }
47
48         serv_puts("PASS " . $pass);
49         $resp = serv_gets();
50         if (substr($resp, 0, 1) != "2") {
51                 return array(FALSE, substr($resp, 4));
52         }
53
54         $_SESSION["username"] = $user;
55         $_SESSION["password"] = $pass;
56         become_logged_in(substr($resp, 4));
57
58         return array(TRUE, "Login successful.  Have fun.");
59 }
60
61
62 //
63 // create_new_user() -- attempt to create a new user 
64 //                      using a supplied username/password
65 // Returns an array with two variables:
66 // 0. TRUE or FALSE to determine success or failure
67 // 1. String error message (if relevant)
68 //
69 function create_new_user($user, $pass) {
70         global $clientsocket;
71
72         serv_puts("NEWU " . $user);
73         $resp = serv_gets();
74         if (substr($resp, 0, 1) != "2") {
75                 return array(FALSE, substr($resp, 4));
76         }
77
78         serv_puts("SETP " . $pass);
79         $resp = serv_gets();
80         if (substr($resp, 0, 1) != "2") {
81                 return array(FALSE, substr($resp, 4));
82         }
83
84         $_SESSION["username"] = $user;
85         $_SESSION["password"] = $pass;
86         become_logged_in(substr($resp, 4));
87
88         return array(TRUE, "Login successful.  Have fun.");
89 }
90
91
92 //
93 // Code common to both existing-user and new-user logins
94 //
95 function become_logged_in($server_parms) {
96         $_SESSION["logged_in"] = 1;
97 }
98
99
100
101 //
102 // Learn all sorts of interesting things about the Citadel server to
103 // which we are connected.
104 //
105 function ctdl_get_serv_info() {
106         serv_puts("INFO");
107         $buf = serv_gets();
108         if (substr($buf, 0, 1) == "1") {
109                 $i = 0;
110                 do {
111                         $buf = serv_gets();
112                         if ($i == 2) $_SESSION["serv_humannode"] = $buf;
113                         if ($i == 4) $_SESSION["serv_software"] = $buf;
114                         $i = $i + 1;
115                 } while (strcasecmp($buf, "000"));
116         }
117
118 }
119
120
121 //
122 // Temporary function to verify communication with the Citadel server.
123 //
124 function test_for_echo() {
125         global $clientsocket, $session;
126
127         $command = "ECHO Video vertigo ... test for echo.";
128         serv_puts($command);
129         $response = serv_gets();
130         echo $response, "<BR>";
131         flush();
132 }
133
134
135 //
136 // Display a system banner.
137 // (This is probably temporary because it outputs more or less finalized
138 // markup.  For now it's just usable.)
139 //
140 function ctdl_mesg($msgname) {
141         global $clientsocket;
142
143         serv_puts("MESG " . $msgname);
144         $response = serv_gets();
145         
146         if (substr($response, 0, 1) == "1") {
147                 echo "<DIV ALIGN=CENTER>\n";
148                 while (strcmp($buf = serv_gets(), "000")) {
149                         echo "<TT>", $buf, "</TT><BR>\n" ;
150                 }
151                 echo "</DIV>\n";
152         }
153         else {
154                 echo "<B><I>", substr($response, 4), "</I></B><BR>\n";
155         }
156 }
157
158
159 //
160 // Fetch the list of users currently logged in.
161 //
162 function ctdl_rwho() {
163         global $clientsocket;
164
165         serv_puts("RWHO");
166         $response = serv_gets();
167
168         if (substr($response, 0, 1) != "1") {
169                 return array(0, NULL);
170         }
171         
172         $all_lines = array();
173         $num_lines = 0;
174
175         while (strcmp($buf = serv_gets(), "000")) {
176
177                 $thisline = array();
178
179                 $tok = strtok($buf, "|");
180                 if ($tok) $thisline["session"] = $tok;
181
182                 $tok = strtok("|");
183                 if ($tok) $thisline["user"] = $tok;
184                 
185                 $tok = strtok("|");
186                 if ($tok) $thisline["room"] = $tok;
187                 
188                 $tok = strtok("|");
189                 if ($tok) $thisline["host"] = $tok;
190                 
191                 $tok = strtok("|");
192                 if ($tok) $thisline["client"] = $tok;
193
194                 // IGnore the rest of the fields for now.
195
196                 $num_lines = array_push($all_lines, $thisline);
197         }
198
199         return array($num_lines, $all_lines);
200
201 }
202
203
204
205 ?>