1<?php 2/** 3 * Utilities for handling pagenames 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9/** 10 * Fetch the pageid 11 * 12 * Uses either standard $_REQUEST variable or extracts it from 13 * the full request URI when userewrite is set to 2 14 * 15 * Returns $conf['start'] if no id was found. 16 * 17 * @author Andreas Gohr <andi@splitbrain.org> 18 */ 19function getID(){ 20 global $conf; 21 22 $id = cleanID($_REQUEST['id']); 23 24 //construct page id from request URI 25 if(empty($id) && $conf['userewrite'] == 2){ 26 //get the script URL 27 if($conf['basedir']){ 28 $script = $conf['basedir'].DOKU_SCRIPT; 29 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 30 $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 31 $_SERVER['SCRIPT_FILENAME']); 32 $script = '/'.$script; 33 }else{ 34 $script = $_SERVER['SCRIPT_NAME']; 35 } 36 37 //remove script URL and Querystring to gain the id 38 if(preg_match('/^'.preg_quote($script,'/').'(.*)/', 39 $_SERVER['REQUEST_URI'], $match)){ 40 $id = preg_replace ('/\?.*/','',$match[1]); 41 } 42 $id = cleanID($id); 43 } 44 if(empty($id)) $id = $conf['start']; 45 46 return $id; 47} 48 49/** 50 * Remove unwanted chars from ID 51 * 52 * Cleans a given ID to only use allowed characters. Accented characters are 53 * converted to unaccented ones 54 * 55 * @author Andreas Gohr <andi@splitbrain.org> 56 */ 57function cleanID($id){ 58 global $conf; 59 global $lang; 60 $id = trim($id); 61 $id = utf8_strtolower($id); 62 63 //alternative namespace seperator 64 $id = strtr($id,';',':'); 65 if($conf['useslash']){ 66 $id = strtr($id,'/',':'); 67 }else{ 68 $id = strtr($id,'/','_'); 69 } 70 71 if($conf['deaccent']) $id = utf8_deaccent($id,-1); 72 73 //remove specials 74 $id = utf8_stripspecials($id,'_'); 75 76 //clean up 77 $id = preg_replace('#_+#','_',$id); 78 $id = preg_replace('#:+#',':',$id); 79 $id = trim($id,':._-'); 80 $id = preg_replace('#:[:\._\-]+#',':',$id); 81 82 return($id); 83} 84 85/** 86 * Return namespacepart of a wiki ID 87 * 88 * @author Andreas Gohr <andi@splitbrain.org> 89 */ 90function getNS($id){ 91 if(strpos($id,':')!==false){ 92 return substr($id,0,strrpos($id,':')); 93 } 94 return false; 95} 96 97/** 98 * Returns the ID without the namespace 99 * 100 * @author Andreas Gohr <andi@splitbrain.org> 101 */ 102function noNS($id){ 103 return preg_replace('/.*:/','',$id); 104} 105 106/** 107 * returns the full path to the datafile specified by ID and 108 * optional revision 109 * 110 * The filename is URL encoded to protect Unicode chars 111 * 112 * @author Andreas Gohr <andi@splitbrain.org> 113 */ 114function wikiFN($id,$rev=''){ 115 global $conf; 116 $id = cleanID($id); 117 $id = str_replace(':','/',$id); 118 if(empty($rev)){ 119 $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 120 }else{ 121 $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 122 if($conf['usegzip'] && !@file_exists($fn)){ 123 //return gzip if enabled and plaintext doesn't exist 124 $fn .= '.gz'; 125 } 126 } 127 return $fn; 128} 129 130/** 131 * returns the full path to the mediafile specified by ID 132 * 133 * The filename is URL encoded to protect Unicode chars 134 * 135 * @author Andreas Gohr <andi@splitbrain.org> 136 */ 137function mediaFN($id){ 138 global $conf; 139 $id = cleanID($id); 140 $id = str_replace(':','/',$id); 141 $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 142 return $fn; 143} 144 145/** 146 * Returns the full filepath to a localized textfile if local 147 * version isn't found the english one is returned 148 * 149 * @author Andreas Gohr <andi@splitbrain.org> 150 */ 151function localeFN($id){ 152 global $conf; 153 $file = './lang/'.$conf['lang'].'/'.$id.'.txt'; 154 if(!@file_exists($file)){ 155 //fall back to english 156 $file = './lang/en/'.$id.'.txt'; 157 } 158 return $file; 159} 160 161/** 162 * Returns a full media id 163 * 164 * @author Andreas Gohr <andi@splitbrain.org> 165 */ 166function resolve_mediaid($ns,&$page,&$exists){ 167 global $conf; 168 169 //if links starts with . add current namespace 170 if($page{0} == '.'){ 171 $page = $ns.':'.substr($page,1); 172 } 173 174 //if link contains no namespace. add current namespace (if any) 175 if($ns !== false && strpos($page,':') === false){ 176 $page = $ns.':'.$page; 177 } 178 179 $page = cleanID($page); 180 $file = mediaFN($page); 181 $exists = @file_exists($file); 182} 183 184/** 185 * Returns a full page id 186 * 187 * @author Andreas Gohr <andi@splitbrain.org> 188 */ 189function resolve_pageid($ns,&$page,&$exists){ 190 global $conf; 191 $exists = false; 192 193 //if links starts with . add current namespace 194 if($page{0} == '.'){ 195 $page = $ns.':'.substr($page,1); 196 } 197 198 //if link contains no namespace. add current namespace (if any) 199 if($ns !== false && strpos($page,':') === false){ 200 $page = $ns.':'.$page; 201 } 202 203 //keep hashlink if exists then clean both parts 204 list($page,$hash) = split('#',$page,2); 205 $page = cleanID($page); 206 $hash = cleanID($hash); 207 208 $file = wikiFN($page); 209 210 //check alternative plural/nonplural form 211 if(!@file_exists($file)){ 212 if( $conf['autoplural'] ){ 213 if(substr($page,-1) == 's'){ 214 $try = substr($page,0,-1); 215 }else{ 216 $try = $page.'s'; 217 } 218 if(@file_exists(wikiFN($try))){ 219 $page = $try; 220 $exists = true; 221 } 222 } 223 }else{ 224 $exists = true; 225 } 226 227 //add hash if any 228 if(!empty($hash)) $page .= '#'.$hash; 229} 230 231//Setup VIM: ex: et ts=2 enc=utf-8 : 232