From a0d00c88f8c08da2818bcec645d5e62118b920ba Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 14 Jul 2023 17:21:05 +0200 Subject: [PATCH] Use short circuits and replace large string concats with `\sprintf()` --- .../system/bbcode/AttachmentBBCode.class.php | 185 ++++++++++-------- 1 file changed, 104 insertions(+), 81 deletions(-) diff --git a/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php b/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php index a0df4a7fbf..54228c8cb4 100644 --- a/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/AttachmentBBCode.class.php @@ -35,45 +35,21 @@ final class AttachmentBBCode extends AbstractBBCode $outputType = $parser->getOutputType(); if ($attachment->showAsImage() && $attachment->canViewPreview() && ($outputType == 'text/html' || $outputType == 'text/simplified-html')) { - return $this->renderAsImage($attachment, $outputType, $openingTag['attributes']); + return $this->showImage($attachment, $outputType, $openingTag['attributes']); } elseif (\substr($attachment->fileType, 0, 6) === 'video/' && $outputType == 'text/html') { - return $this->renderAsVideoPlayer($attachment); + return $this->showVideoPlayer($attachment); } elseif (\substr($attachment->fileType, 0, 6) === 'audio/' && $outputType == 'text/html') { - return $this->rederAsAudioPlayer($attachment); + return $this->showAudioPlayer($attachment); } return StringUtil::getAnchorTag($attachment->getLink(), $attachment->filename); } - private function renderAsImage(Attachment $attachment, string $outputType, array $attributes): string + private function showImage(Attachment $attachment, string $outputType, array $attributes): string { - // image - $alignment = ($attributes[1] ?? ''); - $thumbnail = ($attributes[2] ?? false); + $alignment = $attributes[1] ?? ''; - // backward compatibility, check if width is larger than thumbnail's width to display full version - if (\is_numeric($thumbnail)) { - if ($thumbnail == 0) { - $thumbnail = true; - } else { - // true if supplied width is smaller or equal to thumbnail's width - $thumbnail = ($attachment->thumbnailWidth >= $thumbnail) ? true : false; - } - } elseif ($thumbnail === 'false') { - $thumbnail = false; - } elseif ($thumbnail !== false) { - $thumbnail = true; - } - - // always use thumbnail in simplified version - if ($outputType == 'text/simplified-html') { - $thumbnail = true; - } - - // Force the use of the thumbnail if the user cannot access the full version. - if (!$thumbnail && !$attachment->canDownload()) { - $thumbnail = true; - } + $thumbnail = $this->renderImageAsThumbnail($attachment, $outputType, $attributes[2] ?? false); $hasParentLink = false; if (!empty($closingTag['__parents'])) { @@ -86,77 +62,124 @@ final class AttachmentBBCode extends AbstractBBCode } } - if (!$thumbnail) { + if ($thumbnail === false) { $class = ''; - if ($alignment == 'left' || $alignment == 'right') { + if ($alignment === 'left' || $alignment === 'right') { $class = 'messageFloatObject' . \ucfirst($alignment); } $source = StringUtil::encodeHTML($attachment->getLink()); $title = StringUtil::encodeHTML($attachment->filename); - $result = ''; + $image = \sprintf( + '', + $source, + $attachment->width, + $attachment->height, + ); if (!$hasParentLink && ($attachment->width > ATTACHMENT_THUMBNAIL_WIDTH || $attachment->height > ATTACHMENT_THUMBNAIL_HEIGHT)) { $icon = FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24); - $result = << - {$result} - - {$icon} - - - HTML; - } else { - $result = '' . $result . ''; - } - } else { - $icon = FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24); - $enlarge = '' . $icon . ''; - - $linkParameters = [ - 'object' => $attachment, - ]; - if ($attachment->hasThumbnail()) { - $linkParameters['thumbnail'] = 1; + return << + {$image} + + {$icon} + + + HTML; } - $class = ''; - if ($alignment == 'left' || $alignment == 'right') { - $class = 'messageFloatObject' . \ucfirst($alignment); - } + return \sprintf( + '%s', + $title, + $class, + $image, + ); + } - $imageClasses = ''; - if (!$attachment->hasThumbnail()) { - $imageClasses = 'embeddedAttachmentLink jsResizeImage'; - } + $icon = FontAwesomeIcon::fromValues('magnifying-glass')->toHtml(24); + $enlarge = '' . $icon . ''; - if ($class && (!$attachment->hasThumbnail() || !$attachment->canDownload())) { - $imageClasses .= ' ' . $class; - } + $linkParameters = [ + 'object' => $attachment, + ]; + if ($attachment->hasThumbnail()) { + $linkParameters['thumbnail'] = 1; + } + + $class = ''; + if ($alignment == 'left' || $alignment == 'right') { + $class = 'messageFloatObject' . \ucfirst($alignment); + } - $result = ''; + $imageClasses = ''; + if (!$attachment->hasThumbnail()) { + $imageClasses = 'embeddedAttachmentLink jsResizeImage'; + } - if (!$hasParentLink && $attachment->hasThumbnail() && $attachment->canDownload()) { - $result = '' . $result . $enlarge . ''; - } else { - if (\str_contains($imageClasses, 'embeddedAttachmentLink')) { - $result = $result . $enlarge; - } + if ($class && (!$attachment->hasThumbnail() || !$attachment->canDownload())) { + $imageClasses .= ' ' . $class; + } + + $image = \sprintf( + '', + StringUtil::encodeHTML(LinkHandler::getInstance()->getLink('Attachment', $linkParameters)), + $imageClasses, + $attachment->hasThumbnail() ? $attachment->thumbnailWidth : $attachment->width, + $attachment->hasThumbnail() ? $attachment->thumbnailHeight : $attachment->height, + ); + + if (!$hasParentLink && $attachment->hasThumbnail() && $attachment->canDownload()) { + return \sprintf( + '%s%s', + StringUtil::encodeHTML(LinkHandler::getInstance()->getLink('Attachment', ['object' => $attachment])), + StringUtil::encodeHTML($attachment->filename), + $class, + $image, + $enlarge, + ); + } + + return \sprintf( + '%s%s', + $class, + $image, + \str_contains($imageClasses, 'embeddedAttachmentLink') ? $enlarge : '', + ); + } + + private function renderImageAsThumbnail(Attachment $attachment, string $outputType, mixed $thumbnail): bool + { + // Always use thumbnails for the simplified HTML output. + if ($outputType == 'text/simplified-html') { + return true; + } - $result = '' . $result . ''; + // WCF 2.x permitted image resizing using exact pixel values. These + // values are interpreted as a signal for the use of thumbnails. + if (\is_numeric($thumbnail)) { + if ($thumbnail === 0) { + $thumbnail = true; + } else { + // Interpret the number as a request for the thumbnail if the + // width matches or falls short of the thumbnail’s width. + $thumbnail = ($attachment->thumbnailWidth >= $thumbnail); } + } elseif ($thumbnail === 'false') { + $thumbnail = false; + } elseif ($thumbnail !== false) { + $thumbnail = true; + } + + // Force the use of the thumbnail if the user cannot access the full version. + if (!$thumbnail && !$attachment->canDownload()) { + $thumbnail = true; } - return $result; + return $thumbnail; } - private function renderAsVideoPlayer(Attachment $attachment): string + private function showVideoPlayer(Attachment $attachment): string { return WCF::getTPL()->fetch('__videoAttachmentBBCode', 'wcf', [ 'attachment' => $attachment, @@ -164,7 +187,7 @@ final class AttachmentBBCode extends AbstractBBCode ]); } - private function rederAsAudioPlayer(Attachment $attachment): string + private function showAudioPlayer(Attachment $attachment): string { return WCF::getTPL()->fetch('__audioAttachmentBBCode', 'wcf', [ 'attachment' => $attachment, -- 2.20.1