You are here

function ldap_server_tokenize_entry in Lightweight Directory Access Protocol (LDAP) 7

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

an ldap entry such as:

'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

2 calls to ldap_server_tokenize_entry()
ldap_servers_test_form_submit in ldap_servers/ldap_servers.test_form.inc
Submit hook for the LDAP server form.
ldap_server_token_replace in ldap_servers/ldap_servers.functions.inc

File

ldap_servers/ldap_servers.functions.inc, line 199
collection of functions that don't belong in server object

Code

function ldap_server_tokenize_entry($ldap_entry, $token_keys = 'all', $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
  $detailed_watchdog_log = variable_get('ldap_help_watchdog_detail', 0);
  $tokens = array();

  // 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);
    if (!($attr_value = ldap_server_check_plain($attr_value, $attr_name))) {
      continue;
    }
    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) {
        if ($value = ldap_server_check_plain($attr_value[0], $attr_name)) {
          $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = $value;
          $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . '0' . $post] = $value;
          $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . 'last' . $post] = $value;
        }
      }
      elseif (is_array($attr_value) && $attr_value['count'] > 1) {
        if ($value = ldap_server_check_plain($attr_value[$attr_value['count'] - 1], $attr_name)) {
          $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . 'last' . $post] = $value;
        }
        for ($i = 0; $i < $attr_value['count']; $i++) {
          if ($value = ldap_server_check_plain($attr_value[$i], $attr_name)) {
            $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . LDAP_SERVERS_TOKEN_DEL . $i . $post] = $value;
          }
        }
      }
      elseif (is_scalar($attr_value)) {
        if ($value = ldap_server_check_plain($attr_value, $attr_name)) {
          $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = $value;
        }
      }
    }
  }
  else {
    foreach ($token_keys as $token_key) {
      $parts = explode(LDAP_SERVERS_TOKEN_DEL, $token_key);
      $last_key = $parts[count($parts) - 1];
      if ($last_key == 'last') {
        $attr_name = join(LDAP_SERVERS_TOKEN_DEL, array_pop($parts));
        $count = $ldap_entry[$attr_name]['count'];
        $value = $ldap_entry[$attr_name][$count - 1];
      }
      elseif (is_numeric($last_key) || $last_key == '0') {
        $discard = array_pop($parts);
        $attr_name = join(LDAP_SERVERS_TOKEN_DEL, $parts);
        $value = $ldap_entry[$attr_name][(int) $last_key];
      }
      elseif (empty($ldap_entry[$token_key])) {
        continue;
      }
      else {
        $attr_name = $token_key;
        $value = $ldap_entry[$token_key][0];
      }
      $value = ldap_server_check_plain($value, $token_key);
      if ($value === FALSE) {
        continue;

        // don't tokenize data that can't pass check_plain
      }
      $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = $value;
    }
  }

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