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';
14 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)
24 $info['name'] = 'PlanetLab';
34 function planetlab_menu($may_cache)
40 'path' => 'planetlab/logout',
41 'title' => t('Log out of %s', array('%s' => variable_get('site_name', 'local'))),
42 'callback' => 'planetlab_logout',
44 'type' => MENU_CALLBACK
48 'title' => variable_get('site_name', 'local'),
49 'callback' => 'planetlab_page',
51 'type' => MENU_CALLBACK
58 function planetlab_block($op = 'list', $delta = 0, $edit = array())
63 $blocks[0]['info'] = t('PlanetLab login');
67 else if ($op == 'view') {
71 // Force login via HTTPS
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'),
81 $form['pass'] = array('#type' => 'password',
82 '#title' => t('Password'),
86 $form['submit'] = array('#type' => 'submit',
87 '#value' => t('Log in'),
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>';
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']);
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>';
107 $site_item .= "<li><a href='/db/sites/join_request.php'>Join Requests</a></li>";
109 $site_item .= "<li><a href='/db/sites/peers.php'>Migration Status</a></li>";
110 $site_item .= "</ul>";
111 $items[] = $site_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>';
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>";
125 $user_item .= '</ul>';
126 $items [] = $user_item;
129 $node_item .= '<a href="/db/nodes/">Nodes</a>';
130 $node_item .= '<ul>';
131 $node_item .= '<li><a href="/db/nodes/index.php?site_id='. $plc->person['site_ids'][0] .'">My Site Nodes</a></li>';
132 $node_item .= "<li><a href='/db/nodes/add_node.php'>Add Node</a></li>";
134 $node_item .= "<li> <a href='/db/nodes/settings.php'> Setting Types </a> </li>";
135 $node_item .= '</ul>';
136 $items [] = $node_item;
139 //if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) )
140 $slice_item .= '<a href="/db/slices/">Slices</a>';
141 $slice_item .= '<ul>';
142 if( $is_admin || $is_pi ) {
143 $slice_item .= "<li><a href='/db/slices/add_slice.php'>Create Slice</a></li>";
145 if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) )
146 $slice_item .= '<li><a href="/db/sirius/index.php">Sirius</a></li>';
147 $slice_item .= '</ul>';
148 $items [] = $slice_item;
150 if( $is_admin || $is_pi ) {
152 $tag_item .= '<a href="/db/slices/tags.php?type=all">Tags</a>';
154 $tag_item .= "<li><a href='/db/slices/tags.php?type=node'>Node Tags</a></li>";
155 $tag_item .= "<li><a href='/db/slices/tags.php?type=interface'>Interface Tags</a></li>";
156 $tag_item .= "<li><a href='/db/slices/tags.php?type=slice'>Slice Tags</a></li>";
157 $tag_item .= '</ul>';
158 $items [] = $tag_item;
163 $items[] = l(t('Peers'),'db/peers/');
166 $items[] = l(t('Events'),'db/events/');
168 $items[] = l(t('About'),'db/about.php');
171 // Drupal logout (destroys the session and cleans up $user)
172 $items[] = l(t('Log out of %s', array('%s' => variable_get('site_name', 'local'))), 'logout');
174 // PlanetLab logout (just destroy the session)
175 $items[] = l(t('Log out'), 'planetlab/logout');
178 $block['content'] = theme('item_list', $items);
186 $block['content'] .= ob_get_contents();
194 function planetlab_login_validate($form_id, $form_values)
198 if ($form_values['name'] && $form_values['pass']) {
199 // Drupal login succeeded
200 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
205 $plc = new PLCSession($form_values['name'], $form_values['pass']);
207 // PlanetLab login failed
209 form_set_error('login', t('Sorry. Unrecognized username or password.'));
210 watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
213 // PlanetLab login succeeded
215 // Login admins to Drupal as the superuser
216 if (in_array('admin', $plc->person['roles'])) {
217 $user = user_load(array('uid' => 1));
223 function planetlab_login_submit($form_id, $form_values)
227 // Our referring page is encased in a query string of the form
228 // "destination=referrer".
229 parse_str(drupal_get_destination()); // => $destination
231 // The referrer itself is a URL path with the original query string,
232 // e.g. "referer.php?query".
233 extract(parse_url($destination)); // => $query
235 // Which we then have to parse again as a query string.
236 parse_str($query); // => $url
239 // To handle the edge case where this function is called during a
240 // bootstrap, check for the existence of t().
241 if (function_exists('t')) {
242 $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
245 $message = "Session opened for ". check_plain($person['email']);
247 watchdog('planetlab', $message);
250 // Create a timestamped final URL so that browsers don't return the user to
251 // a cached page (where it would appear as if they never logged in or out).
252 return array('time='. time());
254 // Make sure that redirections are always local
255 $url = urldecode($url);
256 if ($url[0] != "/") {
259 Header("Location: $url");
265 function planetlab_logout()
270 // Invalidate PlanetLab session
272 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
275 // Destroy the current session:
278 // The time prevents caching.
279 drupal_goto(NULL, 'time='. time());
282 function planetlab_user($type, &$edit, &$user, $category = NULL)
288 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
294 function planetlab_page()
296 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
298 // error_log("Requested " . $_GET['q'] . " -> $path");
301 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
302 if (is_file($path . "/$index")) {
309 if (is_file($path)) {
310 if (preg_match('/.php$/', $path)) {
313 $output = ob_get_contents();
316 $output = file_get_contents($path);
324 function theme_planetlab($content)