You are here

linkedin.module in LinkedIn Integration 6

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

File

linkedin.module
View source
<?php

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

/*
 * Implementation of 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') . '/oauth.lib.php')) {

      // Cope with OAuth module 6.x-2
      variable_set('linkedin_liboauth_path', drupal_get_path('module', 'oauth') . '/oauth.lib.php');
    }
    elseif (file_exists(drupal_get_path('module', 'oauth_common') . '/lib/OAuth.php')) {

      // Cope with OAuth module 6.x-3
      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');
    }
  }
  module_load_include('inc', 'linkedin');
  module_load_include('pages.inc', 'linkedin');
}

/*
 * Implementation of hook_menu
 */
function linkedin_menu() {
  $items = array();

  //global settings form
  $items['admin/settings/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',
    '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,
    ),
    'file' => 'linkedin.pages.inc',
  );
  return $items;
}

/*
 * Custom access callback for linkedin/token/%user
 */
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
 */
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;
}

/*
 * Implementation of hook_user().
 */
function linkedin_user($op, &$edit, &$account, $category = NULL) {
  if ($op == 'delete') {
    db_query("DELETE FROM {linkedin_token} WHERE uid = %d", $account->uid);
  }
}