* Identify our message format preference (HTML followed by plain text)
[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                 sleep(2);
51
52                 // Ok, now try again.
53                 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
54
55                 // Try to log the user back in and go back to the correct room.
56                 if ($clientsocket) {
57
58                         ctdl_iden();    // Identify client
59
60                         if ($_SESSION["username"]) {
61                                 login_existing_user(
62                                         $_SESSION["username"],
63                                         $_SESSION["password"]
64                                 );
65                         }
66
67                         if ($_SESSION["room"]) {
68                                 ctdl_goto($_SESSION["room"]);
69                         }
70                         else {
71                                 ctdl_goto("_BASEROOM_");
72                         }
73                 }
74         }
75
76         if ($clientsocket) {
77                 if (!$_SESSION["serv_humannode"]) {
78                         ctdl_get_serv_info();
79                 }
80         }
81         else {
82                 echo "ERROR: no Citadel socket!<BR>\n";
83                 flush();
84         }
85
86         // If the user is trying to call up any page other than
87         // login.php logout.php do_login.php,
88         // and the session is not logged in, redirect to login.php
89         //
90         if ($_SESSION["logged_in"] != 1) {
91                 $filename = basename(getenv('SCRIPT_NAME'));
92                 if (    (strcmp($filename, "login.php"))
93                    &&   (strcmp($filename, "logout.php"))
94                    &&   (strcmp($filename, "do_login.php"))
95                 ) {
96                         header("Location: login.php");
97                         exit(0);
98                 }
99         }
100
101         
102 }
103
104
105 //
106 // Clear out both our Citadel session and our PHP session.  We're done.
107 //
108 function ctdl_end_session() {
109         global $clientsocket, $session;
110
111         // Tell the Citadel server to terminate our connection.
112         // (The extra newlines force it to see that the Citadel session
113         // ended, and the proxy will quit.)
114         //
115         fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
116         $response = fgets($clientsocket, 4096);         // IGnore response
117         fclose($clientsocket);
118         unset($clientsocket);
119
120         // Now clear our PHP session.
121         $_SESSION = array();
122         session_write_close();
123 }
124
125 ?>