Could not create or update the FileStore directory "' . $store_path . '". Please check the effective permissions.

'; return false; } return new Auth_OpenID_FileStore($store_path); } /* * getConsumer. */ function &getConsumer() { $store=getStore(); if (!$store) return false; return new Auth_OpenID_Consumer($store); } /* * getTrustRoot */ function getTrustRoot() { $scheme = 'http'; $port = '80'; if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') { $scheme .= 's'; $port = '443'; } if ($port!=$_SERVER['SERVER_PORT']) $port = ':'.$port; else $port = ''; return sprintf("%s://%s%s%s/", $scheme, $_SERVER['SERVER_NAME'], $port, dirname($_SERVER['PHP_SELF'])); } function getReturnTo() { return getTrustRoot(); } /* * Try authentication with supplied OpenID. */ function try_auth($openid) { global $_openid; if (!$consumer = getConsumer()) return false; // Begin the OpenID authentication process. if (!($auth_request = $consumer->begin($openid))) { $_openid = '

Authentication error; not a valid OpenID.

'; return false; } // Build request. $sreg_request = Auth_OpenID_SRegRequest::build( // Required array('nickname'), // Optional array('fullname', 'email')); if ($sreg_request) $auth_request->addExtension($sreg_request); // Redirect the user to the OpenID server for authentication. // Store the token for this authentication so we can verify the // response. // For OpenID 1, send a redirect. For OpenID 2, use a Javascript // form to send a POST request to the server. if ($auth_request->shouldSendRedirect()) { $redirect_url = $auth_request->redirectURL(getTrustRoot(), getReturnTo()); // If the redirect URL can't be built, display an error // message. if (Auth_OpenID::isFailure($redirect_url)) { $_openid = '

Could not redirect to server: ' . $redirect_url->message . '

'; return false; } // Send redirect. header("Location: ".$redirect_url); $consumer->consumer->store->cleanupAssociations(); $consumer->consumer->store->cleanupNonces(); } else { // Generate form markup and render it. $form_id = 'openid_message'; $form_html = $auth_request->formMarkup(getTrustRoot(), getReturnTo(), false, array('id' => $form_id)); // Display an error if the form markup couldn't be generated; // otherwise, render the HTML. if (Auth_OpenID::isFailure($form_html)) { $_openid = '

Could not redirect to server: ' . $form_html->message . '

'; return false; } else { $page_contents = array( "", "OpenID transaction in progress", "", "", $form_html, ""); print implode("\n", $page_contents); $consumer->consumer->store->cleanupAssociations(); $consumer->consumer->store->cleanupNonces(); exit; } } return true; } /* * Finish authentication with OpenID. */ function finish_auth() { global $_openid; if (!$consumer = getConsumer()) return false; // Complete the authentication process using the server's // response. $return_to = getReturnTo(); $response = $consumer->complete($return_to); // Check the response status. if ($response->status == Auth_OpenID_CANCEL) { // This means the authentication was cancelled. $_openid = '

OpenID verification was cancelled.

'; return false; } else if ($response->status == Auth_OpenID_FAILURE) { // Authentication failed; display the error message. $_openid = '

OpenID authentication failed: ' . $response->message . '

'; return false; } else if ($response->status == Auth_OpenID_SUCCESS) { // This means the authentication succeeded; extract the // identity URL and Simple Registration data (if it was // returned). $_openid = array('identity' => htmlspecialchars($response->identity_url, ENT_QUOTES)); if ($response->endpoint->canonicalID) $_openid['canonicalid'] = $response->endpoint->canonicalID; $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response); $sreg = $sreg_resp->contents(); foreach ($sreg as $k => $v) $_openid[$k] = htmlspecialchars($v, ENT_QUOTES); // Get additional information from store. if ($a = $consumer->consumer->store->getAssociation($_openid['identity'])) { $o = unserialize($a->handle); foreach ($o as $k => $v) { if (!isset($_openid[$k])) $_openid[$k] = $v; } } else openid_set_var('version', 1); $_SESSION['openid'] = $_openid; header("Location: ".getTrustRoot()); } return true; } /* * Main... */ $_openid=''; // Log out? if (isset($_POST['openid_logout'])) { unset($_SESSION['openid']); header("Location: ".getTrustRoot()); return; } // Already logged in? if (isset($_SESSION) && sizeof($_SESSION) && isset($_SESSION['openid']) && is_array($_SESSION['openid']) && isset($_SESSION['openid']['identity'])) { $_openid = $_SESSION['openid']; return; } // Create login form? if ((!isset($_POST['openid_url']) || ''==$_POST['openid_url']) && !isset($_GET['openid_mode'])) { $_openid = '

'; return; } // Logging in? if (isset($_POST['openid_url'])) { if (!try_auth($_POST['openid_url'])) return; $_openid = '

Trying to authenticate...

'; return; } // Returning from authentication? if (!finish_auth()) return; $_openid = '

Back from authentication...

'; return; } function openid_set_var($var, $value='') { global $_openid; if (!$consumer = getConsumer()) return false; if (!is_array($_openid) || !isset($_openid['identity'])) return false; if ($value) $_openid = array_merge($_openid, array($var => $value)); elseif (isset($_openid[$var])) unset($_openid[$var]); $_SESSION['openid'] = $_openid; $a = new Auth_OpenID_Association(serialize($_openid), '-', time(), 86400*365, 'HMAC-SHA1'); $consumer->consumer->store->storeAssociation($_openid['identity'], $a); } function openid($email=false) { global $_openid; if (!preg_match('|^[^@]+@[^@\.]+\.[^@]+$|', $email)) $email=false; // Logged in? if (is_array($_openid)) { // Log out form... print '

You are logged in as '; if (isset($_openid['fullname']) && ''!=$_openid['fullname']) print $_openid['fullname']; elseif (isset($_openid['nickname']) && ''!=$_openid['nickname']) print $_openid['nickname']; else print $_openid['identity']; print '.

'; // No e-mail stored. if ($email && !isset($_openid['email'])) { if (isset($_POST['openid_email_register']) && preg_match('|^\S+@\S+\.\S+$|', $_POST['openid_email_register']) && isset($_POST['openid_email_send_verification'])) { openid_set_var('email', $_POST['openid_email_register']); } else { print '

'; } } // E-mail verification? if ($email && isset($_openid['email'])) { // Send e-mail verification? if (isset($_POST['openid_email_send_verification']) && (!isset($_openid['email_verification_code']) || isset($_openid['email_verification_email']) && $_openid['email_verification_email']!=$_openid['email'])) { $email_verification_code = md5(session_id().time().$_openid['email']); $message = 'Please verify your e-mail address by entering the following code into the field at ' . getTrustroot() . ':' . "\r\n" . $email_verification_code . "\r\n"; $headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n"; if (!@mail($_openid['email'], 'E-mail verification', $message, $headers)) print '

Unable to send verification mail, sorry.

'; else { openid_set_var('email_verification_code', $email_verification_code); openid_set_var('email_verification_email', $_openid['email']); } } // Verify e-mail? if (isset($_POST['openid_email_verification_code']) && ''!=$_POST['openid_email_verification_code'] && isset($_openid['email_verification_code']) && isset($_openid['email_verification_email']) && $_POST['openid_email_verification_code']==$_openid['email_verification_code'] && $_openid['email_verification_email']==$_openid['email']) { openid_set_var('verified_email', $_openid['email']); openid_set_var('email_verification_code'); } // Check verification status. if (isset($_openid['verified_email']) && $_openid['verified_email']==$_openid['email'] && isset($_openid['email_verification_email']) && $_openid['email_verification_email']==$_openid['email']) { // E-mail verified. print '

Your e-mail address, '.$_openid['email'].', has been verified.

'; if (isset($_openid['email_verification_code'])) openid_set_var('email_verification_code'); } elseif (isset($_openid['email_verification_code']) && isset($_openid['email_verification_email']) && $_openid['email_verification_email']==$_openid['email']) { // Verification pending. print '

An e-mail has been sent to your e-mail address for verification.

'; } else { // E-mail not verified. print '

Your e-mail address, '.$_openid['email'].', has not been verified.

'; if (isset($_openid['email_verification_code']) || isset($_openid['email_verification_email'])) { openid_set_var('email_verification_code'); openid_set_var('email_verification_email'); } } } print '
'; return $_openid; } // Print form or message. print $_openid; return false; } ?>