fetch.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * DokuWiki media passthrough file
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <andi@splitbrain.org>
  7. */
  8. use dokuwiki\Extension\Event;
  9. if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../');
  10. if (!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT', 1);
  11. require_once(DOKU_INC.'inc/init.php');
  12. session_write_close(); //close session
  13. require_once(DOKU_INC.'inc/fetch.functions.php');
  14. if (defined('SIMPLE_TEST')) {
  15. $INPUT = new \dokuwiki\Input\Input();
  16. }
  17. // BEGIN main
  18. $mimetypes = getMimeTypes();
  19. //get input
  20. $MEDIA = stripctl(getID('media', false)); // no cleaning except control chars - maybe external
  21. $CACHE = calc_cache($INPUT->str('cache'));
  22. $WIDTH = $INPUT->int('w');
  23. $HEIGHT = $INPUT->int('h');
  24. $REV = & $INPUT->ref('rev');
  25. //sanitize revision
  26. $REV = preg_replace('/[^0-9]/', '', $REV);
  27. list($EXT, $MIME, $DL) = mimetype($MEDIA, false);
  28. if($EXT === false) {
  29. $EXT = 'unknown';
  30. $MIME = 'application/octet-stream';
  31. $DL = true;
  32. }
  33. // check for permissions, preconditions and cache external files
  34. list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE, $REV, $WIDTH, $HEIGHT);
  35. // prepare data for plugin events
  36. $data = array(
  37. 'media' => $MEDIA,
  38. 'file' => $FILE,
  39. 'orig' => $FILE,
  40. 'mime' => $MIME,
  41. 'download' => $DL,
  42. 'cache' => $CACHE,
  43. 'ext' => $EXT,
  44. 'width' => $WIDTH,
  45. 'height' => $HEIGHT,
  46. 'status' => $STATUS,
  47. 'statusmessage' => $STATUSMESSAGE,
  48. 'ispublic' => media_ispublic($MEDIA),
  49. 'csp' => [
  50. 'default-src' => "'none'",
  51. 'style-src' => "'unsafe-inline'",
  52. 'media-src' => "'self'",
  53. 'object-src' => "'self'",
  54. 'font-src' => "'self' data:",
  55. 'form-action' => "'none'",
  56. 'frame-ancestors' => "'self'",
  57. ],
  58. );
  59. // handle the file status
  60. $evt = new Event('FETCH_MEDIA_STATUS', $data);
  61. if($evt->advise_before()) {
  62. // redirects
  63. if($data['status'] > 300 && $data['status'] <= 304) {
  64. if (defined('SIMPLE_TEST')) return; //TestResponse doesn't recognize redirects
  65. send_redirect($data['statusmessage']);
  66. }
  67. // send any non 200 status
  68. if($data['status'] != 200) {
  69. http_status($data['status'], $data['statusmessage']);
  70. }
  71. // die on errors
  72. if($data['status'] > 203) {
  73. print $data['statusmessage'];
  74. if (defined('SIMPLE_TEST')) return;
  75. exit;
  76. }
  77. }
  78. $evt->advise_after();
  79. unset($evt);
  80. //handle image resizing/cropping
  81. $evt = new Event('MEDIA_RESIZE', $data);
  82. if($evt->advise_before()) {
  83. if(
  84. $MIME != 'image/svg+xml' &&
  85. (substr($MIME, 0, 5) == 'image') &&
  86. ($WIDTH || $HEIGHT)
  87. ) {
  88. if($HEIGHT && $WIDTH) {
  89. $data['file'] = $FILE = media_crop_image($data['file'], $EXT, $WIDTH, $HEIGHT);
  90. } else {
  91. $data['file'] = $FILE = media_resize_image($data['file'], $EXT, $WIDTH, $HEIGHT);
  92. }
  93. }
  94. }
  95. $evt->advise_after();
  96. unset($evt);
  97. // finally send the file to the client
  98. $evt = new Event('MEDIA_SENDFILE', $data);
  99. if($evt->advise_before()) {
  100. sendFile(
  101. $data['file'],
  102. $data['mime'],
  103. $data['download'],
  104. $data['cache'],
  105. $data['ispublic'],
  106. $data['orig'],
  107. $data['csp']
  108. );
  109. }
  110. // Do something after the download finished.
  111. $evt->advise_after(); // will not be emitted on 304 or x-sendfile
  112. // END DO main
  113. //Setup VIM: ex: et ts=2 :