* cleanup
[citadel.git] / ctdlphp / ctdlsession.php
1 <?PHP
2
3 // $Id$
4 //
5 // This gets called from within the header functions.  It establishes or
6 // connects to a PHP session, and then connects to Citadel if necessary.
7 //
8
9 function establish_citadel_session() {
10
11         global $session, $clientsocket;
12
13         if (strcmp('4.3.0', phpversion()) > 0) {
14                 die("This program requires PHP 4.3.0 or newer.");
15         }
16
17
18         session_start();
19
20         if ($_SESSION["ctdlsession"]) {
21                 $session = $_SESSION["ctdlsession"];
22         }
23         else {
24                 $session = "CtdlSession." . time() . rand(1000,9999) ;
25                 $_SESSION["ctdlsession"] = $session;
26         }
27
28         // See if there's a Citadel connection proxy open for this session.
29         // The name of the socket is identical to the name of the
30         // session, and it's found in the /tmp directory.
31
32         $sockname = "/tmp/" . $session . ".socket" ;
33
34         $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
35         if (!$clientsocket) {
36                 // It ain't there, dude.  Open up the proxy. (C version)
37                 //$cmd = "./sessionproxy " . $sockname ;
38                 //exec($cmd);
39
40                 // It ain't there, dude.  Open up the proxy.  (PHP version)
41                 $cmd = "./sessionproxy.php " . $sockname .
42                         " </dev/null >/dev/null 2>&1 " .
43                         " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
44                 exec($cmd);
45                 sleep(2);
46
47                 // Ok, now try again.
48                 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
49
50                 // Try to log the user back in and go back to the correct room.
51                 if ($clientsocket) {
52
53                         ctdl_iden();    // Identify client
54
55                         if ($_SESSION["username"]) {
56                                 login_existing_user(
57                                         $_SESSION["username"],
58                                         $_SESSION["password"]
59                                 );
60                         }
61
62                         if ($_SESSION["room"]) {
63                                 ctdl_goto($_SESSION["room"]);
64                         }
65                         else {
66                                 ctdl_goto("_BASEROOM_");
67                         }
68                 }
69         }
70
71         if ($clientsocket) {
72                 if (!$_SESSION["serv_humannode"]) {
73                         ctdl_get_serv_info();
74                 }
75         }
76         else {
77                 echo "ERROR: no Citadel socket!<BR>\n";
78                 flush();
79         }
80
81         // If the user is trying to call up any page other than
82         // login.php logout.php do_login.php,
83         // and the session is not logged in, redirect to login.php
84         //
85         if ($_SESSION["logged_in"] != 1) {
86                 $filename = basename(getenv('SCRIPT_NAME'));
87                 if (    (strcmp($filename, "login.php"))
88                    &&   (strcmp($filename, "logout.php"))
89                    &&   (strcmp($filename, "do_login.php"))
90                 ) {
91                         header("Location: login.php");
92                         exit(0);
93                 }
94         }
95
96         
97 }
98
99
100 //
101 // Clear out both our Citadel session and our PHP session.  We're done.
102 //
103 function ctdl_end_session() {
104         global $clientsocket, $session;
105
106         // Tell the Citadel server to terminate our connection.
107         // (The extra newlines force it to see that the Citadel session
108         // ended, and the proxy will quit.)
109         //
110         fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
111         $response = fgets($clientsocket, 4096);         // IGnore response
112         fclose($clientsocket);
113         unset($clientsocket);
114
115         // Now clear our PHP session.
116         $_SESSION = array();
117         session_write_close();
118 }
119
120 ?>