Passed
Pull Request — master (#201)
by
unknown
09:49 queued 02:44
created

FileUrlViewHelper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 32
dl 0
loc 84
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 8 2
A buildFileUri() 0 33 6
A render() 0 12 2
A initializeArguments() 0 5 1
1
<?php
2
namespace EWW\Dpf\ViewHelpers;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19
20
class FileUrlViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper
21
{
22
    /**
23
     * Secret API key for delivering inactive documents.
24
     * @var string
25
     */
26
    private $secretKey;
27
28
    /**
29
     * Initialize secret key from plugin TYPOScript configuration.
30
     */
31
    public function initialize() {
32
        parent::initialize();
33
34
        $configurationManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
35
        $settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
36
37
        if (isset($settings['plugin.']['tx_dpf.']['settings.']['deliverInactiveSecretKey'])) {
38
            $this->secretKey = $settings['plugin.']['tx_dpf.']['settings.']['deliverInactiveSecretKey'];
39
        }
40
    }
41
42
    public function initializeArguments()
43
    {
44
        parent::initializeArguments();
45
46
        $this->registerArgument('uri', 'string', '', true);
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function render()
53
    {
54
        $uri = $this->arguments['uri'];
55
56
        $fileUri = $this->buildFileUri($uri);
57
58
        // pass configured API secret key parameter to enable dissemination for inactive documents
59
        if (isset($this->secretKey)) {
60
            $fileUri .= '?tx_dpf[deliverInactive]=' . $this->secretKey;
61
        }
62
63
        return $fileUri;
64
    }
65
66
    /**
67
     * Construct file URI
68
     * @param $uri
69
     * @return string
70
     */
71
    protected function buildFileUri($uri)
72
    {
73
        $uploadFileUrl = new \EWW\Dpf\Helper\UploadFileUrl;
74
75
        if (strpos(strtolower($uri), "datastreams")) {
76
            $regex = '/\/(\w*:\d*)\/datastreams\/(\w*-\d*)/';
77
            preg_match($regex, $uri, $treffer);
78
79
            if (!empty($treffer)) {
80
                $qid = $treffer[1];
81
                $fid = $treffer[2];
82
                $uri = $uploadFileUrl->getBaseUrl() . '/api/' . urlencode($qid) . '/attachment/' . $fid;
83
            }
84
        }
85
86
        $host = parse_url($uri, PHP_URL_HOST);
87
88
        // If in docker (development) mode: IP address needs to be replaced by "localhost", since docker container IP addresses
89
        // are not reachable from TYPO3 frontend.
90
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
91
        $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
92
        $settings = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);
93
        if (
94
            $settings['development']['docker'] && (
95
                $host == '127.0.0.1' ||
96
                !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
97
                )
98
            )
99
        ) {
100
            $uri = str_replace($host, "localhost", $uri);
101
        }
102
103
        return $uri;
104
    }
105
106
}
107