a47ef2b011f0b4d40fa7b254e4455376ff846a4b
[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         if (is_array(stat($sockname)))
39                 $clientsocket = fsockopen(SOCKET_PREFIX.$sockname, 0, $errno, $errstr, 5);
40         else
41                 $clientsocket = false;
42         if (!$clientsocket) {
43                 // It ain't there, dude.  Open up the proxy. (C version)
44                 //$cmd = "./sessionproxy " . $sockname ;
45                 //exec($cmd);
46
47                 // It ain't there, dude.  Open up the proxy.  (PHP version)
48                 if (CITADEL_DEBUG_PROXY){
49                         $stdout = '>>/tmp/sessionproxyout.txt ';
50                 }
51                 else{
52                         $stdout = '>/dev/null ';
53                 }
54
55                 $cmd = "./sessionproxy.php " . $sockname .
56                         " </dev/null ".$stdout."2>&1 " .
57                         " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
58                 exec($cmd);
59                 sleep(1);
60
61                 // Keep attempting connections 10 times per second up to 100 times
62                 $attempts = 0;
63                 while (!$clientsocket) {
64                         usleep(100);
65                         if (is_array(stat($sockname)))
66                                 $clientsocket = fsockopen(SOCKET_PREFIX.$sockname, 0, $errno, $errstr, 5);
67                         else 
68                                 $clientsocket = false;
69                         $attempts += 1;
70                         if ($attempts > 100) {
71                                 echo "ERROR: unable to start connection proxy. ";
72                                 echo "Please contact your system administrator.<BR>\n";
73                                 flush();
74                                 exit(1);
75                         }
76                 }
77
78                 // At this point we have a good connection to Citadel.
79                 $identity=array(
80                         "DevelNr" => '0',
81                         "ClientID" => '8',
82                         "VersionNumber" => '001',
83                         "ClientInfoString" => 'PHP web client|',
84                         "Remote Address" => $_SERVER['REMOTE_ADDR'] );
85
86                 ctdl_iden($identity);   // Identify client
87                 ctdl_MessageFormatsPrefered(array("text/html","text/plain"));
88                 if (isset($_SESSION["username"])) {
89                         login_existing_user(
90                                 $_SESSION["username"],
91                                 $_SESSION["password"]
92                         );
93                 }
94
95                 if (isset($_SESSION["room"])) {
96                         ctdl_goto($_SESSION["room"]);
97                 }
98                 else {
99                         ctdl_goto("_BASEROOM_");
100                 }
101         }
102
103         if (!isset($_SESSION["serv_humannode"])) {
104                 $server_info = ctdl_get_serv_info();
105                 // print_r($server_info);
106                 $keys = array_keys($server_info);
107                 foreach ($keys as $key)
108                         $_SESSION[$key] = $server_info[$key];
109         }
110
111         // If the user is trying to call up any page other than
112         // login.php logout.php do_login.php,
113         // and the session is not logged in, redirect to login.php
114         //
115         if (isset($_SESSION["logged_in"]) && ($_SESSION["logged_in"] != 1)) {
116                 $filename = basename(getenv('SCRIPT_NAME'));
117                 if (    (strcmp($filename, "login.php"))
118                    &&   (strcmp($filename, "logout.php"))
119                    &&   (strcmp($filename, "do_login.php"))
120                 ) {
121                         header("Location: login.php");
122                         exit(0);
123                 }
124         }
125
126         
127 }
128
129
130 //
131 // Clear out both our Citadel session and our PHP session.  We're done.
132 //
133 function ctdl_end_session() {
134         global $clientsocket, $session;
135
136         // Tell the Citadel server to terminate our connection.
137         // (The extra newlines force it to see that the Citadel session
138         // ended, and the proxy will quit.)
139         //
140         fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
141         $response = fgets($clientsocket, 4096);         // IGnore response
142         fclose($clientsocket);
143         unset($clientsocket);
144
145         // Now clear our PHP session.
146         $_SESSION = array();
147         session_write_close();
148 }
149
150 ?>