Passed
Push — master ( 9c67ab...22c7cd )
by
unknown
14:11
created

CanonicalGenerator::isPageWithinSiteRoot()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Seo\Canonical;
19
20
use Psr\EventDispatcher\EventDispatcherInterface;
21
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Core\Utility\RootlineUtility;
24
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
25
use TYPO3\CMS\Frontend\Utility\CanonicalizationUtility;
26
use TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent;
27
28
/**
29
 * Class to add the canonical tag to the page
30
 *
31
 * @internal this class is not part of TYPO3's Core API.
32
 */
33
class CanonicalGenerator
34
{
35
    /**
36
     * @var TypoScriptFrontendController
37
     */
38
    protected $typoScriptFrontendController;
39
40
    /**
41
     * @var PageRepository
42
     */
43
    protected $pageRepository;
44
45
    /**
46
     * @var EventDispatcherInterface
47
     */
48
    protected $eventDispatcher;
49
50
    public function __construct(TypoScriptFrontendController $typoScriptFrontendController = null, EventDispatcherInterface $eventDispatcher = null)
51
    {
52
        $this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
53
        $this->typoScriptFrontendController = $typoScriptFrontendController ?? $this->getTypoScriptFrontendController();
54
        $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class);
55
    }
56
57
    public function generate(): string
58
    {
59
        if ($this->typoScriptFrontendController->config['config']['disableCanonical'] ?? false) {
60
            return '';
61
        }
62
63
        $event = $this->eventDispatcher->dispatch(new ModifyUrlForCanonicalTagEvent(''));
64
        $href = $event->getUrl();
65
66
        if (empty($href) && (int)$this->typoScriptFrontendController->page['no_index'] === 1) {
67
            return '';
68
        }
69
70
        if (empty($href)) {
71
            // 1) Check if page show content from other page
72
            $href = $this->checkContentFromPid();
73
        }
74
        if (empty($href)) {
75
            // 2) Check if page has canonical URL set
76
            $href = $this->checkForCanonicalLink();
77
        }
78
        if (empty($href)) {
79
            // 3) Fallback, create canonical URL
80
            $href = $this->checkDefaultCanonical();
81
        }
82
83
        if (!empty($href)) {
84
            $canonical = '<link ' . GeneralUtility::implodeAttributes([
85
                    'rel' => 'canonical',
86
                    'href' => $href
87
                ], true) . '/>' . LF;
88
            $this->typoScriptFrontendController->additionalHeaderData[] = $canonical;
89
            return $canonical;
90
        }
91
        return '';
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    protected function checkForCanonicalLink(): string
98
    {
99
        if (!empty($this->typoScriptFrontendController->page['canonical_link'])) {
100
            return $this->typoScriptFrontendController->cObj->typoLink_URL([
101
                'parameter' => $this->typoScriptFrontendController->page['canonical_link'],
102
                'forceAbsoluteUrl' => true,
103
            ]);
104
        }
105
        return '';
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    protected function checkContentFromPid(): string
112
    {
113
        if (!empty($this->typoScriptFrontendController->page['content_from_pid'])) {
114
            $parameter = (int)$this->typoScriptFrontendController->page['content_from_pid'];
115
            if ($parameter > 0) {
116
                $targetPage = $this->pageRepository->getPage($parameter, true);
117
                if (!empty($targetPage['canonical_link'])) {
118
                    $parameter = $targetPage['canonical_link'];
119
                }
120
                return $this->typoScriptFrontendController->cObj->typoLink_URL([
121
                    'parameter' => $parameter,
122
                    'forceAbsoluteUrl' => true,
123
                ]);
124
            }
125
        }
126
        return '';
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    protected function checkDefaultCanonical(): string
133
    {
134
        // We should only create a canonical link to the target, if the target is within a valid site root
135
        $inSiteRoot = $this->isPageWithinSiteRoot((int)$this->typoScriptFrontendController->id);
136
        if (!$inSiteRoot) {
137
            return '';
138
        }
139
140
        // Temporarily remove current mountpoint information as we want to have the
141
        // URL of the target page and not of the page within the mountpoint if the
142
        // current page is a mountpoint.
143
        $previousMp = $this->typoScriptFrontendController->MP;
144
        $this->typoScriptFrontendController->MP = '';
145
146
        $link = $this->typoScriptFrontendController->cObj->typoLink_URL([
147
            'parameter' => $this->typoScriptFrontendController->id . ',' . $this->typoScriptFrontendController->type,
148
            'forceAbsoluteUrl' => true,
149
            'addQueryString' => true,
150
            'addQueryString.' => [
151
                'exclude' => implode(
152
                    ',',
153
                    CanonicalizationUtility::getParamsToExcludeForCanonicalizedUrl(
154
                        (int)$this->typoScriptFrontendController->id,
155
                        (array)$GLOBALS['TYPO3_CONF_VARS']['FE']['additionalCanonicalizedUrlParameters']
156
                    )
157
                )
158
            ]
159
        ]);
160
        $this->typoScriptFrontendController->MP = $previousMp;
161
        return $link;
162
    }
163
164
    protected function isPageWithinSiteRoot(int $id): bool
165
    {
166
        $rootline = GeneralUtility::makeInstance(RootlineUtility::class, $id)->get();
167
        foreach ($rootline as $page) {
168
            if ($page['is_siteroot']) {
169
                return true;
170
            }
171
        }
172
        return false;
173
    }
174
175
    /**
176
     * @return TypoScriptFrontendController
177
     */
178
    protected function getTypoScriptFrontendController(): TypoScriptFrontendController
179
    {
180
        return $GLOBALS['TSFE'];
181
    }
182
}
183