Add a 14 day default session time rather than 24 hours.
[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       $seconds_to_expire = (24 * 60 * 60 * 14);
62       $session = $api->GetSession($seconds_to_expire);
63       if (!$session) {
64           return NULL;
65       }
66
67       // Change GetSession() at some point to return expires as well
68       $expires = time() + $seconds_to_expire;
69
70       // Change to session authentication
71       $api->auth = array('AuthMethod' => "session", 'session' => $session);
72       $this->api = $api;
73
74       // Get account details
75       list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
76       $this->person = $person;
77
78       // Save session variables
79       $_SESSION['plc'] = array('auth' => $api->auth,
80                                'person' => $person,
81                                'expires' => $expires);
82      }  
83     }
84
85     function BecomePerson($person_id)
86     {
87         list($person) = $this->api->GetPersons(array($person_id));
88         if ($person)
89         {
90             //Get this users session if one exists, create 
91             //one otherwise
92             list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
93             if (!$session)
94             {
95                 $session = $this->api->AddSession($person['person_id']);        
96             }
97             else
98             {
99                 $session = $session['session_id'];
100             }
101
102             // Update session authentication info
103             $this->alt_auth = $this->api->auth;
104             $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
105
106             // su to user
107             $this->alt_person = $this->person;
108             $this->person = $person;
109
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;
115             
116         }       
117     }
118
119     function BecomeSelf()
120     {   
121         if($this->alt_auth && $this->alt_person )
122         {
123             $this->person = $this->alt_person;
124             $this->api->auth = $this->alt_auth;
125             $this->alt_person = NULL;
126             $this->alt_auth = NULL;
127
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']);
132         } 
133     }
134   
135
136   function logout()
137   {
138     $this->api->DeleteSession();
139   }
140 }
141
142 global $plc, $api;
143
144 $plc = new PLCSession();
145
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'];
154   } else {
155     // Destroy PHP session
156     session_destroy();
157   }
158 }
159
160 // For convenience
161 $api = $plc->api;
162
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());
167   }
168 }
169
170 ?>