xref: /dokuwiki/inc/pageutils.php (revision 4ceab83fd024405dfa43e10241bb9a9f7fee5bf5)
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
2403c4aec3Schris  $id = isset($_REQUEST[$param]) ? $_REQUEST[$param] : null;
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  }
55671a58a6SGuy Brand
56671a58a6SGuy Brand  // Namespace autolinking from URL
57671a58a6SGuy Brand  if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
58103c256aSChris Smith    if(page_exists($id.$conf['start'])){
59671a58a6SGuy Brand      // start page inside namespace
60671a58a6SGuy Brand      $id = $id.$conf['start'];
61103c256aSChris Smith    }elseif(page_exists($id.noNS(cleanID($id)))){
62671a58a6SGuy Brand      // page named like the NS inside the NS
63671a58a6SGuy Brand      $id = $id.noNS(cleanID($id));
64103c256aSChris Smith    }elseif(page_exists($id)){
65671a58a6SGuy Brand      // page like namespace exists
667a42ac9eSBen Coburn      $id = substr($id,0,-1);
67671a58a6SGuy Brand    }else{
68671a58a6SGuy Brand      // fall back to default
69671a58a6SGuy Brand      $id = $id.$conf['start'];
70671a58a6SGuy Brand    }
71671a58a6SGuy Brand    header("Location: ".wl($id,'',true));
72671a58a6SGuy Brand  }
73671a58a6SGuy Brand
7442905504SAndreas Gohr  if($clean) $id = cleanID($id);
750868021bSAndreas Gohr  if(empty($id) && $param=='id') $id = $conf['start'];
766c7843b5Sandi
776c7843b5Sandi  return $id;
786c7843b5Sandi}
79b625487dSandi
80b625487dSandi/**
81b625487dSandi * Remove unwanted chars from ID
82b625487dSandi *
83b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
84b625487dSandi * converted to unaccented ones
85b625487dSandi *
86b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
876e0cc83aSchris * @param  string  $raw_id    The pageid to clean
888a831f2bSAndreas Gohr * @param  boolean $ascii     Force ASCII
8963b0c1a7SGina Haeussge * @param  boolean $media     Allow leading or trailing _ for media files
90b625487dSandi */
9163b0c1a7SGina Haeussgefunction cleanID($raw_id,$ascii=false,$media=false){
92b625487dSandi  global $conf;
93b625487dSandi  global $lang;
944b5db43bSjoe.lapp  static $sepcharpat = null;
954b5db43bSjoe.lapp
96dc2c0e04Schris  global $cache_cleanid;
97dc2c0e04Schris  $cache = & $cache_cleanid;
986e0cc83aSchris
996e0cc83aSchris  // check if it's already in the memory cache
1003a50618cSgweissbach  if (isset($cache[(string)$raw_id])) {
1013a50618cSgweissbach    return $cache[(string)$raw_id];
1026e0cc83aSchris    }
1036e0cc83aSchris
1044b5db43bSjoe.lapp  $sepchar = $conf['sepchar'];
1054b5db43bSjoe.lapp  if($sepcharpat == null) // build string only once to save clock cycles
1064b5db43bSjoe.lapp    $sepcharpat = '#\\'.$sepchar.'+#';
1074b5db43bSjoe.lapp
1083a50618cSgweissbach  $id = trim((string)$raw_id);
109b625487dSandi  $id = utf8_strtolower($id);
110b625487dSandi
111b625487dSandi  //alternative namespace seperator
112b625487dSandi  $id = strtr($id,';',':');
113b625487dSandi  if($conf['useslash']){
114b625487dSandi    $id = strtr($id,'/',':');
115b625487dSandi  }else{
1164eeffcd2SAndreas Gohr    $id = strtr($id,'/',$sepchar);
117b625487dSandi  }
118b625487dSandi
1198a831f2bSAndreas Gohr  if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1208a831f2bSAndreas Gohr  if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
121b625487dSandi
122b625487dSandi  //remove specials
123ad81d431SAndreas Gohr  $id = utf8_stripspecials($id,$sepchar,'\*');
124b625487dSandi
1258a831f2bSAndreas Gohr  if($ascii) $id = utf8_strip($id);
1268a831f2bSAndreas Gohr
127b625487dSandi  //clean up
1284b5db43bSjoe.lapp  $id = preg_replace($sepcharpat,$sepchar,$id);
129b625487dSandi  $id = preg_replace('#:+#',':',$id);
13063b0c1a7SGina Haeussge  $id = ($media ? trim($id,':.-') : trim($id,':._-'));
131b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
132b625487dSandi
1333a50618cSgweissbach  $cache[(string)$raw_id] = $id;
134b625487dSandi  return($id);
135b625487dSandi}
136b625487dSandi
137b625487dSandi/**
138b625487dSandi * Return namespacepart of a wiki ID
139b625487dSandi *
140b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
141b625487dSandi */
142b625487dSandifunction getNS($id){
1433a50618cSgweissbach  $pos = strrpos((string)$id,':');
144c4e0e4a1SAndreas Gohr  if($pos!==false){
1453a50618cSgweissbach    return substr((string)$id,0,$pos);
146b625487dSandi  }
147b625487dSandi  return false;
148b625487dSandi}
149b625487dSandi
150b625487dSandi/**
151b625487dSandi * Returns the ID without the namespace
152b625487dSandi *
153b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
154b625487dSandi */
155b625487dSandifunction noNS($id) {
1562844584fSBen Coburn  $pos = strrpos($id, ':');
1572844584fSBen Coburn  if ($pos!==false) {
1582844584fSBen Coburn    return substr($id, $pos+1);
1592844584fSBen Coburn  } else {
1602844584fSBen Coburn    return $id;
1612844584fSBen Coburn  }
1621a84a0f3SAnika Henke}
1631a84a0f3SAnika Henke
1641a84a0f3SAnika Henke/**
1651a84a0f3SAnika Henke * Returns the current namespace
1661a84a0f3SAnika Henke *
1671a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1681a84a0f3SAnika Henke */
1691a84a0f3SAnika Henkefunction curNS($id) {
1701a84a0f3SAnika Henke    return noNS(getNS($id));
1711a84a0f3SAnika Henke}
1721a84a0f3SAnika Henke
1731a84a0f3SAnika Henke/**
1741a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages
1751a84a0f3SAnika Henke *
1761a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1771a84a0f3SAnika Henke */
1781a84a0f3SAnika Henkefunction noNSorNS($id) {
1791a84a0f3SAnika Henke    global $conf;
1801a84a0f3SAnika Henke
1811a84a0f3SAnika Henke    $p = noNS($id);
1821a84a0f3SAnika Henke    if ($p == $conf['start']) {
1831a84a0f3SAnika Henke        $p = curNS($id);
1841a84a0f3SAnika Henke        if ($p == false) {
1851a84a0f3SAnika Henke            return noNS($id);
1861a84a0f3SAnika Henke        }
1871a84a0f3SAnika Henke    }
1881a84a0f3SAnika Henke    return $p;
189b625487dSandi}
190*4ceab83fSAndreas Gohr
191*4ceab83fSAndreas Gohr/**
192*4ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title
193*4ceab83fSAndreas Gohr *
194*4ceab83fSAndreas Gohr * @param string  $title   The headline title
195*4ceab83fSAndreas Gohr * @param array   $check   List of existing IDs
196*4ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
197*4ceab83fSAndreas Gohr */
198*4ceab83fSAndreas Gohrfunction sectionID($title,&$check=null) {
199*4ceab83fSAndreas Gohr    $title = str_replace(':','',cleanID($title));
200*4ceab83fSAndreas Gohr    $new = ltrim($title,'0123456789._-');
201*4ceab83fSAndreas Gohr    if(empty($new)){
202*4ceab83fSAndreas Gohr        $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
203*4ceab83fSAndreas Gohr    }else{
204*4ceab83fSAndreas Gohr        $title = $new;
205*4ceab83fSAndreas Gohr    }
206*4ceab83fSAndreas Gohr
207*4ceab83fSAndreas Gohr    if(!is_null($check) && is_array($check)){
208*4ceab83fSAndreas Gohr        // make sure tiles are unique
209*4ceab83fSAndreas Gohr        $num = '';
210*4ceab83fSAndreas Gohr        while(in_array($title.$num,$check)){
211*4ceab83fSAndreas Gohr            ($num) ? $num++ : $num = 1;
212*4ceab83fSAndreas Gohr        }
213*4ceab83fSAndreas Gohr        $title = $title.$num;
214*4ceab83fSAndreas Gohr        $check[] = $title;
215*4ceab83fSAndreas Gohr    }
216*4ceab83fSAndreas Gohr
217*4ceab83fSAndreas Gohr    return $title;
218*4ceab83fSAndreas Gohr}
219*4ceab83fSAndreas Gohr
220b625487dSandi
221b625487dSandi/**
222103c256aSChris Smith *  Wiki page existence check
223103c256aSChris Smith *
224103c256aSChris Smith *  parameters as for wikiFN
225103c256aSChris Smith *
226103c256aSChris Smith *  @author Chris Smith <chris@jalakai.co.uk>
227103c256aSChris Smith */
228103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
229103c256aSChris Smith  return @file_exists(wikiFN($id,$rev,$clean));
230103c256aSChris Smith}
231103c256aSChris Smith
232103c256aSChris Smith/**
233103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
234b625487dSandi *
235b625487dSandi * The filename is URL encoded to protect Unicode chars
236b625487dSandi *
237103c256aSChris Smith * @param  $raw_id  string   id of wikipage
238103c256aSChris Smith * @param  $rev     string   page revision, empty string for current
239103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
240103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
241103c256aSChris Smith *
242b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
243b625487dSandi */
2446e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
245b625487dSandi  global $conf;
2466e0cc83aSchris
247dc2c0e04Schris  global $cache_wikifn;
248dc2c0e04Schris  $cache = & $cache_wikifn;
249dc2c0e04Schris
2506e0cc83aSchris  if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2516e0cc83aSchris    return $cache[$raw_id][$rev];
2526e0cc83aSchris  }
2536e0cc83aSchris
2546e0cc83aSchris  $id = $raw_id;
2556e0cc83aSchris
2560d8ea614Schris  if ($clean) $id = cleanID($id);
257b625487dSandi  $id = str_replace(':','/',$id);
258b625487dSandi  if(empty($rev)){
259b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
260b625487dSandi  }else{
261b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
262ff3ed99fSmarcel    if($conf['compression']){
263ff3ed99fSmarcel      //test for extensions here, we want to read both compressions
264d8186216SBen Coburn       if (@file_exists($fn . '.gz')){
265b625487dSandi          $fn .= '.gz';
266d8186216SBen Coburn       }else if(@file_exists($fn . '.bz2')){
267ff3ed99fSmarcel          $fn .= '.bz2';
268ff3ed99fSmarcel       }else{
269ff3ed99fSmarcel          //file doesnt exist yet, so we take the configured extension
270ff3ed99fSmarcel          $fn .= '.' . $conf['compression'];
271ff3ed99fSmarcel       }
272b625487dSandi    }
273b625487dSandi  }
2746e0cc83aSchris
27550602150SBen Coburn  if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2766e0cc83aSchris  $cache[$raw_id][$rev] = $fn;
277b625487dSandi  return $fn;
278b625487dSandi}
279b625487dSandi
280b625487dSandi/**
281c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
282c9b4bd1eSBen Coburn *
283c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
284c9b4bd1eSBen Coburn */
285c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
286c9b4bd1eSBen Coburn  global $conf;
287662ff478SAndreas Gohr  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
288c9b4bd1eSBen Coburn}
289c9b4bd1eSBen Coburn
290c9b4bd1eSBen Coburn
291c9b4bd1eSBen Coburn/**
2921380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
293b158d625SSteven Danz *
294b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
295b158d625SSteven Danz *
296b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
297b158d625SSteven Danz */
2981380fc45SAndreas Gohrfunction metaFN($id,$ext){
299b158d625SSteven Danz  global $conf;
300b158d625SSteven Danz  $id = cleanID($id);
301b158d625SSteven Danz  $id = str_replace(':','/',$id);
3021380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
303b158d625SSteven Danz  return $fn;
304b158d625SSteven Danz}
305b158d625SSteven Danz
306b158d625SSteven Danz/**
307e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
308e1f3d9e1SEsther Brunner *
309e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
310e1f3d9e1SEsther Brunner */
311e1f3d9e1SEsther Brunnerfunction metaFiles($id){
312e1f3d9e1SEsther Brunner   $name   = noNS($id);
3136a5b38cdSMichael Klier   $ns     = getNS($id);
3146a5b38cdSMichael Klier   $dir    = ($ns) ? metaFN($ns,'').'/' : metaFN($ns,'');
315e1f3d9e1SEsther Brunner   $files  = array();
316e1f3d9e1SEsther Brunner
317e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
3185011da9dSEsther Brunner   if(!$dh) return $files;
319e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
3201a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
321e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
322e1f3d9e1SEsther Brunner   }
323e1f3d9e1SEsther Brunner   closedir($dh);
324e1f3d9e1SEsther Brunner
325e1f3d9e1SEsther Brunner   return $files;
326e1f3d9e1SEsther Brunner}
327e1f3d9e1SEsther Brunner
328e1f3d9e1SEsther Brunner/**
329b625487dSandi * returns the full path to the mediafile specified by ID
330b625487dSandi *
331b625487dSandi * The filename is URL encoded to protect Unicode chars
332b625487dSandi *
333b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
334b625487dSandi */
335b625487dSandifunction mediaFN($id){
336b625487dSandi  global $conf;
337b625487dSandi  $id = cleanID($id);
338b625487dSandi  $id = str_replace(':','/',$id);
339b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
340b625487dSandi  return $fn;
341b625487dSandi}
342b625487dSandi
343b625487dSandi/**
344b625487dSandi * Returns the full filepath to a localized textfile if local
345b625487dSandi * version isn't found the english one is returned
346b625487dSandi *
347b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
348b625487dSandi */
349b625487dSandifunction localeFN($id){
350b625487dSandi  global $conf;
351bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
352b625487dSandi  if(!@file_exists($file)){
353b625487dSandi    //fall back to english
354bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
355b625487dSandi  }
356b625487dSandi  return $file;
357b625487dSandi}
358b625487dSandi
359b625487dSandi/**
360c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
361c4e0e4a1SAndreas Gohr *
362c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
363c4e0e4a1SAndreas Gohr * instead
364c4e0e4a1SAndreas Gohr *
365c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
366c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
367c4e0e4a1SAndreas Gohr *
368c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
369c4e0e4a1SAndreas Gohr */
370a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
371c662a49aSAndreas Gohr  global $conf;
372c662a49aSAndreas Gohr
373c662a49aSAndreas Gohr  // some pre cleaning for useslash:
374c662a49aSAndreas Gohr  if($conf['useslash']) $id = str_replace('/',':',$id);
375c662a49aSAndreas Gohr
376c4e0e4a1SAndreas Gohr  // if the id starts with a dot we need to handle the
377c4e0e4a1SAndreas Gohr  // relative stuff
378c4e0e4a1SAndreas Gohr  if($id{0} == '.'){
379c4e0e4a1SAndreas Gohr    // normalize initial dots without a colon
380c4e0e4a1SAndreas Gohr    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
381c4e0e4a1SAndreas Gohr    // prepend the current namespace
382c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
383c4e0e4a1SAndreas Gohr
384c4e0e4a1SAndreas Gohr    // cleanup relatives
385c4e0e4a1SAndreas Gohr    $result = array();
386c4e0e4a1SAndreas Gohr    $pathA  = explode(':', $id);
387c4e0e4a1SAndreas Gohr    if (!$pathA[0]) $result[] = '';
388c4e0e4a1SAndreas Gohr    foreach ($pathA AS $key => $dir) {
389c4e0e4a1SAndreas Gohr      if ($dir == '..') {
390c4e0e4a1SAndreas Gohr        if (end($result) == '..') {
391c4e0e4a1SAndreas Gohr          $result[] = '..';
392c4e0e4a1SAndreas Gohr        } elseif (!array_pop($result)) {
393c4e0e4a1SAndreas Gohr          $result[] = '..';
394c4e0e4a1SAndreas Gohr        }
395c4e0e4a1SAndreas Gohr      } elseif ($dir && $dir != '.') {
396c4e0e4a1SAndreas Gohr        $result[] = $dir;
397c4e0e4a1SAndreas Gohr      }
398c4e0e4a1SAndreas Gohr    }
399c4e0e4a1SAndreas Gohr    if (!end($pathA)) $result[] = '';
400c4e0e4a1SAndreas Gohr    $id = implode(':', $result);
401c4e0e4a1SAndreas Gohr  }elseif($ns !== false && strpos($id,':') === false){
402c4e0e4a1SAndreas Gohr    //if link contains no namespace. add current namespace (if any)
403c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
404c4e0e4a1SAndreas Gohr  }
405c4e0e4a1SAndreas Gohr
406a6ef4796SAndreas Gohr  if($clean) $id = cleanID($id);
407a6ef4796SAndreas Gohr  return $id;
408c4e0e4a1SAndreas Gohr}
409c4e0e4a1SAndreas Gohr
410c4e0e4a1SAndreas Gohr/**
411b625487dSandi * Returns a full media id
412b625487dSandi *
413b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
414b625487dSandi */
41537e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
416c4e0e4a1SAndreas Gohr  $page   = resolve_id($ns,$page);
417b625487dSandi  $file   = mediaFN($page);
418b625487dSandi  $exists = @file_exists($file);
419b625487dSandi}
420b625487dSandi
421b625487dSandi/**
422b625487dSandi * Returns a full page id
423b625487dSandi *
424b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
425b625487dSandi */
42637e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
427b625487dSandi  global $conf;
4280b7c14c2Sandi  $exists = false;
429b625487dSandi
430b625487dSandi  //keep hashlink if exists then clean both parts
43103c4aec3Schris  if (strpos($page,'#')) {
432b625487dSandi    list($page,$hash) = split('#',$page,2);
43303c4aec3Schris  } else {
43403c4aec3Schris    $hash = '';
43503c4aec3Schris  }
436b625487dSandi  $hash = cleanID($hash);
437a6ef4796SAndreas Gohr  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
438b625487dSandi
439a6ef4796SAndreas Gohr  // get filename (calls clean itself)
440b625487dSandi  $file = wikiFN($page);
441b625487dSandi
4421179df0eSGuy Brand  // if ends with colon or slash we have a namespace link
4431179df0eSGuy Brand  if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){
444103c256aSChris Smith    if(page_exists($page.$conf['start'])){
445a6ef4796SAndreas Gohr      // start page inside namespace
446a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
447a6ef4796SAndreas Gohr      $exists = true;
448103c256aSChris Smith    }elseif(page_exists($page.noNS(cleanID($page)))){
449a6ef4796SAndreas Gohr      // page named like the NS inside the NS
450a6ef4796SAndreas Gohr      $page = $page.noNS(cleanID($page));
451a6ef4796SAndreas Gohr      $exists = true;
452103c256aSChris Smith    }elseif(page_exists($page)){
453a6ef4796SAndreas Gohr      // page like namespace exists
454a6ef4796SAndreas Gohr      $page = $page;
455a6ef4796SAndreas Gohr      $exists = true;
456a6ef4796SAndreas Gohr    }else{
457a6ef4796SAndreas Gohr      // fall back to default
458a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
459a6ef4796SAndreas Gohr    }
460a6ef4796SAndreas Gohr  }else{
461b625487dSandi    //check alternative plural/nonplural form
462b625487dSandi    if(!@file_exists($file)){
463b625487dSandi      if( $conf['autoplural'] ){
464b625487dSandi        if(substr($page,-1) == 's'){
465b625487dSandi          $try = substr($page,0,-1);
466b625487dSandi        }else{
467b625487dSandi          $try = $page.'s';
468b625487dSandi        }
469103c256aSChris Smith        if(page_exists($try)){
470b625487dSandi          $page   = $try;
471b625487dSandi          $exists = true;
472b625487dSandi        }
473b625487dSandi      }
474b625487dSandi    }else{
475b625487dSandi      $exists = true;
476b625487dSandi    }
477a6ef4796SAndreas Gohr  }
478a6ef4796SAndreas Gohr
479a6ef4796SAndreas Gohr  // now make sure we have a clean page
480a6ef4796SAndreas Gohr  $page = cleanID($page);
481b625487dSandi
482b625487dSandi  //add hash if any
483b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
484b625487dSandi}
485b625487dSandi
48698407a7aSandi/**
48798407a7aSandi * Returns the name of a cachefile from given data
48898407a7aSandi *
48998407a7aSandi * The needed directory is created by this function!
49098407a7aSandi *
49198407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
49298407a7aSandi *
49398407a7aSandi * @param string $data  This data is used to create a unique md5 name
49498407a7aSandi * @param string $ext   This is appended to the filename if given
49598407a7aSandi * @return string       The filename of the cachefile
49698407a7aSandi */
49798407a7aSandifunction getCacheName($data,$ext=''){
49898407a7aSandi  global $conf;
49998407a7aSandi  $md5  = md5($data);
50098407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
50198407a7aSandi  io_makeFileDir($file);
50298407a7aSandi  return $file;
50398407a7aSandi}
50498407a7aSandi
5050dc92c6fSAndreas Gohr/**
5060dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
5070dc92c6fSAndreas Gohr *
5080dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5090dc92c6fSAndreas Gohr */
5100dc92c6fSAndreas Gohrfunction isHiddenPage($id){
5110dc92c6fSAndreas Gohr  global $conf;
512e6a873d7SMichael Klier  global $ACT;
5130dc92c6fSAndreas Gohr  if(empty($conf['hidepages'])) return false;
514e6a873d7SMichael Klier  if($ACT == 'admin') return false;
5150dc92c6fSAndreas Gohr
5160dc92c6fSAndreas Gohr  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
5170dc92c6fSAndreas Gohr    return true;
5180dc92c6fSAndreas Gohr  }
5190dc92c6fSAndreas Gohr  return false;
5200dc92c6fSAndreas Gohr}
5210dc92c6fSAndreas Gohr
5220dc92c6fSAndreas Gohr/**
5230dc92c6fSAndreas Gohr * Reverse of isHiddenPage
5240dc92c6fSAndreas Gohr *
5250dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5260dc92c6fSAndreas Gohr */
5270dc92c6fSAndreas Gohrfunction isVisiblePage($id){
5280dc92c6fSAndreas Gohr  return !isHiddenPage($id);
5290dc92c6fSAndreas Gohr}
5300dc92c6fSAndreas Gohr
531254e5c84SBen Coburn/**
532254e5c84SBen Coburn * Checks and sets HTTP headers for conditional HTTP requests
533254e5c84SBen Coburn *
534254e5c84SBen Coburn * @author   Simon Willison <swillison@gmail.com>
535254e5c84SBen Coburn * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
5360ac9a84dSoliver * @param    timestamp $timestamp lastmodified time of the cache file
5370ac9a84dSoliver * @returns  void or void with previously header() commands executed
538254e5c84SBen Coburn */
539254e5c84SBen Coburnfunction http_conditionalRequest($timestamp){
540254e5c84SBen Coburn  // A PHP implementation of conditional get, see
541254e5c84SBen Coburn  //   http://fishbowl.pastiche.org/archives/001132.html
5426d88439aSAndreas Gohr  $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT';
543254e5c84SBen Coburn  $etag = '"'.md5($last_modified).'"';
544254e5c84SBen Coburn  // Send the headers
545254e5c84SBen Coburn  header("Last-Modified: $last_modified");
546254e5c84SBen Coburn  header("ETag: $etag");
547254e5c84SBen Coburn  // See if the client has provided the required headers
5480ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
5490ac9a84dSoliver    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
5500ac9a84dSoliver  }else{
5510ac9a84dSoliver    $if_modified_since = false;
5520ac9a84dSoliver  }
5530ac9a84dSoliver
5540ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
5550ac9a84dSoliver    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
5560ac9a84dSoliver  }else{
5570ac9a84dSoliver    $if_none_match = false;
5580ac9a84dSoliver  }
5590ac9a84dSoliver
560254e5c84SBen Coburn  if (!$if_modified_since && !$if_none_match){
561254e5c84SBen Coburn    return;
562254e5c84SBen Coburn  }
5630ac9a84dSoliver
564254e5c84SBen Coburn  // At least one of the headers is there - check them
565254e5c84SBen Coburn  if ($if_none_match && $if_none_match != $etag) {
566254e5c84SBen Coburn    return; // etag is there but doesn't match
567254e5c84SBen Coburn  }
5680ac9a84dSoliver
569254e5c84SBen Coburn  if ($if_modified_since && $if_modified_since != $last_modified) {
570254e5c84SBen Coburn    return; // if-modified-since is there but doesn't match
571254e5c84SBen Coburn  }
5720ac9a84dSoliver
573254e5c84SBen Coburn  // Nothing has changed since their last request - serve a 304 and exit
574254e5c84SBen Coburn  header('HTTP/1.0 304 Not Modified');
575fc39f9b4SJens Wilke
576fc39f9b4SJens Wilke  // don't produce output, even if compression is on
577fc39f9b4SJens Wilke  ob_end_clean();
578254e5c84SBen Coburn  exit;
579254e5c84SBen Coburn}
580254e5c84SBen Coburn
581b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
582