initial import from onelab svn codebase
[plewww.git] / planetlab / includes / plc_session.php
1 <?php
2 //
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
6 // filesystem.
7 //
8 // To use, include this file and declare the global variable
9 // $plc. This object contains the following members:
10 //
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
14 //
15 // Mark Huang <mlhuang@cs.princeton.edu>
16 // Copyright (C) 2006 The Trustees of Princeton University
17 //
18 // $Id: plc_session.php 804 2007-08-31 13:58:58Z thierry $ $
19 //
20
21 // Usually in /etc/planetlab/php
22 require_once 'plc_config.php';
23
24 // Usually in /usr/share/plc_api/php
25 require_once 'plc_api.php';
26
27 $cwd = getcwd();
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);
35 } else {
36   // Drupal not available, use regular PHP session handling
37   session_start();
38 }
39 chdir($cwd);
40
41 class PLCSession
42 {
43   var $api;
44   var $person;
45   var $alt_person;
46   var $alt_auth;
47
48   function PLCSession($name = NULL, $pass = NULL)
49   {
50                 $name= strtolower( $name );
51     // User API access
52     if ($name && $pass) {
53       $api = new PLCAPI(array('AuthMethod' => "password",
54                               'Username' => $name,
55                               'AuthString' => $pass));
56
57       // Authenticate user and get session key
58       $session = $api->GetSession();
59       if (!$session) {
60         return NULL;
61       }
62
63       // Change GetSession() at some point to return expires as well
64       $expires = time() + (24 * 60 * 60);
65
66       // Change to session authentication
67       $api->auth = array('AuthMethod' => "session", 'session' => $session);
68       $this->api = $api;
69
70       // Get account details
71       list($person) = $api->GetPersons(array($name));
72       $this->person = $person;
73
74       // Save session variables
75       $_SESSION['plc'] = array('auth' => $api->auth,
76                                'person' => $person,
77                                'expires' => $expires);
78      }  
79     }
80
81     function BecomePerson($person_id)
82     {
83         list($person) = $this->api->GetPersons(array($person_id));
84         if ($person)
85         {
86             //Get this users session if one exists, create 
87             //one otherwise
88             list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
89             if (!$session)
90             {
91                 $session = $this->api->AddSession($person['person_id']);        
92             }
93             else
94             {
95                 $session = $session['session_id'];
96             }
97
98             // Update session authentication info
99             $this->alt_auth = $this->api->auth;
100             $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
101
102             // su to user
103             $this->alt_person = $this->person;
104             $this->person = $person;
105
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;
111             
112         }       
113     }
114
115     function BecomeSelf()
116     {   
117         if($this->alt_auth && $this->alt_person )
118         {
119             $this->person = $this->alt_person;
120             $this->api->auth = $this->alt_auth;
121             $this->alt_person = NULL;
122             $this->alt_auth = NULL;
123
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']);
128         } 
129     }
130   
131
132   function logout()
133   {
134     $this->api->DeleteSession();
135   }
136 }
137
138 global $plc, $api;
139
140 $plc = new PLCSession();
141
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'];
148   } else {
149     // Destroy PHP session
150     session_destroy();
151   }
152 }
153
154 // For convenience
155 $api = $plc->api;
156
157 ?>