You are here

function ldap_servers_token_replace in Lightweight Directory Access Protocol (LDAP) 8.2

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

Parameters

array $ldap_entry:

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

Return value

string $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 56
collection of functions related to ldap tokens

Code

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

  // user_account
  $desired_tokens = ldap_servers_token_tokens_needed_for_template($text);

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

    // if no tokens exist in text, return text itself.  It is literal value
  }
  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;
  }
  $attributes = array_keys($tokens);

  //array of attributes (sn, givenname, etc)
  $values = array_values($tokens);

  //array of attribute values (Lincoln, Abe, etc)
  $result = str_replace($attributes, $values, $text);
  $result = preg_replace('/\\[[^\\]]*>/', '', $result);

  // strip out any unreplace tokens
  return $result == '' ? NULL : $result;

  // return NULL if $result is empty, else $result
}