4 include "config_ctdlclient.php";
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.
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.
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.
18 // sock_gets() -- reads one line of text from a socket
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;
25 if (preg_match("'\n$'s", $buf)) {
26 $buf = substr($buf, 0, strpos($buf, "\n"));
36 // *** Start of main program ***
38 error_reporting(E_ALL);
40 /* Allow the script to hang around waiting for connections. */
43 /* Turn on implicit output flushing so we see what we're getting
49 echo "usage: ", $argv[0], " sockname\n";
55 if ($sockname == "/tmp/") {
56 echo "Invalid socket name.\n";
60 // If there's a dead socket already there, remove it.
61 system("/bin/rm -f " . $sockname);
63 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
65 echo "socket_create() failed: ", socket_strerror($sock), "\n";
66 system("/bin/rm -f " . $sockname);
70 $ret = socket_bind($sock, $sockname);
72 echo "socket_bind() failed: ", socket_strerror($ret), "\n";
73 system("/bin/rm -f " . $sockname);
77 $ret = socket_listen($sock, 5);
79 echo "socket_listen() failed: ", socket_strerror($ret), "\n";
80 system("/bin/rm -f " . $sockname);
84 // Set the permissions so someone else doesn't jump into our connection.
85 chmod($sockname, 0600);
87 // We need to get a connection to the Citadel server going now.
89 $ctdlsock = fsockopen(CITADEL_HOSTNAME, CITADEL_TCP_PORTNO, $errno, $errstr, 30);
92 system("/bin/rm -f " . $sockname);
96 // Read the greeting from the Citadel server.
97 if (!$buf = fgets($ctdlsock, 4096)) {
99 system("/bin/rm -f " . $sockname);
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);
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.
115 if (socket_select($readsock = array($sock),
121 socket_close ($sock);
122 system("/bin/rm -f " . $sockname);
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")) {
134 system("/bin/rm -f " . $sockname);
137 $talkback = fgets($ctdlsock, 4096);
141 system("/bin/rm -f " . $sockname);
144 socket_write($msgsock, $talkback, strlen($talkback));
146 // LISTING_FOLLOWS mode
147 if (substr($talkback, 0, 1) == "1") do {
148 $buf = fgets($ctdlsock, 4096);
153 socket_write($msgsock, $buf,
156 } while ($buf != "000\n");
158 // BINARY_FOLLOWS mode
159 if (substr($talkback, 0, 1) == "6") {
160 $bytes = intval(substr($talkback, 4));
161 $buf = fread($ctdlock, $bytes);
162 socket_write($msgsock, $buf, $bytes);
166 if (substr($talkback, 0, 1) == "4") do {
167 socket_clear_error($msgsock);
168 $buf = sock_gets($msgsock);
169 if (socket_last_error($msgsock)) {
172 if (!fwrite($ctdlsock, $buf . "\n")) {
175 system("/bin/rm -f " . $sockname);
178 } while ($buf != "000");
181 } while($buf !== false);
183 socket_close ($msgsock);
189 system("/bin/rm -f " . $sockname);