2 require_once("db.php");
4 /* provide OpenID support
6 * taken from http://www.plaxo.com/api/openid_recipe
9 function OpenIDVerify()
13 /* need the openip library */
14 require_once $OPENIDPATH."examples/consumer/common.php";
16 $consumer = getConsumer();
18 $return_to = getReturnTo();
19 $response = $consumer->complete($return_to);
21 // Check the response status.
22 if ($response->status == Auth_OpenID_CANCEL) {
23 // This means the authentication was cancelled.
24 echo 'Verification cancelled.';
26 } else if ($response->status == Auth_OpenID_FAILURE) {
27 // Authentication failed; display the error message.
28 echo "OpenID authentication failed: " . $response->message;
30 } else if ($response->status == Auth_OpenID_SUCCESS) {
31 // This means the authentication succeeded; extract the
32 // identity URL and Simple Registration data (if it was
34 $openid = $response->getDisplayIdentifier();
36 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
37 $sreg = $sreg_resp->contents();
46 function OpenIDAskForVerification($openid_url)
50 /* ask for openid verification */
51 require_once $OPENIDPATH."examples/consumer/common.php";
53 $openid =$_REQUEST['openid_url'];
54 $consumer = getConsumer();
56 /* check for authentication */
57 // Begin the OpenID authentication process.
58 $auth_request = $consumer->begin($openid);
60 // No auth request means we can't begin OpenID.
62 echo "Authentication error; not a valid OpenID.";
65 $sreg_request = Auth_OpenID_SRegRequest::build(array(),array('fullname','email', 'nickname'));
68 $auth_request->addExtension($sreg_request);
71 // Redirect the user to the OpenID server for authentication.
72 // Store the token for this authentication so we can verify the
75 // For OpenID 1, send a redirect. For OpenID 2, use a Javascript
76 // form to send a POST request to the server.
77 if ($auth_request->shouldSendRedirect()) {
78 $redirect_url = $auth_request->redirectURL(getTrustRoot(),
81 // If the redirect URL can't be built, display an error
83 if (Auth_OpenID::isFailure($redirect_url)) {
84 displayError("Could not redirect to server: " . $redirect_url->message);
87 header("Location: ".$redirect_url);
90 // Generate form markup and render it.
91 $form_id = 'openid_message';
92 $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(),
93 false, array('id' => $form_id));
95 // Display an error if the form markup couldn't be generated;
96 // otherwise, render the HTML.
97 if (Auth_OpenID::isFailure($form_html)) {
98 displayError("Could not redirect to server: " . $form_html->message);
105 function OpenIDUrlEncode($openid_url)
107 /* this converts each url to a standard form
108 * (domain lowercase and http at the beginning)
113 $parts = explode("/",$openid_url);
114 $return .= "http://";
116 /* check for http:// */
117 if( strtolower($parts[0]) == "http:" )
122 /* next part is the server*/
123 $return .= strtolower( $parts[0] );
126 foreach ($parts as $t)
132 function DB_GetUserId($openid_url)
134 $result = DB_query_array("SELECT user_id FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url)));
142 function DB_GetOpenIDsByUser($user_id)
144 return DB_query_array_all("SELECT openid_url FROM user_openids WHERE user_id = '$user_id'");
147 function DB_AttachOpenID($openid_url, $user_id)
149 DB_query("INSERT INTO user_openids VALUES (".DB_quote_smart(OpenIDUrlEncode($openid_url)).", '$user_id')");
152 function DB_DetachOpenID($openid_url, $user_id)
154 DB_query("DELETE FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url))." AND user_id = '$user_id'");
157 function DB_DetachOpenIDsByUser($user_id)
159 DB_query("DELETE FROM user_openids WHERE user_id = '$user_id'");