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