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