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
57 //////////////////// formatting helpers - specific to module presentation
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>'; }
64 # this should get embedded into a ul_start()/ul_end() pair
65 function plc_my_site_links() {
67 $sites = plc_my_sites();
68 if (count($sites)>1) foreach ($sites as $site) {
69 $html .= leaf( href(l_site($site['site_id']),$site['abbreviated_name']));
71 $html .= leaf( href(l_site(plc_my_site_id()),"My Site"));
77 function plc_my_node_links () {
79 if (count (plc_my_site_ids()) == 1) {
80 $html .= leaf( href (l_nodes_my_site() ,"My Site Nodes"));
82 $html .= leaf( href (l_nodes_all_my_site(),"All My Sites Nodes"));
87 // fake theme to look like menu
88 function menu_theme ($menu) {
90 $result .= ul_start();
91 foreach ($menu as $item) $result .= $item;
96 function planetlab_block($op = 'list', $delta = 0, $edit = array()) {
100 $blocks[0]['info'] = t('PlanetLab login');
104 } else if ($op == 'view') {
108 // Force login via HTTPS
109 unset($_GET['time']);
110 $form['#action'] = "https://" . $_SERVER['HTTP_HOST'] . url($_GET['q'], drupal_get_destination());
111 $form['#id'] = 'planetlab-login-form';
112 $form['name'] = array('#type' => 'textfield',
113 '#title' => t('E-mail'),
118 $form['pass'] = array('#type' => 'password',
119 '#title' => t('Password'),
123 $form['submit'] = array('#type' => 'submit',
124 '#value' => t('Log in'),
127 $block['subject'] = t('%s login', array('%s' => variable_get('site_name', 'local')));
128 $block['content'] = drupal_get_form('planetlab_login_block', $form, 'planetlab_login');
129 $block['content'] .= p('');
130 $block['content'] .= p( href (l_reset_password(),"Forgot your password?") );
131 $block['content'] .= p( href(l_person_register(),"Create an account") );
132 $block['content'] .= p( href(l_site_register(),"File a site registration") );
134 $block['subject'] = truncate($plc->person['email'],30);
136 //////////////////// Logout
139 // Drupal logout (destroys the session and cleans up $user)
140 // Thierry unclear when this triggers, I suspect this is obsolete
141 $bullet_item .= l(t('Logout of %s',
142 array('%s' => variable_get('site_name', 'local'))), 'logout');
144 // PlanetLab logout (just destroy the session)
145 $bullet_item .= href (l_logout(),'Logout');
147 $bullet_item .= ul_start();
149 if ( $plc->alt_person && $plc->alt_auth) {
150 $email = truncate($plc->person['email'],20);
151 $bullet_item .= leaf( href( l_sulogout(),"Un-become $email"));
153 $bullet_item .= ul_end();
154 $items []= expanded ($bullet_item);
156 //////////////////// accounts
158 $bullet_item .= l_person_t(plc_my_person_id(),"My Account");
159 $bullet_item .= ul_start();
160 if (plc_is_admin() || plc_is_pi())
161 $bullet_item .= leaf( href( l_persons_site(plc_my_site_id()), "My Site Accounts"));
162 if (plc_is_admin()) {
163 $bullet_item .= leaf(href(l_persons_peer('local'),'Local Accounts (slow)'));
164 $bullet_item .= leaf(href(l_persons(),'All Accounts (slow)'));
167 $bullet_item .= ul_end();
168 $items [] = expanded($bullet_item);
170 //////////////////// Sites
172 $bullet_item .= href(l_sites(),"Sites");
173 $bullet_item .= ul_start();
174 $bullet_item .= plc_my_site_links();
175 if ( plc_is_admin() )
176 $bullet_item .= leaf( href(l_sites_pending(),"Pending Requests"));
177 $bullet_item .= ul_end();
178 $items[] = expanded($bullet_item);
180 //////////////////// Nodes
182 $bullet_item .= href(l_nodes(),"Nodes");
183 $bullet_item .= ul_start();
184 $bullet_item .= plc_my_node_links();
185 if (plc_is_admin() || plc_is_pi() || plc_is_tech())
186 $bullet_item .= leaf( href (l_register_node(),"Register Node"));
187 $bullet_item .= ul_end();
188 $items [] = expanded($bullet_item);
190 //////////////////// Slices
192 //if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
193 if (plc_is_admin()) {
194 $bullet_item .= href(l_slices(),"Slices");
196 $bullet_item .= href(l_slices(),"My Slices");
198 $bullet_item .= ul_start();
200 $bullet_item .= leaf ( href(l_slices_my_site(), 'My Site Slices'));
201 if( plc_is_admin() || plc_is_pi() ) {
202 $bullet_item .= leaf(href(l_slice_add(),"Create Slice"));
204 if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
205 $bullet_item .= leaf(href(l_sirius(),"Sirius"));
206 $bullet_item .= ul_end();
207 $items [] = expanded($bullet_item);
209 //////////////////// Admin
210 if( plc_is_admin() || plc_is_pi() ) {
212 $bullet_item .= href(l_admin(),"Admin search");
213 $bullet_item .= ul_start();
214 if ( plc_is_admin() )
215 $bullet_item .= leaf( href (l_node_add(),"Add Node"));
216 $bullet_item .= leaf( href( l_tags(),"Tags"));
217 $bullet_item .= leaf( href( l_nodegroups(),"Node groups"));
218 $bullet_item .= leaf (href(l_peers(),'Peers'));
219 if ( plc_is_admin() )
220 $bullet_item .= leaf (href(l_events(),'Events'));
221 $bullet_item .= ul_end();
222 $items [] = expanded($bullet_item);
226 $bullet_item .= href(l_about(),'About MyPLC');
227 $bullet_item .= ul_start();
228 $bullet_item .= leaf ( href (l_doc_plcapi(),"PLCAPI doc"));
229 $bullet_item .= leaf ( href (l_doc_nmapi(),"NMAPI doc"));
230 $bullet_item .= ul_end();
231 $items[] = expanded($bullet_item);
233 //$block['content'] = theme('list_item', $items);
234 $block['content'] = menu_theme($items);
242 $block['content'] .= ob_get_contents();
250 function planetlab_login_validate($form_id, $form_values) {
253 if ($form_values['name'] && $form_values['pass']) {
254 // Drupal login succeeded
255 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
260 $plc = new PLCSession($form_values['name'], $form_values['pass']);
262 // PlanetLab login failed
264 form_set_error('login', t('Sorry. Unrecognized username or password.'));
265 watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
268 // PlanetLab login succeeded
270 // Login admins to Drupal as the superuser
271 if (in_array('admin', $plc->person['roles'])) {
272 $user = user_load(array('uid' => 1));
278 function planetlab_login_submit($form_id, $form_values) {
281 // Our referring page is encased in a query string of the form
282 // "destination=referrer".
283 parse_str(drupal_get_destination()); // => $destination
285 // The referrer itself is a URL path with the original query string,
286 // e.g. "referer.php?query".
287 extract(parse_url($destination)); // => $query
289 // Which we then have to parse again as a query string.
290 parse_str($query); // => $url
293 // To handle the edge case where this function is called during a
294 // bootstrap, check for the existence of t().
295 if (function_exists('t')) {
296 $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
299 $message = "Session opened for ". check_plain($person['email']);
301 watchdog('planetlab', $message);
304 // Create a timestamped final URL so that browsers don't return the user to
305 // a cached page (where it would appear as if they never logged in or out).
306 return array('time='. time());
308 // Make sure that redirections are always local
309 $url = urldecode($url);
310 if ($url[0] != "/") {
313 header("Location: $url");
319 function planetlab_logout() {
323 // Invalidate PlanetLab session
325 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
328 // Destroy the current session:
331 // The time prevents caching.
332 drupal_goto(NULL, 'time='. time());
335 function planetlab_user($type, &$edit, &$user, $category = NULL) {
340 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
346 function planetlab_page() {
347 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
349 // error_log("Requested " . $_GET['q'] . " -> $path");
352 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
353 if (is_file($path . "/$index")) {
360 if (is_file($path)) {
361 if (preg_match('/.php$/', $path)) {
364 $output = ob_get_contents();
367 $output = file_get_contents($path);
375 function theme_planetlab($content) {