Skip to content
Snippets Groups Projects
Commit 03293305 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

Changed FETCH_MEDIA_4XERROR to FETCH_MEDIA_STATUS

parent cd98d9c3
No related branches found
No related tags found
No related merge requests found
...@@ -32,9 +32,11 @@ ...@@ -32,9 +32,11 @@
$MIME = 'application/octet-stream'; $MIME = 'application/octet-stream';
$DL = true; $DL = true;
} }
// prepare data for error handling / clean download handling // check for permissions, preconditions and cache external files
list($STATUS, $STATUSMESSAGE) = check4XErrors($MEDIA, $FILE); // here goes the old 4X error checking list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE);
// prepare data for plugin events
$data = array('media' => $MEDIA, $data = array('media' => $MEDIA,
'file' => $FILE, 'file' => $FILE,
'orig' => $FILE, 'orig' => $FILE,
...@@ -47,17 +49,26 @@ ...@@ -47,17 +49,26 @@
'status' => $STATUS, 'status' => $STATUS,
'statusmessage' => $STATUSMESSAGE, 'statusmessage' => $STATUSMESSAGE,
); );
// handle any 4XX status messages // handle the file status
if ( $STATUS >= 400 && $STATUSMESSAGE < 500 ) { $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data);
$evt = new Doku_Event('FETCH_MEDIA_4XERROR', $data); if ( $evt->advise_before() ) {
if ( $evt->advise_before() ) { // redirects
if($data['status'] > 300 && $data['status'] <= 304){
send_redirect($data['statusmessage']);
}
// send any non 200 status
if($data['status'] != 200){
header('HTTP/1.0 ' . $data['status'] . ' ' . $data['statusmessage']); header('HTTP/1.0 ' . $data['status'] . ' ' . $data['statusmessage']);
}
// die on errors
if($data['status'] > 203){
print $data['statusmessage']; print $data['statusmessage'];
exit; exit;
} }
unset($evt);
} }
$evt->advise_after();
unset($evt);
//handle image resizing/cropping //handle image resizing/cropping
if((substr($MIME,0,5) == 'image') && $WIDTH){ if((substr($MIME,0,5) == 'image') && $WIDTH){
...@@ -67,7 +78,7 @@ ...@@ -67,7 +78,7 @@
$data['file'] = $FILE = media_resize_image($data['file'],$EXT,$WIDTH,$HEIGHT); $data['file'] = $FILE = media_resize_image($data['file'],$EXT,$WIDTH,$HEIGHT);
} }
} }
// finally send the file to the client // finally send the file to the client
$evt = new Doku_Event('MEDIA_SENDFILE', $data); $evt = new Doku_Event('MEDIA_SENDFILE', $data);
if ($evt->advise_before()) { if ($evt->advise_before()) {
...@@ -131,21 +142,20 @@ function sendFile($file,$mime,$dl,$cache){ ...@@ -131,21 +142,20 @@ function sendFile($file,$mime,$dl,$cache){
} }
} }
/* /**
* File fetch 4XX error checker * Check for media for preconditions and return correct status code
* *
* Check for preconditions and return 4XX errors if needed
* READ: MEDIA, MIME, EXT, CACHE * READ: MEDIA, MIME, EXT, CACHE
* WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE ) * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
* *
* @author Gerry Weissbach <gerry.w@gammaproduction.de> * @author Gerry Weissbach <gerry.w@gammaproduction.de>
* @param $media reference to the media id * @param $media reference to the media id
* @param $file reference to the file variable * @param $file reference to the file variable
* @returns array(STATUS, STATUSMESSAGE) * @returns array(STATUS, STATUSMESSAGE)
*/ */
function check4XErrors(&$media, &$file) { function checkFileStatus(&$media, &$file) {
global $MIME, $EXT, $CACHE; global $MIME, $EXT, $CACHE;
//media to local file //media to local file
if(preg_match('#^(https?)://#i',$media)){ if(preg_match('#^(https?)://#i',$media)){
//check hash //check hash
...@@ -156,8 +166,7 @@ function check4XErrors(&$media, &$file) { ...@@ -156,8 +166,7 @@ function check4XErrors(&$media, &$file) {
if(strncmp($MIME,'image/',6) == 0) $file = media_get_from_URL($media,$EXT,$CACHE); if(strncmp($MIME,'image/',6) == 0) $file = media_get_from_URL($media,$EXT,$CACHE);
if(!$file){ if(!$file){
//download failed - redirect to original URL //download failed - redirect to original URL
header('Location: '.$media); return array( 302, $media );
exit;
} }
}else{ }else{
$media = cleanID($media); $media = cleanID($media);
...@@ -174,11 +183,10 @@ function check4XErrors(&$media, &$file) { ...@@ -174,11 +183,10 @@ function check4XErrors(&$media, &$file) {
//check file existance //check file existance
if(!@file_exists($file)){ if(!@file_exists($file)){
// FIXME add some default broken image
return array( 404, 'Not Found' ); return array( 404, 'Not Found' );
} }
return array(null, null); return array(200, null);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment