xref: /dokuwiki/inc/pageutils.php (revision a6858c6a90527664406b82e32fcf457628700df1)
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  if($clean) $id = cleanID($id);
56  if(empty($id) && $param=='id') $id = $conf['start'];
57
58  return $id;
59}
60
61/**
62 * Remove unwanted chars from ID
63 *
64 * Cleans a given ID to only use allowed characters. Accented characters are
65 * converted to unaccented ones
66 *
67 * @author Andreas Gohr <andi@splitbrain.org>
68 * @param  string  $id    The pageid to clean
69 * @param  boolean $ascii Force ASCII
70 */
71function cleanID($id,$ascii=false){
72  global $conf;
73  global $lang;
74  static $sepcharpat = null;
75
76  $sepchar = $conf['sepchar'];
77  if($sepcharpat == null) // build string only once to save clock cycles
78    $sepcharpat = '#\\'.$sepchar.'+#';
79
80  $id = trim($id);
81  $id = utf8_strtolower($id);
82
83  //alternative namespace seperator
84  $id = strtr($id,';',':');
85  if($conf['useslash']){
86    $id = strtr($id,'/',':');
87  }else{
88    $id = strtr($id,'/',$sepchar);
89  }
90
91  if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
92  if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
93
94  //remove specials
95  $id = utf8_stripspecials($id,$sepchar,'\*');
96
97  if($ascii) $id = utf8_strip($id);
98
99  //clean up
100  $id = preg_replace($sepcharpat,$sepchar,$id);
101  $id = preg_replace('#:+#',':',$id);
102  $id = trim($id,':._-');
103  $id = preg_replace('#:[:\._\-]+#',':',$id);
104
105  return($id);
106}
107
108/**
109 * Return namespacepart of a wiki ID
110 *
111 * @author Andreas Gohr <andi@splitbrain.org>
112 */
113function getNS($id){
114  $pos = strrpos($id,':');
115  if($pos!==false){
116    return substr($id,0,$pos);
117  }
118  return false;
119}
120
121/**
122 * Returns the ID without the namespace
123 *
124 * @author Andreas Gohr <andi@splitbrain.org>
125 */
126function noNS($id) {
127  $pos = strrpos($id, ':');
128  if ($pos!==false) {
129    return substr($id, $pos+1);
130  } else {
131    return $id;
132  }
133}
134
135/**
136 * returns the full path to the datafile specified by ID and
137 * optional revision
138 *
139 * The filename is URL encoded to protect Unicode chars
140 *
141 * @author Andreas Gohr <andi@splitbrain.org>
142 */
143function wikiFN($id,$rev=''){
144  global $conf;
145  $id = cleanID($id);
146  $id = str_replace(':','/',$id);
147  if(empty($rev)){
148    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
149  }else{
150    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
151    if($conf['usegzip'] && !@file_exists($fn)){
152      //return gzip if enabled and plaintext doesn't exist
153      $fn .= '.gz';
154    }
155  }
156  return $fn;
157}
158
159/**
160 * Returns the full path to the file for locking the page while editing.
161 *
162 * @author Ben Coburn <btcoburn@silicodon.net>
163 */
164function wikiLockFN($id) {
165  global $conf;
166  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
167}
168
169
170/**
171 * returns the full path to the meta file specified by ID and extension
172 *
173 * The filename is URL encoded to protect Unicode chars
174 *
175 * @author Steven Danz <steven-danz@kc.rr.com>
176 */
177function metaFN($id,$ext){
178  global $conf;
179  $id = cleanID($id);
180  $id = str_replace(':','/',$id);
181  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
182  return $fn;
183}
184
185/**
186 * returns an array of full paths to all metafiles of a given ID
187 *
188 * @author Esther Brunner <esther@kaffeehaus.ch>
189 */
190function metaFiles($id){
191   $name   = noNS($id);
192   $dir    = metaFN(getNS($id),'');
193   $files  = array();
194
195   $dh = @opendir($dir);
196   if(!$dh) return $files;
197   while(($file = readdir($dh)) !== false){
198     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
199       $files[] = $dir.$file;
200   }
201   closedir($dh);
202
203   return $files;
204}
205
206/**
207 * returns the full path to the mediafile specified by ID
208 *
209 * The filename is URL encoded to protect Unicode chars
210 *
211 * @author Andreas Gohr <andi@splitbrain.org>
212 */
213function mediaFN($id){
214  global $conf;
215  $id = cleanID($id);
216  $id = str_replace(':','/',$id);
217    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
218  return $fn;
219}
220
221/**
222 * Returns the full filepath to a localized textfile if local
223 * version isn't found the english one is returned
224 *
225 * @author Andreas Gohr <andi@splitbrain.org>
226 */
227function localeFN($id){
228  global $conf;
229  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
230  if(!@file_exists($file)){
231    //fall back to english
232    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
233  }
234  return $file;
235}
236
237/**
238 * Resolve relative paths in IDs
239 *
240 * Do not call directly use resolve_mediaid or resolve_pageid
241 * instead
242 *
243 * Partyly based on a cleanPath function found at
244 * http://www.php.net/manual/en/function.realpath.php#57016
245 *
246 * @author <bart at mediawave dot nl>
247 */
248function resolve_id($ns,$id,$clean=true){
249  // if the id starts with a dot we need to handle the
250  // relative stuff
251  if($id{0} == '.'){
252    // normalize initial dots without a colon
253    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
254    // prepend the current namespace
255    $id = $ns.':'.$id;
256
257    // cleanup relatives
258    $result = array();
259    $pathA  = explode(':', $id);
260    if (!$pathA[0]) $result[] = '';
261    foreach ($pathA AS $key => $dir) {
262      if ($dir == '..') {
263        if (end($result) == '..') {
264          $result[] = '..';
265        } elseif (!array_pop($result)) {
266          $result[] = '..';
267        }
268      } elseif ($dir && $dir != '.') {
269        $result[] = $dir;
270      }
271    }
272    if (!end($pathA)) $result[] = '';
273    $id = implode(':', $result);
274  }elseif($ns !== false && strpos($id,':') === false){
275    //if link contains no namespace. add current namespace (if any)
276    $id = $ns.':'.$id;
277  }
278
279  if($clean) $id = cleanID($id);
280  return $id;
281}
282
283/**
284 * Returns a full media id
285 *
286 * @author Andreas Gohr <andi@splitbrain.org>
287 */
288function resolve_mediaid($ns,&$page,&$exists){
289  $page   = resolve_id($ns,$page);
290  $file   = mediaFN($page);
291  $exists = @file_exists($file);
292}
293
294/**
295 * Returns a full page id
296 *
297 * @author Andreas Gohr <andi@splitbrain.org>
298 */
299function resolve_pageid($ns,&$page,&$exists){
300  global $conf;
301  $exists = false;
302
303  //keep hashlink if exists then clean both parts
304  if (strpos($page,'#')) {
305    list($page,$hash) = split('#',$page,2);
306  } else {
307    $hash = '';
308  }
309  $hash = cleanID($hash);
310  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
311
312  // get filename (calls clean itself)
313  $file = wikiFN($page);
314
315  // if ends with colon we have a namespace link
316  if(substr($page,-1) == ':'){
317    if(@file_exists(wikiFN($page.$conf['start']))){
318      // start page inside namespace
319      $page = $page.$conf['start'];
320      $exists = true;
321    }elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){
322      // page named like the NS inside the NS
323      $page = $page.noNS(cleanID($page));
324      $exists = true;
325    }elseif(@file_exists(wikiFN($page))){
326      // page like namespace exists
327      $page = $page;
328      $exists = true;
329    }else{
330      // fall back to default
331      $page = $page.$conf['start'];
332    }
333  }else{
334    //check alternative plural/nonplural form
335    if(!@file_exists($file)){
336      if( $conf['autoplural'] ){
337        if(substr($page,-1) == 's'){
338          $try = substr($page,0,-1);
339        }else{
340          $try = $page.'s';
341        }
342        if(@file_exists(wikiFN($try))){
343          $page   = $try;
344          $exists = true;
345        }
346      }
347    }else{
348      $exists = true;
349    }
350  }
351
352  // now make sure we have a clean page
353  $page = cleanID($page);
354
355  //add hash if any
356  if(!empty($hash)) $page .= '#'.$hash;
357}
358
359/**
360 * Returns the name of a cachefile from given data
361 *
362 * The needed directory is created by this function!
363 *
364 * @author Andreas Gohr <andi@splitbrain.org>
365 *
366 * @param string $data  This data is used to create a unique md5 name
367 * @param string $ext   This is appended to the filename if given
368 * @return string       The filename of the cachefile
369 */
370function getCacheName($data,$ext=''){
371  global $conf;
372  $md5  = md5($data);
373  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
374  io_makeFileDir($file);
375  return $file;
376}
377
378/**
379 * Checks a pageid against $conf['hidepages']
380 *
381 * @author Andreas Gohr <gohr@cosmocode.de>
382 */
383function isHiddenPage($id){
384  global $conf;
385  if(empty($conf['hidepages'])) return false;
386
387  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
388    return true;
389  }
390  return false;
391}
392
393/**
394 * Reverse of isHiddenPage
395 *
396 * @author Andreas Gohr <gohr@cosmocode.de>
397 */
398function isVisiblePage($id){
399  return !isHiddenPage($id);
400}
401
402/**
403 * Checks and sets HTTP headers for conditional HTTP requests
404 *
405 * @author   Simon Willison <swillison@gmail.com>
406 * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
407 * @param    timestamp $timestamp lastmodified time of the cache file
408 * @returns  void or void with previously header() commands executed
409 */
410function http_conditionalRequest($timestamp){
411  // A PHP implementation of conditional get, see
412  //   http://fishbowl.pastiche.org/archives/001132.html
413  $last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
414  $etag = '"'.md5($last_modified).'"';
415  // Send the headers
416  header("Last-Modified: $last_modified");
417  header("ETag: $etag");
418  // See if the client has provided the required headers
419  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
420    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
421  }else{
422    $if_modified_since = false;
423  }
424
425  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
426    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
427  }else{
428    $if_none_match = false;
429  }
430
431  if (!$if_modified_since && !$if_none_match){
432    return;
433  }
434
435  // At least one of the headers is there - check them
436  if ($if_none_match && $if_none_match != $etag) {
437    return; // etag is there but doesn't match
438  }
439
440  if ($if_modified_since && $if_modified_since != $last_modified) {
441    return; // if-modified-since is there but doesn't match
442  }
443
444  // Nothing has changed since their last request - serve a 304 and exit
445  header('HTTP/1.0 304 Not Modified');
446  exit;
447}
448
449//Setup VIM: ex: et ts=2 enc=utf-8 :
450