3
* Utilities for handling pagenames
5
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
* @author Andreas Gohr <andi@splitbrain.org>
7
* @todo Combine similar functions like {wiki,media,meta}FN()
11
* Fetch the an ID from request
13
* Uses either standard $_REQUEST variable or extracts it from
14
* the full request URI when userewrite is set to 2
16
* For $param='id' $conf['start'] is returned if no id was found.
17
* If the second parameter is true (default) the ID is cleaned.
19
* @author Andreas Gohr <andi@splitbrain.org>
21
function getID($param='id',$clean=true){
24
$id = isset($_REQUEST[$param]) ? $_REQUEST[$param] : null;
26
//construct page id from request URI
27
if(empty($id) && $conf['userewrite'] == 2){
32
$relpath = 'lib/exe/';
34
$script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
35
}elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
36
$script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
37
$_SERVER['SCRIPT_FILENAME']);
38
$script = '/'.$script;
40
$script = $_SERVER['SCRIPT_NAME'];
43
//clean script and request (fixes a windows problem)
44
$script = preg_replace('/\/\/+/','/',$script);
45
$request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']);
47
//remove script URL and Querystring to gain the id
48
if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
49
$id = preg_replace ('/\?.*/','',$match[1]);
52
//strip leading slashes
53
$id = preg_replace('!^/+!','',$id);
55
if($clean) $id = cleanID($id);
56
if(empty($id) && $param=='id') $id = $conf['start'];
62
* Remove unwanted chars from ID
64
* Cleans a given ID to only use allowed characters. Accented characters are
65
* converted to unaccented ones
67
* @author Andreas Gohr <andi@splitbrain.org>
68
* @param string $raw_id The pageid to clean
69
* @param boolean $ascii Force ASCII
71
function cleanID($raw_id,$ascii=false){
74
static $sepcharpat = null;
76
global $cache_cleanid;
77
$cache = & $cache_cleanid;
79
// check if it's already in the memory cache
80
if (isset($cache[$raw_id])) {
81
return $cache[$raw_id];
84
$sepchar = $conf['sepchar'];
85
if($sepcharpat == null) // build string only once to save clock cycles
86
$sepcharpat = '#\\'.$sepchar.'+#';
89
$id = utf8_strtolower($id);
91
//alternative namespace seperator
92
$id = strtr($id,';',':');
93
if($conf['useslash']){
94
$id = strtr($id,'/',':');
96
$id = strtr($id,'/',$sepchar);
99
if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
100
if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
103
$id = utf8_stripspecials($id,$sepchar,'\*');
105
if($ascii) $id = utf8_strip($id);
108
$id = preg_replace($sepcharpat,$sepchar,$id);
109
$id = preg_replace('#:+#',':',$id);
110
$id = trim($id,':._-');
111
$id = preg_replace('#:[:\._\-]+#',':',$id);
113
$cache[$raw_id] = $id;
118
* Return namespacepart of a wiki ID
120
* @author Andreas Gohr <andi@splitbrain.org>
123
$pos = strrpos($id,':');
125
return substr($id,0,$pos);
131
* Returns the ID without the namespace
133
* @author Andreas Gohr <andi@splitbrain.org>
136
$pos = strrpos($id, ':');
138
return substr($id, $pos+1);
145
* returns the full path to the datafile specified by ID and
148
* The filename is URL encoded to protect Unicode chars
150
* @author Andreas Gohr <andi@splitbrain.org>
152
function wikiFN($raw_id,$rev='',$clean=true){
155
global $cache_wikifn;
156
$cache = & $cache_wikifn;
158
if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
159
return $cache[$raw_id][$rev];
164
if ($clean) $id = cleanID($id);
165
$id = str_replace(':','/',$id);
167
$fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
169
$fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
170
if($conf['compression']){
171
//test for extensions here, we want to read both compressions
172
if (@file_exists($fn . '.gz')){
174
}else if(@file_exists($fn . '.bz2')){
177
//file doesnt exist yet, so we take the configured extension
178
$fn .= '.' . $conf['compression'];
183
if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
184
$cache[$raw_id][$rev] = $fn;
189
* Returns the full path to the file for locking the page while editing.
191
* @author Ben Coburn <btcoburn@silicodon.net>
193
function wikiLockFN($id) {
195
return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
200
* returns the full path to the meta file specified by ID and extension
202
* The filename is URL encoded to protect Unicode chars
204
* @author Steven Danz <steven-danz@kc.rr.com>
206
function metaFN($id,$ext){
209
$id = str_replace(':','/',$id);
210
$fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
215
* returns an array of full paths to all metafiles of a given ID
217
* @author Esther Brunner <esther@kaffeehaus.ch>
219
function metaFiles($id){
221
$dir = metaFN(getNS($id),'');
224
$dh = @opendir($dir);
225
if(!$dh) return $files;
226
while(($file = readdir($dh)) !== false){
227
if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
228
$files[] = $dir.$file;
236
* returns the full path to the mediafile specified by ID
238
* The filename is URL encoded to protect Unicode chars
240
* @author Andreas Gohr <andi@splitbrain.org>
242
function mediaFN($id){
245
$id = str_replace(':','/',$id);
246
$fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
251
* Returns the full filepath to a localized textfile if local
252
* version isn't found the english one is returned
254
* @author Andreas Gohr <andi@splitbrain.org>
256
function localeFN($id){
258
$file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
259
if(!@file_exists($file)){
260
//fall back to english
261
$file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
267
* Resolve relative paths in IDs
269
* Do not call directly use resolve_mediaid or resolve_pageid
272
* Partyly based on a cleanPath function found at
273
* http://www.php.net/manual/en/function.realpath.php#57016
275
* @author <bart at mediawave dot nl>
277
function resolve_id($ns,$id,$clean=true){
278
// if the id starts with a dot we need to handle the
281
// normalize initial dots without a colon
282
$id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
283
// prepend the current namespace
288
$pathA = explode(':', $id);
289
if (!$pathA[0]) $result[] = '';
290
foreach ($pathA AS $key => $dir) {
292
if (end($result) == '..') {
294
} elseif (!array_pop($result)) {
297
} elseif ($dir && $dir != '.') {
301
if (!end($pathA)) $result[] = '';
302
$id = implode(':', $result);
303
}elseif($ns !== false && strpos($id,':') === false){
304
//if link contains no namespace. add current namespace (if any)
308
if($clean) $id = cleanID($id);
313
* Returns a full media id
315
* @author Andreas Gohr <andi@splitbrain.org>
317
function resolve_mediaid($ns,&$page,&$exists){
318
$page = resolve_id($ns,$page);
319
$file = mediaFN($page);
320
$exists = @file_exists($file);
324
* Returns a full page id
326
* @author Andreas Gohr <andi@splitbrain.org>
328
function resolve_pageid($ns,&$page,&$exists){
332
//keep hashlink if exists then clean both parts
333
if (strpos($page,'#')) {
334
list($page,$hash) = split('#',$page,2);
338
$hash = cleanID($hash);
339
$page = resolve_id($ns,$page,false); // resolve but don't clean, yet
341
// get filename (calls clean itself)
342
$file = wikiFN($page);
344
// if ends with colon we have a namespace link
345
if(substr($page,-1) == ':'){
346
if(@file_exists(wikiFN($page.$conf['start']))){
347
// start page inside namespace
348
$page = $page.$conf['start'];
350
}elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){
351
// page named like the NS inside the NS
352
$page = $page.noNS(cleanID($page));
354
}elseif(@file_exists(wikiFN($page))){
355
// page like namespace exists
359
// fall back to default
360
$page = $page.$conf['start'];
363
//check alternative plural/nonplural form
364
if(!@file_exists($file)){
365
if( $conf['autoplural'] ){
366
if(substr($page,-1) == 's'){
367
$try = substr($page,0,-1);
371
if(@file_exists(wikiFN($try))){
381
// now make sure we have a clean page
382
$page = cleanID($page);
385
if(!empty($hash)) $page .= '#'.$hash;
389
* Returns the name of a cachefile from given data
391
* The needed directory is created by this function!
393
* @author Andreas Gohr <andi@splitbrain.org>
395
* @param string $data This data is used to create a unique md5 name
396
* @param string $ext This is appended to the filename if given
397
* @return string The filename of the cachefile
399
function getCacheName($data,$ext=''){
402
$file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
403
io_makeFileDir($file);
408
* Checks a pageid against $conf['hidepages']
410
* @author Andreas Gohr <gohr@cosmocode.de>
412
function isHiddenPage($id){
414
if(empty($conf['hidepages'])) return false;
416
if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
423
* Reverse of isHiddenPage
425
* @author Andreas Gohr <gohr@cosmocode.de>
427
function isVisiblePage($id){
428
return !isHiddenPage($id);
432
* Checks and sets HTTP headers for conditional HTTP requests
434
* @author Simon Willison <swillison@gmail.com>
435
* @link http://simon.incutio.com/archive/2003/04/23/conditionalGet
436
* @param timestamp $timestamp lastmodified time of the cache file
437
* @returns void or void with previously header() commands executed
439
function http_conditionalRequest($timestamp){
440
// A PHP implementation of conditional get, see
441
// http://fishbowl.pastiche.org/archives/001132.html
442
$last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
443
$etag = '"'.md5($last_modified).'"';
445
header("Last-Modified: $last_modified");
446
header("ETag: $etag");
447
// See if the client has provided the required headers
448
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
449
$if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
451
$if_modified_since = false;
454
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
455
$if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
457
$if_none_match = false;
460
if (!$if_modified_since && !$if_none_match){
464
// At least one of the headers is there - check them
465
if ($if_none_match && $if_none_match != $etag) {
466
return; // etag is there but doesn't match
469
if ($if_modified_since && $if_modified_since != $last_modified) {
470
return; // if-modified-since is there but doesn't match
473
// Nothing has changed since their last request - serve a 304 and exit
474
header('HTTP/1.0 304 Not Modified');
478
//Setup VIM: ex: et ts=2 enc=utf-8 :