From 6de3759a1b7a74492845c76e1f0bd1078e2220c7 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 14 Aug 2005 12:18:34 +0200 Subject: URL rewriting for media files This patch adds nicer URLs for media files (for fetch.php and detail.php) !IMPORTANT! Users of rewrite mode 1 need to adjust their .htaccess See .htaccess.dist for an example darcs-hash:20050814101834-7ad00-37ef1dea00affc9d808d9ee1289fa7454199cd24.gz --- .htaccess.dist | 2 ++ inc/common.php | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++- inc/pageutils.php | 13 +++++----- inc/parser/xhtml.php | 16 ++++++------- inc/template.php | 17 ++++--------- lib/exe/detail.php | 4 ++-- lib/exe/fetch.php | 2 +- 7 files changed, 91 insertions(+), 31 deletions(-) diff --git a/.htaccess.dist b/.htaccess.dist index c77ee05f0..45747c1ca 100644 --- a/.htaccess.dist +++ b/.htaccess.dist @@ -12,6 +12,8 @@ Options -Indexes ## Comment these rules in if you want to have nice URLs #RewriteEngine on +#RewriteRule ^_media/(.*) lib/exe/fetch.php?media=$1 [QSA,L] +#RewriteRule ^_detail/(.*) lib/exe/detail.php?media=$1 [QSA,L] #RewriteRule ^$ doku.php [L] #RewriteCond %{REQUEST_FILENAME} !-f #RewriteCond %{REQUEST_FILENAME} !-d diff --git a/inc/common.php b/inc/common.php index b0d7c4c10..891601357 100644 --- a/inc/common.php +++ b/inc/common.php @@ -238,7 +238,11 @@ function idfilter($id,$ue=true){ */ function wl($id='',$more='',$abs=false){ global $conf; - $more = str_replace(',','&',$more); + if(is_array($more)){ + $more = buildURLparams($more); + }else{ + $more = str_replace(',','&',$more); + } $id = idfilter($id); if($abs){ @@ -261,6 +265,68 @@ function wl($id='',$more='',$abs=false){ return $xlink; } +/** + * Build a link to a media file + * + * Will return a link to the detail page if $direct is false + */ +function ml($id='',$more='',$direct=true){ + global $conf; + if(is_array($more)){ + $more = buildURLparams($more); + }else{ + $more = str_replace(',','&',$more); + } + + $xlink = DOKU_BASE; + + // external URLs are always direct without rewriting + if(preg_match('#^(https?|ftp)://#i',$id)){ + $xlink .= 'lib/exe/fetch.php'; + if($more){ + $xlink .= '?'.$more; + $xlink .= '&media='.$id; + }else{ + $xlink .= '?media='.$id; + } + return $xlink; + } + + $id = idfilter($id); + + // decide on scriptname + if($direct){ + if($conf['userewrite'] == 1){ + $script = '_media'; + }else{ + $script = 'lib/exe/fetch.php'; + } + }else{ + if($conf['userewrite'] == 1){ + $script = '_detail'; + }else{ + $script = 'lib/exe/detail.php'; + } + } + + // build URL based on rewrite mode + if($conf['userewrite']){ + $xlink .= $script.'/'.$id; + if($more) $xlink .= '?'.$more; + }else{ + if($more){ + $xlink .= '?'.$more; + $xlink .= '&media='.$id; + }else{ + $xlink .= '?media='.$id; + } + } + + return $xlink; +} + + + /** * Just builds a link to a script * diff --git a/inc/pageutils.php b/inc/pageutils.php index 1e0792b7d..d81d22a58 100644 --- a/inc/pageutils.php +++ b/inc/pageutils.php @@ -8,25 +8,25 @@ */ /** - * Fetch the pageid + * Fetch the an ID from request * * Uses either standard $_REQUEST variable or extracts it from * the full request URI when userewrite is set to 2 * - * Returns $conf['start'] if no id was found. + * Returns $conf['start'] if no id was found and $param is 'id' * * @author Andreas Gohr */ -function getID(){ +function getID($param='id'){ global $conf; - $id = cleanID($_REQUEST['id']); + $id = cleanID($_REQUEST[$param]); //construct page id from request URI if(empty($id) && $conf['userewrite'] == 2){ //get the script URL if($conf['basedir']){ - $script = $conf['basedir'].DOKU_SCRIPT; + $script = $conf['basedir'].basedir($_SERVER['SCRIPT_FILENAME']); }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', $_SERVER['SCRIPT_FILENAME']); @@ -43,9 +43,10 @@ function getID(){ if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ $id = preg_replace ('/\?.*/','',$match[1]); } + $id = urldecode($id); $id = cleanID($id); } - if(empty($id)) $id = $conf['start']; + if(empty($id) && $param='id') $id = $conf['start']; return $id; } diff --git a/inc/parser/xhtml.php b/inc/parser/xhtml.php index 181562b2a..f6e9b7add 100644 --- a/inc/parser/xhtml.php +++ b/inc/parser/xhtml.php @@ -734,10 +734,10 @@ class Doku_Renderer_xhtml extends Doku_Renderer { $link['title'] = $this->_xmlEntities($src); list($ext,$mime) = mimetype($src); - if(substr($mime,0,5) == 'image' && !preg_match('#^(https?|ftp)://#i',$src)){ - $link['url']= DOKU_BASE.'lib/exe/detail.php?id='.$ID.'&cache='.$cache.'&media='.urlencode($src); + if(substr($mime,0,5)){ + $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),false); }else{ - $link['url']= DOKU_BASE.'lib/exe/fetch.php?cache='.$cache.'&media='.urlencode($src); + $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true); } $link['name'] = $this->_media ($src, $title, $align, $width, $height, $cache); @@ -762,7 +762,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer { $link['target'] = $conf['target']['media']; $link['title'] = $this->_xmlEntities($src); - $link['url'] = DOKU_BASE.'lib/exe/fetch.php?cache='.$cache.'&media='.urlencode($src); + $link['url'] = ml($src,array('cache'=>$cache)); $link['name'] = $this->_media ($src, $title, $align, $width, $height, $cache); @@ -917,9 +917,7 @@ class Doku_Renderer_xhtml extends Doku_Renderer { list($ext,$mime) = mimetype($src); if(substr($mime,0,5) == 'image'){ //add image tag - $ret .= '$width,'h'=>$height,'cache'=>$cache)).'"'; $ret .= ' class="media'.$align.'"'; if (!is_null($title)) { @@ -952,9 +950,9 @@ class Doku_Renderer_xhtml extends Doku_Renderer { if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"'; if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"'; $ret .= '>'.DOKU_LF; - $ret .= ''.DOKU_LF; + $ret .= ''.DOKU_LF; $ret .= ''.DOKU_LF; - $ret .= '_xmlEntities($width).'"'; if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"'; diff --git a/inc/template.php b/inc/template.php index 34fdfb009..9f39b6b79 100644 --- a/inc/template.php +++ b/inc/template.php @@ -674,11 +674,7 @@ function tpl_mediafilelist(){ $h = floor($h * $ratio); } - $p = array(); - $p['w'] = $w; - $p['h'] = $h; - $p['media'] = $item['id']; - $src = DOKU_BASE.'lib/exe/fetch.php?'.buildURLParams($p); + $src = ml($item['id'],array('w'=>$w,'h'=>$h)); $p = array(); $p['width'] = $w; @@ -854,12 +850,9 @@ function tpl_img($maxwidth=900,$maxheight=700){ $h = floor($ratio*$h); } - //prepare URL - $p = array(); - $p['cache'] = $_REQUEST['cache']; - $p['media'] = $IMG; - $p = buildURLparams($p); - $url=DOKU_BASE.'lib/exe/fetch.php?'.$p; + //prepare URLs + $url=ml($IMG,array('cache'=>$_REQUEST['cache'])); + $src=ml($IMG,array('cache'=>$_REQUEST['cache'],'w'=>$w,'h'=>$h)); //prepare attributes $alt=tpl_img_getTag('Simple.Title'); @@ -876,7 +869,7 @@ function tpl_img($maxwidth=900,$maxheight=700){ $p = buildAttributes($p); print ''; - print ''; + print ''; print ''; } diff --git a/lib/exe/detail.php b/lib/exe/detail.php index 6e2ae96c6..21a34d721 100644 --- a/lib/exe/detail.php +++ b/lib/exe/detail.php @@ -11,8 +11,8 @@ //close session session_write_close(); - $IMG = cleanID($_REQUEST['media']); - $ID = getID(); + $IMG = getID('media'); + $ID = cleanID($_REQUEST['id']); $ERROR = false; // check image permissions diff --git a/lib/exe/fetch.php b/lib/exe/fetch.php index 812850267..e647ea384 100644 --- a/lib/exe/fetch.php +++ b/lib/exe/fetch.php @@ -18,7 +18,7 @@ $mimetypes = getMimeTypes(); //get input - $MEDIA = $_REQUEST['media']; + $MEDIA = getID('media'); $CACHE = calc_cache($_REQUEST['cache']); $WIDTH = $_REQUEST['w']; $HEIGHT = $_REQUEST['h']; -- cgit v1.2.3