image.php 5.37 KB
Newer Older
Henning Leutz's avatar
Henning Leutz committed
1 2
<?php

3 4
if (!\defined('QUIQQER_SYSTEM')) {
    \define('QUIQQER_SYSTEM', true);
5
}
Henning Leutz's avatar
Henning Leutz committed
6

7
require_once 'bootstrap.php';
Henning Leutz's avatar
Henning Leutz committed
8

9
if (!isset($_REQUEST['project']) || !isset($_REQUEST['id'])) {
10
    \header("HTTP/1.0 404 Not Found");
11 12
    exit;
}
13

14
use QUI\Projects\Media;
15
use QUI\Utils\System\File;
16

17 18 19 20 21 22 23 24
/**
 * return mime_type of a file
 *
 * @param string $file
 * @return string
 */
function getMimeType($file)
{
25
    if (!\file_exists($file)) {
26 27 28
        return '';
    }

29 30
    if (\function_exists('mime_content_type')) { // PHP interne Funktionen
        return \mime_content_type($file);
31 32
    }

33 34 35
    if (\function_exists('finfo_open') && \function_exists('finfo_file')) { // PECL
        $finfo = \finfo_open(FILEINFO_MIME);
        $part  = \explode(';', \finfo_file($finfo, $file));
36 37 38 39 40 41 42

        return $part[0];
    }

    return '';
}

43 44
try {
    /* @var $project \QUI\Projects\Project */
Henning Leutz's avatar
Henning Leutz committed
45 46 47
    $Project = QUI\Projects\Manager::getProject($_REQUEST['project']);
    $Media   = $Project->getMedia();
    $File    = $Media->get((int)$_REQUEST['id']);
Henning Leutz's avatar
Henning Leutz committed
48

49 50
    if (Media\Utils::isFolder($File)) {
        QUI\Utils\System\File::send(
51
            BIN_DIR.'16x16/folder.png'
52 53 54 55
        );
        exit;
    }

56
    // Bilder direkt im Browser ausgeben
Henning Leutz's avatar
Henning Leutz committed
57 58
    $file    = $File->getAttribute('file');
    $image   = false;
59 60 61
    $isAdmin = false;

    if (isset($_SERVER['HTTP_REFERER'])
62 63
        && \strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== false
        && \strpos($_SERVER['HTTP_REFERER'], URL_SYS_DIR)
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    ) {
        $isAdmin = true;
    }

    if (isset($_REQUEST['quiadmin'])) {
        $isAdmin = true;
    }

    // admin image request
    if (!isset($_REQUEST['noresize'])
        && !isset($_REQUEST['maxwidth'])
        && !isset($_REQUEST['maxheight'])
        && $isAdmin
    ) {
Henning Leutz's avatar
Henning Leutz committed
78
        $_REQUEST['maxwidth']  = 500;
79 80 81 82
        $_REQUEST['maxheight'] = 500;
    }

    // admin output
83 84
    if ($isAdmin
        && Media\Utils::isImage($File)
Henning Leutz's avatar
Henning Leutz committed
85
        && QUI::getUsers()->getUserBySession()->canUseBackend()
86
    ) {
Henning Leutz's avatar
Henning Leutz committed
87 88 89 90 91 92 93 94
        if (!isset($_REQUEST['maxwidth'])) {
            $_REQUEST['maxwidth'] = null;
        }

        if (!isset($_REQUEST['maxheight'])) {
            $_REQUEST['maxheight'] = null;
        }

95
        if (!$_REQUEST['maxwidth'] && !$_REQUEST['maxheight']) {
Henning Leutz's avatar
Henning Leutz committed
96
            $_REQUEST['maxwidth']  = 500;
97 98 99
            $_REQUEST['maxheight'] = 500;
        }

100
        // cache
101 102 103
        $cacheDir = VAR_DIR.'media/cache/admin/'
                    .$Project->getName().'/'
                    .$Project->getLang().'/';
104 105 106 107

        QUI\Utils\System\File::mkdir($cacheDir);

        // filecache
108
        $ext = \pathinfo($File->getFullPath(), \PATHINFO_EXTENSION);
109

110
        if ($File->getAttribute('mime_type') == 'image/svg+xml') {
111 112
            \header('Content-type: image/svg+xml');
            echo \file_get_contents($File->getFullPath());
113 114 115
            exit;
        }

116 117 118
        $cacheFile = $cacheDir.$File->getId()
                     .'__'.$_REQUEST['maxheight'].'x'
                     .$_REQUEST['maxwidth'].'.'.$ext;
119

120
        if (getMimeType($cacheFile) == 'image/svg+xml') {
121 122
            \header('Content-type: image/svg+xml');
            echo \file_get_contents($cacheFile);
123 124
            exit;
        }
125

126
        if (\file_exists($cacheFile)) {
127
            QUI\Utils\System\File::fileHeader($cacheFile);
128 129 130 131 132
            exit;
        }

        $Image = $Media->getImageManager()->make($File->getFullPath());

Henning Leutz's avatar
Henning Leutz committed
133
        if (isset($_REQUEST['noresize'])) {
134
            $Image->save($cacheFile);
135 136 137 138 139 140 141 142 143
        } else {
            $Image->resize(
                $_REQUEST['maxwidth'],
                $_REQUEST['maxheight'],
                function ($Constraint) {
                    $Constraint->aspectRatio();
                    $Constraint->upsize();
                }
            );
144

145
            $Image->save($cacheFile);
Henning Leutz's avatar
Henning Leutz committed
146 147
        }

148
        QUI\Utils\System\File::fileHeader($cacheFile);
149 150
        exit;
    }
151

152 153 154 155 156

    if (!isset($_REQUEST['noresize'])
        && Media\Utils::isImage($File)
        && (isset($_REQUEST['maxwidth']) || isset($_REQUEST['maxheight']))
    ) {
Henning Leutz's avatar
Henning Leutz committed
157
        $maxwidth  = false;
158 159 160 161
        $maxheight = false;

        if (isset($_REQUEST['maxwidth'])) {
            $maxwidth = (int)$_REQUEST['maxwidth'];
162 163
        }

164 165
        if (isset($_REQUEST['maxheight'])) {
            $maxheight = (int)$_REQUEST['maxheight'];
166 167
        }

168 169 170 171
        $image = $File->createResizeCache($maxwidth, $maxheight);
    }

    if (!$image) {
172
        $image = CMS_DIR.'media/sites/'.$Project->getName().'/'.$file;
173 174
    }

175 176 177
    if (!\file_exists($image)) {
        \header("HTTP/1.0 404 Not Found");
        QUI\System\Log::addInfo('File not exist '.$image);
178
        exit;
Henning Leutz's avatar
Henning Leutz committed
179
    }
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194
    \header("Content-Type: ".$File->getAttribute('mime_type'));
    \header("Expires: ".\gmdate("D, d M Y H:i:s")." GMT");
    \header("Pragma: public");
    \header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    \header("Accept-Ranges: bytes");
    \header("Content-Size: ".\filesize($image));
    \header("Content-Length: ".\filesize($image));
    \header("Last-Modified: ".\gmdate("D, d M Y H:i:s")." GMT");
    \header("Connection: Keep-Alive");
    \header("Content-Disposition: inline; filename=\"".\pathinfo($file, PATHINFO_BASENAME)."\"");

    $fo_image = \fopen($image, "r");
    $fr_image = \fread($fo_image, \filesize($image));
    \fclose($fo_image);
195 196 197

    echo $fr_image;
    exit;
Henning Leutz's avatar
Henning Leutz committed
198
} catch (QUI\Exception $Exception) {
Henning Leutz's avatar
Henning Leutz committed
199
}
Henning Leutz's avatar
Henning Leutz committed
200

201

Henning Leutz's avatar
Henning Leutz committed
202
// wenn es das Bild nicht mehr gibt
203
\header("HTTP/1.0 404 Not Found");
Henning Leutz's avatar
Henning Leutz committed
204
exit;