Subversion Repositories php_utils

Rev

Rev 77 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 77 Rev 89
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * VtsLDAPUtils - Simple LDAP helper functions
4
 * VtsLDAPUtils - Simple LDAP helper functions
5
 * Copyright 2021 - 2023 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2021 - 2023 Daniel Marschall, ViaThinkSoft
6
 * Revision: 2023-04-09
6
 * Revision: 2023-11-30
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
10
 * You may obtain a copy of the License at
11
 *
11
 *
Line 85... Line 85...
85
                        ldap_close($this->conn);
85
                        ldap_close($this->conn);
86
                        $this->conn = null;
86
                        $this->conn = null;
87
                }
87
                }
88
        }
88
        }
89
 
89
 
90
        public function connect($cfg_ldap_server, $cfg_ldap_port) {
90
        public function connect($cfg_ldap_server, $cfg_ldap_port=389) {
91
                $this->disconnect();
91
                $this->disconnect();
92
 
92
 
93
                // Connect to the server
93
                // Connect to the server
94
                if (!empty($cfg_ldap_port)) {
94
                if (strpos($cfg_ldap_server, '://') !== false) {
95
                        if (!($ldapconn = @ldap_connect($cfg_ldap_server, $cfg_ldap_port))) throw new Exception(self::_L('Cannot connect to LDAP server'));
95
                        // e.g. ldap://hostname:port or ldaps://hostname:port
-
 
96
                        $uri = $cfg_ldap_server;
96
                } else {
97
                } else {
97
                        if (!($ldapconn = @ldap_connect($cfg_ldap_server))) throw new Exception(self::_L('Cannot connect to LDAP server'));
98
                        $secure = ($cfg_ldap_port == 636) || ($cfg_ldap_port == 3268) || ($cfg_ldap_port == 3269);
-
 
99
                        $schema = $secure ? 'ldaps' : 'ldap';
-
 
100
                        $uri =  $schema . '://' . $cfg_ldap_server . ':' . $cfg_ldap_port;
98
                }
101
                }
-
 
102
                if (!($ldapconn = @ldap_connect($uri))) throw new Exception(self::_L('Cannot connect to LDAP server'));
-
 
103
 
99
                ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
104
                ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
100
                ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
105
                ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
101
 
106
 
102
                $this->conn = $ldapconn;
107
                $this->conn = $ldapconn;
103
        }
108
        }