Skip to content

Commit

Permalink
[TASK] Send only the domain to bing for grabbing an image
Browse files Browse the repository at this point in the history
  • Loading branch information
einpraegsam committed Oct 30, 2024
1 parent f5751d7 commit 870a58f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 7 deletions.
15 changes: 10 additions & 5 deletions Classes/Domain/Service/Image/VisitorImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
declare(strict_types=1);
namespace In2code\Lux\Domain\Service\Image;

use In2code\Lux\Domain\Model\Visitor;
use In2code\Lux\Domain\Service\Provider\CustomerMail;
use In2code\Lux\Utility\ConfigurationUtility;
use In2code\Lux\Utility\EmailUtility;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException;
use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException;
Expand All @@ -16,7 +19,7 @@ class VisitorImageService extends AbstractImageService
public const CACHE_KEY = 'lux_visitor_imageurl';
protected int $size = 150;

public function __construct()
public function __construct(protected readonly CustomerMail $customerMail)
{
$this->cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache(self::CACHE_KEY);
}
Expand All @@ -28,11 +31,13 @@ public function __construct()
*/
protected function buildImageUrl(): string
{
/** @var Visitor $visitor */
$visitor = $this->arguments['visitor'];
$url = '';
$url = $this->getImageUrlFromFrontenduser($url);
$url = $this->getImageUrlFromGravatar($url);
if ($this->arguments['visitor']->isIdentified()) {
$url = $this->getImageFromBing($url, $this->arguments['visitor']->getEmail());
if ($visitor->isIdentified() && $this->customerMail->isB2bEmail($visitor->getEmail())) {
$url = $this->getImageFromBing($url, EmailUtility::getDomainFromEmail($visitor->getEmail()));
}
$url = $this->getDefaultUrl($url);
return $url;
Expand All @@ -47,8 +52,8 @@ protected function getImageUrlFromFrontenduser(string $url): string
$imageService = GeneralUtility::makeInstance(ImageService::class);
$image = $imageService->getImage('', $file, false);
$processConfiguration = [
'width' => (string)$this->size . 'c',
'height' => (string)$this->size . 'c',
'width' => $this->size . 'c',
'height' => $this->size . 'c',
];
$processedImage = $imageService->applyProcessingInstructions($image, $processConfiguration);
$url = $imageService->getImageUri($processedImage, true);
Expand Down
76 changes: 76 additions & 0 deletions Classes/Domain/Service/Provider/CustomerMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);
namespace In2code\Lux\Domain\Service\Provider;

use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;

/**
* Class CustomerMail
* this class can decide if an email belongs to a b2c or b2b contact
*/
class CustomerMail
{
protected array $b2cEmailDomains = [
'1und1.de',
'aok.de',
'aon.at',
'arcor.de',
'barmer.de',
'bluemail.ch',
'bluewin.ch',
'chello.at',
'dak.de',
'email.de',
'ewe.net',
'freenet.de',
'gmail.com',
'gmx.at',
'gmx.ch',
'gmx.de',
'gmx.net',
'googlemail.com',
'hotmail.com',
'hotmail.de',
'htp-tel.de',
'icloud.com',
'ikk-classic.de',
'kabelbw.de',
'live.com',
'mac.com',
'mail.de',
'me.com',
'mozmail.com',
'o2online.de',
'online.de',
'osnanet.de',
'ostfalia.de',
'outlook.com',
'outlook.de',
'posteo.de',
't-online.de',
'telekom.de',
'tk.de',
'versanet.de',
'vodafone.de',
'vodafonemail.de',
'web.de',
'yahoo.com',
'yahoo.de',
];

public function __construct(protected readonly AllowedMail $allowedMail)
{
}

public function isB2cEmail(string $email): bool
{
$domain = strtolower(substr(strrchr($email, '@'), 1));
return in_array($domain, $this->b2cEmailDomains) || $this->allowedMail->isEmailAllowed($email) === false;
}

public function isB2bEmail(string $email): bool
{
return $this->isB2cEmail($email) === false;
}
}
5 changes: 5 additions & 0 deletions Classes/Utility/EmailUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public static function extendEmailReceiverArray(array $emails, string $receiverN
}
return $extendedArray;
}

public static function getDomainFromEmail(string $email): string
{
return strtolower(substr(strrchr($email, '@') ?: '', 1));
}
}
6 changes: 6 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ services:
- name: event.listener
identifier: 'lux/preventReferenceIndex'

In2code\Lux\Domain\Service\Image\VisitorImageService:
public: true

In2code\Lux\Domain\Service\Provider\CustomerMail:
public: true

In2code\Lux\ViewHelpers\Lead\GetDateOfLatestPageVisitAndPageViewHelper:
public: true

Expand Down
2 changes: 1 addition & 1 deletion Documentation/Technical/Installation/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ If you click on the settings symbol for extension lux, you can change some basic
| Advanced: Disable ckeditor configuration | Toggle if an automatic ckeditor configuration should be added or not (for email4link feature) |
| Advanced: Disable ip logging | Disable the logging of the visitors IP address |
| Advanced: Anonymize IP | As an alternative to disableIpLogging, you can anonymize the visitors IP-address when saving. The last part of the IP will be anonymized with "***" |
| Advanced: Lead picture | Decide if TYPO3 should try to find an image of a lead by searching on gravatar.com or on bing image search by given email address. |
| Advanced: Lead picture | Decide if TYPO3 should try to find an image of a lead by searching on gravatar.com (with hashed email) or on bing image search by given email domain (not full address). |
| Advanced: Show render time | For an easier debugging all views in backend can be shown with render times. This is only visible for backend administrators. |
| Advanced: Use cache layer | If you are facing performance issues with lux backend modules or with the page overview view (quick analysis), you can cache views (e.g. for 24h) when turning the feature on. In addition there is a command that can be executed via scheduler task to warmup caches (e.g. every night). |

Expand Down
72 changes: 72 additions & 0 deletions Tests/Unit/Utility/EmailUtilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace In2code\Lux\Tests\Unit\Utility;

use In2code\Lux\Utility\EmailUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
* @coversDefaultClass EmailUtility
*/
class EmailUtilityTest extends UnitTestCase
{
public static function extendEmailReceiverArrayDataProvider(): array
{
return [
[
[
'[email protected]',
'[email protected]',
'[email protected]',
],
null,
[
'[email protected]' => 'receiver',
'[email protected]' => 'receiver',
'[email protected]' => 'receiver',
],
],
[
[
'[email protected]',
'[email protected]',
],
'name',
[
'[email protected]' => 'name',
'[email protected]' => 'name',
],
],
];
}

/**
* @param array $emails
* @param string|null $name
* @param array $expectedResult
* @return void
* @dataProvider extendEmailReceiverArrayDataProvider
* @covers ::extendEmailReceiverArray
*/
public function testExtendEmailReceiverArray(array $emails, ?string $name, array $expectedResult): void
{
if ($name !== null) {
$result = EmailUtility::extendEmailReceiverArray($emails, $name);
} else {
$result = EmailUtility::extendEmailReceiverArray($emails);
}
$this->assertSame($expectedResult, $result);
}

/**
* @return void
* @covers ::getDomainFromEmail
*/
public function testGetDomainFromEmail(): void
{
$this->assertSame('in2code.de', EmailUtility::getDomainFromEmail('[email protected]'));
$this->assertSame('fuz.bayern', EmailUtility::getDomainFromEmail('[email protected]'));
$this->assertSame('', EmailUtility::getDomainFromEmail(''));
$this->assertSame('', EmailUtility::getDomainFromEmail('foobar'));
}
}
2 changes: 1 addition & 1 deletion ext_conf_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ disableIpLogging = 0
# cat=advanced/enable/240; type=boolean; label= Anonymize IP: As an alternative to disableIpLogging, you can anonymize the visitors IP-address when saving. The last part of the IP will be anonymized with "***".
anonymizeIp = 1

# cat=advanced/enable/250; type=options[all,nosearchengine,nogravatar,noexternal]; label= Lead pictures: Decide if TYPO3 should try to find an image of a lead by searching on gravatar.com or on bing image search by given email address.
# cat=advanced/enable/250; type=options[all,nosearchengine,nogravatar,noexternal]; label= Lead pictures: Decide if TYPO3 should try to find an image of a lead by searching on gravatar.com (with hashed email) or on bing image search by given email domain (not full address).
leadImageFromExternalSources = all

# cat=advanced/enable/260; type=boolean; label= Show render time: Show rendering times for all backend views for an easier performance debugging (only visible for administrators).
Expand Down

0 comments on commit 870a58f

Please sign in to comment.