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 // admin: Admin API handle. Use cautiously.
12 // person: If logged in, the user's GetPersons() details
13 // api: If logged in, the user's API handle
15 // Mark Huang <mlhuang@cs.princeton.edu>
16 // Copyright (C) 2006 The Trustees of Princeton University
21 // Usually in /etc/planetlab/php
22 require_once 'plc_config.php';
24 // Usually in /usr/share/plc_api/php
25 require_once 'plc_api.php';
28 chdir($_SERVER['DOCUMENT_ROOT']);
29 $included = include_once('./includes/bootstrap.inc');
30 if ($included === TRUE) {
31 // Already included, no need to bootstrap
32 } elseif ($included) {
33 // Not already included, initialize Drupal session handling
34 drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
36 // Drupal not available, use regular PHP session handling
48 function PLCSession($name = NULL, $pass = NULL)
50 $name= strtolower( $name );
53 $api = new PLCAPI(array('AuthMethod' => "password",
55 'AuthString' => $pass));
57 // Authenticate user and get session key
58 $session = $api->GetSession();
63 // Change GetSession() at some point to return expires as well
64 $expires = time() + (24 * 60 * 60);
66 // Change to session authentication
67 $api->auth = array('AuthMethod' => "session", 'session' => $session);
70 // Get account details
71 list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
72 $this->person = $person;
74 // Save session variables
75 $_SESSION['plc'] = array('auth' => $api->auth,
77 'expires' => $expires);
81 function BecomePerson($person_id)
83 list($person) = $this->api->GetPersons(array($person_id));
86 //Get this users session if one exists, create
88 list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
91 $session = $this->api->AddSession($person['person_id']);
95 $session = $session['session_id'];
98 // Update session authentication info
99 $this->alt_auth = $this->api->auth;
100 $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
103 $this->alt_person = $this->person;
104 $this->person = $person;
106 // Save session variables
107 $_SESSION['plc']['auth'] = $this->api->auth;
108 $_SESSION['plc']['person'] = $this->person;
109 $_SESSION['plc']['alt_person'] = $this->alt_person;
110 $_SESSION['plc']['alt_auth'] = $this->alt_auth;
115 function BecomeSelf()
117 if($this->alt_auth && $this->alt_person )
119 $this->person = $this->alt_person;
120 $this->api->auth = $this->alt_auth;
121 $this->alt_person = NULL;
122 $this->alt_auth = NULL;
124 $_SESSION['plc']['auth'] = $_SESSION['plc']['alt_auth'];
125 $_SESSION['plc']['person'] = $_SESSION['plc']['alt_person'];
126 unset($_SESSION['plc']['alt_auth']);
127 unset($_SESSION['plc']['alt_person']);
134 $this->api->DeleteSession();
140 $plc = new PLCSession();
142 if (!empty($_SESSION['plc'])) {
143 if ($_SESSION['plc']['expires'] > time()) {
144 $plc->person = $_SESSION['plc']['person'];
145 $plc->api = new PLCAPI($_SESSION['plc']['auth']);
146 $plc->alt_person = $_SESSION['plc']['alt_person'];
147 $plc->alt_auth = $_SESSION['plc']['alt_auth'];
149 // Destroy PHP session