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