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.
7 // Web designers: please make changes in ctdlheader.php, not here.
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.
13 function establish_citadel_session() {
15 global $session, $clientsocket;
17 if (strcmp('4.3.0', phpversion()) > 0) {
18 die("This program requires PHP 4.3.0 or newer.");
24 if (isset($_SESSION["ctdlsession"])) {
25 $session = $_SESSION["ctdlsession"];
28 $session = "CtdlSession." . time() . rand(1000,9999) ;
29 $_SESSION["ctdlsession"] = $session;
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.
36 $sockname = "/tmp/" . $session . ".socket" ;
38 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
40 // It ain't there, dude. Open up the proxy. (C version)
41 //$cmd = "./sessionproxy " . $sockname ;
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 & " ;
50 // Keep attempting connections 10 times per second up to 100 times
52 while (!$clientsocket) {
54 $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
56 if ($attempts > 100) {
57 echo "ERROR: unable to start connection proxy. ";
58 echo "Please contact your system administrator.<BR>\n";
64 // At this point we have a good connection to Citadel.
68 "VersionNumber" => '001',
69 "ClientInfoString" => 'PHP web client|',
70 "Remote Address" => $_SERVER['REMOTE_ADDR'] );
72 ctdl_iden($identity); // Identify client
73 ctdl_MessageFormatsPrefered(array("text/html","text/plain"));
74 if ($_SESSION["username"]) {
76 $_SESSION["username"],
81 if ($_SESSION["room"]) {
82 ctdl_goto($_SESSION["room"]);
85 ctdl_goto("_BASEROOM_");
89 if (!isset($_SESSION["serv_humannode"])) {
90 $server_info = ctdl_get_serv_info();
91 print_r($server_info);
92 $keys = array_keys($server_info);
93 foreach ($keys as $key)
94 $_SESSION[$key] = $server_info[$key];
97 // If the user is trying to call up any page other than
98 // login.php logout.php do_login.php,
99 // and the session is not logged in, redirect to login.php
101 if (isset($_SESSION["logged_in"]) && ($_SESSION["logged_in"] != 1)) {
102 $filename = basename(getenv('SCRIPT_NAME'));
103 if ( (strcmp($filename, "login.php"))
104 && (strcmp($filename, "logout.php"))
105 && (strcmp($filename, "do_login.php"))
107 header("Location: login.php");
117 // Clear out both our Citadel session and our PHP session. We're done.
119 function ctdl_end_session() {
120 global $clientsocket, $session;
122 // Tell the Citadel server to terminate our connection.
123 // (The extra newlines force it to see that the Citadel session
124 // ended, and the proxy will quit.)
126 fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
127 $response = fgets($clientsocket, 4096); // IGnore response
128 fclose($clientsocket);
129 unset($clientsocket);
131 // Now clear our PHP session.
133 session_write_close();