* added debug protocol flag
[citadel.git] / ctdlphp / sessionproxy.php
1 #!/usr/bin/php -q
2
3 <?php
4 include "config_ctdlclient.php";
5 // $Id$
6 //
7 // This is the session proxy that binds a unix domain socket to a Citadel
8 // server connection.  We need one of these for each session because PHP does
9 // not have a way to bind a session to a persistent socket.
10 //
11 // Web designers: don't touch this module.  It's not included in your web pages
12 // and therefore you don't need to be here.
13 //
14 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
15 // This program is released under the terms of the GNU General Public License.
16
17
18 // sock_gets() -- reads one line of text from a socket
19 // 
20 function sock_gets($sock) {
21         socket_clear_error($msgsock);
22         $buf = socket_read($sock, 4096, PHP_NORMAL_READ);
23         if (socket_last_error($buf)) return false;
24
25         if (preg_match("'\n$'s", $buf)) {
26                 $buf = substr($buf, 0, strpos($buf, "\n"));
27         }
28
29         return $buf;
30 }
31
32
33
34
35
36 // *** Start of main program ***
37
38 error_reporting(E_ALL);
39
40 /* Allow the script to hang around waiting for connections. */
41 set_time_limit(0);
42
43 /* Turn on implicit output flushing so we see what we're getting
44  * as it comes in.
45  */
46 ob_implicit_flush();
47
48 if ($argc != 2) {
49         echo "usage: ", $argv[0], " sockname\n";
50         exit(1);
51 }
52
53 $sockname = $argv[1];
54
55 if ($sockname == "/tmp/") {
56         echo "Invalid socket name.\n";
57         exit(1);
58 }
59
60 // If there's a dead socket already there, remove it.
61 system("/bin/rm -f " . $sockname);
62
63 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
64 if (!$sock) {
65         echo "socket_create() failed: ", socket_strerror($sock), "\n";
66         system("/bin/rm -f " . $sockname);
67         exit(2);
68 }
69
70 $ret = socket_bind($sock, $sockname);
71 if (!$ret) {
72         echo "socket_bind() failed: ", socket_strerror($ret), "\n";
73         system("/bin/rm -f " . $sockname);
74         exit(3);
75 }
76
77 $ret = socket_listen($sock, 5);
78 if (!$ret) {
79         echo "socket_listen() failed: ", socket_strerror($ret), "\n";
80         system("/bin/rm -f " . $sockname);
81         exit(4);
82 }
83
84 // Set the permissions so someone else doesn't jump into our connection.
85 chmod($sockname, 0600);
86
87 // We need to get a connection to the Citadel server going now.
88
89 $ctdlsock = fsockopen(CITADEL_HOSTNAME, CITADEL_TCP_PORTNO, $errno, $errstr, 30);
90 if (!$ctdlsock) {
91         socket_close ($sock);
92         system("/bin/rm -f " . $sockname);
93         exit(5);
94 }
95
96 // Read the greeting from the Citadel server.
97 if (!$buf = fgets($ctdlsock, 4096)) {
98         socket_close ($sock);
99         system("/bin/rm -f " . $sockname);
100         exit(6);
101 }
102
103 // Make sure the server is allowing logins.
104 if (substr($buf, 0, 1) != "2") {
105         socket_close ($sock);
106         system("/bin/rm -f " . $sockname);
107         exit(7);
108 }
109
110 do {
111         // Wait for connections, but time out after 15 minutes.
112         // socket_select() is completely b0rken in PHP 4.1, which is why
113         // this program requires PHP 4.3 or newer.
114         //
115         if (socket_select($readsock = array($sock),
116                         $writesock = NULL,
117                         $exceptsock = NULL,
118                         900, 0
119         ) == 0) {
120                 // Timing out.
121                 socket_close ($sock);
122                 system("/bin/rm -f " . $sockname);
123                 exit(8);
124         }
125
126         // Ok, there's a valid connection coming in.  Accept it.
127         $msgsock = socket_accept($sock);
128         if ($msgsock >= 0) do {
129                 $buf = sock_gets($msgsock);
130                 if ($buf !== false) {
131                         if (!fwrite($ctdlsock, $buf . "\n")) {
132                                 fclose($ctdlsock);
133                                 socket_close($sock);
134                                 system("/bin/rm -f " . $sockname);
135                                 exit(9);
136                         }
137                         $talkback = fgets($ctdlsock, 4096);
138                         if (!$talkback) {
139                                 fclose($ctdlsock);
140                                 socket_close($sock);
141                                 system("/bin/rm -f " . $sockname);
142                                 exit(10);
143                         }
144                         socket_write($msgsock, $talkback, strlen($talkback));
145
146                         // LISTING_FOLLOWS mode
147                         if (substr($talkback, 0, 1) == "1") do {
148                                 $buf = fgets($ctdlsock, 4096);
149                                 if (!$buf) {
150                                         $buf = "000\n" ;
151                                 }
152                                 else {
153                                         socket_write($msgsock, $buf,
154                                                 strlen($buf));
155                                 }
156                         } while ($buf != "000\n");
157
158                         // SEND_LISTING mode
159                         if (substr($talkback, 0, 1) == "4") do {
160                                 socket_clear_error($msgsock);
161                                 $buf = sock_gets($msgsock);
162                                 if (socket_last_error($msgsock)) {
163                                         $buf = "000" ;
164                                 }
165                                 if (!fwrite($ctdlsock, $buf . "\n")) {
166                                         fclose($ctdlsock);
167                                         socket_close($sock);
168                                         system("/bin/rm -f " . $sockname);
169                                         exit(11);
170                                 }
171                         } while ($buf != "000");
172
173                 }
174         } while($buf !== false);
175
176         socket_close ($msgsock);
177
178 } while (true);
179
180 socket_close($sock);
181 fclose($ctdlsock);
182 system("/bin/rm -f " . $sockname);
183 exit(0);
184
185 ?>