summaryrefslogtreecommitdiffstats
path: root/include/openid.php
blob: 14024b8b132c1ba7b8be50f5523004b56696407b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php

require_once("db.php");

  /* provide OpenID support
   *
   * taken from http://www.plaxo.com/api/openid_recipe
   */

function OpenIDVerify()
{
  global $OPENIDPATH;

  /* need the openip library */
  require_once $OPENIDPATH."examples/consumer/common.php";

  $consumer = getConsumer();

  $return_to = getReturnTo();
  $response = $consumer->complete($return_to);

  // Check the response status.
  if ($response->status == Auth_OpenID_CANCEL) {
    // This means the authentication was cancelled.
    echo 'Verification cancelled.';
    return False;
  } else if ($response->status == Auth_OpenID_FAILURE) {
    // Authentication failed; display the error message.
    echo "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 = $response->getDisplayIdentifier();

    $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
    $sreg = $sreg_resp->contents();
  }

  if(isset($sreg))
    return $sreg;
  else
    return "ok";
}

function OpenIDAskForVerification($openid_url)
{
  global $OPENIDPATH;

  /* ask for openid verification */
  require_once $OPENIDPATH."examples/consumer/common.php";

  $openid =$_REQUEST['openid_url'];
  $consumer = getConsumer();

  /* check for authentication */
  // Begin the OpenID authentication process.
  $auth_request = $consumer->begin($openid);

  // No auth request means we can't begin OpenID.
  if (!$auth_request) {
    echo "Authentication error; not a valid OpenID.";
    }

  $sreg_request = Auth_OpenID_SRegRequest::build(array(),array('fullname','email', 'nickname'));

  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)) {
      displayError("Could not redirect to server: " . $redirect_url->message);
    } else {
	// Send redirect.
      header("Location: ".$redirect_url);
      }
  } else {
    // Generate form markup and render it.
    $form_id = 'openid_message';
    $form_html = $auth_request->htmlMarkup(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)) {
	displayError("Could not redirect to server: " . $form_html->message);
    } else {
      print $form_html;
    }
  }
}

function OpenIDUrlEncode($openid_url)
{
  /* this converts each url to a standard form
   * (domain lowercase and http at the beginning)
   */

  $return = "";

  $parts = explode("/",$openid_url);
  $return .= "http://";

  /* check for http:// */
  if( strtolower($parts[0]) == "http:" )
    array_shift($parts);
  if( $parts[0] == "")
    array_shift($parts);

  /* next part is the server*/
  $return .= strtolower( $parts[0] );
  array_shift($parts);

  foreach ($parts as $t)
    $return .= "/$t";

  return $return;
}

function DB_GetUserId($openid_url)
{
  $result = DB_query_array("SELECT user_id FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url)));

  if($result)
    return $result[0];
  else
    return False;
}

function DB_GetOpenIDsByUser($user_id)
{
  return DB_query_array_all("SELECT openid_url FROM user_openids WHERE user_id =".DB_quote_smart($user_id));
}

function DB_AttachOpenID($openid_url, $user_id)
{
  DB_query("INSERT INTO user_openids VALUES (".DB_quote_smart(OpenIDUrlEncode($openid_url)).", ".DB_quote_smart($user_id).")");
}

function DB_DetachOpenID($openid_url, $user_id)
{
  DB_query("DELETE FROM user_openids WHERE openid_url = ".DB_quote_smart(OpenIDUrlEncode($openid_url)).
	   " AND user_id = ".DB_quote_smart($user_id));
}

function DB_DetachOpenIDsByUser($user_id)
{
  DB_query("DELETE FROM user_openids WHERE user_id = ".DB_quote_smart($user_id));
}

?>