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 function planetlab_help($section) {
17 case 'admin/modules#description':
18 return t('Enables authenticated login via a PlanetLab API server.');
22 function planetlab_info($field = 0) {
23 $info['name'] = 'PlanetLab';
32 function planetlab_menu($may_cache) {
37 'path' => 'planetlab/logout',
38 'title' => t('Log out of %s', array('%s' => variable_get('site_name', 'local'))),
39 'callback' => 'planetlab_logout',
41 'type' => MENU_CALLBACK
45 'title' => variable_get('site_name', 'local'),
46 'callback' => 'planetlab_page',
48 'type' => MENU_CALLBACK
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) {
64 $result .= ul_start();
65 foreach ($menu as $item) $result .= $item;
70 function planetlab_block($op = 'list', $delta = 0, $edit = array()) {
74 $blocks[0]['info'] = t('PlanetLab login');
78 } else if ($op == 'view') {
82 // Force login via HTTPS
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'),
92 $form['pass'] = array('#type' => 'password',
93 '#title' => t('Password'),
97 $form['submit'] = array('#type' => 'submit',
98 '#value' => t('Log in'),
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") );
108 $block['subject'] = truncate($plc->person['email'],30);
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']);
114 //////////////////// Log out
116 // Drupal logout (destroys the session and cleans up $user)
117 // Thierry unclear when this triggers, I suspect this is obsolete
118 $items[] = leaf (l(t('Log out of %s',
119 array('%s' => variable_get('site_name', 'local'))), 'logout'));
121 // PlanetLab logout (just destroy the session)
122 $items[] = leaf ( href (l_logout(),'Log out'));
125 //////////////////// Sites
127 $site_item .= href(l_sites(),"Sites");
128 $site_item .= ul_start();
129 $site_item .= leaf( href(l_site(plc_my_site_id()),"My site"));
131 $site_item .= leaf( href(l_sites_pending(),"Pending Requests"));
132 $site_item .= ul_end();
133 $items[] = expanded($site_item);
135 //////////////////// Users
137 $user_item .= href(l_persons(),'Users');
138 $user_item .= ul_start();
139 $user_item .= leaf( l_person_t(plc_my_person_id(),"My account"));
141 $user_item .= leaf( href( l_persons_site(plc_my_site_id()), "My users"));
142 if ( $plc->alt_person && $plc->alt_auth) {
143 $email = truncate($plc->person['email'],20);
144 $user_item .= leaf( href( l_sulogout(),"Log out of $email"));
146 $user_item .= ul_end();
147 $items [] = expanded($user_item);
149 //////////////////// Nodes
151 $node_item .= href(l_nodes(),"Nodes");
152 $node_item .= ul_start();
153 $node_item .= leaf( href (l_nodes_site (plc_my_site_id()),"My Site Nodes"));
154 $node_item .= leaf( href (l_node_add(),"Add Node"));
155 $node_item .= ul_end();
156 $items [] = expanded($node_item);
158 //////////////////// Slices
160 //if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) )
161 $slice_item .= href(l_slices(),"Slices");
162 $slice_item .= ul_start();
163 if( $is_admin || $is_pi ) {
164 $slice_item .= leaf(href(l_slice_add(),"Create Slice"));
166 if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) )
167 $slice_item .= leaf(href(l_sirius(),"Sirius"));
168 $slice_item .= ul_end();
169 $items [] = expanded($slice_item);
171 //////////////////// Admin
172 if( $is_admin || $is_pi ) {
174 $admin_item .= href(l_admin(),"Admin");
175 $admin_item .= ul_start();
176 $admin_item .= leaf( href( l_tags(),"Tags"));
177 $admin_item .= leaf( href( l_nodegroups(),"Node groups"));
178 $admin_item .= leaf (href(l_peers(),'Peers'));
180 $admin_item .= leaf (href(l_events(),'Events'));
181 $admin_item .= ul_end();
182 $items [] = expanded($admin_item);
186 $doc_item .= href(l_about(),'About MyPLC');
187 $doc_item .= ul_start();
188 $doc_item .= leaf ( href (l_doc_plcapi(),"PLCAPI doc"));
189 $doc_item .= leaf ( href (l_doc_nmapi(),"NMAPI doc"));
190 $doc_item .= ul_end();
191 $items[] = expanded($doc_item);
193 //$block['content'] = theme('list_item', $items);
194 $block['content'] = menu_theme($items);
202 $block['content'] .= ob_get_contents();
210 function planetlab_login_validate($form_id, $form_values) {
213 if ($form_values['name'] && $form_values['pass']) {
214 // Drupal login succeeded
215 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
220 $plc = new PLCSession($form_values['name'], $form_values['pass']);
222 // PlanetLab login failed
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']))));
228 // PlanetLab login succeeded
230 // Login admins to Drupal as the superuser
231 if (in_array('admin', $plc->person['roles'])) {
232 $user = user_load(array('uid' => 1));
238 function planetlab_login_submit($form_id, $form_values) {
241 // Our referring page is encased in a query string of the form
242 // "destination=referrer".
243 parse_str(drupal_get_destination()); // => $destination
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
249 // Which we then have to parse again as a query string.
250 parse_str($query); // => $url
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'])));
259 $message = "Session opened for ". check_plain($person['email']);
261 watchdog('planetlab', $message);
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());
268 // Make sure that redirections are always local
269 $url = urldecode($url);
270 if ($url[0] != "/") {
273 header("Location: $url");
279 function planetlab_logout() {
283 // Invalidate PlanetLab session
285 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
288 // Destroy the current session:
291 // The time prevents caching.
292 drupal_goto(NULL, 'time='. time());
295 function planetlab_user($type, &$edit, &$user, $category = NULL) {
300 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
306 function planetlab_page() {
307 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
309 // error_log("Requested " . $_GET['q'] . " -> $path");
312 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
313 if (is_file($path . "/$index")) {
320 if (is_file($path)) {
321 if (preg_match('/.php$/', $path)) {
324 $output = ob_get_contents();
327 $output = file_get_contents($path);
335 function theme_planetlab($content) {