*** empty log message ***
[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                 // It ain't there, dude.  Open up the proxy. (C version)
30                 //$cmd = "./sessionproxy " . $sockname ;
31                 //exec($cmd);
32
33                 // It ain't there, dude.  Open up the proxy.  (PHP version)
34                 $cmd = "./sessionproxy.php " . $sockname .
35                         " </dev/null >/dev/null 2>&1 " .
36                         " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
37                 exec($cmd);
38                 sleep(2);
39
40                 // Ok, now try again.
41                 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
42         }
43
44         if ($clientsocket) {
45                 /*
46                 echo "Connected.  Performing echo tests.<BR>\n";
47                 flush();
48                 $cmd = "ECHO test echo string upon connection\n";
49                 fwrite($clientsocket, $cmd);
50                 $response = fgets($clientsocket, 4096);
51                 echo "Response is: ", $response, "<BR>\n";
52                 flush();
53
54                 $cmd = "ECHO second test for echo\n";
55                 fwrite($clientsocket, $cmd);
56                 $response = fgets($clientsocket, 4096);
57                 echo "Response is: ", $response, "<BR>\n";
58                 flush();
59                 */
60         }
61         else {
62                 echo "ERROR: no Citadel socket!<BR>\n";
63                 flush();
64         }
65         
66 }
67
68
69 //
70 // Clear out both our Citadel session and our PHP session.  We're done.
71 //
72 function ctdl_end_session() {
73         global $clientsocket, $session;
74
75         // Tell the Citadel server to terminate our connection.
76         fwrite($clientsocket, "QUIT\n");
77         $response = fgets($clientsocket, 4096);         // IGnore response
78         fclose($clientsocket);
79         unset($clientsocket);
80
81         // Now clear our PHP session.
82         unset($session); 
83         session_write_close();
84 }
85
86 ?>