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