xref: /dokuwiki/inc/common.php (revision d437bcc4a25f62fad65e625f4bc13cab8873f994)
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/**
598*d437bcc4SAndreas Gohr * Internal function used by getRecents
599*d437bcc4SAndreas Gohr *
600*d437bcc4SAndreas Gohr * don't call directly
601*d437bcc4SAndreas Gohr *
602*d437bcc4SAndreas Gohr * @see getRecents()
603*d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
604*d437bcc4SAndreas Gohr */
605*d437bcc4SAndreas Gohrfunction _handleRecent($line,$incdel,$ns,$subNS){
606*d437bcc4SAndreas Gohr  static $seen  = array();         //caches seen pages and skip them
607*d437bcc4SAndreas Gohr  if(empty($line)) return false;   //skip empty lines
608*d437bcc4SAndreas Gohr
609*d437bcc4SAndreas Gohr  // split the line into parts
610*d437bcc4SAndreas Gohr  list($dt,$ip,$id,$usr,$sum) = explode("\t",$line);
611*d437bcc4SAndreas Gohr
612*d437bcc4SAndreas Gohr  // skip seen ones
613*d437bcc4SAndreas Gohr  if($seen[$id]) return false;
614*d437bcc4SAndreas Gohr
615*d437bcc4SAndreas Gohr  // remember in seen to skip additional sights
616*d437bcc4SAndreas Gohr  $seen[$id] = 1;
617*d437bcc4SAndreas Gohr
618*d437bcc4SAndreas Gohr  // filter namespace
619*d437bcc4SAndreas Gohr  if (($ns) && (strpos($id,$ns.':') !== 0)) return false;
620*d437bcc4SAndreas Gohr
621*d437bcc4SAndreas Gohr  // exclude subnamespaces
622*d437bcc4SAndreas Gohr  if ((!$subNS) && (getNS($id) != $ns)) return false;
623*d437bcc4SAndreas Gohr
624*d437bcc4SAndreas Gohr  // check existance
625*d437bcc4SAndreas Gohr  if(!@file_exists(wikiFN($id))){
626*d437bcc4SAndreas Gohr    if(!$incdel){
627*d437bcc4SAndreas Gohr      return false;
628*d437bcc4SAndreas Gohr    }else{
629*d437bcc4SAndreas Gohr      $recent = array();
630*d437bcc4SAndreas Gohr      $recent['del'] = true;
631*d437bcc4SAndreas Gohr    }
632*d437bcc4SAndreas Gohr  }else{
633*d437bcc4SAndreas Gohr    $recent = array();
634*d437bcc4SAndreas Gohr    $recent['del'] = false;
635*d437bcc4SAndreas Gohr  }
636*d437bcc4SAndreas Gohr
637*d437bcc4SAndreas Gohr  $recent['id']   = $id;
638*d437bcc4SAndreas Gohr  $recent['date'] = $dt;
639*d437bcc4SAndreas Gohr  $recent['ip']   = $ip;
640*d437bcc4SAndreas Gohr  $recent['user'] = $usr;
641*d437bcc4SAndreas Gohr  $recent['sum']  = $sum;
642*d437bcc4SAndreas Gohr
643*d437bcc4SAndreas Gohr  return $recent;
644*d437bcc4SAndreas Gohr}
645*d437bcc4SAndreas Gohr
646*d437bcc4SAndreas Gohr/**
647f3f0262cSandi * returns an array of recently changed files using the
648f3f0262cSandi * changelog
649*d437bcc4SAndreas Gohr *
650*d437bcc4SAndreas Gohr * @param int    $first   number of first entry returned (for paginating
651*d437bcc4SAndreas Gohr * @param int    $num     return $num entries
652*d437bcc4SAndreas Gohr * @param bool   $incdel  include deleted pages?
653*d437bcc4SAndreas Gohr * @param string $ns      restrict to given namespace
654*d437bcc4SAndreas Gohr * @param bool   $subNS   include subnamespaces
65515fae107Sandi *
65615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
657f3f0262cSandi */
6588f1d587cSEsther Brunnerfunction getRecents($first,$num,$incdel=false,$ns='',$subNS=true){
659f3f0262cSandi  global $conf;
660f3f0262cSandi  $recent = array();
661*d437bcc4SAndreas Gohr  $count  = 0;
6625749f1ceSmatthiasgrimm
6635749f1ceSmatthiasgrimm  if(!$num)
6645749f1ceSmatthiasgrimm    return $recent;
665f3f0262cSandi
666c1049928Sandi  if(!@is_readable($conf['changelog'])){
667c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
668c1049928Sandi    return $recent;
669c1049928Sandi  }
670c1049928Sandi
671*d437bcc4SAndreas Gohr  $fh  = fopen($conf['changelog'],'r');
672*d437bcc4SAndreas Gohr  $buf = '';
673*d437bcc4SAndreas Gohr  $csz = 4096;                              //chunksize
674*d437bcc4SAndreas Gohr  fseek($fh,0,SEEK_END);                    // jump to the end
675*d437bcc4SAndreas Gohr  $pos = ftell($fh);                        // position pointer
676f3f0262cSandi
677*d437bcc4SAndreas Gohr  // now read backwards into buffer
678*d437bcc4SAndreas Gohr  while($pos > 0){
679*d437bcc4SAndreas Gohr    $pos -= $csz;                           // seek to previous chunk...
680*d437bcc4SAndreas Gohr    if($pos < 0) $pos = 0;                  // ...or rest of file
681*d437bcc4SAndreas Gohr    fseek($fh,$pos);
682dbb00abcSEsther Brunner
683*d437bcc4SAndreas Gohr    $buf = fread($fh,$csz).$buf;            // prepend to buffer
6848f1d587cSEsther Brunner
685*d437bcc4SAndreas Gohr    $lines = explode("\n",$buf);            // split buffer into lines
6865749f1ceSmatthiasgrimm
687*d437bcc4SAndreas Gohr    if($pos > 0){
688*d437bcc4SAndreas Gohr      $buf = array_shift($lines);           // first one may be still incomplete
689f3f0262cSandi    }
690*d437bcc4SAndreas Gohr
691*d437bcc4SAndreas Gohr    $cnt = count($lines);
692*d437bcc4SAndreas Gohr    if(!$cnt) continue;                     // no lines yet
693*d437bcc4SAndreas Gohr
694*d437bcc4SAndreas Gohr    // handle lines
695*d437bcc4SAndreas Gohr    for($i = $cnt-1; $i >= 0; $i--){
696*d437bcc4SAndreas Gohr      $rec = _handleRecent($lines[$i],$incdel,$ns,$subNS);
697*d437bcc4SAndreas Gohr      if($rec !== false){
698*d437bcc4SAndreas Gohr        if(--$first >= 0) continue;         // skip first entries
699*d437bcc4SAndreas Gohr        $recent[] = $rec;
700*d437bcc4SAndreas Gohr        $count++;
701*d437bcc4SAndreas Gohr
702*d437bcc4SAndreas Gohr        // break while when we have enough entries
703*d437bcc4SAndreas Gohr        if($count >= $num){
704*d437bcc4SAndreas Gohr          $pos = 0; // will break the while loop
705*d437bcc4SAndreas Gohr          break;    // will break the for loop
706f3f0262cSandi        }
707f3f0262cSandi      }
708*d437bcc4SAndreas Gohr    }
709*d437bcc4SAndreas Gohr  }// end of while
710*d437bcc4SAndreas Gohr
711*d437bcc4SAndreas Gohr  fclose($fh);
712f3f0262cSandi  return $recent;
713f3f0262cSandi}
714f3f0262cSandi
715f3f0262cSandi/**
716652610a2Sandi * gets additonal informations for a certain pagerevison
717652610a2Sandi * from the changelog
718652610a2Sandi *
719652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
720652610a2Sandi */
721652610a2Sandifunction getRevisionInfo($id,$rev){
722652610a2Sandi  global $conf;
723258641c6Sandi
724258641c6Sandi  if(!$rev) return(null);
725258641c6Sandi
726c1049928Sandi  $info = array();
727c1049928Sandi  if(!@is_readable($conf['changelog'])){
728c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
729c1049928Sandi    return $recent;
730c1049928Sandi  }
731652610a2Sandi  $loglines = file($conf['changelog']);
732652610a2Sandi  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
733dc42ff59Sandi  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
734652610a2Sandi  $line = split("\t",$loglines[0]);
735652610a2Sandi  $info['date'] = $line[0];
736652610a2Sandi  $info['ip']   = $line[1];
737652610a2Sandi  $info['user'] = $line[3];
738652610a2Sandi  $info['sum']   = $line[4];
739652610a2Sandi  return $info;
740652610a2Sandi}
741652610a2Sandi
742652610a2Sandi/**
743f3f0262cSandi * Saves a wikitext by calling io_saveFile
74415fae107Sandi *
74515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
746f3f0262cSandi */
747f3f0262cSandifunction saveWikiText($id,$text,$summary){
748f3f0262cSandi  global $conf;
749f3f0262cSandi  global $lang;
750f3f0262cSandi  umask($conf['umask']);
751f3f0262cSandi  // ignore if no changes were made
752f3f0262cSandi  if($text == rawWiki($id,'')){
753f3f0262cSandi    return;
754f3f0262cSandi  }
755f3f0262cSandi
756f3f0262cSandi  $file = wikiFN($id);
757f3f0262cSandi  $old  = saveOldRevision($id);
758f3f0262cSandi
759f3f0262cSandi  if (empty($text)){
760e1f3d9e1SEsther Brunner    // remove empty file
761f3f0262cSandi    @unlink($file);
762e1f3d9e1SEsther Brunner    // remove any meta info
763e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
764e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
765e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
766b158d625SSteven Danz    }
767f3f0262cSandi    $del = true;
7683ce054b3Sandi    //autoset summary on deletion
7693ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
77053d6ccfeSandi    //remove empty namespaces
77153d6ccfeSandi    io_sweepNS($id);
772f3f0262cSandi  }else{
773f3f0262cSandi    // save file (datadir is created in io_saveFile)
774f3f0262cSandi    io_saveFile($file,$text);
775f3f0262cSandi    $del = false;
776f3f0262cSandi  }
777f3f0262cSandi
778652610a2Sandi  addLogEntry(@filemtime($file),$id,$summary);
77926a0801fSAndreas Gohr  // send notify mails
78026a0801fSAndreas Gohr  notify($id,'admin',$old,$summary);
78126a0801fSAndreas Gohr  notify($id,'subscribers',$old,$summary);
782f3f0262cSandi
783f3f0262cSandi  //purge cache on add by updating the purgefile
784f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
78598407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
786f3f0262cSandi  }
787f3f0262cSandi}
788f3f0262cSandi
789f3f0262cSandi/**
790f3f0262cSandi * moves the current version to the attic and returns its
791f3f0262cSandi * revision date
79215fae107Sandi *
79315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
794f3f0262cSandi */
795f3f0262cSandifunction saveOldRevision($id){
796f3f0262cSandi	global $conf;
797f3f0262cSandi  umask($conf['umask']);
798f3f0262cSandi  $oldf = wikiFN($id);
799f3f0262cSandi  if(!@file_exists($oldf)) return '';
800f3f0262cSandi  $date = filemtime($oldf);
801f3f0262cSandi  $newf = wikiFN($id,$date);
802f3f0262cSandi  if(substr($newf,-3)=='.gz'){
803f3f0262cSandi    io_saveFile($newf,rawWiki($id));
804f3f0262cSandi  }else{
805f3f0262cSandi    io_makeFileDir($newf);
806f3f0262cSandi    copy($oldf, $newf);
807f3f0262cSandi  }
808f3f0262cSandi  return $date;
809f3f0262cSandi}
810f3f0262cSandi
811f3f0262cSandi/**
81226a0801fSAndreas Gohr * Sends a notify mail on page change
81326a0801fSAndreas Gohr *
81426a0801fSAndreas Gohr * @param  string $id       The changed page
81526a0801fSAndreas Gohr * @param  string $who      Who to notify (admin|subscribers)
81626a0801fSAndreas Gohr * @param  int    $rev      Old page revision
81726a0801fSAndreas Gohr * @param  string $summary  What changed
81815fae107Sandi *
81915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
820f3f0262cSandi */
82126a0801fSAndreas Gohrfunction notify($id,$who,$rev='',$summary=''){
822f3f0262cSandi  global $lang;
823f3f0262cSandi  global $conf;
824b158d625SSteven Danz
82526a0801fSAndreas Gohr  // decide if there is something to do
82626a0801fSAndreas Gohr  if($who == 'admin'){
82726a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
828f3f0262cSandi    $text = rawLocale('mailtext');
82926a0801fSAndreas Gohr    $to   = $conf['notify'];
83026a0801fSAndreas Gohr    $bcc  = '';
83126a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
83226a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
83326a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
83426a0801fSAndreas Gohr    if(empty($bcc)) return;
83526a0801fSAndreas Gohr    $to   = '';
83626a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
83726a0801fSAndreas Gohr  }else{
83826a0801fSAndreas Gohr    return; //just to be safe
83926a0801fSAndreas Gohr  }
84026a0801fSAndreas Gohr
841f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
842f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
843f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
844f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
845ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
84626a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
84726a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
848ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
849f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
8507a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
851f3f0262cSandi
852f3f0262cSandi  if($rev){
853f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
854ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
855f3f0262cSandi    require_once("inc/DifferenceEngine.php");
856f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
857f3f0262cSandi                    split("\n",rawWiki($id)));
858f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
859f3f0262cSandi    $diff    = $dformat->format($df);
860f3f0262cSandi  }else{
861f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
862f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
863f3f0262cSandi    $diff = rawWiki($id);
864f3f0262cSandi  }
865f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
866241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
867f3f0262cSandi
86826a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
869f3f0262cSandi}
870f3f0262cSandi
87115fae107Sandi/**
87215fae107Sandi * Return a list of available page revisons
87315fae107Sandi *
87415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
87515fae107Sandi */
876f3f0262cSandifunction getRevisions($id){
877f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
878f3f0262cSandi  $revs = array();
879f3f0262cSandi  $clid = cleanID($id);
880f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
881493a6929SKobaYY  $clid = utf8_encodeFN($clid);
882f3f0262cSandi
883f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
884f3f0262cSandi    while (($file = readdir($dh)) !== false) {
885f3f0262cSandi      if (is_dir($revd.'/'.$file)) continue;
886f3f0262cSandi      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
887f3f0262cSandi        $revs[]=$match[1];
888f3f0262cSandi      }
889f3f0262cSandi    }
890f3f0262cSandi    closedir($dh);
891f3f0262cSandi  }
892f3f0262cSandi  rsort($revs);
893f3f0262cSandi  return $revs;
894f3f0262cSandi}
895f3f0262cSandi
896f3f0262cSandi/**
897f3f0262cSandi * extracts the query from a google referer
89815fae107Sandi *
8996b13307fSandi * @todo   should be more generic and support yahoo et al
90015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
901f3f0262cSandi */
902f3f0262cSandifunction getGoogleQuery(){
903f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
9045c3f206fSandi  if(!$url) return '';
905f3f0262cSandi
906f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
907f3f0262cSandi  $query = array();
908f3f0262cSandi  parse_str($url['query'],$query);
909f3f0262cSandi
910f3f0262cSandi  return $query['q'];
911f3f0262cSandi}
912f3f0262cSandi
913f3f0262cSandi/**
91415fae107Sandi * Try to set correct locale
91515fae107Sandi *
916095bfd5cSandi * @deprecated No longer used
91715fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
918f3f0262cSandi */
919f3f0262cSandifunction setCorrectLocale(){
920f3f0262cSandi  global $conf;
921f3f0262cSandi  global $lang;
922f3f0262cSandi
923f3f0262cSandi  $enc = strtoupper($lang['encoding']);
924f3f0262cSandi  foreach ($lang['locales'] as $loc){
925f3f0262cSandi    //try locale
926f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
927f3f0262cSandi    //try loceale with encoding
928f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
929f3f0262cSandi  }
930f3f0262cSandi  //still here? try to set from environment
931f3f0262cSandi  @setlocale(LC_ALL,"");
932f3f0262cSandi}
933f3f0262cSandi
934f3f0262cSandi/**
935f3f0262cSandi * Return the human readable size of a file
936f3f0262cSandi *
937f3f0262cSandi * @param       int    $size   A file size
938f3f0262cSandi * @param       int    $dec    A number of decimal places
939f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
940f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
941f3f0262cSandi * @version     1.0.0
942f3f0262cSandi */
943f31d5b73Sandifunction filesize_h($size, $dec = 1){
944f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
945f3f0262cSandi  $count = count($sizes);
946f3f0262cSandi  $i = 0;
947f3f0262cSandi
948f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
949f3f0262cSandi    $size /= 1024;
950f3f0262cSandi    $i++;
951f3f0262cSandi  }
952f3f0262cSandi
953f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
954f3f0262cSandi}
955f3f0262cSandi
95615fae107Sandi/**
95700a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
95800a7b5adSEsther Brunner *
95900a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
96000a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
96100a7b5adSEsther Brunner */
96200a7b5adSEsther Brunnerfunction obfuscate($email) {
96300a7b5adSEsther Brunner  global $conf;
96400a7b5adSEsther Brunner
96500a7b5adSEsther Brunner  switch ($conf['mailguard']) {
96600a7b5adSEsther Brunner    case 'visible' :
96700a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
96800a7b5adSEsther Brunner      return strtr($email, $obfuscate);
96900a7b5adSEsther Brunner
97000a7b5adSEsther Brunner    case 'hex' :
97100a7b5adSEsther Brunner      $encode = '';
97200a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
97300a7b5adSEsther Brunner      return $encode;
97400a7b5adSEsther Brunner
97500a7b5adSEsther Brunner    case 'none' :
97600a7b5adSEsther Brunner    default :
97700a7b5adSEsther Brunner      return $email;
97800a7b5adSEsther Brunner  }
97900a7b5adSEsther Brunner}
98000a7b5adSEsther Brunner
98100a7b5adSEsther Brunner/**
982dc57ef04Sandi * Return DokuWikis version
98315fae107Sandi *
98415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
98515fae107Sandi */
986f31d5b73Sandifunction getVersion(){
987f31d5b73Sandi  //import version string
988f31d5b73Sandi  if(@file_exists('VERSION')){
989f31d5b73Sandi    //official release
9900647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
991f31d5b73Sandi  }elseif(is_dir('_darcs')){
992f31d5b73Sandi    //darcs checkout
993f31d5b73Sandi    $inv = file('_darcs/inventory');
994f31d5b73Sandi    $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv);
995f31d5b73Sandi    $cur = array_pop($inv);
996f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
997f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
998f31d5b73Sandi  }else{
999f31d5b73Sandi    return 'snapshot?';
1000f31d5b73Sandi  }
1001f31d5b73Sandi}
1002f31d5b73Sandi
1003f31d5b73Sandi/**
1004f31d5b73Sandi * Run a few sanity checks
1005f31d5b73Sandi *
1006f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
1007f31d5b73Sandi */
1008f3f0262cSandifunction check(){
1009f3f0262cSandi  global $conf;
1010f3f0262cSandi  global $INFO;
1011f3f0262cSandi
1012f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
1013f31d5b73Sandi
101449022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
101549022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
101649022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
101749022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
101849022a38Sandi  }else{
101949022a38Sandi    msg('PHP version '.phpversion(),1);
102049022a38Sandi  }
102149022a38Sandi
1022f3f0262cSandi  if(is_writable($conf['changelog'])){
1023f3f0262cSandi    msg('Changelog is writable',1);
1024f3f0262cSandi  }else{
1025f3f0262cSandi    msg('Changelog is not writable',-1);
1026f3f0262cSandi  }
1027f3f0262cSandi
1028f3f0262cSandi  if(is_writable($conf['datadir'])){
1029f3f0262cSandi    msg('Datadir is writable',1);
1030f3f0262cSandi  }else{
1031f3f0262cSandi    msg('Datadir is not writable',-1);
1032f3f0262cSandi  }
1033f3f0262cSandi
1034f3f0262cSandi  if(is_writable($conf['olddir'])){
1035f3f0262cSandi    msg('Attic is writable',1);
1036f3f0262cSandi  }else{
1037f3f0262cSandi    msg('Attic is not writable',-1);
1038f3f0262cSandi  }
1039f3f0262cSandi
1040f3f0262cSandi  if(is_writable($conf['mediadir'])){
1041f3f0262cSandi    msg('Mediadir is writable',1);
1042f3f0262cSandi  }else{
1043f3f0262cSandi    msg('Mediadir is not writable',-1);
1044f3f0262cSandi  }
1045f3f0262cSandi
104698407a7aSandi  if(is_writable($conf['cachedir'])){
104798407a7aSandi    msg('Cachedir is writable',1);
104898407a7aSandi  }else{
104998407a7aSandi    msg('Cachedir is not writable',-1);
105098407a7aSandi  }
105198407a7aSandi
1052e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
10538c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1054f3f0262cSandi  }else{
10558c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1056f3f0262cSandi  }
105793a9e835Sandi
105893a9e835Sandi  if(function_exists('mb_strpos')){
105993a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
106093a9e835Sandi      msg('mb_string extension is available but will not be used',0);
106193a9e835Sandi    }else{
106293a9e835Sandi      msg('mb_string extension is available and will be used',1);
106393a9e835Sandi    }
106493a9e835Sandi  }else{
106593a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
106693a9e835Sandi  }
1067f3f0262cSandi
1068f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1069f3f0262cSandi
1070f3f0262cSandi  if(is_writable($INFO['filepath'])){
1071f3f0262cSandi    msg('The current page is writable by the webserver',0);
1072f3f0262cSandi  }else{
1073f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1074f3f0262cSandi  }
1075f3f0262cSandi
1076f3f0262cSandi  if($INFO['writable']){
1077f3f0262cSandi    msg('The current page is writable by you',0);
1078f3f0262cSandi  }else{
1079f3f0262cSandi    msg('The current page is not writable you',0);
1080f3f0262cSandi  }
1081f3f0262cSandi}
1082340756e4Sandi
1083b158d625SSteven Danz/**
1084b158d625SSteven Danz * Let us know if a user is tracking a page
1085b158d625SSteven Danz *
10861380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1087b158d625SSteven Danz */
10881380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
10891380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
10901380fc45SAndreas Gohr  if (@file_exists($file)) {
1091b158d625SSteven Danz    $mlist = file($file);
10921380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
10931380fc45SAndreas Gohr    return is_int($pos);
1094b158d625SSteven Danz  }
10951380fc45SAndreas Gohr
1096b158d625SSteven Danz  return false;
1097b158d625SSteven Danz}
1098340756e4Sandi
1099f9eb5648Ssteven-danz/**
1100f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1101f9eb5648Ssteven-danz * users subscribed to a page
1102f9eb5648Ssteven-danz *
110326a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1104f9eb5648Ssteven-danz */
1105f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1106f9eb5648Ssteven-danz  global $conf;
1107f9eb5648Ssteven-danz
1108f9eb5648Ssteven-danz  $emails = '';
1109f9eb5648Ssteven-danz
111026a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
111126a0801fSAndreas Gohr
1112f9eb5648Ssteven-danz  $mlist = array();
1113f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1114f9eb5648Ssteven-danz  if (file_exists($file)) {
1115f9eb5648Ssteven-danz    $mlist = file($file);
1116f9eb5648Ssteven-danz  }
1117f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1118f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1119f9eb5648Ssteven-danz      $who = rtrim($who);
1120f9eb5648Ssteven-danz      $info = auth_getUserData($who);
1121f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1122f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1123f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1124f9eb5648Ssteven-danz          if (empty($emails)) {
1125f9eb5648Ssteven-danz            $emails = $info['mail'];
1126f9eb5648Ssteven-danz          } else {
1127f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1128f9eb5648Ssteven-danz          }
1129f9eb5648Ssteven-danz        }
1130f9eb5648Ssteven-danz      }
1131f9eb5648Ssteven-danz    }
1132f9eb5648Ssteven-danz  }
1133f9eb5648Ssteven-danz
1134f9eb5648Ssteven-danz  return $emails;
1135f9eb5648Ssteven-danz}
1136f9eb5648Ssteven-danz
113789541d4bSAndreas Gohr/**
113889541d4bSAndreas Gohr * Removes quoting backslashes
113989541d4bSAndreas Gohr *
114089541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
114189541d4bSAndreas Gohr */
114289541d4bSAndreas Gohrfunction unslash($string,$char="'"){
114389541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
114489541d4bSAndreas Gohr}
114589541d4bSAndreas Gohr
1146340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1147