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.
8 // Web designers: please make changes in ctdlheader.php, not here.
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.
14 function establish_citadel_session() {
16 global $session, $clientsocket;
18 if (strcmp('4.3.0', phpversion()) > 0) {
19 die("This program requires PHP 4.3.0 or newer.");
25 if ($_SESSION["ctdlsession"]) {
26 $session = $_SESSION["ctdlsession"];
29 $session = "CtdlSession." . time() . rand(1000,9999) ;
30 $_SESSION["ctdlsession"] = $session;
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.
37 $sockname = "/tmp/" . $session . ".socket" ;
39 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
41 // It ain't there, dude. Open up the proxy. (C version)
42 //$cmd = "./sessionproxy " . $sockname ;
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 & " ;
51 // Keep attempting connections 10 times per second up to 100 times
53 while (!$clientsocket) {
55 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
57 if ($attempts > 100) {
58 echo "ERROR: unable to start connection proxy. ";
59 echo "Please contact your system administrator.<BR>\n";
65 // At this point we have a good connection to Citadel.
67 ctdl_iden(); // Identify client
69 if ($_SESSION["username"]) {
71 $_SESSION["username"],
76 if ($_SESSION["room"]) {
77 ctdl_goto($_SESSION["room"]);
80 ctdl_goto("_BASEROOM_");
84 if (!$_SESSION["serv_humannode"]) {
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
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"))
98 header("Location: login.php");
108 // Clear out both our Citadel session and our PHP session. We're done.
110 function ctdl_end_session() {
111 global $clientsocket, $session;
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.)
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);
122 // Now clear our PHP session.
124 session_write_close();