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