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 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
12 // This program is released under the terms of the GNU General Public License.
16 // sock_gets() -- reads one line of text from a socket
18 function sock_gets($sock) {
19 $buf = socket_read($sock, 4096, PHP_NORMAL_READ);
20 if ($buf == false) return false;
22 if (preg_match("'\n$'s", $buf)) {
23 $buf = substr($buf, 0, strpos($buf, "\n"));
33 // *** Start of main program ***
35 error_reporting(E_ALL);
37 /* Allow the script to hang around waiting for connections. */
40 /* Turn on implicit output flushing so we see what we're getting
46 echo "usage: ", $argv[0], " sockname\n";
52 if ($sockname == "/tmp/") {
53 echo "Invalid socket name.\n";
57 // If there's a dead socket already there, remove it.
58 system("/bin/rm -f " . $sockname);
60 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
62 echo "socket_create() failed: ", socket_strerror($sock), "\n";
63 system("/bin/rm -f " . $sockname);
67 $ret = socket_bind($sock, $sockname);
69 echo "socket_bind() failed: ", socket_strerror($ret), "\n";
70 system("/bin/rm -f " . $sockname);
74 $ret = socket_listen($sock, 5);
76 echo "socket_listen() failed: ", socket_strerror($ret), "\n";
77 system("/bin/rm -f " . $sockname);
81 // We need to get a connection to the Citadel server going now.
83 $ctdlsock = fsockopen("uncensored.citadel.org", 504, $errno, $errstr, 30);
84 // $ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $errno, $errstr, 30);
87 system("/bin/rm -f " . $sockname);
91 // Read the greeting from the Citadel server.
92 if (!$buf = fgets($ctdlsock, 4096)) {
94 system("/bin/rm -f " . $sockname);
98 // Make sure the server is allowing logins.
99 if (substr($buf, 0, 1) != "2") {
100 socket_close ($sock);
101 system("/bin/rm -f " . $sockname);
106 $msgsock = socket_accept($sock);
107 if ($msgsock >= 0) do {
108 $buf = sock_gets($msgsock);
109 if ($buf !== false) {
110 if (!fwrite($ctdlsock, $buf . "\n")) {
113 system("/bin/rm -f " . $sockname);
116 $talkback = fgets($ctdlsock, 4096);
120 system("/bin/rm -f " . $sockname);
123 socket_write($msgsock, $talkback, strlen($talkback));
125 if (substr($talkback, 0, 1) == "1") do {
126 $buf = fgets($ctdlsock, 4096);
131 socket_write($msgsock, $buf,
134 } while ($buf != "000\n");
137 } while($buf !== false);
139 socket_close ($msgsock);
145 system("/bin/rm -f " . $sockname);