balanced tags
[plewww.git] / modules / planetlab.module
1 <?php // -*-php-*-
2   //
3   // PlanetLab authentication and integration with Drupal
4   //
5   // Mark Huang <mlhuang@cs.princeton.edu>
6   // Copyright (C) 2006 The Trustees of Princeton University
7   //
8   // $Id$
9   //
10
11 require_once 'plc_config.php';
12 require_once 'plc_session.php';
13
14 function planetlab_help($section) {
15   switch ($section) {
16   case 'admin/modules#description':
17     return t('Enables authenticated login via a PlanetLab API server.');
18   }
19 }
20
21 function planetlab_info($field = 0) {
22   $info['name'] = 'PlanetLab';
23
24   if ($field) {
25     return $info[$field];
26   } else {
27     return $info;
28   }
29 }
30
31 function planetlab_menu($may_cache) {
32   $items = array();
33
34   if ($may_cache) {
35     $items[] = array(
36                      'path' => 'planetlab/logout',
37                      'title' => t('Log out of %s', array('%s' => variable_get('site_name', 'local'))),
38                      'callback' => 'planetlab_logout',
39                      'access' => TRUE,
40                      'type' => MENU_CALLBACK
41                      );
42     $items[] = array(
43                      'path' => 'db',
44                      'title' => variable_get('site_name', 'local'),
45                      'callback' => 'planetlab_page',
46                      'access' => TRUE,
47                      'type' => MENU_CALLBACK
48                      );
49   }
50
51   return $items;
52 }
53
54 function planetlab_block($op = 'list', $delta = 0, $edit = array()) {
55   global $user, $plc;
56
57   if ($op == 'list') {
58     $blocks[0]['info'] = t('PlanetLab login');
59
60     return $blocks;
61
62   } else if ($op == 'view') {
63     $block = array();
64
65     if (!$plc->person) {
66       // Force login via HTTPS
67       unset($_GET['time']);
68       $form['#action'] = "https://" . $_SERVER['HTTP_HOST'] . url($_GET['q'], drupal_get_destination());
69       $form['#id'] = 'planetlab-login-form';
70       $form['name'] = array('#type' => 'textfield',
71                             '#title' => t('E-mail'),
72                             '#maxlength' => 60,
73                             '#size' => 25,
74                             '#required' => TRUE,
75                             );
76       $form['pass'] = array('#type' => 'password',
77                             '#title' => t('Password'),
78                             '#size' => 25,
79                             '#required' => TRUE,
80                             );
81       $form['submit'] = array('#type' => 'submit',
82                               '#value' => t('Log in'),
83                               );
84
85       $block['subject'] = t('%s login', array('%s' => variable_get('site_name', 'local')));
86       $block['content'] = drupal_get_form('planetlab_login_block', $form, 'planetlab_login');
87       $block['content'] .= '<p><p><a href="/db/persons/reset_password.php">Forgot your password?</a></p>';
88       $block['content'] .= '<p><a href="/db/persons/register.php">Create an account</a></p>';
89       $block['content'] .= '<p><a href="/db/sites/register.php">File a site registration</a></p>';
90     } else {
91       $block['subject'] = $plc->person['email'];
92       $is_admin = in_array(10,$plc->person['role_ids']);
93       $is_pi    = in_array(20,$plc->person['role_ids']);
94       $is_user  = in_array(30,$plc->person['role_ids']);
95       $is_tech  = in_array(40,$plc->person['role_ids']);
96
97       $site_item = '';
98       $site_item .= '<a href="/db/sites/">Sites</a>';
99       $site_item .= '<ul>';
100       $site_item .= '<li><a href="/db/sites/index.php?id='. $plc->person['site_ids'][0] .'">My Site</a></li>';
101       if ( $is_admin ) 
102         $site_item .= "<li><a href='/db/sites/join_request.php'>Join Requests</a></li>";
103       if( $is_admin )
104         $site_item .= "<li><a href='/db/sites/peers.php'>Migration Status</a></li>";
105       $site_item .= "</ul>";
106       $items[] = $site_item;
107
108       $user_item = '';
109       $user_item .= '<a href="/db/persons/">Users</a>';
110       $user_item .= '<ul>';
111       $user_item .= '<li><a href="/db/persons/index.php?id='. $plc->person['person_id'] .'">My account</a></li>';
112       if (is_pi) 
113         $user_item .= '<li><a href="/db/persons/index.php?site_id='. $plc->person['site_ids'][0] .'">My users</a></li>';
114       if ( $plc->alt_person && $plc->alt_auth) {
115         $email = $plc->person['email']; 
116         $user_item .= "<li><a href='/db/sulogout.php'>Log out of $email </a></li>";
117       } 
118
119
120       $user_item .= '</ul>';
121       $items [] = $user_item;
122
123       $node_item = '';
124       $node_item .= '<a href="/db/nodes/">Nodes</a>';
125       $node_item .= '<ul>';
126       $node_item .= '<li><a href="/db/nodes/index.php?site_id='. $plc->person['site_ids'][0] .'">My Site Nodes</a></li>';
127       $node_item .= "<li><a href='/db/nodes/add_node.php'>Add Node</a></li>";
128       if ( $is_admin) 
129         $node_item .= "<li> <a href='/db/nodes/settings.php'> Setting Types </a> </li>";
130       $node_item .= '</ul>';
131       $items [] = $node_item;
132
133       $slice_item = '';
134       //if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) ) 
135       $slice_item .= '<a href="/db/slices/">Slices</a>';
136       $slice_item .= '<ul>';
137       if( $is_admin || $is_pi ) {
138         $slice_item .= "<li><a href='/db/slices/add_slice.php'>Create Slice</a></li>";
139       }
140       if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) ) 
141         $slice_item .= '<li><a href="/db/sirius/index.php">Sirius</a></li>';
142       $slice_item .= '</ul>';
143       $items [] = $slice_item;
144
145       if( $is_admin || $is_pi ) {
146         $tag_item = '';
147         $tag_item .= '<a href="/db/slices/tags.php?type=all">Tags</a>';
148         $tag_item .= '<ul>';
149         $tag_item .= "<li><a href='/db/tags/index.php?type=node'>Node Tags</a></li>";
150         $tag_item .= "<li><a href='/db/tags/index.php?type=interface'>Interface Tags</a></li>";
151         $tag_item .= "<li><a href='/db/tags/index.php?type=slice'>Slice Tags</a></li>";
152         $tag_item .= "<li><a href='/db/tags/nodegroups.php'>Node groups (xxx)</a></li>";
153         $tag_item .= '</ul>';
154         $items [] = $tag_item;
155       }
156       
157
158       if ( $is_admin )
159         $items[] = l(t('Peers'),'db/peers/');
160      
161       if ( $is_admin )
162         $items[] = l(t('Events'),'db/events/');
163       
164       $items[] = l(t('About'),'db/about.php');
165
166       if ($user->uid) {
167         // Drupal logout (destroys the session and cleans up $user)
168         $items[] = l(t('Log out of %s', array('%s' => variable_get('site_name', 'local'))), 'logout');
169       } else {
170         // PlanetLab logout (just destroy the session)
171         $items[] = l(t('Log out'), 'planetlab/logout');
172       }
173
174       $block['content'] = theme('item_list', $items);
175     }
176
177     /*
178      ob_start();
179      print '<pre>';
180      print_r($_SESSION);
181      print '</pre>';
182      $block['content'] .= ob_get_contents();
183      ob_end_clean();
184     */
185
186     return $block;
187   }
188 }
189
190 function planetlab_login_validate($form_id, $form_values) {
191   global $user, $plc;
192
193   if ($form_values['name'] && $form_values['pass']) {
194     // Drupal login succeeded
195     if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
196         $user->uid) {
197       return;
198     }
199
200     $plc = new PLCSession($form_values['name'], $form_values['pass']);
201
202     // PlanetLab login failed
203     if (!$plc->person) {
204       form_set_error('login', t('Sorry. Unrecognized username or password.'));
205       watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
206     }
207
208     // PlanetLab login succeeded
209     else {
210       // Login admins to Drupal as the superuser
211       if (in_array('admin', $plc->person['roles'])) {
212         $user = user_load(array('uid' => 1));
213       }
214     }
215   }
216 }
217
218 function planetlab_login_submit($form_id, $form_values) {
219   global $plc;
220
221   // Our referring page is encased in a query string of the form
222   // "destination=referrer".
223   parse_str(drupal_get_destination()); // => $destination
224
225   // The referrer itself is a URL path with the original query string,
226   // e.g. "referer.php?query".
227   extract(parse_url($destination)); // => $query
228
229   // Which we then have to parse again as a query string.
230   parse_str($query); // => $url
231
232   if ($plc->person) {
233     // To handle the edge case where this function is called during a
234     // bootstrap, check for the existence of t().
235     if (function_exists('t')) {
236       $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
237     }
238     else {
239       $message = "Session opened for ". check_plain($person['email']);
240     }
241     watchdog('planetlab', $message);
242
243     if (empty($url)) {
244       // Create a timestamped final URL so that browsers don't return the user to
245       // a cached page (where it would appear as if they never logged in or out).
246       return array('time='. time());
247     } else {
248       // Make sure that redirections are always local
249       $url = urldecode($url);
250       if ($url[0] != "/") {
251         $url = "/$url";
252       }
253       Header("Location: $url");
254       exit();
255     }
256   }
257 }
258
259 function planetlab_logout() {
260   global $plc;
261
262   if ($plc->person) {
263     // Invalidate PlanetLab session
264     $plc->logout();
265     watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
266   }
267
268   // Destroy the current session:
269   session_destroy();
270
271   // The time prevents caching.
272   drupal_goto(NULL, 'time='. time());
273 }
274
275 function planetlab_user($type, &$edit, &$user, $category = NULL)
276 {
277   switch ($type) {
278   case 'logout':
279     if ($plc->person) {
280       $plc->logout();
281       watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
282     }
283     break;
284   }
285 }
286
287 function planetlab_page()
288 {
289   $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
290
291   // error_log("Requested " . $_GET['q'] . " -> $path");
292
293   if (is_dir($path)) {
294     foreach (array('index.php', 'index.html', 'index.htm') as $index) {
295       if (is_file($path . "/$index")) {
296         $path .= "/$index";
297         break;
298       }
299     }
300   }
301
302   if (is_file($path)) {
303     if (preg_match('/.php$/', $path)) {
304       ob_start();
305       include $path;
306       $output = ob_get_contents();
307       ob_end_clean();
308     } else {
309       $output = file_get_contents($path);
310     }
311     return $output;
312   }
313             
314   drupal_not_found();
315 }
316
317 function theme_planetlab($content)
318 {
319   return $content;
320 }
321
322 ?>