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
20 // warning: Undefined array key "#validated" in /var/www/html/includes/form.inc on line 228.
22 set_error_handler(function(int $errno, string $errstr) {
23 if ((strpos($errstr, 'Undefined array key') !== false) && (strpos($errstr, '/var/www/html/includes/') !== false))
25 // for filtering undefined variables
26 // if (strpos($errstr, 'Undefined variable') !== false)
32 // Usually in /etc/planetlab/php
33 require_once 'plc_config.php';
35 // Usually in /usr/share/plc_api/php
36 require_once 'plc_api.php';
39 require_once 'plc_functions.php';
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);
51 // Drupal not available, use regular PHP session handling
63 function __construct($name = NULL, $pass = NULL)
65 $name= strtolower( $name );
68 $api = new PLCAPI(array('AuthMethod' => "password",
70 'AuthString' => $pass));
72 // Authenticate user and get session key
73 $seconds_to_expire = (24 * 60 * 60 * 14);
74 $session = $api->GetSession($seconds_to_expire);
79 // Change GetSession() at some point to return expires as well
80 $expires = time() + $seconds_to_expire;
82 // Change to session authentication
83 $api->auth = array('AuthMethod' => "session", 'session' => $session);
86 // Get account details
87 list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
88 $this->person = $person;
90 // Save session variables
91 $_SESSION['plc'] = array('auth' => $api->auth,
93 'expires' => $expires);
97 function BecomePerson($person_id)
99 list($person) = $this->api->GetPersons(array($person_id));
102 //Get this users session if one exists, create
104 list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
107 $session = $this->api->AddSession($person['person_id']);
111 $session = $session['session_id'];
114 // Update session authentication info
115 $this->alt_auth = $this->api->auth;
116 $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
119 $this->alt_person = $this->person;
120 $this->person = $person;
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;
131 function BecomeSelf()
133 if($this->alt_auth && $this->alt_person )
135 $this->person = $this->alt_person;
136 $this->api->auth = $this->alt_auth;
137 $this->alt_person = NULL;
138 $this->alt_auth = NULL;
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']);
150 $this->api->DeleteSession();
156 $plc = new PLCSession();
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'];
167 // Destroy PHP session
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());