| Total Complexity | 115 |
| Total Lines | 883 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Ldap often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Ldap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Ldap |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * LDAP link identifier. |
||
| 38 | * |
||
| 39 | * @var resource |
||
| 40 | */ |
||
| 41 | protected $ldap; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * LDAP user: authz_id if SASL is in use, binding dn otherwise |
||
| 45 | * |
||
| 46 | * @var string|null |
||
| 47 | */ |
||
| 48 | protected $authz_id = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Timeout value, in seconds. |
||
| 52 | * |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | protected $timeout = 0; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Private constructor restricts instantiation to getInstance(). |
||
| 59 | * |
||
| 60 | * @param string $hostname |
||
| 61 | * @param bool $enable_tls |
||
| 62 | * @param bool $debug |
||
| 63 | * @param int $timeout |
||
| 64 | * @param int $port |
||
| 65 | * @param bool $referrals |
||
| 66 | * @psalm-suppress NullArgument |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 69 | $hostname, |
||
| 70 | $enable_tls = true, |
||
| 71 | $debug = false, |
||
| 72 | $timeout = 0, |
||
| 73 | $port = 389, |
||
| 74 | $referrals = true |
||
| 75 | ) { |
||
| 76 | // Debug |
||
| 77 | Logger::debug('Library - LDAP __construct(): Setup LDAP with '. |
||
| 78 | 'host=\''.$hostname. |
||
| 79 | '\', tls='.var_export($enable_tls, true). |
||
| 80 | ', debug='.var_export($debug, true). |
||
| 81 | ', timeout='.var_export($timeout, true). |
||
| 82 | ', referrals='.var_export($referrals, true)); |
||
| 83 | |||
| 84 | /* |
||
| 85 | * Set debug level before calling connect. Note that this passes |
||
| 86 | * NULL to ldap_set_option, which is an undocumented feature. |
||
| 87 | * |
||
| 88 | * OpenLDAP 2.x.x or Netscape Directory SDK x.x needed for this option. |
||
| 89 | */ |
||
| 90 | if ($debug && !ldap_set_option(null, LDAP_OPT_DEBUG_LEVEL, 7)) { |
||
| 91 | Logger::warning('Library - LDAP __construct(): Unable to set debug level (LDAP_OPT_DEBUG_LEVEL) to 7'); |
||
| 92 | } |
||
| 93 | |||
| 94 | /* |
||
| 95 | * Prepare a connection for to this LDAP server. Note that this function |
||
| 96 | * doesn't actually connect to the server. |
||
| 97 | */ |
||
| 98 | $resource = @ldap_connect($hostname, $port); |
||
| 99 | if ($resource === false) { |
||
| 100 | throw $this->makeException( |
||
| 101 | 'Library - LDAP __construct(): Unable to connect to \''.$hostname.'\'', |
||
| 102 | ERR_INTERNAL |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | $this->ldap = $resource; |
||
| 106 | |||
| 107 | // Enable LDAP protocol version 3 |
||
| 108 | if (!@ldap_set_option($this->ldap, LDAP_OPT_PROTOCOL_VERSION, 3)) { |
||
| 109 | throw $this->makeException( |
||
| 110 | 'Library - LDAP __construct(): Failed to set LDAP Protocol version (LDAP_OPT_PROTOCOL_VERSION) to 3', |
||
| 111 | ERR_INTERNAL |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | // Set referral option |
||
| 116 | if (!@ldap_set_option($this->ldap, LDAP_OPT_REFERRALS, $referrals)) { |
||
| 117 | throw $this->makeException( |
||
| 118 | 'Library - LDAP __construct(): Failed to set LDAP Referrals (LDAP_OPT_REFERRALS) to '.$referrals, |
||
| 119 | ERR_INTERNAL |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | // Set timeouts, if supported |
||
| 124 | // (OpenLDAP 2.x.x or Netscape Directory SDK x.x needed) |
||
| 125 | $this->timeout = $timeout; |
||
| 126 | if ($timeout > 0) { |
||
| 127 | if (!@ldap_set_option($this->ldap, LDAP_OPT_NETWORK_TIMEOUT, $timeout)) { |
||
| 128 | Logger::warning( |
||
| 129 | 'Library - LDAP __construct(): Unable to set timeouts (LDAP_OPT_NETWORK_TIMEOUT) to '.$timeout |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | if (!@ldap_set_option($this->ldap, LDAP_OPT_TIMELIMIT, $timeout)) { |
||
| 133 | Logger::warning( |
||
| 134 | 'Library - LDAP __construct(): Unable to set timeouts (LDAP_OPT_TIMELIMIT) to '.$timeout |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // Enable TLS, if needed |
||
| 140 | if (stripos($hostname, "ldaps:") === false && $enable_tls) { |
||
| 141 | if (!@ldap_start_tls($this->ldap)) { |
||
| 142 | throw $this->makeException('Library - LDAP __construct():'. |
||
| 143 | ' Unable to force TLS', ERR_INTERNAL); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * Convenience method to create an LDAPException as well as log the |
||
| 151 | * description. |
||
| 152 | * |
||
| 153 | * @param string $description The exception's description |
||
| 154 | * @param int|null $type The exception's type |
||
| 155 | * @return \Exception |
||
| 156 | */ |
||
| 157 | private function makeException($description, $type = null) |
||
| 158 | { |
||
| 159 | $errNo = @ldap_errno($this->ldap); |
||
| 160 | |||
| 161 | // Decide exception type and return |
||
| 162 | if ($type !== null) { |
||
| 163 | if ($errNo !== 0) { |
||
| 164 | // Only log real LDAP errors; not success |
||
| 165 | Logger::error($description.'; cause: \''.ldap_error($this->ldap).'\' (0x'.dechex($errNo).')'); |
||
| 166 | } else { |
||
| 167 | Logger::error($description); |
||
| 168 | } |
||
| 169 | |||
| 170 | switch ($type) { |
||
| 171 | case ERR_INTERNAL:// 1 - ExInternal |
||
| 172 | return new Error\Exception($description, $errNo); |
||
| 173 | case ERR_NO_USER:// 2 - ExUserNotFound |
||
| 174 | return new Error\UserNotFound($description, $errNo); |
||
| 175 | case ERR_WRONG_PW:// 3 - ExInvalidCredential |
||
| 176 | return new Error\InvalidCredential($description, $errNo); |
||
| 177 | case ERR_AS_DATA_INCONSIST:// 4 - ExAsDataInconsist |
||
| 178 | return new Error\AuthSource('ldap', $description); |
||
| 179 | case ERR_AS_INTERNAL:// 5 - ExAsInternal |
||
| 180 | return new Error\AuthSource('ldap', $description); |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | if ($errNo !== 0) { |
||
| 184 | $description .= '; cause: \''.ldap_error($this->ldap).'\' (0x'.dechex($errNo).')'; |
||
| 185 | if (@ldap_get_option($this->ldap, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extendedError) |
||
| 186 | && !empty($extendedError) |
||
| 187 | ) { |
||
| 188 | $description .= '; additional: \''.$extendedError.'\''; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | switch ($errNo) { |
||
| 192 | case 0x20://LDAP_NO_SUCH_OBJECT |
||
| 193 | Logger::warning($description); |
||
| 194 | return new Error\UserNotFound($description, $errNo); |
||
| 195 | case 0x31://LDAP_INVALID_CREDENTIALS |
||
| 196 | Logger::info($description); |
||
| 197 | return new Error\InvalidCredential($description, $errNo); |
||
| 198 | case -1://NO_SERVER_CONNECTION |
||
| 199 | Logger::error($description); |
||
| 200 | return new Error\AuthSource('ldap', $description); |
||
| 201 | default: |
||
| 202 | Logger::error($description); |
||
| 203 | return new Error\AuthSource('ldap', $description); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | return new \Exception('Unknown LDAP error.'); |
||
| 207 | } |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Search for DN from a single base. |
||
| 212 | * |
||
| 213 | * @param string $base |
||
| 214 | * Indication of root of subtree to search |
||
| 215 | * @param string|array $attribute |
||
| 216 | * The attribute name(s) to search for. |
||
| 217 | * @param string $value |
||
| 218 | * The attribute value to search for. |
||
| 219 | * Additional search filter |
||
| 220 | * @param string|null $searchFilter |
||
| 221 | * The scope of the search |
||
| 222 | * @param string $scope |
||
| 223 | * @return string |
||
| 224 | * The DN of the resulting found element. |
||
| 225 | * @throws Error\Exception if: |
||
| 226 | * - Attribute parameter is wrong type |
||
| 227 | * @throws Error\AuthSource if: |
||
| 228 | * - Not able to connect to LDAP server |
||
| 229 | * - False search result |
||
| 230 | * - Count return false |
||
| 231 | * - Searche found more than one result |
||
| 232 | * - Failed to get first entry from result |
||
| 233 | * - Failed to get DN for entry |
||
| 234 | * @throws Error\UserNotFound if: |
||
| 235 | * - Zero entries were found |
||
| 236 | * @psalm-suppress TypeDoesNotContainType |
||
| 237 | */ |
||
| 238 | private function search($base, $attribute, $value, $searchFilter = null, $scope = "subtree") |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Search for a DN. |
||
| 313 | * |
||
| 314 | * @param string|array $base |
||
| 315 | * The base, or bases, which to search from. |
||
| 316 | * @param string|array $attribute |
||
| 317 | * The attribute name(s) searched for. |
||
| 318 | * @param string $value |
||
| 319 | * The attribute value searched for. |
||
| 320 | * @param bool $allowZeroHits |
||
| 321 | * Determines if the method will throw an exception if no hits are found. |
||
| 322 | * Defaults to FALSE. |
||
| 323 | * @param string|null $searchFilter |
||
| 324 | * Additional searchFilter to be added to the (attribute=value) filter |
||
| 325 | * @param string $scope |
||
| 326 | * The scope of the search |
||
| 327 | * @return string|null |
||
| 328 | * The DN of the matching element, if found. If no element was found and |
||
| 329 | * $allowZeroHits is set to FALSE, an exception will be thrown; otherwise |
||
| 330 | * NULL will be returned. |
||
| 331 | * @throws Error\AuthSource if: |
||
| 332 | * - LDAP search encounter some problems when searching cataloge |
||
| 333 | * - Not able to connect to LDAP server |
||
| 334 | * @throws Error\UserNotFound if: |
||
| 335 | * - $allowZeroHits is FALSE and no result is found |
||
| 336 | * |
||
| 337 | */ |
||
| 338 | public function searchfordn( |
||
| 339 | $base, |
||
| 340 | $attribute, |
||
| 341 | $value, |
||
| 342 | $allowZeroHits = false, |
||
| 343 | $searchFilter = null, |
||
| 344 | $scope = 'subtree' |
||
| 345 | ) { |
||
| 346 | // Traverse all search bases, returning DN if found |
||
| 347 | $bases = \SimpleSAML\Utils\Arrays::arrayize($base); |
||
| 348 | foreach ($bases as $current) { |
||
| 349 | try { |
||
| 350 | // Single base search |
||
| 351 | $result = $this->search($current, $attribute, $value, $searchFilter, $scope); |
||
| 352 | |||
| 353 | // We don't hawe to look any futher if user is found |
||
| 354 | if (!empty($result)) { |
||
| 355 | return $result; |
||
| 356 | } |
||
| 357 | // If search failed, attempt the other base DNs |
||
| 358 | } catch (Error\UserNotFound $e) { |
||
| 359 | // Just continue searching |
||
| 360 | } |
||
| 361 | } |
||
| 362 | // Decide what to do for zero entries |
||
| 363 | Logger::debug('Library - LDAP searchfordn(): No entries found'); |
||
| 364 | if ($allowZeroHits) { |
||
| 365 | // Zero hits allowed |
||
| 366 | return null; |
||
| 367 | } else { |
||
| 368 | // Zero hits not allowed |
||
| 369 | throw $this->makeException('Library - LDAP searchfordn(): LDAP search returned zero entries for'. |
||
| 370 | ' filter \'('.join(' | ', \SimpleSAML\Utils\Arrays::arrayize($attribute)).' = '.$value.')\' on base(s) \'('.join(' & ', $bases).')\'', 2); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * This method was created specifically for the ldap:AttributeAddUsersGroups->searchActiveDirectory() |
||
| 377 | * method, but could be used for other LDAP search needs. It will search LDAP and return all the entries. |
||
| 378 | * |
||
| 379 | * @throws \Exception |
||
| 380 | * @param string|array $bases |
||
| 381 | * @param string|array $filters Array of 'attribute' => 'values' to be combined into the filter, |
||
| 382 | * or a raw filter string |
||
| 383 | * @param string|array $attributes Array of attributes requested from LDAP |
||
| 384 | * @param bool $and If multiple filters defined, then either bind them with & or | |
||
| 385 | * @param bool $escape Weather to escape the filter values or not |
||
| 386 | * @param string $scope The scope of the search |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | public function searchformultiple( |
||
| 490 | } |
||
| 491 | |||
| 492 | |||
| 493 | /** |
||
| 494 | * Bind to LDAP with a specific DN and password. Simple wrapper around |
||
| 495 | * ldap_bind() with some additional logging. |
||
| 496 | * |
||
| 497 | * @param string $dn |
||
| 498 | * The DN used. |
||
| 499 | * @param string $password |
||
| 500 | * The password used. |
||
| 501 | * @param array $sasl_args |
||
| 502 | * Array of SASL options for SASL bind |
||
| 503 | * @return bool |
||
| 504 | * Returns TRUE if successful, FALSE if |
||
| 505 | * LDAP_INVALID_CREDENTIALS, LDAP_X_PROXY_AUTHZ_FAILURE, |
||
| 506 | * LDAP_INAPPROPRIATE_AUTH, LDAP_INSUFFICIENT_ACCESS |
||
| 507 | * @throws Error\Exception on other errors |
||
| 508 | */ |
||
| 509 | public function bind($dn, $password, array $sasl_args = null) |
||
| 510 | { |
||
| 511 | if ($sasl_args != null) { |
||
| 512 | if (!function_exists('ldap_sasl_bind')) { |
||
| 513 | $ex_msg = 'Library - missing SASL support'; |
||
| 514 | throw $this->makeException($ex_msg); |
||
| 515 | } |
||
| 516 | |||
| 517 | // SASL Bind, with error handling |
||
| 518 | $authz_id = $sasl_args['authz_id']; |
||
| 519 | $error = @ldap_sasl_bind( |
||
| 520 | $this->ldap, |
||
| 521 | $dn, |
||
| 522 | $password, |
||
| 523 | $sasl_args['mech'], |
||
| 524 | $sasl_args['realm'], |
||
| 525 | $sasl_args['authc_id'], |
||
| 526 | $sasl_args['authz_id'], |
||
| 527 | $sasl_args['props'] |
||
| 528 | ); |
||
| 529 | } else { |
||
| 530 | // Simple Bind, with error handling |
||
| 531 | $authz_id = $dn; |
||
| 532 | $error = @ldap_bind($this->ldap, $dn, $password); |
||
| 533 | } |
||
| 534 | |||
| 535 | if ($error === true) { |
||
| 536 | // Good |
||
| 537 | $this->authz_id = $authz_id; |
||
| 538 | Logger::debug('Library - LDAP bind(): Bind successful with DN \''.$dn.'\''); |
||
| 539 | return true; |
||
| 540 | } |
||
| 541 | |||
| 542 | /* Handle errors |
||
| 543 | * LDAP_INVALID_CREDENTIALS |
||
| 544 | * LDAP_INSUFFICIENT_ACCESS */ |
||
| 545 | switch (ldap_errno($this->ldap)) { |
||
| 546 | case 32: // LDAP_NO_SUCH_OBJECT |
||
| 547 | // no break |
||
| 548 | case 47: // LDAP_X_PROXY_AUTHZ_FAILURE |
||
| 549 | // no break |
||
| 550 | case 48: // LDAP_INAPPROPRIATE_AUTH |
||
| 551 | // no break |
||
| 552 | case 49: // LDAP_INVALID_CREDENTIALS |
||
| 553 | // no break |
||
| 554 | case 50: // LDAP_INSUFFICIENT_ACCESS |
||
| 555 | return false; |
||
| 556 | default: |
||
| 557 | break; |
||
| 558 | } |
||
| 559 | |||
| 560 | // Bad |
||
| 561 | throw $this->makeException('Library - LDAP bind(): Bind failed with DN \''.$dn.'\''); |
||
| 562 | } |
||
| 563 | |||
| 564 | |||
| 565 | /** |
||
| 566 | * Applies an LDAP option to the current connection. |
||
| 567 | * |
||
| 568 | * @throws Exception |
||
| 569 | * @param mixed $option |
||
| 570 | * @param mixed $value |
||
| 571 | * @return void |
||
| 572 | */ |
||
| 573 | public function setOption($option, $value) |
||
| 574 | { |
||
| 575 | // Attempt to set the LDAP option |
||
| 576 | if (!@ldap_set_option($this->ldap, $option, $value)) { |
||
| 577 | throw $this->makeException( |
||
| 578 | 'ldap:LdapConnection->setOption : Failed to set LDAP option ['. |
||
| 579 | $option.'] with the value ['.$value.'] error: '.ldap_error($this->ldap), |
||
| 580 | ERR_INTERNAL |
||
| 581 | ); |
||
| 582 | } |
||
| 583 | |||
| 584 | // Log debug message |
||
| 585 | Logger::debug( |
||
| 586 | 'ldap:LdapConnection->setOption : Set the LDAP option ['. |
||
| 587 | $option.'] with the value ['.$value.']' |
||
| 588 | ); |
||
| 589 | } |
||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * Search a given DN for attributes, and return the resulting associative |
||
| 594 | * array. |
||
| 595 | * |
||
| 596 | * @param string $dn |
||
| 597 | * The DN of an element. |
||
| 598 | * @param string|array $attributes |
||
| 599 | * The names of the attribute(s) to retrieve. Defaults to NULL; that is, |
||
| 600 | * all available attributes. Note that this is not very effective. |
||
| 601 | * @param int $maxsize |
||
| 602 | * The maximum size of any attribute's value(s). If exceeded, the attribute |
||
| 603 | * will not be returned. |
||
| 604 | * @return array |
||
| 605 | * The array of attributes and their values. |
||
| 606 | * @see http://no.php.net/manual/en/function.ldap-read.php |
||
| 607 | */ |
||
| 608 | public function getAttributes($dn, $attributes = null, $maxsize = null) |
||
| 679 | } |
||
| 680 | |||
| 681 | |||
| 682 | /** |
||
| 683 | * Enter description here... |
||
| 684 | * |
||
| 685 | * @param array $config |
||
| 686 | * @param string $username |
||
| 687 | * @param string $password |
||
| 688 | * @return array|false |
||
| 689 | */ |
||
| 690 | public function validate($config, $username, $password = null) |
||
| 691 | { |
||
| 692 | /** |
||
| 693 | * Escape any characters with a special meaning in LDAP. The following |
||
| 694 | * characters have a special meaning (according to RFC 2253): |
||
| 695 | * ',', '+', '"', '\', '<', '>', ';', '*' |
||
| 696 | * These characters are escaped by prefixing them with '\'. |
||
| 697 | */ |
||
| 698 | $username = addcslashes($username, ',+"\\<>;*'); |
||
| 699 | |||
| 700 | if (isset($config['priv_user_dn'])) { |
||
| 701 | $this->bind($config['priv_user_dn'], $config['priv_user_pw']); |
||
| 702 | } |
||
| 703 | if (isset($config['dnpattern'])) { |
||
| 704 | $dn = str_replace('%username%', $username, $config['dnpattern']); |
||
| 705 | } else { |
||
| 706 | /** @var string $dn */ |
||
| 707 | $dn = $this->searchfordn($config['searchbase'], $config['searchattributes'], $username, false); |
||
| 708 | } |
||
| 709 | |||
| 710 | if ($password !== null) { |
||
| 711 | // checking users credentials ... assuming below that she may read her own attributes ... |
||
| 712 | // escape characters with a special meaning, also in the password |
||
| 713 | $password = addcslashes($password, ',+"\\<>;*'); |
||
| 714 | if (!$this->bind($dn, $password)) { |
||
| 715 | Logger::info( |
||
| 716 | 'Library - LDAP validate(): Failed to authenticate \''.$username.'\' using DN \''.$dn.'\'' |
||
| 717 | ); |
||
| 718 | return false; |
||
| 719 | } |
||
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Retrieve attributes from LDAP |
||
| 724 | */ |
||
| 725 | $attributes = $this->getAttributes($dn, $config['attributes']); |
||
| 726 | return $attributes; |
||
| 727 | } |
||
| 728 | |||
| 729 | |||
| 730 | /** |
||
| 731 | * Borrowed function from PEAR:LDAP. |
||
| 732 | * |
||
| 733 | * Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters. |
||
| 734 | * |
||
| 735 | * Any control characters with an ACII code < 32 as well as the characters with special meaning in |
||
| 736 | * LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a |
||
| 737 | * backslash followed by two hex digits representing the hexadecimal value of the character. |
||
| 738 | * |
||
| 739 | * @static |
||
| 740 | * @param string|array $values Array of values to escape |
||
| 741 | * @param bool $singleValue |
||
| 742 | * @return string|array Array $values, but escaped |
||
| 743 | */ |
||
| 744 | public static function escape_filter_value($values = [], $singleValue = true) |
||
| 745 | { |
||
| 746 | // Parameter validation |
||
| 747 | $values = \SimpleSAML\Utils\Arrays::arrayize($values); |
||
| 748 | |||
| 749 | foreach ($values as $key => $val) { |
||
| 750 | if ($val === null) { |
||
| 751 | $val = '\0'; // apply escaped "null" if string is empty |
||
| 752 | } else { |
||
| 753 | // Escaping of filter meta characters |
||
| 754 | $val = str_replace('\\', '\5c', $val); |
||
| 755 | $val = str_replace('*', '\2a', $val); |
||
| 756 | $val = str_replace('(', '\28', $val); |
||
| 757 | $val = str_replace(')', '\29', $val); |
||
| 758 | |||
| 759 | // ASCII < 32 escaping |
||
| 760 | $val = self::asc2hex32($val); |
||
| 761 | } |
||
| 762 | |||
| 763 | $values[$key] = $val; |
||
| 764 | } |
||
| 765 | if ($singleValue) { |
||
| 766 | return $values[0]; |
||
| 767 | } |
||
| 768 | return $values; |
||
| 769 | } |
||
| 770 | |||
| 771 | |||
| 772 | /** |
||
| 773 | * Borrowed function from PEAR:LDAP. |
||
| 774 | * |
||
| 775 | * Converts all ASCII chars < 32 to "\HEX" |
||
| 776 | * |
||
| 777 | * @param string $string String to convert |
||
| 778 | * |
||
| 779 | * @static |
||
| 780 | * @return string |
||
| 781 | */ |
||
| 782 | public static function asc2hex32($string) |
||
| 783 | { |
||
| 784 | for ($i = 0; $i < strlen($string); $i++) { |
||
| 785 | $char = substr($string, $i, 1); |
||
| 786 | if (ord($char) < 32) { |
||
| 787 | $hex = dechex(ord($char)); |
||
| 788 | if (strlen($hex) == 1) { |
||
| 789 | $hex = '0'.$hex; |
||
| 790 | } |
||
| 791 | $string = str_replace($char, '\\'.$hex, $string); |
||
| 792 | } |
||
| 793 | } |
||
| 794 | return $string; |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Convert SASL authz_id into a DN |
||
| 799 | * |
||
| 800 | * @param string $searchBase |
||
| 801 | * @param array $searchAttributes |
||
| 802 | * @param string $authz_id |
||
| 803 | * @return string|null |
||
| 804 | */ |
||
| 805 | private function authzidToDn($searchBase, $searchAttributes, $authz_id) |
||
| 806 | { |
||
| 807 | if (preg_match("/^dn:/", $authz_id)) { |
||
| 808 | return preg_replace("/^dn:/", "", $authz_id); |
||
| 809 | } |
||
| 810 | |||
| 811 | if (preg_match("/^u:/", $authz_id)) { |
||
| 812 | return $this->searchfordn( |
||
| 813 | $searchBase, |
||
| 814 | $searchAttributes, |
||
| 815 | preg_replace("/^u:/", "", $authz_id) |
||
| 816 | ); |
||
| 817 | } |
||
| 818 | return $authz_id; |
||
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * ldap_exop_whoami accessor, if available. Use requested authz_id |
||
| 823 | * otherwise. |
||
| 824 | * |
||
| 825 | * ldap_exop_whoami() has been provided as a third party patch that |
||
| 826 | * waited several years to get its way upstream: |
||
| 827 | * http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/databases/php-ldap/files |
||
| 828 | * |
||
| 829 | * When it was integrated into PHP repository, the function prototype |
||
| 830 | * was changed, The new prototype was used in third party patch for |
||
| 831 | * PHP 7.0 and 7.1, hence the version test below. |
||
| 832 | * |
||
| 833 | * @param string $searchBase |
||
| 834 | * @param array $searchAttributes |
||
| 835 | * @throws \Exception |
||
| 836 | * @return string |
||
| 837 | */ |
||
| 838 | public function whoami($searchBase, $searchAttributes) |
||
| 867 | } |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Set for a given DN attributes |
||
| 871 | * |
||
| 872 | * @param string $dn |
||
| 873 | * The DN of an element. |
||
| 874 | * @param array $attributes |
||
| 875 | * The names and value of the attribute(s) to set using ldap_mod_replace structure; |
||
| 876 | * @return bool |
||
| 877 | * Result of operation |
||
| 878 | */ |
||
| 879 | public function setAttributes($dn, $attributes) { |
||
| 892 | } |
||
| 893 | |||
| 894 | /** |
||
| 895 | * Adds for a given DN attributes |
||
| 896 | * |
||
| 897 | * @param string $dn |
||
| 898 | * The DN of an element. |
||
| 899 | * @param array $attributes |
||
| 900 | * The names and value of the attribute(s) to set using ldap_mod_add structure; |
||
| 901 | * @return bool |
||
| 902 | * Result of operation |
||
| 903 | */ |
||
| 904 | public function addAttributes($dn, $attributes) { |
||
| 919 |