Completed
Push — master ( 627023...930aad )
by Morris
15:19
created

Connection::getFromCache()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
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 Brent Bloxam <[email protected]>
8
 * @author Jarkko Lehtoranta <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Jörn Friedrich Dreyer <[email protected]>
11
 * @author Lukas Reschke <[email protected]>
12
 * @author Lyonel Vincent <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Robin Appelman <[email protected]>
15
 * @author Robin McCorkell <[email protected]>
16
 * @author Roeland Jago Douma <[email protected]>
17
 * @author root <[email protected]>
18
 * @author Victor Dubiniuk <[email protected]>
19
 * @author Xuanwo <[email protected]>
20
 *
21
 * @license AGPL-3.0
22
 *
23
 * This code is free software: you can redistribute it and/or modify
24
 * it under the terms of the GNU Affero General Public License, version 3,
25
 * as published by the Free Software Foundation.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License, version 3,
33
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
34
 *
35
 */
36
37
namespace OCA\User_LDAP;
38
39
use OC\ServerNotAvailableException;
40
41
/**
42
 * magic properties (incomplete)
43
 * responsible for LDAP connections in context with the provided configuration
44
 *
45
 * @property string ldapHost
46
 * @property string ldapPort holds the port number
47
 * @property string ldapUserFilter
48
 * @property string ldapUserDisplayName
49
 * @property string ldapUserDisplayName2
50
 * @property boolean turnOnPasswordChange
51
 * @property boolean hasPagedResultSupport
52
 * @property string[] ldapBaseUsers
53
 * @property int|null ldapPagingSize holds an integer
54
 * @property bool|mixed|void ldapGroupMemberAssocAttr
55
 * @property string ldapUuidUserAttribute
56
 * @property string ldapUuidGroupAttribute
57
 * @property string ldapExpertUUIDUserAttr
58
 * @property string ldapExpertUUIDGroupAttr
59
 */
60
class Connection extends LDAPUtility {
61
	private $ldapConnectionRes = null;
62
	private $configPrefix;
63
	private $configID;
64
	private $configured = false;
65
	private $hasPagedResultSupport = true;
66
	//whether connection should be kept on __destruct
67
	private $dontDestruct = false;
68
69
	/**
70
	 * @var bool runtime flag that indicates whether supported primary groups are available
71
	 */
72
	public $hasPrimaryGroups = true;
73
74
	/**
75
	 * @var bool runtime flag that indicates whether supported POSIX gidNumber are available
76
	 */
77
	public $hasGidNumber = true;
78
79
	//cache handler
80
	protected $cache;
81
82
	/** @var Configuration settings handler **/
83
	protected $configuration;
84
85
	protected $doNotValidate = false;
86
87
	protected $ignoreValidation = false;
88
89
	protected $bindResult = [];
90
91
	/**
92
	 * Constructor
93
	 * @param ILDAPWrapper $ldap
94
	 * @param string $configPrefix a string with the prefix for the configkey column (appconfig table)
95
	 * @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections
96
	 */
97
	public function __construct(ILDAPWrapper $ldap, $configPrefix = '', $configID = 'user_ldap') {
98
		parent::__construct($ldap);
99
		$this->configPrefix = $configPrefix;
100
		$this->configID = $configID;
101
		$this->configuration = new Configuration($configPrefix,
102
												 !is_null($configID));
103
		$memcache = \OC::$server->getMemCacheFactory();
104
		if($memcache->isAvailable()) {
105
			$this->cache = $memcache->createDistributed();
106
		}
107
		$helper = new Helper(\OC::$server->getConfig());
108
		$this->doNotValidate = !in_array($this->configPrefix,
109
			$helper->getServerConfigurationPrefixes());
110
		$this->hasPagedResultSupport =
111
			(int)$this->configuration->ldapPagingSize !== 0
112
			|| $this->ldap->hasPagedResultSupport();
113
	}
114
115
	public function __destruct() {
116
		if(!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) {
117
			@$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...
118
			$this->bindResult = [];
119
		}
120
	}
121
122
	/**
123
	 * defines behaviour when the instance is cloned
124
	 */
125
	public function __clone() {
126
		$this->configuration = new Configuration($this->configPrefix,
127
												 !is_null($this->configID));
128
		$this->ldapConnectionRes = null;
129
		$this->dontDestruct = true;
130
	}
131
132
	/**
133
	 * @param string $name
134
	 * @return bool|mixed
135
	 */
136
	public function __get($name) {
137
		if(!$this->configured) {
138
			$this->readConfiguration();
139
		}
140
141
		if($name === 'hasPagedResultSupport') {
142
			return $this->hasPagedResultSupport;
143
		}
144
145
		return $this->configuration->$name;
146
	}
147
148
	/**
149
	 * @param string $name
150
	 * @param mixed $value
151
	 */
152
	public function __set($name, $value) {
153
		$this->doNotValidate = false;
154
		$before = $this->configuration->$name;
155
		$this->configuration->$name = $value;
156
		$after = $this->configuration->$name;
157
		if($before !== $after) {
158
			if ($this->configID !== '' && $this->configID !== null) {
159
				$this->configuration->saveConfiguration();
160
			}
161
			$this->validateConfiguration();
162
		}
163
	}
164
165
	/**
166
	 * sets whether the result of the configuration validation shall
167
	 * be ignored when establishing the connection. Used by the Wizard
168
	 * in early configuration state.
169
	 * @param bool $state
170
	 */
171
	public function setIgnoreValidation($state) {
172
		$this->ignoreValidation = (bool)$state;
173
	}
174
175
	/**
176
	 * initializes the LDAP backend
177
	 * @param bool $force read the config settings no matter what
178
	 */
179
	public function init($force = false) {
180
		$this->readConfiguration($force);
181
		$this->establishConnection();
182
	}
183
184
	/**
185
	 * Returns the LDAP handler
186
	 */
187
	public function getConnectionResource() {
188
		if(!$this->ldapConnectionRes) {
189
			$this->init();
190
		} else if(!$this->ldap->isResource($this->ldapConnectionRes)) {
191
			$this->ldapConnectionRes = null;
192
			$this->establishConnection();
193
		}
194
		if(is_null($this->ldapConnectionRes)) {
195
			\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...
196
			throw new ServerNotAvailableException('Connection to LDAP server could not be established');
197
		}
198
		return $this->ldapConnectionRes;
199
	}
200
201
	/**
202
	 * resets the connection resource
203
	 */
204
	public function resetConnectionResource() {
205
		if(!is_null($this->ldapConnectionRes)) {
206
			@$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...
207
			$this->ldapConnectionRes = null;
208
			$this->bindResult = [];
209
		}
210
	}
211
212
	/**
213
	 * @param string|null $key
214
	 * @return string
215
	 */
216
	private function getCacheKey($key) {
217
		$prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-';
218
		if(is_null($key)) {
219
			return $prefix;
220
		}
221
		return $prefix.md5($key);
222
	}
223
224
	/**
225
	 * @param string $key
226
	 * @return mixed|null
227
	 */
228
	public function getFromCache($key) {
229
		if(!$this->configured) {
230
			$this->readConfiguration();
231
		}
232
		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...
233
			return null;
234
		}
235
		$key = $this->getCacheKey($key);
236
237
		return json_decode(base64_decode($this->cache->get($key)), true);
238
	}
239
240
	/**
241
	 * @param string $key
242
	 * @param mixed $value
243
	 *
244
	 * @return string
245
	 */
246
	public function writeToCache($key, $value) {
247
		if(!$this->configured) {
248
			$this->readConfiguration();
249
		}
250
		if(is_null($this->cache)
251
			|| !$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...
252
			|| !$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...
253
			return null;
254
		}
255
		$key   = $this->getCacheKey($key);
256
		$value = base64_encode(json_encode($value));
257
		$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...
258
	}
259
260
	public function clearCache() {
261
		if(!is_null($this->cache)) {
262
			$this->cache->clear($this->getCacheKey(null));
263
		}
264
	}
265
266
	/**
267
	 * Caches the general LDAP configuration.
268
	 * @param bool $force optional. true, if the re-read should be forced. defaults
269
	 * to false.
270
	 * @return null
271
	 */
272
	private function readConfiguration($force = false) {
273
		if((!$this->configured || $force) && !is_null($this->configID)) {
274
			$this->configuration->readConfiguration();
275
			$this->configured = $this->validateConfiguration();
276
		}
277
	}
278
279
	/**
280
	 * set LDAP configuration with values delivered by an array, not read from configuration
281
	 * @param array $config array that holds the config parameters in an associated array
282
	 * @param array &$setParameters optional; array where the set fields will be given to
283
	 * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters
284
	 */
285
	public function setConfiguration($config, &$setParameters = null) {
286
		if(is_null($setParameters)) {
287
			$setParameters = array();
288
		}
289
		$this->doNotValidate = false;
290
		$this->configuration->setConfiguration($config, $setParameters);
291
		if(count($setParameters) > 0) {
292
			$this->configured = $this->validateConfiguration();
293
		}
294
295
296
		return $this->configured;
297
	}
298
299
	/**
300
	 * saves the current Configuration in the database and empties the
301
	 * cache
302
	 * @return null
303
	 */
304
	public function saveConfiguration() {
305
		$this->configuration->saveConfiguration();
306
		$this->clearCache();
307
	}
308
309
	/**
310
	 * get the current LDAP configuration
311
	 * @return array
312
	 */
313
	public function getConfiguration() {
314
		$this->readConfiguration();
315
		$config = $this->configuration->getConfiguration();
316
		$cta = $this->configuration->getConfigTranslationArray();
317
		$result = array();
318
		foreach($cta as $dbkey => $configkey) {
319
			switch($configkey) {
320
				case 'homeFolderNamingRule':
321
					if(strpos($config[$configkey], 'attr:') === 0) {
322
						$result[$dbkey] = substr($config[$configkey], 5);
323
					} else {
324
						$result[$dbkey] = '';
325
					}
326
					break;
327
				case 'ldapBase':
328
				case 'ldapBaseUsers':
329
				case 'ldapBaseGroups':
330
				case 'ldapAttributesForUserSearch':
331
				case 'ldapAttributesForGroupSearch':
332
					if(is_array($config[$configkey])) {
333
						$result[$dbkey] = implode("\n", $config[$configkey]);
334
						break;
335
					} //else follows default
336
				default:
337
					$result[$dbkey] = $config[$configkey];
338
			}
339
		}
340
		return $result;
341
	}
342
343
	private function doSoftValidation() {
344
		//if User or Group Base are not set, take over Base DN setting
345
		foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) {
346
			$val = $this->configuration->$keyBase;
347
			if(empty($val)) {
348
				$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...
349
			}
350
		}
351
352
		foreach(array('ldapExpertUUIDUserAttr'  => 'ldapUuidUserAttribute',
353
					  'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute')
354
				as $expertSetting => $effectiveSetting) {
355
			$uuidOverride = $this->configuration->$expertSetting;
356
			if(!empty($uuidOverride)) {
357
				$this->configuration->$effectiveSetting = $uuidOverride;
358
			} else {
359
				$uuidAttributes = Access::UUID_ATTRIBUTES;
360
				array_unshift($uuidAttributes, 'auto');
361
				if(!in_array($this->configuration->$effectiveSetting,
362
							$uuidAttributes)
363
					&& (!is_null($this->configID))) {
364
					$this->configuration->$effectiveSetting = 'auto';
365
					$this->configuration->saveConfiguration();
366
					\OCP\Util::writeLog('user_ldap',
367
										'Illegal value for the '.
368
										$effectiveSetting.', '.'reset to '.
369
										'autodetect.', \OCP\Util::INFO);
370
				}
371
372
			}
373
		}
374
375
		$backupPort = (int)$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...
376
		if ($backupPort <= 0) {
377
			$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...
378
		}
379
380
		//make sure empty search attributes are saved as simple, empty array
381
		$saKeys = array('ldapAttributesForUserSearch',
382
						'ldapAttributesForGroupSearch');
383
		foreach($saKeys as $key) {
384
			$val = $this->configuration->$key;
385
			if(is_array($val) && count($val) === 1 && empty($val[0])) {
386
				$this->configuration->$key = array();
387
			}
388
		}
389
390
		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...
391
			&& $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...
392
			$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...
393
			\OCP\Util::writeLog('user_ldap',
394
								'LDAPS (already using secure connection) and '.
395
								'TLS do not work together. Switched off TLS.',
396
								\OCP\Util::INFO);
397
		}
398
	}
399
400
	/**
401
	 * @return bool
402
	 */
403
	private function doCriticalValidation() {
404
		$configurationOK = true;
405
		$errorStr = 'Configuration Error (prefix '.
406
			(string)$this->configPrefix .'): ';
407
408
		//options that shall not be empty
409
		$options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName',
410
						 'ldapGroupDisplayName', 'ldapLoginFilter');
411
		foreach($options as $key) {
412
			$val = $this->configuration->$key;
413
			if(empty($val)) {
414
				switch($key) {
415
					case 'ldapHost':
416
						$subj = 'LDAP Host';
417
						break;
418
					case 'ldapPort':
419
						$subj = 'LDAP Port';
420
						break;
421
					case 'ldapUserDisplayName':
422
						$subj = 'LDAP User Display Name';
423
						break;
424
					case 'ldapGroupDisplayName':
425
						$subj = 'LDAP Group Display Name';
426
						break;
427
					case 'ldapLoginFilter':
428
						$subj = 'LDAP Login Filter';
429
						break;
430
					default:
431
						$subj = $key;
432
						break;
433
				}
434
				$configurationOK = false;
435
				\OCP\Util::writeLog('user_ldap',
436
									$errorStr.'No '.$subj.' given!',
437
									\OCP\Util::WARN);
438
			}
439
		}
440
441
		//combinations
442
		$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...
443
		$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...
444
		if (
445
			($agent === ''  && $pwd !== '')
446
			|| ($agent !== '' && $pwd === '')
447
		) {
448
			\OCP\Util::writeLog('user_ldap',
449
								$errorStr.'either no password is given for the '.
450
								'user agent or a password is given, but not an '.
451
								'LDAP agent.',
452
				\OCP\Util::WARN);
453
			$configurationOK = false;
454
		}
455
456
		$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...
457
		$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...
458
		$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...
459
460
		if(empty($base) && empty($baseUsers) && empty($baseGroups)) {
461
			\OCP\Util::writeLog('user_ldap',
462
								$errorStr.'Not a single Base DN given.',
463
								\OCP\Util::WARN);
464
			$configurationOK = false;
465
		}
466
467
		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...
468
		   === false) {
469
			\OCP\Util::writeLog('user_ldap',
470
								$errorStr.'login filter does not contain %uid '.
471
								'place holder.',
472
								\OCP\Util::WARN);
473
			$configurationOK = false;
474
		}
475
476
		return $configurationOK;
477
	}
478
479
	/**
480
	 * Validates the user specified configuration
481
	 * @return bool true if configuration seems OK, false otherwise
482
	 */
483
	private function validateConfiguration() {
484
485
		if($this->doNotValidate) {
486
			//don't do a validation if it is a new configuration with pure
487
			//default values. Will be allowed on changes via __set or
488
			//setConfiguration
489
			return false;
490
		}
491
492
		// first step: "soft" checks: settings that are not really
493
		// necessary, but advisable. If left empty, give an info message
494
		$this->doSoftValidation();
495
496
		//second step: critical checks. If left empty or filled wrong, mark as
497
		//not configured and give a warning.
498
		return $this->doCriticalValidation();
499
	}
500
501
502
	/**
503
	 * Connects and Binds to LDAP
504
	 */
505
	private function establishConnection() {
506
		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...
507
			return null;
508
		}
509
		static $phpLDAPinstalled = true;
510
		if(!$phpLDAPinstalled) {
511
			return false;
512
		}
513
		if(!$this->ignoreValidation && !$this->configured) {
514
			\OCP\Util::writeLog('user_ldap',
515
								'Configuration is invalid, cannot connect',
516
								\OCP\Util::WARN);
517
			return false;
518
		}
519
		if(!$this->ldapConnectionRes) {
520
			if(!$this->ldap->areLDAPFunctionsAvailable()) {
521
				$phpLDAPinstalled = false;
522
				\OCP\Util::writeLog('user_ldap',
523
									'function ldap_connect is not available. Make '.
524
									'sure that the PHP ldap module is installed.',
525
									\OCP\Util::ERROR);
526
527
				return false;
528
			}
529
			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...
530
				if(putenv('LDAPTLS_REQCERT=never')) {
531
					\OCP\Util::writeLog('user_ldap',
532
						'Turned off SSL certificate validation successfully.',
533
						\OCP\Util::DEBUG);
534
				} else {
535
					\OCP\Util::writeLog('user_ldap',
536
										'Could not turn off SSL certificate validation.',
537
										\OCP\Util::WARN);
538
				}
539
			}
540
541
			$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...
542
				|| $this->getFromCache('overrideMainServer'));
543
			$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...
544
			$bindStatus = false;
545
			$error = -1;
546
			try {
547
				if (!$isOverrideMainServer) {
548
					$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...
549
						$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...
550
					$bindStatus = $this->bind();
551
					$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...
552
						$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...
553
				}
554
				if($bindStatus === true) {
555
					return $bindStatus;
556
				}
557
			} catch (ServerNotAvailableException $e) {
558
				if(!$isBackupHost) {
559
					throw $e;
560
				}
561
			}
562
563
			//if LDAP server is not reachable, try the Backup (Replica!) Server
564
			if($isBackupHost && ($error !== 0 || $isOverrideMainServer)) {
565
				$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...
566
								 $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...
567
				$this->bindResult = [];
568
				$bindStatus = $this->bind();
569
				$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...
570
					$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...
571
				if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) {
572
					//when bind to backup server succeeded and failed to main server,
573
					//skip contacting him until next cache refresh
574
					$this->writeToCache('overrideMainServer', true);
575
				}
576
			}
577
578
			return $bindStatus;
579
		}
580
		return null;
581
	}
582
583
	/**
584
	 * @param string $host
585
	 * @param string $port
586
	 * @return bool
587
	 * @throws \OC\ServerNotAvailableException
588
	 */
589
	private function doConnect($host, $port) {
590
		if ($host === '') {
591
			return false;
592
		}
593
594
		$this->ldapConnectionRes = $this->ldap->connect($host, $port);
595
596
		if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
597
			throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');
598
		}
599
600
		if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) {
601
			throw new ServerNotAvailableException('Could not disable LDAP referrals.');
602
		}
603
604
		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...
605
			if(!$this->ldap->startTls($this->ldapConnectionRes)) {
606
				throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.');
607
			}
608
		}
609
610
		return true;
611
	}
612
613
	/**
614
	 * Binds to LDAP
615
	 */
616
	public function bind() {
617
		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...
618
			return false;
619
		}
620
		$cr = $this->ldapConnectionRes;
621
		if(!$this->ldap->isResource($cr)) {
0 ignored issues
show
Documentation introduced by
$cr 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...
622
			$cr = $this->getConnectionResource();
623
		}
624
625
		if(
626
			count($this->bindResult) !== 0
627
			&& $this->bindResult['dn'] === $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...
628
			&& \OC::$server->getHasher()->verify(
629
				$this->configPrefix . $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...
630
				$this->bindResult['hash']
631
			)
632
		) {
633
			// don't attempt to bind again with the same data as before
634
			// bind might have been invoked via getConnectionResource(),
635
			// but we need results specifically for e.g. user login
636
			return $this->bindResult['result'];
637
		}
638
639
		$ldapLogin = @$this->ldap->bind($cr,
0 ignored issues
show
Documentation introduced by
$cr 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...
640
										$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...
641
										$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...
642
643
		$this->bindResult = [
644
			'dn' => $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...
645
			'hash' => \OC::$server->getHasher()->hash($this->configPrefix . $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...
646
			'result' => $ldapLogin,
647
		];
648
649
		if(!$ldapLogin) {
650
			$errno = $this->ldap->errno($cr);
0 ignored issues
show
Documentation introduced by
$cr 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...
651
652
			\OCP\Util::writeLog('user_ldap',
653
				'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr),
0 ignored issues
show
Documentation introduced by
$cr 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...
654
				\OCP\Util::WARN);
655
656
			// Set to failure mode, if LDAP error code is not LDAP_SUCCESS or LDAP_INVALID_CREDENTIALS
657
			if($errno !== 0x00 && $errno !== 0x31) {
658
				$this->ldapConnectionRes = null;
659
			}
660
661
			return false;
662
		}
663
		return true;
664
	}
665
666
}
667