* Removed stray \n
[citadel.git] / ctdlphp / sessionproxy.php
1 #!/usr/bin/php -q
2
3 <?php
4
5 // $Id$
6 //
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.
10 //
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.
13 //
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.
16
17
18 // sock_gets() -- reads one line of text from a socket
19 // 
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;
24
25         if (preg_match("'\n$'s", $buf)) {
26                 $buf = substr($buf, 0, strpos($buf, "\n"));
27         }
28
29         return $buf;
30 }
31
32
33
34
35
36 // *** Start of main program ***
37
38 error_reporting(E_ALL);
39
40 /* Allow the script to hang around waiting for connections. */
41 set_time_limit(0);
42
43 /* Turn on implicit output flushing so we see what we're getting
44  * as it comes in.
45  */
46 ob_implicit_flush();
47
48 if ($argc != 2) {
49         echo "usage: ", $argv[0], " sockname\n";
50         exit(1);
51 }
52
53 $sockname = $argv[1];
54
55 if ($sockname == "/tmp/") {
56         echo "Invalid socket name.\n";
57         exit(1);
58 }
59
60 // If there's a dead socket already there, remove it.
61 system("/bin/rm -f " . $sockname);
62
63 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
64 if (!$sock) {
65         echo "socket_create() failed: ", socket_strerror($sock), "\n";
66         system("/bin/rm -f " . $sockname);
67         exit(2);
68 }
69
70 $ret = socket_bind($sock, $sockname);
71 if (!$ret) {
72         echo "socket_bind() failed: ", socket_strerror($ret), "\n";
73         system("/bin/rm -f " . $sockname);
74         exit(3);
75 }
76
77 $ret = socket_listen($sock, 5);
78 if (!$ret) {
79         echo "socket_listen() failed: ", socket_strerror($ret), "\n";
80         system("/bin/rm -f " . $sockname);
81         exit(4);
82 }
83
84 // Set the permissions so someone else doesn't jump into our connection.
85 chmod($sockname, 0600);
86
87 // We need to get a connection to the Citadel server going now.
88
89 $ctdlsock = fsockopen("uncensored.citadel.org", 504, $errno, $errstr, 30);
90 //$ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $errno, $errstr, 30);
91 if (!$ctdlsock) {
92         socket_close ($sock);
93         system("/bin/rm -f " . $sockname);
94         exit(5);
95 }
96
97 // Read the greeting from the Citadel server.
98 if (!$buf = fgets($ctdlsock, 4096)) {
99         socket_close ($sock);
100         system("/bin/rm -f " . $sockname);
101         exit(6);
102 }
103
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);
108         exit(7);
109 }
110
111 do {
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.
115         //
116         if (socket_select($readsock = array($sock),
117                         $writesock = NULL,
118                         $exceptsock = NULL,
119                         900, 0
120         ) == 0) {
121                 // Timing out.
122                 socket_close ($sock);
123                 system("/bin/rm -f " . $sockname);
124                 exit(8);
125         }
126
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")) {
133                                 fclose($ctdlsock);
134                                 socket_close($sock);
135                                 system("/bin/rm -f " . $sockname);
136                                 exit(9);
137                         }
138                         $talkback = fgets($ctdlsock, 4096);
139                         if (!$talkback) {
140                                 fclose($ctdlsock);
141                                 socket_close($sock);
142                                 system("/bin/rm -f " . $sockname);
143                                 exit(10);
144                         }
145                         socket_write($msgsock, $talkback, strlen($talkback));
146
147                         // LISTING_FOLLOWS mode
148                         if (substr($talkback, 0, 1) == "1") do {
149                                 $buf = fgets($ctdlsock, 4096);
150                                 if (!$buf) {
151                                         $buf = "000\n" ;
152                                 }
153                                 else {
154                                         socket_write($msgsock, $buf,
155                                                 strlen($buf));
156                                 }
157                         } while ($buf != "000\n");
158
159                         // SEND_LISTING mode
160                         if (substr($talkback, 0, 1) == "4") do {
161                                 socket_clear_error($msgsock);
162                                 $buf = sock_gets($msgsock);
163                                 if (socket_last_error($msgsock)) {
164                                         $buf = "000" ;
165                                 }
166                                 if (!fwrite($ctdlsock, $buf . "\n")) {
167                                         fclose($ctdlsock);
168                                         socket_close($sock);
169                                         system("/bin/rm -f " . $sockname);
170                                         exit(11);
171                                 }
172                         } while ($buf != "000");
173
174                 }
175         } while($buf !== false);
176
177         socket_close ($msgsock);
178
179 } while (true);
180
181 socket_close($sock);
182 fclose($ctdlsock);
183 system("/bin/rm -f " . $sockname);
184 exit(0);
185
186 ?>