* socket syntax is different in php5. make it configurable.
[citadel.git] / ctdlphp / ctdlsession.php
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.
11
12
13 function establish_citadel_session() {
14
15         global $session, $clientsocket;
16
17         if (strcmp('4.3.0', phpversion()) > 0) {
18                 die("This program requires PHP 4.3.0 or newer.");
19         }
20
21
22         session_start();
23
24         if (isset($_SESSION["ctdlsession"])) {
25                 $session = $_SESSION["ctdlsession"];
26         }
27         else {
28                 $session = "CtdlSession." . time() . rand(1000,9999) ;
29                 $_SESSION["ctdlsession"] = $session;
30         }
31
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.
35
36         $sockname = "/tmp/" . $session . ".socket" ;
37
38         $clientsocket = fsockopen(SOCKET_PREFIX.$sockname, 0, $errno, $errstr, 5);
39         if (!$clientsocket) {
40                 // It ain't there, dude.  Open up the proxy. (C version)
41                 //$cmd = "./sessionproxy " . $sockname ;
42                 //exec($cmd);
43
44                 // It ain't there, dude.  Open up the proxy.  (PHP version)
45                 if (CITADEL_DEBUG_PROXY){
46                         $stdout = '>>/tmp/sessionproxyout.txt ';
47                 }
48                 else{
49                         $stdout = '>/dev/null ';
50                 }
51
52                 $cmd = "./sessionproxy.php " . $sockname .
53                         " </dev/null ".$stdout."2>&1 " .
54                         " 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 & " ;
55                 exec($cmd);
56                 sleep(1);
57
58                 // Keep attempting connections 10 times per second up to 100 times
59                 $attempts = 0;
60                 while (!$clientsocket) {
61                         usleep(100);
62                         $clientsocket = fsockopen($sockname, 0, $errno, $errstr, 5);
63                         $attempts += 1;
64                         if ($attempts > 100) {
65                                 echo "ERROR: unable to start connection proxy. ";
66                                 echo "Please contact your system administrator.<BR>\n";
67                                 flush();
68                                 exit(1);
69                         }
70                 }
71
72                 // At this point we have a good connection to Citadel.
73                 $identity=array(
74                         "DevelNr" => '0',
75                         "ClientID" => '8',
76                         "VersionNumber" => '001',
77                         "ClientInfoString" => 'PHP web client|',
78                         "Remote Address" => $_SERVER['REMOTE_ADDR'] );
79
80                 ctdl_iden($identity);   // Identify client
81                 ctdl_MessageFormatsPrefered(array("text/html","text/plain"));
82                 if (isset($_SESSION["username"])) {
83                         login_existing_user(
84                                 $_SESSION["username"],
85                                 $_SESSION["password"]
86                         );
87                 }
88
89                 if (isset($_SESSION["room"])) {
90                         ctdl_goto($_SESSION["room"]);
91                 }
92                 else {
93                         ctdl_goto("_BASEROOM_");
94                 }
95         }
96
97         if (!isset($_SESSION["serv_humannode"])) {
98                 $server_info = ctdl_get_serv_info();
99                 print_r($server_info);
100                 $keys = array_keys($server_info);
101                 foreach ($keys as $key)
102                         $_SESSION[$key] = $server_info[$key];
103         }
104
105         // If the user is trying to call up any page other than
106         // login.php logout.php do_login.php,
107         // and the session is not logged in, redirect to login.php
108         //
109         if (isset($_SESSION["logged_in"]) && ($_SESSION["logged_in"] != 1)) {
110                 $filename = basename(getenv('SCRIPT_NAME'));
111                 if (    (strcmp($filename, "login.php"))
112                    &&   (strcmp($filename, "logout.php"))
113                    &&   (strcmp($filename, "do_login.php"))
114                 ) {
115                         header("Location: login.php");
116                         exit(0);
117                 }
118         }
119
120         
121 }
122
123
124 //
125 // Clear out both our Citadel session and our PHP session.  We're done.
126 //
127 function ctdl_end_session() {
128         global $clientsocket, $session;
129
130         // Tell the Citadel server to terminate our connection.
131         // (The extra newlines force it to see that the Citadel session
132         // ended, and the proxy will quit.)
133         //
134         fwrite($clientsocket, "QUIT\n\n\n\n\n\n\n\n\n\n\n");
135         $response = fgets($clientsocket, 4096);         // IGnore response
136         fclose($clientsocket);
137         unset($clientsocket);
138
139         // Now clear our PHP session.
140         $_SESSION = array();
141         session_write_close();
142 }
143
144 ?>