* Reinstated the 'Delete' button in the mailbox view, for use by users who don't...
[citadel.git] / ctdlphp / ctdlsession.php
blobc9614c753ef57d2b24ee068d67e2d4eed051f4ca
1 <?PHP
2 // $Id$
3 //
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.
6 //
7 // Web designers: please make changes in ctdlheader.php, not here.
8 //
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.");
22 session_start();
24 if (isset($_SESSION["ctdlsession"])) {
25 $session = $_SESSION["ctdlsession"];
27 else {
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" ;
37 $errno = 0;
38 $errstr = "";
39 if (is_array(stat($sockname)))
40 $clientsocket = fsockopen(SOCKET_PREFIX.$sockname, 0, $errno, $errstr, 5);
41 else
42 $clientsocket = false;
43 //// TODO: if we get connection refused...
44 echo "$socketname - $errno - $errstr";
46 if (!$clientsocket) {
47 // It ain't there, dude. Open up the proxy. (C version)
48 //$cmd = "./sessionproxy " . $sockname ;
49 //exec($cmd);
51 // It ain't there, dude. Open up the proxy. (PHP version)
52 if (CITADEL_DEBUG_PROXY){
53 $stdout = '>>/tmp/sessionproxyout.txt ';
55 else{
56 $stdout = '>/dev/null ';
59 $cmd = "./sessionproxy.php " . $sockname .
60 " </dev/null ".$stdout."2>&1 " .
61 " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
62 exec($cmd);
63 sleep(1);
65 // Keep attempting connections 10 times per second up to 100 times
66 $attempts = 0;
67 while (!$clientsocket) {
68 usleep(100);
69 if (is_array(stat($sockname)))
70 $clientsocket = fsockopen(SOCKET_PREFIX.$sockname, 0, $errno, $errstr, 5);
71 else
72 $clientsocket = false;
73 $attempts += 1;
74 if ($attempts > 100) {
75 echo "ERROR: unable to start connection proxy. ";
76 echo "Please contact your system administrator.<BR>\n";
77 flush();
78 exit(1);
82 // At this point we have a good connection to Citadel.
83 $identity=array(
84 "DevelNr" => '0',
85 "ClientID" => '8',
86 "VersionNumber" => '001',
87 "ClientInfoString" => 'PHP web client|',
88 "Remote Address" => $_SERVER['REMOTE_ADDR'] );
90 ctdl_iden($identity); // Identify client
91 ctdl_MessageFormatsPrefered(array("text/html","text/plain"));
92 if (isset($_SESSION["username"])) {
93 login_existing_user(
94 $_SESSION["username"],
95 $_SESSION["password"]
99 if (isset($_SESSION["room"])) {
100 ctdl_goto($_SESSION["room"]);
102 else {
103 ctdl_goto("_BASEROOM_");
107 if (!isset($_SESSION["serv_humannode"])) {
108 $server_info = ctdl_get_serv_info();
109 // print_r($server_info);
110 $keys = array_keys($server_info);
111 foreach ($keys as $key)
112 $_SESSION[$key] = $server_info[$key];
115 // If the user is trying to call up any page other than
116 // login.php logout.php do_login.php,
117 // and the session is not logged in, redirect to login.php
119 if (isset($_SESSION["logged_in"]) && ($_SESSION["logged_in"] != 1)) {
120 $filename = basename(getenv('SCRIPT_NAME'));
121 if ( (strcmp($filename, "login.php"))
122 && (strcmp($filename, "logout.php"))
123 && (strcmp($filename, "do_login.php"))
125 header("Location: login.php");
126 exit(0);
135 // Clear out both our Citadel session and our PHP session. We're done.
137 function ctdl_end_session() {
138 global $clientsocket, $session;
140 // Tell the Citadel server to terminate our connection.
141 // (The extra newlines force it to see that the Citadel session
142 // ended, and the proxy will quit.)
144 fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
145 $response = fgets($clientsocket, 4096); // IGnore response
146 fclose($clientsocket);
147 unset($clientsocket);
149 // Now clear our PHP session.
150 $_SESSION = array();
151 session_write_close();