You are here

function ldap_servers_token_tokenize_entry 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_tokenize_entry()

Turn an ldap entry into a token array suitable for the t() function

Parameters

ldap entry array $ldap_entry:

string prefix token prefix such as !,%,[:

string suffix token suffix such as ]:

$token_keys either an array of key names such as array('cn', 'dn') or string 'all' to return all tokens.:

Return value

token array suitable for t() functions of with lowercase keys as exemplified below

$ldap_entry should be in form of single entry returned from ldap_search() function:

'dn' => 'cn=jdoe,ou=campus accounts,ou=toledo campus,dc=ad,dc=myuniversity,dc=edu', 'mail' => array( 0 => 'jdoe@myuniversity.edu', 'count' => 1), 'sAMAccountName' => array( 0 => 'jdoe', 'count' => 1),

should return tokens such as:

-- from dn attribute [cn] = jdoe [cn:0] = jdoe [cn:last] => jdoe [ou] = campus accounts [ou:0] = campus accounts [ou:1] = toledo campus [ou:last] = toledo campus [dc] = ad [dc:0] = ad [dc:1] = myuniversity [dc:2] = edu [dc:last] = edu

-- from other attributes [mail] = jdoe@myuniversity.edu [mail:0] = jdoe@myuniversity.edu [mail:last] = jdoe@myuniversity.edu [samaccountname] = jdoe [samaccountname:0] = jdoe [samaccountname:last] = jdoe

[guid:0;base64_encode] = apply base64_encode() function to value [guid:0;bin2hex] = apply bin2hex() function to value [guid:0;msguid] = apply ldap_servers_msguid() function to value [guid:0;binary] = apply ldap_servers_binary() function to value. this is the most generic binary function

2 calls to ldap_servers_token_tokenize_entry()
ldap_servers_test_form_submit in ldap_servers/ldap_servers.test_form.inc
Submit hook for the LDAP server form.
ldap_servers_token_replace in ldap_servers/ldap_servers.tokens.inc

File

ldap_servers/ldap_servers.tokens.inc, line 186
collection of functions related to ldap tokens

Code

function ldap_servers_token_tokenize_entry($ldap_entry, $token_keys = 'all', $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
  $detailed_watchdog_log = config('ldap_help.settings')
    ->get('watchdog_detail');
  $tokens = array();
  $watchdog_tokens = array();
  if (function_exists('debug_backtrace') && ($backtrace = debug_backtrace())) {
    $watchdog_tokens['%calling_function'] = $backtrace[1]['function'];
  }
  if (!is_array($ldap_entry)) {
    if ($detailed_watchdog_log) {
      watchdog('ldap_servers', 'skipped tokenization of ldap entry because no ldap entry provided when called from %calling_function.', $watchdog_tokens, WATCHDOG_DEBUG);
    }
    return $tokens;

    // empty array
  }

  // add lowercase keyed entries to ldap array
  foreach ($ldap_entry as $key => $values) {
    $ldap_entry[drupal_strtolower($key)] = $values;
  }

  // 1. tokenize dn
  $dn_parts = ldap_explode_dn($ldap_entry['dn'], 0);

  // escapes attribute values, need to be unescaped later.
  unset($dn_parts['count']);
  $parts_count = array();
  $parts_last_value = array();
  foreach ($dn_parts as $pair) {
    list($attr_name, $attr_value) = explode('=', $pair);
    $attr_value = ldap_pear_unescape_dn_value($attr_value);
    try {
      $attr_value = check_plain($attr_value);
    } catch (Exception $e) {
      if ($detailed_watchdog_log) {
        $watchdog_tokens['%attr_name'] = $attr_name;
        watchdog('ldap_servers', 'skipped tokenization of attribute %attr_name because the value would not pass check_plain function.', $watchdog_tokens, WATCHDOG_DEBUG);
      }
      continue;

      // don't tokenize data that can't pass check_plain
    }
    if (!isset($parts_count[$attr_name])) {
      $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = $attr_value;
      $parts_count[$attr_name] = 0;
    }
    $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . (int) $parts_count[$attr_name] . $post] = $attr_value;
    $parts_last_value[$attr_name] = $attr_value;
    $parts_count[$attr_name]++;
  }
  foreach ($parts_count as $attr_name => $count) {
    $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . 'last' . $post] = $parts_last_value[$attr_name];
  }

  // tokenize other attributes
  if ($token_keys == 'all') {
    $token_keys = array_keys($ldap_entry);
    $token_keys = array_filter($token_keys, "is_string");
    foreach ($token_keys as $attr_name) {
      $attr_value = $ldap_entry[$attr_name];
      if (is_array($attr_value) && is_scalar($attr_value[0]) && $attr_value['count'] == 1) {
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = check_plain($attr_value[0]);
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . '0' . $post] = check_plain($attr_value[0]);
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . 'last' . $post] = check_plain($attr_value[0]);
      }
      elseif (is_array($attr_value) && $attr_value['count'] > 1) {
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . 'last' . $post] = check_plain($attr_value[$attr_value['count'] - 1]);
        for ($i = 0; $i < $attr_value['count']; $i++) {
          $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . $i . $post] = check_plain($attr_value[$i]);
        }
      }
      elseif (is_scalar($attr_value)) {
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = check_plain($attr_value);
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . '0' . $post] = check_plain($attr_value);
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . 'last' . $post] = check_plain($attr_value);
      }
    }
  }
  else {
    foreach ($token_keys as $full_token_key) {

      // $token_key = 'dn', 'mail', 'mail:0', 'mail:last', 'dept:1', 'guid:0;tobase64etc.
      $value = NULL;
      $conversion = FALSE;
      $parts = explode(';', $full_token_key);
      if (count($parts) == 2) {
        $conversion = $parts[1];
        $token_key = $parts[0];
      }
      else {
        $token_key = $full_token_key;
      }
      $parts = explode(LDAP_SERVERS_TOKEN_DEL, $token_key);
      $attr_name = drupal_strtolower($parts[0]);
      $ordinal_key = isset($parts[1]) ? $parts[1] : 0;
      $i = NULL;
      if ($attr_name == 'dn' || !isset($ldap_entry[$attr_name])) {

        // don't use empty() since a 0, "", etc value may be a desired value
        continue;
      }
      else {
        $count = $ldap_entry[$attr_name]['count'];
        if ($ordinal_key === 'last') {
          $i = $count > 0 ? $count - 1 : 0;
          $value = $ldap_entry[$attr_name][$i];
        }
        elseif (is_numeric($ordinal_key) || $ordinal_key == '0') {
          $value = $ldap_entry[$attr_name][$ordinal_key];
        }
        else {
          continue;

          // don't add token if case not covered
        }
      }
      if ($conversion) {
        switch ($conversion) {
          case 'base64_encode':
            $value = base64_encode($value);
            break;
          case 'bin2hex':
            $value = bin2hex($value);
            break;
          case 'msguid':
            $value = ldap_servers_msguid($value);
            break;
          case 'binary':
            $value = ldap_servers_binary($value);
            break;
        }
      }
      $tokens[$pre . $full_token_key . $post] = check_plain($value);
      if ($full_token_key != drupal_strtolower($full_token_key)) {
        $tokens[$pre . drupal_strtolower($full_token_key) . $post] = check_plain($value);
      }
    }
  }

  // include the dn.  it will not be handled correctly by previous loops
  $tokens[$pre . 'dn' . $post] = check_plain($ldap_entry['dn']);
  return $tokens;
}