xref: /dokuwiki/inc/pageutils.php (revision 81124000ddd28d1b4c8c39847dfe27783342ea75)
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 *
1648665d38SAndreas Gohr * For $param='id' $conf['start'] is returned if no id was found
1748665d38SAndreas Gohr * and the returned ID will be cleaned. For other params the
1848665d38SAndreas Gohr * cleaning has to be done outside this function
196c7843b5Sandi *
206c7843b5Sandi * @author Andreas Gohr <andi@splitbrain.org>
216c7843b5Sandi */
226de3759aSAndreas Gohrfunction getID($param='id'){
236c7843b5Sandi  global $conf;
246c7843b5Sandi
2548665d38SAndreas Gohr  $id = $_REQUEST[$param];
2648665d38SAndreas Gohr
2748665d38SAndreas Gohr  if($param == 'id') $id = cleanID($id);
286c7843b5Sandi
296c7843b5Sandi  //construct page id from request URI
306c7843b5Sandi  if(empty($id) && $conf['userewrite'] == 2){
316c7843b5Sandi    //get the script URL
326c7843b5Sandi    if($conf['basedir']){
33*81124000Sjan      $relpath = '';
34*81124000Sjan      if($param != 'id') {
35*81124000Sjan        $relpath = 'lib/exe/';
36*81124000Sjan      }
37*81124000Sjan      $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
386c7843b5Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
396c7843b5Sandi      $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
406c7843b5Sandi                              $_SERVER['SCRIPT_FILENAME']);
416c7843b5Sandi      $script = '/'.$script;
426c7843b5Sandi    }else{
436c7843b5Sandi      $script = $_SERVER['SCRIPT_NAME'];
446c7843b5Sandi    }
456c7843b5Sandi
4652339126Sandi    //clean script and request (fixes a windows problem)
4752339126Sandi    $script  = preg_replace('/\/\/+/','/',$script);
4852339126Sandi    $request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']);
4952339126Sandi
506c7843b5Sandi    //remove script URL and Querystring to gain the id
5152339126Sandi    if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
526c7843b5Sandi      $id = preg_replace ('/\?.*/','',$match[1]);
536c7843b5Sandi    }
546de3759aSAndreas Gohr    $id = urldecode($id);
556c7843b5Sandi    $id = cleanID($id);
566c7843b5Sandi  }
57c25a7c81SAndreas Gohr  if(empty($id) && $param=='id') $id = cleanID($conf['start']);
586c7843b5Sandi
596c7843b5Sandi  return $id;
606c7843b5Sandi}
61b625487dSandi
62b625487dSandi/**
63b625487dSandi * Remove unwanted chars from ID
64b625487dSandi *
65b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
66b625487dSandi * converted to unaccented ones
67b625487dSandi *
68b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
69b625487dSandi */
70b625487dSandifunction cleanID($id){
71b625487dSandi  global $conf;
72b625487dSandi  global $lang;
734b5db43bSjoe.lapp  static $sepcharpat = null;
744b5db43bSjoe.lapp
754b5db43bSjoe.lapp  $sepchar = $conf['sepchar'];
764b5db43bSjoe.lapp  if($sepcharpat == null) // build string only once to save clock cycles
774b5db43bSjoe.lapp    $sepcharpat = '#\\'.$sepchar.'+#';
784b5db43bSjoe.lapp
79b625487dSandi  $id = trim($id);
80b625487dSandi  $id = utf8_strtolower($id);
81b625487dSandi
82b625487dSandi  //alternative namespace seperator
83b625487dSandi  $id = strtr($id,';',':');
84b625487dSandi  if($conf['useslash']){
85b625487dSandi    $id = strtr($id,'/',':');
86b625487dSandi  }else{
874eeffcd2SAndreas Gohr    $id = strtr($id,'/',$sepchar);
88b625487dSandi  }
89b625487dSandi
90b625487dSandi  if($conf['deaccent']) $id = utf8_deaccent($id,-1);
91b625487dSandi
92b625487dSandi  //remove specials
934b5db43bSjoe.lapp  $id = utf8_stripspecials($id,$sepchar);
94b625487dSandi
95b625487dSandi  //clean up
964b5db43bSjoe.lapp  $id = preg_replace($sepcharpat,$sepchar,$id);
97b625487dSandi  $id = preg_replace('#:+#',':',$id);
98b625487dSandi  $id = trim($id,':._-');
99b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
100b625487dSandi
101b625487dSandi  return($id);
102b625487dSandi}
103b625487dSandi
104b625487dSandi/**
105b625487dSandi * Return namespacepart of a wiki ID
106b625487dSandi *
107b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
108b625487dSandi */
109b625487dSandifunction getNS($id){
110b625487dSandi if(strpos($id,':')!==false){
111b625487dSandi   return substr($id,0,strrpos($id,':'));
112b625487dSandi }
113b625487dSandi return false;
114b625487dSandi}
115b625487dSandi
116b625487dSandi/**
117b625487dSandi * Returns the ID without the namespace
118b625487dSandi *
119b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
120b625487dSandi */
121b625487dSandifunction noNS($id){
122b625487dSandi  return preg_replace('/.*:/','',$id);
123b625487dSandi}
124b625487dSandi
125b625487dSandi/**
126b625487dSandi * returns the full path to the datafile specified by ID and
127b625487dSandi * optional revision
128b625487dSandi *
129b625487dSandi * The filename is URL encoded to protect Unicode chars
130b625487dSandi *
131b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
132b625487dSandi */
133b625487dSandifunction wikiFN($id,$rev=''){
134b625487dSandi  global $conf;
135b625487dSandi  $id = cleanID($id);
136b625487dSandi  $id = str_replace(':','/',$id);
137b625487dSandi  if(empty($rev)){
138b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
139b625487dSandi  }else{
140b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
141b625487dSandi    if($conf['usegzip'] && !@file_exists($fn)){
142b625487dSandi      //return gzip if enabled and plaintext doesn't exist
143b625487dSandi      $fn .= '.gz';
144b625487dSandi    }
145b625487dSandi  }
146b625487dSandi  return $fn;
147b625487dSandi}
148b625487dSandi
149b625487dSandi/**
1501380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
151b158d625SSteven Danz *
152b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
153b158d625SSteven Danz *
154b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
155b158d625SSteven Danz */
1561380fc45SAndreas Gohrfunction metaFN($id,$ext){
157b158d625SSteven Danz  global $conf;
158b158d625SSteven Danz  $id = cleanID($id);
159b158d625SSteven Danz  $id = str_replace(':','/',$id);
1601380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
161b158d625SSteven Danz  return $fn;
162b158d625SSteven Danz}
163b158d625SSteven Danz
164b158d625SSteven Danz/**
165e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
166e1f3d9e1SEsther Brunner *
167e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
168e1f3d9e1SEsther Brunner */
169e1f3d9e1SEsther Brunnerfunction metaFiles($id){
170e1f3d9e1SEsther Brunner   $name   = noNS($id);
171e1f3d9e1SEsther Brunner   $dir    = metaFN(getNS($id),'');
172e1f3d9e1SEsther Brunner   $files  = array();
173e1f3d9e1SEsther Brunner
174e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
1755011da9dSEsther Brunner   if(!$dh) return $files;
176e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
1771a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
178e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
179e1f3d9e1SEsther Brunner   }
180e1f3d9e1SEsther Brunner   closedir($dh);
181e1f3d9e1SEsther Brunner
182e1f3d9e1SEsther Brunner   return $files;
183e1f3d9e1SEsther Brunner}
184e1f3d9e1SEsther Brunner
185e1f3d9e1SEsther Brunner/**
186b625487dSandi * returns the full path to the mediafile specified by ID
187b625487dSandi *
188b625487dSandi * The filename is URL encoded to protect Unicode chars
189b625487dSandi *
190b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
191b625487dSandi */
192b625487dSandifunction mediaFN($id){
193b625487dSandi  global $conf;
194b625487dSandi  $id = cleanID($id);
195b625487dSandi  $id = str_replace(':','/',$id);
196b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
197b625487dSandi  return $fn;
198b625487dSandi}
199b625487dSandi
200b625487dSandi/**
201b625487dSandi * Returns the full filepath to a localized textfile if local
202b625487dSandi * version isn't found the english one is returned
203b625487dSandi *
204b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
205b625487dSandi */
206b625487dSandifunction localeFN($id){
207b625487dSandi  global $conf;
208bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
209b625487dSandi  if(!@file_exists($file)){
210b625487dSandi    //fall back to english
211bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
212b625487dSandi  }
213b625487dSandi  return $file;
214b625487dSandi}
215b625487dSandi
216b625487dSandi/**
217b625487dSandi * Returns a full media id
218b625487dSandi *
219b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
220b625487dSandi */
22137e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
222b625487dSandi  global $conf;
22337e34a5eSandi
224b625487dSandi  //if links starts with . add current namespace
225b625487dSandi  if($page{0} == '.'){
226b625487dSandi    $page = $ns.':'.substr($page,1);
227b625487dSandi  }
228b625487dSandi
229b625487dSandi  //if link contains no namespace. add current namespace (if any)
230b625487dSandi  if($ns !== false && strpos($page,':') === false){
231b625487dSandi    $page = $ns.':'.$page;
232b625487dSandi  }
233b625487dSandi
234b625487dSandi  $page   = cleanID($page);
235b625487dSandi  $file   = mediaFN($page);
236b625487dSandi  $exists = @file_exists($file);
237b625487dSandi}
238b625487dSandi
239b625487dSandi/**
240b625487dSandi * Returns a full page id
241b625487dSandi *
242b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
243b625487dSandi */
24437e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
245b625487dSandi  global $conf;
2460b7c14c2Sandi  $exists = false;
247b625487dSandi
248b625487dSandi  //if links starts with . add current namespace
249b625487dSandi  if($page{0} == '.'){
250b625487dSandi    $page = $ns.':'.substr($page,1);
251b625487dSandi  }
252b625487dSandi
253b625487dSandi  //if link contains no namespace. add current namespace (if any)
254b625487dSandi  if($ns !== false && strpos($page,':') === false){
255b625487dSandi    $page = $ns.':'.$page;
256b625487dSandi  }
257b625487dSandi
258b625487dSandi  //keep hashlink if exists then clean both parts
259b625487dSandi  list($page,$hash) = split('#',$page,2);
260b625487dSandi  $page = cleanID($page);
261b625487dSandi  $hash = cleanID($hash);
262b625487dSandi
263b625487dSandi  $file = wikiFN($page);
264b625487dSandi
265b625487dSandi  //check alternative plural/nonplural form
266b625487dSandi  if(!@file_exists($file)){
267b625487dSandi    if( $conf['autoplural'] ){
268b625487dSandi      if(substr($page,-1) == 's'){
269b625487dSandi        $try = substr($page,0,-1);
270b625487dSandi      }else{
271b625487dSandi        $try = $page.'s';
272b625487dSandi      }
273b625487dSandi      if(@file_exists(wikiFN($try))){
274b625487dSandi        $page   = $try;
275b625487dSandi        $exists = true;
276b625487dSandi      }
277b625487dSandi    }
278b625487dSandi  }else{
279b625487dSandi    $exists = true;
280b625487dSandi  }
281b625487dSandi
282b625487dSandi  //add hash if any
283b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
284b625487dSandi}
285b625487dSandi
28698407a7aSandi/**
28798407a7aSandi * Returns the name of a cachefile from given data
28898407a7aSandi *
28998407a7aSandi * The needed directory is created by this function!
29098407a7aSandi *
29198407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
29298407a7aSandi *
29398407a7aSandi * @param string $data  This data is used to create a unique md5 name
29498407a7aSandi * @param string $ext   This is appended to the filename if given
29598407a7aSandi * @return string       The filename of the cachefile
29698407a7aSandi */
29798407a7aSandifunction getCacheName($data,$ext=''){
29898407a7aSandi  global $conf;
29998407a7aSandi  $md5  = md5($data);
30098407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
30198407a7aSandi  io_makeFileDir($file);
30298407a7aSandi  return $file;
30398407a7aSandi}
30498407a7aSandi
305b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
306