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/** 11 * Remove unwanted chars from ID 12 * 13 * Cleans a given ID to only use allowed characters. Accented characters are 14 * converted to unaccented ones 15 * 16 * @author Andreas Gohr <andi@splitbrain.org> 17 */ 18function cleanID($id){ 19 global $conf; 20 global $lang; 21 $id = trim($id); 22 $id = utf8_strtolower($id); 23 24 //alternative namespace seperator 25 $id = strtr($id,';',':'); 26 if($conf['useslash']){ 27 $id = strtr($id,'/',':'); 28 }else{ 29 $id = strtr($id,'/','_'); 30 } 31 32 if($conf['deaccent']) $id = utf8_deaccent($id,-1); 33 34 //remove specials 35 $id = utf8_stripspecials($id,'_','_:.-'); 36 37 //clean up 38 $id = preg_replace('#_+#','_',$id); 39 $id = preg_replace('#:+#',':',$id); 40 $id = trim($id,':._-'); 41 $id = preg_replace('#:[:\._\-]+#',':',$id); 42 43 return($id); 44} 45 46/** 47 * Return namespacepart of a wiki ID 48 * 49 * @author Andreas Gohr <andi@splitbrain.org> 50 */ 51function getNS($id){ 52 if(strpos($id,':')!==false){ 53 return substr($id,0,strrpos($id,':')); 54 } 55 return false; 56} 57 58/** 59 * Returns the ID without the namespace 60 * 61 * @author Andreas Gohr <andi@splitbrain.org> 62 */ 63function noNS($id){ 64 return preg_replace('/.*:/','',$id); 65} 66 67/** 68 * returns the full path to the datafile specified by ID and 69 * optional revision 70 * 71 * The filename is URL encoded to protect Unicode chars 72 * 73 * @author Andreas Gohr <andi@splitbrain.org> 74 */ 75function wikiFN($id,$rev=''){ 76 global $conf; 77 $id = cleanID($id); 78 $id = str_replace(':','/',$id); 79 if(empty($rev)){ 80 $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 81 }else{ 82 $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 83 if($conf['usegzip'] && !@file_exists($fn)){ 84 //return gzip if enabled and plaintext doesn't exist 85 $fn .= '.gz'; 86 } 87 } 88 return $fn; 89} 90 91/** 92 * returns the full path to the mediafile specified by ID 93 * 94 * The filename is URL encoded to protect Unicode chars 95 * 96 * @author Andreas Gohr <andi@splitbrain.org> 97 */ 98function mediaFN($id){ 99 global $conf; 100 $id = cleanID($id); 101 $id = str_replace(':','/',$id); 102 $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 103 return $fn; 104} 105 106/** 107 * Returns the full filepath to a localized textfile if local 108 * version isn't found the english one is returned 109 * 110 * @author Andreas Gohr <andi@splitbrain.org> 111 */ 112function localeFN($id){ 113 global $conf; 114 $file = './lang/'.$conf['lang'].'/'.$id.'.txt'; 115 if(!@file_exists($file)){ 116 //fall back to english 117 $file = './lang/en/'.$id.'.txt'; 118 } 119 return $file; 120} 121 122/** 123 * Returns a full media id 124 * 125 * @author Andreas Gohr <andi@splitbrain.org> 126 */ 127function resolve_mediaid($ns,&$page,&$exists){ 128 global $conf; 129 130 //if links starts with . add current namespace 131 if($page{0} == '.'){ 132 $page = $ns.':'.substr($page,1); 133 } 134 135 //if link contains no namespace. add current namespace (if any) 136 if($ns !== false && strpos($page,':') === false){ 137 $page = $ns.':'.$page; 138 } 139 140 $page = cleanID($page); 141 $file = mediaFN($page); 142 $exists = @file_exists($file); 143} 144 145/** 146 * Returns a full page id 147 * 148 * @author Andreas Gohr <andi@splitbrain.org> 149 */ 150function resolve_pageid($ns,&$page,&$exists){ 151 global $conf; 152 153 //if links starts with . add current namespace 154 if($page{0} == '.'){ 155 $page = $ns.':'.substr($page,1); 156 } 157 158 //if link contains no namespace. add current namespace (if any) 159 if($ns !== false && strpos($page,':') === false){ 160 $page = $ns.':'.$page; 161 } 162 163 //keep hashlink if exists then clean both parts 164 list($page,$hash) = split('#',$page,2); 165 $page = cleanID($page); 166 $hash = cleanID($hash); 167 168 $file = wikiFN($page); 169 170 $exists = false; 171 172 //check alternative plural/nonplural form 173 if(!@file_exists($file)){ 174 if( $conf['autoplural'] ){ 175 if(substr($page,-1) == 's'){ 176 $try = substr($page,0,-1); 177 }else{ 178 $try = $page.'s'; 179 } 180 if(@file_exists(wikiFN($try))){ 181 $page = $try; 182 $exists = true; 183 } 184 } 185 }else{ 186 $exists = true; 187 } 188 189 //add hash if any 190 if(!empty($hash)) $page .= '#'.$hash; 191} 192 193//Setup VIM: ex: et ts=2 enc=utf-8 : 194