* Added an initial version of the goto command
[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         session_start();
14
15         if ($_SESSION["ctdlsession"]) {
16                 $session = $_SESSION["ctdlsession"];
17         }
18         else {
19                 $session = "CtdlSession." . time() . rand(1000,9999) ;
20                 $_SESSION["ctdlsession"] = $session;
21         }
22
23         // See if there's a Citadel connection proxy open for this session.
24         // The name of the socket is identical to the name of the
25         // session, and it's found in the /tmp directory.
26
27         $sockname = "/tmp/" . $session . ".socket" ;
28
29         $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
30         if (!$clientsocket) {
31                 // It ain't there, dude.  Open up the proxy. (C version)
32                 //$cmd = "./sessionproxy " . $sockname ;
33                 //exec($cmd);
34
35                 // It ain't there, dude.  Open up the proxy.  (PHP version)
36                 $cmd = "./sessionproxy.php " . $sockname .
37                         " </dev/null >/dev/null 2>&1 " .
38                         " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
39                 exec($cmd);
40                 sleep(2);
41
42                 // Ok, now try again.
43                 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
44
45                 // Try to log the user back in and go back to the correct room.
46                 if ($clientsocket) {
47
48                         ctdl_iden();    // Identify client
49
50                         if ($_SESSION["username"]) {
51                                 login_existing_user(
52                                         $_SESSION["username"],
53                                         $_SESSION["password"]
54                                 );
55                         }
56
57                         if ($_SESSION["room"]) {
58                                 ctdl_goto($_SESSION["room"]);
59                         }
60                         else {
61                                 ctdl_goto("_BASEROOM_");
62                         }
63                 }
64         }
65
66         if ($clientsocket) {
67                 if (!$_SESSION["serv_humannode"]) {
68                         ctdl_get_serv_info();
69                 }
70         }
71         else {
72                 echo "ERROR: no Citadel socket!<BR>\n";
73                 flush();
74         }
75         
76 }
77
78
79 //
80 // Clear out both our Citadel session and our PHP session.  We're done.
81 //
82 function ctdl_end_session() {
83         global $clientsocket, $session;
84
85         // Tell the Citadel server to terminate our connection.
86         // (The extra newlines force it to see that the Citadel session
87         // ended, and the proxy will quit.)
88         //
89         fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
90         $response = fgets($clientsocket, 4096);         // IGnore response
91         fclose($clientsocket);
92         unset($clientsocket);
93
94         // Now clear our PHP session.
95         $_SESSION = array();
96         session_write_close();
97 }
98
99 ?>