3 // PlanetLab authentication and integration with Drupal
5 // Mark Huang <mlhuang@cs.princeton.edu>
6 // Copyright (C) 2006 The Trustees of Princeton University
12 require_once 'plc_config.php';
13 require_once 'plc_session.php';
14 require_once 'plc_functions.php';
16 drupal_set_html_head('<link href="/planetlab/css/plc_style.css" rel="stylesheet" type="text/css"/>');
18 function planetlab_help($section) {
20 case 'admin/modules#description':
21 return t('Enables authenticated login via a PlanetLab API server.');
25 function planetlab_info($field = 0) {
26 $info['name'] = 'PlanetLab';
35 function planetlab_menu($may_cache) {
40 'path' => 'planetlab/common/logout',
41 'title' => t('Logout 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 //////////////////// formatting helpers - specific to module presentation
59 function ul_start () { return '<ul class="menu">'; }
60 function ul_end () { return '</ul>'; }
61 function leaf($text) { return '<li class="leaf">' . $text . '</li>'; }
62 function expanded($text) { return '<li class="expanded">' . $text . '</li>'; }
63 function p($text) { return '<p>' . $text . '</p>'; }
65 # this should get embedded into a ul_start()/ul_end() pair
66 function plc_my_site_links() {
68 $sites = plc_my_sites();
69 if (count($sites)>1) foreach ($sites as $site) {
70 $html .= leaf( href(l_site($site['site_id']),$site['abbreviated_name']));
72 $html .= leaf( href(l_site(plc_my_site_id()),"My Site"));
78 function plc_my_node_links () {
80 if (count (plc_my_site_ids()) == 1) {
81 $html .= leaf( href (l_nodes_my_site() ,"My Site Nodes"));
83 $html .= leaf( href (l_nodes_all_my_site(),"All My Sites Nodes"));
88 // fake theme to look like menu
89 function menu_theme ($menu) {
91 $result .= ul_start();
92 foreach ($menu as $item) $result .= $item;
97 function planetlab_block($op = 'list', $delta = 0, $edit = array()) {
101 $blocks[0]['info'] = t('PlanetLab login');
105 } else if ($op == 'view') {
108 if ($plc->hide_planetlab_block) {
110 // this is a private hook from the Vicci module to hide the planetlab module
111 } else if (!$plc->person) {
112 // Force login via HTTPS
113 unset($_GET['time']);
114 $form['#action'] = "https://" . $_SERVER['HTTP_HOST'] . url($_GET['q'], drupal_get_destination());
115 $form['#id'] = 'planetlab-login-form';
116 $form['name'] = array('#type' => 'textfield',
117 '#title' => t('E-mail'),
122 $form['pass'] = array('#type' => 'password',
123 '#title' => t('Password'),
127 $form['submit'] = array('#type' => 'submit',
128 '#value' => t('Log in'),
131 $block['subject'] = t('%s login', array('%s' => variable_get('site_name', 'local')));
132 $block['content'] = drupal_get_form('planetlab_login_block', $form, 'planetlab_login');
133 $block['content'] .= p('');
134 $block['content'] .= p( href (l_reset_password(),"Forgot your password?") );
135 $block['content'] .= p( href(l_person_register(),"Create an account") );
136 $block['content'] .= p( href(l_site_register(),"File a site registration") );
138 $block['subject'] = truncate($plc->person['email'],30);
140 //////////////////// Logout
143 // Drupal logout (destroys the session and cleans up $user)
144 // Thierry unclear when this triggers, I suspect this is obsolete
145 $bullet_item .= l(t('Logout of %s',
146 array('%s' => variable_get('site_name', 'local'))), 'logout');
148 // PlanetLab logout (just destroy the session)
149 $bullet_item .= href (l_logout(),'Logout');
151 $bullet_item .= ul_start();
153 if ( $plc->alt_person && $plc->alt_auth) {
154 $email = truncate($plc->person['email'],20);
155 $bullet_item .= leaf( href( l_sulogout(),"Un-become $email"));
157 $bullet_item .= ul_end();
158 $items []= expanded ($bullet_item);
160 //////////////////// accounts
162 $bullet_item .= l_person_t(plc_my_person_id(),"My Account");
163 $bullet_item .= ul_start();
164 if (plc_is_admin() || plc_is_pi())
165 $bullet_item .= leaf( href( l_persons_site(plc_my_site_id()), "My Site Accounts"));
166 if (plc_is_admin()) {
167 $bullet_item .= leaf(href(l_persons_peer('local'),'Local Accounts (slow)'));
168 $bullet_item .= leaf(href(l_persons(),'All Accounts (slow)'));
171 $bullet_item .= ul_end();
172 $items [] = expanded($bullet_item);
174 //////////////////// Sites
176 $bullet_item .= href(l_sites(),"Sites");
177 $bullet_item .= ul_start();
178 $bullet_item .= plc_my_site_links();
179 if ( plc_is_admin() )
180 $bullet_item .= leaf( href(l_sites_pending(),"Pending Requests"));
181 $bullet_item .= ul_end();
182 $items[] = expanded($bullet_item);
184 //////////////////// Nodes
186 $bullet_item .= href(l_nodes(),"Nodes");
187 $bullet_item .= ul_start();
188 $bullet_item .= plc_my_node_links();
189 if (plc_is_admin() || plc_is_pi() || plc_is_tech())
190 $bullet_item .= leaf( href (l_register_node(),"Register Node"));
191 $bullet_item .= ul_end();
192 $items [] = expanded($bullet_item);
194 //////////////////// Slices
196 //if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
197 if (plc_is_admin()) {
198 $bullet_item .= href(l_slices(),"Slices");
200 $bullet_item .= href(l_slices(),"My Slices");
202 $bullet_item .= ul_start();
204 $bullet_item .= leaf ( href(l_slices_my_site(), 'My Site Slices'));
205 if( plc_is_admin() || plc_is_pi() ) {
206 $bullet_item .= leaf(href(l_slice_add(),"Create Slice"));
208 if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
209 $bullet_item .= leaf(href(l_sirius(),"Sirius"));
210 $bullet_item .= ul_end();
211 $items [] = expanded($bullet_item);
213 //////////////////// Admin
214 if( plc_is_admin() || plc_is_pi() ) {
216 $bullet_item .= href(l_admin(),"Admin search");
217 $bullet_item .= ul_start();
218 if ( plc_is_admin() )
219 $bullet_item .= leaf( href (l_node_add(),"Add Node"));
220 $bullet_item .= leaf( href( l_tags(),"Tags"));
221 $bullet_item .= leaf( href( l_nodegroups(),"Node groups"));
222 $bullet_item .= leaf (href(l_peers(),'Peers'));
223 if ( plc_is_admin() )
224 $bullet_item .= leaf (href(l_events(),'Events'));
225 $bullet_item .= ul_end();
226 $items [] = expanded($bullet_item);
230 $bullet_item .= href(l_about(),'About MyPLC');
231 $bullet_item .= ul_start();
232 $bullet_item .= leaf ( href (l_doc_plcapi(),"PLCAPI doc"));
233 $bullet_item .= leaf ( href (l_doc_nmapi(),"NMAPI doc"));
234 $bullet_item .= ul_end();
235 $items[] = expanded($bullet_item);
237 //$block['content'] = theme('list_item', $items);
238 $block['content'] = menu_theme($items);
246 $block['content'] .= ob_get_contents();
254 function planetlab_login_validate($form_id, $form_values) {
257 if ($form_values['name'] && $form_values['pass']) {
258 // Drupal login succeeded
259 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
264 $plc = new PLCSession($form_values['name'], $form_values['pass']);
266 // PlanetLab login failed
268 form_set_error('login', t('Sorry. Unrecognized username or password.'));
269 watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
272 // PlanetLab login succeeded
274 // Login admins to Drupal as the superuser
275 if (in_array('admin', $plc->person['roles'])) {
276 $user = user_load(array('uid' => 1));
282 function planetlab_login_submit($form_id, $form_values) {
285 // Our referring page is encased in a query string of the form
286 // "destination=referrer".
287 parse_str(drupal_get_destination()); // => $destination
289 // The referrer itself is a URL path with the original query string,
290 // e.g. "referer.php?query".
291 extract(parse_url($destination)); // => $query
293 // Which we then have to parse again as a query string.
294 parse_str($query); // => $url
297 // To handle the edge case where this function is called during a
298 // bootstrap, check for the existence of t().
299 if (function_exists('t')) {
300 $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
303 $message = "Session opened for ". check_plain($person['email']);
305 watchdog('planetlab', $message);
308 // Create a timestamped final URL so that browsers don't return the user to
309 // a cached page (where it would appear as if they never logged in or out).
310 return array('time='. time());
312 // Make sure that redirections are always local
313 $url = urldecode($url);
314 if ($url[0] != "/") {
317 header("Location: $url");
323 function planetlab_logout() {
327 // Invalidate PlanetLab session
329 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
332 // Destroy the current session:
335 // The time prevents caching.
336 drupal_goto(NULL, 'time='. time());
339 function planetlab_user($type, &$edit, &$user, $category = NULL) {
344 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
350 function isValidFileName($file) {
352 /* don't allow .. and allow any "word" character \ / */
354 return preg_match('/^(((?:\.)(?!\.))|\w)+$/', $file);
358 function planetlab_page() {
359 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
361 // error_log("Requested " . $_GET['q'] . " -> $path");
364 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
365 if (is_file($path . "/$index")) {
372 if (is_file($path)) {
373 if (preg_match('/.php$/', $path)) {
376 $output = ob_get_contents();
379 if (isValidFileName($path)) {
380 $output = file_get_contents($path);
392 function theme_planetlab($content) {