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 // Usually in /etc/planetlab/php
21 require_once 'plc_config.php';
23 // Usually in /usr/share/plc_api/php
24 require_once 'plc_api.php';
27 require_once 'plc_functions.php';
31 chdir($_SERVER['DOCUMENT_ROOT']);
32 $included = include_once('./includes/bootstrap.inc');
33 if ($included === TRUE) {
34 // Already included, no need to bootstrap
35 } elseif ($included) {
36 // Not already included, initialize Drupal session handling
37 drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
39 // Drupal not available, use regular PHP session handling
51 function PLCSession($name = NULL, $pass = NULL)
53 $name= strtolower( $name );
56 $api = new PLCAPI(array('AuthMethod' => "password",
58 'AuthString' => $pass));
60 // Authenticate user and get session key
61 $seconds_to_expire = (24 * 60 * 60 * 14);
62 $session = $api->GetSession($seconds_to_expire);
67 // Change GetSession() at some point to return expires as well
68 $expires = time() + $seconds_to_expire;
70 // Change to session authentication
71 $api->auth = array('AuthMethod' => "session", 'session' => $session);
74 // Get account details
75 list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
76 $this->person = $person;
78 // Save session variables
79 $_SESSION['plc'] = array('auth' => $api->auth,
81 'expires' => $expires);
85 function BecomePerson($person_id)
87 list($person) = $this->api->GetPersons(array($person_id));
90 //Get this users session if one exists, create
92 list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
95 $session = $this->api->AddSession($person['person_id']);
99 $session = $session['session_id'];
102 // Update session authentication info
103 $this->alt_auth = $this->api->auth;
104 $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
107 $this->alt_person = $this->person;
108 $this->person = $person;
110 // Save session variables
111 $_SESSION['plc']['auth'] = $this->api->auth;
112 $_SESSION['plc']['person'] = $this->person;
113 $_SESSION['plc']['alt_person'] = $this->alt_person;
114 $_SESSION['plc']['alt_auth'] = $this->alt_auth;
119 function BecomeSelf()
121 if($this->alt_auth && $this->alt_person )
123 $this->person = $this->alt_person;
124 $this->api->auth = $this->alt_auth;
125 $this->alt_person = NULL;
126 $this->alt_auth = NULL;
128 $_SESSION['plc']['auth'] = $_SESSION['plc']['alt_auth'];
129 $_SESSION['plc']['person'] = $_SESSION['plc']['alt_person'];
130 unset($_SESSION['plc']['alt_auth']);
131 unset($_SESSION['plc']['alt_person']);
138 $this->api->DeleteSession();
144 $plc = new PLCSession();
146 if (!empty($_SESSION['plc'])) {
147 if ($_SESSION['plc']['expires'] > time()) {
148 $plc->person = $_SESSION['plc']['person'];
149 $plc->api = new PLCAPI($_SESSION['plc']['auth']);
150 if (array_key_exists('alt_person',$_SESSION['plc']))
151 $plc->alt_person = $_SESSION['plc']['alt_person'];
152 if (array_key_exists('alt_auth',$_SESSION['plc']))
153 $plc->alt_auth = $_SESSION['plc']['alt_auth'];
155 // Destroy PHP session
163 if ($api && $api->AuthCheck() != 1) {
164 $current_pagename = basename($_SERVER['PHP_SELF']);
165 if ($current_pagename != basename(l_logout())) {
166 plc_redirect(l_logout());