* Got login/logout working. Still need to redirect unloggedin sessions to
[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         session_start();
15
16         if ($_SESSION["ctdlsession"]) {
17                 $session = $_SESSION["ctdlsession"];
18         }
19         else {
20                 $session = "CtdlSession." . time() . rand(1000,9999) ;
21                 $_SESSION["ctdlsession"] = $session;
22         }
23
24         // See if there's a Citadel connection proxy open for this session.
25         // The name of the socket is identical to the name of the
26         // session, and it's found in the /tmp directory.
27
28         $sockname = "/tmp/" . $session . ".socket" ;
29
30         $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
31         if (!$clientsocket) {
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                 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
45
46                 // Try to log the user back in.
47                 if ($clientsocket) {
48
49
50                         if ($_SESSION["username"]) {
51                                 login_existing_user(
52                                         $_SESSION["username"],
53                                         $_SESSION["password"]
54                                 );
55                         }
56                 }
57         }
58
59         if ($clientsocket) {
60                 if (!$_SESSION["serv_humannode"]) {
61                         ctdl_get_serv_info();
62                 }
63         }
64         else {
65                 echo "ERROR: no Citadel socket!<BR>\n";
66                 flush();
67         }
68         
69 }
70
71
72 //
73 // Clear out both our Citadel session and our PHP session.  We're done.
74 //
75 function ctdl_end_session() {
76         global $clientsocket, $session;
77
78         // Tell the Citadel server to terminate our connection.
79         // (The extra newlines force it to see that the Citadel session
80         // ended, and the proxy will quit.)
81         //
82         fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
83         $response = fgets($clientsocket, 4096);         // IGnore response
84         fclose($clientsocket);
85         unset($clientsocket);
86
87         // Now clear our PHP session.
88         $_SESSION = array();
89         session_write_close();
90 }
91
92 ?>