Skip to content

Commit 3ad4bbb

Browse files
authored
Merge pull request #44658 from nextcloud/fix/migrate-away-from-resource-type
fix: Remove obsolete resource typing
2 parents 33c4ddd + b6f5cfa commit 3ad4bbb

26 files changed

+1973
-9852
lines changed

apps/files_external/lib/Lib/Storage/FtpConnection.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/**
56
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
67
*
@@ -27,8 +28,7 @@
2728
* Low level wrapper around the ftp functions that smooths over some difference between servers
2829
*/
2930
class FtpConnection {
30-
/** @var resource|\FTP\Connection */
31-
private $connection;
31+
private \FTP\Connection $connection;
3232

3333
public function __construct(bool $secure, string $hostname, int $port, string $username, string $password) {
3434
if ($secure) {
@@ -50,10 +50,7 @@ public function __construct(bool $secure, string $hostname, int $port, string $u
5050
}
5151

5252
public function __destruct() {
53-
if ($this->connection) {
54-
ftp_close($this->connection);
55-
}
56-
$this->connection = null;
53+
ftp_close($this->connection);
5754
}
5855

5956
public function setUtf8Mode(): bool {

apps/theming/lib/ImageManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ public function updateImage(string $key, string $tmpFile): string {
240240
imagesavealpha($newImage, true);
241241
imagealphablending($newImage, true);
242242

243-
$newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
243+
$newWidth = (imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
244244
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
245245
$outputImage = imagescale($newImage, $newWidth, $newHeight);
246246
if ($outputImage === false) {
247247
throw new \Exception('Could not scale uploaded background image.');
248248
}
249249

250250
$newTmpFile = $this->tempManager->getTemporaryFile();
251-
imageinterlace($outputImage, 1);
251+
imageinterlace($outputImage, true);
252252
// Keep jpeg images encoded as jpeg
253253
if (str_contains($detectedMimeType, 'image/jpeg')) {
254254
if (!imagejpeg($outputImage, $newTmpFile, 90)) {

apps/user_ldap/lib/Access.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,6 @@ public function readAttribute(string $dn, string $attr, string $filter = 'object
188188
return false;
189189
}
190190
$cr = $this->connection->getConnectionResource();
191-
if (!$this->ldap->isResource($cr)) {
192-
//LDAP not available
193-
$this->logger->debug('LDAP resource not available.', ['app' => 'user_ldap']);
194-
return false;
195-
}
196191
$attr = mb_strtolower($attr, 'UTF-8');
197192
// the actual read attribute later may contain parameters on a ranged
198193
// request, e.g. member;range=99-199. Depends on server reply.
@@ -346,11 +341,6 @@ public function setPassword($userDN, $password) {
346341
throw new \Exception('LDAP password changes are disabled.');
347342
}
348343
$cr = $this->connection->getConnectionResource();
349-
if (!$this->ldap->isResource($cr)) {
350-
//LDAP not available
351-
$this->logger->debug('LDAP resource not available.', ['app' => 'user_ldap']);
352-
return false;
353-
}
354344
try {
355345
// try PASSWD extended operation first
356346
return @$this->invokeLDAPMethod('exopPasswd', $userDN, '', $password) ||
@@ -1110,12 +1100,6 @@ private function executeSearch(
11101100
) {
11111101
// See if we have a resource, in case not cancel with message
11121102
$cr = $this->connection->getConnectionResource();
1113-
if (!$this->ldap->isResource($cr)) {
1114-
// Seems like we didn't find any resource.
1115-
// Return an empty array just like before.
1116-
$this->logger->debug('Could not search, because resource is missing.', ['app' => 'user_ldap']);
1117-
return false;
1118-
}
11191103

11201104
//check whether paged search should be attempted
11211105
try {
@@ -1138,7 +1122,7 @@ private function executeSearch(
11381122
/**
11391123
* processes an LDAP paged search operation
11401124
*
1141-
* @param resource|\LDAP\Result|resource[]|\LDAP\Result[] $sr the array containing the LDAP search resources
1125+
* @param \LDAP\Result|\LDAP\Result[] $sr the array containing the LDAP search resources
11421126
* @param int $foundItems number of results in the single search operation
11431127
* @param int $limit maximum results to be counted
11441128
* @param bool $pagedSearchOK whether a paged search has been executed
@@ -1251,7 +1235,7 @@ private function count(
12511235
}
12521236

12531237
/**
1254-
* @param resource|\LDAP\Result|resource[]|\LDAP\Result[] $sr
1238+
* @param \LDAP\Result|\LDAP\Result[] $sr
12551239
* @return int
12561240
* @throws ServerNotAvailableException
12571241
*/

apps/user_ldap/lib/Connection.php

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,15 @@
8686
* @property string ldapAdminGroup
8787
*/
8888
class Connection extends LDAPUtility {
89-
/**
90-
* @var resource|\LDAP\Connection|null
91-
*/
92-
private $ldapConnectionRes = null;
93-
94-
/**
95-
* @var string
96-
*/
97-
private $configPrefix;
98-
99-
/**
100-
* @var ?string
101-
*/
102-
private $configID;
103-
104-
/**
105-
* @var bool
106-
*/
107-
private $configured = false;
89+
private ?\LDAP\Connection $ldapConnectionRes = null;
90+
private string $configPrefix;
91+
private ?string $configID;
92+
private bool $configured = false;
10893

10994
/**
11095
* @var bool whether connection should be kept on __destruct
11196
*/
112-
private $dontDestruct = false;
97+
private bool $dontDestruct = false;
11398

11499
/**
115100
* @var bool runtime flag that indicates whether supported primary groups are available
@@ -241,14 +226,11 @@ public function init($force = false) {
241226
}
242227

243228
/**
244-
* @return resource|\LDAP\Connection The LDAP resource
229+
* @return \LDAP\Connection The LDAP resource
245230
*/
246-
public function getConnectionResource() {
231+
public function getConnectionResource(): \LDAP\Connection {
247232
if (!$this->ldapConnectionRes) {
248233
$this->init();
249-
} elseif (!$this->ldap->isResource($this->ldapConnectionRes)) {
250-
$this->ldapConnectionRes = null;
251-
$this->establishConnection();
252234
}
253235
if (is_null($this->ldapConnectionRes)) {
254236
$this->logger->error(
@@ -263,7 +245,7 @@ public function getConnectionResource() {
263245
/**
264246
* resets the connection resource
265247
*/
266-
public function resetConnectionResource() {
248+
public function resetConnectionResource(): void {
267249
if (!is_null($this->ldapConnectionRes)) {
268250
@$this->ldap->unbind($this->ldapConnectionRes);
269251
$this->ldapConnectionRes = null;
@@ -273,9 +255,8 @@ public function resetConnectionResource() {
273255

274256
/**
275257
* @param string|null $key
276-
* @return string
277258
*/
278-
private function getCacheKey($key) {
259+
private function getCacheKey($key): string {
279260
$prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-';
280261
if (is_null($key)) {
281262
return $prefix;
@@ -332,9 +313,8 @@ public function clearCache() {
332313
* Caches the general LDAP configuration.
333314
* @param bool $force optional. true, if the re-read should be forced. defaults
334315
* to false.
335-
* @return null
336316
*/
337-
private function readConfiguration($force = false) {
317+
private function readConfiguration(bool $force = false): void {
338318
if ((!$this->configured || $force) && !is_null($this->configID)) {
339319
$this->configuration->readConfiguration();
340320
$this->configured = $this->validateConfiguration();
@@ -406,7 +386,7 @@ public function getConfiguration() {
406386
return $result;
407387
}
408388

409-
private function doSoftValidation() {
389+
private function doSoftValidation(): void {
410390
//if User or Group Base are not set, take over Base DN setting
411391
foreach (['ldapBaseUsers', 'ldapBaseGroups'] as $keyBase) {
412392
$val = $this->configuration->$keyBase;
@@ -461,13 +441,9 @@ private function doSoftValidation() {
461441
}
462442
}
463443

464-
/**
465-
* @return bool
466-
*/
467-
private function doCriticalValidation() {
444+
private function doCriticalValidation(): bool {
468445
$configurationOK = true;
469-
$errorStr = 'Configuration Error (prefix '.
470-
(string)$this->configPrefix .'): ';
446+
$errorStr = 'Configuration Error (prefix ' . $this->configPrefix . '): ';
471447

472448
//options that shall not be empty
473449
$options = ['ldapHost', 'ldapUserDisplayName',
@@ -552,7 +528,7 @@ private function doCriticalValidation() {
552528
* Validates the user specified configuration
553529
* @return bool true if configuration seems OK, false otherwise
554530
*/
555-
private function validateConfiguration() {
531+
private function validateConfiguration(): bool {
556532
if ($this->doNotValidate) {
557533
//don't do a validation if it is a new configuration with pure
558534
//default values. Will be allowed on changes via __set or
@@ -575,7 +551,7 @@ private function validateConfiguration() {
575551
*
576552
* @throws ServerNotAvailableException
577553
*/
578-
private function establishConnection() {
554+
private function establishConnection(): ?bool {
579555
if (!$this->configuration->ldapConfigurationActive) {
580556
return null;
581557
}
@@ -663,15 +639,18 @@ private function establishConnection() {
663639
/**
664640
* @param string $host
665641
* @param string $port
666-
* @return bool
667642
* @throws \OC\ServerNotAvailableException
668643
*/
669-
private function doConnect($host, $port) {
644+
private function doConnect($host, $port): bool {
670645
if ($host === '') {
671646
return false;
672647
}
673648

674-
$this->ldapConnectionRes = $this->ldap->connect($host, $port);
649+
$this->ldapConnectionRes = $this->ldap->connect($host, $port) ?: null;
650+
651+
if ($this->ldapConnectionRes === null) {
652+
throw new ServerNotAvailableException('Connection failed');
653+
}
675654

676655
if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
677656
throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');

apps/user_ldap/lib/Group_LDAP.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,10 +1374,10 @@ public function getGroupDetails($gid) {
13741374
* of the current access.
13751375
*
13761376
* @param string $gid
1377-
* @return resource|\LDAP\Connection The LDAP connection
1377+
* @return \LDAP\Connection The LDAP connection
13781378
* @throws ServerNotAvailableException
13791379
*/
1380-
public function getNewLDAPConnection($gid) {
1380+
public function getNewLDAPConnection($gid): \LDAP\Connection {
13811381
$connection = clone $this->access->getConnection();
13821382
return $connection->getConnectionResource();
13831383
}

apps/user_ldap/lib/Group_Proxy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ public function getLDAPAccess($gid) {
371371
* The connection needs to be closed manually.
372372
*
373373
* @param string $gid
374-
* @return resource|\LDAP\Connection The LDAP connection
374+
* @return \LDAP\Connection The LDAP connection
375375
*/
376-
public function getNewLDAPConnection($gid) {
376+
public function getNewLDAPConnection($gid): \LDAP\Connection {
377377
return $this->handleRequest($gid, 'getNewLDAPConnection', [$gid]);
378378
}
379379

apps/user_ldap/lib/IGroupLDAP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getLDAPAccess($gid);
3636
/**
3737
* Return a new LDAP connection for the specified group.
3838
* @param string $gid
39-
* @return resource|\LDAP\Connection The LDAP connection
39+
* @return \LDAP\Connection The LDAP connection
4040
*/
4141
public function getNewLDAPConnection($gid);
4242
}

0 commit comments

Comments
 (0)