]> code.citadel.org Git - citadel.git/blobdiff - ctdlphp/ctdlsession.php
* use isset() to be correct and don't produce warnings
[citadel.git] / ctdlphp / ctdlsession.php
index 25427420bafb565c18728b175faccf378ab07aa6..996975d7a6d6b2b62d78cf6dd89f39d50d1bd762 100644 (file)
@@ -1,20 +1,33 @@
 <?PHP
-
-//
-// ctdlsession.php
+// $Id$
 //
 // This gets called from within the header functions.  It establishes or
 // connects to a PHP session, and then connects to Citadel if necessary.
 //
+// Web designers: please make changes in ctdlheader.php, not here.
+//
+// Copyright (c) 2003 by Art Cancro <ajc@uncensored.citadel.org>
+// This program is released under the terms of the GNU General Public License.
+
 
 function establish_citadel_session() {
 
        global $session, $clientsocket;
 
-       // echo "Calling session_start()<BR>\n";
-       // flush();
+       if (strcmp('4.3.0', phpversion()) > 0) {
+               die("This program requires PHP 4.3.0 or newer.");
+       }
+
+
        session_start();
-       $session = "CtdlSession." . session_id();
+
+       if (isset($_SESSION["ctdlsession"])) {
+               $session = $_SESSION["ctdlsession"];
+       }
+       else {
+               $session = "CtdlSession." . time() . rand(1000,9999) ;
+               $_SESSION["ctdlsession"] = $session;
+       }
 
        // See if there's a Citadel connection proxy open for this session.
        // The name of the socket is identical to the name of the
@@ -22,13 +35,8 @@ function establish_citadel_session() {
 
        $sockname = "/tmp/" . $session . ".socket" ;
 
-       // echo "Connecting to ", $sockname, "...<BR>\n";
-       // flush();
        $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
        if (!$clientsocket) {
-               //echo "Socket not present.  Firing up a new proxy.<BR>\n";
-               //flush();
-
                // It ain't there, dude.  Open up the proxy. (C version)
                //$cmd = "./sessionproxy " . $sockname ;
                //exec($cmd);
@@ -38,35 +46,59 @@ function establish_citadel_session() {
                        " </dev/null >/dev/null 2>&1 " .
                        " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
                exec($cmd);
-               sleep(2);
 
-               // Ok, now try again.
-               // echo "Connecting to ", $sockname, "...<BR>\n";
-               // flush();
-               $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
+               // Keep attempting connections 10 times per second up to 100 times
+               $attempts = 0;
+               while (!$clientsocket) {
+                       usleep(100);
+                       $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
+                       $attempts += 1;
+                       if ($attempts > 100) {
+                               echo "ERROR: unable to start connection proxy. ";
+                               echo "Please contact your system administrator.<BR>\n";
+                               flush();
+                               exit(1);
+                       }
+               }
+
+               // At this point we have a good connection to Citadel.
+
+               ctdl_iden();    // Identify client
+
+               if ($_SESSION["username"]) {
+                       login_existing_user(
+                               $_SESSION["username"],
+                               $_SESSION["password"]
+                       );
+               }
+
+               if ($_SESSION["room"]) {
+                       ctdl_goto($_SESSION["room"]);
+               }
+               else {
+                       ctdl_goto("_BASEROOM_");
+               }
        }
 
-       if ($clientsocket) {
-               /*
-               echo "Connected.  Performing echo tests.<BR>\n";
-               flush();
-               $cmd = "ECHO test echo string upon connection\n";
-               fwrite($clientsocket, $cmd);
-               $response = fgets($clientsocket, 4096);
-               echo "Response is: ", $response, "<BR>\n";
-               flush();
-
-               $cmd = "ECHO second test for echo\n";
-               fwrite($clientsocket, $cmd);
-               $response = fgets($clientsocket, 4096);
-               echo "Response is: ", $response, "<BR>\n";
-               flush();
-               */
+       if (!isset($_SESSION["serv_humannode"])) {
+               ctdl_get_serv_info();
        }
-       else {
-               echo "ERROR: no Citadel socket!<BR>\n";
-               flush();
+
+       // If the user is trying to call up any page other than
+       // login.php logout.php do_login.php,
+       // and the session is not logged in, redirect to login.php
+       //
+       if (isset($_SESSION["logged_in"]) && ($_SESSION["logged_in"] != 1)) {
+               $filename = basename(getenv('SCRIPT_NAME'));
+               if (    (strcmp($filename, "login.php"))
+                  &&   (strcmp($filename, "logout.php"))
+                  &&   (strcmp($filename, "do_login.php"))
+               ) {
+                       header("Location: login.php");
+                       exit(0);
+               }
        }
+
        
 }
 
@@ -78,13 +110,17 @@ function ctdl_end_session() {
        global $clientsocket, $session;
 
        // Tell the Citadel server to terminate our connection.
-       fwrite($clientsocket, "QUIT\n");
+       // (The extra newlines force it to see that the Citadel session
+       // ended, and the proxy will quit.)
+       //
+       fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
+       $response = fgets($clientsocket, 4096);         // IGnore response
        fclose($clientsocket);
        unset($clientsocket);
 
        // Now clear our PHP session.
-       unset($session); 
-       session_destroy();
+       $_SESSION = array();
+       session_write_close();
 }
 
 ?>