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("uncensored.citadel.org", 504, $errno, $errstr, 30);
90 //$ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $errno, $errstr, 30);
93 system("/bin/rm -f " . $sockname);
97 // Read the greeting from the Citadel server.
98 if (!$buf = fgets($ctdlsock, 4096)) {
100 system("/bin/rm -f " . $sockname);
104 // Make sure the server is allowing logins.
105 if (substr($buf, 0, 1) != "2") {
106 socket_close ($sock);
107 system("/bin/rm -f " . $sockname);
112 // Wait for connections, but time out after 15 minutes.
113 // socket_select() is completely b0rken in PHP 4.1, which is why
114 // this program requires PHP 4.3 or newer.
116 if (socket_select($readsock = array($sock),
122 socket_close ($sock);
123 system("/bin/rm -f " . $sockname);
127 // Ok, there's a valid connection coming in. Accept it.
128 $msgsock = socket_accept($sock);
129 if ($msgsock >= 0) do {
130 $buf = sock_gets($msgsock);
131 if ($buf !== false) {
132 if (!fwrite($ctdlsock, $buf . "\n")) {
135 system("/bin/rm -f " . $sockname);
138 $talkback = fgets($ctdlsock, 4096);
142 system("/bin/rm -f " . $sockname);
145 socket_write($msgsock, $talkback, strlen($talkback));
147 // LISTING_FOLLOWS mode
148 if (substr($talkback, 0, 1) == "1") do {
149 $buf = fgets($ctdlsock, 4096);
154 socket_write($msgsock, $buf,
157 } while ($buf != "000\n");
160 if (substr($talkback, 0, 1) == "4") do {
161 socket_clear_error($msgsock);
162 $buf = sock_gets($msgsock);
163 if (socket_last_error($msgsock)) {
166 if (!fwrite($ctdlsock, $buf . "\n")) {
169 system("/bin/rm -f " . $sockname);
172 } while ($buf != "000");
175 } while($buf !== false);
177 socket_close ($msgsock);
183 system("/bin/rm -f " . $sockname);