1f62ea8a1Sandi<?php 2d4f83172SAndreas Gohr 3f62ea8a1Sandi/** 4f62ea8a1Sandi * DokuWiki media passthrough file 5f62ea8a1Sandi * 6f62ea8a1Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org> 8f62ea8a1Sandi */ 95a5ec053SGerrit Uitslag 10e3c3abf1SAndreas Gohruse dokuwiki\Input\Input; 11e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 12e1d9dcc8SAndreas Gohr 13e3c3abf1SAndreas Gohrif (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../'); 147fb7960fSChristopher Smithif (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1); 15f62ea8a1Sandirequire_once(DOKU_INC . 'inc/init.php'); 1636625b96SAndreas Gohrsession_write_close(); //close session 178746e727Sandi 187fb7960fSChristopher Smithrequire_once(DOKU_INC . 'inc/fetch.functions.php'); 197fb7960fSChristopher Smith 207fb7960fSChristopher Smithif (defined('SIMPLE_TEST')) { 21e3c3abf1SAndreas Gohr $INPUT = new Input(); 227fb7960fSChristopher Smith} 237fb7960fSChristopher Smith 247fb7960fSChristopher Smith// BEGIN main 25f62ea8a1Sandi$mimetypes = getMimeTypes(); 26f62ea8a1Sandi 27f62ea8a1Sandi//get input 2802b0b681SAndreas Gohr$MEDIA = stripctl(getID('media', false)); // no cleaning except control chars - maybe external 29bfd0f597STom N Harris$CACHE = calc_cache($INPUT->str('cache')); 30bfd0f597STom N Harris$WIDTH = $INPUT->int('w'); 31bfd0f597STom N Harris$HEIGHT = $INPUT->int('h'); 32*dd9e8e5eSAndreas Gohr$FIT = $INPUT->bool('fit'); 33bfd0f597STom N Harris$REV = &$INPUT->ref('rev'); 34fc4aefb9SKate Arzamastseva//sanitize revision 35fc4aefb9SKate Arzamastseva$REV = preg_replace('/[^0-9]/', '', $REV); 36fc4aefb9SKate Arzamastseva 37e3c3abf1SAndreas Gohr[$EXT, $MIME, $DL] = mimetype($MEDIA, false); 38f62ea8a1Sandiif ($EXT === false) { 39f62ea8a1Sandi $EXT = 'unknown'; 40f62ea8a1Sandi $MIME = 'application/octet-stream'; 41ecebf3a8SAndreas Gohr $DL = true; 42f62ea8a1Sandi} 43f62ea8a1Sandi 4403293305SAndreas Gohr// check for permissions, preconditions and cache external files 45e3c3abf1SAndreas Gohr[$STATUS, $STATUSMESSAGE] = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT); 4603293305SAndreas Gohr 4703293305SAndreas Gohr// prepare data for plugin events 48e3c3abf1SAndreas Gohr$data = [ 493b399a1bSAndreas Gohr 'media' => $MEDIA, 50cd98d9c3SGerry Weißbach 'file' => $FILE, 51cd98d9c3SGerry Weißbach 'orig' => $FILE, 52cd98d9c3SGerry Weißbach 'mime' => $MIME, 53cd98d9c3SGerry Weißbach 'download' => $DL, 54cd98d9c3SGerry Weißbach 'cache' => $CACHE, 55cd98d9c3SGerry Weißbach 'ext' => $EXT, 56cd98d9c3SGerry Weißbach 'width' => $WIDTH, 57cd98d9c3SGerry Weißbach 'height' => $HEIGHT, 58*dd9e8e5eSAndreas Gohr 'fit' => $FIT, 59cd98d9c3SGerry Weißbach 'status' => $STATUS, 60cd98d9c3SGerry Weißbach 'statusmessage' => $STATUSMESSAGE, 61add8678fSAndreas Gohr 'ispublic' => media_ispublic($MEDIA), 626cda96e3SAndreas Gohr 'csp' => [ 636cda96e3SAndreas Gohr 'default-src' => "'none'", 646cda96e3SAndreas Gohr 'style-src' => "'unsafe-inline'", 656cda96e3SAndreas Gohr 'media-src' => "'self'", 666cda96e3SAndreas Gohr 'object-src' => "'self'", 6736300e60SAndreas Gohr 'font-src' => "'self' data:", 686cda96e3SAndreas Gohr 'form-action' => "'none'", 6901648efdSAndreas Gohr 'frame-ancestors' => "'self'", 70e3c3abf1SAndreas Gohr ] 71e3c3abf1SAndreas Gohr]; 72f62ea8a1Sandi 7303293305SAndreas Gohr// handle the file status 74e1d9dcc8SAndreas Gohr$evt = new Event('FETCH_MEDIA_STATUS', $data); 75cd98d9c3SGerry Weißbachif ($evt->advise_before()) { 7603293305SAndreas Gohr // redirects 7703293305SAndreas Gohr if ($data['status'] > 300 && $data['status'] <= 304) { 78d572baf8SKlap-in if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects 7903293305SAndreas Gohr send_redirect($data['statusmessage']); 8003293305SAndreas Gohr } 8103293305SAndreas Gohr // send any non 200 status 8203293305SAndreas Gohr if ($data['status'] != 200) { 839d2e1be6SAndreas Gohr http_status($data['status'], $data['statusmessage']); 8403293305SAndreas Gohr } 8503293305SAndreas Gohr // die on errors 8603293305SAndreas Gohr if ($data['status'] > 203) { 8726dfc232SAndreas Gohr echo $data['statusmessage']; 887fb7960fSChristopher Smith if (defined('SIMPLE_TEST')) return; 89f62ea8a1Sandi exit; 90f62ea8a1Sandi } 91f62ea8a1Sandi} 9203293305SAndreas Gohr$evt->advise_after(); 9303293305SAndreas Gohrunset($evt); 94f62ea8a1Sandi 9520bc86cfSAndreas Gohr//handle image resizing/cropping 96bfca0246SSam$evt = new Event('MEDIA_RESIZE', $data); 97bfca0246SSamif ($evt->advise_before()) { 988e9d8d55SAndreas Gohr if ( 998e9d8d55SAndreas Gohr $MIME != 'image/svg+xml' && 1001b2deed9Sfiwswe str_starts_with($MIME, 'image') && 1018e9d8d55SAndreas Gohr ($WIDTH || $HEIGHT) 1028e9d8d55SAndreas Gohr ) { 103*dd9e8e5eSAndreas Gohr if ($HEIGHT && $WIDTH && !$FIT) { 104cd98d9c3SGerry Weißbach $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT); 10520bc86cfSAndreas Gohr } else { 106cd98d9c3SGerry Weißbach $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT); 107f62ea8a1Sandi } 10820bc86cfSAndreas Gohr } 109bfca0246SSam} 110bfca0246SSam$evt->advise_after(); 111bfca0246SSamunset($evt); 112f62ea8a1Sandi 113e935fb4aSAndreas Gohr// finally send the file to the client 114e1d9dcc8SAndreas Gohr$evt = new Event('MEDIA_SENDFILE', $data); 115b80bedd6SAndreas Gohrif ($evt->advise_before()) { 1166cda96e3SAndreas Gohr sendFile( 1176cda96e3SAndreas Gohr $data['file'], 1186cda96e3SAndreas Gohr $data['mime'], 1196cda96e3SAndreas Gohr $data['download'], 1206cda96e3SAndreas Gohr $data['cache'], 1216cda96e3SAndreas Gohr $data['ispublic'], 1226cda96e3SAndreas Gohr $data['orig'], 1236cda96e3SAndreas Gohr $data['csp'] 1246cda96e3SAndreas Gohr ); 125b80bedd6SAndreas Gohr} 126cd98d9c3SGerry Weißbach// Do something after the download finished. 127add8678fSAndreas Gohr$evt->advise_after(); // will not be emitted on 304 or x-sendfile 128f62ea8a1Sandi 1297fb7960fSChristopher Smith// END DO main 130