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 chdir($_SERVER['DOCUMENT_ROOT']);
28 $included = include_once('./includes/bootstrap.inc');
29 if ($included === TRUE) {
30 // Already included, no need to bootstrap
31 } elseif ($included) {
32 // Not already included, initialize Drupal session handling
33 drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
35 // Drupal not available, use regular PHP session handling
47 function PLCSession($name = NULL, $pass = NULL)
49 $name= strtolower( $name );
52 $api = new PLCAPI(array('AuthMethod' => "password",
54 'AuthString' => $pass));
56 // Authenticate user and get session key
57 $session = $api->GetSession();
62 // Change GetSession() at some point to return expires as well
63 $expires = time() + (24 * 60 * 60);
65 // Change to session authentication
66 $api->auth = array('AuthMethod' => "session", 'session' => $session);
69 // Get account details
70 list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
71 $this->person = $person;
73 // Save session variables
74 $_SESSION['plc'] = array('auth' => $api->auth,
76 'expires' => $expires);
80 function BecomePerson($person_id)
82 list($person) = $this->api->GetPersons(array($person_id));
85 //Get this users session if one exists, create
87 list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
90 $session = $this->api->AddSession($person['person_id']);
94 $session = $session['session_id'];
97 // Update session authentication info
98 $this->alt_auth = $this->api->auth;
99 $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
102 $this->alt_person = $this->person;
103 $this->person = $person;
105 // Save session variables
106 $_SESSION['plc']['auth'] = $this->api->auth;
107 $_SESSION['plc']['person'] = $this->person;
108 $_SESSION['plc']['alt_person'] = $this->alt_person;
109 $_SESSION['plc']['alt_auth'] = $this->alt_auth;
114 function BecomeSelf()
116 if($this->alt_auth && $this->alt_person )
118 $this->person = $this->alt_person;
119 $this->api->auth = $this->alt_auth;
120 $this->alt_person = NULL;
121 $this->alt_auth = NULL;
123 $_SESSION['plc']['auth'] = $_SESSION['plc']['alt_auth'];
124 $_SESSION['plc']['person'] = $_SESSION['plc']['alt_person'];
125 unset($_SESSION['plc']['alt_auth']);
126 unset($_SESSION['plc']['alt_person']);
133 $this->api->DeleteSession();
139 $plc = new PLCSession();
141 if (!empty($_SESSION['plc'])) {
142 if ($_SESSION['plc']['expires'] > time()) {
143 $plc->person = $_SESSION['plc']['person'];
144 $plc->api = new PLCAPI($_SESSION['plc']['auth']);
145 $plc->alt_person = $_SESSION['plc']['alt_person'];
146 $plc->alt_auth = $_SESSION['plc']['alt_auth'];
148 // Destroy PHP session