1afbc7a2d02f6a827b973622874674c3b2940a2d
[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 // person: If logged in, the user's GetPersons() details
12 // api: If logged in, the user's API handle
13 //
14 // Mark Huang <mlhuang@cs.princeton.edu>
15 // Copyright (C) 2006 The Trustees of Princeton University
16 //
17 // $Id$ $
18 //
19
20 // Usually in /etc/planetlab/php
21 require_once 'plc_config.php';
22
23 // Usually in /usr/share/plc_api/php
24 require_once 'plc_api.php';
25
26
27 require_once 'plc_functions.php';
28
29
30 $cwd = getcwd();
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);
38 } else {
39   // Drupal not available, use regular PHP session handling
40   session_start();
41 }
42 chdir($cwd);
43
44 class PLCSession
45 {
46   var $api;
47   var $person;
48   var $alt_person;
49   var $alt_auth;
50
51   function PLCSession($name = NULL, $pass = NULL)
52   {
53     $name= strtolower( $name );
54     // User API access
55     if ($name && $pass) {
56       $api = new PLCAPI(array('AuthMethod' => "password",
57                               'Username' => $name,
58                               'AuthString' => $pass));
59
60       // Authenticate user and get session key
61       $session = $api->GetSession();
62       if (!$session) {
63         return NULL;
64       }
65
66       // Change GetSession() at some point to return expires as well
67       $expires = time() + (24 * 60 * 60);
68
69       // Change to session authentication
70       $api->auth = array('AuthMethod' => "session", 'session' => $session);
71       $this->api = $api;
72
73       // Get account details
74       list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
75       $this->person = $person;
76
77       // Save session variables
78       $_SESSION['plc'] = array('auth' => $api->auth,
79                                'person' => $person,
80                                'expires' => $expires);
81      }  
82     }
83
84     function BecomePerson($person_id)
85     {
86         list($person) = $this->api->GetPersons(array($person_id));
87         if ($person)
88         {
89             //Get this users session if one exists, create 
90             //one otherwise
91             list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
92             if (!$session)
93             {
94                 $session = $this->api->AddSession($person['person_id']);        
95             }
96             else
97             {
98                 $session = $session['session_id'];
99             }
100
101             // Update session authentication info
102             $this->alt_auth = $this->api->auth;
103             $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
104
105             // su to user
106             $this->alt_person = $this->person;
107             $this->person = $person;
108
109             // Save session variables
110             $_SESSION['plc']['auth'] = $this->api->auth;
111             $_SESSION['plc']['person'] = $this->person;
112             $_SESSION['plc']['alt_person'] = $this->alt_person;
113             $_SESSION['plc']['alt_auth'] = $this->alt_auth;
114             
115         }       
116     }
117
118     function BecomeSelf()
119     {   
120         if($this->alt_auth && $this->alt_person )
121         {
122             $this->person = $this->alt_person;
123             $this->api->auth = $this->alt_auth;
124             $this->alt_person = NULL;
125             $this->alt_auth = NULL;
126
127             $_SESSION['plc']['auth'] = $_SESSION['plc']['alt_auth'];
128             $_SESSION['plc']['person'] = $_SESSION['plc']['alt_person'];
129             unset($_SESSION['plc']['alt_auth']);
130             unset($_SESSION['plc']['alt_person']);
131         } 
132     }
133   
134
135   function logout()
136   {
137     $this->api->DeleteSession();
138   }
139 }
140
141 global $plc, $api;
142
143 $plc = new PLCSession();
144
145 if (!empty($_SESSION['plc'])) {
146   if ($_SESSION['plc']['expires'] > time()) {
147     $plc->person = $_SESSION['plc']['person'];
148     $plc->api = new PLCAPI($_SESSION['plc']['auth']);
149     if (array_key_exists('alt_person',$_SESSION['plc']))
150       $plc->alt_person = $_SESSION['plc']['alt_person'];
151     if (array_key_exists('alt_auth',$_SESSION['plc']))
152       $plc->alt_auth = $_SESSION['plc']['alt_auth'];
153   } else {
154     // Destroy PHP session
155     session_destroy();
156   }
157 }
158
159 // For convenience
160 $api = $plc->api;
161
162 if ($api && $api->AuthCheck() != 1) {
163   $current_pagename = basename($_SERVER['PHP_SELF']);
164   if ($current_pagename != basename(l_logout())) {
165     plc_redirect(l_logout());
166   }
167 }
168
169 ?>