Skip to content

Commit

Permalink
Merge pull request #44658 from nextcloud/fix/migrate-away-from-resour…
Browse files Browse the repository at this point in the history
…ce-type

fix: Remove obsolete resource typing
  • Loading branch information
come-nc authored Apr 16, 2024
2 parents 33c4ddd + b6f5cfa commit 3ad4bbb
Show file tree
Hide file tree
Showing 26 changed files with 1,973 additions and 9,852 deletions.
9 changes: 3 additions & 6 deletions apps/files_external/lib/Lib/Storage/FtpConnection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
Expand All @@ -27,8 +28,7 @@
* 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) {
Expand All @@ -50,10 +50,7 @@ public function __construct(bool $secure, string $hostname, int $port, string $u
}

public function __destruct() {
if ($this->connection) {
ftp_close($this->connection);
}
$this->connection = null;
ftp_close($this->connection);
}

public function setUtf8Mode(): bool {
Expand Down
4 changes: 2 additions & 2 deletions apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ public function updateImage(string $key, string $tmpFile): string {
imagesavealpha($newImage, true);
imagealphablending($newImage, true);

$newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
$newWidth = (imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
$outputImage = imagescale($newImage, $newWidth, $newHeight);
if ($outputImage === false) {
throw new \Exception('Could not scale uploaded background image.');
}

$newTmpFile = $this->tempManager->getTemporaryFile();
imageinterlace($outputImage, 1);
imageinterlace($outputImage, true);
// Keep jpeg images encoded as jpeg
if (str_contains($detectedMimeType, 'image/jpeg')) {
if (!imagejpeg($outputImage, $newTmpFile, 90)) {
Expand Down
20 changes: 2 additions & 18 deletions apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ public function readAttribute(string $dn, string $attr, string $filter = 'object
return false;
}
$cr = $this->connection->getConnectionResource();
if (!$this->ldap->isResource($cr)) {
//LDAP not available
$this->logger->debug('LDAP resource not available.', ['app' => 'user_ldap']);
return false;
}
$attr = mb_strtolower($attr, 'UTF-8');
// the actual read attribute later may contain parameters on a ranged
// request, e.g. member;range=99-199. Depends on server reply.
Expand Down Expand Up @@ -346,11 +341,6 @@ public function setPassword($userDN, $password) {
throw new \Exception('LDAP password changes are disabled.');
}
$cr = $this->connection->getConnectionResource();
if (!$this->ldap->isResource($cr)) {
//LDAP not available
$this->logger->debug('LDAP resource not available.', ['app' => 'user_ldap']);
return false;
}
try {
// try PASSWD extended operation first
return @$this->invokeLDAPMethod('exopPasswd', $userDN, '', $password) ||
Expand Down Expand Up @@ -1110,12 +1100,6 @@ private function executeSearch(
) {
// See if we have a resource, in case not cancel with message
$cr = $this->connection->getConnectionResource();
if (!$this->ldap->isResource($cr)) {
// Seems like we didn't find any resource.
// Return an empty array just like before.
$this->logger->debug('Could not search, because resource is missing.', ['app' => 'user_ldap']);
return false;
}

//check whether paged search should be attempted
try {
Expand All @@ -1138,7 +1122,7 @@ private function executeSearch(
/**
* 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
Expand Down Expand Up @@ -1251,7 +1235,7 @@ private function count(
}

/**
* @param resource|\LDAP\Result|resource[]|\LDAP\Result[] $sr
* @param \LDAP\Result|\LDAP\Result[] $sr
* @return int
* @throws ServerNotAvailableException
*/
Expand Down
63 changes: 21 additions & 42 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,15 @@
* @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
Expand Down Expand Up @@ -241,14 +226,11 @@ public function init($force = false) {
}

/**
* @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)) {
$this->ldapConnectionRes = null;
$this->establishConnection();
}
if (is_null($this->ldapConnectionRes)) {
$this->logger->error(
Expand All @@ -263,7 +245,7 @@ public function getConnectionResource() {
/**
* resets the connection resource
*/
public function resetConnectionResource() {
public function resetConnectionResource(): void {
if (!is_null($this->ldapConnectionRes)) {
@$this->ldap->unbind($this->ldapConnectionRes);
$this->ldapConnectionRes = null;
Expand All @@ -273,9 +255,8 @@ public function resetConnectionResource() {

/**
* @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;
Expand Down Expand Up @@ -332,9 +313,8 @@ public function clearCache() {
* 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();
Expand Down Expand Up @@ -406,7 +386,7 @@ public function getConfiguration() {
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;
Expand Down Expand Up @@ -461,13 +441,9 @@ private function doSoftValidation() {
}
}

/**
* @return bool
*/
private function doCriticalValidation() {
private function doCriticalValidation(): bool {
$configurationOK = true;
$errorStr = 'Configuration Error (prefix '.
(string)$this->configPrefix .'): ';
$errorStr = 'Configuration Error (prefix ' . $this->configPrefix . '): ';

//options that shall not be empty
$options = ['ldapHost', 'ldapUserDisplayName',
Expand Down Expand Up @@ -552,7 +528,7 @@ private function doCriticalValidation() {
* 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
Expand All @@ -575,7 +551,7 @@ private function validateConfiguration() {
*
* @throws ServerNotAvailableException
*/
private function establishConnection() {
private function establishConnection(): ?bool {
if (!$this->configuration->ldapConfigurationActive) {
return null;
}
Expand Down Expand Up @@ -663,15 +639,18 @@ private function establishConnection() {
/**
* @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;
}

$this->ldapConnectionRes = $this->ldap->connect($host, $port);
$this->ldapConnectionRes = $this->ldap->connect($host, $port) ?: null;

if ($this->ldapConnectionRes === null) {
throw new ServerNotAvailableException('Connection failed');
}

if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -1374,10 +1374,10 @@ public function getGroupDetails($gid) {
* 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();
}
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/lib/Group_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ public function getLDAPAccess($gid) {
* 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]);
}

Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/IGroupLDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getLDAPAccess($gid);
/**
* 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);
}
Loading

0 comments on commit 3ad4bbb

Please sign in to comment.