xref: /dokuwiki/inc/pageutils.php (revision ad81d4312bb97f5d88438632707e8c4d561a3914)
1b625487dSandi<?php
2b625487dSandi/**
3b625487dSandi * Utilities for handling pagenames
4b625487dSandi *
5b625487dSandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6b625487dSandi * @author     Andreas Gohr <andi@splitbrain.org>
71380fc45SAndreas Gohr * @todo       Combine similar functions like {wiki,media,meta}FN()
8b625487dSandi */
9b625487dSandi
106c7843b5Sandi/**
116de3759aSAndreas Gohr * Fetch the an ID from request
126c7843b5Sandi *
136c7843b5Sandi * Uses either standard $_REQUEST variable or extracts it from
146c7843b5Sandi * the full request URI when userewrite is set to 2
156c7843b5Sandi *
1642905504SAndreas Gohr * For $param='id' $conf['start'] is returned if no id was found.
1742905504SAndreas Gohr * If the second parameter is true (default) the ID is cleaned.
186c7843b5Sandi *
196c7843b5Sandi * @author Andreas Gohr <andi@splitbrain.org>
206c7843b5Sandi */
2142905504SAndreas Gohrfunction getID($param='id',$clean=true){
226c7843b5Sandi  global $conf;
236c7843b5Sandi
2448665d38SAndreas Gohr  $id = $_REQUEST[$param];
2548665d38SAndreas Gohr
266c7843b5Sandi  //construct page id from request URI
276c7843b5Sandi  if(empty($id) && $conf['userewrite'] == 2){
286c7843b5Sandi    //get the script URL
296c7843b5Sandi    if($conf['basedir']){
3081124000Sjan      $relpath = '';
3181124000Sjan      if($param != 'id') {
3281124000Sjan        $relpath = 'lib/exe/';
3381124000Sjan      }
3481124000Sjan      $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
356c7843b5Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
366c7843b5Sandi      $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
376c7843b5Sandi                              $_SERVER['SCRIPT_FILENAME']);
386c7843b5Sandi      $script = '/'.$script;
396c7843b5Sandi    }else{
406c7843b5Sandi      $script = $_SERVER['SCRIPT_NAME'];
416c7843b5Sandi    }
426c7843b5Sandi
4352339126Sandi    //clean script and request (fixes a windows problem)
4452339126Sandi    $script  = preg_replace('/\/\/+/','/',$script);
4552339126Sandi    $request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']);
4652339126Sandi
476c7843b5Sandi    //remove script URL and Querystring to gain the id
4852339126Sandi    if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
496c7843b5Sandi      $id = preg_replace ('/\?.*/','',$match[1]);
506c7843b5Sandi    }
516de3759aSAndreas Gohr    $id = urldecode($id);
5242905504SAndreas Gohr    //strip leading slashes
5342905504SAndreas Gohr    $id = preg_replace('!^/+!','',$id);
546c7843b5Sandi  }
5542905504SAndreas Gohr  if(empty($id) && $param=='id') $id = $conf['start'];
5642905504SAndreas Gohr  if($clean) $id = cleanID($id);
576c7843b5Sandi
586c7843b5Sandi  return $id;
596c7843b5Sandi}
60b625487dSandi
61b625487dSandi/**
62b625487dSandi * Remove unwanted chars from ID
63b625487dSandi *
64b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
65b625487dSandi * converted to unaccented ones
66b625487dSandi *
67b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
68b625487dSandi */
69b625487dSandifunction cleanID($id){
70b625487dSandi  global $conf;
71b625487dSandi  global $lang;
724b5db43bSjoe.lapp  static $sepcharpat = null;
734b5db43bSjoe.lapp
744b5db43bSjoe.lapp  $sepchar = $conf['sepchar'];
754b5db43bSjoe.lapp  if($sepcharpat == null) // build string only once to save clock cycles
764b5db43bSjoe.lapp    $sepcharpat = '#\\'.$sepchar.'+#';
774b5db43bSjoe.lapp
78b625487dSandi  $id = trim($id);
79b625487dSandi  $id = utf8_strtolower($id);
80b625487dSandi
81b625487dSandi  //alternative namespace seperator
82b625487dSandi  $id = strtr($id,';',':');
83b625487dSandi  if($conf['useslash']){
84b625487dSandi    $id = strtr($id,'/',':');
85b625487dSandi  }else{
864eeffcd2SAndreas Gohr    $id = strtr($id,'/',$sepchar);
87b625487dSandi  }
88b625487dSandi
89b625487dSandi  if($conf['deaccent']) $id = utf8_deaccent($id,-1);
90b625487dSandi
91b625487dSandi  //remove specials
92*ad81d431SAndreas Gohr  $id = utf8_stripspecials($id,$sepchar,'\*');
93b625487dSandi
94b625487dSandi  //clean up
954b5db43bSjoe.lapp  $id = preg_replace($sepcharpat,$sepchar,$id);
96b625487dSandi  $id = preg_replace('#:+#',':',$id);
97b625487dSandi  $id = trim($id,':._-');
98b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
99b625487dSandi
100b625487dSandi  return($id);
101b625487dSandi}
102b625487dSandi
103b625487dSandi/**
104b625487dSandi * Return namespacepart of a wiki ID
105b625487dSandi *
106b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
107b625487dSandi */
108b625487dSandifunction getNS($id){
109b625487dSandi if(strpos($id,':')!==false){
110b625487dSandi   return substr($id,0,strrpos($id,':'));
111b625487dSandi }
112b625487dSandi return false;
113b625487dSandi}
114b625487dSandi
115b625487dSandi/**
116b625487dSandi * Returns the ID without the namespace
117b625487dSandi *
118b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
119b625487dSandi */
120b625487dSandifunction noNS($id){
121b625487dSandi  return preg_replace('/.*:/','',$id);
122b625487dSandi}
123b625487dSandi
124b625487dSandi/**
125b625487dSandi * returns the full path to the datafile specified by ID and
126b625487dSandi * optional revision
127b625487dSandi *
128b625487dSandi * The filename is URL encoded to protect Unicode chars
129b625487dSandi *
130b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
131b625487dSandi */
132b625487dSandifunction wikiFN($id,$rev=''){
133b625487dSandi  global $conf;
134b625487dSandi  $id = cleanID($id);
135b625487dSandi  $id = str_replace(':','/',$id);
136b625487dSandi  if(empty($rev)){
137b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
138b625487dSandi  }else{
139b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
140b625487dSandi    if($conf['usegzip'] && !@file_exists($fn)){
141b625487dSandi      //return gzip if enabled and plaintext doesn't exist
142b625487dSandi      $fn .= '.gz';
143b625487dSandi    }
144b625487dSandi  }
145b625487dSandi  return $fn;
146b625487dSandi}
147b625487dSandi
148b625487dSandi/**
1491380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
150b158d625SSteven Danz *
151b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
152b158d625SSteven Danz *
153b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
154b158d625SSteven Danz */
1551380fc45SAndreas Gohrfunction metaFN($id,$ext){
156b158d625SSteven Danz  global $conf;
157b158d625SSteven Danz  $id = cleanID($id);
158b158d625SSteven Danz  $id = str_replace(':','/',$id);
1591380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
160b158d625SSteven Danz  return $fn;
161b158d625SSteven Danz}
162b158d625SSteven Danz
163b158d625SSteven Danz/**
164e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
165e1f3d9e1SEsther Brunner *
166e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
167e1f3d9e1SEsther Brunner */
168e1f3d9e1SEsther Brunnerfunction metaFiles($id){
169e1f3d9e1SEsther Brunner   $name   = noNS($id);
170e1f3d9e1SEsther Brunner   $dir    = metaFN(getNS($id),'');
171e1f3d9e1SEsther Brunner   $files  = array();
172e1f3d9e1SEsther Brunner
173e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
1745011da9dSEsther Brunner   if(!$dh) return $files;
175e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
1761a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
177e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
178e1f3d9e1SEsther Brunner   }
179e1f3d9e1SEsther Brunner   closedir($dh);
180e1f3d9e1SEsther Brunner
181e1f3d9e1SEsther Brunner   return $files;
182e1f3d9e1SEsther Brunner}
183e1f3d9e1SEsther Brunner
184e1f3d9e1SEsther Brunner/**
185b625487dSandi * returns the full path to the mediafile specified by ID
186b625487dSandi *
187b625487dSandi * The filename is URL encoded to protect Unicode chars
188b625487dSandi *
189b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
190b625487dSandi */
191b625487dSandifunction mediaFN($id){
192b625487dSandi  global $conf;
193b625487dSandi  $id = cleanID($id);
194b625487dSandi  $id = str_replace(':','/',$id);
195b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
196b625487dSandi  return $fn;
197b625487dSandi}
198b625487dSandi
199b625487dSandi/**
200b625487dSandi * Returns the full filepath to a localized textfile if local
201b625487dSandi * version isn't found the english one is returned
202b625487dSandi *
203b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
204b625487dSandi */
205b625487dSandifunction localeFN($id){
206b625487dSandi  global $conf;
207bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
208b625487dSandi  if(!@file_exists($file)){
209b625487dSandi    //fall back to english
210bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
211b625487dSandi  }
212b625487dSandi  return $file;
213b625487dSandi}
214b625487dSandi
215b625487dSandi/**
216b625487dSandi * Returns a full media id
217b625487dSandi *
218b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
219b625487dSandi */
22037e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
221b625487dSandi  global $conf;
22237e34a5eSandi
223b625487dSandi  //if links starts with . add current namespace
224b625487dSandi  if($page{0} == '.'){
225b625487dSandi    $page = $ns.':'.substr($page,1);
226b625487dSandi  }
227b625487dSandi
228b625487dSandi  //if link contains no namespace. add current namespace (if any)
229b625487dSandi  if($ns !== false && strpos($page,':') === false){
230b625487dSandi    $page = $ns.':'.$page;
231b625487dSandi  }
232b625487dSandi
233b625487dSandi  $page   = cleanID($page);
234b625487dSandi  $file   = mediaFN($page);
235b625487dSandi  $exists = @file_exists($file);
236b625487dSandi}
237b625487dSandi
238b625487dSandi/**
239b625487dSandi * Returns a full page id
240b625487dSandi *
241b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
242b625487dSandi */
24337e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
244b625487dSandi  global $conf;
2450b7c14c2Sandi  $exists = false;
246b625487dSandi
247b625487dSandi  //if links starts with . add current namespace
248b625487dSandi  if($page{0} == '.'){
249b625487dSandi    $page = $ns.':'.substr($page,1);
250b625487dSandi  }
251b625487dSandi
252b625487dSandi  //if link contains no namespace. add current namespace (if any)
253b625487dSandi  if($ns !== false && strpos($page,':') === false){
254b625487dSandi    $page = $ns.':'.$page;
255b625487dSandi  }
256b625487dSandi
257b625487dSandi  //keep hashlink if exists then clean both parts
258b625487dSandi  list($page,$hash) = split('#',$page,2);
259b625487dSandi  $page = cleanID($page);
260b625487dSandi  $hash = cleanID($hash);
261b625487dSandi
262b625487dSandi  $file = wikiFN($page);
263b625487dSandi
264b625487dSandi  //check alternative plural/nonplural form
265b625487dSandi  if(!@file_exists($file)){
266b625487dSandi    if( $conf['autoplural'] ){
267b625487dSandi      if(substr($page,-1) == 's'){
268b625487dSandi        $try = substr($page,0,-1);
269b625487dSandi      }else{
270b625487dSandi        $try = $page.'s';
271b625487dSandi      }
272b625487dSandi      if(@file_exists(wikiFN($try))){
273b625487dSandi        $page   = $try;
274b625487dSandi        $exists = true;
275b625487dSandi      }
276b625487dSandi    }
277b625487dSandi  }else{
278b625487dSandi    $exists = true;
279b625487dSandi  }
280b625487dSandi
281b625487dSandi  //add hash if any
282b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
283b625487dSandi}
284b625487dSandi
28598407a7aSandi/**
28698407a7aSandi * Returns the name of a cachefile from given data
28798407a7aSandi *
28898407a7aSandi * The needed directory is created by this function!
28998407a7aSandi *
29098407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
29198407a7aSandi *
29298407a7aSandi * @param string $data  This data is used to create a unique md5 name
29398407a7aSandi * @param string $ext   This is appended to the filename if given
29498407a7aSandi * @return string       The filename of the cachefile
29598407a7aSandi */
29698407a7aSandifunction getCacheName($data,$ext=''){
29798407a7aSandi  global $conf;
29898407a7aSandi  $md5  = md5($data);
29998407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
30098407a7aSandi  io_makeFileDir($file);
30198407a7aSandi  return $file;
30298407a7aSandi}
30398407a7aSandi
3040dc92c6fSAndreas Gohr/**
3050dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
3060dc92c6fSAndreas Gohr *
3070dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
3080dc92c6fSAndreas Gohr */
3090dc92c6fSAndreas Gohrfunction isHiddenPage($id){
3100dc92c6fSAndreas Gohr  global $conf;
3110dc92c6fSAndreas Gohr  if(empty($conf['hidepages'])) return false;
3120dc92c6fSAndreas Gohr
3130dc92c6fSAndreas Gohr  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
3140dc92c6fSAndreas Gohr    return true;
3150dc92c6fSAndreas Gohr  }
3160dc92c6fSAndreas Gohr  return false;
3170dc92c6fSAndreas Gohr}
3180dc92c6fSAndreas Gohr
3190dc92c6fSAndreas Gohr/**
3200dc92c6fSAndreas Gohr * Reverse of isHiddenPage
3210dc92c6fSAndreas Gohr *
3220dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
3230dc92c6fSAndreas Gohr */
3240dc92c6fSAndreas Gohrfunction isVisiblePage($id){
3250dc92c6fSAndreas Gohr  return !isHiddenPage($id);
3260dc92c6fSAndreas Gohr}
3270dc92c6fSAndreas Gohr
328b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
329