Passed
Push — master ( 4c0ad1...a48756 )
by Justin
03:48
created

LDAPClient::bind()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 6
nop 2
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: JuKuCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 24.04.2018
25
 * Time: 23:00
26
 */
27
28
class LDAPClient {
29
30
	//host and port
31
	protected $host = "";
32
	protected $port = 389;
33
34
	//flag, if uri is used instead of host
35
	protected $uri_used = false;
36
	protected $uri = "";
37
38
	protected $conn = null;
39
	protected $res = null;
40
41
	protected $ldap_config = array();
42
43
	//flag, if connection is readonly
44
	protected $readonly = false;
45
46
	public function __construct (string $host = "", int $port = 0, bool $ssl = false) {
47
		$ldap_config = array(
48
			'enabled' => true,
49
			'ssl' => $ssl
50
		);
51
52
		if (empty($host)) {
53
			//load local config
54
			if (!file_exists(CONFIG_PATH . "ldap.php")) {
55
				throw new IllegalStateException("No ldap configuration file config/ldap.php exists!");
56
			}
57
58
			//override $ldap_config
59
			require(CONFIG_PATH . "ldap.php");
60
61
			//check, if ldap is enabled
62
			if ($ldap_config['enabled'] == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition $ldap_config['enabled'] == false is always false.
Loading history...
63
				throw new IllegalStateException("LDAP is disabled.");
64
			}
65
66
			$this->host = $ldap_config['host'];
67
			$this->port = intval($ldap_config['port']);
68
69
			$this->readonly = boolval($ldap_config['readonly']);
70
		} else {
71
			$this->host = $host;
72
			$this->port = $port;
73
		}
74
75
		//check, if SSL is enabled
76
		if ($ldap_config['ssl'] == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
77
			//use OpenLDAP 2.x.x URI instead of host
78
			$this->uri = "ldaps://" . $this->host . ":" . $this->port;
79
80
			//set flag, that uri is used
81
			$this->uri_used = true;
82
		}
83
84
		//check, if host / uri is valide (this statement doesnt connect to server!) - see also http://php.net/manual/de/function.ldap-connect.php
85
		if ($this->uri_used) {
86
			$this->conn = ldap_connect($this->uri);
87
		} else {
88
			$this->conn = ldap_connect($this->host, $this->port);
89
		}
90
91
		if ($this->conn === FALSE) {
0 ignored issues
show
introduced by
The condition $this->conn === FALSE is always false.
Loading history...
92
			throw new IllegalStateException("LDAP connection parameters (host or port) are invalide.");
93
		}
94
95
		//set ldap params
96
		if (isset($ldap_config['params'])) {
97
			foreach ($ldap_config['params'] as $key=>$value) {
0 ignored issues
show
Bug introduced by
The expression $ldap_config['params'] of type boolean is not traversable.
Loading history...
98
				// configure ldap params
99
				ldap_set_option($this->conn,$key, $value);
100
			}
101
		}
102
103
		$this->ldap_config = $ldap_config;
104
	}
105
106
	public function bind (string $username = null, string $password = null) : bool {
107
		if (is_null($username) && isset($this->ldap_config['user'])) {
108
			$username = $this->ldap_config['user'];
109
			$password = $this->ldap_config['password'];
110
		}
111
112
		if ($this->conn === FALSE) {
113
			throw new IllegalStateException("ldap connection check failed.");
114
		}
115
116
		//connect and bind to ldap server
117
		if (!is_null($username)) {
118
			//with authentification
119
			$this->res = ldap_bind($this->conn, $username, $password);
120
		} else {
121
			//anonymous binding
122
			$this->res = ldap_bind($this->conn);
123
		}
124
125
		return $this->res !== FALSE;
126
	}
127
128
	public function getConnection () {
129
		return $this->conn;
130
	}
131
132
	public function disconnect () {
133
		//disconnect from ldap server
134
		ldap_unbind($this->conn);
135
	}
136
137
}
138
139
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
140