Blame view

sources/apps/user_ldap/lib/ldap.php 7.19 KB
31b7f2792   Kload   Upgrade to ownclo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  <?php
  
  /**
   * ownCloud – LDAP Wrapper
   *
   * @author Arthur Schiwon
   * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  namespace OCA\user_ldap\lib;
  
  class LDAP implements ILDAPWrapper {
  	protected $curFunc = '';
  	protected $curArgs = array();
6d9380f96   Cédric Dupont   Update sources OC...
29
30
31
32
33
34
  	/**
  	 * @param resource $link
  	 * @param string $dn
  	 * @param string $password
  	 * @return bool|mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
35
36
37
  	public function bind($link, $dn, $password) {
  		return $this->invokeLDAPMethod('bind', $link, $dn, $password);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
38
39
40
41
42
  	/**
  	 * @param string $host
  	 * @param string $port
  	 * @return mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
43
44
45
  	public function connect($host, $port) {
  		return $this->invokeLDAPMethod('connect', $host, $port);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
46
47
48
49
50
51
  	/**
  	 * @param LDAP $link
  	 * @param LDAP $result
  	 * @param string $cookie
  	 * @return bool|LDAP
  	 */
31b7f2792   Kload   Upgrade to ownclo...
52
53
54
55
56
57
58
59
  	public function controlPagedResultResponse($link, $result, &$cookie) {
  		$this->preFunctionCall('ldap_control_paged_result_response',
  			array($link, $result, $cookie));
  		$result = ldap_control_paged_result_response($link, $result, $cookie);
  		$this->postFunctionCall();
  
  		return $result;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
60
61
62
63
64
65
66
67
68
  	/**
  	 * @param LDAP $link
  	 * @param int $pageSize
  	 * @param bool $isCritical
  	 * @param string $cookie
  	 * @return mixed|true
  	 */
  	public function controlPagedResult($link, $pageSize, $isCritical, $cookie) {
  		return $this->invokeLDAPMethod('control_paged_result', $link, $pageSize,
31b7f2792   Kload   Upgrade to ownclo...
69
70
  										$isCritical, $cookie);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
71
72
73
74
75
  	/**
  	 * @param LDAP $link
  	 * @param LDAP $result
  	 * @return mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
76
77
78
  	public function countEntries($link, $result) {
  		return $this->invokeLDAPMethod('count_entries', $link, $result);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
79
80
81
82
  	/**
  	 * @param LDAP $link
  	 * @return mixed|string
  	 */
31b7f2792   Kload   Upgrade to ownclo...
83
84
85
  	public function errno($link) {
  		return $this->invokeLDAPMethod('errno', $link);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
86
87
88
89
  	/**
  	 * @param LDAP $link
  	 * @return int|mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
90
91
92
  	public function error($link) {
  		return $this->invokeLDAPMethod('error', $link);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  	/**
  	 * Splits DN into its component parts
  	 * @param string $dn
  	 * @param int @withAttrib
  	 * @return array|false
  	 * @link http://www.php.net/manual/en/function.ldap-explode-dn.php
  	 */
  	public function explodeDN($dn, $withAttrib) {
  		return $this->invokeLDAPMethod('ldap_explode_dn', $dn, $withAttrib);
  	}
  
  	/**
  	 * @param LDAP $link
  	 * @param LDAP $result
  	 * @return mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
109
110
111
  	public function firstEntry($link, $result) {
  		return $this->invokeLDAPMethod('first_entry', $link, $result);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
112
113
114
115
116
  	/**
  	 * @param LDAP $link
  	 * @param LDAP $result
  	 * @return array|mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
117
118
119
  	public function getAttributes($link, $result) {
  		return $this->invokeLDAPMethod('get_attributes', $link, $result);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
120
121
122
123
124
  	/**
  	 * @param LDAP $link
  	 * @param LDAP $result
  	 * @return mixed|string
  	 */
31b7f2792   Kload   Upgrade to ownclo...
125
126
127
  	public function getDN($link, $result) {
  		return $this->invokeLDAPMethod('get_dn', $link, $result);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
128
129
130
131
132
  	/**
  	 * @param LDAP $link
  	 * @param LDAP $result
  	 * @return array|mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
133
134
135
  	public function getEntries($link, $result) {
  		return $this->invokeLDAPMethod('get_entries', $link, $result);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
136
137
138
139
140
  	/**
  	 * @param LDAP $link
  	 * @param resource $result
  	 * @return mixed|an
  	 */
31b7f2792   Kload   Upgrade to ownclo...
141
142
143
  	public function nextEntry($link, $result) {
  		return $this->invokeLDAPMethod('next_entry', $link, $result);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
144
145
146
147
148
149
150
  	/**
  	 * @param LDAP $link
  	 * @param string $baseDN
  	 * @param string $filter
  	 * @param array $attr
  	 * @return mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
151
152
153
  	public function read($link, $baseDN, $filter, $attr) {
  		return $this->invokeLDAPMethod('read', $link, $baseDN, $filter, $attr);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
154
155
156
157
158
159
160
161
162
163
164
  	/**
  	 * @param LDAP $link
  	 * @param string $baseDN
  	 * @param string $filter
  	 * @param array $attr
  	 * @param int $attrsOnly
  	 * @param int $limit
  	 * @return mixed
  	 */
  	public function search($link, $baseDN, $filter, $attr, $attrsOnly = 0, $limit = 0) {
  		return $this->invokeLDAPMethod('search', $link, $baseDN, $filter, $attr, $attrsOnly, $limit);
31b7f2792   Kload   Upgrade to ownclo...
165
  	}
6d9380f96   Cédric Dupont   Update sources OC...
166
167
168
169
170
171
  	/**
  	 * @param LDAP $link
  	 * @param string $option
  	 * @param int $value
  	 * @return bool|mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
172
173
174
  	public function setOption($link, $option, $value) {
  		return $this->invokeLDAPMethod('set_option', $link, $option, $value);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
175
176
177
178
179
180
181
182
  	/**
  	 * @param LDAP $link
  	 * @param LDAP $result
  	 * @param string $sortFilter
  	 * @return mixed
  	 */
  	public function sort($link, $result, $sortFilter) {
  		return $this->invokeLDAPMethod('sort', $link, $result, $sortFilter);
31b7f2792   Kload   Upgrade to ownclo...
183
  	}
6d9380f96   Cédric Dupont   Update sources OC...
184
185
186
187
  	/**
  	 * @param LDAP $link
  	 * @return mixed|true
  	 */
31b7f2792   Kload   Upgrade to ownclo...
188
189
190
  	public function startTls($link) {
  		return $this->invokeLDAPMethod('start_tls', $link);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
191
192
193
194
  	/**
  	 * @param resource $link
  	 * @return bool|mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
195
196
197
198
199
  	public function unbind($link) {
  		return $this->invokeLDAPMethod('unbind', $link);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
200
201
  	 * Checks whether the server supports LDAP
  	 * @return boolean if it the case, false otherwise
31b7f2792   Kload   Upgrade to ownclo...
202
203
204
205
206
207
  	 * */
  	public function areLDAPFunctionsAvailable() {
  		return function_exists('ldap_connect');
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
208
209
  	 * Checks whether PHP supports LDAP Paged Results
  	 * @return boolean if it the case, false otherwise
31b7f2792   Kload   Upgrade to ownclo...
210
211
212
213
214
215
216
217
  	 * */
  	public function hasPagedResultSupport() {
  		$hasSupport = function_exists('ldap_control_paged_result')
  			&& function_exists('ldap_control_paged_result_response');
  		return $hasSupport;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
218
219
220
  	 * Checks whether the submitted parameter is a resource
  	 * @param Resource $resource the resource variable to check
  	 * @return bool true if it is a resource, false otherwise
31b7f2792   Kload   Upgrade to ownclo...
221
222
223
224
  	 */
  	public function isResource($resource) {
  		return is_resource($resource);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
225
226
227
  	/**
  	 * @return mixed
  	 */
31b7f2792   Kload   Upgrade to ownclo...
228
229
230
231
232
233
  	private function invokeLDAPMethod() {
  		$arguments = func_get_args();
  		$func = 'ldap_' . array_shift($arguments);
  		if(function_exists($func)) {
  			$this->preFunctionCall($func, $arguments);
  			$result = call_user_func_array($func, $arguments);
6d9380f96   Cédric Dupont   Update sources OC...
234
235
236
  			if ($result === FALSE) {
  				$this->postFunctionCall();
  			}
31b7f2792   Kload   Upgrade to ownclo...
237
238
239
  			return $result;
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
240
241
242
243
  	/**
  	 * @param string $functionName
  	 * @param array $args
  	 */
31b7f2792   Kload   Upgrade to ownclo...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  	private function preFunctionCall($functionName, $args) {
  		$this->curFunc = $functionName;
  		$this->curArgs = $args;
  	}
  
  	private function postFunctionCall() {
  		if($this->isResource($this->curArgs[0])) {
  			$errorCode = ldap_errno($this->curArgs[0]);
  			$errorMsg  = ldap_error($this->curArgs[0]);
  			if($errorCode !== 0) {
  				if($this->curFunc === 'ldap_sort' && $errorCode === -4) {
  					//You can safely ignore that decoding error.
  					//… says https://bugs.php.net/bug.php?id=18023
  				} else if($this->curFunc === 'ldap_get_entries'
  						  && $errorCode === -4) {
  				} else if ($errorCode === 32) {
  					//for now
  				} else if ($errorCode === 10) {
  					//referrals, we switch them off, but then there is AD :)
  				} else {
  					\OCP\Util::writeLog('user_ldap',
  										'LDAP error '.$errorMsg.' (' .
  											$errorCode.') after calling '.
  											$this->curFunc,
  										\OCP\Util::DEBUG);
  				}
  			}
  		}
  
  		$this->curFunc = '';
  		$this->curArgs = array();
  	}
6d9380f96   Cédric Dupont   Update sources OC...
276
  }