Projet

Général

Profil

Révision 32700c57

Ajouté par Assos Assos il y a environ 5 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/ldap/ldap_servers/ldap_servers.tokens.inc
2 2

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

  
8 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
9
 * @param string $attr_name
10
 *   such 'field_user_lname', 'name', 'mail', 'dn'.
11
 * @param string $attr_type
12
 *   such as 'field', 'property', etc.  NULL for ldap attributes.
13
 * @param string $attr_ordinal
14
 *   0, 1, 2, etc.  not used in general.
12 15
 *
13 16
 * @return string such as 'field.field_user_lname', 'samaccountname', etc.
14 17
 */
......
25 28
}
26 29

  
27 30
/**
28
 * @param $user_attr_key of form <attr_type>.<attr_name>[:<instance>]
31
 * @param $user_attr_key
32
 *   of form <attr_type>.<attr_name>[:<instance>]
29 33
 *   such as field.lname, property.mail, field.aliases:2
30 34
 *
31 35
 * @return array array($attr_type, $attr_name, $attr_ordinal) such as array('field','field_user_lname', NULL)
32 36
 */
33 37
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
38
  // Make sure no [] are on attribute.
39
  $user_attr_key = trim($user_attr_key, LDAP_SERVERS_TOKEN_PRE . LDAP_SERVERS_TOKEN_POST);
35 40
  $parts = explode('.', $user_attr_key);
36 41
  $attr_type = $parts[0];
37 42
  $attr_name = (isset($parts[1])) ? $parts[1] : FALSE;
......
44 49
      $attr_ordinal = $attr_name_parts[1];
45 50
    }
46 51
  }
47
  return array($attr_type, $attr_name, $attr_ordinal);
52
  return [$attr_type, $attr_name, $attr_ordinal];
48 53
}
49 54

  
50 55
/**
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
56
 * @param array
57
 * @param string $text
58
 *
59
 * @return string
54 60
 */
55 61

  
56
function ldap_servers_token_replace($resource, $text, $resource_type = 'ldap_entry') { // user_account
62
/**
63
 * User_account.
64
 *
65
 * @param $resource
66
 *   $ldap_entry
67
 * @param $text
68
 *   such as "[dn]", "[cn]@my.org", "[displayName] [sn]", "Drupal Provisioned"
69
 * @param string $resource_type
70
 *
71
 * @return string|string[]|null
72
 *   $text with tokens replaced or NULL if replacement not available
73
 */
74
function ldap_servers_token_replace($resource, $text, $resource_type = 'ldap_entry') {
57 75

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

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

  
84
  $tokens = [];
85

  
86
  // @TODO: Should really be if/else if only those two exist.
64 87
  switch ($resource_type) {
65 88
    case 'ldap_entry':
66
    $tokens = ldap_servers_token_tokenize_entry($resource, $desired_tokens, LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST);
67
    break;
89
      $tokens = ldap_servers_token_tokenize_entry($resource, $desired_tokens, LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST);
90
      break;
68 91

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

  
74
  // add lowercase tokens to avoid case sensitivity
97
  // Add lowercase tokens to avoid case sensitivity.
75 98
  foreach ($tokens as $attribute => $value) {
76 99
    $tokens[drupal_strtolower($attribute)] = $value;
77 100
  }
......
82 105
    $tokens[drupal_strtolower($text)] = '';
83 106
  }
84 107

  
85
  $attributes = array_keys($tokens); //array of attributes (sn, givenname, etc)
86
  $values = array_values($tokens); //array of attribute values (Lincoln, Abe, etc)
108
  // Array of attributes (sn, givenname, etc)
109
  $attributes = array_keys($tokens);
110
  // Array of attribute values (Lincoln, Abe, etc)
111
  $values = array_values($tokens);
87 112
  $result = str_replace($attributes, $values, $text);
88 113

  
89
  $result = preg_replace('/\[[^\]]*]/', '', $result);  // strip out any unreplaced tokens
90
  return ($result == '') ? NULL : $result; // return NULL if $result is empty, else $result
114
  // Strip out any unreplaced tokens.
115
  $result = preg_replace('/\[[^\]]*]/', '', $result);
116
  // Return NULL if $result is empty, else $result.
117
  return ($result == '') ? NULL : $result;
91 118
}
92 119

  
93 120
/**
94
 * @param array $attributes array of attributes passed by reference
95
 * @param string $text with tokens in it
121
 * @param array $attributes
122
 *   array of attributes passed by reference.
123
 * @param string $text
124
 *   with tokens in it
96 125
 *
97
 * by reference return add ldap attribute triplet $attribute_maps[<attr_name>] = (<attr_name>, <ordinal>, <data_type>) to $attributes
126
 *   by reference return add ldap attribute triplet $attribute_maps[<attr_name>] = (<attr_name>, <ordinal>, <data_type>) to $attributes.
98 127
 */
99 128
function ldap_servers_token_extract_attributes(&$attribute_maps, $text) {
100 129
  $tokens = ldap_servers_token_tokens_needed_for_template($text);
101 130
  foreach ($tokens as $token) {
102
    $token = str_replace(array(LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST), array('', ''), $token);
131
    $token = str_replace([LDAP_SERVERS_TOKEN_PRE, LDAP_SERVERS_TOKEN_POST], ['', ''], $token);
103 132
    $parts = explode(LDAP_SERVERS_TOKEN_DEL, $token);
104 133
    $ordinal = (isset($parts[1]) && $parts[1]) ? $parts[1] : 0;
105 134
    $attr_name = $parts[0];
......
113 142
    else {
114 143
      $conversion = NULL;
115 144
    }
116
    $attribute_maps[$attr_name] = ldap_servers_set_attribute_map(@$attribute_maps[$attr_name], $conversion, array($ordinal => NULL));
145
    $attribute_maps[$attr_name] = ldap_servers_set_attribute_map(@$attribute_maps[$attr_name], $conversion, [$ordinal => NULL]);
117 146
  }
118 147
}
119 148

  
120 149
/**
121
 * @param string $token or token expression with singular token in it, eg. [dn], [dn;binary], [titles:0;binary] [cn]@mycompany.com
122
 *
123
 *
150
 * @param string $token
151
 *   or token expression with singular token in it, eg. [dn], [dn;binary], [titles:0;binary] [cn]@mycompany.com.
124 152
 *
125 153
 * @return array(<attr_name>, <ordinal>, <conversion>)
126 154
 */
127 155
function ldap_servers_token_extract_parts($token) {
128
  $attributes = array();
156
  $attributes = [];
129 157
  ldap_servers_token_extract_attributes($attributes, $token);
130 158
  if (is_array($attributes)) {
131 159
    $keys = array_keys($attributes);
......
133 161
    $attr_data = $attributes[$attr_name];
134 162
    $ordinals = array_keys($attr_data['values']);
135 163
    $ordinal = $ordinals[0];
136
    return array($attr_name, $ordinal, $attr_data['conversion']);
164
    return [$attr_name, $ordinal, $attr_data['conversion']];
137 165
  }
138 166
  else {
139
    return array(NULL, NULL, NULL);
167
    return [NULL, NULL, NULL];
140 168
  }
141 169

  
142 170
}
143 171

  
144

  
145

  
146 172
/**
147
 * Turn an ldap entry into a token array suitable for the t() function
173
 * Turn an ldap entry into a token array suitable for the t() function.
174
 *
148 175
 * @param ldap entry array $ldap_entry
149 176
 * @param string prefix token prefix such as !,%,[
150 177
 * @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.
178
 * @param $token_keys
179
 *   either an array of key names such as array('cn', 'dn') or string 'all' to return all tokens.
180
 *
152 181
 * @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
    [cn:reverse:0] = jdoe
168
    [ou] = campus accounts
169
    [ou:0] = campus accounts
170
    [ou:1] = toledo campus
171
    [ou:last] = toledo campus
172
    [ou:reverse:0] = toledo campus
173
    [ou:reverse:1] = campus accounts
174
    [dc] = ad
175
    [dc:0] = ad
176
    [dc:1] = myuniversity
177
    [dc:2] = edu
178
    [dc:last] = edu
179
    [dc:reverse:0] = edu
180
    [dc:reverse:1] = myuniversity
181
    [dc:reverse:2] = ad
182

  
183
    -- from other attributes
184
    [mail] = jdoe@myuniversity.edu
185
    [mail:0] = jdoe@myuniversity.edu
186
    [mail:last] = jdoe@myuniversity.edu
187
    [samaccountname] = jdoe
188
    [samaccountname:0] = jdoe
189
    [samaccountname:last] = jdoe
190

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

  
182
 *
183
 *   $ldap_entry should be in form of single entry returned from ldap_search() function:
184
 *
185
 *   'dn' => 'cn=jdoe,ou=campus accounts,ou=toledo campus,dc=ad,dc=myuniversity,dc=edu',
186
 *   'mail' => array( 0 => 'jdoe@myuniversity.edu', 'count' => 1),
187
 *   'sAMAccountName' => array( 0 => 'jdoe', 'count' => 1),
188
 *   should return tokens such as:
189
 *
190
 *   -- from dn attribute
191
 *   [cn] = jdoe
192
 *   [cn:0] = jdoe
193
 *   [cn:last] => jdoe
194
 *   [cn:reverse:0] = jdoe
195
 *   [ou] = campus accounts
196
 *   [ou:0] = campus accounts
197
 *   [ou:1] = toledo campus
198
 *   [ou:last] = toledo campus
199
 *   [ou:reverse:0] = toledo campus
200
 *   [ou:reverse:1] = campus accounts
201
 *   [dc] = ad
202
 *   [dc:0] = ad
203
 *   [dc:1] = myuniversity
204
 *   [dc:2] = edu
205
 *   [dc:last] = edu
206
 *   [dc:reverse:0] = edu
207
 *   [dc:reverse:1] = myuniversity
208
 *   [dc:reverse:2] = ad
209
 *
210
 *   -- from other attributes
211
 *   [mail] = jdoe@myuniversity.edu
212
 *   [mail:0] = jdoe@myuniversity.edu
213
 *   [mail:last] = jdoe@myuniversity.edu
214
 *   [samaccountname] = jdoe
215
 *   [samaccountname:0] = jdoe
216
 *   [samaccountname:last] = jdoe
217
 *
218
 *   [guid:0;base64_encode] = apply base64_encode() function to value
219
 *   [guid:0;bin2hex] = apply bin2hex() function to value
220
 *   [guid:0;msguid] = apply ldap_servers_msguid() function to value
221
 *   [guid:0;binary] = apply ldap_servers_binary() function to value. this is the most generic binary function
196 222
 */
197

  
198 223
function ldap_servers_token_tokenize_entry($ldap_entry, $token_keys = 'all', $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
199 224

  
200 225
  $detailed_watchdog_log = variable_get('ldap_help_watchdog_detail', 0);
201
  $tokens = array();
202
  $watchdog_tokens = array();
226
  $tokens = [];
227
  $watchdog_tokens = [];
203 228
  if (function_exists('debug_backtrace') && $backtrace = debug_backtrace()) {
204 229
    $watchdog_tokens['%calling_function'] = $backtrace[1]['function'];
205 230
  }
......
207 232
    if ($detailed_watchdog_log) {
208 233
      watchdog('ldap_servers', 'skipped tokenization of ldap entry because no ldap entry provided when called from %calling_function.', $watchdog_tokens, WATCHDOG_DEBUG);
209 234
    }
210
    return $tokens; // empty array
235
    // Empty array.
236
    return $tokens;
211 237
  }
212
      
213
  // add lowercase keyed entries to ldap array
238

  
239
  // Add lowercase keyed entries to ldap array.
214 240
  foreach ($ldap_entry as $key => $values) {
215 241
    $ldap_entry[drupal_strtolower($key)] = $values;
216 242
  }
217 243

  
218 244
  // 1. tokenize dn
219
  $dn_parts = ldap_explode_dn($ldap_entry['dn'], 0); // escapes attribute values, need to be unescaped later.
245
  // escapes attribute values, need to be unescaped later.
246
  $dn_parts = ldap_explode_dn($ldap_entry['dn'], 0);
220 247
  unset($dn_parts['count']);
221
  $parts_count = array();
222
  $parts_last_value = array();
248
  $parts_count = [];
249
  $parts_last_value = [];
223 250
  foreach ($dn_parts as $pair) {
224 251
    list($attr_name, $attr_value) = explode('=', $pair);
225 252
    $attr_value = ldap_pear_unescape_dn_value($attr_value);
......
231 258
        $watchdog_tokens['%attr_name'] = $attr_name;
232 259
        watchdog('ldap_servers', 'skipped tokenization of attribute %attr_name because the value would not pass check_plain function.', $watchdog_tokens, WATCHDOG_DEBUG);
233 260
      }
234
      continue; // don't tokenize data that can't pass check_plain
261
      // don't tokenize data that can't pass check_plain.
262
      continue;
235 263
    }
236 264
    if (!isset($parts_count[$attr_name])) {
237 265
      $tokens[$pre . ldap_server_massage_text($attr_name, 'attr_name', LDAP_SERVER_MASSAGE_TOKEN_REPLACE) . $post] = $attr_value;
238 266
      $parts_count[$attr_name] = 0;
239 267
    }
240
    $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;
268
    $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;
241 269

  
242 270
    $parts_last_value[$attr_name] = $attr_value;
243 271
    $parts_count[$attr_name]++;
......
256 284
    $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];
257 285
  }
258 286

  
259
  // tokenize other attributes
287
  // Tokenize other attributes.
260 288
  if ($token_keys == 'all') {
261 289
    $token_keys = array_keys($ldap_entry);
262 290
    $token_keys = array_filter($token_keys, "is_string");
......
282 310
  }
283 311
  else {
284 312
    foreach ($token_keys as $full_token_key) {
285
      // $token_key = 'dn', 'mail', 'mail:0', 'mail:last', 'dept:1', 'guid:0;tobase64etc.
313
      // Token key = 'dn', 'mail', 'mail:0', 'mail:last', 'dept:1', 'guid:0' etc.
286 314
      $value = NULL;
287 315

  
288 316
      $conversion = FALSE;
......
300 328
      $ordinal_key = isset($parts[1]) ? $parts[1] : 0;
301 329
      $i = NULL;
302 330

  
303
      if ($attr_name == 'dn' || !isset($ldap_entry[$attr_name])) { // don't use empty() since a 0, "", etc value may be a desired value
331
      // don't use empty() since a 0, "", etc value may be a desired value.
332
      if ($attr_name == 'dn' || !isset($ldap_entry[$attr_name])) {
304 333
        continue;
305 334
      }
306 335
      else {
......
313 342
          $value = $ldap_entry[$attr_name][$ordinal_key];
314 343
        }
315 344
        else {
316
          continue;  // don't add token if case not covered
345
          // don't add token if case not covered.
346
          continue;
317 347
        }
318 348
      }
319 349

  
......
338 368
        }
339 369
      }
340 370

  
341

  
342 371
      $tokens[$pre . $full_token_key . $post] = $value;
343 372
      if ($full_token_key != drupal_strtolower($full_token_key)) {
344 373
        $tokens[$pre . drupal_strtolower($full_token_key) . $post] = $value;
......
346 375
    }
347 376
  }
348 377

  
349
  // include the dn.  it will not be handled correctly by previous loops
378
  // Include the dn.  it will not be handled correctly by previous loops.
350 379
  $tokens[$pre . 'dn' . $post] = check_plain($ldap_entry['dn']);
351 380
  return $tokens;
352 381
}
......
354 383
/**
355 384
 *
356 385
 * @param drupal user object $user_account
357
 * @param array or 'all' $token_keys 'all' signifies return
386
 * @param array or 'all' $token_keys
387
 *   'all' signifies return
358 388
 *   all token/value pairs available; otherwise array lists
359 389
 *   token keys (e.g. property.name ...NOT [property.name])
360
 * @param string $pre prefix of token
361
 * @param string $post suffix of token
390
 * @param string $pre
391
 *   prefix of token.
392
 * @param string $post
393
 *   suffix of token.
362 394
 *
363 395
 *
364 396
 * @return should return token/value pairs in array such as
365 397
 *   'status' => 1
366 398
 *   'uid' => 17
367 399
 */
368

  
369 400
function ldap_servers_token_tokenize_user_account($user_account, $token_keys = 'all', $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
370 401

  
371 402
  $detailed_watchdog_log = variable_get('ldap_help_watchdog_detail', 0);
372
  $tokens = array();
373

  
374
  $user_entered_password_available = (boolean)ldap_user_ldap_provision_pwd('get');
375
  // ldapUserPwd((property_exists($user_account, 'ldapUserPwd') && $user_account->ldapUserPwd));
403
  $tokens = [];
376 404

  
405
  $user_entered_password_available = (boolean) ldap_user_ldap_provision_pwd('get');
377 406
  if ($token_keys == 'all') {
378
    // add lowercase keyed entries to ldap array
379
    foreach ((array)$user_account as $property_name => $value) {
407
    // Add lowercase keyed entries to ldap array.
408
    foreach ((array) $user_account as $property_name => $value) {
380 409
      if (is_scalar($value) && $property_name != 'password') {
381 410
        $token_keys[] = 'property.' . $property_name;
382 411
        if (drupal_strtolower($property_name) != $property_name) {
......
390 419
        }
391 420
      }
392 421
      else {
393
        // field or property with no value, so no token can be generated
422
        // Field or property with no value, so no token can be generated.
394 423
      }
395 424
    }
396 425
    $ldap_user_conf_admin = new LdapUserConfAdmin();
397
    if ($ldap_user_conf->setsLdapPassword) {
426
    if ($ldap_user_conf_admin->setsLdapPassword) {
398 427
      $token_keys[] = 'password.random';
399 428
      $token_keys[] = 'password.user-random';
400 429
    }
......
411 440
    switch ($attr_type) {
412 441
      case 'field':
413 442
        $value = @is_scalar($user_account->{$attr_name}[LANGUAGE_NONE][0]['value']) ? $user_account->{$attr_name}[LANGUAGE_NONE][0]['value'] : '';
414
      break;
443
        break;
415 444

  
416 445
      case 'property':
417 446
        $value = @is_scalar($user_account->{$attr_name}) ? $user_account->{$attr_name} : '';
418
      break;
447
        break;
419 448

  
420 449
      case 'password':
421 450

  
......
440 469
        if (empty($value)) {
441 470
          $skip = TRUE;
442 471
        }
443
      break;
472
        break;
444 473
    }
445 474

  
446 475
    if (!$skip) {
......
451 480
          break;
452 481

  
453 482
        case 'to-md5':
454
          $value = "{MD5}" . base64_encode( pack( "H*", md5( $value ) ) );
483
          $value = "{MD5}" . base64_encode(pack("H*", md5($value)));
455 484
          break;
456 485

  
457 486
        case 'to-lowercase':
......
469 498
}
470 499

  
471 500
/**
472
 * @param string $template in form [cn]@myuniversity.edu
501
 * @param string $template
502
 *   in form [cn]@myuniversity.edu.
473 503
 * @return array of all tokens in the template such as array('cn')
474 504
 */
475 505
function ldap_servers_token_tokens_needed_for_template($template, $pre = LDAP_SERVERS_TOKEN_PRE, $post = LDAP_SERVERS_TOKEN_POST) {
......
483 513

  
484 514
}
485 515

  
516
/**
517
 *
518
 */
486 519
function ldap_servers_token_show_sample_user_tokens($sid) {
487 520

  
488 521
  $ldap_server = ldap_servers_get_servers($sid, 'all', TRUE);
489 522
  $test_username = $ldap_server->testingDrupalUsername;
490
  if (!$test_username || ! (
523
  if (!$test_username || !(
491 524
    $ldap_server->bind_method == LDAP_SERVERS_BIND_METHOD_SERVICE_ACCT ||
492 525
    $ldap_server->bind_method == LDAP_SERVERS_BIND_METHOD_ANON
493 526
      )
......
496 529
  }
497 530

  
498 531
  if ($ldap_user = $ldap_server->userUserNameToExistingLdapEntry($test_username)) {
499
    $table = theme('ldap_server_ldap_entry_table', array(
532
    $table = theme('ldap_server_ldap_entry_table', [
500 533
      'entry' => $ldap_user['attr'],
501 534
      'username' => $test_username,
502 535
      'dn' => $ldap_user['dn'],
503
      ));
536
    ]);
504 537
  }
505 538
  else {
506 539
    $table = '<p>' . t('No sample user data found') . '</p>';

Formats disponibles : Unified diff