3 require_once("db.php");
5 /* provide OpenID support
7 * taken from http://www.plaxo.com/api/openid_recipe
10 function OpenIDVerify()
14 /* need the openip library */
15 require_once $OPENIDPATH."examples/consumer/common.php";
17 $consumer = getConsumer();
19 $return_to = getReturnTo();
20 $response = $consumer->complete($return_to);
22 // Check the response status.
23 if ($response->status == Auth_OpenID_CANCEL) {
24 // This means the authentication was cancelled.
25 echo 'Verification cancelled.';
27 } else if ($response->status == Auth_OpenID_FAILURE) {
28 // Authentication failed; display the error message.
29 echo "OpenID authentication failed: " . $response->message;
31 } else if ($response->status == Auth_OpenID_SUCCESS) {
32 // This means the authentication succeeded; extract the
33 // identity URL and Simple Registration data (if it was
35 $openid = $response->getDisplayIdentifier();
37 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
38 $sreg = $sreg_resp->contents();
47 function OpenIDAskForVerification($openid_url)
51 /* ask for openid verification */
52 require_once $OPENIDPATH."examples/consumer/common.php";
54 $openid =$_REQUEST['openid_url'];
55 $consumer = getConsumer();
57 /* check for authentication */
58 // Begin the OpenID authentication process.
59 $auth_request = $consumer->begin($openid);
61 // No auth request means we can't begin OpenID.
63 echo "Authentication error; not a valid OpenID.";
66 $sreg_request = Auth_OpenID_SRegRequest::build(array(),array('fullname','email', 'nickname'));
69 $auth_request->addExtension($sreg_request);
72 // Redirect the user to the OpenID server for authentication.
73 // Store the token for this authentication so we can verify the
76 // For OpenID 1, send a redirect. For OpenID 2, use a Javascript
77 // form to send a POST request to the server.
78 if ($auth_request->shouldSendRedirect()) {
79 $redirect_url = $auth_request->redirectURL(getTrustRoot(),
82 // If the redirect URL can't be built, display an error
84 if (Auth_OpenID::isFailure($redirect_url)) {
85 displayError("Could not redirect to server: " . $redirect_url->message);
88 header("Location: ".$redirect_url);
91 // Generate form markup and render it.
92 $form_id = 'openid_message';
93 $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(),
94 false, array('id' => $form_id));
96 // Display an error if the form markup couldn't be generated;
97 // otherwise, render the HTML.
98 if (Auth_OpenID::isFailure($form_html)) {
99 displayError("Could not redirect to server: " . $form_html->message);
106 function OpenIDUrlEncode($openid_url)
108 /* this converts each url to a standard form
109 * (domain lowercase and http at the beginning)
114 $parts = explode("/",$openid_url);
115 $return .= "http://";
117 /* check for http:// */
118 if( strtolower($parts[0]) == "http:" )
123 /* next part is the server*/
124 $return .= strtolower( $parts[0] );
127 foreach ($parts as $t)
133 function DB_GetUserId($openid_url)
135 $result = DB_query_array("SELECT user_id FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url)));
143 function DB_GetOpenIDsByUser($user_id)
145 return DB_query_array_all("SELECT openid_url FROM user_openids WHERE user_id = '$user_id'");
148 function DB_AttachOpenID($openid_url, $user_id)
150 DB_query("INSERT INTO user_openids VALUES (".DB_quote_smart(OpenIDUrlEncode($openid_url)).", '$user_id')");
153 function DB_DetachOpenID($openid_url, $user_id)
155 DB_query("DELETE FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url))." AND user_id = '$user_id'");
158 function DB_DetachOpenIDsByUser($user_id)
160 DB_query("DELETE FROM user_openids WHERE user_id = '$user_id'");