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