* Replaced the two-second sleep (and associated race condition) for the
[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 // Web designers: please make changes in ctdlheader.php, not here.
9 //
10 // Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
11 // This program is released under the terms of the GNU General Public License.
12
13
14 function establish_citadel_session() {
15
16         global $session, $clientsocket;
17
18         if (strcmp('4.3.0', phpversion()) > 0) {
19                 die("This program requires PHP 4.3.0 or newer.");
20         }
21
22
23         session_start();
24
25         if ($_SESSION["ctdlsession"]) {
26                 $session = $_SESSION["ctdlsession"];
27         }
28         else {
29                 $session = "CtdlSession." . time() . rand(1000,9999) ;
30                 $_SESSION["ctdlsession"] = $session;
31         }
32
33         // See if there's a Citadel connection proxy open for this session.
34         // The name of the socket is identical to the name of the
35         // session, and it's found in the /tmp directory.
36
37         $sockname = "/tmp/" . $session . ".socket" ;
38
39         $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
40         if (!$clientsocket) {
41                 // It ain't there, dude.  Open up the proxy. (C version)
42                 //$cmd = "./sessionproxy " . $sockname ;
43                 //exec($cmd);
44
45                 // It ain't there, dude.  Open up the proxy.  (PHP version)
46                 $cmd = "./sessionproxy.php " . $sockname .
47                         " </dev/null >/dev/null 2>&1 " .
48                         " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
49                 exec($cmd);
50
51                 // Keep attempting connections 10 times per second up to 100 times
52                 $attempts = 0;
53                 while (!$clientsocket) {
54                         usleep(100);
55                         $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
56                         $attempts += 1;
57                         if ($attempts > 100) {
58                                 echo "ERROR: unable to start connection proxy. ";
59                                 echo "Please contact your system administrator.<BR>\n";
60                                 flush();
61                                 exit(1);
62                         }
63                 }
64
65                 // At this point we have a good connection to Citadel.
66
67                 ctdl_iden();    // Identify client
68
69                 if ($_SESSION["username"]) {
70                         login_existing_user(
71                                 $_SESSION["username"],
72                                 $_SESSION["password"]
73                         );
74                 }
75
76                 if ($_SESSION["room"]) {
77                         ctdl_goto($_SESSION["room"]);
78                 }
79                 else {
80                         ctdl_goto("_BASEROOM_");
81                 }
82         }
83
84         if (!$_SESSION["serv_humannode"]) {
85                 ctdl_get_serv_info();
86         }
87
88         // If the user is trying to call up any page other than
89         // login.php logout.php do_login.php,
90         // and the session is not logged in, redirect to login.php
91         //
92         if ($_SESSION["logged_in"] != 1) {
93                 $filename = basename(getenv('SCRIPT_NAME'));
94                 if (    (strcmp($filename, "login.php"))
95                    &&   (strcmp($filename, "logout.php"))
96                    &&   (strcmp($filename, "do_login.php"))
97                 ) {
98                         header("Location: login.php");
99                         exit(0);
100                 }
101         }
102
103         
104 }
105
106
107 //
108 // Clear out both our Citadel session and our PHP session.  We're done.
109 //
110 function ctdl_end_session() {
111         global $clientsocket, $session;
112
113         // Tell the Citadel server to terminate our connection.
114         // (The extra newlines force it to see that the Citadel session
115         // ended, and the proxy will quit.)
116         //
117         fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
118         $response = fgets($clientsocket, 4096);         // IGnore response
119         fclose($clientsocket);
120         unset($clientsocket);
121
122         // Now clear our PHP session.
123         $_SESSION = array();
124         session_write_close();
125 }
126
127 ?>