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