1<?php
2namespace TYPO3\CMS\Frontend\ContentObject;
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
17use TYPO3\CMS\Core\Type\File\ImageInfo;
18use TYPO3\CMS\Core\Utility\GeneralUtility;
19use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
20use TYPO3\CMS\Frontend\Resource\FilePathSanitizer;
21
22/**
23 * Contains FILE class object.
24 * @deprecated FILE cObject will be removed in TYPO3 v10.0.
25 */
26class FileContentObject extends AbstractContentObject
27{
28    /**
29     * Rendering the cObject, FILE
30     *
31     * @param array $conf Array of TypoScript properties
32     * @return string Output
33     */
34    public function render($conf = [])
35    {
36        $this->getTypoScriptFrontendController()->logDeprecatedTyposcript('cObject FILE', 'Use IMAGE to show images instead.');
37        $theValue = '';
38        $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
39        try {
40            $file = GeneralUtility::makeInstance(FilePathSanitizer::class)->sanitize($file);
41            if (file_exists($file)) {
42                $fileInfo = GeneralUtility::split_fileref($file);
43                $extension = $fileInfo['fileext'];
44                if ($extension === 'jpg' || $extension === 'jpeg' || $extension === 'gif' || $extension === 'png') {
45                    $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $file);
46                    $altParameters = trim($this->cObj->getAltParam($conf, false));
47                    $theValue = '<img src="'
48                        . htmlspecialchars($this->getTypoScriptFrontendController()->absRefPrefix . $file)
49                        . '" width="' . (int)$imageInfo->getWidth() . '" height="' . (int)$imageInfo->getHeight()
50                        . '"' . $this->cObj->getBorderAttr(' border="0"') . ' ' . $altParameters . ' />';
51                } elseif (filesize($file) < 1024 * 1024) {
52                    $theValue = file_get_contents($file);
53                }
54            }
55        } catch (\TYPO3\CMS\Core\Resource\Exception $e) {
56            // do nothing
57        }
58        $linkWrap = isset($conf['linkWrap.']) ? $this->cObj->stdWrap($conf['linkWrap'], $conf['linkWrap.']) : $conf['linkWrap'];
59        if ($linkWrap) {
60            $theValue = $this->cObj->linkWrap($theValue, $linkWrap);
61        }
62        $wrap = isset($conf['wrap.']) ? $this->cObj->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
63        if ($wrap) {
64            $theValue = $this->cObj->wrap($theValue, $wrap);
65        }
66        if (isset($conf['stdWrap.'])) {
67            $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
68        }
69        return $theValue;
70    }
71
72    /**
73     * @return TypoScriptFrontendController
74     */
75    protected function getTypoScriptFrontendController()
76    {
77        return $GLOBALS['TSFE'];
78    }
79}
80