* login changes
[citadel.git] / ctdlphp / ctdlsession.php
1 <?PHP
2
3 //
4 // ctdlsession.php
5 //
6 // This gets called from within the header functions.  It establishes or
7 // connects to a PHP session, and then connects to Citadel if necessary.
8 //
9
10 function establish_citadel_session() {
11
12         global $session, $clientsocket;
13
14         // echo "Calling session_start()<BR>\n";
15         // flush();
16         session_start();
17         $session = "CtdlSession." . session_id();
18
19         // See if there's a Citadel connection proxy open for this session.
20         // The name of the socket is identical to the name of the
21         // session, and it's found in the /tmp directory.
22
23         $sockname = "/tmp/" . $session . ".socket" ;
24
25         // echo "Connecting to ", $sockname, "...<BR>\n";
26         // flush();
27         $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
28         if (!$clientsocket) {
29                 //echo "Socket not present.  Firing up a new proxy.<BR>\n";
30                 //flush();
31
32                 // It ain't there, dude.  Open up the proxy. (C version)
33                 //$cmd = "./sessionproxy " . $sockname ;
34                 //exec($cmd);
35
36                 // It ain't there, dude.  Open up the proxy.  (PHP version)
37                 $cmd = "./sessionproxy.php " . $sockname .
38                         " </dev/null >/dev/null 2>&1 " .
39                         " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
40                 exec($cmd);
41                 sleep(2);
42
43                 // Ok, now try again.
44                 // echo "Connecting to ", $sockname, "...<BR>\n";
45                 // flush();
46                 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
47         }
48
49         if ($clientsocket) {
50                 /*
51                 echo "Connected.  Performing echo tests.<BR>\n";
52                 flush();
53                 $cmd = "ECHO test echo string upon connection\n";
54                 fwrite($clientsocket, $cmd);
55                 $response = fgets($clientsocket, 4096);
56                 echo "Response is: ", $response, "<BR>\n";
57                 flush();
58
59                 $cmd = "ECHO second test for echo\n";
60                 fwrite($clientsocket, $cmd);
61                 $response = fgets($clientsocket, 4096);
62                 echo "Response is: ", $response, "<BR>\n";
63                 flush();
64                 */
65         }
66         else {
67                 echo "ERROR: no Citadel socket!<BR>\n";
68                 flush();
69         }
70         
71 }
72
73
74 //
75 // Clear out both our Citadel session and our PHP session.  We're done.
76 //
77 function ctdl_end_session() {
78         global $clientsocket, $session;
79
80         // Tell the Citadel server to terminate our connection.
81         fwrite($clientsocket, "QUIT\n");
82         $response = fgets($clientsocket, 4096);         // IGnore response
83         fclose($clientsocket);
84         unset($clientsocket);
85
86         // Now clear our PHP session.
87         unset($session); 
88         session_destroy();
89 }
90
91 ?>