Projet

Général

Profil

Paste
Télécharger (17,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_servers / ldap_servers.tokens.inc @ 7547bb19

1
<?php
2

    
3
/**
4
 * @file
5
 * collection of functions related to ldap tokens
6
 */
7

    
8
/**
9
 * @param string $attr_name such 'field_user_lname', 'name', 'mail', 'dn'
10
 * @param string $attr_type such as 'field', 'property', etc.  NULL for ldap attributes
11
 * @param string $attr_ordinal 0, 1, 2, etc.  not used in general
12
 *
13
 * @return string such as 'field.field_user_lname', 'samaccountname', etc.
14
 */
15
function ldap_servers_make_token($attr_name, $attr_type = NULL, $ordinal = NULL) {
16
  $inner_token = $attr_name;
17
  if ($attr_type) {
18
    $inner_token .= '.' . $attr_type;
19
  }
20
  if ($ordinal) {
21
    $inner_token .= ':' . $ordinal;
22
  }
23
  $token = LDAP_SERVERS_TOKEN_PRE . $inner_token . LDAP_SERVERS_TOKEN_POST;
24
  return $token;
25
}
26

    
27
/**
28
 * @param $user_attr_key of form <attr_type>.<attr_name>[:<instance>]
29
 *   such as field.lname, property.mail, field.aliases:2
30
 *
31
 * @return array array($attr_type, $attr_name, $attr_ordinal) such as array('field','field_user_lname', NULL)
32
 */
33
function ldap_servers_parse_user_attr_name($user_attr_key) {
34
  $user_attr_key = trim($user_attr_key, LDAP_SERVERS_TOKEN_PRE . LDAP_SERVERS_TOKEN_POST); // make sure no [] are on attribute
35
  $parts = explode('.', $user_attr_key);
36
  $attr_type = $parts[0];
37
  $attr_name = (isset($parts[1])) ? $parts[1] : FALSE;
38
  $attr_ordinal = FALSE;
39

    
40
  if ($attr_name) {
41
    $attr_name_parts = explode(':', $attr_name);
42
    if (isset($attr_name_parts[1])) {
43
      $attr_name = $attr_name_parts[0];
44
      $attr_ordinal = $attr_name_parts[1];
45
    }
46
  }
47
  return array($attr_type, $attr_name, $attr_ordinal);
48
}
49

    
50
/**
51
 * @param array $ldap_entry
52
 * @param string $text such as "[dn]", "[cn]@my.org", "[displayName] [sn]", "Drupal Provisioned"
53
 * @return string $text with tokens replaced or NULL if replacement not available
54
 */
55

    
56
function ldap_servers_token_replace($resource, $text, $resource_type = 'ldap_entry') { // user_account
57

    
58
  $desired_tokens = ldap_servers_token_tokens_needed_for_template($text);  // desired tokens are of form "cn","mail", etc.
59

    
60
  if (empty($desired_tokens)) {
61
    return $text; // if no tokens exist in text, return text itself.  It is literal value
62
  }
63

    
64
  switch ($resource_type) {
65
    case 'ldap_entry':
66
    $tokens = ldap_servers_token_tokenize_entry($resource, $desired_tokens, LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST);
67
    break;
68

    
69
    case 'user_account':
70
    $tokens = ldap_servers_token_tokenize_user_account($resource, $desired_tokens, LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST);
71
    break;
72
  }
73

    
74
  // add lowercase tokens to avoid case sensitivity
75
  foreach ($tokens as $attribute => $value) {
76
    $tokens[drupal_strtolower($attribute)] = $value;
77
  }
78

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

    
85
  $attributes = array_keys($tokens); //array of attributes (sn, givenname, etc)
86
  $values = array_values($tokens); //array of attribute values (Lincoln, Abe, etc)
87
  $result = str_replace($attributes, $values, $text);
88

    
89
  $result = preg_replace('/\[[^\]]*]/', '', $result);  // strip out any unreplaced tokens
90
  return ($result == '') ? NULL : $result; // return NULL if $result is empty, else $result
91
}
92

    
93
/**
94
 * @param array $attributes array of attributes passed by reference
95
 * @param string $text with tokens in it
96
 *
97
 * by reference return add ldap attribute triplet $attribute_maps[<attr_name>] = (<attr_name>, <ordinal>, <data_type>) to $attributes
98
 */
99
function ldap_servers_token_extract_attributes(&$attribute_maps,  $text) {
100
  $tokens = ldap_servers_token_tokens_needed_for_template($text);
101
  foreach ($tokens as $token) {
102
    $token = str_replace(array(LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST), array('', ''), $token);
103
    $parts = explode(LDAP_SERVERS_TOKEN_DEL, $token);
104
    $ordinal = (isset($parts[1]) && $parts[1]) ? $parts[1] : 0;
105
    $attr_name = $parts[0];
106
    $source_data_type = NULL;
107

    
108
    $parts2 = explode(LDAP_SERVERS_TOKEN_MODIFIER_DEL, $attr_name);
109
    if (count($parts2) > 1) {
110
      $attr_name = $parts2[0];
111
      $conversion = $parts2[1];
112
    }
113
    else {
114
      $conversion = NULL;
115
    }
116
    $attribute_maps[$attr_name] = ldap_servers_set_attribute_map(@$attribute_maps[$attr_name], $conversion, array($ordinal => NULL));
117
  }
118
}
119

    
120
/**
121
 * @param string $token or token expression with singular token in it, eg. [dn], [dn;binary], [titles:0;binary] [cn]@mycompany.com
122
 *
123
 *
124
 *
125
 * @return array(<attr_name>, <ordinal>, <conversion>)
126
 */
127
function ldap_servers_token_extract_parts($token) {
128
  $attributes = array();
129
  ldap_servers_token_extract_attributes($attributes, $token);
130
  if (is_array($attributes)) {
131
    $keys = array_keys($attributes);
132
    $attr_name = $keys[0];
133
    $attr_data = $attributes[$attr_name];
134
    $ordinals = array_keys($attr_data['values']);
135
    $ordinal = $ordinals[0];
136
    return array($attr_name, $ordinal, $attr_data['conversion']);
137
  }
138
  else {
139
    return array(NULL, NULL, NULL);
140
  }
141

    
142
}
143

    
144

    
145

    
146
/**
147
 * Turn an ldap entry into a token array suitable for the t() function
148
 * @param ldap entry array $ldap_entry
149
 * @param string prefix token prefix such as !,%,[
150
 * @param string suffix token suffix such as ]
151
 * @param $token_keys either an array of key names such as array('cn', 'dn') or string 'all' to return all tokens.
152
 * @return token array suitable for t() functions of with lowercase keys as exemplified below
153

    
154

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

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

    
161
should return tokens such as:
162

    
163
    -- from dn attribute
164
    [cn] = jdoe
165
    [cn:0] = jdoe
166
    [cn:last] => jdoe
167
    [ou] = campus accounts
168
    [ou:0] = campus accounts
169
    [ou:1] = toledo campus
170
    [ou:last] = toledo campus
171
    [dc] = ad
172
    [dc:0] = ad
173
    [dc:1] = myuniversity
174
    [dc:2] = edu
175
    [dc:last] = edu
176

    
177
    -- from other attributes
178
    [mail] = jdoe@myuniversity.edu
179
    [mail:0] = jdoe@myuniversity.edu
180
    [mail:last] = jdoe@myuniversity.edu
181
    [samaccountname] = jdoe
182
    [samaccountname:0] = jdoe
183
    [samaccountname:last] = jdoe
184

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

    
190
 */
191

    
192
function ldap_servers_token_tokenize_entry($ldap_entry, $token_keys = 'all', $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
193

    
194
  $detailed_watchdog_log = variable_get('ldap_help_watchdog_detail', 0);
195
  $tokens = array();
196
  $watchdog_tokens = array();
197
  if (function_exists('debug_backtrace') && $backtrace = debug_backtrace()) {
198
    $watchdog_tokens['%calling_function'] = $backtrace[1]['function'];
199
  }
200
  if (!is_array($ldap_entry)) {
201
    if ($detailed_watchdog_log) {
202
      watchdog('ldap_servers', 'skipped tokenization of ldap entry because no ldap entry provided when called from %calling_function.', $watchdog_tokens, WATCHDOG_DEBUG);
203
    }
204
    return $tokens; // empty array
205
  }
206
      
207
  // add lowercase keyed entries to ldap array
208
  foreach ($ldap_entry as $key => $values) {
209
    $ldap_entry[drupal_strtolower($key)] = $values;
210
  }
211

    
212
  // 1. tokenize dn
213
  $dn_parts = ldap_explode_dn($ldap_entry['dn'], 0); // escapes attribute values, need to be unescaped later.
214
  unset($dn_parts['count']);
215
  $parts_count = array();
216
  $parts_last_value = array();
217
  foreach ($dn_parts as $pair) {
218
    list($attr_name, $attr_value) = explode('=', $pair);
219
    $attr_value = ldap_pear_unescape_dn_value($attr_value);
220
    try {
221
      $attr_value = check_plain($attr_value);
222
    }
223
    catch (Exception $e) {
224
      if ($detailed_watchdog_log) {
225
        $watchdog_tokens['%attr_name'] = $attr_name;
226
        watchdog('ldap_servers', 'skipped tokenization of attribute %attr_name because the value would not pass check_plain function.', $watchdog_tokens, WATCHDOG_DEBUG);
227
      }
228
      continue; // don't tokenize data that can't pass check_plain
229
    }
230
    if (!isset($parts_count[$attr_name])) {
231
      $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = $attr_value;
232
      $parts_count[$attr_name] = 0;
233
    }
234
    $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;
235

    
236
    $parts_last_value[$attr_name] = $attr_value;
237
    $parts_count[$attr_name]++;
238
  }
239

    
240
  foreach ($parts_count as $attr_name => $count) {
241
    $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];
242
  }
243

    
244
  // tokenize other attributes
245
  if ($token_keys == 'all') {
246
    $token_keys = array_keys($ldap_entry);
247
    $token_keys = array_filter($token_keys, "is_string");
248
    foreach ($token_keys as $attr_name) {
249
      $attr_value = $ldap_entry[$attr_name];
250
      if (is_array($attr_value) && is_scalar($attr_value[0]) && $attr_value['count'] == 1) {
251
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = check_plain($attr_value[0]);
252
        $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]);
253
        $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]);
254
      }
255
      elseif (is_array($attr_value) && $attr_value['count'] > 1) {
256
        $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]);
257
        for ($i=0; $i<$attr_value['count']; $i++) {
258
          $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]);
259
        }
260
      }
261
      elseif (is_scalar($attr_value)) {
262
        $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = check_plain($attr_value);
263
        $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);
264
        $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);
265
      }
266
    }
267
  }
268
  else {
269
    foreach ($token_keys as $full_token_key) {
270
      // $token_key = 'dn', 'mail', 'mail:0', 'mail:last', 'dept:1', 'guid:0;tobase64etc.
271
      $value = NULL;
272

    
273
      $conversion = FALSE;
274
      $parts = explode(';', $full_token_key);
275
      if (count($parts) == 2) {
276
        $conversion = $parts[1];
277
        $token_key = $parts[0];
278
      }
279
      else {
280
        $token_key = $full_token_key;
281
      }
282

    
283
      $parts = explode(LDAP_SERVERS_TOKEN_DEL, $token_key);
284
      $attr_name = drupal_strtolower($parts[0]);
285
      $ordinal_key = isset($parts[1]) ? $parts[1] : 0;
286
      $i = NULL;
287

    
288
      if ($attr_name == 'dn' || !isset($ldap_entry[$attr_name])) { // don't use empty() since a 0, "", etc value may be a desired value
289
        continue;
290
      }
291
      else {
292
        $count = $ldap_entry[$attr_name]['count'];
293
        if ($ordinal_key === 'last') {
294
          $i = ($count > 0) ? $count - 1 : 0;
295
          $value = $ldap_entry[$attr_name][$i];
296
        }
297
        elseif (is_numeric($ordinal_key) || $ordinal_key == '0') {
298
          $value = $ldap_entry[$attr_name][$ordinal_key];
299
        }
300
        else {
301
          continue;  // don't add token if case not covered
302
        }
303
      }
304

    
305
      if ($conversion) {
306
        switch ($conversion) {
307

    
308
          case 'base64_encode':
309
            $value = base64_encode($value);
310
            break;
311

    
312
          case 'bin2hex':
313
            $value = bin2hex($value);
314
            break;
315

    
316
          case 'msguid':
317
            $value = ldap_servers_msguid($value);
318
            break;
319

    
320
          case 'binary':
321
            $value = ldap_servers_binary($value);
322
            break;
323
        }
324
      }
325

    
326

    
327
      $tokens[$pre . $full_token_key . $post] = $value;
328
      if ($full_token_key != drupal_strtolower($full_token_key)) {
329
        $tokens[$pre . drupal_strtolower($full_token_key) . $post] = $value;
330
      }
331
    }
332
  }
333

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

    
339
/**
340
 *
341
 * @param drupal user object $user_account
342
 * @param array or 'all' $token_keys 'all' signifies return
343
 *   all token/value pairs available; otherwise array lists
344
 *   token keys (e.g. property.name ...NOT [property.name])
345
 * @param string $pre prefix of token
346
 * @param string $post suffix of token
347
 *
348
 *
349
 * @return should return token/value pairs in array such as
350
 *   'status' => 1
351
 *   'uid' => 17
352
 */
353

    
354
function ldap_servers_token_tokenize_user_account($user_account, $token_keys = 'all', $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
355

    
356
  $detailed_watchdog_log = variable_get('ldap_help_watchdog_detail', 0);
357
  $tokens = array();
358

    
359
  $user_entered_password_available = (boolean)ldap_user_ldap_provision_pwd('get');
360
  // ldapUserPwd((property_exists($user_account, 'ldapUserPwd') && $user_account->ldapUserPwd));
361

    
362
  if ($token_keys == 'all') {
363
    // add lowercase keyed entries to ldap array
364
    foreach ((array)$user_account as $property_name => $value) {
365
      if (is_scalar($value) && $property_name != 'password') {
366
        $token_keys[] = 'property.' . $property_name;
367
        if (drupal_strtolower($property_name) != $property_name) {
368
          $token_keys[] = 'property.' . drupal_strtolower($property_name);
369
        }
370
      }
371
      elseif (isset($user_account->{$attr_name}[LANGUAGE_NONE][0]['value']) && is_scalar($user_account->{$attr_name}[LANGUAGE_NONE][0]['value'])) {
372
        $token_keys[] = 'field.' . $property_name;
373
        if (drupal_strtolower($property_name) != $property_name) {
374
          $token_keys[] = 'field.' . drupal_strtolower($property_name);
375
        }
376
      }
377
      else {
378
        // field or property with no value, so no token can be generated
379
      }
380
    }
381
    $ldap_user_conf_admin = new LdapUserConfAdmin();
382
    if ($ldap_user_conf->setsLdapPassword) {
383
      $token_keys[] = 'password.random';
384
      $token_keys[] = 'password.user-random';
385
    }
386
  }
387

    
388
  foreach ($token_keys as $token_key) {
389
    $parts = explode('.', $token_key);
390
    $attr_type = $parts[0];
391
    $attr_name = $parts[1];
392
    $attr_conversion = (isset($parts[2])) ? $parts[2] : 'none';
393
    $value = FALSE;
394
    $skip = FALSE;
395

    
396
    switch ($attr_type) {
397
      case 'field':
398
        $value = @is_scalar($user_account->{$attr_name}[LANGUAGE_NONE][0]['value']) ? $user_account->{$attr_name}[LANGUAGE_NONE][0]['value'] : '';
399
      break;
400

    
401
      case 'property':
402
        $value = @is_scalar($user_account->{$attr_name}) ? $user_account->{$attr_name} : '';
403
      break;
404

    
405
      case 'password':
406

    
407
        switch ($attr_name) {
408

    
409
          case 'user':
410
          case 'user-only':
411
            $pwd = ldap_user_ldap_provision_pwd('get');
412
            $value = ($pwd) ? $pwd : NULL;
413
            break;
414

    
415
          case 'user-random':
416
            $pwd = ldap_user_ldap_provision_pwd('get');
417
            $value = ($pwd) ? $pwd : user_password();
418
            break;
419

    
420
          case 'random':
421
            $value = user_password();
422
            break;
423

    
424
        }
425
        if (empty($value)) {
426
          $skip = TRUE;
427
        }
428
      break;
429
    }
430

    
431
    if (!$skip) {
432

    
433
      switch ($attr_conversion) {
434

    
435
        case 'none':
436
          break;
437

    
438
        case 'to-md5':
439
          $value = "{MD5}" . base64_encode( pack( "H*", md5( $value ) ) );
440
          break;
441

    
442
        case 'to-lowercase':
443
          $value = drupal_strtolower($value);
444
          break;
445
      }
446

    
447
      $tokens[$pre . $token_key . $post] = ($attr_type == 'password') ? $value : check_plain($value);
448
      if ($token_key != drupal_strtolower($token_key)) {
449
        $tokens[$pre . drupal_strtolower($token_key) . $post] = ($attr_type == 'password') ? $value : check_plain($value);
450
      }
451
    }
452
  }
453
  return $tokens;
454
}
455

    
456
/**
457
 * @param string $template in form [cn]@myuniversity.edu
458
 * @return array of all tokens in the template such as array('cn')
459
 */
460
function ldap_servers_token_tokens_needed_for_template($template, $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
461
  preg_match_all('/
462
    \[             # [ - pattern start
463
    ([^\[\]]*)  # match $type not containing whitespace : [ or ]
464
    \]             # ] - pattern end
465
    /x', $template, $matches);
466

    
467
  return @$matches[1];
468

    
469
}
470

    
471
function ldap_servers_token_show_sample_user_tokens($sid) {
472

    
473
  $ldap_server = ldap_servers_get_servers($sid, 'all', TRUE);
474
  $test_username = $ldap_server->testingDrupalUsername;
475
  if (!$test_username || ! (
476
    $ldap_server->bind_method == LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT ||
477
    $ldap_server->bind_method == LDAP_SERVERS_BIND_METHOD_ANON
478
      )
479
    ) {
480
    return FALSE;
481
  }
482

    
483
  if ($ldap_user = $ldap_server->userUserNameToExistingLdapEntry($test_username)) {
484
    $table = theme('ldap_server_ldap_entry_table', array(
485
      'entry' => $ldap_user['attr'],
486
      'username' => $test_username,
487
      'dn' => $ldap_user['dn'],
488
      ));
489
  }
490
  else {
491
    $table = '<p>' . t('No sample user data found') . '</p>';
492
  }
493

    
494
  return $table;
495
}