3 // PlanetLab authentication and integration with Drupal
5 // Mark Huang <mlhuang@cs.princeton.edu>
6 // Copyright (C) 2006 The Trustees of Princeton University
13 require_once 'plc_config.php';
14 require_once 'plc_session.php';
15 require_once 'plc_functions.php';
17 drupal_set_html_head('<link href="/planetlab/css/plc_style.css" rel="stylesheet" type="text/css"/>');
19 function planetlab_help($section) {
21 case 'admin/modules#description':
22 return t('Enables authenticated login via a PlanetLab API server.');
26 function planetlab_info($field = 0) {
27 $info['name'] = 'PlanetLab';
36 function planetlab_menu($may_cache) {
41 'path' => 'planetlab/common/logout',
42 'title' => t('Logout of %s', array('%s' => variable_get('site_name', 'local'))),
43 'callback' => 'planetlab_logout',
45 'type' => MENU_CALLBACK
49 'title' => variable_get('site_name', 'local'),
50 'callback' => 'planetlab_page',
52 'type' => MENU_CALLBACK
59 //////////////////// formatting helpers - specific to module presentation
60 function ul_start () { return '<ul class="menu">'; }
61 function ul_end () { return '</ul>'; }
62 function leaf($text) { return '<li class="leaf">' . $text . '</li>'; }
63 function expanded($text) { return '<li class="expanded">' . $text . '</li>'; }
64 function p($text) { return '<p>' . $text . '</p>'; }
66 # this should get embedded into a ul_start()/ul_end() pair
67 function plc_my_site_links() {
69 $sites = plc_my_sites();
70 if (count($sites)>1) foreach ($sites as $site) {
71 $html .= leaf( href(l_site($site['site_id']),$site['abbreviated_name']));
73 $html .= leaf( href(l_site(plc_my_site_id()),"My Site"));
79 function plc_my_node_links () {
81 if (count (plc_my_site_ids()) == 1) {
82 $html .= leaf( href (l_nodes_my_site() ,"My Site Nodes"));
84 $html .= leaf( href (l_nodes_all_my_site(),"All My Sites Nodes"));
89 // fake theme to look like menu
90 function menu_theme ($menu) {
92 $result .= ul_start();
93 foreach ($menu as $item) $result .= $item;
98 function planetlab_block($op = 'list', $delta = 0, $edit = array()) {
102 $blocks[0]['info'] = t('PlanetLab login');
106 } else if ($op == 'view') {
109 if (property_exists($plc, 'hide_planetlab_block')) {
111 // this is a private hook from the Vicci module to hide the planetlab module
112 } else if (!$plc->person) {
113 // Force login via HTTPS
114 unset($_GET['time']);
115 $form['#action'] = "https://" . $_SERVER['HTTP_HOST'] . url($_GET['q'], drupal_get_destination());
116 $form['#id'] = 'planetlab-login-form';
117 $form['name'] = array('#type' => 'textfield',
118 '#title' => t('E-mail'),
123 $form['pass'] = array('#type' => 'password',
124 '#title' => t('Password'),
128 $form['submit'] = array('#type' => 'submit',
129 '#value' => t('Log in'),
132 $block['subject'] = t('%s login', array('%s' => variable_get('site_name', 'local')));
133 $block['content'] = drupal_get_form('planetlab_login_block', $form, 'planetlab_login');
134 $block['content'] .= p('');
135 $block['content'] .= p( href (l_reset_password(),"Forgot your password?") );
136 $block['content'] .= p( href(l_person_register(),"Create an account") );
137 $block['content'] .= p( href(l_site_register(),"File a site registration") );
139 $block['subject'] = truncate($plc->person['email'],30);
141 //////////////////// Logout
144 // Drupal logout (destroys the session and cleans up $user)
145 // Thierry unclear when this triggers, I suspect this is obsolete
146 $bullet_item .= l(t('Logout of %s',
147 array('%s' => variable_get('site_name', 'local'))), 'logout');
149 // PlanetLab logout (just destroy the session)
150 $bullet_item .= href (l_logout(),'Logout');
152 $bullet_item .= ul_start();
154 if ( $plc->alt_person && $plc->alt_auth) {
155 $email = truncate($plc->person['email'],20);
156 $bullet_item .= leaf( href( l_sulogout(),"Un-become $email"));
158 $bullet_item .= ul_end();
159 $items []= expanded ($bullet_item);
161 //////////////////// accounts
163 $bullet_item .= l_person_t(plc_my_person_id(),"My Account");
164 $bullet_item .= ul_start();
165 if (plc_is_admin() || plc_is_pi())
166 $bullet_item .= leaf( href( l_persons_site(plc_my_site_id()), "My Site Accounts"));
167 if (plc_is_admin()) {
168 $bullet_item .= leaf(href(l_persons_peer('local'),'Local Accounts (slow)'));
169 $bullet_item .= leaf(href(l_persons(),'All Accounts (slow)'));
172 $bullet_item .= ul_end();
173 $items [] = expanded($bullet_item);
175 //////////////////// Sites
177 $bullet_item .= href(l_sites(),"Sites");
178 $bullet_item .= ul_start();
179 $bullet_item .= plc_my_site_links();
180 if ( plc_is_admin() )
181 $bullet_item .= leaf( href(l_sites_pending(),"Pending Requests"));
182 $bullet_item .= ul_end();
183 $items[] = expanded($bullet_item);
185 //////////////////// Nodes
187 $bullet_item .= href(l_nodes(),"Nodes");
188 $bullet_item .= ul_start();
189 $bullet_item .= plc_my_node_links();
190 if (plc_is_admin() || plc_is_pi() || plc_is_tech()) {
191 $bullet_item .= leaf( href (l_register_node(), "Register Node (ko)"));
193 $bullet_item .= ul_end();
194 $items [] = expanded($bullet_item);
196 //////////////////// Slices
198 //if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
199 if (plc_is_admin()) {
200 $bullet_item .= href(l_slices(),"Slices");
202 $bullet_item .= href(l_slices(),"My Slices");
204 $bullet_item .= ul_start();
206 $bullet_item .= leaf ( href(l_slices_my_site(), 'My Site Slices'));
207 if( plc_is_admin() || plc_is_pi() ) {
208 $bullet_item .= leaf(href(l_slice_add(),"Create Slice"));
210 if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
211 $bullet_item .= leaf(href(l_sirius(),"Sirius"));
212 $bullet_item .= ul_end();
213 $items [] = expanded($bullet_item);
215 //////////////////// Admin
216 if( plc_is_admin() || plc_is_pi() ) {
218 $bullet_item .= href(l_admin(),"Admin search");
219 $bullet_item .= ul_start();
220 if ( plc_is_admin() )
221 $bullet_item .= leaf( href (l_node_add(),"Add Node"));
222 $bullet_item .= leaf( href( l_tags(),"Tags"));
223 $bullet_item .= leaf( href( l_nodegroups(),"Node groups"));
224 $bullet_item .= leaf (href(l_peers(),'Peers'));
225 if ( plc_is_admin() )
226 $bullet_item .= leaf (href(l_events(),'Events'));
227 $bullet_item .= ul_end();
228 $items [] = expanded($bullet_item);
232 $bullet_item .= href(l_about(),'About MyPLC');
233 $bullet_item .= ul_start();
234 $bullet_item .= leaf ( href (l_doc_plcapi(),"PLCAPI doc"));
235 $bullet_item .= leaf ( href (l_doc_nmapi(),"NMAPI doc"));
236 $bullet_item .= ul_end();
237 $items[] = expanded($bullet_item);
239 //$block['content'] = theme('list_item', $items);
240 $block['content'] = menu_theme($items);
248 $block['content'] .= ob_get_contents();
256 function planetlab_login_validate($form_id, $form_values) {
259 if ($form_values['name'] && $form_values['pass']) {
260 // Drupal login succeeded
261 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
266 $plc = new PLCSession($form_values['name'], $form_values['pass']);
268 // PlanetLab login failed
270 form_set_error('login', t('Sorry. Unrecognized username or password.'));
271 watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
274 // PlanetLab login succeeded
276 // Login admins to Drupal as the superuser
277 if (in_array('admin', $plc->person['roles'])) {
278 $user = user_load(array('uid' => 1));
284 function planetlab_login_submit($form_id, $form_values) {
287 // Our referring page is encased in a query string of the form
288 // "destination=referrer".
289 parse_str(drupal_get_destination(), $variables);
290 $destination = $variables['destination'];
292 // The referrer itself is a URL path with the original query string,
293 // e.g. "referer.php?query".
294 extract(parse_url($destination)); // => $query
296 // Which we then have to parse again as a query string.
297 parse_str($query, $variables);
298 $url = $variables['url'];
301 // To handle the edge case where this function is called during a
302 // bootstrap, check for the existence of t().
303 if (function_exists('t')) {
304 $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
307 $message = "Session opened for ". check_plain($person['email']);
309 watchdog('planetlab', $message);
312 // Create a timestamped final URL so that browsers don't return the user to
313 // a cached page (where it would appear as if they never logged in or out).
314 return array('time='. time());
316 // Make sure that redirections are always local
317 $url = urldecode($url);
318 if ($url[0] != "/") {
321 header("Location: $url");
327 function planetlab_logout() {
331 // Invalidate PlanetLab session
333 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
336 // Destroy the current session:
339 // The time prevents caching.
340 drupal_goto(NULL, 'time='. time());
343 function planetlab_user($type, &$edit, &$user, $category = NULL) {
348 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
354 function isValidFileName($file) {
356 /* don't allow .. and allow any "word" character \ / */
358 return preg_match('/^(((?:\.)(?!\.))|\w)+$/', $file);
362 function planetlab_page() {
363 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
365 // error_log("Requested " . $_GET['q'] . " -> $path");
368 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
369 if (is_file($path . "/$index")) {
376 if (is_file($path)) {
377 if (preg_match('/.php$/', $path)) {
380 $output = ob_get_contents();
383 if (isValidFileName($path)) {
384 $output = file_get_contents($path);
396 function theme_planetlab($content) {