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 ($plc->hide_planetlab_block) {
112 // this is a private hook from the Vicci module to hide the planetlab module
113 } else if (!$plc->person) {
114 // Force login via HTTPS
115 unset($_GET['time']);
116 $form['#action'] = "https://" . $_SERVER['HTTP_HOST'] . url($_GET['q'], drupal_get_destination());
117 $form['#id'] = 'planetlab-login-form';
118 $form['name'] = array('#type' => 'textfield',
119 '#title' => t('E-mail'),
124 $form['pass'] = array('#type' => 'password',
125 '#title' => t('Password'),
129 $form['submit'] = array('#type' => 'submit',
130 '#value' => t('Log in'),
133 $block['subject'] = t('%s login', array('%s' => variable_get('site_name', 'local')));
134 $block['content'] = drupal_get_form('planetlab_login_block', $form, 'planetlab_login');
135 $block['content'] .= p('');
136 $block['content'] .= p( href (l_reset_password(),"Forgot your password?") );
137 $block['content'] .= p( href(l_person_register(),"Create an account") );
138 $block['content'] .= p( href(l_site_register(),"File a site registration") );
140 $block['subject'] = truncate($plc->person['email'],30);
142 //////////////////// Logout
145 // Drupal logout (destroys the session and cleans up $user)
146 // Thierry unclear when this triggers, I suspect this is obsolete
147 $bullet_item .= l(t('Logout of %s',
148 array('%s' => variable_get('site_name', 'local'))), 'logout');
150 // PlanetLab logout (just destroy the session)
151 $bullet_item .= href (l_logout(),'Logout');
153 $bullet_item .= ul_start();
155 if ( $plc->alt_person && $plc->alt_auth) {
156 $email = truncate($plc->person['email'],20);
157 $bullet_item .= leaf( href( l_sulogout(),"Un-become $email"));
159 $bullet_item .= ul_end();
160 $items []= expanded ($bullet_item);
162 //////////////////// accounts
164 $bullet_item .= l_person_t(plc_my_person_id(),"My Account");
165 $bullet_item .= ul_start();
166 if (plc_is_admin() || plc_is_pi())
167 $bullet_item .= leaf( href( l_persons_site(plc_my_site_id()), "My Site Accounts"));
168 if (plc_is_admin()) {
169 $bullet_item .= leaf(href(l_persons_peer('local'),'Local Accounts (slow)'));
170 $bullet_item .= leaf(href(l_persons(),'All Accounts (slow)'));
173 $bullet_item .= ul_end();
174 $items [] = expanded($bullet_item);
176 //////////////////// Sites
178 $bullet_item .= href(l_sites(),"Sites");
179 $bullet_item .= ul_start();
180 $bullet_item .= plc_my_site_links();
181 if ( plc_is_admin() )
182 $bullet_item .= leaf( href(l_sites_pending(),"Pending Requests"));
183 $bullet_item .= ul_end();
184 $items[] = expanded($bullet_item);
186 //////////////////// Nodes
188 $bullet_item .= href(l_nodes(),"Nodes");
189 $bullet_item .= ul_start();
190 $bullet_item .= plc_my_node_links();
191 if (plc_is_admin() || plc_is_pi() || plc_is_tech()) {
192 $bullet_item .= leaf( href (l_register_node(), "Register Node (ko)"));
194 $bullet_item .= ul_end();
195 $items [] = expanded($bullet_item);
197 //////////////////// Slices
199 //if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
200 if (plc_is_admin()) {
201 $bullet_item .= href(l_slices(),"Slices");
203 $bullet_item .= href(l_slices(),"My Slices");
205 $bullet_item .= ul_start();
207 $bullet_item .= leaf ( href(l_slices_my_site(), 'My Site Slices'));
208 if( plc_is_admin() || plc_is_pi() ) {
209 $bullet_item .= leaf(href(l_slice_add(),"Create Slice"));
211 if( !( plc_is_tech() && ! plc_is_user() && ! plc_is_pi() && ! plc_is_admin() ) )
212 $bullet_item .= leaf(href(l_sirius(),"Sirius"));
213 $bullet_item .= ul_end();
214 $items [] = expanded($bullet_item);
216 //////////////////// Admin
217 if( plc_is_admin() || plc_is_pi() ) {
219 $bullet_item .= href(l_admin(),"Admin search");
220 $bullet_item .= ul_start();
221 if ( plc_is_admin() )
222 $bullet_item .= leaf( href (l_node_add(),"Add Node"));
223 $bullet_item .= leaf( href( l_tags(),"Tags"));
224 $bullet_item .= leaf( href( l_nodegroups(),"Node groups"));
225 $bullet_item .= leaf (href(l_peers(),'Peers'));
226 if ( plc_is_admin() )
227 $bullet_item .= leaf (href(l_events(),'Events'));
228 $bullet_item .= ul_end();
229 $items [] = expanded($bullet_item);
233 $bullet_item .= href(l_about(),'About MyPLC');
234 $bullet_item .= ul_start();
235 $bullet_item .= leaf ( href (l_doc_plcapi(),"PLCAPI doc"));
236 $bullet_item .= leaf ( href (l_doc_nmapi(),"NMAPI doc"));
237 $bullet_item .= ul_end();
238 $items[] = expanded($bullet_item);
240 //$block['content'] = theme('list_item', $items);
241 $block['content'] = menu_theme($items);
249 $block['content'] .= ob_get_contents();
257 function planetlab_login_validate($form_id, $form_values) {
260 if ($form_values['name'] && $form_values['pass']) {
261 // Drupal login succeeded
262 if (($user = user_authenticate($form_values['name'], trim($form_values['pass']))) &&
267 $plc = new PLCSession($form_values['name'], $form_values['pass']);
269 // PlanetLab login failed
271 form_set_error('login', t('Sorry. Unrecognized username or password.'));
272 watchdog('planetlab', t('Login attempt failed for %user.', array('%user' => theme('placeholder', $form_values['name']))));
275 // PlanetLab login succeeded
277 // Login admins to Drupal as the superuser
278 if (in_array('admin', $plc->person['roles'])) {
279 $user = user_load(array('uid' => 1));
285 function planetlab_login_submit($form_id, $form_values) {
288 // Our referring page is encased in a query string of the form
289 // "destination=referrer".
290 parse_str(drupal_get_destination(), $variables);
291 $destination = $variables['destination'];
293 // The referrer itself is a URL path with the original query string,
294 // e.g. "referer.php?query".
295 extract(parse_url($destination)); // => $query
297 // Which we then have to parse again as a query string.
298 parse_str($query, $variables);
299 $url = $variables['url'];
302 // To handle the edge case where this function is called during a
303 // bootstrap, check for the existence of t().
304 if (function_exists('t')) {
305 $message = t('Session opened for %name.', array('%name' => theme('placeholder', $plc->person['email'])));
308 $message = "Session opened for ". check_plain($person['email']);
310 watchdog('planetlab', $message);
313 // Create a timestamped final URL so that browsers don't return the user to
314 // a cached page (where it would appear as if they never logged in or out).
315 return array('time='. time());
317 // Make sure that redirections are always local
318 $url = urldecode($url);
319 if ($url[0] != "/") {
322 header("Location: $url");
328 function planetlab_logout() {
332 // Invalidate PlanetLab session
334 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
337 // Destroy the current session:
340 // The time prevents caching.
341 drupal_goto(NULL, 'time='. time());
344 function planetlab_user($type, &$edit, &$user, $category = NULL) {
349 watchdog('planetlab', t('Session closed for %name.', array('%name' => theme('placeholder', $plc->person['email']))));
355 function isValidFileName($file) {
357 /* don't allow .. and allow any "word" character \ / */
359 return preg_match('/^(((?:\.)(?!\.))|\w)+$/', $file);
363 function planetlab_page() {
364 $path = $_SERVER['DOCUMENT_ROOT'] . preg_replace('/^db\//', '/planetlab/', $_GET['q']);
366 // error_log("Requested " . $_GET['q'] . " -> $path");
369 foreach (array('index.php', 'index.html', 'index.htm') as $index) {
370 if (is_file($path . "/$index")) {
377 if (is_file($path)) {
378 if (preg_match('/.php$/', $path)) {
381 $output = ob_get_contents();
384 if (isValidFileName($path)) {
385 $output = file_get_contents($path);
397 function theme_planetlab($content) {