You are here

hybridauth.install in HybridAuth Social Login 7.2

Same filename and directory in other branches
  1. 6.2 hybridauth.install
  2. 7 hybridauth.install

Install, update and uninstall functions for the HybridAuth module.

File

hybridauth.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the HybridAuth module.
 */

/**
 * Implements hook_uninstall().
 */
function hybridauth_uninstall() {
  module_load_include('module', 'hybridauth');
  foreach (hybridauth_providers_list() as $provider_id => $provider_name) {
    variable_del('hybridauth_provider_' . $provider_id . '_keys_id');
    variable_del('hybridauth_provider_' . $provider_id . '_keys_key');
    variable_del('hybridauth_provider_' . $provider_id . '_keys_secret');
    variable_del('hybridauth_provider_' . $provider_id . '_window_type');
    variable_del('hybridauth_provider_' . $provider_id . '_window_width');
    variable_del('hybridauth_provider_' . $provider_id . '_window_height');
    variable_del('hybridauth_provider_' . $provider_id . '_display');
    variable_del('hybridauth_provider_' . $provider_id . '_scope');

    // Legacy variables.
    variable_del('hybridauth_provider_' . $provider_id . '_enabled');
    variable_del('hybridauth_provider_' . $provider_id . '_weight');
  }
  variable_del('hybridauth_providers');
  variable_del('hybridauth_library_path');
  variable_del('hybridauth_required_fields');
  variable_del('hybridauth_widget_title');
  variable_del('hybridauth_widget_type');
  variable_del('hybridauth_widget_use_overlay');
  variable_del('hybridauth_widget_link_text');
  variable_del('hybridauth_widget_link_title');
  variable_del('hybridauth_widget_icon_pack');
  variable_del('hybridauth_widget_weight');
  variable_del('hybridauth_widget_hide_links');
  variable_del('hybridauth_disable_username_change');
  variable_del('hybridauth_remove_password_fields');
  variable_del('hybridauth_pictures');
  variable_del('hybridauth_register');
  variable_del('hybridauth_email_verification');
  variable_del('hybridauth_email_verification_body');
  variable_del('hybridauth_email_verification_subject');
  variable_del('hybridauth_username');
  variable_del('hybridauth_registration_username_change');
  variable_del('hybridauth_registration_password');
  variable_del('hybridauth_display_name');
  variable_del('hybridauth_override_realname');
  variable_del('hybridauth_destination');
  variable_del('hybridauth_destination_error');
  variable_del('hybridauth_forms');
  variable_del('hybridauth_duplicate_emails');
  variable_del('hybridauth_proxy');
  variable_del('hybridauth_debug');

  // Legacy variables.
  variable_del('hybridauth_registration_role_choice');
  variable_del('hybridauth_registration_roles');
  variable_del('hybridauth_window_type');
}

/**
 * Implements hook_requirements().
 */
function hybridauth_requirements($phase) {
  module_load_include('module', 'hybridauth');
  $requirements = array();

  // Ensure translations don't break at install time.
  $t = get_t();
  if ($phase == 'install' || $phase == 'runtime') {
    if ($lib_path = _hybridauth_library_path()) {
      try {
        require_once $lib_path . '/Hybrid/Auth.php';
        $requirements['hybridauth'] = array(
          'title' => $t('HybridAuth library'),
          'value' => Hybrid_Auth::$version,
          'severity' => REQUIREMENT_OK,
        );
      } catch (Exception $e) {
        $requirements['hybridauth'] = array(
          'title' => $t('HybridAuth library'),
          'description' => $e
            ->getMessage(),
          'severity' => REQUIREMENT_ERROR,
        );
      }
    }
    else {
      $requirements['hybridauth'] = array(
        'title' => $t('HybridAuth library'),
        'description' => $t('HybridAuth library is missing'),
        'severity' => REQUIREMENT_ERROR,
      );
    }
  }
  return $requirements;
}

/**
 * Implements hook_schema().
 */
function hybridauth_schema() {
  $schema = array();
  $schema['hybridauth_identity'] = array(
    'description' => 'Holds identities from HybridAuth library.',
    'fields' => array(
      'id' => array(
        'description' => 'Unique ID of HybridAuth identity.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'The {users}.uid that owns this HybridAuth identity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'provider' => array(
        'description' => 'The authentication provider for this HybridAuth identity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'provider_identifier' => array(
        'description' => 'The authentication provider UID for this HybridAuth identity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'data' => array(
        'description' => 'A serialized array containing information from HybridAuth library.',
        'type' => 'blob',
        'not null' => TRUE,
        'size' => 'normal',
      ),
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
    ),
    'unique keys' => array(
      'provider_provider_identifier' => array(
        'provider',
        'provider_identifier',
      ),
    ),
    'foreign keys' => array(
      'hybridauth_identity_user' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $schema['hybridauth_session'] = array(
    'description' => 'Holds sessions data from HybridAuth library.',
    'fields' => array(
      'uid' => array(
        'description' => 'The {users}.uid that owns this HybridAuth session data.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'data' => array(
        'description' => 'A serialized array containing session data from HybridAuth library.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'medium',
      ),
      'updated' => array(
        'description' => 'The Unix timestamp when the session was saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'hybridauth_session_user' => array(
        'table' => 'users',
        'columns' => array(
          'uid' => 'uid',
        ),
      ),
    ),
    'primary key' => array(
      'uid',
    ),
  );
  return $schema;
}

/**
 * Set us at schema rev 7000 as a base version.
 */
function hybridauth_update_7000(&$sandbox) {
}

/**
 * Add hybridauth_session DB table.
 */
function hybridauth_update_7001(&$sandbox) {
  if (!db_table_exists('hybridauth_session')) {
    $schema = hybridauth_schema();
    db_create_table('hybridauth_session', $schema['hybridauth_session']);
  }
}

/**
 * Remove unused hybridauth variables of disabled providers.
 */
function hybridauth_update_7002(&$sandbox) {
  foreach (hybridauth_providers_list() as $provider_id => $provider_name) {
    if (!variable_get('hybridauth_provider_' . $provider_id . '_enabled', 0)) {
      variable_del('hybridauth_provider_' . $provider_id . '_enabled');
      variable_del('hybridauth_provider_' . $provider_id . '_weight');
    }
  }
}

/**
 * Remove plenty of variables (2 per provider).
 */
function hybridauth_update_7003(&$sandbox) {
  $weights = array();
  $hybridauth_providers = array();
  foreach (array_keys(hybridauth_providers_list()) as $provider_id) {
    $weights[$provider_id] = variable_get('hybridauth_provider_' . $provider_id . '_weight', 50);
    variable_del('hybridauth_provider_' . $provider_id . '_weight');
  }
  asort($weights);
  foreach (array_keys($weights) as $provider_id) {
    $hybridauth_providers[$provider_id] = variable_get('hybridauth_provider_' . $provider_id . '_enabled', 0) ? $provider_id : 0;
    variable_del('hybridauth_provider_' . $provider_id . '_enabled');
  }
  variable_set('hybridauth_providers', $hybridauth_providers);
}

/**
 * Remove users passwords stored in plaintext from users DB table.
 */
function hybridauth_update_7004(&$sandbox) {

  // Calculate the total number of users.
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = db_select('users', 'u')
      ->condition('data', '%' . db_like('pass') . '%', 'LIKE')
      ->countQuery()
      ->execute()
      ->fetchField();
    if (!$sandbox['total']) {
      return t('There are no user accounts to process.');
    }
    $sandbox['current'] = 0;
  }

  // Process this number of users per pass.
  $users_per_pass = 10;

  // Get users' ids for one pass.
  $result = db_select('users', 'u')
    ->fields('u', array(
    'uid',
  ))
    ->condition('data', '%' . db_like('pass') . '%', 'LIKE')
    ->orderBy('u.uid')
    ->range($sandbox['current'], $users_per_pass)
    ->execute();

  // Remove user password from $user['data']['hybridauth'].
  foreach ($result as $row) {

    // Skip the anonymous user uid == 0.
    if (!empty($row->uid)) {
      $account = user_load($row->uid);
      if (isset($account->data['hybridauth']['pass'])) {
        unset($account->data['hybridauth']['pass']);
        user_save($account);
      }
    }
    $sandbox['current']++;
  }
  $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
  if ($sandbox['#finished'] === 1) {
    return t('@total user accounts were processed.', array(
      '@total' => $sandbox['total'],
    ));
  }
}

/**
 * Failed update, moved to hybridauth_update_7006().
 */
function hybridauth_update_7005(&$sandbox) {
}

/**
 * Remove users passwords stored in plaintext from hybridauth_identity DB table.
 */
function hybridauth_update_7006(&$sandbox) {

  // Calculate the total number of HybridAuth identities.
  if (!isset($sandbox['total'])) {
    $sandbox['total'] = db_select('hybridauth_identity', 'ha_id')
      ->condition('data', '%' . db_like('pass') . '%', 'LIKE')
      ->countQuery()
      ->execute()
      ->fetchField();
    if (!$sandbox['total']) {
      return t('There are no HybridAuth identities to process.');
    }
    $sandbox['current'] = 0;
  }

  // Process this number of HybridAuth identities per pass.
  $identities_per_pass = 10;

  // Get HybridAuth identities ids for one pass.
  $result = db_select('hybridauth_identity', 'ha_id')
    ->fields('ha_id', array(
    'id',
  ))
    ->condition('data', '%' . db_like('pass') . '%', 'LIKE')
    ->orderBy('ha_id.id')
    ->range($sandbox['current'], $identities_per_pass)
    ->execute();

  // Remove user passwords from HybridAuth identities.
  foreach ($result as $row) {
    $identity = _hybridauth_identity_load_by_id($row->id);
    $data = unserialize($identity['data']);
    if (isset($data['pass'])) {
      unset($data['pass']);
      _hybridauth_identity_save($data, $identity['uid']);
    }
    $sandbox['current']++;
  }
  $sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];
  if ($sandbox['#finished'] === 1) {
    return t('@total HybridAuth identities were processed.', array(
      '@total' => $sandbox['total'],
    ));
  }
}

Functions

Namesort descending Description
hybridauth_requirements Implements hook_requirements().
hybridauth_schema Implements hook_schema().
hybridauth_uninstall Implements hook_uninstall().
hybridauth_update_7000 Set us at schema rev 7000 as a base version.
hybridauth_update_7001 Add hybridauth_session DB table.
hybridauth_update_7002 Remove unused hybridauth variables of disabled providers.
hybridauth_update_7003 Remove plenty of variables (2 per provider).
hybridauth_update_7004 Remove users passwords stored in plaintext from users DB table.
hybridauth_update_7005 Failed update, moved to hybridauth_update_7006().
hybridauth_update_7006 Remove users passwords stored in plaintext from hybridauth_identity DB table.