Passed
Push — master ( af49bb...0a24d1 )
by
unknown
15:19
created

EmailViewHelper::isFrontendAvailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
17
18
use TYPO3\CMS\Core\Http\ApplicationType;
19
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
20
21
/**
22
 * Email link ViewHelper.
23
 * Generates an email link incorporating TYPO3s `spamProtectEmailAddresses`_ TypoScript setting.
24
 *
25
 * .. _spamProtectEmailAddresses: https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/Setup/Config/Index.html#spamprotectemailaddresses
26
 *
27
 * Examples
28
 * ========
29
 *
30
 * Basic email link
31
 * ----------------
32
 *
33
 * ::
34
 *
35
 *    <f:link.email email="[email protected]" />
36
 *
37
 * Output::
38
 *
39
 *    <a href="javascript:linkTo_UnCryptMailto('ocknvq,hqqBdct0vnf');">foo(at)bar.tld</a>
40
 *
41
 * Depending on `spamProtectEmailAddresses`_ setting.
42
 *
43
 * Email link with custom linktext
44
 * -------------------------------
45
 *
46
 * ::
47
 *
48
 *    <f:link.email email="[email protected]">some custom content</f:link.email>
49
 *
50
 * Output::
51
 *
52
 *    <a href="javascript:linkTo_UnCryptMailto('ocknvq,hqqBdct0vnf');">some custom content</a>
53
 *
54
 * Depending on `spamProtectEmailAddresses`_ setting.
55
 */
56
class EmailViewHelper extends AbstractTagBasedViewHelper
57
{
58
    /**
59
     * @var string
60
     */
61
    protected $tagName = 'a';
62
63
    /**
64
     * Arguments initialization
65
     */
66
    public function initializeArguments()
67
    {
68
        parent::initializeArguments();
69
        $this->registerArgument('email', 'string', 'The email address to be turned into a link', true);
70
        $this->registerUniversalTagAttributes();
71
        $this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor');
72
        $this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document');
73
        $this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document');
74
        $this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document');
75
    }
76
77
    /**
78
     * @return string Rendered email link
79
     */
80
    public function render()
81
    {
82
        $email = $this->arguments['email'];
83
84
        if (ApplicationType::fromRequest($this->renderingContext->getRequest())->isFrontend()) {
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on TYPO3Fluid\Fluid\Core\Re...nderingContextInterface. It seems like you code against a sub-type of TYPO3Fluid\Fluid\Core\Re...nderingContextInterface such as TYPO3\CMS\Fluid\Core\Rendering\RenderingContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        if (ApplicationType::fromRequest($this->renderingContext->/** @scrutinizer ignore-call */ getRequest())->isFrontend()) {
Loading history...
85
            [$linkHref, $linkText] = $GLOBALS['TSFE']->cObj->getMailTo($email, '');
86
            $escapeSpecialCharacters = !isset($GLOBALS['TSFE']->spamProtectEmailAddresses) || $GLOBALS['TSFE']->spamProtectEmailAddresses !== 'ascii';
87
        } else {
88
            $linkHref = 'mailto:' . $email;
89
            $linkText = htmlspecialchars($email);
90
            $escapeSpecialCharacters = true;
91
        }
92
        $tagContent = $this->renderChildren();
93
        if ($tagContent !== null) {
94
            $linkText = $tagContent;
95
        }
96
        $this->tag->setContent($linkText);
97
        $this->tag->addAttribute('href', $linkHref, $escapeSpecialCharacters);
98
        $this->tag->forceClosingTag(true);
99
        return $this->tag->render();
100
    }
101
}
102