xref: /dokuwiki/inc/pageutils.php (revision b158d625b53833ef391800a991ad93d965d9425e)
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>
7b625487dSandi */
8b625487dSandi
96c7843b5Sandi/**
106c7843b5Sandi * Fetch the pageid
116c7843b5Sandi *
126c7843b5Sandi * Uses either standard $_REQUEST variable or extracts it from
136c7843b5Sandi * the full request URI when userewrite is set to 2
146c7843b5Sandi *
156c7843b5Sandi * Returns $conf['start'] if no id was found.
166c7843b5Sandi *
176c7843b5Sandi * @author Andreas Gohr <andi@splitbrain.org>
186c7843b5Sandi */
196c7843b5Sandifunction getID(){
206c7843b5Sandi  global $conf;
216c7843b5Sandi
226c7843b5Sandi  $id = cleanID($_REQUEST['id']);
236c7843b5Sandi
246c7843b5Sandi  //construct page id from request URI
256c7843b5Sandi  if(empty($id) && $conf['userewrite'] == 2){
266c7843b5Sandi    //get the script URL
276c7843b5Sandi    if($conf['basedir']){
286c7843b5Sandi      $script = $conf['basedir'].DOKU_SCRIPT;
296c7843b5Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
306c7843b5Sandi      $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
316c7843b5Sandi                              $_SERVER['SCRIPT_FILENAME']);
326c7843b5Sandi      $script = '/'.$script;
336c7843b5Sandi    }else{
346c7843b5Sandi      $script = $_SERVER['SCRIPT_NAME'];
356c7843b5Sandi    }
366c7843b5Sandi
3752339126Sandi    //clean script and request (fixes a windows problem)
3852339126Sandi    $script  = preg_replace('/\/\/+/','/',$script);
3952339126Sandi    $request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']);
4052339126Sandi
416c7843b5Sandi    //remove script URL and Querystring to gain the id
4252339126Sandi    if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
436c7843b5Sandi      $id = preg_replace ('/\?.*/','',$match[1]);
446c7843b5Sandi    }
456c7843b5Sandi    $id = cleanID($id);
466c7843b5Sandi  }
476c7843b5Sandi  if(empty($id)) $id = $conf['start'];
486c7843b5Sandi
496c7843b5Sandi  return $id;
506c7843b5Sandi}
51b625487dSandi
52b625487dSandi/**
53b625487dSandi * Remove unwanted chars from ID
54b625487dSandi *
55b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
56b625487dSandi * converted to unaccented ones
57b625487dSandi *
58b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
59b625487dSandi */
60b625487dSandifunction cleanID($id){
61b625487dSandi  global $conf;
62b625487dSandi  global $lang;
63b625487dSandi  $id = trim($id);
64b625487dSandi  $id = utf8_strtolower($id);
65b625487dSandi
66b625487dSandi  //alternative namespace seperator
67b625487dSandi  $id = strtr($id,';',':');
68b625487dSandi  if($conf['useslash']){
69b625487dSandi    $id = strtr($id,'/',':');
70b625487dSandi  }else{
71b625487dSandi    $id = strtr($id,'/','_');
72b625487dSandi  }
73b625487dSandi
74b625487dSandi  if($conf['deaccent']) $id = utf8_deaccent($id,-1);
75b625487dSandi
76b625487dSandi  //remove specials
775c812709Sandi  $id = utf8_stripspecials($id,'_');
78b625487dSandi
79b625487dSandi  //clean up
8006bd9a88Sandi  $id = preg_replace('#_+#','_',$id);
81b625487dSandi  $id = preg_replace('#:+#',':',$id);
82b625487dSandi  $id = trim($id,':._-');
83b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
84b625487dSandi
85b625487dSandi  return($id);
86b625487dSandi}
87b625487dSandi
88b625487dSandi/**
89b625487dSandi * Return namespacepart of a wiki ID
90b625487dSandi *
91b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
92b625487dSandi */
93b625487dSandifunction getNS($id){
94b625487dSandi if(strpos($id,':')!==false){
95b625487dSandi   return substr($id,0,strrpos($id,':'));
96b625487dSandi }
97b625487dSandi return false;
98b625487dSandi}
99b625487dSandi
100b625487dSandi/**
101b625487dSandi * Returns the ID without the namespace
102b625487dSandi *
103b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
104b625487dSandi */
105b625487dSandifunction noNS($id){
106b625487dSandi  return preg_replace('/.*:/','',$id);
107b625487dSandi}
108b625487dSandi
109b625487dSandi/**
110b625487dSandi * returns the full path to the datafile specified by ID and
111b625487dSandi * optional revision
112b625487dSandi *
113b625487dSandi * The filename is URL encoded to protect Unicode chars
114b625487dSandi *
115b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
116b625487dSandi */
117b625487dSandifunction wikiFN($id,$rev=''){
118b625487dSandi  global $conf;
119b625487dSandi  $id = cleanID($id);
120b625487dSandi  $id = str_replace(':','/',$id);
121b625487dSandi  if(empty($rev)){
122b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
123b625487dSandi  }else{
124b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
125b625487dSandi    if($conf['usegzip'] && !@file_exists($fn)){
126b625487dSandi      //return gzip if enabled and plaintext doesn't exist
127b625487dSandi      $fn .= '.gz';
128b625487dSandi    }
129b625487dSandi  }
130b625487dSandi  return $fn;
131b625487dSandi}
132b625487dSandi
133b625487dSandi/**
134*b158d625SSteven Danz * returns the full path to the mailist specified by ID
135*b158d625SSteven Danz *
136*b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
137*b158d625SSteven Danz *
138*b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
139*b158d625SSteven Danz */
140*b158d625SSteven Danzfunction wikiMN($id){
141*b158d625SSteven Danz  global $conf;
142*b158d625SSteven Danz  $id = cleanID($id);
143*b158d625SSteven Danz  $id = str_replace(':','/',$id);
144*b158d625SSteven Danz  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).'.mlist';
145*b158d625SSteven Danz  return $fn;
146*b158d625SSteven Danz}
147*b158d625SSteven Danz
148*b158d625SSteven Danz/**
149b625487dSandi * returns the full path to the mediafile specified by ID
150b625487dSandi *
151b625487dSandi * The filename is URL encoded to protect Unicode chars
152b625487dSandi *
153b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
154b625487dSandi */
155b625487dSandifunction mediaFN($id){
156b625487dSandi  global $conf;
157b625487dSandi  $id = cleanID($id);
158b625487dSandi  $id = str_replace(':','/',$id);
159b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
160b625487dSandi  return $fn;
161b625487dSandi}
162b625487dSandi
163b625487dSandi/**
164b625487dSandi * Returns the full filepath to a localized textfile if local
165b625487dSandi * version isn't found the english one is returned
166b625487dSandi *
167b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
168b625487dSandi */
169b625487dSandifunction localeFN($id){
170b625487dSandi  global $conf;
171bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
172b625487dSandi  if(!@file_exists($file)){
173b625487dSandi    //fall back to english
174bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
175b625487dSandi  }
176b625487dSandi  return $file;
177b625487dSandi}
178b625487dSandi
179b625487dSandi/**
180b625487dSandi * Returns a full media id
181b625487dSandi *
182b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
183b625487dSandi */
18437e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
185b625487dSandi  global $conf;
18637e34a5eSandi
187b625487dSandi  //if links starts with . add current namespace
188b625487dSandi  if($page{0} == '.'){
189b625487dSandi    $page = $ns.':'.substr($page,1);
190b625487dSandi  }
191b625487dSandi
192b625487dSandi  //if link contains no namespace. add current namespace (if any)
193b625487dSandi  if($ns !== false && strpos($page,':') === false){
194b625487dSandi    $page = $ns.':'.$page;
195b625487dSandi  }
196b625487dSandi
197b625487dSandi  $page   = cleanID($page);
198b625487dSandi  $file   = mediaFN($page);
199b625487dSandi  $exists = @file_exists($file);
200b625487dSandi}
201b625487dSandi
202b625487dSandi/**
203b625487dSandi * Returns a full page id
204b625487dSandi *
205b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
206b625487dSandi */
20737e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
208b625487dSandi  global $conf;
2090b7c14c2Sandi  $exists = false;
210b625487dSandi
211b625487dSandi  //if links starts with . add current namespace
212b625487dSandi  if($page{0} == '.'){
213b625487dSandi    $page = $ns.':'.substr($page,1);
214b625487dSandi  }
215b625487dSandi
216b625487dSandi  //if link contains no namespace. add current namespace (if any)
217b625487dSandi  if($ns !== false && strpos($page,':') === false){
218b625487dSandi    $page = $ns.':'.$page;
219b625487dSandi  }
220b625487dSandi
221b625487dSandi  //keep hashlink if exists then clean both parts
222b625487dSandi  list($page,$hash) = split('#',$page,2);
223b625487dSandi  $page = cleanID($page);
224b625487dSandi  $hash = cleanID($hash);
225b625487dSandi
226b625487dSandi  $file = wikiFN($page);
227b625487dSandi
228b625487dSandi  //check alternative plural/nonplural form
229b625487dSandi  if(!@file_exists($file)){
230b625487dSandi    if( $conf['autoplural'] ){
231b625487dSandi      if(substr($page,-1) == 's'){
232b625487dSandi        $try = substr($page,0,-1);
233b625487dSandi      }else{
234b625487dSandi        $try = $page.'s';
235b625487dSandi      }
236b625487dSandi      if(@file_exists(wikiFN($try))){
237b625487dSandi        $page   = $try;
238b625487dSandi        $exists = true;
239b625487dSandi      }
240b625487dSandi    }
241b625487dSandi  }else{
242b625487dSandi    $exists = true;
243b625487dSandi  }
244b625487dSandi
245b625487dSandi  //add hash if any
246b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
247b625487dSandi}
248b625487dSandi
24998407a7aSandi/**
25098407a7aSandi * Returns the name of a cachefile from given data
25198407a7aSandi *
25298407a7aSandi * The needed directory is created by this function!
25398407a7aSandi *
25498407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
25598407a7aSandi *
25698407a7aSandi * @param string $data  This data is used to create a unique md5 name
25798407a7aSandi * @param string $ext   This is appended to the filename if given
25898407a7aSandi * @return string       The filename of the cachefile
25998407a7aSandi */
26098407a7aSandifunction getCacheName($data,$ext=''){
26198407a7aSandi  global $conf;
26298407a7aSandi  $md5  = md5($data);
26398407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
26498407a7aSandi  io_makeFileDir($file);
26598407a7aSandi  return $file;
26698407a7aSandi}
26798407a7aSandi
268b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
269