You are here

function ldap_servers_token_replace in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_servers/ldap_servers.tokens.inc \ldap_servers_token_replace()

User_account.

Parameters

$resource: $ldap_entry

$text: such as "[dn]", "[cn]@my.org", "[displayName] [sn]", "Drupal Provisioned"

string $resource_type:

Return value

string|string[]|null $text with tokens replaced or NULL if replacement not available

4 calls to ldap_servers_token_replace()
LdapServer::userEmailFromLdapEntry in ldap_servers/LdapServer.class.php
LdapServersTestCase::testInstall in ldap_servers/tests/ldap_servers.test
LdapUserConf::drupalUserToLdapEntry in ldap_user/LdapUserConf.class.php
Populate ldap entry array for provisioning.
LdapUserConf::entryToUserEdit in ldap_user/LdapUserConf.class.php
Populate $user edit array (used in hook_user_save, hook_user_update, etc) ... should not assume all attribues are present in ldap entry.

File

ldap_servers/ldap_servers.tokens.inc, line 74
Collection of functions related to ldap tokens.

Code

function ldap_servers_token_replace($resource, $text, $resource_type = 'ldap_entry') {

  // Desired tokens are of form "cn","mail", etc.
  $desired_tokens = ldap_servers_token_tokens_needed_for_template($text);
  if (empty($desired_tokens)) {

    // If no tokens exist in text, return text itself.  It is literal value.
    return $text;
  }
  $tokens = [];

  // @TODO: Should really be if/else if only those two exist.
  switch ($resource_type) {
    case 'ldap_entry':
      $tokens = ldap_servers_token_tokenize_entry($resource, $desired_tokens, LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST);
      break;
    case 'user_account':
      $tokens = ldap_servers_token_tokenize_user_account($resource, $desired_tokens, LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST);
      break;
  }

  // Add lowercase tokens to avoid case sensitivity.
  foreach ($tokens as $attribute => $value) {
    $tokens[drupal_strtolower($attribute)] = $value;
  }

  // If $text is not present as an attribute key, insert it and set the key's value to an empty string.
  if (!array_key_exists(drupal_strtolower($text), $tokens)) {
    $tokens[$text] = '';
    $tokens[drupal_strtolower($text)] = '';
  }

  // Array of attributes (sn, givenname, etc)
  $attributes = array_keys($tokens);

  // Array of attribute values (Lincoln, Abe, etc)
  $values = array_values($tokens);
  $result = str_replace($attributes, $values, $text);

  // Strip out any unreplaced tokens.
  $result = preg_replace('/\\[[^\\]]*]/', '', $result);

  // Return NULL if $result is empty, else $result.
  return $result == '' ? NULL : $result;
}