BUGFIX: one email message still had a hard-coded header
[e-DoKo.git] / include / openid.php
1 <?php
2 require_once("db.php");
3
4   /* provide OpenID support
5    *
6    * taken from http://www.plaxo.com/api/openid_recipe
7    */
8
9 function OpenIDVerify()
10 {
11   global $OPENIDPATH;
12
13   /* need the openip library */
14   require_once $OPENIDPATH."examples/consumer/common.php";
15
16   $consumer = getConsumer();
17
18   $return_to = getReturnTo();
19   $response = $consumer->complete($return_to);
20
21   // Check the response status.
22   if ($response->status == Auth_OpenID_CANCEL) {
23     // This means the authentication was cancelled.
24     echo 'Verification cancelled.';
25     return False;
26   } else if ($response->status == Auth_OpenID_FAILURE) {
27     // Authentication failed; display the error message.
28     echo "OpenID authentication failed: " . $response->message;
29     return False;
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
33     // returned).
34     $openid = $response->getDisplayIdentifier();
35
36     $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
37     $sreg = $sreg_resp->contents();
38   }
39
40   if(isset($sreg))
41     return $sreg;
42   else
43     return "ok";
44 }
45
46 function OpenIDAskForVerification($openid_url)
47 {
48   global $OPENIDPATH;
49
50   /* ask for openid verification */
51   require_once $OPENIDPATH."examples/consumer/common.php";
52
53   $openid =$_REQUEST['openid_url'];
54   $consumer = getConsumer();
55
56   /* check for authentication */
57   // Begin the OpenID authentication process.
58   $auth_request = $consumer->begin($openid);
59
60   // No auth request means we can't begin OpenID.
61   if (!$auth_request) {
62     echo "Authentication error; not a valid OpenID.";
63     }
64
65   $sreg_request = Auth_OpenID_SRegRequest::build(array(),array('fullname','email', 'nickname'));
66
67   if ($sreg_request) {
68     $auth_request->addExtension($sreg_request);
69   }
70
71   // Redirect the user to the OpenID server for authentication.
72   // Store the token for this authentication so we can verify the
73   // response.
74
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(),
79                                                getReturnTo());
80
81     // If the redirect URL can't be built, display an error
82     // message.
83     if (Auth_OpenID::isFailure($redirect_url)) {
84       displayError("Could not redirect to server: " . $redirect_url->message);
85     } else {
86         // Send redirect.
87       header("Location: ".$redirect_url);
88       }
89   } else {
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));
94
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);
99     } else {
100       print $form_html;
101     }
102   }
103 }
104
105 function OpenIDUrlEncode($openid_url)
106 {
107   /* this converts each url to a standard form
108    * (domain lowercase and http at the beginning)
109    */
110
111   $return = "";
112
113   $parts = explode("/",$openid_url);
114   $return .= "http://";
115
116   /* check for http:// */
117   if( strtolower($parts[0]) == "http:" )
118     array_shift($parts);
119   if( $parts[0] == "")
120     array_shift($parts);
121
122   /* next part is the server*/
123   $return .= strtolower( $parts[0] );
124   array_shift($parts);
125
126   foreach ($parts as $t)
127     $return .= "/$t";
128
129   return $return;
130 }
131
132 function DB_GetUserId($openid_url)
133 {
134   $result = DB_query_array("SELECT user_id FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url)));
135
136   if($result)
137     return $result[0];
138   else
139     return False;
140 }
141
142 function DB_GetOpenIDsByUser($user_id)
143 {
144   return DB_query_array_all("SELECT openid_url FROM user_openids WHERE user_id = '$user_id'");
145 }
146
147 function DB_AttachOpenID($openid_url, $user_id)
148 {
149   DB_query("INSERT INTO user_openids VALUES (".DB_quote_smart(OpenIDUrlEncode($openid_url)).", '$user_id')");
150 }
151
152 function DB_DetachOpenID($openid_url, $user_id)
153 {
154   DB_query("DELETE FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url))." AND user_id = '$user_id'");
155 }
156
157 function DB_DetachOpenIDsByUser($user_id)
158 {
159   DB_query("DELETE FROM user_openids WHERE user_id = '$user_id'");
160 }
161
162 ?>