|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Libresign\Service\Crl\Ldap; |
| 11 | + |
| 12 | +/** |
| 13 | + * Production implementation of ILdapConnection that delegates every call |
| 14 | + * to the corresponding PHP ldap_* function. |
| 15 | + * |
| 16 | + * Requires the PHP ldap extension to be loaded. If the extension is missing |
| 17 | + * the constructor throws so the DI container can catch the problem early |
| 18 | + * rather than failing silently at connection time. |
| 19 | + */ |
| 20 | +class PhpLdapConnection implements ILdapConnection { |
| 21 | + public function __construct() { |
| 22 | + if (!function_exists('ldap_connect')) { |
| 23 | + throw new \RuntimeException('PHP ldap extension is not loaded'); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + #[\Override] |
| 28 | + public function connect(string $host, int $port): mixed { |
| 29 | + $conn = @ldap_connect($host, $port); |
| 30 | + if (!$conn) { |
| 31 | + throw new \RuntimeException(sprintf('ldap_connect failed for %s:%d', $host, $port)); |
| 32 | + } |
| 33 | + return $conn; |
| 34 | + } |
| 35 | + |
| 36 | + #[\Override] |
| 37 | + public function configure(mixed $ldap): void { |
| 38 | + ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); |
| 39 | + ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, 10); |
| 40 | + ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); |
| 41 | + } |
| 42 | + |
| 43 | + public function setOption(mixed $ldap, int $option, mixed $value): void { |
| 44 | + ldap_set_option($ldap, $option, $value); |
| 45 | + } |
| 46 | + |
| 47 | + #[\Override] |
| 48 | + public function bind(mixed $ldap): bool { |
| 49 | + return (bool)@ldap_bind($ldap); |
| 50 | + } |
| 51 | + |
| 52 | + #[\Override] |
| 53 | + public function read(mixed $ldap, string $dn, string $filter, array $attributes): mixed { |
| 54 | + return @ldap_read($ldap, $dn, $filter, $attributes); |
| 55 | + } |
| 56 | + |
| 57 | + #[\Override] |
| 58 | + public function listEntries(mixed $ldap, string $dn, string $filter, array $attributes): mixed { |
| 59 | + return @ldap_list($ldap, $dn, $filter, $attributes); |
| 60 | + } |
| 61 | + |
| 62 | + #[\Override] |
| 63 | + public function search(mixed $ldap, string $dn, string $filter, array $attributes): mixed { |
| 64 | + return @ldap_search($ldap, $dn, $filter, $attributes); |
| 65 | + } |
| 66 | + |
| 67 | + #[\Override] |
| 68 | + public function getEntries(mixed $ldap, mixed $result): array { |
| 69 | + return ldap_get_entries($ldap, $result) ?: []; |
| 70 | + } |
| 71 | + |
| 72 | + #[\Override] |
| 73 | + public function unbind(mixed $ldap): void { |
| 74 | + @ldap_unbind($ldap); |
| 75 | + } |
| 76 | +} |
0 commit comments