3 // PlanetLab authentication and integration with Drupal
5 // Mark Huang <mlhuang@cs.princeton.edu>
6 // Copyright (C) 2006 The Trustees of Princeton University
11 require_once 'plc_config.php';
12 require_once 'plc_session.php';
13 require_once 'plc_functions.php';
15 drupal_set_html_head('<link href="/planetlab/css/plc_style.css" rel="stylesheet" type="text/css"/>');
17 function planetlab_help($section) {
19 case 'admin/modules#description':
20 return t('Enables authenticated login via a PlanetLab API server.');
24 function planetlab_info($field = 0) {
25 $info['name'] = 'PlanetLab';
34 function planetlab_menu($may_cache) {
39 'path' => 'planetlab/common/logout',
40 'title' => t('Logout of %s', array('%s' => variable_get('site_name', 'local'))),
41 'callback' => 'planetlab_logout',
43 'type' => MENU_CALLBACK
47 'title' => variable_get('site_name', 'local'),
48 'callback' => 'planetlab_page',
50 'type' => MENU_CALLBACK
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) {
66 $result .= ul_start();
67 foreach ($menu as $item) $result .= $item;
72 function planetlab_block($op = 'list', $delta = 0, $edit = array()) {
76 $blocks[0]['info'] = t('PlanetLab login');
80 } else if ($op == 'view') {
84 // Force login via HTTPS
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'),
94 $form['pass'] = array('#type' => 'password',
95 '#title' => t('Password'),
99 $form['submit'] = array('#type' => 'submit',
100 '#value' => t('Log in'),
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") );
110 $block['subject'] = truncate($plc->person['email'],30);
112 //////////////////// Logout
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');
120 // PlanetLab logout (just destroy the session)
121 $bullet_item .= href (l_logout(),'Logout');
123 $bullet_item .= ul_start();
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"));
129 $bullet_item .= ul_end();
130 $items []= expanded ($bullet_item);
132 //////////////////// accounts
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)'));
143 $bullet_item .= ul_end();
144 $items [] = expanded($bullet_item);
146 //////////////////// Sites
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);
156 //////////////////// Nodes
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_node_add(),"Add Node"));
162 $bullet_item .= leaf( href (l_register_node(),"Register Node"));
163 $bullet_item .= ul_end();
164 $items [] = expanded($bullet_item);
166 //////////////////// Slices
168 //if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
169 $bullet_item .= href(l_slices(),"Slices");
170 $bullet_item .= ul_start();
172 $bullet_item .= leaf ( href(l_slices_site(plc_my_site_id()), 'My Site Slices'));
173 if( plc_is_admin() || plc_is_pi() ) {
174 $bullet_item .= leaf(href(l_slice_add(),"Create Slice"));
176 if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
177 $bullet_item .= leaf(href(l_sirius(),"Sirius"));
178 $bullet_item .= ul_end();
179 $items [] = expanded($bullet_item);
181 //////////////////// Admin
182 if( plc_is_admin() || plc_is_pi() ) {
184 $bullet_item .= href(l_admin(),"Admin search");
185 $bullet_item .= ul_start();
186 $bullet_item .= leaf( href (l_node_add(),"Add Node"));
187 $bullet_item .= leaf( href( l_tags(),"Tags"));
188 $bullet_item .= leaf( href( l_nodegroups(),"Node groups"));
189 $bullet_item .= leaf (href(l_peers(),'Peers'));
190 if ( plc_is_admin() )
191 $bullet_item .= leaf (href(l_events(),'Events'));
192 $bullet_item .= ul_end();
193 $items [] = expanded($bullet_item);
197 $bullet_item .= href(l_about(),'About MyPLC');
198 $bullet_item .= ul_start();
199 $bullet_item .= leaf ( href (l_doc_plcapi(),"PLCAPI doc"));
200 $bullet_item .= leaf ( href (l_doc_nmapi(),"NMAPI doc"));
201 $bullet_item .= ul_end();
202 $items[] = expanded($bullet_item);
204 //$block['content'] = theme('list_item', $items);
205 $block['content'] = menu_theme($items);
213 $block['content'] .= ob_get_contents();
221 function planetlab_login_validate($form_id, $form_values) {
224 if ($form_values['name'] && $form_values['pass']) {
225 // Drupal login succeeded
226 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
231 $plc = new PLCSession($form_values['name'], $form_values['pass']);
233 // PlanetLab login failed
235 form_set_error('login', t('Sorry. Unrecognized username or password.'));
236 watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
239 // PlanetLab login succeeded
241 // Login admins to Drupal as the superuser
242 if (in_array('admin', $plc->person['roles'])) {
243 $user = user_load(array('uid' => 1));
249 function planetlab_login_submit($form_id, $form_values) {
252 // Our referring page is encased in a query string of the form
253 // "destination=referrer".
254 parse_str(drupal_get_destination()); // => $destination
256 // The referrer itself is a URL path with the original query string,
257 // e.g. "referer.php?query".
258 extract(parse_url($destination)); // => $query
260 // Which we then have to parse again as a query string.
261 parse_str($query); // => $url
264 // To handle the edge case where this function is called during a
265 // bootstrap, check for the existence of t().
266 if (function_exists('t')) {
267 $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
270 $message = "Session opened for ". check_plain($person['email']);
272 watchdog('planetlab', $message);
275 // Create a timestamped final URL so that browsers don't return the user to
276 // a cached page (where it would appear as if they never logged in or out).
277 return array('time='. time());
279 // Make sure that redirections are always local
280 $url = urldecode($url);
281 if ($url[0] != "/") {
284 header("Location: $url");
290 function planetlab_logout() {
294 // Invalidate PlanetLab session
296 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
299 // Destroy the current session:
302 // The time prevents caching.
303 drupal_goto(NULL, 'time='. time());
306 function planetlab_user($type, &$edit, &$user, $category = NULL) {
311 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
317 function planetlab_page() {
318 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
320 // error_log("Requested " . $_GET['q'] . " -> $path");
323 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
324 if (is_file($path . "/$index")) {
331 if (is_file($path)) {
332 if (preg_match('/.php$/', $path)) {
335 $output = ob_get_contents();
338 $output = file_get_contents($path);
346 function theme_planetlab($content) {