4e0fbec045f5ab578c713e8449aa837379f01362
[plewww.git] / planetlab / includes / plc_session.php
1 <?php
2 //
3 // PlanetLab session handling. In a Drupal environment, session
4 // variables are stored in the database (i.e., the session handling
5 // functions have been overridden). By default, they are stored on the
6 // filesystem.
7 //
8 // To use, include this file and declare the global variable
9 // $plc. This object contains the following members:
10 //
11 // person: If logged in, the user's GetPersons() details
12 // api: If logged in, the user's API handle
13 //
14 // Mark Huang <mlhuang@cs.princeton.edu>
15 // Copyright (C) 2006 The Trustees of Princeton University
16 //
17 // $Id$ $
18 //
19
20 // warning: Undefined array key "#validated" in /var/www/html/includes/form.inc on line 228.
21
22 set_error_handler(function(int $errno, string $errstr) {
23     if ((strpos($errstr, 'Undefined array key') !== false) && (strpos($errstr, '/var/www/html/includes/') !== false))
24         return false;
25     // for filtering undefined variables
26 //        if (strpos($errstr, 'Undefined variable') !== false)
27 //        return false;
28     return true;
29     }, E_WARNING);
30
31
32 // Usually in /etc/planetlab/php
33 require_once 'plc_config.php';
34
35 // Usually in /usr/share/plc_api/php
36 require_once 'plc_api.php';
37
38
39 require_once 'plc_functions.php';
40
41
42 $cwd = getcwd();
43 chdir($_SERVER['DOCUMENT_ROOT']);
44 $included = include_once('./includes/bootstrap.inc');
45 if ($included === TRUE) {
46   // Already included, no need to bootstrap
47 } elseif ($included) {
48   // Not already included, initialize Drupal session handling
49   drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
50 } else {
51   // Drupal not available, use regular PHP session handling
52   session_start();
53 }
54 chdir($cwd);
55
56 class PLCSession
57 {
58   var $api;
59   var $person;
60   var $alt_person;
61   var $alt_auth;
62
63   function __construct($name = NULL, $pass = NULL)
64   {
65     $name= strtolower( $name );
66     // User API access
67     if ($name && $pass) {
68       $api = new PLCAPI(array('AuthMethod' => "password",
69                               'Username' => $name,
70                               'AuthString' => $pass));
71
72       // Authenticate user and get session key
73       $seconds_to_expire = (24 * 60 * 60 * 14);
74       $session = $api->GetSession($seconds_to_expire);
75       if (!$session) {
76           return NULL;
77       }
78
79       // Change GetSession() at some point to return expires as well
80       $expires = time() + $seconds_to_expire;
81
82       // Change to session authentication
83       $api->auth = array('AuthMethod' => "session", 'session' => $session);
84       $this->api = $api;
85
86       // Get account details
87       list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
88       $this->person = $person;
89
90       // Save session variables
91       $_SESSION['plc'] = array('auth' => $api->auth,
92                                'person' => $person,
93                                'expires' => $expires);
94      }
95     }
96
97     function BecomePerson($person_id)
98     {
99         list($person) = $this->api->GetPersons(array($person_id));
100         if ($person)
101         {
102             //Get this users session if one exists, create
103             //one otherwise
104             list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
105             if (!$session)
106             {
107                 $session = $this->api->AddSession($person['person_id']);
108             }
109             else
110             {
111                 $session = $session['session_id'];
112             }
113
114             // Update session authentication info
115             $this->alt_auth = $this->api->auth;
116             $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
117
118             // su to user
119             $this->alt_person = $this->person;
120             $this->person = $person;
121
122             // Save session variables
123             $_SESSION['plc']['auth'] = $this->api->auth;
124             $_SESSION['plc']['person'] = $this->person;
125             $_SESSION['plc']['alt_person'] = $this->alt_person;
126             $_SESSION['plc']['alt_auth'] = $this->alt_auth;
127
128         }
129     }
130
131     function BecomeSelf()
132     {
133         if($this->alt_auth && $this->alt_person )
134         {
135             $this->person = $this->alt_person;
136             $this->api->auth = $this->alt_auth;
137             $this->alt_person = NULL;
138             $this->alt_auth = NULL;
139
140             $_SESSION['plc']['auth'] = $_SESSION['plc']['alt_auth'];
141             $_SESSION['plc']['person'] = $_SESSION['plc']['alt_person'];
142             unset($_SESSION['plc']['alt_auth']);
143             unset($_SESSION['plc']['alt_person']);
144         }
145     }
146
147
148   function logout()
149   {
150     $this->api->DeleteSession();
151   }
152 }
153
154 global $plc, $api;
155
156 $plc = new PLCSession();
157
158 if (!empty($_SESSION['plc'])) {
159   if ($_SESSION['plc']['expires'] > time()) {
160     $plc->person = $_SESSION['plc']['person'];
161     $plc->api = new PLCAPI($_SESSION['plc']['auth']);
162     if (array_key_exists('alt_person',$_SESSION['plc']))
163       $plc->alt_person = $_SESSION['plc']['alt_person'];
164     if (array_key_exists('alt_auth',$_SESSION['plc']))
165       $plc->alt_auth = $_SESSION['plc']['alt_auth'];
166   } else {
167     // Destroy PHP session
168     session_destroy();
169   }
170 }
171
172 // For convenience
173 $api = $plc->api;
174
175 if ($api && $api->AuthCheck() != 1) {
176   $current_pagename = basename($_SERVER['PHP_SELF']);
177   if ($current_pagename != basename(l_logout())) {
178     plc_redirect(l_logout());
179   }
180 }
181
182 ?>