5 // Implements various Citadel server commands.
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.
13 // serv_gets() -- generic function to read one line of text from the server
15 function serv_gets() {
18 $buf = fgets($clientsocket, 4096); // Read line
19 $buf = substr($buf, 0, (strlen($buf)-1) ); // strip trailing LF
25 // serv_puts() -- generic function to write one line of text to the server
27 function serv_puts($buf) {
30 fwrite($clientsocket, $buf . "\n", (strlen($buf)+1) );
36 // Identify this client, and the hostname where the user is, to Citadel.
38 function ctdl_iden() {
41 serv_puts("IDEN 0|8|001|PHP web client|" . $_SERVER['REMOTE_ADDR'] );
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)
53 function login_existing_user($user, $pass) {
56 serv_puts("USER " . $user);
58 if (substr($resp, 0, 1) != "3") {
59 return array(FALSE, substr($resp, 4));
62 serv_puts("PASS " . $pass);
64 if (substr($resp, 0, 1) != "2") {
65 return array(FALSE, substr($resp, 4));
68 $_SESSION["username"] = $user;
69 $_SESSION["password"] = $pass;
70 become_logged_in(substr($resp, 4));
72 return array(TRUE, "Login successful. Have fun.");
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)
83 function create_new_user($user, $pass) {
86 serv_puts("NEWU " . $user);
88 if (substr($resp, 0, 1) != "2") {
89 return array(FALSE, substr($resp, 4));
92 serv_puts("SETP " . $pass);
94 if (substr($resp, 0, 1) != "2") {
95 return array(FALSE, substr($resp, 4));
98 $_SESSION["username"] = $user;
99 $_SESSION["password"] = $pass;
100 become_logged_in(substr($resp, 4));
102 return array(TRUE, "Login successful. Have fun.");
107 // Code common to both existing-user and new-user logins
109 function become_logged_in($server_parms) {
110 $_SESSION["logged_in"] = 1;
116 // Learn all sorts of interesting things about the Citadel server to
117 // which we are connected.
119 function ctdl_get_serv_info() {
122 if (substr($buf, 0, 1) == "1") {
126 if ($i == 2) $_SESSION["serv_humannode"] = $buf;
127 if ($i == 4) $_SESSION["serv_software"] = $buf;
129 } while (strcasecmp($buf, "000"));
136 // Temporary function to verify communication with the Citadel server.
138 function test_for_echo() {
139 global $clientsocket, $session;
141 $command = "ECHO Video vertigo ... test for echo.";
143 $response = serv_gets();
144 echo $response, "<BR>";
150 // Display a system banner.
151 // (This is probably temporary because it outputs more or less finalized
152 // markup. For now it's just usable.)
154 function ctdl_mesg($msgname) {
155 global $clientsocket;
157 serv_puts("MESG " . $msgname);
158 $response = serv_gets();
160 if (substr($response, 0, 1) == "1") {
161 echo "<DIV ALIGN=CENTER>\n";
162 while (strcmp($buf = serv_gets(), "000")) {
163 echo "<TT>", $buf, "</TT><BR>\n" ;
168 echo "<B><I>", substr($response, 4), "</I></B><BR>\n";
174 // Fetch the list of users currently logged in.
176 function ctdl_rwho() {
177 global $clientsocket;
180 $response = serv_gets();
182 if (substr($response, 0, 1) != "1") {
183 return array(0, NULL);
186 $all_lines = array();
189 while (strcmp($buf = serv_gets(), "000")) {
193 $tok = strtok($buf, "|");
194 if ($tok) $thisline["session"] = $tok;
197 if ($tok) $thisline["user"] = $tok;
200 if ($tok) $thisline["room"] = $tok;
203 if ($tok) $thisline["host"] = $tok;
206 if ($tok) $thisline["client"] = $tok;
208 // IGnore the rest of the fields for now.
210 $num_lines = array_push($all_lines, $thisline);
213 return array($num_lines, $all_lines);