You are here

linkedin.module in LinkedIn Integration 7

Same filename and directory in other branches
  1. 6 linkedin.module

File

linkedin.module
View source
<?php

/*
 * @file : Main hooks implementation for Linkedin module
 */

/*
 * Implements hook_init()
 * Make sure we load needed files.
 */
function linkedin_init() {
  if (!@(include_once variable_get('linkedin_liboauth_path', ''))) {
    if (file_exists(drupal_get_path('module', 'oauth_common') . '/lib/OAuth.php')) {
      variable_set('linkedin_liboauth_path', drupal_get_path('module', 'oauth_common') . '/lib/OAuth.php');
    }
    else {
      drupal_set_message(t('Unable to find the OAuth library. Please check your settings for the Linkedin module.'), 'warning');
    }
  }
}

/*
 * Implements of hook_menu
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_menu() {
  $items = array();

  //global settings form
  $items['admin/config/services/linkedin'] = array(
    'title' => 'LinkedIn integration',
    'description' => 'linkedin module settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'linkedin_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'linkedin.pages.inc',
  );

  //callback for oauth token request on linkedin API.
  $items['linkedin/token/%user'] = array(
    'type' => MENU_CALLBACK,
    'description' => 'Let user tie their LI account with their local user account',
    'page callback' => 'linkedin_access_token',
    'page arguments' => array(
      2,
    ),
    'access callback' => 'linkedin_token_access',
    'access arguments' => array(
      2,
    ),
    'file' => 'linkedin.inc',
  );

  // User settings form : used by submodules.
  $items['user/%user/edit/linkedin'] = array(
    'title' => 'Linkedin',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'linkedin_user_settings',
    'file' => 'linkedin.pages.inc',
    'page arguments' => array(
      1,
    ),
    'access callback' => 'linkedin_user_access',
    //access arguments don't support multiple arguments, so create our access handler
    'access arguments' => array(
      1,
    ),
  );
  return $items;
}

/*
 * Custom access callback for linkedin/token/%user
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_token_access($account) {
  global $user;
  if ($account->uid > 0 && $account->uid == $user->uid) {
    return TRUE;
  }
  if (variable_get('linkedin_debug_mode', 0) == 1) {
    drupal_set_message(t('LinkedIn debug : Access denied to /linkedin/token/@requested. Requesting user has uid @requesting, which is different from the requested account.', array(
      '@requested' => $account->uid,
      '@requesting' => $user->uid,
    )), 'warning');
  }
  return FALSE;
}

/*
 * Custom access callback for user/%user/edit/linkedin
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_user_access($account) {
  global $user;
  if ($user->uid == $account->uid) {

    // Check if some perms have been defined by submodules.
    $perms = module_invoke_all('linkedin_user_edit_perms');
    foreach ($perms as $perm) {
      if (user_access($perm)) {
        return TRUE;
      }
    }
    if (module_exists('linkedin_auth')) {
      return TRUE;
    }
  }
  if (variable_get('linkedin_debug_mode', 0) == 1) {
    if ($user->uid != $account->uid) {
      drupal_set_message(t('LinkedIn debug : Access denied to /linkedin/token/@requested.<br />
      Requesting user (uid @requesting) is different from the requested account (uid @requested)', array(
        '@requested' => $account->uid,
        '@requesting' => $user->uid,
      )));
      return FALSE;
    }
    if (empty($perms)) {
      drupal_set_message(t('LinkedIn debug : Access denied to /linkedin/token/@requested.<br />
      No module is implementing hook_linkedin_user_edit_perms. Enable at least one submodule and check permissions'));
      return FALSE;
    }
    foreach ($perms as $perm) {
      $permissions .= $perm . ', ';
    }
    drupal_set_message(t('LinkedIn debug : Access denied to /linkedin/token/@requested.<br />
      User must have at least one of these permissions : @permissions', array(
      '@permissions' => $permissions,
    )));
  }
  return FALSE;
}

/*
 * Let us retrieve profile fields.
 * Returns an array contening the fields requested.
 * @params
 * $uid : the uid of the user we want to access infos
 * $fields : the fields we want to retrieve, as an array (see http://developer.linkedin.com/docs/DOC-1061 for complete list).
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_get_profile_fields($uid, $fields = array()) {
  module_load_include('inc', 'linkedin');
  return _linkedin_get_profile_fields($uid, $fields);
}

/*
 * Returns an array contening the fields requested.
 * @params
 * $url : full request url to a linkedin API ressource (see API doc for syntax)
 * $tokens : the user tokens, as an array containing keys 'token_key' and 'token_secret' with their value
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_get_fields($url, $tokens) {
  module_load_include('inc', 'linkedin');
  return _linkedin_get_fields($url, $tokens);
}

/*
 * Let us 'put' data into user profile.
 * Usage :
 * $uid : the uid of the user we want to access infos
 * $api : the api we want to use to update. For now, only Share API is available
 * $body : The content to be sent.
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_put_profile_field($uid, $body, $api = 'shares') {
  module_load_include('inc', 'linkedin');
  return _linkedin_put_profile_field($uid, $body, $api);
}

/**
 * Implements hook_user_delete()
 */
function linkedin_user_delete($account) {

  // Delete token when user is deleted
  db_delete('linkedin_token')
    ->condition('uid', $account->uid)
    ->execute();
}

Functions

Namesort descending Description
linkedin_get_fields @todo Please document this function.
linkedin_get_profile_fields @todo Please document this function.
linkedin_init
linkedin_menu @todo Please document this function.
linkedin_put_profile_field @todo Please document this function.
linkedin_token_access @todo Please document this function.
linkedin_user_access @todo Please document this function.
linkedin_user_delete Implements hook_user_delete()