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);
111 $is_admin = in_array(10,$plc->person['role_ids']);
112 $is_pi = in_array(20,$plc->person['role_ids']);
113 $is_user = in_array(30,$plc->person['role_ids']);
114 $is_tech = in_array(40,$plc->person['role_ids']);
116 //////////////////// Logout
118 // Drupal logout (destroys the session and cleans up $user)
119 // Thierry unclear when this triggers, I suspect this is obsolete
120 $items[] = leaf (l(t('Logout of %s',
121 array('%s' => variable_get('site_name', 'local'))), 'logout'));
123 // PlanetLab logout (just destroy the session)
124 $items[] = leaf ( href (l_logout(),'Logout'));
127 //////////////////// Sites
129 $site_item .= href(l_sites(),"Sites");
130 $site_item .= ul_start();
131 #$site_item .= leaf( href(l_site(plc_my_site_id()),"My Site"));
132 $site_item .= plc_my_site_links(); # leaf( href(l_site(plc_my_site_id()),"My Site"));
134 $site_item .= leaf( href(l_sites_pending(),"Pending Requests"));
135 $site_item .= ul_end();
136 $items[] = expanded($site_item);
138 //////////////////// Users
140 $user_item .= href(l_persons(),'Users');
141 $user_item .= ul_start();
142 $user_item .= leaf( l_person_t(plc_my_person_id(),"My Account"));
144 $user_item .= leaf( href( l_persons_site(plc_my_site_id()), "My Users"));
145 if ( $plc->alt_person && $plc->alt_auth) {
146 $email = truncate($plc->person['email'],20);
147 $user_item .= leaf( href( l_sulogout(),"Logout of $email"));
149 $user_item .= ul_end();
150 $items [] = expanded($user_item);
152 //////////////////// Nodes
154 $node_item .= href(l_nodes(),"Nodes");
155 $node_item .= ul_start();
156 $node_item .= leaf( href (l_nodes_site (plc_my_site_id()),"My Site Nodes"));
157 $node_item .= leaf( href (l_node_add(),"Add Node"));
158 $node_item .= ul_end();
159 $items [] = expanded($node_item);
161 //////////////////// Slices
163 //if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) )
164 $slice_item .= href(l_slices(),"Slices");
165 $slice_item .= ul_start();
166 if( $is_admin || $is_pi ) {
167 $slice_item .= leaf(href(l_slice_add(),"Create Slice"));
169 if( !( $is_tech && ! $is_user && ! $is_pi && ! $is_admin ) )
170 $slice_item .= leaf(href(l_sirius(),"Sirius"));
171 $slice_item .= ul_end();
172 $items [] = expanded($slice_item);
174 //////////////////// Admin
175 if( plc_is_admin() || plc_is_pi() ) {
177 $bullet_item .= href(l_admin(),"Admin search");
178 $bullet_item .= ul_start();
179 $bullet_item .= leaf( href (l_node_add(),"Add Node"));
180 $bullet_item .= leaf( href( l_tags(),"Tags"));
181 $bullet_item .= leaf( href( l_nodegroups(),"Node groups"));
182 $bullet_item .= leaf (href(l_peers(),'Peers'));
183 if ( plc_is_admin() )
184 $bullet_item .= leaf (href(l_events(),'Events'));
185 $bullet_item .= ul_end();
186 $items [] = expanded($bullet_item);
190 $doc_item .= href(l_about(),'About MyPLC');
191 $doc_item .= ul_start();
192 $doc_item .= leaf ( href (l_doc_plcapi(),"PLCAPI doc"));
193 $doc_item .= leaf ( href (l_doc_nmapi(),"NMAPI doc"));
194 $doc_item .= ul_end();
195 $items[] = expanded($doc_item);
197 //$block['content'] = theme('list_item', $items);
198 $block['content'] = menu_theme($items);
206 $block['content'] .= ob_get_contents();
214 function planetlab_login_validate($form_id, $form_values) {
217 if ($form_values['name'] && $form_values['pass']) {
218 // Drupal login succeeded
219 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
224 $plc = new PLCSession($form_values['name'], $form_values['pass']);
226 // PlanetLab login failed
228 form_set_error('login', t('Sorry. Unrecognized username or password.'));
229 watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
232 // PlanetLab login succeeded
234 // Login admins to Drupal as the superuser
235 if (in_array('admin', $plc->person['roles'])) {
236 $user = user_load(array('uid' => 1));
242 function planetlab_login_submit($form_id, $form_values) {
245 // Our referring page is encased in a query string of the form
246 // "destination=referrer".
247 parse_str(drupal_get_destination()); // => $destination
249 // The referrer itself is a URL path with the original query string,
250 // e.g. "referer.php?query".
251 extract(parse_url($destination)); // => $query
253 // Which we then have to parse again as a query string.
254 parse_str($query); // => $url
257 // To handle the edge case where this function is called during a
258 // bootstrap, check for the existence of t().
259 if (function_exists('t')) {
260 $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
263 $message = "Session opened for ". check_plain($person['email']);
265 watchdog('planetlab', $message);
268 // Create a timestamped final URL so that browsers don't return the user to
269 // a cached page (where it would appear as if they never logged in or out).
270 return array('time='. time());
272 // Make sure that redirections are always local
273 $url = urldecode($url);
274 if ($url[0] != "/") {
277 header("Location: $url");
283 function planetlab_logout() {
287 // Invalidate PlanetLab session
289 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
292 // Destroy the current session:
295 // The time prevents caching.
296 drupal_goto(NULL, 'time='. time());
299 function planetlab_user($type, &$edit, &$user, $category = NULL) {
304 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
310 function planetlab_page() {
311 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
313 // error_log("Requested " . $_GET['q'] . " -> $path");
316 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
317 if (is_file($path . "/$index")) {
324 if (is_file($path)) {
325 if (preg_match('/.php$/', $path)) {
328 $output = ob_get_contents();
331 $output = file_get_contents($path);
339 function theme_planetlab($content) {