9f56d18b58b25eb5e4a917c4743070dde9d373b5
[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 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
12 // This program is released under the terms of the GNU General Public License.
13 //
14
15
16 // sock_gets() -- reads one line of text from a socket
17 // 
18 function sock_gets($sock) {
19         $buf = socket_read($sock, 4096, PHP_NORMAL_READ);
20         if ($buf == false) return false;
21
22         if (preg_match("'\n$'s", $buf)) {
23                 $buf = substr($buf, 0, strpos($buf, "\n"));
24         }
25
26         return $buf;
27 }
28
29
30
31
32
33 // *** Start of main program ***
34
35 error_reporting(E_ALL);
36
37 /* Allow the script to hang around waiting for connections. */
38 set_time_limit(0);
39
40 /* Turn on implicit output flushing so we see what we're getting
41  * as it comes in.
42  */
43 ob_implicit_flush();
44
45 if ($argc != 2) {
46         echo "usage: ", $argv[0], " sockname\n";
47         exit(1);
48 }
49
50 $sockname = $argv[1];
51
52 if ($sockname == "/tmp/") {
53         echo "Invalid socket name.\n";
54         exit(1);
55 }
56
57 // If there's a dead socket already there, remove it.
58 system("/bin/rm -f " . $sockname);
59
60 $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
61 if (!$sock) {
62         echo "socket_create() failed: ", socket_strerror($sock), "\n";
63         system("/bin/rm -f " . $sockname);
64         exit(2);
65 }
66
67 $ret = socket_bind($sock, $sockname);
68 if (!$ret) {
69         echo "socket_bind() failed: ", socket_strerror($ret), "\n";
70         system("/bin/rm -f " . $sockname);
71         exit(3);
72 }
73
74 $ret = socket_listen($sock, 5);
75 if (!$ret) {
76         echo "socket_listen() failed: ", socket_strerror($ret), "\n";
77         system("/bin/rm -f " . $sockname);
78         exit(4);
79 }
80
81 // Set the permissions so someone else doesn't jump into our connection.
82 chmod($sockname, 0600);
83
84 // We need to get a connection to the Citadel server going now.
85
86 $ctdlsock = fsockopen("uncensored.citadel.org", 504, $errno, $errstr, 30);
87 //$ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $errno, $errstr, 30);
88 if (!$ctdlsock) {
89         socket_close ($sock);
90         system("/bin/rm -f " . $sockname);
91         exit(5);
92 }
93
94 // Read the greeting from the Citadel server.
95 if (!$buf = fgets($ctdlsock, 4096)) {
96         socket_close ($sock);
97         system("/bin/rm -f " . $sockname);
98         exit(6);
99 }
100
101 // Make sure the server is allowing logins.
102 if (substr($buf, 0, 1) != "2") {
103         socket_close ($sock);
104         system("/bin/rm -f " . $sockname);
105         exit(7);
106 }
107
108 do {
109         // Wait for connections, but time out after 15 minutes.
110         if (socket_select($readsock = array($sock),
111                         $writesock = NULL,
112                         $exceptsock = NULL,
113                         900, 0
114         ) == 0) {
115                 // Timing out.
116                 socket_close ($sock);
117                 system("/bin/rm -f " . $sockname);
118                 exit(8);
119         }
120
121         // Ok, there's a valid connection coming in.  Accept it.
122         $msgsock = socket_accept($sock);
123         if ($msgsock >= 0) do {
124                 $buf = sock_gets($msgsock);
125                 if ($buf !== false) {
126                         if (!fwrite($ctdlsock, $buf . "\n")) {
127                                 fclose($ctdlsock);
128                                 socket_close($sock);
129                                 system("/bin/rm -f " . $sockname);
130                                 exit(9);
131                         }
132                         $talkback = fgets($ctdlsock, 4096);
133                         if (!$talkback) {
134                                 fclose($ctdlsock);
135                                 socket_close($sock);
136                                 system("/bin/rm -f " . $sockname);
137                                 exit(10);
138                         }
139                         socket_write($msgsock, $talkback, strlen($talkback));
140
141                         if (substr($talkback, 0, 1) == "1") do {
142                                 $buf = fgets($ctdlsock, 4096);
143                                 if (!$buf) {
144                                         $buf = "000\n" ;
145                                 }
146                                 else {
147                                         socket_write($msgsock, $buf,
148                                                 strlen($buf));
149                                 }
150                         } while ($buf != "000\n");
151
152                 }
153         } while($buf !== false);
154
155         socket_close ($msgsock);
156
157 } while (true);
158
159 socket_close($sock);
160 fclose($ctdlsock);
161 system("/bin/rm -f " . $sockname);
162 exit(0);
163
164 ?>