• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 6809777185

09 Nov 2023 09:22AM UTC coverage: 22.835%. Remained the same
6809777185

push

github

koertho
deprecate twig filters and tests

2 of 32 new or added lines in 8 files covered. (6.25%)

4 existing lines in 4 files now uncovered.

1255 of 5496 relevant lines covered (22.83%)

1.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Twig/DownloadExtension.php
1
<?php
2

3
/*
4
 * Copyright (c) 2023 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Twig;
10

11
use Contao\Config;
12
use Contao\Controller;
13
use Contao\CoreBundle\Exception\ResponseException;
14
use Contao\Dbafs;
15
use Contao\Environment;
16
use Contao\File;
17
use Contao\Image;
18
use Contao\StringUtil;
19
use Contao\System;
20
use Contao\Validator;
21
use HeimrichHannot\UtilsBundle\Util\Utils;
22
use Symfony\Component\HttpFoundation\RequestStack;
23
use Twig\Error\Error;
24
use Twig\Extension\AbstractExtension;
25
use Twig\TwigFilter;
26
use Twig_Error;
27

28
class DownloadExtension extends AbstractExtension
29
{
30
    /**
31
     * @var RequestStack
32
     */
33
    private $requestStack;
34
    /**
35
     * @var Utils
36
     */
37
    private $utils;
38
    /**
39
     * @var \Twig\Environment
40
     */
41
    private $twig;
42

43
    public function __construct(RequestStack $requestStack, Utils $utils, \Twig\Environment $twig)
44
    {
45
        $this->requestStack = $requestStack;
×
46
        $this->utils = $utils;
×
47
        $this->twig = $twig;
×
48
    }
49

50
    /**
51
     * Get list of twig filters.
52
     *
53
     * @return array|\Twig_SimpleFilter[]
54
     */
55
    public function getFilters()
56
    {
57
        return [
×
NEW
58
            new TwigFilter('download', [$this, 'getDownload', ['deprecated' => true]]),
×
NEW
59
            new TwigFilter('download_link', [$this, 'getDownloadLink'], ['deprecated' => true]),
×
NEW
60
            new TwigFilter('download_path', [$this, 'getDownloadPath'], ['deprecated' => true]),
×
NEW
61
            new TwigFilter('download_data', [$this, 'getDownloadData'], ['deprecated' => true]),
×
NEW
62
            new TwigFilter('download_title', [$this, 'getDownloadTitle'], ['deprecated' => true]),
×
UNCOV
63
        ];
×
64
    }
65

66
    /**
67
     * Get download data based on given path/uuid.
68
     *
69
     * @param mixed $path File path/uuid
70
     * @param array $data Add custom data here
71
     *
72
     * @return array|null Download element data
73
     */
74
    public function getDownloadData($path, array $data = []): ?array
75
    {
76
        if (Validator::isUuid($path)) {
×
77
            $path = $this->utils->file()->getPathFromUuid($path);
×
78
        } else {
79
            $path = Controller::replaceInsertTags($path);
×
80
        }
81

82
        if (!$path) {
×
83
            return null;
×
84
        }
85

86
        try {
87
            $file = new File(urldecode($path));
×
88
        } catch (\Exception $e) {
×
89
            return null;
×
90
        }
91

92
        $model = $file->getModel();
×
93

94
        if (null === $model) {
×
95
            try {
96
                Dbafs::addResource($file->path);
×
97
            } catch (\Exception $e) {
×
98
                return null;
×
99
            }
100

101
            if (null === $model) {
×
102
                return null;
×
103
            }
104
        }
105

106
        $request = $this->requestStack->getCurrentRequest();
×
107

108
        if (!$request) {
×
109
            return null;
×
110
        }
111

112
        $requestedFile = $request->query->get('file');
×
113

114
        // Send the file to the browser and do not send a 404 header (see #4632)
115
        if ('' != $requestedFile && $requestedFile == $file->path) {
×
116
            try {
117
                ob_clean();
×
118
                Controller::sendFileToBrowser($requestedFile);
×
119
            } catch (ResponseException $e) {
×
120
                $e->getResponse()->send();
×
121
            }
122

123
            return null;
×
124
        }
125

126
        $fileData['model'] = $file->getModel()->row();
×
127

128
        $allowedDownload = StringUtil::trimsplit(',', strtolower(Config::get('allowedDownload')));
×
129

130
        // Return if the file type is not allowed
131
        if (!\in_array($file->extension, $allowedDownload)) {
×
132
            return null;
×
133
        }
134

135
        if (!isset($data['linkTitle'])) {
×
136
            $fileData['linkTitle'] = StringUtil::specialchars($file->basename);
×
137
        }
138

139
        if (!isset($data['titleText'])) {
×
140
            $data['titleText'] = sprintf($GLOBALS['TL_LANG']['MSC']['download'], $file->basename);
×
141
        }
142

143
        $strHref = Environment::get('request');
×
144

145
        // Remove an existing file parameter (see #5683)
146
        $strHref = $this->utils->url()->addQueryStringParameterToUrl('file='.System::urlEncode($file->value), $strHref);
×
147

148
        $fileData['link'] = $fileData['linkTitle'];
×
149
        $fileData['title'] = StringUtil::specialchars($data['titleText']);
×
150
        $fileData['href'] = $strHref;
×
151
        $fileData['filesize'] = System::getReadableSize($file->filesize, 1);
×
152
        $fileData['icon'] = Image::getPath($file->icon);
×
153
        $fileData['mime'] = $file->mime;
×
154
        $fileData['extension'] = $file->extension;
×
155
        $fileData['path'] = $file->dirname;
×
156

157
        return array_merge($fileData, $data);
×
158
    }
159

160
    /**
161
     * Get download based on given path/uuid.
162
     *
163
     * @param mixed  $path     File path/uuid
164
     * @param bool   $download Return link as download link if true, as download path if false
165
     * @param array  $data     Add custom data here
166
     * @param string $template Use custom download template
167
     *
168
     * @throws \Twig\Error\LoaderError
169
     * @throws \Twig\Error\RuntimeError
170
     * @throws \Twig\Error\SyntaxError
171
     *
172
     * @return string Download html element
173
     */
174
    public function getDownload($path, bool $download = true, array $data = [], string $template = '@HeimrichHannotContaoUtils/download.html.twig'): string
175
    {
176
        if (null === ($data = $this->getDownloadData($path, $data))) {
×
177
            return '';
×
178
        }
179

180
        if (false === $download) {
×
181
            $data['href'] = $data['model']['path'];
×
182
            $data['target'] = true;
×
183
        }
184

185
        try {
186
            return $this->twig->render($template, $data);
×
187
        } catch (Twig_Error $e) {
×
188
        } catch (Error $error) {
×
189
        }
190

191
        return '';
×
192
    }
193

194
    /**
195
     * Get download link `?file=` based on given path/uuid.
196
     *
197
     * @param mixed $path File path/uuid
198
     * @param array $data Add custom data here
199
     *
200
     * @return string File download link
201
     */
202
    public function getDownloadLink($path, array $data = []): string
203
    {
204
        if (null === ($data = $this->getDownloadData($path, $data))) {
×
205
            return '';
×
206
        }
207

208
        return $data['href'];
×
209
    }
210

211
    /**
212
     * Get download path based on given path/uuid.
213
     *
214
     * @param mixed $path File path/uuid
215
     * @param array $data Add custom data here
216
     *
217
     * @return string File path
218
     */
219
    public function getDownloadPath($path, array $data = []): string
220
    {
221
        if (null === ($data = $this->getDownloadData($path, $data))) {
×
222
            return '';
×
223
        }
224

225
        return $data['model']['path'];
×
226
    }
227

228
    /**
229
     * Get download title based on given path/uuid.
230
     *
231
     * @param mixed $path File path/uuid
232
     * @param array $data Add custom data here
233
     *
234
     * @return string Download title
235
     */
236
    public function getDownloadTitle($path, array $data = []): string
237
    {
238
        if (null === ($data = $this->getDownloadData($path, $data))) {
×
239
            return '';
×
240
        }
241

242
        return $data['title'];
×
243
    }
244
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc