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