xref: /dokuwiki/inc/pageutils.php (revision 48665d389b9bb386283c08172b24f3af26628bce)
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 *
16*48665d38SAndreas Gohr * For $param='id' $conf['start'] is returned if no id was found
17*48665d38SAndreas Gohr * and the returned ID will be cleaned. For other params the
18*48665d38SAndreas 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
25*48665d38SAndreas Gohr  $id = $_REQUEST[$param];
26*48665d38SAndreas Gohr
27*48665d38SAndreas 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']){
336de3759aSAndreas Gohr      $script = $conf['basedir'].basedir($_SERVER['SCRIPT_FILENAME']);
346c7843b5Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
356c7843b5Sandi      $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
366c7843b5Sandi                              $_SERVER['SCRIPT_FILENAME']);
376c7843b5Sandi      $script = '/'.$script;
386c7843b5Sandi    }else{
396c7843b5Sandi      $script = $_SERVER['SCRIPT_NAME'];
406c7843b5Sandi    }
416c7843b5Sandi
4252339126Sandi    //clean script and request (fixes a windows problem)
4352339126Sandi    $script  = preg_replace('/\/\/+/','/',$script);
4452339126Sandi    $request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']);
4552339126Sandi
466c7843b5Sandi    //remove script URL and Querystring to gain the id
4752339126Sandi    if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
486c7843b5Sandi      $id = preg_replace ('/\?.*/','',$match[1]);
496c7843b5Sandi    }
506de3759aSAndreas Gohr    $id = urldecode($id);
516c7843b5Sandi    $id = cleanID($id);
526c7843b5Sandi  }
536de3759aSAndreas Gohr  if(empty($id) && $param='id') $id = $conf['start'];
546c7843b5Sandi
556c7843b5Sandi  return $id;
566c7843b5Sandi}
57b625487dSandi
58b625487dSandi/**
59b625487dSandi * Remove unwanted chars from ID
60b625487dSandi *
61b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
62b625487dSandi * converted to unaccented ones
63b625487dSandi *
64b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
65b625487dSandi */
66b625487dSandifunction cleanID($id){
67b625487dSandi  global $conf;
68b625487dSandi  global $lang;
69b625487dSandi  $id = trim($id);
70b625487dSandi  $id = utf8_strtolower($id);
71b625487dSandi
72b625487dSandi  //alternative namespace seperator
73b625487dSandi  $id = strtr($id,';',':');
74b625487dSandi  if($conf['useslash']){
75b625487dSandi    $id = strtr($id,'/',':');
76b625487dSandi  }else{
77b625487dSandi    $id = strtr($id,'/','_');
78b625487dSandi  }
79b625487dSandi
80b625487dSandi  if($conf['deaccent']) $id = utf8_deaccent($id,-1);
81b625487dSandi
82b625487dSandi  //remove specials
835c812709Sandi  $id = utf8_stripspecials($id,'_');
84b625487dSandi
85b625487dSandi  //clean up
8606bd9a88Sandi  $id = preg_replace('#_+#','_',$id);
87b625487dSandi  $id = preg_replace('#:+#',':',$id);
88b625487dSandi  $id = trim($id,':._-');
89b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
90b625487dSandi
91b625487dSandi  return($id);
92b625487dSandi}
93b625487dSandi
94b625487dSandi/**
95b625487dSandi * Return namespacepart of a wiki ID
96b625487dSandi *
97b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
98b625487dSandi */
99b625487dSandifunction getNS($id){
100b625487dSandi if(strpos($id,':')!==false){
101b625487dSandi   return substr($id,0,strrpos($id,':'));
102b625487dSandi }
103b625487dSandi return false;
104b625487dSandi}
105b625487dSandi
106b625487dSandi/**
107b625487dSandi * Returns the ID without the namespace
108b625487dSandi *
109b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
110b625487dSandi */
111b625487dSandifunction noNS($id){
112b625487dSandi  return preg_replace('/.*:/','',$id);
113b625487dSandi}
114b625487dSandi
115b625487dSandi/**
116b625487dSandi * returns the full path to the datafile specified by ID and
117b625487dSandi * optional revision
118b625487dSandi *
119b625487dSandi * The filename is URL encoded to protect Unicode chars
120b625487dSandi *
121b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
122b625487dSandi */
123b625487dSandifunction wikiFN($id,$rev=''){
124b625487dSandi  global $conf;
125b625487dSandi  $id = cleanID($id);
126b625487dSandi  $id = str_replace(':','/',$id);
127b625487dSandi  if(empty($rev)){
128b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
129b625487dSandi  }else{
130b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
131b625487dSandi    if($conf['usegzip'] && !@file_exists($fn)){
132b625487dSandi      //return gzip if enabled and plaintext doesn't exist
133b625487dSandi      $fn .= '.gz';
134b625487dSandi    }
135b625487dSandi  }
136b625487dSandi  return $fn;
137b625487dSandi}
138b625487dSandi
139b625487dSandi/**
1401380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
141b158d625SSteven Danz *
142b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
143b158d625SSteven Danz *
144b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
145b158d625SSteven Danz */
1461380fc45SAndreas Gohrfunction metaFN($id,$ext){
147b158d625SSteven Danz  global $conf;
148b158d625SSteven Danz  $id = cleanID($id);
149b158d625SSteven Danz  $id = str_replace(':','/',$id);
1501380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
151b158d625SSteven Danz  return $fn;
152b158d625SSteven Danz}
153b158d625SSteven Danz
154b158d625SSteven Danz/**
155b625487dSandi * returns the full path to the mediafile specified by ID
156b625487dSandi *
157b625487dSandi * The filename is URL encoded to protect Unicode chars
158b625487dSandi *
159b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
160b625487dSandi */
161b625487dSandifunction mediaFN($id){
162b625487dSandi  global $conf;
163b625487dSandi  $id = cleanID($id);
164b625487dSandi  $id = str_replace(':','/',$id);
165b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
166b625487dSandi  return $fn;
167b625487dSandi}
168b625487dSandi
169b625487dSandi/**
170b625487dSandi * Returns the full filepath to a localized textfile if local
171b625487dSandi * version isn't found the english one is returned
172b625487dSandi *
173b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
174b625487dSandi */
175b625487dSandifunction localeFN($id){
176b625487dSandi  global $conf;
177bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
178b625487dSandi  if(!@file_exists($file)){
179b625487dSandi    //fall back to english
180bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
181b625487dSandi  }
182b625487dSandi  return $file;
183b625487dSandi}
184b625487dSandi
185b625487dSandi/**
186b625487dSandi * Returns a full media id
187b625487dSandi *
188b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
189b625487dSandi */
19037e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
191b625487dSandi  global $conf;
19237e34a5eSandi
193b625487dSandi  //if links starts with . add current namespace
194b625487dSandi  if($page{0} == '.'){
195b625487dSandi    $page = $ns.':'.substr($page,1);
196b625487dSandi  }
197b625487dSandi
198b625487dSandi  //if link contains no namespace. add current namespace (if any)
199b625487dSandi  if($ns !== false && strpos($page,':') === false){
200b625487dSandi    $page = $ns.':'.$page;
201b625487dSandi  }
202b625487dSandi
203b625487dSandi  $page   = cleanID($page);
204b625487dSandi  $file   = mediaFN($page);
205b625487dSandi  $exists = @file_exists($file);
206b625487dSandi}
207b625487dSandi
208b625487dSandi/**
209b625487dSandi * Returns a full page id
210b625487dSandi *
211b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
212b625487dSandi */
21337e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
214b625487dSandi  global $conf;
2150b7c14c2Sandi  $exists = false;
216b625487dSandi
217b625487dSandi  //if links starts with . add current namespace
218b625487dSandi  if($page{0} == '.'){
219b625487dSandi    $page = $ns.':'.substr($page,1);
220b625487dSandi  }
221b625487dSandi
222b625487dSandi  //if link contains no namespace. add current namespace (if any)
223b625487dSandi  if($ns !== false && strpos($page,':') === false){
224b625487dSandi    $page = $ns.':'.$page;
225b625487dSandi  }
226b625487dSandi
227b625487dSandi  //keep hashlink if exists then clean both parts
228b625487dSandi  list($page,$hash) = split('#',$page,2);
229b625487dSandi  $page = cleanID($page);
230b625487dSandi  $hash = cleanID($hash);
231b625487dSandi
232b625487dSandi  $file = wikiFN($page);
233b625487dSandi
234b625487dSandi  //check alternative plural/nonplural form
235b625487dSandi  if(!@file_exists($file)){
236b625487dSandi    if( $conf['autoplural'] ){
237b625487dSandi      if(substr($page,-1) == 's'){
238b625487dSandi        $try = substr($page,0,-1);
239b625487dSandi      }else{
240b625487dSandi        $try = $page.'s';
241b625487dSandi      }
242b625487dSandi      if(@file_exists(wikiFN($try))){
243b625487dSandi        $page   = $try;
244b625487dSandi        $exists = true;
245b625487dSandi      }
246b625487dSandi    }
247b625487dSandi  }else{
248b625487dSandi    $exists = true;
249b625487dSandi  }
250b625487dSandi
251b625487dSandi  //add hash if any
252b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
253b625487dSandi}
254b625487dSandi
25598407a7aSandi/**
25698407a7aSandi * Returns the name of a cachefile from given data
25798407a7aSandi *
25898407a7aSandi * The needed directory is created by this function!
25998407a7aSandi *
26098407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
26198407a7aSandi *
26298407a7aSandi * @param string $data  This data is used to create a unique md5 name
26398407a7aSandi * @param string $ext   This is appended to the filename if given
26498407a7aSandi * @return string       The filename of the cachefile
26598407a7aSandi */
26698407a7aSandifunction getCacheName($data,$ext=''){
26798407a7aSandi  global $conf;
26898407a7aSandi  $md5  = md5($data);
26998407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
27098407a7aSandi  io_makeFileDir($file);
27198407a7aSandi  return $file;
27298407a7aSandi}
27398407a7aSandi
274b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
275