admin db search rewritten + removed some obsolete action files
[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 $cwd = getcwd();
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);
34 } else {
35   // Drupal not available, use regular PHP session handling
36   session_start();
37 }
38 chdir($cwd);
39
40 class PLCSession
41 {
42   var $api;
43   var $person;
44   var $alt_person;
45   var $alt_auth;
46
47   function PLCSession($name = NULL, $pass = NULL)
48   {
49     $name= strtolower( $name );
50     // User API access
51     if ($name && $pass) {
52       $api = new PLCAPI(array('AuthMethod' => "password",
53                               'Username' => $name,
54                               'AuthString' => $pass));
55
56       // Authenticate user and get session key
57       $session = $api->GetSession();
58       if (!$session) {
59         return NULL;
60       }
61
62       // Change GetSession() at some point to return expires as well
63       $expires = time() + (24 * 60 * 60);
64
65       // Change to session authentication
66       $api->auth = array('AuthMethod' => "session", 'session' => $session);
67       $this->api = $api;
68
69       // Get account details
70       list($person) = $api->GetPersons(array('email'=>$name,'peer_id'=>NULL));
71       $this->person = $person;
72
73       // Save session variables
74       $_SESSION['plc'] = array('auth' => $api->auth,
75                                'person' => $person,
76                                'expires' => $expires);
77      }  
78     }
79
80     function BecomePerson($person_id)
81     {
82         list($person) = $this->api->GetPersons(array($person_id));
83         if ($person)
84         {
85             //Get this users session if one exists, create 
86             //one otherwise
87             list($session) = $this->api->GetSessions(array('person_id' => $person['person_id']));
88             if (!$session)
89             {
90                 $session = $this->api->AddSession($person['person_id']);        
91             }
92             else
93             {
94                 $session = $session['session_id'];
95             }
96
97             // Update session authentication info
98             $this->alt_auth = $this->api->auth;
99             $this->api->auth = array('AuthMethod' => "session", 'session' => $session);
100
101             // su to user
102             $this->alt_person = $this->person;
103             $this->person = $person;
104
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;
110             
111         }       
112     }
113
114     function BecomeSelf()
115     {   
116         if($this->alt_auth && $this->alt_person )
117         {
118             $this->person = $this->alt_person;
119             $this->api->auth = $this->alt_auth;
120             $this->alt_person = NULL;
121             $this->alt_auth = NULL;
122
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']);
127         } 
128     }
129   
130
131   function logout()
132   {
133     $this->api->DeleteSession();
134   }
135 }
136
137 global $plc, $api;
138
139 $plc = new PLCSession();
140
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     if (array_key_exists('alt_person',$_SESSION['plc']))
146       $plc->alt_person = $_SESSION['plc']['alt_person'];
147     if (array_key_exists('alt_auth',$_SESSION['plc']))
148       $plc->alt_auth = $_SESSION['plc']['alt_auth'];
149   } else {
150     // Destroy PHP session
151     session_destroy();
152   }
153 }
154
155 // For convenience
156 $api = $plc->api;
157
158 ?>