]> code.citadel.org Git - citadel.git/blobdiff - ctdlphp/sessionproxy.php
* Run all posts through stripslashes()
[citadel.git] / ctdlphp / sessionproxy.php
index 8d544270b7552a89cd2e3725e551a26567493729..75bd902438306cc0160626bc4d1a35bf86546fee 100755 (executable)
@@ -8,16 +8,19 @@
 // server connection.  We need one of these for each session because PHP does
 // not have a way to bind a session to a persistent socket.
 //
+// Web designers: don't touch this module.  It's not included in your web pages
+// and therefore you don't need to be here.
+//
 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
 // This program is released under the terms of the GNU General Public License.
-//
 
 
 // sock_gets() -- reads one line of text from a socket
 // 
 function sock_gets($sock) {
+       socket_clear_error($msgsock);
        $buf = socket_read($sock, 4096, PHP_NORMAL_READ);
-       if ($buf == false) return false;
+       if (socket_last_error($buf)) return false;
 
        if (preg_match("'\n$'s", $buf)) {
                $buf = substr($buf, 0, strpos($buf, "\n"));
@@ -58,21 +61,21 @@ if ($sockname == "/tmp/") {
 system("/bin/rm -f " . $sockname);
 
 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
-if ($sock < 0) {
+if (!$sock) {
        echo "socket_create() failed: ", socket_strerror($sock), "\n";
        system("/bin/rm -f " . $sockname);
        exit(2);
 }
 
 $ret = socket_bind($sock, $sockname);
-if ($ret < 0) {
+if (!$ret) {
        echo "socket_bind() failed: ", socket_strerror($ret), "\n";
        system("/bin/rm -f " . $sockname);
        exit(3);
 }
 
 $ret = socket_listen($sock, 5);
-if ($ret < 0) {
+if (!$ret) {
        echo "socket_listen() failed: ", socket_strerror($ret), "\n";
        system("/bin/rm -f " . $sockname);
        exit(4);
@@ -84,7 +87,7 @@ chmod($sockname, 0600);
 // We need to get a connection to the Citadel server going now.
 
 $ctdlsock = fsockopen("uncensored.citadel.org", 504, $errno, $errstr, 30);
-// $ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $errno, $errstr, 30);
+//$ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $errno, $errstr, 30);
 if (!$ctdlsock) {
        socket_close ($sock);
        system("/bin/rm -f " . $sockname);
@@ -106,6 +109,22 @@ if (substr($buf, 0, 1) != "2") {
 }
 
 do {
+       // Wait for connections, but time out after 15 minutes.
+       // socket_select() is completely b0rken in PHP 4.1, which is why
+       // this program requires PHP 4.3 or newer.
+       //
+       if (socket_select($readsock = array($sock),
+                       $writesock = NULL,
+                       $exceptsock = NULL,
+                       900, 0
+       ) == 0) {
+               // Timing out.
+               socket_close ($sock);
+               system("/bin/rm -f " . $sockname);
+               exit(8);
+       }
+
+       // Ok, there's a valid connection coming in.  Accept it.
        $msgsock = socket_accept($sock);
        if ($msgsock >= 0) do {
                $buf = sock_gets($msgsock);
@@ -114,17 +133,18 @@ do {
                                fclose($ctdlsock);
                                socket_close($sock);
                                system("/bin/rm -f " . $sockname);
-                               exit(8);
+                               exit(9);
                        }
                        $talkback = fgets($ctdlsock, 4096);
                        if (!$talkback) {
                                fclose($ctdlsock);
                                socket_close($sock);
                                system("/bin/rm -f " . $sockname);
-                               exit(9);
+                               exit(10);
                        }
                        socket_write($msgsock, $talkback, strlen($talkback));
 
+                       // LISTING_FOLLOWS mode
                        if (substr($talkback, 0, 1) == "1") do {
                                $buf = fgets($ctdlsock, 4096);
                                if (!$buf) {
@@ -136,6 +156,21 @@ do {
                                }
                        } while ($buf != "000\n");
 
+                       // SEND_LISTING mode
+                       if (substr($talkback, 0, 1) == "4") do {
+                               socket_clear_error($msgsock);
+                               $buf = sock_gets($msgsock);
+                               if (socket_last_error($msgsock)) {
+                                       $buf = "000" ;
+                               }
+                               if (!fwrite($ctdlsock, $buf . "\n")) {
+                                       fclose($ctdlsock);
+                                       socket_close($sock);
+                                       system("/bin/rm -f " . $sockname);
+                                       exit(11);
+                               }
+                       } while ($buf != "000");
+
                }
        } while($buf !== false);