You are here

function cas_ldap_tokens in CAS Attributes 7

Implements hook_tokens().

File

./cas_ldap.tokens.inc, line 22
Token module integration.

Code

function cas_ldap_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'cas' && !empty($data['cas'])) {
    $cas = $data['cas'];

    // Provide [cas:attribute:?] dynamic tokens.
    if ($attribute_tokens = token_find_with_prefix($tokens, 'ldap')) {
      $attribute = cas_ldap_attributes($cas);
      foreach ($attribute_tokens as $name => $original) {
        $name = drupal_strtolower($name);
        if (isset($attribute[$name])) {
          $value = $attribute[$name];
          if (is_array($value)) {
            $value = $value[0];
          }

          // Values that are not valid UTF-8 cannot be run through
          // check_plain(). Convert these to a string. Normally, this would be a
          // GUID, so format as such.
          if ($sanitize && !drupal_validate_utf8($value)) {
            $value = cas_ldap_guid_bin2hex($value);
          }
          $replacements[$original] = $sanitize ? check_plain($value) : $value;
        }
        elseif ($name == '?') {
          $keys = array_keys($attribute);
          if ($sanitize) {
            $keys = array_map('check_plain', $keys);
          }
          $replacements[$original] = t('Available attributes: %keys', array(
            '%keys' => implode(', ', $keys),
          ));
        }
      }
    }
  }
  return $replacements;
}