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 $buf = socket_read($sock, 4096, PHP_NORMAL_READ);
22 if ($buf == false) return false;
24 if (preg_match("'\n$'s", $buf)) {
25 $buf = substr($buf, 0, strpos($buf, "\n"));
35 // *** Start of main program ***
37 error_reporting(E_ALL);
39 /* Allow the script to hang around waiting for connections. */
42 /* Turn on implicit output flushing so we see what we're getting
48 echo "usage: ", $argv[0], " sockname\n";
54 if ($sockname == "/tmp/") {
55 echo "Invalid socket name.\n";
59 // If there's a dead socket already there, remove it.
60 system("/bin/rm -f " . $sockname);
62 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
64 echo "socket_create() failed: ", socket_strerror($sock), "\n";
65 system("/bin/rm -f " . $sockname);
69 $ret = socket_bind($sock, $sockname);
71 echo "socket_bind() failed: ", socket_strerror($ret), "\n";
72 system("/bin/rm -f " . $sockname);
76 $ret = socket_listen($sock, 5);
78 echo "socket_listen() failed: ", socket_strerror($ret), "\n";
79 system("/bin/rm -f " . $sockname);
83 // Set the permissions so someone else doesn't jump into our connection.
84 chmod($sockname, 0600);
86 // We need to get a connection to the Citadel server going now.
88 $ctdlsock = fsockopen("uncensored.citadel.org", 504, $errno, $errstr, 30);
89 //$ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $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 if (substr($talkback, 0, 1) == "1") do {
147 $buf = fgets($ctdlsock, 4096);
152 socket_write($msgsock, $buf,
155 } while ($buf != "000\n");
158 } while($buf !== false);
160 socket_close ($msgsock);
166 system("/bin/rm -f " . $sockname);