image.php 5.35 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

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

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

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

        return $part[0];
    }

    return '';
}

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

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

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

    if (isset($_SERVER['HTTP_REFERER'])
61 62
        && \strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== false
        && \strpos($_SERVER['HTTP_REFERER'], URL_SYS_DIR)
63 64 65 66 67 68 69 70 71 72 73 74 75 76
    ) {
        $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
77
        $_REQUEST['maxwidth']  = 500;
78 79 80 81
        $_REQUEST['maxheight'] = 500;
    }

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

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

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

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

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

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

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

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

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

125
        if (\file_exists($cacheFile)) {
126 127 128 129 130 131 132
            $Image = $Media->getImageManager()->make($cacheFile);
            echo $Image->response();
            exit;
        }

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

Henning Leutz's avatar
Henning Leutz committed
133
        if (isset($_REQUEST['noresize'])) {
134 135
            $Image->save($cacheFile);

Henning Leutz's avatar
Henning Leutz committed
136 137 138 139
            echo $Image->response();
            exit;
        }

Henning Leutz's avatar
Henning Leutz committed
140 141 142
        echo $Image->resize(
            $_REQUEST['maxwidth'],
            $_REQUEST['maxheight'],
Henning Leutz's avatar
Henning Leutz committed
143 144 145
            function ($Constraint) {
                $Constraint->aspectRatio();
                $Constraint->upsize();
Henning Leutz's avatar
Henning Leutz committed
146 147
            }
        )->response();
148 149 150

        $Image->save($cacheFile);

151 152
        exit;
    }
153

154 155 156 157 158

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

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

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

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

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

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

183 184 185 186 187 188 189 190 191 192 193 194 195 196
    \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);
197 198 199

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

203

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