X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=ctdlphp%2Fctdlsession.php;h=c9614c753ef57d2b24ee068d67e2d4eed051f4ca;hb=fa2dd842abb5feedea3e2253255722fcaecc3c6f;hp=605529b52d1ec158e52763eeb5cc89cd6447bb55;hpb=ff6b88ee11a3bb07d630d2255d6defbb7d8330a0;p=citadel.git diff --git a/ctdlphp/ctdlsession.php b/ctdlphp/ctdlsession.php index 605529b52..c9614c753 100644 --- a/ctdlphp/ctdlsession.php +++ b/ctdlphp/ctdlsession.php @@ -1,85 +1,154 @@ +// This program is released under the terms of the GNU General Public License. + function establish_citadel_session() { global $session, $clientsocket; - // echo "Calling session_start()
\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 // session, and it's found in the /tmp directory. $sockname = "/tmp/" . $session . ".socket" ; - - // echo "Connecting to ", $sockname, "...
\n"; - // flush(); - $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5); + $errno = 0; + $errstr = ""; + if (is_array(stat($sockname))) + $clientsocket = fsockopen(SOCKET_PREFIX.$sockname, 0, $errno, $errstr, 5); + else + $clientsocket = false; +//// TODO: if we get connection refused... + echo "$socketname - $errno - $errstr"; + if (!$clientsocket) { - //echo "Socket not present. Firing up a new proxy.
\n"; - //flush(); - // It ain't there, dude. Open up the proxy. (C version) //$cmd = "./sessionproxy " . $sockname ; //exec($cmd); // It ain't there, dude. Open up the proxy. (PHP version) + if (CITADEL_DEBUG_PROXY){ + $stdout = '>>/tmp/sessionproxyout.txt '; + } + else{ + $stdout = '>/dev/null '; + } + $cmd = "./sessionproxy.php " . $sockname . - " /dev/null 2>&1 " . + " &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, "...
\n"; - // flush(); - $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5); + sleep(1); + + // Keep attempting connections 10 times per second up to 100 times + $attempts = 0; + while (!$clientsocket) { + usleep(100); + if (is_array(stat($sockname))) + $clientsocket = fsockopen(SOCKET_PREFIX.$sockname, 0, $errno, $errstr, 5); + else + $clientsocket = false; + $attempts += 1; + if ($attempts > 100) { + echo "ERROR: unable to start connection proxy. "; + echo "Please contact your system administrator.
\n"; + flush(); + exit(1); + } + } + + // At this point we have a good connection to Citadel. + $identity=array( + "DevelNr" => '0', + "ClientID" => '8', + "VersionNumber" => '001', + "ClientInfoString" => 'PHP web client|', + "Remote Address" => $_SERVER['REMOTE_ADDR'] ); + + ctdl_iden($identity); // Identify client + ctdl_MessageFormatsPrefered(array("text/html","text/plain")); + if (isset($_SESSION["username"])) { + login_existing_user( + $_SESSION["username"], + $_SESSION["password"] + ); + } + + if (isset($_SESSION["room"])) { + ctdl_goto($_SESSION["room"]); + } + else { + ctdl_goto("_BASEROOM_"); + } } - if ($clientsocket) { - /* - echo "Connected. Performing echo tests.
\n"; - flush(); - $cmd = "ECHO test echo string upon connection\n"; - fwrite($clientsocket, $cmd, strlen($cmd)); - $response = fgets($clientsocket, 4096); - echo "Response is: ", $response, "
\n"; - flush(); - - $cmd = "ECHO second test for echo\n"; - fwrite($clientsocket, $cmd, strlen($cmd)); - $response = fgets($clientsocket, 4096); - echo "Response is: ", $response, "
\n"; - flush(); - */ + if (!isset($_SESSION["serv_humannode"])) { + $server_info = ctdl_get_serv_info(); + // print_r($server_info); + $keys = array_keys($server_info); + foreach ($keys as $key) + $_SESSION[$key] = $server_info[$key]; } - else { - echo "ERROR: no Citadel socket!
\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); + } } + } +// +// Clear out both our Citadel session and our PHP session. We're done. +// function ctdl_end_session() { global $clientsocket, $session; - session_destroy(); + // Tell the Citadel server to terminate our connection. + // (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); - unset($session); - echo "Session destroyed.
\n"; - + // Now clear our PHP session. + $_SESSION = array(); + session_write_close(); } ?>