PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/elgg/mod/openid_client/actions/return.php

https://bitbucket.org/rhizomatik/lorea_production/
PHP | 207 lines | 139 code | 26 blank | 42 comment | 60 complexity | 1945a653931a5e74d1978e156ff12f5a MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Callback for return_to url redirection. The identity server will
  4. * redirect back to this handler with the results of the
  5. * authentication attempt.
  6. */
  7. ini_set('display_errors', '1');
  8. require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/engine/start.php");
  9. require_once('../openid_include.php');
  10. set_context('openid');
  11. $store = new OpenID_ElggStore();
  12. $consumer = new Auth_OpenID_Consumer($store);
  13. $return_url = $CONFIG->wwwroot.'mod/openid_client/actions/return.php';
  14. // TODO - handle passthru_url properly
  15. // $dest = $query['destination'];
  16. $response = $consumer->complete($return_url);
  17. if ($response->status == Auth_OpenID_CANCEL) {
  18. register_error(elgg_echo("openid_client:authentication_cancelled"));
  19. } else if ($response->status != Auth_OpenID_SUCCESS) {
  20. register_error(sprintf(elgg_echo("openid_client:authentication_failed"),$response->status,$response->message) );
  21. } else { // SUCCESS.
  22. $openid_url = $response->getDisplayIdentifier();
  23. // Look for sreg data.
  24. $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
  25. $sreg = $sreg_resp->contents();
  26. if ($sreg) {
  27. $email = trim($sreg['email']);
  28. $fullname = trim($sreg['fullname']);
  29. "client:".error_log($email);
  30. "client:".error_log($fullname);
  31. //print ($email.' '.$fullname);
  32. }
  33. $entities = get_entities_from_metadata('alias', $openid_url, 'user', 'openid');
  34. if (!$entities || $entities[0]->active == 'no') {
  35. if (!$entities) {
  36. // this account does not exist
  37. if (!$email || !openid_validate_email($email)) {
  38. // there is a problem with the email provided by the profile exchange, so generate a form to collect it
  39. if ($user = openid_client_create_openid_user($openid_url,$email, $fullname, true)) {
  40. $details = openid_client_create_invitation('a',$openid_url,$user->getGUID(),$email,$fullname);
  41. $body = openid_client_generate_missing_data_form($openid_url,'',$fullname,true,$details);
  42. }
  43. $missing_data = true;
  44. } elseif (!$fullname) {
  45. // the name is missing
  46. $email_confirmation = openid_client_check_email_confirmation($openid_url);
  47. if ($email_confirmation) {
  48. $prefix = 'a';
  49. } else {
  50. $prefix = 'n';
  51. }
  52. // create the account
  53. if ($user = openid_client_create_openid_user($openid_url,$email, $fullname, $email_confirmation)) {
  54. $details = openid_client_create_invitation($prefix,$openid_url,$user->getGUID(),$email,$fullname);
  55. $body = openid_client_generate_missing_data_form($openid_url,$email,'',$email_confirmation,$details);
  56. }
  57. $missing_data = true;
  58. } else {
  59. // email address and name look good
  60. $login = false;
  61. // create a new account
  62. $email_confirmation = openid_client_check_email_confirmation($openid_url);
  63. $user = openid_client_create_openid_user($openid_url,$email, $fullname, $email_confirmation);
  64. $missing_data = false;
  65. }
  66. } else {
  67. // this is an inactive account
  68. $user = $entities[0];
  69. // need to figure out why the account is inactive
  70. $email_confirmation = openid_client_check_email_confirmation($openid_url);
  71. if ($user->email && $user->name) {
  72. $missing_data = false;
  73. // no missing information
  74. if (!$email_confirmation) {
  75. // OK, this is weird - no email confirmation required and all the information has been supplied
  76. // this should not happen, so just go ahead and activate the account
  77. $user->active = 'yes';
  78. $user->save();
  79. }
  80. } else {
  81. // missing information
  82. $missing_data = true;
  83. // does this person have an existing magic code?
  84. if ($details = openid_client_get_invitation_by_username($user->alias)) {
  85. $body = openid_client_generate_missing_data_form($openid_url,$user->email,$user->name,$email_confirmation,$details);
  86. } else {
  87. // create a new magic code
  88. $details = openid_client_create_invitation('a',$openid_url,$user->getGUID(),$user->email,$user->name);
  89. $body = openid_client_generate_missing_data_form($openid_url,$user->email,$user->name,$email_confirmation,$details);
  90. }
  91. }
  92. }
  93. if ($user && !$missing_data) {
  94. if ($email_confirmation) {
  95. $i_code = openid_client_create_invitation('a',$openid_url,$user->guid,$email,$fullname);
  96. openid_client_send_activate_confirmation_message($i_code);
  97. system_message(sprintf(elgg_echo("openid_client:activate_confirmation"), $email));
  98. } else {
  99. system_message(sprintf(elgg_echo("openid_client:created_openid_account"),$email, $fullname));
  100. $login = true;
  101. }
  102. }
  103. } else {
  104. $user = $entities[0];
  105. // account is active, check to see if this user has been banned
  106. if (isset($user->banned) && $user->banned == 'yes') { // this needs to change.
  107. register_error(elgg_echo("openid_client:banned"));
  108. } else {
  109. // user has not been banned
  110. // check to see if email address has changed
  111. if ($email && $email != $user->email && openid_validate_email($email)) {
  112. // the email on the OpenID server is not the same as the email registered on this local client system
  113. $email_confirmation = openid_client_check_email_confirmation($openid_url);
  114. if ($CONFIG->openid_client_always_sync == 'yes') {
  115. // this client always forces client/server data syncs
  116. if ($fullname) {
  117. $user->name = $fullname;
  118. }
  119. if ($email_confirmation) {
  120. // don't let this user in until the email address change is confirmed
  121. $login = false;
  122. $i_code = openid_client_create_invitation('c',$openid_url,$user->guid,$email,$fullname);
  123. openid_client_send_change_confirmation_message($i_code);
  124. system_message(sprintf(elgg_echo("openid_client:change_confirmation"), $email));
  125. } else {
  126. $login = true;
  127. if (openid_client_get_user_by_email($email)) {
  128. register_error(elgg_echo("openid_client:email_in_use"),$email);
  129. } else {
  130. $user->email = $email;
  131. system_message(sprintf(elgg_echo("openid_client:email_updated"),$email));
  132. }
  133. }
  134. } else {
  135. $login = true;
  136. if (!$store->getNoSyncStatus($user)) {
  137. // the following conditions are true:
  138. // the email address has changed on the server,
  139. // this client does not *require* syncing with the server,
  140. // but this user has not turned off syncing
  141. // therefore the user needs to be offered the chance to sync his or her data
  142. $body = openid_client_generate_sync_form($email,$fullname,$user,$email_confirmation);
  143. }
  144. }
  145. } elseif ($fullname && $fullname != $user->name) {
  146. // the fullname on the OpenID server is not the same as the name registered on this local client system
  147. $login = true;
  148. if ($CONFIG->openid_client_always_sync == 'yes') {
  149. // this client always forces client/server data syncs
  150. $user->name = $fullname;
  151. } else {
  152. if (!$store->getNoSyncStatus($user)) {
  153. // the following conditions are true:
  154. // the fullname has changed on the server,
  155. // this client does not *require* syncing with the server,
  156. // but this user has not turned off syncing
  157. // therefore the user needs to be offered the chance to sync his or her data
  158. $body = openid_client_generate_sync_form($email,$fullname,$user,false);
  159. }
  160. }
  161. } else {
  162. // nothing has changed or the data is null so let this person in
  163. $login = true;
  164. }
  165. }
  166. }
  167. if ($login) {
  168. $rememberme = get_input('remember',0);
  169. if (!empty($rememberme)) {
  170. login($user,true);
  171. } else {
  172. login($user);
  173. }
  174. }
  175. }
  176. if(isset($body) && $body) {
  177. page_draw(elgg_echo('openid_client:information_title'),$body);
  178. } else {
  179. forward();
  180. }
  181. ?>