1f62ea8a1Sandi<?php 2*d4f83172SAndreas 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'); 32bfd0f597STom N Harris$REV = &$INPUT->ref('rev'); 33fc4aefb9SKate Arzamastseva//sanitize revision 34fc4aefb9SKate Arzamastseva$REV = preg_replace('/[^0-9]/', '', $REV); 35fc4aefb9SKate Arzamastseva 36e3c3abf1SAndreas Gohr[$EXT, $MIME, $DL] = mimetype($MEDIA, false); 37f62ea8a1Sandiif ($EXT === false) { 38f62ea8a1Sandi $EXT = 'unknown'; 39f62ea8a1Sandi $MIME = 'application/octet-stream'; 40ecebf3a8SAndreas Gohr $DL = true; 41f62ea8a1Sandi} 42f62ea8a1Sandi 4303293305SAndreas Gohr// check for permissions, preconditions and cache external files 44e3c3abf1SAndreas Gohr[$STATUS, $STATUSMESSAGE] = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT); 4503293305SAndreas Gohr 4603293305SAndreas Gohr// prepare data for plugin events 47e3c3abf1SAndreas Gohr$data = [ 483b399a1bSAndreas Gohr 'media' => $MEDIA, 49cd98d9c3SGerry Weißbach 'file' => $FILE, 50cd98d9c3SGerry Weißbach 'orig' => $FILE, 51cd98d9c3SGerry Weißbach 'mime' => $MIME, 52cd98d9c3SGerry Weißbach 'download' => $DL, 53cd98d9c3SGerry Weißbach 'cache' => $CACHE, 54cd98d9c3SGerry Weißbach 'ext' => $EXT, 55cd98d9c3SGerry Weißbach 'width' => $WIDTH, 56cd98d9c3SGerry Weißbach 'height' => $HEIGHT, 57cd98d9c3SGerry Weißbach 'status' => $STATUS, 58cd98d9c3SGerry Weißbach 'statusmessage' => $STATUSMESSAGE, 59add8678fSAndreas Gohr 'ispublic' => media_ispublic($MEDIA), 606cda96e3SAndreas Gohr 'csp' => [ 616cda96e3SAndreas Gohr 'default-src' => "'none'", 626cda96e3SAndreas Gohr 'style-src' => "'unsafe-inline'", 636cda96e3SAndreas Gohr 'media-src' => "'self'", 646cda96e3SAndreas Gohr 'object-src' => "'self'", 6536300e60SAndreas Gohr 'font-src' => "'self' data:", 666cda96e3SAndreas Gohr 'form-action' => "'none'", 6701648efdSAndreas Gohr 'frame-ancestors' => "'self'", 68e3c3abf1SAndreas Gohr ] 69e3c3abf1SAndreas Gohr]; 70f62ea8a1Sandi 7103293305SAndreas Gohr// handle the file status 72e1d9dcc8SAndreas Gohr$evt = new Event('FETCH_MEDIA_STATUS', $data); 73cd98d9c3SGerry Weißbachif ($evt->advise_before()) { 7403293305SAndreas Gohr // redirects 7503293305SAndreas Gohr if ($data['status'] > 300 && $data['status'] <= 304) { 76d572baf8SKlap-in if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects 7703293305SAndreas Gohr send_redirect($data['statusmessage']); 7803293305SAndreas Gohr } 7903293305SAndreas Gohr // send any non 200 status 8003293305SAndreas Gohr if ($data['status'] != 200) { 819d2e1be6SAndreas Gohr http_status($data['status'], $data['statusmessage']); 8203293305SAndreas Gohr } 8303293305SAndreas Gohr // die on errors 8403293305SAndreas Gohr if ($data['status'] > 203) { 8526dfc232SAndreas Gohr echo $data['statusmessage']; 867fb7960fSChristopher Smith if (defined('SIMPLE_TEST')) return; 87f62ea8a1Sandi exit; 88f62ea8a1Sandi } 89f62ea8a1Sandi} 9003293305SAndreas Gohr$evt->advise_after(); 9103293305SAndreas Gohrunset($evt); 92f62ea8a1Sandi 9320bc86cfSAndreas Gohr//handle image resizing/cropping 94bfca0246SSam$evt = new Event('MEDIA_RESIZE', $data); 95bfca0246SSamif ($evt->advise_before()) { 968e9d8d55SAndreas Gohr if ( 978e9d8d55SAndreas Gohr $MIME != 'image/svg+xml' && 988e9d8d55SAndreas Gohr (substr($MIME, 0, 5) == 'image') && 998e9d8d55SAndreas Gohr ($WIDTH || $HEIGHT) 1008e9d8d55SAndreas Gohr ) { 101793c31f2SChristopher Smith if ($HEIGHT && $WIDTH) { 102cd98d9c3SGerry Weißbach $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT); 10320bc86cfSAndreas Gohr } else { 104cd98d9c3SGerry Weißbach $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT); 105f62ea8a1Sandi } 10620bc86cfSAndreas Gohr } 107bfca0246SSam} 108bfca0246SSam$evt->advise_after(); 109bfca0246SSamunset($evt); 110f62ea8a1Sandi 111e935fb4aSAndreas Gohr// finally send the file to the client 112e1d9dcc8SAndreas Gohr$evt = new Event('MEDIA_SENDFILE', $data); 113b80bedd6SAndreas Gohrif ($evt->advise_before()) { 1146cda96e3SAndreas Gohr sendFile( 1156cda96e3SAndreas Gohr $data['file'], 1166cda96e3SAndreas Gohr $data['mime'], 1176cda96e3SAndreas Gohr $data['download'], 1186cda96e3SAndreas Gohr $data['cache'], 1196cda96e3SAndreas Gohr $data['ispublic'], 1206cda96e3SAndreas Gohr $data['orig'], 1216cda96e3SAndreas Gohr $data['csp'] 1226cda96e3SAndreas Gohr ); 123b80bedd6SAndreas Gohr} 124cd98d9c3SGerry Weißbach// Do something after the download finished. 125add8678fSAndreas Gohr$evt->advise_after(); // will not be emitted on 304 or x-sendfile 126f62ea8a1Sandi 1277fb7960fSChristopher Smith// END DO main 128