* Cleanup of session creation
[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 < 0) {
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 < 0) {
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 < 0) {
76         echo "socket_listen() failed: ", socket_strerror($ret), "\n";
77         system("/bin/rm -f " . $sockname);
78         exit(4);
79 }
80
81 // We need to get a connection to the Citadel server going now.
82
83 $ctdlsock = fsockopen("uncensored.citadel.org", 504, $errno, $errstr, 30);
84 // $ctdlsock = fsockopen("/appl/citadel/citadel.socket", 0, $errno, $errstr, 30);
85 if (!$ctdlsock) {
86         socket_close ($sock);
87         system("/bin/rm -f " . $sockname);
88         exit(5);
89 }
90
91 // Read the greeting from the Citadel server.
92 if (!$buf = fgets($ctdlsock, 4096)) {
93         socket_close ($sock);
94         system("/bin/rm -f " . $sockname);
95         exit(6);
96 }
97
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);
102         exit(7);
103 }
104
105 do {
106         $msgsock = socket_accept($sock);
107         if ($msgsock >= 0) do {
108                 $buf = sock_gets($msgsock);
109                 if ($buf !== false) {
110                         fwrite($ctdlsock, $buf . "\n", (strlen($buf)+1) );
111                         $talkback = fgets($ctdlsock, 4096);
112                         socket_write($msgsock, $talkback, strlen($talkback));
113                 }
114         } while($buf !== false);
115
116         socket_close ($msgsock);
117
118 } while (true);
119
120 socket_close($sock);
121 socket_close($ctdlsock);
122 system("/bin/rm -f " . $sockname);
123 exit(0);
124
125 ?>