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
8 // To use, include this file and declare the global variable
9 // $plc. This object contains the following members:
11 // person: If logged in, the user's GetPersons() details
12 // api: If logged in, the user's API handle
14 // Mark Huang <mlhuang@cs.princeton.edu>
15 // Copyright (C) 2006 The Trustees of Princeton University
18 // warning: Undefined array key "#validated" in /var/www/html/includes/form.inc on line 228.
20 set_error_handler(function(int $errno, string $errstr) {
21 if ((strpos($errstr, 'Undefined array key') !== false) && (strpos($errstr, '/var/www/html/includes/') !== false))
23 // for filtering undefined variables
24 // if (strpos($errstr, 'Undefined variable') !== false)
30 // Usually in /etc/planetlab/php
31 require_once 'plc_config.php';
33 // Usually in /usr/share/plc_api/php
34 require_once 'plc_api.php';
37 require_once 'plc_functions.php';
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);
49 // Drupal not available, use regular PHP session handling
61 function __construct($name = NULL, $pass = NULL)
63 $name= strtolower( $name );
66 $api = new PLCAPI(array('AuthMethod' => "password",
68 'AuthString' => $pass));
70 // Authenticate user and get session key
71 $seconds_to_expire = (24 * 60 * 60 * 14);
72 $session = $api->GetSession($seconds_to_expire);
77 // Change GetSession() at some point to return expires as well
78 $expires = time() + $seconds_to_expire;
80 // Change to session authentication
81 $api->auth = array('AuthMethod' => "session", 'session' => $session);
84 // Get account details
85 list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
86 $this->person = $person;
88 // Save session variables
89 $_SESSION['plc'] = array('auth' => $api->auth,
91 'expires' => $expires);
95 function BecomePerson($person_id)
97 list($person) = $this->api->GetPersons(array($person_id));
100 //Get this users session if one exists, create
102 list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
105 $session = $this->api->AddSession($person['person_id']);
109 $session = $session['session_id'];
112 // Update session authentication info
113 $this->alt_auth = $this->api->auth;
114 $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
117 $this->alt_person = $this->person;
118 $this->person = $person;
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;
129 function BecomeSelf()
131 if($this->alt_auth && $this->alt_person )
133 $this->person = $this->alt_person;
134 $this->api->auth = $this->alt_auth;
135 $this->alt_person = NULL;
136 $this->alt_auth = NULL;
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']);
148 $this->api->DeleteSession();
154 $plc = new PLCSession();
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'];
165 // Destroy PHP session
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());