You are here

function salesforce_api_login in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 6.2 salesforce_api/salesforce_api.module \salesforce_api_login()

Helper function for salesforce_api_connect(). You should probably not call this function directly

Parameters

string $username:

string $password:

string $token:

Return value

Salesforce Client object

1 call to salesforce_api_login()
salesforce_api_connect in salesforce_api/salesforce_api.module
Creates an object used for communicating with the Salesforce server and performs a login to verify the API credentials.

File

salesforce_api/salesforce_api.module, line 466
Defines an API that enables modules to interact with the Salesforce server.

Code

function salesforce_api_login($username, $password, $token) {
  require_once DRUPAL_ROOT . '/' . SALESFORCE_DIR_SOAPCLIENT . '/SforceEnterpriseClient.php';

  // Create a new Salesforce object with the API credentials.
  $sf = (object) array(
    'username' => $username,
    'password' => $password,
    'token' => $token,
    'client' => new SforceEnterpriseClient(),
  );

  // Default to the uploaded WSDL, then any wsdl in web path if available.
  if (!($dir = variable_get('salesforce_api_dir_wsdl', FALSE))) {
    $dir = SALESFORCE_DIR_WSDL;
  }
  $wsdl = $dir . '/enterprise.wsdl.xml';

  // Otherwise fall back to the one included with the Toolkit.
  if (!file_exists($wsdl)) {
    $wsdl = SALESFORCE_DIR_SOAPCLIENT . '/enterprise.wsdl.xml';
  }

  // Get proxy settings if a proxy is configured.
  $proxy = NULL;
  if (variable_get('salesforce_api_proxy', FALSE)) {
    $proxy = new ProxySettings();
    $proxy->host = variable_get('salesforce_api_proxy_host', '');
    $proxy->port = variable_get('salesforce_api_proxy_port', '');
    $proxy->login = variable_get('salesforce_api_proxy_login', '');
    $proxy->password = variable_get('salesforce_api_proxy_password', '');
  }

  // Connect to the server and login, logging any failures to the watchdog.
  try {

    // Connect to the server.
    // Ensure that the WSDL cache is not set.
    ini_set('soap.wsdl_cache_enabled', '0');
    ini_set('soap.wsdl_cache_ttl', '0');
    $sf->client
      ->createConnection($wsdl, $proxy);

    // Attempt a login with the credentials entered by the user.
    $sf->login = $sf->client
      ->login($username, $password . $token);

    // Log the login occurence.
    salesforce_api_log(SALESFORCE_LOG_ALL, '@user (@email) logged into Salesforce.', array(
      '@user' => $sf->login->userInfo->userFullName,
      '@email' => $sf->login->userInfo->userEmail,
    ));
  } catch (Exception $e) {

    // Log the error message.
    // @todo: Determine whether it is a security risk to show !debug, since it can contain a username, password, and security token.
    salesforce_api_log(SALESFORCE_LOG_SOME, 'Could not login to Salesforce: %message.', array(
      '%message' => $e->faultstring,
      '!debug' => check_plain($sf->client
        ->getLastRequest()),
    ), WATCHDOG_ERROR);

    // Indicate the failed login.
    return FALSE;
  }

  // Indicate the successful login.
  return $sf;
}