Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
PhpLdapConnection
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 9
156
0.00% covered (danger)
0.00%
0 / 1
 connect
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 configure
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 setOption
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 bind
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 read
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 listEntries
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 search
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEntries
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 unbind
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
7 * SPDX-License-Identifier: AGPL-3.0-or-later
8 */
9
10namespace 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 * If the PHP ldap extension is absent the class can still be instantiated;
17 * callers are expected to check function_exists('ldap_connect') before use.
18 */
19class PhpLdapConnection implements ILdapConnection {
20
21    #[\Override]
22    public function connect(string $host, int $port): mixed {
23        if (!function_exists('ldap_connect')) {
24            throw new \RuntimeException('PHP ldap extension is not loaded');
25        }
26        $conn = @ldap_connect("{$host}:{$port}");
27        if (!$conn) {
28            throw new \RuntimeException(sprintf('ldap_connect failed for %s:%d', $host, $port));
29        }
30        return $conn;
31    }
32
33    /** @psalm-suppress UndefinedConstant */
34    #[\Override]
35    public function configure(mixed $ldap): void {
36        ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
37        ldap_set_option($ldap, LDAP_OPT_NETWORK_TIMEOUT, 10);
38        ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
39    }
40
41    public function setOption(mixed $ldap, int $option, mixed $value): void {
42        ldap_set_option($ldap, $option, $value);
43    }
44
45    #[\Override]
46    public function bind(mixed $ldap): bool {
47        return (bool)@ldap_bind($ldap);
48    }
49
50    #[\Override]
51    public function read(mixed $ldap, string $dn, string $filter, array $attributes): mixed {
52        return @ldap_read($ldap, $dn, $filter, $attributes);
53    }
54
55    #[\Override]
56    public function listEntries(mixed $ldap, string $dn, string $filter, array $attributes): mixed {
57        return @ldap_list($ldap, $dn, $filter, $attributes);
58    }
59
60    #[\Override]
61    public function search(mixed $ldap, string $dn, string $filter, array $attributes): mixed {
62        return @ldap_search($ldap, $dn, $filter, $attributes);
63    }
64
65    #[\Override]
66    public function getEntries(mixed $ldap, mixed $result): array {
67        return ldap_get_entries($ldap, $result) ?: [];
68    }
69
70    #[\Override]
71    public function unbind(mixed $ldap): void {
72        @ldap_unbind($ldap);
73    }
74}