* Got login/logout working. Still need to redirect unloggedin sessions to
[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
57         return array(TRUE, "Login successful.  Have fun.");
58 }
59
60
61 //
62 // create_new_user() -- attempt to create a new user 
63 //                      using a supplied username/password
64 // Returns an array with two variables:
65 // 0. TRUE or FALSE to determine success or failure
66 // 1. String error message (if relevant)
67 //
68 function create_new_user($user, $pass) {
69         global $clientsocket;
70
71         serv_puts("NEWU " . $user);
72         $resp = serv_gets();
73         if (substr($resp, 0, 1) != "2") {
74                 return array(FALSE, substr($resp, 4));
75         }
76
77         serv_puts("SETP " . $pass);
78         $resp = serv_gets();
79         if (substr($resp, 0, 1) != "2") {
80                 return array(FALSE, substr($resp, 4));
81         }
82
83         $_SESSION["username"] = $user;
84         $_SESSION["password"] = $pass;
85
86         return array(TRUE, "Login successful.  Have fun.");
87 }
88
89
90
91 //
92 // Learn all sorts of interesting things about the Citadel server to
93 // which we are connected.
94 //
95 function ctdl_get_serv_info() {
96         serv_puts("INFO");
97         $buf = serv_gets();
98         if (substr($buf, 0, 1) == "1") {
99                 $i = 0;
100                 do {
101                         $buf = serv_gets();
102                         if ($i == 2) $_SESSION["serv_humannode"] = $buf;
103                         if ($i == 4) $_SESSION["serv_software"] = $buf;
104                         $i = $i + 1;
105                 } while (strcasecmp($buf, "000"));
106         }
107
108 }
109
110
111
112 function test_for_echo() {
113
114         global $clientsocket, $session;
115
116         $command = "ECHO Video vertigo ... test for echo.";
117
118         serv_puts($command);
119         $response = serv_gets();
120         echo $response, "<BR>";
121         flush();
122 }
123
124 function ctdl_mesg($msgname) {
125         global $clientsocket;
126
127         serv_puts("MESG " . $msgname);
128         $response = serv_gets();
129         
130         if (substr($response, 0, 1) == "1") {
131                 echo "<DIV ALIGN=CENTER>\n";
132                 do {
133                         $buf = serv_gets();
134                         if (strcasecmp($buf, "000")) {
135                                 echo "<TT>", $buf, "</TT><BR>\n" ;
136                         }
137                 } while (strcasecmp($buf, "000"));
138                 echo "</DIV>\n";
139         }
140         else {
141                 echo "<B><I>", substr($response, 4), "</I></B><BR>\n";
142         }
143 }
144
145 ?>