* Minor code cleanups
[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 }
112
113
114
115 //
116 // Learn all sorts of interesting things about the Citadel server to
117 // which we are connected.
118 //
119 function ctdl_get_serv_info() {
120         serv_puts("INFO");
121         $buf = serv_gets();
122         if (substr($buf, 0, 1) == "1") {
123                 $i = 0;
124                 do {
125                         $buf = serv_gets();
126                         if ($i == 1) $_SESSION["serv_nodename"] = $buf;
127                         if ($i == 2) $_SESSION["serv_humannode"] = $buf;
128                         if ($i == 3) $_SESSION["serv_fqdn"] = $buf;
129                         if ($i == 4) $_SESSION["serv_software"] = $buf;
130                         if ($i == 6) $_SESSION["serv_city"] = $buf;
131                         if ($i == 7) $_SESSION["serv_sysadmin"] = $buf;
132                         $i = $i + 1;
133                 } while (strcasecmp($buf, "000"));
134         }
135
136 }
137
138
139 //
140 // Display a system banner.  (Returns completed HTML.)
141 // (This is probably temporary because it outputs more or less finalized
142 // markup.  For now it's just usable.)
143 //
144 function ctdl_mesg($msgname) {
145         global $clientsocket;
146
147         $msgtext = "<DIV ALIGN=CENTER>\n";
148
149         serv_puts("MESG " . $msgname);
150         $response = serv_gets();
151
152         if (substr($response, 0, 1) == "1") {
153                 while (strcmp($buf = serv_gets(), "000")) {
154                         $msgtext .= "<TT>" . htmlspecialchars($buf)
155                                 . "</TT><BR>\n" ;
156                 }
157         }
158         else {
159                 $msgtext .= "<B><I>" . substr($response, 4) . "</I></B><BR>\n";
160         }
161
162         $msgtext .= "</DIV>\n";
163         return($msgtext);
164 }
165
166
167 //
168 // Fetch the list of users currently logged in.
169 //
170 function ctdl_rwho() {
171         global $clientsocket;
172
173         serv_puts("RWHO");
174         $response = serv_gets();
175
176         if (substr($response, 0, 1) != "1") {
177                 return array(0, NULL);
178         }
179         
180         $all_lines = array();
181         $num_lines = 0;
182
183         while (strcmp($buf = serv_gets(), "000")) {
184
185                 $thisline = array();
186
187                 $tok = strtok($buf, "|");
188                 if ($tok) $thisline["session"] = $tok;
189
190                 $tok = strtok("|");
191                 if ($tok) $thisline["user"] = $tok;
192                 
193                 $tok = strtok("|");
194                 if ($tok) $thisline["room"] = $tok;
195                 
196                 $tok = strtok("|");
197                 if ($tok) $thisline["host"] = $tok;
198                 
199                 $tok = strtok("|");
200                 if ($tok) $thisline["client"] = $tok;
201
202                 // IGnore the rest of the fields for now.
203
204                 $num_lines = array_push($all_lines, $thisline);
205         }
206
207         return array($num_lines, $all_lines);
208
209 }
210
211
212
213 ?>