diff options
Diffstat (limited to 'inc/pageutils.php')
-rw-r--r-- | inc/pageutils.php | 539 |
1 files changed, 269 insertions, 270 deletions
diff --git a/inc/pageutils.php b/inc/pageutils.php index 65b140ea3..484516363 100644 --- a/inc/pageutils.php +++ b/inc/pageutils.php @@ -19,68 +19,68 @@ * @author Andreas Gohr <andi@splitbrain.org> */ function getID($param='id',$clean=true){ - global $conf; - - $id = isset($_REQUEST[$param]) ? $_REQUEST[$param] : null; - - $request = $_SERVER['REQUEST_URI']; - - //construct page id from request URI - if(empty($id) && $conf['userewrite'] == 2){ - //get the script URL - if($conf['basedir']){ - $relpath = ''; - if($param != 'id') { - $relpath = 'lib/exe/'; - } - $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']); - - }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['PATH_TRANSLATED']){ - $request = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', - $_SERVER['PATH_TRANSLATED']); - }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ - $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', - $_SERVER['SCRIPT_FILENAME']); - $script = '/'.$script; - }else{ - $script = $_SERVER['SCRIPT_NAME']; - } + global $conf; - //clean script and request (fixes a windows problem) - $script = preg_replace('/\/\/+/','/',$script); - $request = preg_replace('/\/\/+/','/',$request); + $id = isset($_REQUEST[$param]) ? $_REQUEST[$param] : null; + + $request = $_SERVER['REQUEST_URI']; + + //construct page id from request URI + if(empty($id) && $conf['userewrite'] == 2){ + //get the script URL + if($conf['basedir']){ + $relpath = ''; + if($param != 'id') { + $relpath = 'lib/exe/'; + } + $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']); + + }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['PATH_TRANSLATED']){ + $request = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', + $_SERVER['PATH_TRANSLATED']); + }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ + $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', + $_SERVER['SCRIPT_FILENAME']); + $script = '/'.$script; + }else{ + $script = $_SERVER['SCRIPT_NAME']; + } + + //clean script and request (fixes a windows problem) + $script = preg_replace('/\/\/+/','/',$script); + $request = preg_replace('/\/\/+/','/',$request); - //remove script URL and Querystring to gain the id - if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ - $id = preg_replace ('/\?.*/','',$match[1]); + //remove script URL and Querystring to gain the id + if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ + $id = preg_replace ('/\?.*/','',$match[1]); + } + $id = urldecode($id); + //strip leading slashes + $id = preg_replace('!^/+!','',$id); } - $id = urldecode($id); - //strip leading slashes - $id = preg_replace('!^/+!','',$id); - } - - // Namespace autolinking from URL - if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ - if(page_exists($id.$conf['start'])){ - // start page inside namespace - $id = $id.$conf['start']; - }elseif(page_exists($id.noNS(cleanID($id)))){ - // page named like the NS inside the NS - $id = $id.noNS(cleanID($id)); - }elseif(page_exists($id)){ - // page like namespace exists - $id = substr($id,0,-1); - }else{ - // fall back to default - $id = $id.$conf['start']; + + // Namespace autolinking from URL + if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ + if(page_exists($id.$conf['start'])){ + // start page inside namespace + $id = $id.$conf['start']; + }elseif(page_exists($id.noNS(cleanID($id)))){ + // page named like the NS inside the NS + $id = $id.noNS(cleanID($id)); + }elseif(page_exists($id)){ + // page like namespace exists + $id = substr($id,0,-1); + }else{ + // fall back to default + $id = $id.$conf['start']; + } + send_redirect(wl($id,'',true)); } - send_redirect(wl($id,'',true)); - } - if($clean) $id = cleanID($id); - if(empty($id) && $param=='id') $id = $conf['start']; + if($clean) $id = cleanID($id); + if(empty($id) && $param=='id') $id = $conf['start']; - return $id; + return $id; } /** @@ -95,49 +95,49 @@ function getID($param='id',$clean=true){ * @param boolean $media Allow leading or trailing _ for media files */ function cleanID($raw_id,$ascii=false,$media=false){ - global $conf; - global $lang; - static $sepcharpat = null; + global $conf; + global $lang; + static $sepcharpat = null; - global $cache_cleanid; - $cache = & $cache_cleanid; + global $cache_cleanid; + $cache = & $cache_cleanid; - // check if it's already in the memory cache - if (isset($cache[(string)$raw_id])) { - return $cache[(string)$raw_id]; + // check if it's already in the memory cache + if (isset($cache[(string)$raw_id])) { + return $cache[(string)$raw_id]; } - $sepchar = $conf['sepchar']; - if($sepcharpat == null) // build string only once to save clock cycles - $sepcharpat = '#\\'.$sepchar.'+#'; + $sepchar = $conf['sepchar']; + if($sepcharpat == null) // build string only once to save clock cycles + $sepcharpat = '#\\'.$sepchar.'+#'; - $id = trim((string)$raw_id); - $id = utf8_strtolower($id); + $id = trim((string)$raw_id); + $id = utf8_strtolower($id); - //alternative namespace seperator - $id = strtr($id,';',':'); - if($conf['useslash']){ - $id = strtr($id,'/',':'); - }else{ - $id = strtr($id,'/',$sepchar); - } + //alternative namespace seperator + $id = strtr($id,';',':'); + if($conf['useslash']){ + $id = strtr($id,'/',':'); + }else{ + $id = strtr($id,'/',$sepchar); + } - if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); - if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); + if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); + if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); - //remove specials - $id = utf8_stripspecials($id,$sepchar,'\*'); + //remove specials + $id = utf8_stripspecials($id,$sepchar,'\*'); - if($ascii) $id = utf8_strip($id); + if($ascii) $id = utf8_strip($id); - //clean up - $id = preg_replace($sepcharpat,$sepchar,$id); - $id = preg_replace('#:+#',':',$id); - $id = ($media ? trim($id,':.-') : trim($id,':._-')); - $id = preg_replace('#:[:\._\-]+#',':',$id); + //clean up + $id = preg_replace($sepcharpat,$sepchar,$id); + $id = preg_replace('#:+#',':',$id); + $id = ($media ? trim($id,':.-') : trim($id,':._-')); + $id = preg_replace('#:[:\._\-]+#',':',$id); - $cache[(string)$raw_id] = $id; - return($id); + $cache[(string)$raw_id] = $id; + return($id); } /** @@ -146,11 +146,11 @@ function cleanID($raw_id,$ascii=false,$media=false){ * @author Andreas Gohr <andi@splitbrain.org> */ function getNS($id){ - $pos = strrpos((string)$id,':'); - if($pos!==false){ - return substr((string)$id,0,$pos); - } - return false; + $pos = strrpos((string)$id,':'); + if($pos!==false){ + return substr((string)$id,0,$pos); + } + return false; } /** @@ -159,12 +159,12 @@ function getNS($id){ * @author Andreas Gohr <andi@splitbrain.org> */ function noNS($id) { - $pos = strrpos($id, ':'); - if ($pos!==false) { - return substr($id, $pos+1); - } else { - return $id; - } + $pos = strrpos($id, ':'); + if ($pos!==false) { + return substr($id, $pos+1); + } else { + return $id; + } } /** @@ -225,14 +225,14 @@ function sectionID($title,&$check) { /** - * Wiki page existence check + * Wiki page existence check * - * parameters as for wikiFN + * parameters as for wikiFN * - * @author Chris Smith <chris@jalakai.co.uk> + * @author Chris Smith <chris@jalakai.co.uk> */ function page_exists($id,$rev='',$clean=true) { - return @file_exists(wikiFN($id,$rev,$clean)); + return @file_exists(wikiFN($id,$rev,$clean)); } /** @@ -248,39 +248,39 @@ function page_exists($id,$rev='',$clean=true) { * @author Andreas Gohr <andi@splitbrain.org> */ function wikiFN($raw_id,$rev='',$clean=true){ - global $conf; - - global $cache_wikifn; - $cache = & $cache_wikifn; - - if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { - return $cache[$raw_id][$rev]; - } - - $id = $raw_id; - - if ($clean) $id = cleanID($id); - $id = str_replace(':','/',$id); - if(empty($rev)){ - $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; - }else{ - $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; - if($conf['compression']){ - //test for extensions here, we want to read both compressions - if (@file_exists($fn . '.gz')){ - $fn .= '.gz'; - }else if(@file_exists($fn . '.bz2')){ - $fn .= '.bz2'; - }else{ - //file doesnt exist yet, so we take the configured extension - $fn .= '.' . $conf['compression']; - } + global $conf; + + global $cache_wikifn; + $cache = & $cache_wikifn; + + if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { + return $cache[$raw_id][$rev]; } - } - if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } - $cache[$raw_id][$rev] = $fn; - return $fn; + $id = $raw_id; + + if ($clean) $id = cleanID($id); + $id = str_replace(':','/',$id); + if(empty($rev)){ + $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; + }else{ + $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; + if($conf['compression']){ + //test for extensions here, we want to read both compressions + if (@file_exists($fn . '.gz')){ + $fn .= '.gz'; + }else if(@file_exists($fn . '.bz2')){ + $fn .= '.bz2'; + }else{ + //file doesnt exist yet, so we take the configured extension + $fn .= '.' . $conf['compression']; + } + } + } + + if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } + $cache[$raw_id][$rev] = $fn; + return $fn; } /** @@ -289,8 +289,8 @@ function wikiFN($raw_id,$rev='',$clean=true){ * @author Ben Coburn <btcoburn@silicodon.net> */ function wikiLockFN($id) { - global $conf; - return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; + global $conf; + return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; } @@ -302,11 +302,11 @@ function wikiLockFN($id) { * @author Steven Danz <steven-danz@kc.rr.com> */ function metaFN($id,$ext){ - global $conf; - $id = cleanID($id); - $id = str_replace(':','/',$id); - $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; - return $fn; + global $conf; + $id = cleanID($id); + $id = str_replace(':','/',$id); + $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; + return $fn; } /** @@ -315,20 +315,20 @@ function metaFN($id,$ext){ * @author Esther Brunner <esther@kaffeehaus.ch> */ function metaFiles($id){ - $name = noNS($id); - $ns = getNS($id); - $dir = ($ns) ? metaFN($ns,'').'/' : metaFN($ns,''); - $files = array(); - - $dh = @opendir($dir); - if(!$dh) return $files; - while(($file = readdir($dh)) !== false){ - if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file)) - $files[] = $dir.$file; - } - closedir($dh); - - return $files; + $name = noNS($id); + $ns = getNS($id); + $dir = ($ns) ? metaFN($ns,'').'/' : metaFN($ns,''); + $files = array(); + + $dh = @opendir($dir); + if(!$dh) return $files; + while(($file = readdir($dh)) !== false){ + if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file)) + $files[] = $dir.$file; + } + closedir($dh); + + return $files; } /** @@ -339,11 +339,11 @@ function metaFiles($id){ * @author Andreas Gohr <andi@splitbrain.org> */ function mediaFN($id){ - global $conf; - $id = cleanID($id); - $id = str_replace(':','/',$id); + global $conf; + $id = cleanID($id); + $id = str_replace(':','/',$id); $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); - return $fn; + return $fn; } /** @@ -353,13 +353,13 @@ function mediaFN($id){ * @author Andreas Gohr <andi@splitbrain.org> */ function localeFN($id){ - global $conf; - $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt'; - if(!@file_exists($file)){ - //fall back to english - $file = DOKU_INC.'inc/lang/en/'.$id.'.txt'; - } - return $file; + global $conf; + $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt'; + if(!@file_exists($file)){ + //fall back to english + $file = DOKU_INC.'inc/lang/en/'.$id.'.txt'; + } + return $file; } /** @@ -374,43 +374,43 @@ function localeFN($id){ * @author <bart at mediawave dot nl> */ function resolve_id($ns,$id,$clean=true){ - global $conf; - - // some pre cleaning for useslash: - if($conf['useslash']) $id = str_replace('/',':',$id); - - // if the id starts with a dot we need to handle the - // relative stuff - if($id{0} == '.'){ - // normalize initial dots without a colon - $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); - // prepend the current namespace - $id = $ns.':'.$id; - - // cleanup relatives - $result = array(); - $pathA = explode(':', $id); - if (!$pathA[0]) $result[] = ''; - foreach ($pathA AS $key => $dir) { - if ($dir == '..') { - if (end($result) == '..') { - $result[] = '..'; - } elseif (!array_pop($result)) { - $result[] = '..'; + global $conf; + + // some pre cleaning for useslash: + if($conf['useslash']) $id = str_replace('/',':',$id); + + // if the id starts with a dot we need to handle the + // relative stuff + if($id{0} == '.'){ + // normalize initial dots without a colon + $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); + // prepend the current namespace + $id = $ns.':'.$id; + + // cleanup relatives + $result = array(); + $pathA = explode(':', $id); + if (!$pathA[0]) $result[] = ''; + foreach ($pathA AS $key => $dir) { + if ($dir == '..') { + if (end($result) == '..') { + $result[] = '..'; + } elseif (!array_pop($result)) { + $result[] = '..'; + } + } elseif ($dir && $dir != '.') { + $result[] = $dir; + } } - } elseif ($dir && $dir != '.') { - $result[] = $dir; - } + if (!end($pathA)) $result[] = ''; + $id = implode(':', $result); + }elseif($ns !== false && strpos($id,':') === false){ + //if link contains no namespace. add current namespace (if any) + $id = $ns.':'.$id; } - if (!end($pathA)) $result[] = ''; - $id = implode(':', $result); - }elseif($ns !== false && strpos($id,':') === false){ - //if link contains no namespace. add current namespace (if any) - $id = $ns.':'.$id; - } - - if($clean) $id = cleanID($id); - return $id; + + if($clean) $id = cleanID($id); + return $id; } /** @@ -419,9 +419,9 @@ function resolve_id($ns,$id,$clean=true){ * @author Andreas Gohr <andi@splitbrain.org> */ function resolve_mediaid($ns,&$page,&$exists){ - $page = resolve_id($ns,$page); - $file = mediaFN($page); - $exists = @file_exists($file); + $page = resolve_id($ns,$page); + $file = mediaFN($page); + $exists = @file_exists($file); } /** @@ -430,63 +430,63 @@ function resolve_mediaid($ns,&$page,&$exists){ * @author Andreas Gohr <andi@splitbrain.org> */ function resolve_pageid($ns,&$page,&$exists){ - global $conf; - $exists = false; - - //keep hashlink if exists then clean both parts - if (strpos($page,'#')) { - list($page,$hash) = explode('#',$page,2); - } else { - $hash = ''; - } - $hash = cleanID($hash); - $page = resolve_id($ns,$page,false); // resolve but don't clean, yet - - // get filename (calls clean itself) - $file = wikiFN($page); - - // if ends with colon or slash we have a namespace link - if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){ - if(page_exists($page.$conf['start'])){ - // start page inside namespace - $page = $page.$conf['start']; - $exists = true; - }elseif(page_exists($page.noNS(cleanID($page)))){ - // page named like the NS inside the NS - $page = $page.noNS(cleanID($page)); - $exists = true; - }elseif(page_exists($page)){ - // page like namespace exists - $page = $page; - $exists = true; - }else{ - // fall back to default - $page = $page.$conf['start']; + global $conf; + $exists = false; + + //keep hashlink if exists then clean both parts + if (strpos($page,'#')) { + list($page,$hash) = explode('#',$page,2); + } else { + $hash = ''; } - }else{ - //check alternative plural/nonplural form - if(!@file_exists($file)){ - if( $conf['autoplural'] ){ - if(substr($page,-1) == 's'){ - $try = substr($page,0,-1); + $hash = cleanID($hash); + $page = resolve_id($ns,$page,false); // resolve but don't clean, yet + + // get filename (calls clean itself) + $file = wikiFN($page); + + // if ends with colon or slash we have a namespace link + if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){ + if(page_exists($page.$conf['start'])){ + // start page inside namespace + $page = $page.$conf['start']; + $exists = true; + }elseif(page_exists($page.noNS(cleanID($page)))){ + // page named like the NS inside the NS + $page = $page.noNS(cleanID($page)); + $exists = true; + }elseif(page_exists($page)){ + // page like namespace exists + $page = $page; + $exists = true; }else{ - $try = $page.'s'; - } - if(page_exists($try)){ - $page = $try; - $exists = true; + // fall back to default + $page = $page.$conf['start']; } - } }else{ - $exists = true; + //check alternative plural/nonplural form + if(!@file_exists($file)){ + if( $conf['autoplural'] ){ + if(substr($page,-1) == 's'){ + $try = substr($page,0,-1); + }else{ + $try = $page.'s'; + } + if(page_exists($try)){ + $page = $try; + $exists = true; + } + } + }else{ + $exists = true; + } } - } - // now make sure we have a clean page - $page = cleanID($page); + // now make sure we have a clean page + $page = cleanID($page); - //add hash if any - if(!empty($hash)) $page .= '#'.$hash; + //add hash if any + if(!empty($hash)) $page .= '#'.$hash; } /** @@ -501,11 +501,11 @@ function resolve_pageid($ns,&$page,&$exists){ * @return string The filename of the cachefile */ function getCacheName($data,$ext=''){ - global $conf; - $md5 = md5($data); - $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; - io_makeFileDir($file); - return $file; + global $conf; + $md5 = md5($data); + $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; + io_makeFileDir($file); + return $file; } /** @@ -514,15 +514,15 @@ function getCacheName($data,$ext=''){ * @author Andreas Gohr <gohr@cosmocode.de> */ function isHiddenPage($id){ - global $conf; - global $ACT; - if(empty($conf['hidepages'])) return false; - if($ACT == 'admin') return false; - - if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){ - return true; - } - return false; + global $conf; + global $ACT; + if(empty($conf['hidepages'])) return false; + if($ACT == 'admin') return false; + + if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){ + return true; + } + return false; } /** @@ -531,8 +531,7 @@ function isHiddenPage($id){ * @author Andreas Gohr <gohr@cosmocode.de> */ function isVisiblePage($id){ - return !isHiddenPage($id); + return !isHiddenPage($id); } -//Setup VIM: ex: et ts=2 enc=utf-8 : |