[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/exe/ -> fetch.php (source)

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