aboutsummaryrefslogtreecommitdiff
path: root/return.php
blob: 3d949b4922b11343caf696acfdd4bed836100a5e (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php

/**
 * Callback for return_to url redirection. The identity server will
 * redirect back to this handler with the results of the
 * authentication attempt.
 * 
 * Note: the Elgg action system strips off the query string and is incompatible with
 * the JanRain OpenID library, so we need to keep this as an ordinary PHP file
 * for now.
 * 
 */

require_once(dirname(dirname(dirname(__FILE__))).'/engine/start.php');
require_once(dirname(__FILE__).'/models/model.php');

global $CONFIG;

set_context('openid');
$store = new OpenID_ElggStore();
$consumer = new Auth_OpenID_Consumer($store);

$return_url = $CONFIG->wwwroot.'mod/openid_client/return.php';

// TODO - handle passthru_url properly
// $dest = $query['destination'];
$response = $consumer->complete($return_url);

if ($response->status == Auth_OpenID_CANCEL) {
    register_error(elgg_echo("openid_client:authentication_cancelled"));
} else if ($response->status != Auth_OpenID_SUCCESS) {
    register_error(sprintf(elgg_echo("openid_client:authentication_failed"),$response->status,$response->message) );
} else { // SUCCESS.
	$openid_url = $response->getDisplayIdentifier();
	
    // Look for sreg data.
    $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
    $sreg = $sreg_resp->contents();
	if ($sreg) {
        $email = trim($sreg['email']);
        $fullname = trim($sreg['fullname']);
        //print ($email.' '.$fullname);
    }
    
    $entities = get_entities_from_metadata('alias', $openid_url, 'user', 'openid');

	if (!$entities || $entities[0]->active == 'no') {
		if (!$entities) {
			// this account does not exist
	    	if (!$email || !validate_email_address($email)) {
    	    	// there is a problem with the email provided by the profile exchange, so generate a form to collect it
				if ($user = openid_client_create_openid_user($openid_url,$email, $fullname, true)) {
		    		$details = openid_client_create_invitation('a',$openid_url,$user->getGUID(),$email,$fullname);
	    			$body = openid_client_generate_missing_data_form($openid_url,'',$fullname,true,$details);
				}
				$missing_data = true;
			} elseif (!$fullname) {
    			// the name is missing
				$email_confirmation = openid_client_check_email_confirmation($openid_url);
				if ($email_confirmation) {
					$prefix = 'a';
				} else {
					$prefix = 'n';
				}
				// create the account
				if ($user = openid_client_create_openid_user($openid_url,$email, $fullname, $email_confirmation)) {
					$details = openid_client_create_invitation($prefix,$openid_url,$user->getGUID(),$email,$fullname);
					$body = openid_client_generate_missing_data_form($openid_url,$email,'',$email_confirmation,$details);
				}
				$missing_data = true;
			} else {
				// email address and name look good 
				
				$login = false;			
							    
			    // create a new account
				   
			   	$email_confirmation = openid_client_check_email_confirmation($openid_url);		    					    
			    							
				$user = openid_client_create_openid_user($openid_url,$email, $fullname, $email_confirmation);
				$missing_data = false;
			}
		} else {
			// this is an inactive account
			$user = $entities[0];
			
			// need to figure out why the account is inactive
			
			$email_confirmation = openid_client_check_email_confirmation($openid_url);
			
			if ($user->email && $user->name) {
    			$missing_data = false;
    			// no missing information
			    if (!$email_confirmation) {
    			    // OK, this is weird - no email confirmation required and all the information has been supplied
    			    // this should not happen, so just go ahead and activate the account
    			    $user->active = 'yes';
    			    $user->save();
			    }
		    } else {    		    
    		    // missing information
    		    $missing_data = true;
    		    // does this person have an existing magic code?
    		    if ($details = openid_client_get_invitation_by_username($user->alias)) {
        		    $body = openid_client_generate_missing_data_form($openid_url,$user->email,$user->name,$email_confirmation,$details);
    		    } else {
        		    // create a new magic code
        		    $details = openid_client_create_invitation('a',$openid_url,$user->getGUID(),$user->email,$user->name);
	    			$body = openid_client_generate_missing_data_form($openid_url,$user->email,$user->name,$email_confirmation,$details);
    			}   
		    }
		}
		if ($user && !$missing_data) {
				
			if ($email_confirmation) {
				$i_code = openid_client_create_invitation('a',$openid_url,$user->guid,$email,$fullname);
				openid_client_send_activate_confirmation_message($i_code);
				system_message(sprintf(elgg_echo("openid_client:activate_confirmation"), $email));
			} else {
				system_message(sprintf(elgg_echo("openid_client:created_openid_account"),$email, $fullname));
				$login = true;
			}
		}
				
	} else {
    	
    	$user = $entities[0];
		
		// account is active, check to see if this user has been banned
	
	    if (isset($user->banned) && $user->banned == 'yes') { // this needs to change.
	        register_error(elgg_echo("openid_client:banned"));
	    } else {
		    // user has not been banned
		    // check to see if email address has changed
		    if ($email && $email != $user->email && validate_email_address($email)) {
			    // the email on the OpenID server is not the same as the email registered on this local client system
			    $email_confirmation = openid_client_check_email_confirmation($openid_url);
			    if ($CONFIG->openid_client_always_sync == 'yes') {
				    // this client always forces client/server data syncs
				    if ($fullname) {
				    	$user->name = $fullname;
			    	}
				    if ($email_confirmation) {
					    // don't let this user in until the email address change is confirmed
					    $login = false;
					    $i_code = openid_client_create_invitation('c',$openid_url,$user->guid,$email,$fullname);
					    openid_client_send_change_confirmation_message($i_code);
					    system_message(sprintf(elgg_echo("openid_client:change_confirmation"), $email));
					} else {
						$login = true;
						if (openid_client_get_user_by_email($email)) {
							register_error(elgg_echo("openid_client:email_in_use"),$email);
						} else {
    						$user->email = $email;
							system_message(sprintf(elgg_echo("openid_client:email_updated"),$email));
						}
					}
				} else {
					$login = true;
					if (!$store->getNoSyncStatus($user)) {
						// the following conditions are true:
						// the email address has changed on the server,
						// this client does not *require* syncing with the server,
						// but this user has not turned off syncing
						// therefore the user needs to be offered the chance to sync his or her data
						$body = openid_client_generate_sync_form($email,$fullname,$user,$email_confirmation);
					}
				}
			} elseif ($fullname && $fullname != $user->name) {
				// the fullname on the OpenID server is not the same as the name registered on this local client system
				$login = true;
				if ($CONFIG->openid_client_always_sync == 'yes') {
				    // this client always forces client/server data syncs
				    $user->name = $fullname;
				} else {
					if (!$store->getNoSyncStatus($user)) {
				    	// the following conditions are true:
						// the fullname has changed on the server,
						// this client does not *require* syncing with the server,
						// but this user has not turned off syncing
						// therefore the user needs to be offered the chance to sync his or her data
						$body = openid_client_generate_sync_form($email,$fullname,$user,false);
					}
				}
			} else {
				// nothing has changed or the data is null so let this person in
				$login = true;
			}
		}							    
	}
    
    if ($login) {
		
		$rememberme = get_input('remember',0);
		if (!empty($rememberme)) {
			login($user,true);
		} else {
    		login($user);
		}
	}
} 

if(isset($body) && $body) {
    
    page_draw(elgg_echo('openid_client:information_title'),$body);

} else {
	forward();
}