Skip to content

Commit

Permalink
fix(mail): Fix big logos in mail templates for Outlook
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jul 11, 2024
1 parent e31f474 commit 7e6ce49
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
13 changes: 13 additions & 0 deletions apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public function delete(string $key): void {
} catch (NotFoundException $e) {
} catch (NotPermittedException $e) {
}

if ($key === 'logo') {
$this->config->deleteAppValue('theming', 'logoDimensions');

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::deleteAppValue has been marked as deprecated
}
}

public function updateImage(string $key, string $tmpFile): string {
Expand Down Expand Up @@ -258,6 +262,15 @@ public function updateImage(string $key, string $tmpFile): string {

$target->putContent(file_get_contents($tmpFile));

if ($key === 'logo') {
$newImage = @imagecreatefromstring(file_get_contents($tmpFile));
if ($newImage === false) {
throw new \Exception('Could not read background image, possibly corrupted.');
}

$this->config->setAppValue('theming', 'logoDimensions', imagesx($newImage) . 'x' . imagesy($newImage));

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::setAppValue has been marked as deprecated
}

return $detectedMimeType;
}

Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@
'OC\\Repair\\RemoveLinkShares' => $baseDir . '/lib/private/Repair/RemoveLinkShares.php',
'OC\\Repair\\RepairDavShares' => $baseDir . '/lib/private/Repair/RepairDavShares.php',
'OC\\Repair\\RepairInvalidShares' => $baseDir . '/lib/private/Repair/RepairInvalidShares.php',
'OC\\Repair\\RepairLogoDimension' => $baseDir . '/lib/private/Repair/RepairLogoDimension.php',
'OC\\Repair\\RepairMimeTypes' => $baseDir . '/lib/private/Repair/RepairMimeTypes.php',
'OC\\RichObjectStrings\\Validator' => $baseDir . '/lib/private/RichObjectStrings/Validator.php',
'OC\\Route\\CachingRouter' => $baseDir . '/lib/private/Route/CachingRouter.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Repair\\RemoveLinkShares' => __DIR__ . '/../../..' . '/lib/private/Repair/RemoveLinkShares.php',
'OC\\Repair\\RepairDavShares' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairDavShares.php',
'OC\\Repair\\RepairInvalidShares' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairInvalidShares.php',
'OC\\Repair\\RepairLogoDimension' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairLogoDimension.php',
'OC\\Repair\\RepairMimeTypes' => __DIR__ . '/../../..' . '/lib/private/Repair/RepairMimeTypes.php',
'OC\\RichObjectStrings\\Validator' => __DIR__ . '/../../..' . '/lib/private/RichObjectStrings/Validator.php',
'OC\\Route\\CachingRouter' => __DIR__ . '/../../..' . '/lib/private/Route/CachingRouter.php',
Expand Down
6 changes: 4 additions & 2 deletions lib/private/Mail/EMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class EMailTemplate implements IEMailTemplate {
<tbody>
<tr style="padding:0;text-align:left;vertical-align:top">
<center data-parsed="" style="background-color:%s;min-width:175px;max-height:175px; padding:35px 0px;border-radius:200px">
<img class="logo float-center" src="%s" alt="%s" align="center" style="-ms-interpolation-mode:bicubic;clear:both;display:block;float:none;margin:0 auto;outline:0;text-align:center;text-decoration:none;max-height:105px;max-width:105px;width:auto;height:auto">
<img class="logo float-center" src="%s" alt="%s" align="center" style="-ms-interpolation-mode:bicubic;clear:both;display:block;float:none;margin:0 auto;outline:0;text-align:center;text-decoration:none;max-height:105px;max-width:105px;width:auto;height:auto" width="%s" height="%s">
</center>
</tr>
</tbody>
Expand Down Expand Up @@ -308,6 +308,8 @@ public function __construct(
protected Defaults $themingDefaults,
protected IURLGenerator $urlGenerator,
protected IFactory $l10nFactory,
protected int $logoWidth,
protected int $logoHeight,
protected string $emailId,
protected array $data,
) {
Expand All @@ -331,7 +333,7 @@ public function addHeader(): void {
$this->headerAdded = true;

$logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo(false));
$this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getDefaultColorPrimary(), $logoUrl, $this->themingDefaults->getName()]);
$this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getDefaultColorPrimary(), $logoUrl, $this->themingDefaults->getName(), $this->logoWidth, $this->logoHeight]);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
* @package OC\Mail
*/
class Mailer implements IMailer {
// Do not move this block or change it's content without contacting the release crew
public const DEFAULT_DIMENSIONS = '252x120';
// Do not move this block or change it's content without contacting the release crew

public const MAX_LOGO_SIZE = 105;

private ?MailerInterface $instance = null;

public function __construct(
Expand Down Expand Up @@ -109,10 +115,33 @@ public function createEMailTemplate(string $emailId, array $data = []): IEMailTe
);
}

$logoDimensions = $this->config->getAppValue('theming', 'logoDimensions', self::DEFAULT_DIMENSIONS);
[$width, $height] = explode('x', $logoDimensions);
$width = (int) $width;
$height = (int) $height;

if ($width > self::MAX_LOGO_SIZE || $height > self::MAX_LOGO_SIZE) {
if ($width === $height) {
$logoWidth = self::MAX_LOGO_SIZE;
$logoHeight = self::MAX_LOGO_SIZE;
} elseif ($width > $height) {
$logoWidth = self::MAX_LOGO_SIZE;
$logoHeight = (int) (($height / $width) * self::MAX_LOGO_SIZE);
} else {
$logoWidth = (int) (($width / $height) * self::MAX_LOGO_SIZE);
$logoHeight = self::MAX_LOGO_SIZE;
}
} else {
$logoWidth = $width;
$logoHeight = $height;
}

return new EMailTemplate(
$this->defaults,
$this->urlGenerator,
$this->l10nFactory,
$logoWidth,
$logoHeight,
$emailId,
$data
);
Expand Down
59 changes: 59 additions & 0 deletions lib/private/Repair/RepairLogoDimension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Repair;

use OCA\Theming\ImageManager;
use OCP\IConfig;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use OCP\Server;

class RepairLogoDimension implements IRepairStep {
public function __construct(
protected IConfig $config,
) {
}

public function getName(): string {
return 'Cache logo dimension to fix size in emails on Outlook';
}

public function run(IOutput $output): void {
$logoDimensions = $this->config->getAppValue('theming', 'logoDimensions');
if (preg_match('/^\d+x\d+$/', $logoDimensions)) {
$output->info('Theming is not used to provide a logo');
return;
}

try {
/** @var ImageManager $imageManager */
$imageManager = Server::get(ImageManager::class);
} catch (\Throwable) {
$output->info('Theming is disabled');
return;
}

if (!$imageManager->hasImage('logo')) {
$output->info('Theming is not used to provide a logo');
return;
}

$simpleFile = $imageManager->getImage('logo', false);

$image = @imagecreatefromstring($simpleFile->getContent());
if ($image === false) {
$output->warning('Failed to read dimensions from logo');
return;
}

$dimensions = imagesx($image) . 'x' . imagesy($image);
$this->config->setAppValue('theming', 'logoDimensions', $dimensions);
$output->info('Updated logo dimensions: ' . $dimensions);
}
}

0 comments on commit 7e6ce49

Please sign in to comment.