xref: /dokuwiki/inc/common.php (revision ae56bfb6216da28c214a2237e3ba7e9a5f1da37d)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * Common DokuWiki functions
415fae107Sandi *
515fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
715fae107Sandi */
815fae107Sandi
9ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10e7cb32dcSAndreas Gohr  require_once(DOKU_CONF.'dokuwiki.php');
11ed7b5f09Sandi  require_once(DOKU_INC.'inc/io.php');
12ed7b5f09Sandi  require_once(DOKU_INC.'inc/utf8.php');
13ed7b5f09Sandi  require_once(DOKU_INC.'inc/mail.php');
14c112d578Sandi  require_once(DOKU_INC.'inc/parserutils.php');
15f3f0262cSandi
16f3f0262cSandi/**
1715fae107Sandi * Return info about the current document as associative
18f3f0262cSandi * array.
1915fae107Sandi *
2015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
21f3f0262cSandi */
22f3f0262cSandifunction pageinfo(){
23f3f0262cSandi  global $ID;
24f3f0262cSandi  global $REV;
25f3f0262cSandi  global $USERINFO;
26f3f0262cSandi  global $conf;
27f3f0262cSandi
28f3f0262cSandi  if($_SERVER['REMOTE_USER']){
29f3f0262cSandi    $info['userinfo']   = $USERINFO;
30f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
311380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
32f3f0262cSandi  }else{
33f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
341380fc45SAndreas Gohr    $info['subscribed'] = false;
35f3f0262cSandi  }
36f3f0262cSandi
37f3f0262cSandi  $info['namespace'] = getNS($ID);
38f3f0262cSandi  $info['locked']    = checklock($ID);
39f3f0262cSandi  $info['filepath']  = realpath(wikiFN($ID,$REV));
40f3f0262cSandi  $info['exists']    = @file_exists($info['filepath']);
41f3f0262cSandi  if($REV && !$info['exists']){
42f3f0262cSandi    //check if current revision was meant
43f3f0262cSandi    $cur = wikiFN($ID);
44f3f0262cSandi    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
45f3f0262cSandi      $info['filepath'] = realpath($cur);
46f3f0262cSandi      $info['exists']   = true;
47f3f0262cSandi      $REV = '';
48f3f0262cSandi    }
49f3f0262cSandi  }
50c112d578Sandi  $info['rev'] = $REV;
51f3f0262cSandi  if($info['exists']){
52f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
53f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
54f3f0262cSandi  }else{
55f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
56f3f0262cSandi  }
57f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
58f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
59f3f0262cSandi
60652610a2Sandi  //who's the editor
61652610a2Sandi  if($REV){
62652610a2Sandi    $revinfo = getRevisionInfo($ID,$REV);
63652610a2Sandi  }else{
64652610a2Sandi    $revinfo = getRevisionInfo($ID,$info['lastmod']);
65652610a2Sandi  }
66652610a2Sandi  $info['ip']     = $revinfo['ip'];
67652610a2Sandi  $info['user']   = $revinfo['user'];
68652610a2Sandi  $info['sum']    = $revinfo['sum'];
6959f257aeSchris
7088f522e9Sandi  if($revinfo['user']){
7188f522e9Sandi    $info['editor'] = $revinfo['user'];
7288f522e9Sandi  }else{
7388f522e9Sandi    $info['editor'] = $revinfo['ip'];
7488f522e9Sandi  }
75652610a2Sandi
76f3f0262cSandi  return $info;
77f3f0262cSandi}
78f3f0262cSandi
79f3f0262cSandi/**
802684e50aSAndreas Gohr * Build an string of URL parameters
812684e50aSAndreas Gohr *
822684e50aSAndreas Gohr * @author Andreas Gohr
832684e50aSAndreas Gohr */
842684e50aSAndreas Gohrfunction buildURLparams($params){
852684e50aSAndreas Gohr  $url = '';
862684e50aSAndreas Gohr  $amp = false;
872684e50aSAndreas Gohr  foreach($params as $key => $val){
882684e50aSAndreas Gohr    if($amp) $url .= '&amp;';
892684e50aSAndreas Gohr
902684e50aSAndreas Gohr    $url .= $key.'=';
912684e50aSAndreas Gohr    $url .= urlencode($val);
922684e50aSAndreas Gohr    $amp = true;
932684e50aSAndreas Gohr  }
942684e50aSAndreas Gohr  return $url;
952684e50aSAndreas Gohr}
962684e50aSAndreas Gohr
972684e50aSAndreas Gohr/**
982684e50aSAndreas Gohr * Build an string of html tag attributes
992684e50aSAndreas Gohr *
1002684e50aSAndreas Gohr * @author Andreas Gohr
1012684e50aSAndreas Gohr */
1022684e50aSAndreas Gohrfunction buildAttributes($params){
1032684e50aSAndreas Gohr  $url = '';
1042684e50aSAndreas Gohr  foreach($params as $key => $val){
1052684e50aSAndreas Gohr    $url .= $key.'="';
1062684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1072684e50aSAndreas Gohr    $url .= '" ';
1082684e50aSAndreas Gohr  }
1092684e50aSAndreas Gohr  return $url;
1102684e50aSAndreas Gohr}
1112684e50aSAndreas Gohr
1122684e50aSAndreas Gohr
1132684e50aSAndreas Gohr/**
1140396becbSandi * print a message
1150396becbSandi *
1160396becbSandi * If HTTP headers were not sent yet the message is added
1170396becbSandi * to the global message array else it's printed directly
1180396becbSandi * using html_msgarea()
1190396becbSandi *
120f3f0262cSandi *
121f3f0262cSandi * Levels can be:
122f3f0262cSandi *
123f3f0262cSandi * -1 error
124f3f0262cSandi *  0 info
125f3f0262cSandi *  1 success
12615fae107Sandi *
12715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1280396becbSandi * @see    html_msgarea
129f3f0262cSandi */
130f3f0262cSandifunction msg($message,$lvl=0){
131f3f0262cSandi  global $MSG;
132f3f0262cSandi  $errors[-1] = 'error';
133f3f0262cSandi  $errors[0]  = 'info';
134f3f0262cSandi  $errors[1]  = 'success';
135f3f0262cSandi
136cc20ad51Sandi  if(!headers_sent()){
137f3f0262cSandi    if(!isset($MSG)) $MSG = array();
138f3f0262cSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
1390396becbSandi  }else{
1400396becbSandi    $MSG = array();
1410396becbSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
142f62ea8a1Sandi    if(function_exists('html_msgarea')){
1430396becbSandi      html_msgarea();
144f62ea8a1Sandi    }else{
145f62ea8a1Sandi      print "ERROR($lvl) $message";
146f62ea8a1Sandi    }
1470396becbSandi  }
148f3f0262cSandi}
149f3f0262cSandi
150f3f0262cSandi/**
15115fae107Sandi * This builds the breadcrumb trail and returns it as array
15215fae107Sandi *
15315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
154f3f0262cSandi */
155f3f0262cSandifunction breadcrumbs(){
1568746e727Sandi  // we prepare the breadcrumbs early for quick session closing
1578746e727Sandi  static $crumbs = null;
1588746e727Sandi  if($crumbs != null) return $crumbs;
1598746e727Sandi
160f3f0262cSandi  global $ID;
161f3f0262cSandi  global $ACT;
162f3f0262cSandi  global $conf;
163f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
164f3f0262cSandi
165f3f0262cSandi  //first visit?
166f3f0262cSandi  if (!is_array($crumbs)){
167f3f0262cSandi    $crumbs = array();
168f3f0262cSandi  }
169f3f0262cSandi  //we only save on show and existing wiki documents
170a77f5846Sjan  $file = wikiFN($ID);
171a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
172f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
173f3f0262cSandi    return $crumbs;
174f3f0262cSandi  }
175a77f5846Sjan
176a77f5846Sjan  // page names
177a77f5846Sjan  $name = noNS($ID);
178a77f5846Sjan  if ($conf['useheading']) {
179a77f5846Sjan    // get page title
180bb0a59d4Sjan    $title = p_get_first_heading($ID);
181a77f5846Sjan    if ($title) {
182a77f5846Sjan      $name = $title;
183a77f5846Sjan    }
184a77f5846Sjan  }
185a77f5846Sjan
186f3f0262cSandi  //remove ID from array
187a77f5846Sjan  if (isset($crumbs[$ID])) {
188a77f5846Sjan    unset($crumbs[$ID]);
189f3f0262cSandi  }
190f3f0262cSandi
191f3f0262cSandi  //add to array
192a77f5846Sjan  $crumbs[$ID] = $name;
193f3f0262cSandi  //reduce size
194f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
195f3f0262cSandi    array_shift($crumbs);
196f3f0262cSandi  }
197f3f0262cSandi  //save to session
198f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
199f3f0262cSandi  return $crumbs;
200f3f0262cSandi}
201f3f0262cSandi
202f3f0262cSandi/**
20315fae107Sandi * Filter for page IDs
20415fae107Sandi *
205f3f0262cSandi * This is run on a ID before it is outputted somewhere
206f3f0262cSandi * currently used to replace the colon with something else
207f3f0262cSandi * on Windows systems and to have proper URL encoding
20815fae107Sandi *
20949c713a3Sandi * Urlencoding is ommitted when the second parameter is false
21049c713a3Sandi *
21115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
212f3f0262cSandi */
21349c713a3Sandifunction idfilter($id,$ue=true){
214f3f0262cSandi  global $conf;
215f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
216f3f0262cSandi    $id = strtr($id,':','/');
217f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
218f3f0262cSandi      $conf['userewrite']) {
219f3f0262cSandi    $id = strtr($id,':',';');
220f3f0262cSandi  }
22149c713a3Sandi  if($ue){
222f3f0262cSandi    $id = urlencode($id);
223f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
224f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
22549c713a3Sandi  }
226f3f0262cSandi  return $id;
227f3f0262cSandi}
228f3f0262cSandi
229f3f0262cSandi/**
230ed7b5f09Sandi * This builds a link to a wikipage
23115fae107Sandi *
2326c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2336c7843b5Sandi * given in $more
2346c7843b5Sandi *
23515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
236f3f0262cSandi */
237ed7b5f09Sandifunction wl($id='',$more='',$abs=false){
238f3f0262cSandi  global $conf;
2396de3759aSAndreas Gohr  if(is_array($more)){
2406de3759aSAndreas Gohr    $more = buildURLparams($more);
2416de3759aSAndreas Gohr  }else{
242f3f0262cSandi    $more = str_replace(',','&amp;',$more);
2436de3759aSAndreas Gohr  }
244f3f0262cSandi
245f3f0262cSandi  $id    = idfilter($id);
246ed7b5f09Sandi  if($abs){
247ed7b5f09Sandi    $xlink = DOKU_URL;
248ed7b5f09Sandi  }else{
249ed7b5f09Sandi    $xlink = DOKU_BASE;
250ed7b5f09Sandi  }
251f3f0262cSandi
2526c7843b5Sandi  if($conf['userewrite'] == 2){
2536c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
2546c7843b5Sandi    if($more) $xlink .= '?'.$more;
2556c7843b5Sandi  }elseif($conf['userewrite']){
256f3f0262cSandi    $xlink .= $id;
257f3f0262cSandi    if($more) $xlink .= '?'.$more;
2586c7843b5Sandi  }else{
2596c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
2606c7843b5Sandi    if($more) $xlink .= '&amp;'.$more;
261f3f0262cSandi  }
262f3f0262cSandi
263f3f0262cSandi  return $xlink;
264f3f0262cSandi}
265f3f0262cSandi
266f3f0262cSandi/**
2676de3759aSAndreas Gohr * Build a link to a media file
2686de3759aSAndreas Gohr *
2696de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
2706de3759aSAndreas Gohr */
2716de3759aSAndreas Gohrfunction ml($id='',$more='',$direct=true){
2726de3759aSAndreas Gohr  global $conf;
2736de3759aSAndreas Gohr  if(is_array($more)){
2746de3759aSAndreas Gohr    $more = buildURLparams($more);
2756de3759aSAndreas Gohr  }else{
2766de3759aSAndreas Gohr    $more = str_replace(',','&amp;',$more);
2776de3759aSAndreas Gohr  }
2786de3759aSAndreas Gohr
2796de3759aSAndreas Gohr  $xlink = DOKU_BASE;
2806de3759aSAndreas Gohr
2816de3759aSAndreas Gohr  // external URLs are always direct without rewriting
2826de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
2836de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
2846de3759aSAndreas Gohr    if($more){
2856de3759aSAndreas Gohr      $xlink .= '?'.$more;
28648665d38SAndreas Gohr      $xlink .= '&amp;media='.urlencode($id);
2876de3759aSAndreas Gohr    }else{
28848665d38SAndreas Gohr      $xlink .= '?media='.urlencode($id);
2896de3759aSAndreas Gohr    }
2906de3759aSAndreas Gohr    return $xlink;
2916de3759aSAndreas Gohr  }
2926de3759aSAndreas Gohr
2936de3759aSAndreas Gohr  $id = idfilter($id);
2946de3759aSAndreas Gohr
2956de3759aSAndreas Gohr  // decide on scriptname
2966de3759aSAndreas Gohr  if($direct){
2976de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
2986de3759aSAndreas Gohr      $script = '_media';
2996de3759aSAndreas Gohr    }else{
3006de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3016de3759aSAndreas Gohr    }
3026de3759aSAndreas Gohr  }else{
3036de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3046de3759aSAndreas Gohr      $script = '_detail';
3056de3759aSAndreas Gohr    }else{
3066de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3076de3759aSAndreas Gohr    }
3086de3759aSAndreas Gohr  }
3096de3759aSAndreas Gohr
3106de3759aSAndreas Gohr  // build URL based on rewrite mode
3116de3759aSAndreas Gohr   if($conf['userewrite']){
3126de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3136de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3146de3759aSAndreas Gohr   }else{
3156de3759aSAndreas Gohr     if($more){
316a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
3176de3759aSAndreas Gohr       $xlink .= '&amp;media='.$id;
3186de3759aSAndreas Gohr     }else{
319a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
3206de3759aSAndreas Gohr     }
3216de3759aSAndreas Gohr   }
3226de3759aSAndreas Gohr
3236de3759aSAndreas Gohr  return $xlink;
3246de3759aSAndreas Gohr}
3256de3759aSAndreas Gohr
3266de3759aSAndreas Gohr
3276de3759aSAndreas Gohr
3286de3759aSAndreas Gohr/**
329f3f0262cSandi * Just builds a link to a script
33015fae107Sandi *
331ed7b5f09Sandi * @todo   maybe obsolete
33215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
333f3f0262cSandi */
334f3f0262cSandifunction script($script='doku.php'){
335ed7b5f09Sandi#  $link = getBaseURL();
336ed7b5f09Sandi#  $link .= $script;
337ed7b5f09Sandi#  return $link;
338ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
339f3f0262cSandi}
340f3f0262cSandi
341f3f0262cSandi/**
34215fae107Sandi * Spamcheck against wordlist
34315fae107Sandi *
344f3f0262cSandi * Checks the wikitext against a list of blocked expressions
345f3f0262cSandi * returns true if the text contains any bad words
34615fae107Sandi *
34715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
348f3f0262cSandi */
349f3f0262cSandifunction checkwordblock(){
350f3f0262cSandi  global $TEXT;
351f3f0262cSandi  global $conf;
352f3f0262cSandi
353f3f0262cSandi  if(!$conf['usewordblock']) return false;
354f3f0262cSandi
355e7cb32dcSAndreas Gohr  $blockfile = file(DOKU_CONF.'wordblock.conf');
3563e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
3573e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
3583e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
3593e2965d7Sandi    //backreferences are used - the maximum is 99
3603e2965d7Sandi    //this is very bad performancewise and may even be too high still
3613e2965d7Sandi    $chunksize = 40;
3623e2965d7Sandi  }else{
363703f6fdeSandi    //read file in chunks of 600 - this should work around the
3643e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
3653e2965d7Sandi    $chunksize = 600;
3663e2965d7Sandi  }
3673e2965d7Sandi  while($blocks = array_splice($blockfile,0,$chunksize)){
368f3f0262cSandi    $re = array();
369f3f0262cSandi    #build regexp from blocks
370f3f0262cSandi    foreach($blocks as $block){
371f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
372f3f0262cSandi      $block = trim($block);
373f3f0262cSandi      if(empty($block)) continue;
374f3f0262cSandi      $re[]  = $block;
375f3f0262cSandi    }
376f3f0262cSandi    if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true;
377703f6fdeSandi  }
378f3f0262cSandi  return false;
379f3f0262cSandi}
380f3f0262cSandi
381f3f0262cSandi/**
38215fae107Sandi * Return the IP of the client
38315fae107Sandi *
38415fae107Sandi * Honours X-Forwarded-For Proxy Headers
38515fae107Sandi *
38615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
387f3f0262cSandi */
388f3f0262cSandifunction clientIP(){
389f3f0262cSandi  $my = $_SERVER['REMOTE_ADDR'];
390f3f0262cSandi  if($_SERVER['HTTP_X_FORWARDED_FOR']){
391f3f0262cSandi    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
392f3f0262cSandi  }
393f3f0262cSandi  return $my;
394f3f0262cSandi}
395f3f0262cSandi
396f3f0262cSandi/**
39715fae107Sandi * Checks if a given page is currently locked.
39815fae107Sandi *
399f3f0262cSandi * removes stale lockfiles
40015fae107Sandi *
40115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
402f3f0262cSandi */
403f3f0262cSandifunction checklock($id){
404f3f0262cSandi  global $conf;
405f3f0262cSandi  $lock = wikiFN($id).'.lock';
406f3f0262cSandi
407f3f0262cSandi  //no lockfile
408f3f0262cSandi  if(!@file_exists($lock)) return false;
409f3f0262cSandi
410f3f0262cSandi  //lockfile expired
411f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
412f3f0262cSandi    unlink($lock);
413f3f0262cSandi    return false;
414f3f0262cSandi  }
415f3f0262cSandi
416f3f0262cSandi  //my own lock
417f3f0262cSandi  $ip = io_readFile($lock);
418f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
419f3f0262cSandi    return false;
420f3f0262cSandi  }
421f3f0262cSandi
422f3f0262cSandi  return $ip;
423f3f0262cSandi}
424f3f0262cSandi
425f3f0262cSandi/**
42615fae107Sandi * Lock a page for editing
42715fae107Sandi *
42815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
429f3f0262cSandi */
430f3f0262cSandifunction lock($id){
431f3f0262cSandi  $lock = wikiFN($id).'.lock';
432f3f0262cSandi  if($_SERVER['REMOTE_USER']){
433f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
434f3f0262cSandi  }else{
435f3f0262cSandi    io_saveFile($lock,clientIP());
436f3f0262cSandi  }
437f3f0262cSandi}
438f3f0262cSandi
439f3f0262cSandi/**
44015fae107Sandi * Unlock a page if it was locked by the user
441f3f0262cSandi *
44215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
44315fae107Sandi * @return bool true if a lock was removed
444f3f0262cSandi */
445f3f0262cSandifunction unlock($id){
446f3f0262cSandi  $lock = wikiFN($id).'.lock';
447f3f0262cSandi  if(@file_exists($lock)){
448f3f0262cSandi    $ip = io_readFile($lock);
449f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
450f3f0262cSandi      @unlink($lock);
451f3f0262cSandi      return true;
452f3f0262cSandi    }
453f3f0262cSandi  }
454f3f0262cSandi  return false;
455f3f0262cSandi}
456f3f0262cSandi
457f3f0262cSandi/**
458f3f0262cSandi * convert line ending to unix format
459f3f0262cSandi *
46015fae107Sandi * @see    formText() for 2crlf conversion
46115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
462f3f0262cSandi */
463f3f0262cSandifunction cleanText($text){
464f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
465f3f0262cSandi  return $text;
466f3f0262cSandi}
467f3f0262cSandi
468f3f0262cSandi/**
469f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
470f3f0262cSandi * It also converts line endings to Windows format which is
471f3f0262cSandi * pseudo standard for webforms.
472f3f0262cSandi *
47315fae107Sandi * @see    cleanText() for 2unix conversion
47415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
475f3f0262cSandi */
476f3f0262cSandifunction formText($text){
477f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
478f3f0262cSandi  return htmlspecialchars($text);
479f3f0262cSandi}
480f3f0262cSandi
481f3f0262cSandi/**
48215fae107Sandi * Returns the specified local text in raw format
48315fae107Sandi *
48415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
485f3f0262cSandi */
486f3f0262cSandifunction rawLocale($id){
487f3f0262cSandi  return io_readFile(localeFN($id));
488f3f0262cSandi}
489f3f0262cSandi
490f3f0262cSandi/**
491f3f0262cSandi * Returns the raw WikiText
49215fae107Sandi *
49315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
494f3f0262cSandi */
495f3f0262cSandifunction rawWiki($id,$rev=''){
496f3f0262cSandi  return io_readFile(wikiFN($id,$rev));
497f3f0262cSandi}
498f3f0262cSandi
499f3f0262cSandi/**
5007146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
5017146cee2SAndreas Gohr *
5027146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5037146cee2SAndreas Gohr */
5047146cee2SAndreas Gohrfunction pageTemplate($id){
505a15ce62dSEsther Brunner  global $conf;
506a15ce62dSEsther Brunner  global $INFO;
507a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
508a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
509a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
510a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
511a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
512a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
513a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
514a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
515a15ce62dSEsther Brunner  return $tpl;
5167146cee2SAndreas Gohr}
5177146cee2SAndreas Gohr
5187146cee2SAndreas Gohr
5197146cee2SAndreas Gohr/**
52015fae107Sandi * Returns the raw Wiki Text in three slices.
52115fae107Sandi *
52215fae107Sandi * The range parameter needs to have the form "from-to"
52315cfe303Sandi * and gives the range of the section in bytes - no
52415cfe303Sandi * UTF-8 awareness is needed.
525f3f0262cSandi * The returned order is prefix, section and suffix.
52615fae107Sandi *
52715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
528f3f0262cSandi */
529f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
530f3f0262cSandi  list($from,$to) = split('-',$range,2);
531f3f0262cSandi  $text = io_readFile(wikiFN($id,$rev));
532f3f0262cSandi  if(!$from) $from = 0;
533c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
534f3f0262cSandi
53515cfe303Sandi  $slices[0] = substr($text,0,$from-1);
53615cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
53715cfe303Sandi  $slices[2] = substr($text,$to);
538f3f0262cSandi
539f3f0262cSandi  return $slices;
540f3f0262cSandi}
541f3f0262cSandi
542f3f0262cSandi/**
54315fae107Sandi * Joins wiki text slices
54415fae107Sandi *
545f3f0262cSandi * function to join the text slices with correct lineendings again.
546f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
547f3f0262cSandi * lines between sections if needed (used on saving).
54815fae107Sandi *
54915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
550f3f0262cSandi */
551f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
552f3f0262cSandi
553f3f0262cSandi  if($pretty){
554f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
555f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
556f3f0262cSandi  }
557f3f0262cSandi
558f3f0262cSandi  if($pre) $pre .= "\n";
559f3f0262cSandi  if($suf) $text .= "\n";
560f3f0262cSandi  return $pre.$text.$suf;
561f3f0262cSandi}
562f3f0262cSandi
563f3f0262cSandi/**
56415fae107Sandi * print debug messages
56515fae107Sandi *
566f3f0262cSandi * little function to print the content of a var
56715fae107Sandi *
56815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
569f3f0262cSandi */
570f3f0262cSandifunction dbg($msg,$hidden=false){
571f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
572f3f0262cSandi  print_r($msg);
573f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
574f3f0262cSandi}
575f3f0262cSandi
576f3f0262cSandi/**
577f3f0262cSandi * Add's an entry to the changelog
57815fae107Sandi *
57915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
580f3f0262cSandi */
581652610a2Sandifunction addLogEntry($date,$id,$summary=""){
582f3f0262cSandi  global $conf;
583c1049928Sandi
584c1049928Sandi  if(!@is_writable($conf['changelog'])){
585c1049928Sandi    msg($conf['changelog'].' is not writable!',-1);
586c1049928Sandi    return;
587c1049928Sandi  }
588c1049928Sandi
589652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
590f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
591f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
592f3f0262cSandi
593f3f0262cSandi  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
594dbb00abcSEsther Brunner  io_saveFile($conf['changelog'],$logline,true);
595f3f0262cSandi}
596f3f0262cSandi
597f3f0262cSandi/**
598d437bcc4SAndreas Gohr * Internal function used by getRecents
599d437bcc4SAndreas Gohr *
600d437bcc4SAndreas Gohr * don't call directly
601d437bcc4SAndreas Gohr *
602d437bcc4SAndreas Gohr * @see getRecents()
603d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
604d437bcc4SAndreas Gohr */
605d437bcc4SAndreas Gohrfunction _handleRecent($line,$incdel,$ns,$subNS){
606d437bcc4SAndreas Gohr  static $seen  = array();         //caches seen pages and skip them
607d437bcc4SAndreas Gohr  if(empty($line)) return false;   //skip empty lines
608d437bcc4SAndreas Gohr
609d437bcc4SAndreas Gohr  // split the line into parts
610d437bcc4SAndreas Gohr  list($dt,$ip,$id,$usr,$sum) = explode("\t",$line);
611d437bcc4SAndreas Gohr
612d437bcc4SAndreas Gohr  // skip seen ones
613d437bcc4SAndreas Gohr  if($seen[$id]) return false;
614d437bcc4SAndreas Gohr
615d437bcc4SAndreas Gohr  // remember in seen to skip additional sights
616d437bcc4SAndreas Gohr  $seen[$id] = 1;
617d437bcc4SAndreas Gohr
618d437bcc4SAndreas Gohr  // filter namespace
619d437bcc4SAndreas Gohr  if (($ns) && (strpos($id,$ns.':') !== 0)) return false;
620d437bcc4SAndreas Gohr
621d437bcc4SAndreas Gohr  // exclude subnamespaces
622d437bcc4SAndreas Gohr  if ((!$subNS) && (getNS($id) != $ns)) return false;
623d437bcc4SAndreas Gohr
624*ae56bfb6SAndreas Gohr  // check ACL
625*ae56bfb6SAndreas Gohr  if (auth_quickaclcheck($id) < AUTH_READ) return false;
626*ae56bfb6SAndreas Gohr
627d437bcc4SAndreas Gohr  // check existance
628d437bcc4SAndreas Gohr  if(!@file_exists(wikiFN($id))){
629d437bcc4SAndreas Gohr    if(!$incdel){
630d437bcc4SAndreas Gohr      return false;
631d437bcc4SAndreas Gohr    }else{
632d437bcc4SAndreas Gohr      $recent = array();
633d437bcc4SAndreas Gohr      $recent['del'] = true;
634d437bcc4SAndreas Gohr    }
635d437bcc4SAndreas Gohr  }else{
636d437bcc4SAndreas Gohr    $recent = array();
637d437bcc4SAndreas Gohr    $recent['del'] = false;
638d437bcc4SAndreas Gohr  }
639d437bcc4SAndreas Gohr
640d437bcc4SAndreas Gohr  $recent['id']   = $id;
641d437bcc4SAndreas Gohr  $recent['date'] = $dt;
642d437bcc4SAndreas Gohr  $recent['ip']   = $ip;
643d437bcc4SAndreas Gohr  $recent['user'] = $usr;
644d437bcc4SAndreas Gohr  $recent['sum']  = $sum;
645d437bcc4SAndreas Gohr
646d437bcc4SAndreas Gohr  return $recent;
647d437bcc4SAndreas Gohr}
648d437bcc4SAndreas Gohr
649d437bcc4SAndreas Gohr/**
650f3f0262cSandi * returns an array of recently changed files using the
651f3f0262cSandi * changelog
652d437bcc4SAndreas Gohr *
653d437bcc4SAndreas Gohr * @param int    $first   number of first entry returned (for paginating
654d437bcc4SAndreas Gohr * @param int    $num     return $num entries
655d437bcc4SAndreas Gohr * @param bool   $incdel  include deleted pages?
656d437bcc4SAndreas Gohr * @param string $ns      restrict to given namespace
657d437bcc4SAndreas Gohr * @param bool   $subNS   include subnamespaces
65815fae107Sandi *
65915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
660f3f0262cSandi */
6618f1d587cSEsther Brunnerfunction getRecents($first,$num,$incdel=false,$ns='',$subNS=true){
662f3f0262cSandi  global $conf;
663f3f0262cSandi  $recent = array();
664d437bcc4SAndreas Gohr  $count  = 0;
6655749f1ceSmatthiasgrimm
6665749f1ceSmatthiasgrimm  if(!$num)
6675749f1ceSmatthiasgrimm    return $recent;
668f3f0262cSandi
669c1049928Sandi  if(!@is_readable($conf['changelog'])){
670c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
671c1049928Sandi    return $recent;
672c1049928Sandi  }
673c1049928Sandi
674d437bcc4SAndreas Gohr  $fh  = fopen($conf['changelog'],'r');
675d437bcc4SAndreas Gohr  $buf = '';
676d437bcc4SAndreas Gohr  $csz = 4096;                              //chunksize
677d437bcc4SAndreas Gohr  fseek($fh,0,SEEK_END);                    // jump to the end
678d437bcc4SAndreas Gohr  $pos = ftell($fh);                        // position pointer
679f3f0262cSandi
680d437bcc4SAndreas Gohr  // now read backwards into buffer
681d437bcc4SAndreas Gohr  while($pos > 0){
682d437bcc4SAndreas Gohr    $pos -= $csz;                           // seek to previous chunk...
683d437bcc4SAndreas Gohr    if($pos < 0) $pos = 0;                  // ...or rest of file
684d437bcc4SAndreas Gohr    fseek($fh,$pos);
685dbb00abcSEsther Brunner
686d437bcc4SAndreas Gohr    $buf = fread($fh,$csz).$buf;            // prepend to buffer
6878f1d587cSEsther Brunner
688d437bcc4SAndreas Gohr    $lines = explode("\n",$buf);            // split buffer into lines
6895749f1ceSmatthiasgrimm
690d437bcc4SAndreas Gohr    if($pos > 0){
691d437bcc4SAndreas Gohr      $buf = array_shift($lines);           // first one may be still incomplete
692f3f0262cSandi    }
693d437bcc4SAndreas Gohr
694d437bcc4SAndreas Gohr    $cnt = count($lines);
695d437bcc4SAndreas Gohr    if(!$cnt) continue;                     // no lines yet
696d437bcc4SAndreas Gohr
697d437bcc4SAndreas Gohr    // handle lines
698d437bcc4SAndreas Gohr    for($i = $cnt-1; $i >= 0; $i--){
699d437bcc4SAndreas Gohr      $rec = _handleRecent($lines[$i],$incdel,$ns,$subNS);
700d437bcc4SAndreas Gohr      if($rec !== false){
701d437bcc4SAndreas Gohr        if(--$first >= 0) continue;         // skip first entries
702d437bcc4SAndreas Gohr        $recent[] = $rec;
703d437bcc4SAndreas Gohr        $count++;
704d437bcc4SAndreas Gohr
705d437bcc4SAndreas Gohr        // break while when we have enough entries
706d437bcc4SAndreas Gohr        if($count >= $num){
707d437bcc4SAndreas Gohr          $pos = 0; // will break the while loop
708d437bcc4SAndreas Gohr          break;    // will break the for loop
709f3f0262cSandi        }
710f3f0262cSandi      }
711d437bcc4SAndreas Gohr    }
712d437bcc4SAndreas Gohr  }// end of while
713d437bcc4SAndreas Gohr
714d437bcc4SAndreas Gohr  fclose($fh);
715f3f0262cSandi  return $recent;
716f3f0262cSandi}
717f3f0262cSandi
718f3f0262cSandi/**
719652610a2Sandi * gets additonal informations for a certain pagerevison
720652610a2Sandi * from the changelog
721652610a2Sandi *
722652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
723652610a2Sandi */
724652610a2Sandifunction getRevisionInfo($id,$rev){
725652610a2Sandi  global $conf;
726258641c6Sandi
727258641c6Sandi  if(!$rev) return(null);
728258641c6Sandi
729c1049928Sandi  $info = array();
730c1049928Sandi  if(!@is_readable($conf['changelog'])){
731c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
732c1049928Sandi    return $recent;
733c1049928Sandi  }
734652610a2Sandi  $loglines = file($conf['changelog']);
735652610a2Sandi  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
736dc42ff59Sandi  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
737652610a2Sandi  $line = split("\t",$loglines[0]);
738652610a2Sandi  $info['date'] = $line[0];
739652610a2Sandi  $info['ip']   = $line[1];
740652610a2Sandi  $info['user'] = $line[3];
741652610a2Sandi  $info['sum']   = $line[4];
742652610a2Sandi  return $info;
743652610a2Sandi}
744652610a2Sandi
745652610a2Sandi/**
746f3f0262cSandi * Saves a wikitext by calling io_saveFile
74715fae107Sandi *
74815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
749f3f0262cSandi */
750f3f0262cSandifunction saveWikiText($id,$text,$summary){
751f3f0262cSandi  global $conf;
752f3f0262cSandi  global $lang;
753f3f0262cSandi  umask($conf['umask']);
754f3f0262cSandi  // ignore if no changes were made
755f3f0262cSandi  if($text == rawWiki($id,'')){
756f3f0262cSandi    return;
757f3f0262cSandi  }
758f3f0262cSandi
759f3f0262cSandi  $file = wikiFN($id);
760f3f0262cSandi  $old  = saveOldRevision($id);
761f3f0262cSandi
762f3f0262cSandi  if (empty($text)){
763e1f3d9e1SEsther Brunner    // remove empty file
764f3f0262cSandi    @unlink($file);
765e1f3d9e1SEsther Brunner    // remove any meta info
766e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
767e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
768e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
769b158d625SSteven Danz    }
770f3f0262cSandi    $del = true;
7713ce054b3Sandi    //autoset summary on deletion
7723ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
77353d6ccfeSandi    //remove empty namespaces
77453d6ccfeSandi    io_sweepNS($id);
775f3f0262cSandi  }else{
776f3f0262cSandi    // save file (datadir is created in io_saveFile)
777f3f0262cSandi    io_saveFile($file,$text);
778f3f0262cSandi    $del = false;
779f3f0262cSandi  }
780f3f0262cSandi
781652610a2Sandi  addLogEntry(@filemtime($file),$id,$summary);
78226a0801fSAndreas Gohr  // send notify mails
78326a0801fSAndreas Gohr  notify($id,'admin',$old,$summary);
78426a0801fSAndreas Gohr  notify($id,'subscribers',$old,$summary);
785f3f0262cSandi
786f3f0262cSandi  //purge cache on add by updating the purgefile
787f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
78898407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
789f3f0262cSandi  }
790f3f0262cSandi}
791f3f0262cSandi
792f3f0262cSandi/**
793f3f0262cSandi * moves the current version to the attic and returns its
794f3f0262cSandi * revision date
79515fae107Sandi *
79615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
797f3f0262cSandi */
798f3f0262cSandifunction saveOldRevision($id){
799f3f0262cSandi	global $conf;
800f3f0262cSandi  umask($conf['umask']);
801f3f0262cSandi  $oldf = wikiFN($id);
802f3f0262cSandi  if(!@file_exists($oldf)) return '';
803f3f0262cSandi  $date = filemtime($oldf);
804f3f0262cSandi  $newf = wikiFN($id,$date);
805f3f0262cSandi  if(substr($newf,-3)=='.gz'){
806f3f0262cSandi    io_saveFile($newf,rawWiki($id));
807f3f0262cSandi  }else{
808f3f0262cSandi    io_makeFileDir($newf);
809f3f0262cSandi    copy($oldf, $newf);
810f3f0262cSandi  }
811f3f0262cSandi  return $date;
812f3f0262cSandi}
813f3f0262cSandi
814f3f0262cSandi/**
81526a0801fSAndreas Gohr * Sends a notify mail on page change
81626a0801fSAndreas Gohr *
81726a0801fSAndreas Gohr * @param  string $id       The changed page
81826a0801fSAndreas Gohr * @param  string $who      Who to notify (admin|subscribers)
81926a0801fSAndreas Gohr * @param  int    $rev      Old page revision
82026a0801fSAndreas Gohr * @param  string $summary  What changed
82115fae107Sandi *
82215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
823f3f0262cSandi */
82426a0801fSAndreas Gohrfunction notify($id,$who,$rev='',$summary=''){
825f3f0262cSandi  global $lang;
826f3f0262cSandi  global $conf;
827b158d625SSteven Danz
82826a0801fSAndreas Gohr  // decide if there is something to do
82926a0801fSAndreas Gohr  if($who == 'admin'){
83026a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
831f3f0262cSandi    $text = rawLocale('mailtext');
83226a0801fSAndreas Gohr    $to   = $conf['notify'];
83326a0801fSAndreas Gohr    $bcc  = '';
83426a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
83526a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
83626a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
83726a0801fSAndreas Gohr    if(empty($bcc)) return;
83826a0801fSAndreas Gohr    $to   = '';
83926a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
84026a0801fSAndreas Gohr  }else{
84126a0801fSAndreas Gohr    return; //just to be safe
84226a0801fSAndreas Gohr  }
84326a0801fSAndreas Gohr
844f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
845f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
846f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
847f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
848ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
84926a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
85026a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
851ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
852f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
8537a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
854f3f0262cSandi
855f3f0262cSandi  if($rev){
856f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
857ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
858f3f0262cSandi    require_once("inc/DifferenceEngine.php");
859f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
860f3f0262cSandi                    split("\n",rawWiki($id)));
861f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
862f3f0262cSandi    $diff    = $dformat->format($df);
863f3f0262cSandi  }else{
864f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
865f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
866f3f0262cSandi    $diff = rawWiki($id);
867f3f0262cSandi  }
868f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
869241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
870f3f0262cSandi
87126a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
872f3f0262cSandi}
873f3f0262cSandi
87415fae107Sandi/**
87515fae107Sandi * Return a list of available page revisons
87615fae107Sandi *
87715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
87815fae107Sandi */
879f3f0262cSandifunction getRevisions($id){
880f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
881f3f0262cSandi  $revs = array();
882f3f0262cSandi  $clid = cleanID($id);
883f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
884493a6929SKobaYY  $clid = utf8_encodeFN($clid);
885f3f0262cSandi
886f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
887f3f0262cSandi    while (($file = readdir($dh)) !== false) {
888f3f0262cSandi      if (is_dir($revd.'/'.$file)) continue;
889f3f0262cSandi      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
890f3f0262cSandi        $revs[]=$match[1];
891f3f0262cSandi      }
892f3f0262cSandi    }
893f3f0262cSandi    closedir($dh);
894f3f0262cSandi  }
895f3f0262cSandi  rsort($revs);
896f3f0262cSandi  return $revs;
897f3f0262cSandi}
898f3f0262cSandi
899f3f0262cSandi/**
900f3f0262cSandi * extracts the query from a google referer
90115fae107Sandi *
9026b13307fSandi * @todo   should be more generic and support yahoo et al
90315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
904f3f0262cSandi */
905f3f0262cSandifunction getGoogleQuery(){
906f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
9075c3f206fSandi  if(!$url) return '';
908f3f0262cSandi
909f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
910f3f0262cSandi  $query = array();
911f3f0262cSandi  parse_str($url['query'],$query);
912f3f0262cSandi
913f3f0262cSandi  return $query['q'];
914f3f0262cSandi}
915f3f0262cSandi
916f3f0262cSandi/**
91715fae107Sandi * Try to set correct locale
91815fae107Sandi *
919095bfd5cSandi * @deprecated No longer used
92015fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
921f3f0262cSandi */
922f3f0262cSandifunction setCorrectLocale(){
923f3f0262cSandi  global $conf;
924f3f0262cSandi  global $lang;
925f3f0262cSandi
926f3f0262cSandi  $enc = strtoupper($lang['encoding']);
927f3f0262cSandi  foreach ($lang['locales'] as $loc){
928f3f0262cSandi    //try locale
929f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
930f3f0262cSandi    //try loceale with encoding
931f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
932f3f0262cSandi  }
933f3f0262cSandi  //still here? try to set from environment
934f3f0262cSandi  @setlocale(LC_ALL,"");
935f3f0262cSandi}
936f3f0262cSandi
937f3f0262cSandi/**
938f3f0262cSandi * Return the human readable size of a file
939f3f0262cSandi *
940f3f0262cSandi * @param       int    $size   A file size
941f3f0262cSandi * @param       int    $dec    A number of decimal places
942f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
943f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
944f3f0262cSandi * @version     1.0.0
945f3f0262cSandi */
946f31d5b73Sandifunction filesize_h($size, $dec = 1){
947f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
948f3f0262cSandi  $count = count($sizes);
949f3f0262cSandi  $i = 0;
950f3f0262cSandi
951f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
952f3f0262cSandi    $size /= 1024;
953f3f0262cSandi    $i++;
954f3f0262cSandi  }
955f3f0262cSandi
956f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
957f3f0262cSandi}
958f3f0262cSandi
95915fae107Sandi/**
96000a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
96100a7b5adSEsther Brunner *
96200a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
96300a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
96400a7b5adSEsther Brunner */
96500a7b5adSEsther Brunnerfunction obfuscate($email) {
96600a7b5adSEsther Brunner  global $conf;
96700a7b5adSEsther Brunner
96800a7b5adSEsther Brunner  switch ($conf['mailguard']) {
96900a7b5adSEsther Brunner    case 'visible' :
97000a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
97100a7b5adSEsther Brunner      return strtr($email, $obfuscate);
97200a7b5adSEsther Brunner
97300a7b5adSEsther Brunner    case 'hex' :
97400a7b5adSEsther Brunner      $encode = '';
97500a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
97600a7b5adSEsther Brunner      return $encode;
97700a7b5adSEsther Brunner
97800a7b5adSEsther Brunner    case 'none' :
97900a7b5adSEsther Brunner    default :
98000a7b5adSEsther Brunner      return $email;
98100a7b5adSEsther Brunner  }
98200a7b5adSEsther Brunner}
98300a7b5adSEsther Brunner
98400a7b5adSEsther Brunner/**
985dc57ef04Sandi * Return DokuWikis version
98615fae107Sandi *
98715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
98815fae107Sandi */
989f31d5b73Sandifunction getVersion(){
990f31d5b73Sandi  //import version string
991f31d5b73Sandi  if(@file_exists('VERSION')){
992f31d5b73Sandi    //official release
9930647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
994f31d5b73Sandi  }elseif(is_dir('_darcs')){
995f31d5b73Sandi    //darcs checkout
996f31d5b73Sandi    $inv = file('_darcs/inventory');
997f31d5b73Sandi    $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv);
998f31d5b73Sandi    $cur = array_pop($inv);
999f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1000f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1001f31d5b73Sandi  }else{
1002f31d5b73Sandi    return 'snapshot?';
1003f31d5b73Sandi  }
1004f31d5b73Sandi}
1005f31d5b73Sandi
1006f31d5b73Sandi/**
1007f31d5b73Sandi * Run a few sanity checks
1008f31d5b73Sandi *
1009f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
1010f31d5b73Sandi */
1011f3f0262cSandifunction check(){
1012f3f0262cSandi  global $conf;
1013f3f0262cSandi  global $INFO;
1014f3f0262cSandi
1015f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
1016f31d5b73Sandi
101749022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
101849022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
101949022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
102049022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
102149022a38Sandi  }else{
102249022a38Sandi    msg('PHP version '.phpversion(),1);
102349022a38Sandi  }
102449022a38Sandi
1025f3f0262cSandi  if(is_writable($conf['changelog'])){
1026f3f0262cSandi    msg('Changelog is writable',1);
1027f3f0262cSandi  }else{
1028f3f0262cSandi    msg('Changelog is not writable',-1);
1029f3f0262cSandi  }
1030f3f0262cSandi
1031f3f0262cSandi  if(is_writable($conf['datadir'])){
1032f3f0262cSandi    msg('Datadir is writable',1);
1033f3f0262cSandi  }else{
1034f3f0262cSandi    msg('Datadir is not writable',-1);
1035f3f0262cSandi  }
1036f3f0262cSandi
1037f3f0262cSandi  if(is_writable($conf['olddir'])){
1038f3f0262cSandi    msg('Attic is writable',1);
1039f3f0262cSandi  }else{
1040f3f0262cSandi    msg('Attic is not writable',-1);
1041f3f0262cSandi  }
1042f3f0262cSandi
1043f3f0262cSandi  if(is_writable($conf['mediadir'])){
1044f3f0262cSandi    msg('Mediadir is writable',1);
1045f3f0262cSandi  }else{
1046f3f0262cSandi    msg('Mediadir is not writable',-1);
1047f3f0262cSandi  }
1048f3f0262cSandi
104998407a7aSandi  if(is_writable($conf['cachedir'])){
105098407a7aSandi    msg('Cachedir is writable',1);
105198407a7aSandi  }else{
105298407a7aSandi    msg('Cachedir is not writable',-1);
105398407a7aSandi  }
105498407a7aSandi
1055e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
10568c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1057f3f0262cSandi  }else{
10588c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1059f3f0262cSandi  }
106093a9e835Sandi
106193a9e835Sandi  if(function_exists('mb_strpos')){
106293a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
106393a9e835Sandi      msg('mb_string extension is available but will not be used',0);
106493a9e835Sandi    }else{
106593a9e835Sandi      msg('mb_string extension is available and will be used',1);
106693a9e835Sandi    }
106793a9e835Sandi  }else{
106893a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
106993a9e835Sandi  }
1070f3f0262cSandi
1071f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1072f3f0262cSandi
1073f3f0262cSandi  if(is_writable($INFO['filepath'])){
1074f3f0262cSandi    msg('The current page is writable by the webserver',0);
1075f3f0262cSandi  }else{
1076f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1077f3f0262cSandi  }
1078f3f0262cSandi
1079f3f0262cSandi  if($INFO['writable']){
1080f3f0262cSandi    msg('The current page is writable by you',0);
1081f3f0262cSandi  }else{
1082f3f0262cSandi    msg('The current page is not writable you',0);
1083f3f0262cSandi  }
1084f3f0262cSandi}
1085340756e4Sandi
1086b158d625SSteven Danz/**
1087b158d625SSteven Danz * Let us know if a user is tracking a page
1088b158d625SSteven Danz *
10891380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1090b158d625SSteven Danz */
10911380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
10921380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
10931380fc45SAndreas Gohr  if (@file_exists($file)) {
1094b158d625SSteven Danz    $mlist = file($file);
10951380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
10961380fc45SAndreas Gohr    return is_int($pos);
1097b158d625SSteven Danz  }
10981380fc45SAndreas Gohr
1099b158d625SSteven Danz  return false;
1100b158d625SSteven Danz}
1101340756e4Sandi
1102f9eb5648Ssteven-danz/**
1103f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1104f9eb5648Ssteven-danz * users subscribed to a page
1105f9eb5648Ssteven-danz *
110626a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1107f9eb5648Ssteven-danz */
1108f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1109f9eb5648Ssteven-danz  global $conf;
1110f9eb5648Ssteven-danz
1111f9eb5648Ssteven-danz  $emails = '';
1112f9eb5648Ssteven-danz
111326a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
111426a0801fSAndreas Gohr
1115f9eb5648Ssteven-danz  $mlist = array();
1116f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1117f9eb5648Ssteven-danz  if (file_exists($file)) {
1118f9eb5648Ssteven-danz    $mlist = file($file);
1119f9eb5648Ssteven-danz  }
1120f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1121f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1122f9eb5648Ssteven-danz      $who = rtrim($who);
1123f9eb5648Ssteven-danz      $info = auth_getUserData($who);
1124f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1125f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1126f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1127f9eb5648Ssteven-danz          if (empty($emails)) {
1128f9eb5648Ssteven-danz            $emails = $info['mail'];
1129f9eb5648Ssteven-danz          } else {
1130f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1131f9eb5648Ssteven-danz          }
1132f9eb5648Ssteven-danz        }
1133f9eb5648Ssteven-danz      }
1134f9eb5648Ssteven-danz    }
1135f9eb5648Ssteven-danz  }
1136f9eb5648Ssteven-danz
1137f9eb5648Ssteven-danz  return $emails;
1138f9eb5648Ssteven-danz}
1139f9eb5648Ssteven-danz
114089541d4bSAndreas Gohr/**
114189541d4bSAndreas Gohr * Removes quoting backslashes
114289541d4bSAndreas Gohr *
114389541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
114489541d4bSAndreas Gohr */
114589541d4bSAndreas Gohrfunction unslash($string,$char="'"){
114689541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
114789541d4bSAndreas Gohr}
114889541d4bSAndreas Gohr
1149340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1150