Added a comma after each msgnum exported. The parser was globbing them all together...
[citadel.git] / ctdlphp / sessionproxy.php
index 0ace0b57e037e02f074f9d4ee2475962e135e847..22f4d4a0eddc1272e5e210b17088de6b85c2bc6e 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/php -q
 
 <?php
-
+include "config_ctdlclient.php";
 // $Id$
 //
 // This is the session proxy that binds a unix domain socket to a Citadel
 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
 // This program is released under the terms of the GNU General Public License.
 
+global $msgsock;
 
 // sock_gets() -- reads one line of text from a socket
 // 
+if (CITADEL_DEBUG_PROXY)
+{
+       define_syslog_variables();
+       openlog("sessionproxylog", LOG_PID | LOG_PERROR, LOG_LOCAL0);
+}
 function sock_gets($sock) {
+       global $msgsock;
+       socket_clear_error($msgsock);
        $buf = socket_read($sock, 4096, PHP_NORMAL_READ);
-       if ($buf == false) return false;
+       if (CITADEL_DEBUG_PROXY)
+       {
+               syslog(LOG_DEBUG, "gets Read: ".$buf);
+       }
+       if (socket_last_error($sock)) return false;
 
        if (preg_match("'\n$'s", $buf)) {
                $buf = substr($buf, 0, strpos($buf, "\n"));
@@ -31,7 +43,6 @@ function sock_gets($sock) {
 
 
 
-
 // *** Start of main program ***
 
 error_reporting(E_ALL);
@@ -85,8 +96,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(CITADEL_HOSTNAME, CITADEL_TCP_PORTNO, $errno, $errstr, 30);
 if (!$ctdlsock) {
        socket_close ($sock);
        system("/bin/rm -f " . $sockname);
@@ -128,6 +138,8 @@ do {
        if ($msgsock >= 0) do {
                $buf = sock_gets($msgsock);
                if ($buf !== false) {
+//                     fwrite($logfd, ">>");
+//                     fwride($logfd, $buf);
                        if (!fwrite($ctdlsock, $buf . "\n")) {
                                fclose($ctdlsock);
                                socket_close($sock);
@@ -135,7 +147,15 @@ do {
                                exit(9);
                        }
                        $talkback = fgets($ctdlsock, 4096);
+                       if (CITADEL_DEBUG_PROXY)
+                       {
+                               syslog(LOG_DEBUG, "talkback: ".$talkback);
+                       }
                        if (!$talkback) {
+                               if (CITADEL_DEBUG_PROXY)
+                               {
+                                       syslog(LOG_ERROR, "closing socket.");
+                               }
                                fclose($ctdlsock);
                                socket_close($sock);
                                system("/bin/rm -f " . $sockname);
@@ -143,6 +163,21 @@ do {
                        }
                        socket_write($msgsock, $talkback, strlen($talkback));
 
+                       // BINARY_FOLLOWS mode
+                       if (substr($talkback, 0, 1) == "6") {
+                               $bytes = intval(substr($talkback, 4));
+                               if (CITADEL_DEBUG_PROXY)
+                               {
+                                       syslog(LOG_DEBUG, "reading ".$bytes." bytes from server");
+                               }
+                               $buf = fread($ctdlsock, $bytes);
+                               if (CITADEL_DEBUG_PROXY)
+                               {
+                                       syslog(LOG_DEBUG, "Read: ".$buf);
+                               }
+                               socket_write($msgsock, $buf, $bytes);
+                       }
+
                        // LISTING_FOLLOWS mode
                        if (substr($talkback, 0, 1) == "1") do {
                                $buf = fgets($ctdlsock, 4096);
@@ -157,8 +192,9 @@ do {
 
                        // SEND_LISTING mode
                        if (substr($talkback, 0, 1) == "4") do {
+                               socket_clear_error($msgsock);
                                $buf = sock_gets($msgsock);
-                               if (!$buf) {
+                               if (socket_last_error($msgsock)) {
                                        $buf = "000" ;
                                }
                                if (!fwrite($ctdlsock, $buf . "\n")) {