/* login.php: this file can be used by PHP scripts to enable authentication via ODP::Passport by include()ing it at the top of the script. it checks whether the user has a valid key set as a cookie, and redirects them to o::p if not (which then redirects them back after authentication). more information at: http://pgl.yoyo.org/odp/passport/ - pgl@yoyo.org */ define('ODP_PASSPORT_VERSION', '1.0'); // config (originally in a seperate file; probably a good idea to move it out of this file...) $collection = ''; // name of the collection of tools $secret = ''; // secret shared with passport.rpfuller.org $numdays = 1; // number of days after which cookies expire // just in case someone wants to override cookie lifetimes with really short values $duration = 60 * 60 * $numdays; // lifetime of cookies in seconds // make $ppuser with values from cookies $ppuser = array( 'key' => $_REQUEST['key'], 'user' => $_REQUEST['user'], 'issued' => $_REQUEST['issued'], 'privs' => $_REQUEST['privs'], ); // log the user out from any script using this file - just use &odp_pp_logout=1 if ($odp_pp_logout) { odp_passport_unsetcookies($ppuser, $GLOBALS['PHP_SELF']); redirect('http://passport.rpfuller.org/logout.cgi'); exit; } // check for valid key if (!$ppuser['key'] || !odp_passport_validuser($ppuser, $secret)) { // either the user has no key set or it's invalid, so redirect them to // http://passport.rpfuller.org/ with a query string for logging in $url = "http://passport.rpfuller.org/?pp_cat=$collection&pp_tool=" . urlencode($tool ? $tool : $GLOBALS['PHP_SELF']); // add any vars from the query string that we want passed back after authentication if ($_GET) { foreach ($_GET as $field => $val) { $url .= '&pp_p_' . urlencode(stripslashes($field)) . '=' . urlencode(stripslashes($val)); } } redirect($url); exit; } else { // user has a valid key, they're either staying here (if this is an include) or // being redirected (if this is the main login script) // set cookies every time the user connects so the cookie doesn't die out odp_passport_setcookies($ppuser, $GLOBALS['PHP_SELF'], $duration); // just in case... (not sure about that last one actually, must check) unset($ppuser['key'], $_COOKIE['key'], $_REQUEST['key'], $_POST['key'], $GLOBALS['key']); // check if $tool is set, redirect to it if so if ($tool) { // find variables prefixed with pp_p_ and add them to the query string $vars = array(); foreach ($_REQUEST as $field => $val) { if (preg_match('/^pp_p_(.*)$/', $field, $m)) { $vars[] = "{$m[1]}=" . urlencode($val); } } $url = "http://{$GLOBALS['SERVER_NAME']}$tool" . (!empty($vars) ? '?' . join('&', $vars) : ''); redirect($url); exit; } else { // otherwise this is just an include file, so return and let the rest of the script // get on with it return; } } // functions function odp_passport_validuser($user, $secret) { $text = "{$user{'user'}}.$secret.{$user['issued']}.{$user['privs']}"; $realkey = md5($text); if (defined('ODP_PASSPORT_DEBUG') && ODP_PASSPORT_DEBUG) { $perlcmd = "/usr/bin/perl -MDigest::MD5 -e 'print Digest::MD5::md5_hex($text)'"; $perlout = `$perlcmd`; echo '
',
"key generated now is: $realkey\n",
"key from p.r.org is: {$user['key']}\n",
"perl say the key is: $perlout\n\n",
"text is: $text\n",
"perl command is: $perlcmd\n",
'';
}
return $realkey == $user['key'];
}
function odp_passport_setcookies($user, $url, $duration=86400) {
$expiry = ($duration > 0 ? time() + $duration : $duration);
$path = dirname($url);
foreach ($user as $field => $val) {
if ($_COOKIE[$field] == $val)
continue;
setcookie($field, $val, $expiry, $path);
}
}
function odp_passport_unsetcookies($user, $url) {
$path = dirname($url);
foreach ($user as $field => $val) {
setcookie($field, $val, time() - 86400, $path);
}
}
?>