Index: openid_client_sreg.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openid_client_sreg/openid_client_sreg.module,v
retrieving revision 1.1.2.2
diff -u -p -r1.1.2.2 openid_client_sreg.module
--- openid_client_sreg.module	31 Mar 2009 01:45:03 -0000	1.1.2.2
+++ openid_client_sreg.module	24 Apr 2009 17:09:01 -0000
@@ -5,146 +5,121 @@
  * @file
  * Module providing openid client simple registration capability for Drupal
  */
-define('OPENID_CLIENT_SREG_PATH',    drupal_get_path('module', 'openid_client_sreg'));
-define('OPENID_CLIENT_SREG',         'http://openid.net/extensions/sreg/1.1');
-define('OPENID_CLIENT_SREG_ENABLED', variable_get('openid_client_sreg_enabled', false));
-define('OPENID_CLIENT_SREG_FIELDS',  variable_get('openid_client_sreg_fields', ''));
 
 /**
- * Implementation of hook_perm().
+ * Implementation of hook_menu_alter()
  */
-function openid_client_sreg_perm() {
-  return array('administer openid client sreg');
-}
-
-/**
- * Implementation of hook_menu()
- */
-function openid_client_sreg_menu() {
-  $items['admin/settings/openid_client_sreg'] = array(
-    'title'            => 'OpenID Client Simple Registration',
-    'description'      => 'Configure the OpenID Client Simple Registration settings',
-    'page callback'    => 'drupal_get_form',
-    'page arguments'   => array('openid_client_sreg_admin_settings'),
-    'access arguments' => array('administer openid client sreg'),
-  );
-  return $items;
+function openid_client_sreg_menu_alter(&$callbacks) {
+  $callbacks['openid/authenticate']['page callback'] = 'openid_client_sreg_authentication_page';
+  $callbacks['openid/authenticate']['file'] = NULL;
 }
 
 /**
  * Implementation of hook_openid()
  */
 function openid_client_sreg_openid($op, $request = array()) {
-   $response = array();
   // add our information to the request if op is request and we are enabled
-  if ($op == 'request' && OPENID_CLIENT_SREG_ENABLED) {
-    $required = $optional = array();
-    $fields = explode("\n", OPENID_CLIENT_SREG_FIELDS);
-    foreach ($fields as $field) {
-      $row = explode("|", $field);
-      if ($row[2]) {
-        $required[] = trim(strrchr($row[1], "."), ".");
-      }
-      else {
-        $optional[] = trim(strrchr($row[1], "."), ".");
+  if ($op == 'request') {
+    $return = array();
+    $attributes = module_invoke_all('openid_client', 'get');
+    $short_names = array();
+    foreach ($attributes as $attrib_url) {
+      if (strstr($attrib_url, '.sreg.')) {
+        $parts = explode('.', $attrib_url);
+        $short_names[] = $parts[count($parts) - 1];
       }
     }
-    // Only put the simple reg namespace in if we have data we wish to retrieve
-    if (!empty($required) || !empty($optional)) {
-      // Only set it if the sreg is not currently set in the system
+    // If we have fields set them in the request as optional fields
+    if (!empty($short_names)) {
       if (!isset($request['openid.ns.sreg'])) {
-        $response['openid.ns.sreg'] = OPENID_CLIENT_SREG;
-      }
-      // If we have required fields set them in the request
-      if (!empty($required)) {
-        $response['openid.sreg.required'] = implode(",", $required);
-      }
-      // If we have optional fields set them in the request
-      if (!empty($optional)) {
-        $response['openid.sreg.optional'] = implode(",", $optional);
+        $return['openid.ns.sreg'] = 'http://openid.net/extensions/sreg/1.1';
       }
+      $return['openid.sreg.optional'] = implode(",", $short_names);
     }
+    return $return;
   }
-  return $response;
 }
 
 /**
- * Implementation of hook_openid_response()
+ * Menu callback; Process an OpenID authentication.
+ */
+function openid_client_sreg_authentication_page() {
+  $result = openid_complete();
+  switch ($result['status']) {
+    case 'success':
+      return openid_client_sreg_authentication($result);
+    case 'failed':
+      drupal_set_message(t('OpenID login failed.'), 'error');
+      break;
+    case 'cancel':
+      drupal_set_message(t('OpenID login cancelled.'));
+      break;
+  }
+  drupal_goto();
+}
+
+/**
+ * Authenticate a user or attempt registration.
  *
- * @param $response
- *      Array holding all openid response data
- * @param $account
- *      User object of the user being updated
+ * @param $response Response values from the OpenID Provider.
  */
-function openid_client_sreg_openid_response($response = array(), $account) {
-  if (OPENID_CLIENT_SREG_ENABLED) {
-    $user_update = array();
-    if (module_exists('content_profile')) {
-      $types = content_profile_get_types('names');
-      foreach ($types as $type) {
-        break;
-      }
-      // Update the users existing content profile node if one exists for the user
-      if (!($profile = content_profile_load($type, $account->uid))) {
-        $profile = new StdClass();
-        $profile->uid = $account->uid;
-        $profile->type = $type;
+function openid_client_sreg_authentication($response) {
+  module_load_include('inc', 'openid');
+
+  $identity = $response['openid.claimed_id'];
+
+  $account = user_external_load($identity);
+  if (isset($account->uid)) {
+    if (!variable_get('user_email_verification', TRUE) || $account->login) {
+      if (user_external_login($account, $_SESSION['openid']['user_login_values']) === TRUE) {
+        // Let other modules to save the things to the user
+        module_invoke_all('openid_client', 'save', $response, $account);
       }
     }
-    // go through all of the returned attributes and save them for the user
-    $fields = explode("\n", OPENID_CLIENT_SREG_FIELDS);
-    // Only do updates if we actually have field mappings
-    if (is_array($fields)) {
-      foreach ($fields as $field) {
-        $row = explode("|", $field);
-        $field_name = $row[0];
-        $response_field_name = $row[1];
-        // If the field in the response is not set we need to ignore this value
-        if (isset($response[$response_field_name])) {
-          if (isset($account->{$field_name})) {
-            $user_update[$field_name] = $response[$response_field_name];
-          }
-          elseif (module_exists('content_profile')) {
-            // replacing the type for value so we can retrieve the value from the system
-            if (isset($profile->{$field_name})) {
-              $profile->{$field_name}[0]['#value'] = $response[$response_field_name];
-            }
-            else {
-              $profile->{$field_name} = array(0 => array('value' => $response[$reponse_field_name]));
-            }
-          }
-        }
-      }
-      // saving the user account with any pertinent information inside it
-      if (!empty($user_update)) {
-        user_save($account, $user_update);
-      }
-      // If the content profile module exists save the content profile node
-      if (module_exists('content_profile')) {
-        $profile = node_submit($profile);
-        node_save($profile);
+    else {
+      drupal_set_message(t('You must validate your email address for this account before logging in via OpenID'));
+    }
+  }
+  elseif (variable_get('user_register', 1)) {
+    // Register new user
+    $form_state['redirect'] = NULL;
+    $form_state['values']['name'] = (empty($response['openid.sreg.nickname'])) ? $identity : $response['openid.sreg.nickname'];
+    $form_state['values']['mail'] = (empty($response['openid.sreg.email'])) ? '' : $response['openid.sreg.email'];
+    $form_state['values']['pass']  = user_password();
+    $form_state['values']['status'] = 1;
+    $form_state['values']['response'] = $response;
+    $form_state['values']['auth_openid'] = $identity;
+    $form = drupal_retrieve_form('user_register', $form_state);
+    drupal_prepare_form('user_register', $form, $form_state);
+    drupal_validate_form('user_register', $form, $form_state);
+    if (form_get_errors()) {
+      // We were unable to register a valid new user, redirect to standard
+      // user/register and prefill with the values we received.
+      drupal_set_message(t('OpenID registration failed for the reasons listed. You may register now, or if you already have an account you can <a href="@login">log in</a> now and add your OpenID under "My Account"', array('@login' => url('user/login'))), 'error');
+      $_SESSION['openid']['values'] = $form_state['values'];
+      // We'll want to redirect back to the same place.
+      $destination = drupal_get_destination();
+      unset($_REQUEST['destination']);
+      drupal_goto('user/register', $destination);
+    }
+    else {
+      unset($form_state['values']['response']);
+      $account = user_save('', $form_state['values']);
+      // Terminate if an error occured during user_save().
+      if (!$account) {
+        drupal_set_message(t("Error saving user account."), 'error');
+        drupal_goto();
+      }
+      // Verify if the user_external_login failed or suceeded
+      if (user_external_login($account) === TRUE && variable_get('openid_client_ax_enabled', FALSE)) {
+        // Let other modules to save the things to the user
+        module_invoke_all('openid_client', 'save', $response, $account);
       }
     }
+    drupal_redirect_form($form, $form_state['redirect']);
   }
-}
-
-/**
- * Settings function for the module
- */
-function openid_client_sreg_admin_settings() {
-  $form['openid_client_sreg'] = array('#type' => 'fieldset', '#title' => t('OpenID Client Simple Registration Settings'), '#collapsible' => TRUE, '#collapsed' => FALSE);
-  $form['openid_client_sreg']['openid_client_sreg_enabled'] = array(
-    '#type'          => 'checkbox',
-    '#title'         => t('Enable Simple Registration'),
-    '#default_value' => OPENID_CLIENT_SREG_ENABLED,
-    '#description'   => t('Enable the simple registration for OpenID'),
-  );
-  $form['openid_client_sreg']['openid_client_sreg_fields'] = array(
-    '#type'          => 'textarea',
-    '#title'         => t('OpenID / Drupal Field Mappings'),
-    '#default_value' => OPENID_CLIENT_SREG_FIELDS,
-    '#rows'          => 10,
-    '#description'   => t('Field mappings between the drupal system fields and the openid fields. Any fields in here will be requested by the openid simple registration. Format drupalfield|openidfield|required where required will be 1 (required) or 0 (optional)'),
-  );
-  return system_settings_form($form);
+  else {
+    drupal_set_message(t('Only site administrators can create new user accounts.'), 'error');
+  }
+  drupal_goto();
 }
