In PHP>=8.1, LDAP and FTP resources are always typed objects Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>pull/44658/head
@@ -1,6 +1,7 @@ | |||
<?php | |||
declare(strict_types=1); | |||
/** | |||
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl> | |||
* | |||
@@ -27,8 +28,7 @@ namespace OCA\Files_External\Lib\Storage; | |||
* Low level wrapper around the ftp functions that smooths over some difference between servers | |||
*/ | |||
class FtpConnection { | |||
/** @var resource|\FTP\Connection */ | |||
private $connection; | |||
private \FTP\Connection $connection; | |||
public function __construct(bool $secure, string $hostname, int $port, string $username, string $password) { | |||
if ($secure) { |
@@ -1136,7 +1136,7 @@ class Access extends LDAPUtility { | |||
/** | |||
* processes an LDAP paged search operation | |||
* | |||
* @param resource|\LDAP\Result|resource[]|\LDAP\Result[] $sr the array containing the LDAP search resources | |||
* @param \LDAP\Result|\LDAP\Result[] $sr the array containing the LDAP search resources | |||
* @param int $foundItems number of results in the single search operation | |||
* @param int $limit maximum results to be counted | |||
* @param bool $pagedSearchOK whether a paged search has been executed | |||
@@ -1249,7 +1249,7 @@ class Access extends LDAPUtility { | |||
} | |||
/** | |||
* @param resource|\LDAP\Result|resource[]|\LDAP\Result[] $sr | |||
* @param \LDAP\Result|\LDAP\Result[] $sr | |||
* @return int | |||
* @throws ServerNotAvailableException | |||
*/ |
@@ -86,30 +86,15 @@ use Psr\Log\LoggerInterface; | |||
* @property string ldapAdminGroup | |||
*/ | |||
class Connection extends LDAPUtility { | |||
/** | |||
* @var resource|\LDAP\Connection|null | |||
*/ | |||
private $ldapConnectionRes = null; | |||
/** | |||
* @var string | |||
*/ | |||
private $configPrefix; | |||
/** | |||
* @var ?string | |||
*/ | |||
private $configID; | |||
/** | |||
* @var bool | |||
*/ | |||
private $configured = false; | |||
private ?\LDAP\Connection $ldapConnectionRes = null; | |||
private string $configPrefix; | |||
private ?string $configID; | |||
private bool $configured = false; | |||
/** | |||
* @var bool whether connection should be kept on __destruct | |||
*/ | |||
private $dontDestruct = false; | |||
private bool $dontDestruct = false; | |||
/** | |||
* @var bool runtime flag that indicates whether supported primary groups are available | |||
@@ -241,9 +226,9 @@ class Connection extends LDAPUtility { | |||
} | |||
/** | |||
* @return resource|\LDAP\Connection The LDAP resource | |||
* @return \LDAP\Connection The LDAP resource | |||
*/ | |||
public function getConnectionResource() { | |||
public function getConnectionResource(): \LDAP\Connection { | |||
if (!$this->ldapConnectionRes) { | |||
$this->init(); | |||
} elseif (!$this->ldap->isResource($this->ldapConnectionRes)) { | |||
@@ -263,7 +248,7 @@ class Connection extends LDAPUtility { | |||
/** | |||
* resets the connection resource | |||
*/ | |||
public function resetConnectionResource() { | |||
public function resetConnectionResource(): void { | |||
if (!is_null($this->ldapConnectionRes)) { | |||
@$this->ldap->unbind($this->ldapConnectionRes); | |||
$this->ldapConnectionRes = null; | |||
@@ -273,9 +258,8 @@ class Connection extends LDAPUtility { | |||
/** | |||
* @param string|null $key | |||
* @return string | |||
*/ | |||
private function getCacheKey($key) { | |||
private function getCacheKey($key): string { | |||
$prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-'; | |||
if (is_null($key)) { | |||
return $prefix; | |||
@@ -332,9 +316,8 @@ class Connection extends LDAPUtility { | |||
* Caches the general LDAP configuration. | |||
* @param bool $force optional. true, if the re-read should be forced. defaults | |||
* to false. | |||
* @return null | |||
*/ | |||
private function readConfiguration($force = false) { | |||
private function readConfiguration(bool $force = false): void { | |||
if ((!$this->configured || $force) && !is_null($this->configID)) { | |||
$this->configuration->readConfiguration(); | |||
$this->configured = $this->validateConfiguration(); | |||
@@ -406,7 +389,7 @@ class Connection extends LDAPUtility { | |||
return $result; | |||
} | |||
private function doSoftValidation() { | |||
private function doSoftValidation(): void { | |||
//if User or Group Base are not set, take over Base DN setting | |||
foreach (['ldapBaseUsers', 'ldapBaseGroups'] as $keyBase) { | |||
$val = $this->configuration->$keyBase; | |||
@@ -461,10 +444,7 @@ class Connection extends LDAPUtility { | |||
} | |||
} | |||
/** | |||
* @return bool | |||
*/ | |||
private function doCriticalValidation() { | |||
private function doCriticalValidation(): bool { | |||
$configurationOK = true; | |||
$errorStr = 'Configuration Error (prefix '. | |||
(string)$this->configPrefix .'): '; | |||
@@ -552,7 +532,7 @@ class Connection extends LDAPUtility { | |||
* Validates the user specified configuration | |||
* @return bool true if configuration seems OK, false otherwise | |||
*/ | |||
private function validateConfiguration() { | |||
private function validateConfiguration(): bool { | |||
if ($this->doNotValidate) { | |||
//don't do a validation if it is a new configuration with pure | |||
//default values. Will be allowed on changes via __set or | |||
@@ -575,7 +555,7 @@ class Connection extends LDAPUtility { | |||
* | |||
* @throws ServerNotAvailableException | |||
*/ | |||
private function establishConnection() { | |||
private function establishConnection(): ?bool { | |||
if (!$this->configuration->ldapConfigurationActive) { | |||
return null; | |||
} | |||
@@ -663,10 +643,9 @@ class Connection extends LDAPUtility { | |||
/** | |||
* @param string $host | |||
* @param string $port | |||
* @return bool | |||
* @throws \OC\ServerNotAvailableException | |||
*/ | |||
private function doConnect($host, $port) { | |||
private function doConnect($host, $port): bool { | |||
if ($host === '') { | |||
return false; | |||
} |
@@ -1374,10 +1374,10 @@ class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDis | |||
* of the current access. | |||
* | |||
* @param string $gid | |||
* @return resource|\LDAP\Connection The LDAP connection | |||
* @return \LDAP\Connection The LDAP connection | |||
* @throws ServerNotAvailableException | |||
*/ | |||
public function getNewLDAPConnection($gid) { | |||
public function getNewLDAPConnection($gid): \LDAP\Connection { | |||
$connection = clone $this->access->getConnection(); | |||
return $connection->getConnectionResource(); | |||
} |
@@ -371,9 +371,9 @@ class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGet | |||
* The connection needs to be closed manually. | |||
* | |||
* @param string $gid | |||
* @return resource|\LDAP\Connection The LDAP connection | |||
* @return \LDAP\Connection The LDAP connection | |||
*/ | |||
public function getNewLDAPConnection($gid) { | |||
public function getNewLDAPConnection($gid): \LDAP\Connection { | |||
return $this->handleRequest($gid, 'getNewLDAPConnection', [$gid]); | |||
} | |||
@@ -36,7 +36,7 @@ interface IGroupLDAP { | |||
/** | |||
* Return a new LDAP connection for the specified group. | |||
* @param string $gid | |||
* @return resource|\LDAP\Connection The LDAP connection | |||
* @return \LDAP\Connection The LDAP connection | |||
*/ | |||
public function getNewLDAPConnection($gid); | |||
} |
@@ -34,7 +34,7 @@ interface ILDAPWrapper { | |||
/** | |||
* Bind to LDAP directory | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param string $dn an RDN to log in with | |||
* @param string $password the password | |||
* @return bool true on success, false otherwise | |||
@@ -47,14 +47,14 @@ interface ILDAPWrapper { | |||
* connect to an LDAP server | |||
* @param string $host The host to connect to | |||
* @param string $port The port to connect to | |||
* @return resource|\LDAP\Connection|false a link resource on success, otherwise false | |||
* @return \LDAP\Connection|false a link resource on success, otherwise false | |||
*/ | |||
public function connect($host, $port); | |||
/** | |||
* Retrieve the LDAP pagination cookie | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param resource|\LDAP\Result $result LDAP result resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Result $result LDAP result resource | |||
* @param string &$cookie structure sent by LDAP server | |||
* @return bool true on success, false otherwise | |||
* | |||
@@ -64,22 +64,22 @@ interface ILDAPWrapper { | |||
/** | |||
* Count the number of entries in a search | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param resource|\LDAP\Result $result LDAP result resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Result $result LDAP result resource | |||
* @return int|false number of results on success, false otherwise | |||
*/ | |||
public function countEntries($link, $result); | |||
/** | |||
* Return the LDAP error number of the last LDAP command | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @return int error code | |||
*/ | |||
public function errno($link); | |||
/** | |||
* Return the LDAP error message of the last LDAP command | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @return string error message | |||
*/ | |||
public function error($link); | |||
@@ -95,69 +95,69 @@ interface ILDAPWrapper { | |||
/** | |||
* Return first result id | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param resource|\LDAP\Result $result LDAP result resource | |||
* @return resource|\LDAP\ResultEntry an LDAP entry resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Result $result LDAP result resource | |||
* @return \LDAP\ResultEntry an LDAP entry resource | |||
* */ | |||
public function firstEntry($link, $result); | |||
/** | |||
* Get attributes from a search result entry | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param resource|\LDAP\ResultEntry $result LDAP result resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\ResultEntry $result LDAP result resource | |||
* @return array|false containing the results, false on error | |||
* */ | |||
public function getAttributes($link, $result); | |||
/** | |||
* Get the DN of a result entry | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param resource|\LDAP\ResultEntry $result LDAP result resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\ResultEntry $result LDAP result resource | |||
* @return string|false containing the DN, false on error | |||
*/ | |||
public function getDN($link, $result); | |||
/** | |||
* Get all result entries | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param resource|\LDAP\Result $result LDAP result resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Result $result LDAP result resource | |||
* @return array|false containing the results, false on error | |||
*/ | |||
public function getEntries($link, $result); | |||
/** | |||
* Return next result id | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param resource|\LDAP\ResultEntry $result LDAP result resource | |||
* @return resource|\LDAP\ResultEntry an LDAP entry resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\ResultEntry $result LDAP result resource | |||
* @return \LDAP\ResultEntry an LDAP entry resource | |||
* */ | |||
public function nextEntry($link, $result); | |||
/** | |||
* Read an entry | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param string $baseDN The DN of the entry to read from | |||
* @param string $filter An LDAP filter | |||
* @param array $attr array of the attributes to read | |||
* @return resource|\LDAP\Result an LDAP search result resource | |||
* @return \LDAP\Result an LDAP search result resource | |||
*/ | |||
public function read($link, $baseDN, $filter, $attr); | |||
/** | |||
* Search LDAP tree | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param string $baseDN The DN of the entry to read from | |||
* @param string $filter An LDAP filter | |||
* @param array $attr array of the attributes to read | |||
* @param int $attrsOnly optional, 1 if only attribute types shall be returned | |||
* @param int $limit optional, limits the result entries | |||
* @return resource|\LDAP\Result|false an LDAP search result resource, false on error | |||
* @return \LDAP\Result|false an LDAP search result resource, false on error | |||
*/ | |||
public function search($link, string $baseDN, string $filter, array $attr, int $attrsOnly = 0, int $limit = 0, int $pageSize = 0, string $cookie = ''); | |||
/** | |||
* Replace the value of a userPassword by $password | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param string $userDN the DN of the user whose password is to be replaced | |||
* @param string $password the new value for the userPassword | |||
* @return bool true on success, false otherwise | |||
@@ -166,14 +166,14 @@ interface ILDAPWrapper { | |||
/** | |||
* Performs a PASSWD extended operation. | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @return bool|string The generated password if new_password is empty or omitted. Otherwise true on success and false on failure. | |||
*/ | |||
public function exopPasswd($link, string $userDN, string $oldPassword, string $password); | |||
/** | |||
* Sets the value of the specified option to be $value | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @param int $option a defined LDAP Server option | |||
* @param mixed $value the new value for the option | |||
* @return bool true on success, false otherwise | |||
@@ -182,14 +182,14 @@ interface ILDAPWrapper { | |||
/** | |||
* establish Start TLS | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @return bool true on success, false otherwise | |||
*/ | |||
public function startTls($link); | |||
/** | |||
* Unbind from LDAP directory | |||
* @param resource|\LDAP\Connection $link LDAP link resource | |||
* @param \LDAP\Connection $link LDAP link resource | |||
* @return bool true on success, false otherwise | |||
*/ | |||
public function unbind($link); |
@@ -37,7 +37,7 @@ interface IUserLDAP { | |||
/** | |||
* Return a new LDAP connection for the specified user. | |||
* @param string $uid | |||
* @return resource|\LDAP\Connection of the LDAP connection | |||
* @return \LDAP\Connection of the LDAP connection | |||
*/ | |||
public function getNewLDAPConnection($uid); | |||
@@ -360,7 +360,7 @@ class LDAP implements ILDAPWrapper { | |||
/** | |||
* Analyzes the returned LDAP error and acts accordingly if not 0 | |||
* | |||
* @param resource|\LDAP\Connection $resource the LDAP Connection resource | |||
* @param \LDAP\Connection $resource the LDAP Connection resource | |||
* @throws ConstraintViolationException | |||
* @throws ServerNotAvailableException | |||
* @throws \Exception |
@@ -149,7 +149,7 @@ class LDAPProvider implements ILDAPProvider, IDeletionFlagSupport { | |||
* Return a new LDAP connection resource for the specified user. | |||
* The connection must be closed manually. | |||
* @param string $uid user id | |||
* @return resource|\LDAP\Connection The LDAP connection | |||
* @return \LDAP\Connection The LDAP connection | |||
* @throws \Exception if user id was not found in LDAP | |||
*/ | |||
public function getLDAPConnection($uid) { | |||
@@ -163,7 +163,7 @@ class LDAPProvider implements ILDAPProvider, IDeletionFlagSupport { | |||
* Return a new LDAP connection resource for the specified user. | |||
* The connection must be closed manually. | |||
* @param string $gid group id | |||
* @return resource|\LDAP\Connection The LDAP connection | |||
* @return \LDAP\Connection The LDAP connection | |||
* @throws \Exception if group id was not found in LDAP | |||
*/ | |||
public function getGroupLDAPConnection($gid) { |
@@ -622,7 +622,7 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I | |||
* The cloned connection needs to be closed manually. | |||
* of the current access. | |||
* @param string $uid | |||
* @return resource|\LDAP\Connection The LDAP connection | |||
* @return \LDAP\Connection The LDAP connection | |||
*/ | |||
public function getNewLDAPConnection($uid) { | |||
$connection = clone $this->access->getConnection(); |
@@ -438,7 +438,7 @@ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP | |||
* The connection needs to be closed manually. | |||
* | |||
* @param string $uid | |||
* @return resource|\LDAP\Connection The LDAP connection | |||
* @return \LDAP\Connection The LDAP connection | |||
*/ | |||
public function getNewLDAPConnection($uid) { | |||
return $this->handleRequest($uid, 'getNewLDAPConnection', [$uid]); |
@@ -48,8 +48,7 @@ use Psr\Log\LoggerInterface; | |||
class Wizard extends LDAPUtility { | |||
protected static ?IL10N $l = null; | |||
protected Access $access; | |||
/** @var resource|\LDAP\Connection|null */ | |||
protected $cr; | |||
protected ?\LDAP\Connection $cr = null; | |||
protected Configuration $configuration; | |||
protected WizardResult $result; | |||
protected LoggerInterface $logger; | |||
@@ -361,7 +360,7 @@ class Wizard extends LDAPUtility { | |||
if (!$this->ldap->isResource($rr)) { | |||
return false; | |||
} | |||
/** @var resource|\LDAP\Result $rr */ | |||
/** @var \LDAP\Result $rr */ | |||
$er = $this->ldap->firstEntry($cr, $rr); | |||
$attributes = $this->ldap->getAttributes($cr, $er); | |||
if ($attributes === false) { | |||
@@ -649,7 +648,7 @@ class Wizard extends LDAPUtility { | |||
if (!$this->ldap->isResource($cr)) { | |||
throw new \Exception('connection error'); | |||
} | |||
/** @var resource|\LDAP\Connection $cr */ | |||
/** @var \LDAP\Connection $cr */ | |||
if (mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') | |||
=== false) { | |||
@@ -819,7 +818,7 @@ class Wizard extends LDAPUtility { | |||
if (!$this->ldap->isResource($rr)) { | |||
return false; | |||
} | |||
/** @var resource|\LDAP\Result $rr */ | |||
/** @var \LDAP\Result $rr */ | |||
$er = $this->ldap->firstEntry($cr, $rr); | |||
while ($this->ldap->isResource($er)) { | |||
$this->ldap->getDN($cr, $er); | |||
@@ -866,7 +865,7 @@ class Wizard extends LDAPUtility { | |||
); | |||
return false; | |||
} | |||
/** @var resource|\LDAP\Result $rr */ | |||
/** @var \LDAP\Result $rr */ | |||
$entries = $this->ldap->countEntries($cr, $rr); | |||
return ($entries !== false) && ($entries > 0); | |||
} | |||
@@ -929,7 +928,7 @@ class Wizard extends LDAPUtility { | |||
if (!$this->ldap->isResource($rr)) { | |||
continue; | |||
} | |||
/** @var resource|\LDAP\Result $rr */ | |||
/** @var \LDAP\Result $rr */ | |||
$er = $this->ldap->firstEntry($cr, $rr); | |||
$attrs = $this->ldap->getAttributes($cr, $er); | |||
$dn = $this->ldap->getDN($cr, $er); | |||
@@ -1073,7 +1072,7 @@ class Wizard extends LDAPUtility { | |||
if (!$this->ldap->isResource($cr)) { | |||
throw new \Exception(self::$l->t('Invalid Host')); | |||
} | |||
/** @var resource|\LDAP\Connection $cr */ | |||
/** @var \LDAP\Connection $cr */ | |||
//set LDAP options | |||
$this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); | |||
@@ -1169,7 +1168,7 @@ class Wizard extends LDAPUtility { | |||
if (!$this->ldap->isResource($cr)) { | |||
return false; | |||
} | |||
/** @var resource|\LDAP\Connection $cr */ | |||
/** @var \LDAP\Connection $cr */ | |||
$lastFilter = null; | |||
if (isset($filters[count($filters) - 1])) { | |||
$lastFilter = $filters[count($filters) - 1]; | |||
@@ -1184,7 +1183,7 @@ class Wizard extends LDAPUtility { | |||
if (!$this->ldap->isResource($rr)) { | |||
continue; | |||
} | |||
/** @var resource|\LDAP\Result $rr */ | |||
/** @var \LDAP\Result $rr */ | |||
$entries = $this->ldap->countEntries($cr, $rr); | |||
$getEntryFunc = 'firstEntry'; | |||
if (($entries !== false) && ($entries > 0)) { | |||
@@ -1310,9 +1309,9 @@ class Wizard extends LDAPUtility { | |||
} | |||
/** | |||
* @return resource|\LDAP\Connection|false a link resource on success, otherwise false | |||
* @return \LDAP\Connection|false a link resource on success, otherwise false | |||
*/ | |||
private function getConnection() { | |||
private function getConnection(): \LDAP\Connection|false { | |||
if (!is_null($this->cr)) { | |||
return $this->cr; | |||
} |