]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix: Fix typing issues related to resource migration
authorCôme Chilliet <come.chilliet@nextcloud.com>
Tue, 9 Apr 2024 08:48:27 +0000 (10:48 +0200)
committerCôme Chilliet <come.chilliet@nextcloud.com>
Tue, 9 Apr 2024 08:48:27 +0000 (10:48 +0200)
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
apps/theming/lib/ImageManager.php
apps/user_ldap/lib/Access.php
apps/user_ldap/lib/Connection.php
apps/user_ldap/lib/Wizard.php
lib/private/Avatar/Avatar.php
lib/private/legacy/OC_Image.php
lib/public/IStreamImage.php

index f536bae0421deff1febd6bc5052dcaa2408489ed..994e3f35118f94a187572ead9f2cc34eb9d29cca 100644 (file)
@@ -240,7 +240,7 @@ class ImageManager {
                                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) {
@@ -248,7 +248,7 @@ class ImageManager {
                                }
 
                                $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)) {
index 70332cb1c165df9aa061cd5276293d1f6c44f2bb..7732f008a10e00d40d23142acbaaa9a098ea8b4e 100644 (file)
@@ -188,11 +188,6 @@ class Access extends LDAPUtility {
                        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.
@@ -344,11 +339,6 @@ class Access extends LDAPUtility {
                        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) ||
@@ -1108,12 +1098,6 @@ class Access extends LDAPUtility {
        ) {
                // 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 {
index 9fafa7bf8590e3ac5a821afa93dbef304337d1fd..7c780ccbb4a4ff1873d7f80661e477646bdbc648 100644 (file)
@@ -231,9 +231,6 @@ class Connection extends LDAPUtility {
        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(
index be83be66786a57a821fbad442d58b1af052de20e..1b4c9162b71b985b979ccd3be30e4da61ba5b91e 100644 (file)
@@ -645,10 +645,6 @@ class Wizard extends LDAPUtility {
                }
 
                $cr = $this->access->connection->getConnectionResource();
-               if (!$this->ldap->isResource($cr)) {
-                       throw new \Exception('connection error');
-               }
-               /** @var \LDAP\Connection $cr */
 
                if (mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8')
                        === false) {
@@ -1213,9 +1209,7 @@ class Wizard extends LDAPUtility {
                                        $dnReadCount++;
                                        $foundItems = array_merge($foundItems, $newItems);
                                        $dnRead[] = $dn;
-                               } while (($state === self::LRESULT_PROCESSED_SKIP
-                                               || $this->ldap->isResource($entry))
-                                               && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit));
+                               } while ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit);
                        }
                }
 
index 69bf9bacfcf3e5dc4a7de6dcc6a6434270d5e586..020c509b86cb648d4c4c12f4a718cb2ff5ebd96e 100644 (file)
@@ -151,6 +151,7 @@ abstract class Avatar implements IAvatar {
 
        /**
         * Generate png avatar with GD
+        * @throws \Exception when an error occurs in gd calls
         */
        protected function generateAvatar(string $userDisplayName, int $size, bool $darkTheme): string {
                $text = $this->getAvatarText();
@@ -158,6 +159,9 @@ abstract class Avatar implements IAvatar {
                $backgroundColor = $textColor->alphaBlending(0.1, $darkTheme ? new Color(0, 0, 0) : new Color(255, 255, 255));
 
                $im = imagecreatetruecolor($size, $size);
+               if ($im === false) {
+                       throw new \Exception('Failed to create avatar image');
+               }
                $background = imagecolorallocate(
                        $im,
                        $backgroundColor->red(),
@@ -169,6 +173,9 @@ abstract class Avatar implements IAvatar {
                        $textColor->green(),
                        $textColor->blue()
                );
+               if ($background === false || $textColor === false) {
+                       throw new \Exception('Failed to create avatar image color');
+               }
                imagefilledrectangle($im, 0, 0, $size, $size, $background);
 
                $font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
@@ -191,7 +198,7 @@ abstract class Avatar implements IAvatar {
        /**
         * Calculate real image ttf center
         *
-        * @param resource $image
+        * @param \GdImage $image
         * @param string $text text string
         * @param string $font font path
         * @param int $size font size
index 28d4af118f0d9bd09af3b8b1f1e7b984b2319d40..8807eef4b565abe8d13ffe007964082b5330e5f4 100644 (file)
@@ -59,7 +59,7 @@ class OC_Image implements \OCP\IImage {
        // Default quality for webp images
        protected const DEFAULT_WEBP_QUALITY = 80;
 
-       /** @var false|resource|\GdImage */
+       /** @var false|\GdImage */
        protected $resource = false; // tmp resource.
        /** @var int */
        protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident.
@@ -67,20 +67,20 @@ class OC_Image implements \OCP\IImage {
        protected $mimeType = 'image/png'; // Default to png
        /** @var null|string */
        protected $filePath = null;
-       /** @var finfo */
-       private $fileInfo;
+       /** @var ?finfo */
+       private $fileInfo = null;
        /** @var \OCP\ILogger */
        private $logger;
        /** @var \OCP\IConfig */
        private $config;
-       /** @var array */
-       private $exif;
+       /** @var ?array */
+       private $exif = null;
 
        /**
         * Constructor.
         *
-        * @param resource|string|\GdImage $imageRef The path to a local file, a base64 encoded string or a resource created by
-        * an imagecreate* function.
+        * @param mixed $imageRef Deprecated, should be null
+        * @psalm-assert null $imageRef
         * @param \OCP\ILogger $logger
         * @param \OCP\IConfig $config
         * @throws \InvalidArgumentException in case the $imageRef parameter is not null
@@ -107,11 +107,11 @@ class OC_Image implements \OCP\IImage {
        /**
         * Determine whether the object contains an image resource.
         *
+        * @psalm-assert-if-true \GdImage $this->resource
         * @return bool
         */
        public function valid(): bool {
-               if ((is_resource($this->resource) && get_resource_type($this->resource) === 'gd') ||
-                       (is_object($this->resource) && get_class($this->resource) === \GdImage::class)) {
+               if (is_object($this->resource) && get_class($this->resource) === \GdImage::class) {
                        return true;
                }
 
@@ -134,10 +134,7 @@ class OC_Image implements \OCP\IImage {
         */
        public function width(): int {
                if ($this->valid()) {
-                       $width = imagesx($this->resource);
-                       if ($width !== false) {
-                               return $width;
-                       }
+                       return imagesx($this->resource);
                }
                return -1;
        }
@@ -149,10 +146,7 @@ class OC_Image implements \OCP\IImage {
         */
        public function height(): int {
                if ($this->valid()) {
-                       $height = imagesy($this->resource);
-                       if ($height !== false) {
-                               return $height;
-                       }
+                       return imagesy($this->resource);
                }
                return -1;
        }
@@ -338,25 +332,14 @@ class OC_Image implements \OCP\IImage {
        }
 
        /**
-        * @param resource|\GdImage $resource
-        * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd"
+        * @param \GdImage $resource
         */
-       public function setResource($resource) {
-               // For PHP<8
-               if (is_resource($resource) && get_resource_type($resource) === 'gd') {
-                       $this->resource = $resource;
-                       return;
-               }
-               // PHP 8 has real objects for GD stuff
-               if (is_object($resource) && get_class($resource) === \GdImage::class) {
-                       $this->resource = $resource;
-                       return;
-               }
-               throw new \InvalidArgumentException('Supplied resource is not of type "gd".');
+       public function setResource(\GdImage $resource): void {
+               $this->resource = $resource;
        }
 
        /**
-        * @return false|resource|\GdImage Returns the image resource if any
+        * @return false|\GdImage Returns the image resource if any
         */
        public function resource() {
                return $this->resource;
@@ -394,8 +377,7 @@ class OC_Image implements \OCP\IImage {
                                $res = imagepng($this->resource);
                                break;
                        case "image/jpeg":
-                               /** @psalm-suppress InvalidScalarArgument */
-                               imageinterlace($this->resource, (PHP_VERSION_ID >= 80000 ? true : 1));
+                               imageinterlace($this->resource, true);
                                $quality = $this->getJpegQuality();
                                $res = imagejpeg($this->resource, null, $quality);
                                break;
@@ -584,7 +566,7 @@ class OC_Image implements \OCP\IImage {
         * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
         *
         * @param resource $handle
-        * @return resource|\GdImage|false An image resource or false on error
+        * @return \GdImage|false An image resource or false on error
         */
        public function loadFromFileHandle($handle) {
                $contents = stream_get_contents($handle);
@@ -663,7 +645,7 @@ class OC_Image implements \OCP\IImage {
         * Loads an image from a local file.
         *
         * @param bool|string $imagePath The path to a local file.
-        * @return bool|resource|\GdImage An image resource or false on error
+        * @return bool|\GdImage An image resource or false on error
         */
        public function loadFromFile($imagePath = false) {
                // exif_imagetype throws "read error!" if file is less than 12 byte
@@ -801,7 +783,7 @@ class OC_Image implements \OCP\IImage {
         * Loads an image from a string of data.
         *
         * @param string $str A string of image data as read from a file.
-        * @return bool|resource|\GdImage An image resource or false on error
+        * @return bool|\GdImage An image resource or false on error
         */
        public function loadFromData(string $str) {
                if (!$this->checkImageDataSize($str)) {
@@ -827,7 +809,7 @@ class OC_Image implements \OCP\IImage {
         * Loads an image from a base64 encoded string.
         *
         * @param string $str A string base64 encoded string of image data.
-        * @return bool|resource|\GdImage An image resource or false on error
+        * @return bool|\GdImage An image resource or false on error
         */
        public function loadFromBase64(string $str) {
                $data = base64_decode($str);
@@ -868,7 +850,7 @@ class OC_Image implements \OCP\IImage {
 
        /**
         * @param $maxSize
-        * @return resource|bool|\GdImage
+        * @return bool|\GdImage
         */
        private function resizeNew(int $maxSize) {
                if (!$this->valid()) {
@@ -909,7 +891,7 @@ class OC_Image implements \OCP\IImage {
        /**
         * @param int $width
         * @param int $height
-        * @return resource|bool|\GdImage
+        * @return bool|\GdImage
         */
        public function preciseResizeNew(int $width, int $height) {
                if (!($width > 0) || !($height > 0)) {
@@ -985,13 +967,13 @@ class OC_Image implements \OCP\IImage {
 
                // preserve transparency
                if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
-                       imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
+                       imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127) ?: null);
                        imagealphablending($process, false);
                        imagesavealpha($process, true);
                }
 
-               imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
-               if ($process === false) {
+               $result = imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
+               if ($result === false) {
                        $this->logger->debug('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, ['app' => 'core']);
                        return false;
                }
@@ -1027,7 +1009,7 @@ class OC_Image implements \OCP\IImage {
         * @param int $y Vertical position
         * @param int $w Width
         * @param int $h Height
-        * @return resource|\GdImage|false
+        * @return \GdImage|false
         */
        public function cropNew(int $x, int $y, int $w, int $h) {
                if (!$this->valid()) {
@@ -1042,13 +1024,13 @@ class OC_Image implements \OCP\IImage {
 
                // preserve transparency
                if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
-                       imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
+                       imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127) ?: null);
                        imagealphablending($process, false);
                        imagesavealpha($process, true);
                }
 
-               imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
-               if ($process === false) {
+               $result = imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
+               if ($result === false) {
                        $this->logger->debug(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, ['app' => 'core']);
                        return false;
                }
index a93381dd5ed1c968e35b7c84e626342106a977ca..3325e4b79865a29d6f359e8ccd51e2eceae6ebb8 100644 (file)
@@ -27,4 +27,8 @@ namespace OCP;
  * @since 24.0.0
  */
 interface IStreamImage extends IImage {
+       /**
+        * @return false|resource Returns the image resource if any
+        */
+       public function resource();
 }