You are here

ulogin.module in uLogin (advanced version) 6

Same filename and directory in other branches
  1. 8 ulogin.module
  2. 7 ulogin.module

File

ulogin.module
View source
<?php

if (module_exists('token')) {
  include_once drupal_get_path('module', 'ulogin') . '/ulogin.tokens.inc';
}

/**
 * Implements hook_perm().
 */
function ulogin_perm() {
  return array(
    'use ulogin',
  );
}

/**
 * Implements hook_menu().
 */
function ulogin_menu() {
  $items = array();
  $items['admin/user/ulogin'] = array(
    'title' => 'uLogin',
    'description' => 'Manage uLogin widget default settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ulogin_settings_form',
    ),
    //'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ulogin.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/user/ulogin/main'] = array(
    'title' => 'Main',
    'description' => 'Manage uLogin widget default settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ulogin_settings_form',
    ),
    //'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ulogin.admin.inc',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/user/ulogin/advanced'] = array(
    'title' => 'Advanced',
    'description' => 'Manage uLogin widget default settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ulogin_adv_settings_form',
    ),
    //'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'ulogin.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  $items['admin/reports/ulogin'] = array(
    'title' => 'uLogin identities',
    'description' => 'View uLogin identities counts grouped by authentication provider.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ulogin_report',
    ),
    //'access callback' => 'user_access',
    'access arguments' => array(
      'access site reports',
    ),
    'file' => 'ulogin.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['ulogin'] = array(
    'page callback' => 'ulogin_callback',
    //'access callback' => 'user_access',
    'access arguments' => array(
      'use ulogin',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'ulogin.pages.inc',
  );
  $items['user/%user/ulogin'] = array(
    'title' => 'uLogin identities',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ulogin_user_identity',
      1,
    ),
    'access callback' => 'user_edit_access',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'ulogin.pages.inc',
  );
  $items['user/%user/ulogin/delete'] = array(
    'title' => 'Delete uLogin identity',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ulogin_user_identity_delete',
      1,
      4,
    ),
    'access callback' => 'user_edit_access',
    'access arguments' => array(
      1,
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'ulogin.pages.inc',
  );
  return $items;
}

/**
 * Implements hook_block().
 */
function ulogin_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks['ulogin']['info'] = t('User login - uLogin widget only');
    return $blocks;
  }
  elseif ($op == 'view' && $delta == 'ulogin' && !user_is_logged_in()) {
    $block = array();
    $block['subject'] = t('User login');
    $element['#type'] = 'ulogin_widget';
    $block['content'] = drupal_render($element);
    return $block;
  }
}

/**
 * Implements hook_user().
 */
function ulogin_user($op, &$edit, &$account, $category = NULL) {
  global $ulogin_data;
  if ($op == 'insert' && !empty($ulogin_data)) {

    // the user is being created
    $edit['ulogin'] = $ulogin_data;
  }
  if ($op == 'delete') {
    _ulogin_identity_delete_by_uid($account->uid);
  }

  //realname integration
  if ($op == 'load' && !empty($account->ulogin) && module_exists('realname') && $account->name == $account->realname) {
    $account->realname = $account->ulogin['first_name'] . ' ' . $account->ulogin['last_name'];
  }
}

/**
 * Implements hook_nodeapi().
 * Change the realname...
 */
function ulogin_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'view' && module_exists('realname') && !empty($node->realname) && $node->realname == $node->realname_save) {
    $account = user_load($node->uid);
    $node->realname = $node->name = $account->realname;
  }
}

/**
 * Implements hook_comment().
 * Add realname...
 */
function ulogin_comment(&$comment, $op) {
  if ($op == 'view' && !empty($comment->ulogin)) {
    $comment->realname = $comment->ulogin['first_name'] . ' ' . $comment->ulogin['last_name'];
  }
}

/**
 * Implements hook_form_alter().
 */
function ulogin_form_alter(&$form, &$form_state, $form_id) {
  _ulogin_form_alter($form, $form_state, $form_id);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ulogin_form_comment_form_alter(&$form, &$form_state) {
  _ulogin_form_alter($form, $form_state, 'comment_form');
}
function _ulogin_form_alter(&$form, &$form_state, $form_id) {
  if (user_access('use ulogin') && user_is_anonymous() && in_array($form_id, array_filter(variable_get('ulogin_forms', array(
    'user_login',
    'user_login_block',
  ))))) {
    $form['ulogin'] = array(
      '#type' => 'ulogin_widget',
      '#weight' => -100,
    );
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ulogin_form_user_profile_form_alter(&$form, &$form_state) {
  if (!empty($form['_account']['#value']->ulogin)) {
    if (variable_get('ulogin_disable_username_change', 1)) {
      $form['account']['name']['#access'] = FALSE;
    }
    if (variable_get('ulogin_remove_password_fields', 1)) {
      unset($form['account']['pass']);
    }
  }
}

/**
 * Implements hook_elements().
 */
function ulogin_elements() {
  $types = array();
  $types['ulogin_widget'] = array(
    '#input' => FALSE,
    '#theme' => 'ulogin_widget',
    '#ulogin_id' => 'uLogin',
    '#ulogin_display' => variable_get('ulogin_display', 'panel'),
    '#ulogin_fields' => implode(',', array_filter(variable_get('ulogin_fields', array(
      'first_name',
      'last_name',
      'email',
      'nickname',
      'bdate',
      'sex',
      'photo',
      'photo_big',
      'country',
      'city',
    )))),
    '#ulogin_optional' => '',
    '#ulogin_providers' => variable_get('ulogin_adv_providers', '') ? variable_get('ulogin_adv_providers', '') : implode(',', array_filter(variable_get('ulogin_providers', array(
      'vkontakte',
      'odnoklassniki',
      'mailru',
      'facebook',
    )))),
    '#ulogin_hidden' => variable_get('ulogin_adv_hidden', '') ? variable_get('ulogin_adv_hidden', '') : implode(',', array_filter(variable_get('ulogin_hidden', array(
      'twitter',
      'google',
      'yandex',
      'livejournal',
      'openid',
    )))),
    '#ulogin_destination' => variable_get('ulogin_destination', ''),
    '#ulogin_redirect' => variable_get('ulogin_redirect', 0),
  );

  //TODO: how to make it to respect custom value for '#ulogin_redirect'
  if ($types['ulogin_widget']['#ulogin_redirect']) {

    //$types['ulogin_widget']['#attached']['js'][drupal_get_path('module', 'ulogin') . '/js/ulogin.js'] = array('type' => 'file');
  }
  return $types;
}

/**
 * Implements hook_theme().
 */
function ulogin_theme($existing, $type, $theme, $path) {
  return array(
    'ulogin_widget' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}
function theme_ulogin_widget($element) {
  $output = '';
  static $counter;
  if (!isset($counter)) {
    $counter = 0;

    //adding js only once

    /*if (variable_get('ulogin_load_type', 1)) {
        drupal_add_js(drupal_get_path('module', 'ulogin') . '/js/ulogin.async.js');
      }
      else {
        drupal_add_js('document.write(unescape("%3Cscript src=\'http://ulogin.ru/js/ulogin.js\' type=\'text/javascript\'%3E%3C/script%3E"));', 'inline');
      }*/
    drupal_add_js(drupal_get_path('module', 'ulogin') . '/js/ulogin.async.js');
  }
  $increment = FALSE;
  if ($element['#ulogin_id'] == 'uLogin') {
    $increment = TRUE;
    $counter++;
  }
  if (variable_get('ulogin_redirect', 0)) {
    $callback = 'Drupalulogintoken';
    $redirect = url('sites/all/libraries/ulogin/ulogin_xd.html', array(
      'absolute' => TRUE,
    ));
  }
  else {
    $callback = '';
    $redirect = _ulogin_token_url($element['#ulogin_destination']);
  }
  $id = $element['#ulogin_id'] . ($increment ? '-' . $counter : '');
  if ($element['#ulogin_display'] == 'small' || $element['#ulogin_display'] == 'panel') {
    $output = '<div ' . 'id="' . $id . '" ' . 'data-ulogin="display=' . $element['#ulogin_display'] . ';fields=' . $element['#ulogin_fields'] . ';optional=' . $element['#ulogin_optional'] . ';providers=' . $element['#ulogin_providers'] . ';hidden=' . $element['#ulogin_hidden'] . ';callback=' . $callback . ';redirect_uri=' . $redirect . '"></div>';
  }
  elseif ($element['#ulogin_display'] == 'window') {
    $output = '<a href="#" ' . 'id="' . $id . '" ' . 'data-ulogin="display=' . $element['#ulogin_display'] . ';fields=' . $element['#ulogin_fields'] . ';optional=' . $element['#ulogin_optional'] . ';callback=' . $callback . ';redirect_uri=' . $redirect . '"><img src="//ulogin.ru/img/button.png" width=187 height=30 alt="' . t('MultiAuthentication') . '"/></a>';
  }

  /*if (variable_get('ulogin_load_type', 1)) {
      drupal_add_js(array('ulogin' => array($id)), 'setting');
    }*/
  drupal_add_js(array(
    'ulogin' => array(
      $id,
    ),
  ), 'setting');
  return theme('form_element', $element, $output);
}
function _ulogin_token_url($destination = NULL) {
  if (empty($destination)) {
    $destination = drupal_get_destination();
  }
  else {
    $destination = array(
      'destination' => $destination,
    );
  }
  $token_url = url('ulogin', array(
    'absolute' => TRUE,
    'query' => $destination,
  ));
  return urlencode($token_url);
}
function _ulogin_providers_list() {
  return array(
    'vkontakte' => t('VKontakte'),
    'odnoklassniki' => t('Odnoklassniki'),
    'mailru' => t('Mail.ru'),
    'facebook' => t('Facebook'),
    'twitter' => t('Twitter'),
    'google' => t('Google'),
    'yandex' => t('Yandex'),
    'livejournal' => t('Live Journal'),
    'openid' => t('OpenID'),
    'lastfm' => t('Last.fm'),
    'linkedin' => t('LinkedIn'),
    'liveid' => t('Live ID'),
    'soundcloud' => t('SoundCloud'),
    'steam' => t('Steam'),
    'flickr' => t('Flickr'),
    'vimeo' => t('Vimeo'),
    'youtube' => t('YouTube'),
    'webmoney' => t('WebMoney'),
    'foursquare' => t('foursquare'),
    'tumblr' => t('tumblr'),
    'googleplus' => t('Google+'),
  );
}
function _ulogin_fields_list() {
  return array(
    'first_name' => t('First name'),
    'last_name' => t('Last name'),
    'email' => t('Email address'),
    'nickname' => t('Nickname'),
    'bdate' => t('Birthday'),
    'sex' => t('Gender'),
    'photo' => t('Photo'),
    'photo_big' => t('Big photo'),
    'country' => t('Country'),
    'city' => t('City'),
    'phone' => t('Phone number'),
  );
}
function _ulogin_make_username($data) {
  $ulogin_name = $name = 'ulogin_' . $data['network'] . '_' . $data['uid'];
  drupal_alter('ulogin_username', $name, $data);

  //check that the altered username is unique
  if (user_load(array(
    'name' => $name,
  ))) {
    return $ulogin_name;
  }
  else {
    return $name;
  }
}
function _ulogin_user_is_blocked_by_uid($uid) {
  $deny = db_fetch_object(db_query("SELECT name FROM {users} WHERE status = 0 AND uid = %d", $uid));
  return $deny;
}
function _ulogin_identity_save($data) {
  global $user;

  //check if this identity is already registered - this might not be needed

  //db_result(db_query("SELECT uid FROM {ulogin_identity} WHERE network = '%s' AND ulogin_uid = '%s'",

  //  $data['network'], $data['uid']));
  db_query("INSERT INTO {ulogin_identity} (uid, network, ulogin_uid, data) VALUES (%d, '%s', '%s', '%s')", $user->uid, $data['network'], $data['uid'], serialize($data));
}
function _ulogin_identity_load($data) {
  $result = db_query("SELECT * FROM {ulogin_identity} WHERE network = '%s' AND ulogin_uid = '%s'", $data['network'], $data['uid']);
  return db_fetch_array($result);
}
function _ulogin_identity_load_by_uid($uid) {
  $result = db_query("SELECT * FROM {ulogin_identity} WHERE uid = '%d'", $uid);
  $identities = array();
  while ($identity = db_fetch_array($result)) {
    $identities[$identity['id']] = $identity;
  }
  return $identities;
}
function _ulogin_identity_load_by_id($id) {
  $result = db_query("SELECT * FROM {ulogin_identity} WHERE id = '%d'", $id);
  return db_fetch_array($result);
}
function _ulogin_identity_delete_by_uid($uid) {
  $result = db_query("DELETE FROM {ulogin_identity} WHERE uid = '%d'", $uid);
  return $result;
}
function _ulogin_identity_delete_by_id($id) {
  $result = db_query("DELETE FROM {ulogin_identity} WHERE id = '%d'", $id);
  return $result;
}

/**
 * Implements hook_email_registration_name().
 */
function ulogin_email_registration_name($edit, $account) {
  global $ulogin_data;
  if (!empty($ulogin_data) || !empty($account->ulogin)) {
    return $account->name;
  }
  return NULL;
}