Completed
Push — master ( 91fc25...ff93dd )
by Morris
13:21
created

Connection::getConnectionResource()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 0
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Jörn Friedrich Dreyer <[email protected]>
9
 * @author Lukas Reschke <[email protected]>
10
 * @author Lyonel Vincent <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Robin McCorkell <[email protected]>
14
 * @author Roger Szabo <[email protected]>
15
 * @author Xuanwo <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
namespace OCA\User_LDAP;
34
35
use OC\ServerNotAvailableException;
36
37
/**
38
 * magic properties (incomplete)
39
 * responsible for LDAP connections in context with the provided configuration
40
 *
41
 * @property string ldapHost
42
 * @property string ldapPort holds the port number
43
 * @property string ldapUserFilter
44
 * @property string ldapUserDisplayName
45
 * @property string ldapUserDisplayName2
46
 * @property boolean turnOnPasswordChange
47
 * @property boolean hasPagedResultSupport
48
 * @property string[] ldapBaseUsers
49
 * @property int|string ldapPagingSize holds an integer
50
 * @property bool|mixed|void ldapGroupMemberAssocAttr
51
 * @property string ldapUuidUserAttribute
52
 * @property string ldapUuidGroupAttribute
53
 */
54
class Connection extends LDAPUtility {
55
	private $ldapConnectionRes = null;
56
	private $configPrefix;
57
	private $configID;
58
	private $configured = false;
59
	private $hasPagedResultSupport = true;
60
	//whether connection should be kept on __destruct
61
	private $dontDestruct = false;
62
63
	/**
64
	 * @var bool runtime flag that indicates whether supported primary groups are available
65
	 */
66
	public $hasPrimaryGroups = true;
67
68
	/**
69
	 * @var bool runtime flag that indicates whether supported POSIX gidNumber are available
70
	 */
71
	public $hasGidNumber = true;
72
73
	//cache handler
74
	protected $cache;
75
76
	/** @var Configuration settings handler **/
77
	protected $configuration;
78
79
	protected $doNotValidate = false;
80
81
	protected $ignoreValidation = false;
82
83
	/**
84
	 * Constructor
85
	 * @param ILDAPWrapper $ldap
86
	 * @param string $configPrefix a string with the prefix for the configkey column (appconfig table)
87
	 * @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections
88
	 */
89
	public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') {
90
		parent::__construct($ldap);
91
		$this->configPrefix = $configPrefix;
92
		$this->configID = $configID;
93
		$this->configuration = new Configuration($configPrefix,
94
												 !is_null($configID));
95
		$memcache = \OC::$server->getMemCacheFactory();
96
		if($memcache->isAvailable()) {
97
			$this->cache = $memcache->create();
98
		}
99
		$helper = new Helper(\OC::$server->getConfig());
100
		$this->doNotValidate = !in_array($this->configPrefix,
101
			$helper->getServerConfigurationPrefixes());
102
		$this->hasPagedResultSupport =
103
			intval($this->configuration->ldapPagingSize) !== 0
104
			|| $this->ldap->hasPagedResultSupport();
105
	}
106
107
	public function __destruct() {
108
		if(!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) {
109
			@$this->ldap->unbind($this->ldapConnectionRes);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
110
		};
111
	}
112
113
	/**
114
	 * defines behaviour when the instance is cloned
115
	 */
116
	public function __clone() {
117
		$this->configuration = new Configuration($this->configPrefix,
118
												 !is_null($this->configID));
119
		$this->ldapConnectionRes = null;
120
		$this->dontDestruct = true;
121
	}
122
123
	/**
124
	 * @param string $name
125
	 * @return bool|mixed|void
126
	 */
127
	public function __get($name) {
128
		if(!$this->configured) {
129
			$this->readConfiguration();
130
		}
131
132
		if($name === 'hasPagedResultSupport') {
133
			return $this->hasPagedResultSupport;
134
		}
135
136
		return $this->configuration->$name;
137
	}
138
139
	/**
140
	 * @param string $name
141
	 * @param mixed $value
142
	 */
143
	public function __set($name, $value) {
144
		$this->doNotValidate = false;
145
		$before = $this->configuration->$name;
146
		$this->configuration->$name = $value;
147
		$after = $this->configuration->$name;
148
		if($before !== $after) {
149
			if ($this->configID !== '') {
150
				$this->configuration->saveConfiguration();
151
			}
152
			$this->validateConfiguration();
153
		}
154
	}
155
156
	/**
157
	 * sets whether the result of the configuration validation shall
158
	 * be ignored when establishing the connection. Used by the Wizard
159
	 * in early configuration state.
160
	 * @param bool $state
161
	 */
162
	public function setIgnoreValidation($state) {
163
		$this->ignoreValidation = (bool)$state;
164
	}
165
166
	/**
167
	 * initializes the LDAP backend
168
	 * @param bool $force read the config settings no matter what
169
	 */
170
	public function init($force = false) {
171
		$this->readConfiguration($force);
172
		$this->establishConnection();
173
	}
174
175
	/**
176
	 * Returns the LDAP handler
177
	 */
178
	public function getConnectionResource() {
179
		if(!$this->ldapConnectionRes) {
180
			$this->init();
181
		} else if(!$this->ldap->isResource($this->ldapConnectionRes)) {
182
			$this->ldapConnectionRes = null;
183
			$this->establishConnection();
184
		}
185
		if(is_null($this->ldapConnectionRes)) {
186
			\OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, \OCP\Util::ERROR);
0 ignored issues
show
Documentation introduced by
The property ldapHost does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
187
			throw new ServerNotAvailableException('Connection to LDAP server could not be established');
188
		}
189
		return $this->ldapConnectionRes;
190
	}
191
192
	/**
193
	 * resets the connection resource
194
	 */
195
	public function resetConnectionResource() {
196
		if(!is_null($this->ldapConnectionRes)) {
197
			@$this->ldap->unbind($this->ldapConnectionRes);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
198
			$this->ldapConnectionRes = null;
199
		}
200
	}
201
202
	/**
203
	 * @param string|null $key
204
	 * @return string
205
	 */
206
	private function getCacheKey($key) {
207
		$prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-';
208
		if(is_null($key)) {
209
			return $prefix;
210
		}
211
		return $prefix.md5($key);
212
	}
213
214
	/**
215
	 * @param string $key
216
	 * @return mixed|null
217
	 */
218
	public function getFromCache($key) {
219
		if(!$this->configured) {
220
			$this->readConfiguration();
221
		}
222
		if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) {
0 ignored issues
show
Documentation introduced by
The property ldapCacheTTL does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
223
			return null;
224
		}
225
		$key = $this->getCacheKey($key);
226
227
		return json_decode(base64_decode($this->cache->get($key)), true);
228
	}
229
230
	/**
231
	 * @param string $key
232
	 * @param mixed $value
233
	 *
234
	 * @return string
235
	 */
236
	public function writeToCache($key, $value) {
237
		if(!$this->configured) {
238
			$this->readConfiguration();
239
		}
240
		if(is_null($this->cache)
241
			|| !$this->configuration->ldapCacheTTL
0 ignored issues
show
Documentation introduced by
The property ldapCacheTTL does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
242
			|| !$this->configuration->ldapConfigurationActive) {
0 ignored issues
show
Bug introduced by
The property ldapConfigurationActive does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
243
			return null;
244
		}
245
		$key   = $this->getCacheKey($key);
246
		$value = base64_encode(json_encode($value));
247
		$this->cache->set($key, $value, $this->configuration->ldapCacheTTL);
0 ignored issues
show
Documentation introduced by
The property ldapCacheTTL does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
248
	}
249
250
	public function clearCache() {
251
		if(!is_null($this->cache)) {
252
			$this->cache->clear($this->getCacheKey(null));
253
		}
254
	}
255
256
	/**
257
	 * Caches the general LDAP configuration.
258
	 * @param bool $force optional. true, if the re-read should be forced. defaults
259
	 * to false.
260
	 * @return null
261
	 */
262
	private function readConfiguration($force = false) {
263
		if((!$this->configured || $force) && !is_null($this->configID)) {
264
			$this->configuration->readConfiguration();
265
			$this->configured = $this->validateConfiguration();
266
		}
267
	}
268
269
	/**
270
	 * set LDAP configuration with values delivered by an array, not read from configuration
271
	 * @param array $config array that holds the config parameters in an associated array
272
	 * @param array &$setParameters optional; array where the set fields will be given to
273
	 * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters
274
	 */
275
	public function setConfiguration($config, &$setParameters = null) {
276
		if(is_null($setParameters)) {
277
			$setParameters = array();
278
		}
279
		$this->doNotValidate = false;
280
		$this->configuration->setConfiguration($config, $setParameters);
281
		if(count($setParameters) > 0) {
282
			$this->configured = $this->validateConfiguration();
283
		}
284
285
286
		return $this->configured;
287
	}
288
289
	/**
290
	 * saves the current Configuration in the database and empties the
291
	 * cache
292
	 * @return null
293
	 */
294
	public function saveConfiguration() {
295
		$this->configuration->saveConfiguration();
296
		$this->clearCache();
297
	}
298
299
	/**
300
	 * get the current LDAP configuration
301
	 * @return array
302
	 */
303
	public function getConfiguration() {
304
		$this->readConfiguration();
305
		$config = $this->configuration->getConfiguration();
306
		$cta = $this->configuration->getConfigTranslationArray();
307
		$result = array();
308
		foreach($cta as $dbkey => $configkey) {
309
			switch($configkey) {
310
				case 'homeFolderNamingRule':
311
					if(strpos($config[$configkey], 'attr:') === 0) {
312
						$result[$dbkey] = substr($config[$configkey], 5);
313
					} else {
314
						$result[$dbkey] = '';
315
					}
316
					break;
317
				case 'ldapBase':
318
				case 'ldapBaseUsers':
319
				case 'ldapBaseGroups':
320
				case 'ldapAttributesForUserSearch':
321
				case 'ldapAttributesForGroupSearch':
322
					if(is_array($config[$configkey])) {
323
						$result[$dbkey] = implode("\n", $config[$configkey]);
324
						break;
325
					} //else follows default
326
				default:
327
					$result[$dbkey] = $config[$configkey];
328
			}
329
		}
330
		return $result;
331
	}
332
333
	private function doSoftValidation() {
334
		//if User or Group Base are not set, take over Base DN setting
335
		foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) {
336
			$val = $this->configuration->$keyBase;
337
			if(empty($val)) {
338
				$this->configuration->$keyBase = $this->configuration->ldapBase;
0 ignored issues
show
Documentation introduced by
The property ldapBase does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
339
			}
340
		}
341
342
		foreach(array('ldapExpertUUIDUserAttr'  => 'ldapUuidUserAttribute',
343
					  'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute')
344
				as $expertSetting => $effectiveSetting) {
345
			$uuidOverride = $this->configuration->$expertSetting;
346
			if(!empty($uuidOverride)) {
347
				$this->configuration->$effectiveSetting = $uuidOverride;
348
			} else {
349
				$uuidAttributes = array('auto', 'entryuuid', 'nsuniqueid',
350
										'objectguid', 'guid', 'ipauniqueid');
351
				if(!in_array($this->configuration->$effectiveSetting,
352
							$uuidAttributes)
353
					&& (!is_null($this->configID))) {
354
					$this->configuration->$effectiveSetting = 'auto';
355
					$this->configuration->saveConfiguration();
356
					\OCP\Util::writeLog('user_ldap',
357
										'Illegal value for the '.
358
										$effectiveSetting.', '.'reset to '.
359
										'autodetect.', \OCP\Util::INFO);
360
				}
361
362
			}
363
		}
364
365
		$backupPort = intval($this->configuration->ldapBackupPort);
0 ignored issues
show
Documentation introduced by
The property ldapBackupPort does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
366
		if ($backupPort <= 0) {
367
			$this->configuration->backupPort = $this->configuration->ldapPort;
0 ignored issues
show
Documentation introduced by
The property backupPort does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property ldapPort does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
368
		}
369
370
		//make sure empty search attributes are saved as simple, empty array
371
		$saKeys = array('ldapAttributesForUserSearch',
372
						'ldapAttributesForGroupSearch');
373
		foreach($saKeys as $key) {
374
			$val = $this->configuration->$key;
375
			if(is_array($val) && count($val) === 1 && empty($val[0])) {
376
				$this->configuration->$key = array();
377
			}
378
		}
379
380
		if((stripos($this->configuration->ldapHost, 'ldaps://') === 0)
0 ignored issues
show
Documentation introduced by
The property ldapHost does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
381
			&& $this->configuration->ldapTLS) {
0 ignored issues
show
Documentation introduced by
The property ldapTLS does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
382
			$this->configuration->ldapTLS = false;
0 ignored issues
show
Documentation introduced by
The property ldapTLS does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
383
			\OCP\Util::writeLog('user_ldap',
384
								'LDAPS (already using secure connection) and '.
385
								'TLS do not work together. Switched off TLS.',
386
								\OCP\Util::INFO);
387
		}
388
	}
389
390
	/**
391
	 * @return bool
392
	 */
393
	private function doCriticalValidation() {
394
		$configurationOK = true;
395
		$errorStr = 'Configuration Error (prefix '.
396
					strval($this->configPrefix).'): ';
397
398
		//options that shall not be empty
399
		$options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName',
400
						 'ldapGroupDisplayName', 'ldapLoginFilter');
401
		foreach($options as $key) {
402
			$val = $this->configuration->$key;
403
			if(empty($val)) {
404
				switch($key) {
405
					case 'ldapHost':
406
						$subj = 'LDAP Host';
407
						break;
408
					case 'ldapPort':
409
						$subj = 'LDAP Port';
410
						break;
411
					case 'ldapUserDisplayName':
412
						$subj = 'LDAP User Display Name';
413
						break;
414
					case 'ldapGroupDisplayName':
415
						$subj = 'LDAP Group Display Name';
416
						break;
417
					case 'ldapLoginFilter':
418
						$subj = 'LDAP Login Filter';
419
						break;
420
					default:
421
						$subj = $key;
422
						break;
423
				}
424
				$configurationOK = false;
425
				\OCP\Util::writeLog('user_ldap',
426
									$errorStr.'No '.$subj.' given!',
427
									\OCP\Util::WARN);
428
			}
429
		}
430
431
		//combinations
432
		$agent = $this->configuration->ldapAgentName;
0 ignored issues
show
Documentation introduced by
The property ldapAgentName does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
433
		$pwd = $this->configuration->ldapAgentPassword;
0 ignored issues
show
Documentation introduced by
The property ldapAgentPassword does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
434
		if (
435
			($agent === ''  && $pwd !== '')
436
			|| ($agent !== '' && $pwd === '')
437
		) {
438
			\OCP\Util::writeLog('user_ldap',
439
								$errorStr.'either no password is given for the'.
440
								'user agent or a password is given, but not an'.
441
								'LDAP agent.',
442
				\OCP\Util::WARN);
443
			$configurationOK = false;
444
		}
445
446
		$base = $this->configuration->ldapBase;
0 ignored issues
show
Documentation introduced by
The property ldapBase does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
447
		$baseUsers = $this->configuration->ldapBaseUsers;
0 ignored issues
show
Documentation introduced by
The property ldapBaseUsers does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
448
		$baseGroups = $this->configuration->ldapBaseGroups;
0 ignored issues
show
Documentation introduced by
The property ldapBaseGroups does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
449
450
		if(empty($base) && empty($baseUsers) && empty($baseGroups)) {
451
			\OCP\Util::writeLog('user_ldap',
452
								$errorStr.'Not a single Base DN given.',
453
								\OCP\Util::WARN);
454
			$configurationOK = false;
455
		}
456
457
		if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8')
0 ignored issues
show
Documentation introduced by
The property ldapLoginFilter does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
458
		   === false) {
459
			\OCP\Util::writeLog('user_ldap',
460
								$errorStr.'login filter does not contain %uid '.
461
								'place holder.',
462
								\OCP\Util::WARN);
463
			$configurationOK = false;
464
		}
465
466
		return $configurationOK;
467
	}
468
469
	/**
470
	 * Validates the user specified configuration
471
	 * @return bool true if configuration seems OK, false otherwise
472
	 */
473
	private function validateConfiguration() {
474
475
		if($this->doNotValidate) {
476
			//don't do a validation if it is a new configuration with pure
477
			//default values. Will be allowed on changes via __set or
478
			//setConfiguration
479
			return false;
480
		}
481
482
		// first step: "soft" checks: settings that are not really
483
		// necessary, but advisable. If left empty, give an info message
484
		$this->doSoftValidation();
485
486
		//second step: critical checks. If left empty or filled wrong, mark as
487
		//not configured and give a warning.
488
		return $this->doCriticalValidation();
489
	}
490
491
492
	/**
493
	 * Connects and Binds to LDAP
494
	 */
495
	private function establishConnection() {
496
		if(!$this->configuration->ldapConfigurationActive) {
0 ignored issues
show
Bug introduced by
The property ldapConfigurationActive does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
497
			return null;
498
		}
499
		static $phpLDAPinstalled = true;
500
		if(!$phpLDAPinstalled) {
501
			return false;
502
		}
503
		if(!$this->ignoreValidation && !$this->configured) {
504
			\OCP\Util::writeLog('user_ldap',
505
								'Configuration is invalid, cannot connect',
506
								\OCP\Util::WARN);
507
			return false;
508
		}
509
		if(!$this->ldapConnectionRes) {
510
			if(!$this->ldap->areLDAPFunctionsAvailable()) {
511
				$phpLDAPinstalled = false;
512
				\OCP\Util::writeLog('user_ldap',
513
									'function ldap_connect is not available. Make '.
514
									'sure that the PHP ldap module is installed.',
515
									\OCP\Util::ERROR);
516
517
				return false;
518
			}
519
			if($this->configuration->turnOffCertCheck) {
0 ignored issues
show
Documentation introduced by
The property turnOffCertCheck does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
520
				if(putenv('LDAPTLS_REQCERT=never')) {
521
					\OCP\Util::writeLog('user_ldap',
522
						'Turned off SSL certificate validation successfully.',
523
						\OCP\Util::DEBUG);
524
				} else {
525
					\OCP\Util::writeLog('user_ldap',
526
										'Could not turn off SSL certificate validation.',
527
										\OCP\Util::WARN);
528
				}
529
			}
530
531
			$isOverrideMainServer = ($this->configuration->ldapOverrideMainServer
0 ignored issues
show
Documentation introduced by
The property ldapOverrideMainServer does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
532
				|| $this->getFromCache('overrideMainServer'));
533
			$isBackupHost = (trim($this->configuration->ldapBackupHost) !== "");
0 ignored issues
show
Documentation introduced by
The property ldapBackupHost does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
534
			$bindStatus = false;
535
			$error = -1;
536
			try {
537
				if (!$isOverrideMainServer) {
538
					$this->doConnect($this->configuration->ldapHost,
0 ignored issues
show
Documentation introduced by
The property ldapHost does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
539
						$this->configuration->ldapPort);
0 ignored issues
show
Documentation introduced by
The property ldapPort does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
540
					$bindStatus = $this->bind();
541
					$error = $this->ldap->isResource($this->ldapConnectionRes) ?
0 ignored issues
show
Documentation introduced by
$this->ldapConnectionRes is of type null, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
542
						$this->ldap->errno($this->ldapConnectionRes) : -1;
0 ignored issues
show
Documentation introduced by
$this->ldapConnectionRes is of type null, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
543
				}
544
				if($bindStatus === true) {
545
					return $bindStatus;
546
				}
547
			} catch (ServerNotAvailableException $e) {
548
				if(!$isBackupHost) {
549
					throw $e;
550
				}
551
			}
552
553
			//if LDAP server is not reachable, try the Backup (Replica!) Server
554
			if($isBackupHost && ($error !== 0 || $isOverrideMainServer)) {
555
				$this->doConnect($this->configuration->ldapBackupHost,
0 ignored issues
show
Documentation introduced by
The property ldapBackupHost does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
556
								 $this->configuration->ldapBackupPort);
0 ignored issues
show
Documentation introduced by
The property ldapBackupPort does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
557
				$bindStatus = $this->bind();
558
				$error = $this->ldap->isResource($this->ldapConnectionRes) ?
0 ignored issues
show
Documentation introduced by
$this->ldapConnectionRes is of type null, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
559
					$this->ldap->errno($this->ldapConnectionRes) : -1;
0 ignored issues
show
Documentation introduced by
$this->ldapConnectionRes is of type null, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
560
				if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) {
561
					//when bind to backup server succeeded and failed to main server,
562
					//skip contacting him until next cache refresh
563
					$this->writeToCache('overrideMainServer', true);
564
				}
565
			}
566
567
			return $bindStatus;
568
		}
569
		return null;
570
	}
571
572
	/**
573
	 * @param string $host
574
	 * @param string $port
575
	 * @return bool
576
	 * @throws \OC\ServerNotAvailableException
577
	 */
578
	private function doConnect($host, $port) {
579
		if ($host === '') {
580
			return false;
581
		}
582
583
		$this->ldapConnectionRes = $this->ldap->connect($host, $port);
584
585
		if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
586
			throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');
587
		}
588
589
		if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) {
590
			throw new ServerNotAvailableException('Could not disable LDAP referrals.');
591
		}
592
593
		if($this->configuration->ldapTLS) {
0 ignored issues
show
Documentation introduced by
The property ldapTLS does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
594
			if(!$this->ldap->startTls($this->ldapConnectionRes)) {
595
				throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.');
596
			}
597
		}
598
599
		return true;
600
	}
601
602
	/**
603
	 * Binds to LDAP
604
	 */
605
	public function bind() {
606
		if(!$this->configuration->ldapConfigurationActive) {
0 ignored issues
show
Bug introduced by
The property ldapConfigurationActive does not seem to exist. Did you mean config?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
607
			return false;
608
		}
609
		$cr = $this->getConnectionResource();
610
		if(!$this->ldap->isResource($cr)) {
611
			return false;
612
		}
613
		$ldapLogin = @$this->ldap->bind($cr,
614
										$this->configuration->ldapAgentName,
0 ignored issues
show
Documentation introduced by
The property ldapAgentName does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
615
										$this->configuration->ldapAgentPassword);
0 ignored issues
show
Documentation introduced by
The property ldapAgentPassword does not exist on object<OCA\User_LDAP\Configuration>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
616
		if(!$ldapLogin) {
617
			$errno = $this->ldap->errno($cr);
618
619
			\OCP\Util::writeLog('user_ldap',
620
				'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr),
621
				\OCP\Util::WARN);
622
623
			// Set to failure mode, if LDAP error code is not LDAP_SUCCESS or LDAP_INVALID_CREDENTIALS
624
			if($errno !== 0x00 && $errno !== 0x31) {
625
				$this->ldapConnectionRes = null;
626
			}
627
628
			return false;
629
		}
630
		return true;
631
	}
632
633
}
634