xref: /dokuwiki/inc/pageutils.php (revision 6faaead86be36e84bff4062af115119428f35166)
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 * @todo       Combine similar functions like {wiki,media,meta}FN()
8 */
9
10/**
11 * Fetch the an ID from request
12 *
13 * Uses either standard $_REQUEST variable or extracts it from
14 * the full request URI when userewrite is set to 2
15 *
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.
18 *
19 * @author Andreas Gohr <andi@splitbrain.org>
20 */
21function getID($param='id',$clean=true){
22  global $conf;
23
24  $id = isset($_REQUEST[$param]) ? $_REQUEST[$param] : null;
25
26  //construct page id from request URI
27  if(empty($id) && $conf['userewrite'] == 2){
28    //get the script URL
29    if($conf['basedir']){
30      $relpath = '';
31      if($param != 'id') {
32        $relpath = 'lib/exe/';
33      }
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;
39    }else{
40      $script = $_SERVER['SCRIPT_NAME'];
41    }
42
43    //clean script and request (fixes a windows problem)
44    $script  = preg_replace('/\/\/+/','/',$script);
45    $request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']);
46
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]);
50    }
51    $id = urldecode($id);
52    //strip leading slashes
53    $id = preg_replace('!^/+!','',$id);
54  }
55
56  // Namespace autolinking from URL
57  if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
58    if(page_exists($id.$conf['start'])){
59      // start page inside namespace
60      $id = $id.$conf['start'];
61    }elseif(page_exists($id.noNS(cleanID($id)))){
62      // page named like the NS inside the NS
63      $id = $id.noNS(cleanID($id));
64    }elseif(page_exists($id)){
65      // page like namespace exists
66      $id = substr($id,0,-1);
67    }else{
68      // fall back to default
69      $id = $id.$conf['start'];
70    }
71    header("Location: ".wl($id,'',true));
72  }
73
74  if($clean) $id = cleanID($id);
75  if(empty($id) && $param=='id') $id = $conf['start'];
76
77  return $id;
78}
79
80/**
81 * Remove unwanted chars from ID
82 *
83 * Cleans a given ID to only use allowed characters. Accented characters are
84 * converted to unaccented ones
85 *
86 * @author Andreas Gohr <andi@splitbrain.org>
87 * @param  string  $raw_id    The pageid to clean
88 * @param  boolean $ascii     Force ASCII
89 */
90function cleanID($raw_id,$ascii=false){
91  global $conf;
92  global $lang;
93  static $sepcharpat = null;
94
95  global $cache_cleanid;
96  $cache = & $cache_cleanid;
97
98  // check if it's already in the memory cache
99  if (isset($cache[$raw_id])) {
100    return $cache[$raw_id];
101    }
102
103  $sepchar = $conf['sepchar'];
104  if($sepcharpat == null) // build string only once to save clock cycles
105    $sepcharpat = '#\\'.$sepchar.'+#';
106
107  $id = trim($raw_id);
108  $id = utf8_strtolower($id);
109
110  //alternative namespace seperator
111  $id = strtr($id,';',':');
112  if($conf['useslash']){
113    $id = strtr($id,'/',':');
114  }else{
115    $id = strtr($id,'/',$sepchar);
116  }
117
118  if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
119  if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
120
121  //remove specials
122  $id = utf8_stripspecials($id,$sepchar,'\*');
123
124  if($ascii) $id = utf8_strip($id);
125
126  //clean up
127  $id = preg_replace($sepcharpat,$sepchar,$id);
128  $id = preg_replace('#:+#',':',$id);
129  $id = trim($id,':._-');
130  $id = preg_replace('#:[:\._\-]+#',':',$id);
131
132  $cache[$raw_id] = $id;
133  return($id);
134}
135
136/**
137 * Return namespacepart of a wiki ID
138 *
139 * @author Andreas Gohr <andi@splitbrain.org>
140 */
141function getNS($id){
142  $pos = strrpos($id,':');
143  if($pos!==false){
144    return substr($id,0,$pos);
145  }
146  return false;
147}
148
149/**
150 * Returns the ID without the namespace
151 *
152 * @author Andreas Gohr <andi@splitbrain.org>
153 */
154function noNS($id) {
155  $pos = strrpos($id, ':');
156  if ($pos!==false) {
157    return substr($id, $pos+1);
158  } else {
159    return $id;
160  }
161}
162
163/**
164* Returns the current namespace
165*
166* @author Nathan Fritz <fritzn@crown.edu>
167*/
168function curNS($id) {
169    return noNS(getNS($id));
170}
171
172/**
173* Returns the ID without the namespace or current namespace for 'start' pages
174*
175* @author Nathan Fritz <fritzn@crown.edu>
176*/
177function noNSorNS($id) {
178    global $conf;
179
180    $p = noNS($id);
181    if ($p == $conf['start']) {
182        $p = curNS($id);
183        if ($p == false) {
184            return noNS($id);
185        }
186    }
187    return $p;
188}
189
190/**
191 *  Wiki page existence check
192 *
193 *  parameters as for wikiFN
194 *
195 *  @author Chris Smith <chris@jalakai.co.uk>
196 */
197function page_exists($id,$rev='',$clean=true) {
198  return @file_exists(wikiFN($id,$rev,$clean));
199}
200
201/**
202 * returns the full path to the datafile specified by ID and optional revision
203 *
204 * The filename is URL encoded to protect Unicode chars
205 *
206 * @param  $raw_id  string   id of wikipage
207 * @param  $rev     string   page revision, empty string for current
208 * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
209 *                           when $id is guaranteed to have been cleaned already.
210 *
211 * @author Andreas Gohr <andi@splitbrain.org>
212 */
213function wikiFN($raw_id,$rev='',$clean=true){
214  global $conf;
215
216  global $cache_wikifn;
217  $cache = & $cache_wikifn;
218
219  if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
220    return $cache[$raw_id][$rev];
221  }
222
223  $id = $raw_id;
224
225  if ($clean) $id = cleanID($id);
226  $id = str_replace(':','/',$id);
227  if(empty($rev)){
228    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
229  }else{
230    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
231    if($conf['compression']){
232      //test for extensions here, we want to read both compressions
233       if (@file_exists($fn . '.gz')){
234          $fn .= '.gz';
235       }else if(@file_exists($fn . '.bz2')){
236          $fn .= '.bz2';
237       }else{
238          //file doesnt exist yet, so we take the configured extension
239          $fn .= '.' . $conf['compression'];
240       }
241    }
242  }
243
244  if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
245  $cache[$raw_id][$rev] = $fn;
246  return $fn;
247}
248
249/**
250 * Returns the full path to the file for locking the page while editing.
251 *
252 * @author Ben Coburn <btcoburn@silicodon.net>
253 */
254function wikiLockFN($id) {
255  global $conf;
256  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
257}
258
259
260/**
261 * returns the full path to the meta file specified by ID and extension
262 *
263 * The filename is URL encoded to protect Unicode chars
264 *
265 * @author Steven Danz <steven-danz@kc.rr.com>
266 */
267function metaFN($id,$ext){
268  global $conf;
269  $id = cleanID($id);
270  $id = str_replace(':','/',$id);
271  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
272  return $fn;
273}
274
275/**
276 * returns an array of full paths to all metafiles of a given ID
277 *
278 * @author Esther Brunner <esther@kaffeehaus.ch>
279 */
280function metaFiles($id){
281   $name   = noNS($id);
282   $dir    = metaFN(getNS($id),'');
283   $files  = array();
284
285   $dh = @opendir($dir);
286   if(!$dh) return $files;
287   while(($file = readdir($dh)) !== false){
288     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
289       $files[] = $dir.$file;
290   }
291   closedir($dh);
292
293   return $files;
294}
295
296/**
297 * returns the full path to the mediafile specified by ID
298 *
299 * The filename is URL encoded to protect Unicode chars
300 *
301 * @author Andreas Gohr <andi@splitbrain.org>
302 */
303function mediaFN($id){
304  global $conf;
305  $id = cleanID($id);
306  $id = str_replace(':','/',$id);
307    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
308  return $fn;
309}
310
311/**
312 * Returns the full filepath to a localized textfile if local
313 * version isn't found the english one is returned
314 *
315 * @author Andreas Gohr <andi@splitbrain.org>
316 */
317function localeFN($id){
318  global $conf;
319  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
320  if(!@file_exists($file)){
321    //fall back to english
322    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
323  }
324  return $file;
325}
326
327/**
328 * Resolve relative paths in IDs
329 *
330 * Do not call directly use resolve_mediaid or resolve_pageid
331 * instead
332 *
333 * Partyly based on a cleanPath function found at
334 * http://www.php.net/manual/en/function.realpath.php#57016
335 *
336 * @author <bart at mediawave dot nl>
337 */
338function resolve_id($ns,$id,$clean=true){
339  global $conf;
340
341  // some pre cleaning for useslash:
342  if($conf['useslash']) $id = str_replace('/',':',$id);
343
344  // if the id starts with a dot we need to handle the
345  // relative stuff
346  if($id{0} == '.'){
347    // normalize initial dots without a colon
348    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
349    // prepend the current namespace
350    $id = $ns.':'.$id;
351
352    // cleanup relatives
353    $result = array();
354    $pathA  = explode(':', $id);
355    if (!$pathA[0]) $result[] = '';
356    foreach ($pathA AS $key => $dir) {
357      if ($dir == '..') {
358        if (end($result) == '..') {
359          $result[] = '..';
360        } elseif (!array_pop($result)) {
361          $result[] = '..';
362        }
363      } elseif ($dir && $dir != '.') {
364        $result[] = $dir;
365      }
366    }
367    if (!end($pathA)) $result[] = '';
368    $id = implode(':', $result);
369  }elseif($ns !== false && strpos($id,':') === false){
370    //if link contains no namespace. add current namespace (if any)
371    $id = $ns.':'.$id;
372  }
373
374  if($clean) $id = cleanID($id);
375  return $id;
376}
377
378/**
379 * Returns a full media id
380 *
381 * @author Andreas Gohr <andi@splitbrain.org>
382 */
383function resolve_mediaid($ns,&$page,&$exists){
384  $page   = resolve_id($ns,$page);
385  $file   = mediaFN($page);
386  $exists = @file_exists($file);
387}
388
389/**
390 * Returns a full page id
391 *
392 * @author Andreas Gohr <andi@splitbrain.org>
393 */
394function resolve_pageid($ns,&$page,&$exists){
395  global $conf;
396  $exists = false;
397
398  //keep hashlink if exists then clean both parts
399  if (strpos($page,'#')) {
400    list($page,$hash) = split('#',$page,2);
401  } else {
402    $hash = '';
403  }
404  $hash = cleanID($hash);
405  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
406
407  // get filename (calls clean itself)
408  $file = wikiFN($page);
409
410  // if ends with colon or slash we have a namespace link
411  if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){
412    if(page_exists($page.$conf['start'])){
413      // start page inside namespace
414      $page = $page.$conf['start'];
415      $exists = true;
416    }elseif(page_exists($page.noNS(cleanID($page)))){
417      // page named like the NS inside the NS
418      $page = $page.noNS(cleanID($page));
419      $exists = true;
420    }elseif(page_exists($page)){
421      // page like namespace exists
422      $page = $page;
423      $exists = true;
424    }else{
425      // fall back to default
426      $page = $page.$conf['start'];
427    }
428  }else{
429    //check alternative plural/nonplural form
430    if(!@file_exists($file)){
431      if( $conf['autoplural'] ){
432        if(substr($page,-1) == 's'){
433          $try = substr($page,0,-1);
434        }else{
435          $try = $page.'s';
436        }
437        if(page_exists($try)){
438          $page   = $try;
439          $exists = true;
440        }
441      }
442    }else{
443      $exists = true;
444    }
445  }
446
447  // now make sure we have a clean page
448  $page = cleanID($page);
449
450  //add hash if any
451  if(!empty($hash)) $page .= '#'.$hash;
452}
453
454/**
455 * Returns the name of a cachefile from given data
456 *
457 * The needed directory is created by this function!
458 *
459 * @author Andreas Gohr <andi@splitbrain.org>
460 *
461 * @param string $data  This data is used to create a unique md5 name
462 * @param string $ext   This is appended to the filename if given
463 * @return string       The filename of the cachefile
464 */
465function getCacheName($data,$ext=''){
466  global $conf;
467  $md5  = md5($data);
468  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
469  io_makeFileDir($file);
470  return $file;
471}
472
473/**
474 * Checks a pageid against $conf['hidepages']
475 *
476 * @author Andreas Gohr <gohr@cosmocode.de>
477 */
478function isHiddenPage($id){
479  global $conf;
480  if(empty($conf['hidepages'])) return false;
481
482  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
483    return true;
484  }
485  return false;
486}
487
488/**
489 * Reverse of isHiddenPage
490 *
491 * @author Andreas Gohr <gohr@cosmocode.de>
492 */
493function isVisiblePage($id){
494  return !isHiddenPage($id);
495}
496
497/**
498 * Checks and sets HTTP headers for conditional HTTP requests
499 *
500 * @author   Simon Willison <swillison@gmail.com>
501 * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
502 * @param    timestamp $timestamp lastmodified time of the cache file
503 * @returns  void or void with previously header() commands executed
504 */
505function http_conditionalRequest($timestamp){
506  // A PHP implementation of conditional get, see
507  //   http://fishbowl.pastiche.org/archives/001132.html
508  $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT';
509  $etag = '"'.md5($last_modified).'"';
510  // Send the headers
511  header("Last-Modified: $last_modified");
512  header("ETag: $etag");
513  // See if the client has provided the required headers
514  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
515    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
516  }else{
517    $if_modified_since = false;
518  }
519
520  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
521    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
522  }else{
523    $if_none_match = false;
524  }
525
526  if (!$if_modified_since && !$if_none_match){
527    return;
528  }
529
530  // At least one of the headers is there - check them
531  if ($if_none_match && $if_none_match != $etag) {
532    return; // etag is there but doesn't match
533  }
534
535  if ($if_modified_since && $if_modified_since != $last_modified) {
536    return; // if-modified-since is there but doesn't match
537  }
538
539  // Nothing has changed since their last request - serve a 304 and exit
540  header('HTTP/1.0 304 Not Modified');
541
542  // don't produce output, even if compression is on
543  ob_end_clean();
544  exit;
545}
546
547//Setup VIM: ex: et ts=2 enc=utf-8 :
548