xref: /dokuwiki/inc/common.php (revision 00a7b5ad8c432fcfb4f394ab300d802a0fe4c35c)
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['user']       = $_SERVER['REMOTE_USER'];
30f3f0262cSandi    $info['userinfo']   = $USERINFO;
31f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
321380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
33f3f0262cSandi  }else{
34f3f0262cSandi    $info['user']       = '';
35f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
361380fc45SAndreas Gohr    $info['subscribed'] = false;
37f3f0262cSandi  }
38f3f0262cSandi
39f3f0262cSandi  $info['namespace'] = getNS($ID);
40f3f0262cSandi  $info['locked']    = checklock($ID);
41f3f0262cSandi  $info['filepath']  = realpath(wikiFN($ID,$REV));
42f3f0262cSandi  $info['exists']    = @file_exists($info['filepath']);
43f3f0262cSandi  if($REV && !$info['exists']){
44f3f0262cSandi    //check if current revision was meant
45f3f0262cSandi    $cur = wikiFN($ID);
46f3f0262cSandi    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
47f3f0262cSandi      $info['filepath'] = realpath($cur);
48f3f0262cSandi      $info['exists']   = true;
49f3f0262cSandi      $REV = '';
50f3f0262cSandi    }
51f3f0262cSandi  }
52c112d578Sandi  $info['rev'] = $REV;
53f3f0262cSandi  if($info['exists']){
54f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
55f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
56f3f0262cSandi  }else{
57f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
58f3f0262cSandi  }
59f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
60f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
61f3f0262cSandi
62652610a2Sandi  //who's the editor
63652610a2Sandi  if($REV){
64652610a2Sandi    $revinfo = getRevisionInfo($ID,$REV);
65652610a2Sandi  }else{
66652610a2Sandi    $revinfo = getRevisionInfo($ID,$info['lastmod']);
67652610a2Sandi  }
68652610a2Sandi  $info['ip']     = $revinfo['ip'];
69652610a2Sandi  $info['user']   = $revinfo['user'];
70652610a2Sandi  $info['sum']    = $revinfo['sum'];
71652610a2Sandi  $info['editor'] = $revinfo['ip'];
7288f522e9Sandi  if($revinfo['user']){
7388f522e9Sandi    $info['editor'] = $revinfo['user'];
7488f522e9Sandi  }else{
7588f522e9Sandi    $info['editor'] = $revinfo['ip'];
7688f522e9Sandi  }
77652610a2Sandi
78f3f0262cSandi  return $info;
79f3f0262cSandi}
80f3f0262cSandi
81f3f0262cSandi/**
822684e50aSAndreas Gohr * Build an string of URL parameters
832684e50aSAndreas Gohr *
842684e50aSAndreas Gohr * @author Andreas Gohr
852684e50aSAndreas Gohr */
862684e50aSAndreas Gohrfunction buildURLparams($params){
872684e50aSAndreas Gohr  $url = '';
882684e50aSAndreas Gohr  $amp = false;
892684e50aSAndreas Gohr  foreach($params as $key => $val){
902684e50aSAndreas Gohr    if($amp) $url .= '&amp;';
912684e50aSAndreas Gohr
922684e50aSAndreas Gohr    $url .= $key.'=';
932684e50aSAndreas Gohr    $url .= urlencode($val);
942684e50aSAndreas Gohr    $amp = true;
952684e50aSAndreas Gohr  }
962684e50aSAndreas Gohr  return $url;
972684e50aSAndreas Gohr}
982684e50aSAndreas Gohr
992684e50aSAndreas Gohr/**
1002684e50aSAndreas Gohr * Build an string of html tag attributes
1012684e50aSAndreas Gohr *
1022684e50aSAndreas Gohr * @author Andreas Gohr
1032684e50aSAndreas Gohr */
1042684e50aSAndreas Gohrfunction buildAttributes($params){
1052684e50aSAndreas Gohr  $url = '';
1062684e50aSAndreas Gohr  foreach($params as $key => $val){
1072684e50aSAndreas Gohr    $url .= $key.'="';
1082684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1092684e50aSAndreas Gohr    $url .= '" ';
1102684e50aSAndreas Gohr  }
1112684e50aSAndreas Gohr  return $url;
1122684e50aSAndreas Gohr}
1132684e50aSAndreas Gohr
1142684e50aSAndreas Gohr
1152684e50aSAndreas Gohr/**
1160396becbSandi * print a message
1170396becbSandi *
1180396becbSandi * If HTTP headers were not sent yet the message is added
1190396becbSandi * to the global message array else it's printed directly
1200396becbSandi * using html_msgarea()
1210396becbSandi *
122f3f0262cSandi *
123f3f0262cSandi * Levels can be:
124f3f0262cSandi *
125f3f0262cSandi * -1 error
126f3f0262cSandi *  0 info
127f3f0262cSandi *  1 success
12815fae107Sandi *
12915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1300396becbSandi * @see    html_msgarea
131f3f0262cSandi */
132f3f0262cSandifunction msg($message,$lvl=0){
133f3f0262cSandi  global $MSG;
134f3f0262cSandi  $errors[-1] = 'error';
135f3f0262cSandi  $errors[0]  = 'info';
136f3f0262cSandi  $errors[1]  = 'success';
137f3f0262cSandi
138cc20ad51Sandi  if(!headers_sent()){
139f3f0262cSandi    if(!isset($MSG)) $MSG = array();
140f3f0262cSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
1410396becbSandi  }else{
1420396becbSandi    $MSG = array();
1430396becbSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
144f62ea8a1Sandi    if(function_exists('html_msgarea')){
1450396becbSandi      html_msgarea();
146f62ea8a1Sandi    }else{
147f62ea8a1Sandi      print "ERROR($lvl) $message";
148f62ea8a1Sandi    }
1490396becbSandi  }
150f3f0262cSandi}
151f3f0262cSandi
152f3f0262cSandi/**
15315fae107Sandi * This builds the breadcrumb trail and returns it as array
15415fae107Sandi *
15515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
156f3f0262cSandi */
157f3f0262cSandifunction breadcrumbs(){
1588746e727Sandi  // we prepare the breadcrumbs early for quick session closing
1598746e727Sandi  static $crumbs = null;
1608746e727Sandi  if($crumbs != null) return $crumbs;
1618746e727Sandi
162f3f0262cSandi  global $ID;
163f3f0262cSandi  global $ACT;
164f3f0262cSandi  global $conf;
165f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
166f3f0262cSandi
167f3f0262cSandi  //first visit?
168f3f0262cSandi  if (!is_array($crumbs)){
169f3f0262cSandi    $crumbs = array();
170f3f0262cSandi  }
171f3f0262cSandi  //we only save on show and existing wiki documents
172a77f5846Sjan  $file = wikiFN($ID);
173a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
174f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
175f3f0262cSandi    return $crumbs;
176f3f0262cSandi  }
177a77f5846Sjan
178a77f5846Sjan  // page names
179a77f5846Sjan  $name = noNS($ID);
180a77f5846Sjan  if ($conf['useheading']) {
181a77f5846Sjan    // get page title
182bb0a59d4Sjan    $title = p_get_first_heading($ID);
183a77f5846Sjan    if ($title) {
184a77f5846Sjan      $name = $title;
185a77f5846Sjan    }
186a77f5846Sjan  }
187a77f5846Sjan
188f3f0262cSandi  //remove ID from array
189a77f5846Sjan  if (isset($crumbs[$ID])) {
190a77f5846Sjan    unset($crumbs[$ID]);
191f3f0262cSandi  }
192f3f0262cSandi
193f3f0262cSandi  //add to array
194a77f5846Sjan  $crumbs[$ID] = $name;
195f3f0262cSandi  //reduce size
196f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
197f3f0262cSandi    array_shift($crumbs);
198f3f0262cSandi  }
199f3f0262cSandi  //save to session
200f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
201f3f0262cSandi  return $crumbs;
202f3f0262cSandi}
203f3f0262cSandi
204f3f0262cSandi/**
20515fae107Sandi * Filter for page IDs
20615fae107Sandi *
207f3f0262cSandi * This is run on a ID before it is outputted somewhere
208f3f0262cSandi * currently used to replace the colon with something else
209f3f0262cSandi * on Windows systems and to have proper URL encoding
21015fae107Sandi *
21149c713a3Sandi * Urlencoding is ommitted when the second parameter is false
21249c713a3Sandi *
21315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
214f3f0262cSandi */
21549c713a3Sandifunction idfilter($id,$ue=true){
216f3f0262cSandi  global $conf;
217f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
218f3f0262cSandi    $id = strtr($id,':','/');
219f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
220f3f0262cSandi      $conf['userewrite']) {
221f3f0262cSandi    $id = strtr($id,':',';');
222f3f0262cSandi  }
22349c713a3Sandi  if($ue){
224f3f0262cSandi    $id = urlencode($id);
225f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
226f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
22749c713a3Sandi  }
228f3f0262cSandi  return $id;
229f3f0262cSandi}
230f3f0262cSandi
231f3f0262cSandi/**
232ed7b5f09Sandi * This builds a link to a wikipage
23315fae107Sandi *
2346c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2356c7843b5Sandi * given in $more
2366c7843b5Sandi *
23715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
238f3f0262cSandi */
239ed7b5f09Sandifunction wl($id='',$more='',$abs=false){
240f3f0262cSandi  global $conf;
2416de3759aSAndreas Gohr  if(is_array($more)){
2426de3759aSAndreas Gohr    $more = buildURLparams($more);
2436de3759aSAndreas Gohr  }else{
244f3f0262cSandi    $more = str_replace(',','&amp;',$more);
2456de3759aSAndreas Gohr  }
246f3f0262cSandi
247f3f0262cSandi  $id    = idfilter($id);
248ed7b5f09Sandi  if($abs){
249ed7b5f09Sandi    $xlink = DOKU_URL;
250ed7b5f09Sandi  }else{
251ed7b5f09Sandi    $xlink = DOKU_BASE;
252ed7b5f09Sandi  }
253f3f0262cSandi
2546c7843b5Sandi  if($conf['userewrite'] == 2){
2556c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
2566c7843b5Sandi    if($more) $xlink .= '?'.$more;
2576c7843b5Sandi  }elseif($conf['userewrite']){
258f3f0262cSandi    $xlink .= $id;
259f3f0262cSandi    if($more) $xlink .= '?'.$more;
2606c7843b5Sandi  }else{
2616c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
2626c7843b5Sandi    if($more) $xlink .= '&amp;'.$more;
263f3f0262cSandi  }
264f3f0262cSandi
265f3f0262cSandi  return $xlink;
266f3f0262cSandi}
267f3f0262cSandi
268f3f0262cSandi/**
2696de3759aSAndreas Gohr * Build a link to a media file
2706de3759aSAndreas Gohr *
2716de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
2726de3759aSAndreas Gohr */
2736de3759aSAndreas Gohrfunction ml($id='',$more='',$direct=true){
2746de3759aSAndreas Gohr  global $conf;
2756de3759aSAndreas Gohr  if(is_array($more)){
2766de3759aSAndreas Gohr    $more = buildURLparams($more);
2776de3759aSAndreas Gohr  }else{
2786de3759aSAndreas Gohr    $more = str_replace(',','&amp;',$more);
2796de3759aSAndreas Gohr  }
2806de3759aSAndreas Gohr
2816de3759aSAndreas Gohr  $xlink = DOKU_BASE;
2826de3759aSAndreas Gohr
2836de3759aSAndreas Gohr  // external URLs are always direct without rewriting
2846de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
2856de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
2866de3759aSAndreas Gohr    if($more){
2876de3759aSAndreas Gohr      $xlink .= '?'.$more;
28848665d38SAndreas Gohr      $xlink .= '&amp;media='.urlencode($id);
2896de3759aSAndreas Gohr    }else{
29048665d38SAndreas Gohr      $xlink .= '?media='.urlencode($id);
2916de3759aSAndreas Gohr    }
2926de3759aSAndreas Gohr    return $xlink;
2936de3759aSAndreas Gohr  }
2946de3759aSAndreas Gohr
2956de3759aSAndreas Gohr  $id = idfilter($id);
2966de3759aSAndreas Gohr
2976de3759aSAndreas Gohr  // decide on scriptname
2986de3759aSAndreas Gohr  if($direct){
2996de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3006de3759aSAndreas Gohr      $script = '_media';
3016de3759aSAndreas Gohr    }else{
3026de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3036de3759aSAndreas Gohr    }
3046de3759aSAndreas Gohr  }else{
3056de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3066de3759aSAndreas Gohr      $script = '_detail';
3076de3759aSAndreas Gohr    }else{
3086de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3096de3759aSAndreas Gohr    }
3106de3759aSAndreas Gohr  }
3116de3759aSAndreas Gohr
3126de3759aSAndreas Gohr  // build URL based on rewrite mode
3136de3759aSAndreas Gohr   if($conf['userewrite']){
3146de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3156de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3166de3759aSAndreas Gohr   }else{
3176de3759aSAndreas Gohr     if($more){
318a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
3196de3759aSAndreas Gohr       $xlink .= '&amp;media='.$id;
3206de3759aSAndreas Gohr     }else{
321a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
3226de3759aSAndreas Gohr     }
3236de3759aSAndreas Gohr   }
3246de3759aSAndreas Gohr
3256de3759aSAndreas Gohr  return $xlink;
3266de3759aSAndreas Gohr}
3276de3759aSAndreas Gohr
3286de3759aSAndreas Gohr
3296de3759aSAndreas Gohr
3306de3759aSAndreas Gohr/**
331f3f0262cSandi * Just builds a link to a script
33215fae107Sandi *
333ed7b5f09Sandi * @todo   maybe obsolete
33415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
335f3f0262cSandi */
336f3f0262cSandifunction script($script='doku.php'){
337ed7b5f09Sandi#  $link = getBaseURL();
338ed7b5f09Sandi#  $link .= $script;
339ed7b5f09Sandi#  return $link;
340ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
341f3f0262cSandi}
342f3f0262cSandi
343f3f0262cSandi/**
34415fae107Sandi * Spamcheck against wordlist
34515fae107Sandi *
346f3f0262cSandi * Checks the wikitext against a list of blocked expressions
347f3f0262cSandi * returns true if the text contains any bad words
34815fae107Sandi *
34915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
350f3f0262cSandi */
351f3f0262cSandifunction checkwordblock(){
352f3f0262cSandi  global $TEXT;
353f3f0262cSandi  global $conf;
354f3f0262cSandi
355f3f0262cSandi  if(!$conf['usewordblock']) return false;
356f3f0262cSandi
357e7cb32dcSAndreas Gohr  $blockfile = file(DOKU_CONF.'wordblock.conf');
3583e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
3593e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
3603e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
3613e2965d7Sandi    //backreferences are used - the maximum is 99
3623e2965d7Sandi    //this is very bad performancewise and may even be too high still
3633e2965d7Sandi    $chunksize = 40;
3643e2965d7Sandi  }else{
365703f6fdeSandi    //read file in chunks of 600 - this should work around the
3663e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
3673e2965d7Sandi    $chunksize = 600;
3683e2965d7Sandi  }
3693e2965d7Sandi  while($blocks = array_splice($blockfile,0,$chunksize)){
370f3f0262cSandi    $re = array();
371f3f0262cSandi    #build regexp from blocks
372f3f0262cSandi    foreach($blocks as $block){
373f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
374f3f0262cSandi      $block = trim($block);
375f3f0262cSandi      if(empty($block)) continue;
376f3f0262cSandi      $re[]  = $block;
377f3f0262cSandi    }
378f3f0262cSandi    if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true;
379703f6fdeSandi  }
380f3f0262cSandi  return false;
381f3f0262cSandi}
382f3f0262cSandi
383f3f0262cSandi/**
38415fae107Sandi * Return the IP of the client
38515fae107Sandi *
38615fae107Sandi * Honours X-Forwarded-For Proxy Headers
38715fae107Sandi *
38815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
389f3f0262cSandi */
390f3f0262cSandifunction clientIP(){
391f3f0262cSandi  $my = $_SERVER['REMOTE_ADDR'];
392f3f0262cSandi  if($_SERVER['HTTP_X_FORWARDED_FOR']){
393f3f0262cSandi    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
394f3f0262cSandi  }
395f3f0262cSandi  return $my;
396f3f0262cSandi}
397f3f0262cSandi
398f3f0262cSandi/**
39915fae107Sandi * Checks if a given page is currently locked.
40015fae107Sandi *
401f3f0262cSandi * removes stale lockfiles
40215fae107Sandi *
40315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
404f3f0262cSandi */
405f3f0262cSandifunction checklock($id){
406f3f0262cSandi  global $conf;
407f3f0262cSandi  $lock = wikiFN($id).'.lock';
408f3f0262cSandi
409f3f0262cSandi  //no lockfile
410f3f0262cSandi  if(!@file_exists($lock)) return false;
411f3f0262cSandi
412f3f0262cSandi  //lockfile expired
413f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
414f3f0262cSandi    unlink($lock);
415f3f0262cSandi    return false;
416f3f0262cSandi  }
417f3f0262cSandi
418f3f0262cSandi  //my own lock
419f3f0262cSandi  $ip = io_readFile($lock);
420f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
421f3f0262cSandi    return false;
422f3f0262cSandi  }
423f3f0262cSandi
424f3f0262cSandi  return $ip;
425f3f0262cSandi}
426f3f0262cSandi
427f3f0262cSandi/**
42815fae107Sandi * Lock a page for editing
42915fae107Sandi *
43015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
431f3f0262cSandi */
432f3f0262cSandifunction lock($id){
433f3f0262cSandi  $lock = wikiFN($id).'.lock';
434f3f0262cSandi  if($_SERVER['REMOTE_USER']){
435f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
436f3f0262cSandi  }else{
437f3f0262cSandi    io_saveFile($lock,clientIP());
438f3f0262cSandi  }
439f3f0262cSandi}
440f3f0262cSandi
441f3f0262cSandi/**
44215fae107Sandi * Unlock a page if it was locked by the user
443f3f0262cSandi *
44415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
44515fae107Sandi * @return bool true if a lock was removed
446f3f0262cSandi */
447f3f0262cSandifunction unlock($id){
448f3f0262cSandi  $lock = wikiFN($id).'.lock';
449f3f0262cSandi  if(@file_exists($lock)){
450f3f0262cSandi    $ip = io_readFile($lock);
451f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
452f3f0262cSandi      @unlink($lock);
453f3f0262cSandi      return true;
454f3f0262cSandi    }
455f3f0262cSandi  }
456f3f0262cSandi  return false;
457f3f0262cSandi}
458f3f0262cSandi
459f3f0262cSandi/**
460f3f0262cSandi * convert line ending to unix format
461f3f0262cSandi *
46215fae107Sandi * @see    formText() for 2crlf conversion
46315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
464f3f0262cSandi */
465f3f0262cSandifunction cleanText($text){
466f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
467f3f0262cSandi  return $text;
468f3f0262cSandi}
469f3f0262cSandi
470f3f0262cSandi/**
471f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
472f3f0262cSandi * It also converts line endings to Windows format which is
473f3f0262cSandi * pseudo standard for webforms.
474f3f0262cSandi *
47515fae107Sandi * @see    cleanText() for 2unix conversion
47615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
477f3f0262cSandi */
478f3f0262cSandifunction formText($text){
479f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
480f3f0262cSandi  return htmlspecialchars($text);
481f3f0262cSandi}
482f3f0262cSandi
483f3f0262cSandi/**
48415fae107Sandi * Returns the specified local text in raw format
48515fae107Sandi *
48615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
487f3f0262cSandi */
488f3f0262cSandifunction rawLocale($id){
489f3f0262cSandi  return io_readFile(localeFN($id));
490f3f0262cSandi}
491f3f0262cSandi
492f3f0262cSandi/**
493f3f0262cSandi * Returns the raw WikiText
49415fae107Sandi *
49515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
496f3f0262cSandi */
497f3f0262cSandifunction rawWiki($id,$rev=''){
498f3f0262cSandi  return io_readFile(wikiFN($id,$rev));
499f3f0262cSandi}
500f3f0262cSandi
501f3f0262cSandi/**
5027146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
5037146cee2SAndreas Gohr *
5047146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5057146cee2SAndreas Gohr */
5067146cee2SAndreas Gohrfunction pageTemplate($id){
507a15ce62dSEsther Brunner  global $conf;
508a15ce62dSEsther Brunner  global $INFO;
509a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
510a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
511a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
512a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
513a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
514a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
515a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
516a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
517a15ce62dSEsther Brunner  return $tpl;
5187146cee2SAndreas Gohr}
5197146cee2SAndreas Gohr
5207146cee2SAndreas Gohr
5217146cee2SAndreas Gohr/**
52215fae107Sandi * Returns the raw Wiki Text in three slices.
52315fae107Sandi *
52415fae107Sandi * The range parameter needs to have the form "from-to"
52515cfe303Sandi * and gives the range of the section in bytes - no
52615cfe303Sandi * UTF-8 awareness is needed.
527f3f0262cSandi * The returned order is prefix, section and suffix.
52815fae107Sandi *
52915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
530f3f0262cSandi */
531f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
532f3f0262cSandi  list($from,$to) = split('-',$range,2);
533f3f0262cSandi  $text = io_readFile(wikiFN($id,$rev));
534f3f0262cSandi  if(!$from) $from = 0;
535c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
536f3f0262cSandi
53715cfe303Sandi  $slices[0] = substr($text,0,$from-1);
53815cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
53915cfe303Sandi  $slices[2] = substr($text,$to);
540f3f0262cSandi
541f3f0262cSandi  return $slices;
542f3f0262cSandi}
543f3f0262cSandi
544f3f0262cSandi/**
54515fae107Sandi * Joins wiki text slices
54615fae107Sandi *
547f3f0262cSandi * function to join the text slices with correct lineendings again.
548f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
549f3f0262cSandi * lines between sections if needed (used on saving).
55015fae107Sandi *
55115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
552f3f0262cSandi */
553f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
554f3f0262cSandi
555f3f0262cSandi  if($pretty){
556f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
557f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
558f3f0262cSandi  }
559f3f0262cSandi
560f3f0262cSandi  if($pre) $pre .= "\n";
561f3f0262cSandi  if($suf) $text .= "\n";
562f3f0262cSandi  return $pre.$text.$suf;
563f3f0262cSandi}
564f3f0262cSandi
565f3f0262cSandi/**
56615fae107Sandi * print debug messages
56715fae107Sandi *
568f3f0262cSandi * little function to print the content of a var
56915fae107Sandi *
57015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
571f3f0262cSandi */
572f3f0262cSandifunction dbg($msg,$hidden=false){
573f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
574f3f0262cSandi  print_r($msg);
575f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
576f3f0262cSandi}
577f3f0262cSandi
578f3f0262cSandi/**
579f3f0262cSandi * Add's an entry to the changelog
58015fae107Sandi *
58115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
582f3f0262cSandi */
583652610a2Sandifunction addLogEntry($date,$id,$summary=""){
584f3f0262cSandi  global $conf;
585c1049928Sandi
586c1049928Sandi  if(!@is_writable($conf['changelog'])){
587c1049928Sandi    msg($conf['changelog'].' is not writable!',-1);
588c1049928Sandi    return;
589c1049928Sandi  }
590c1049928Sandi
591652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
592f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
593f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
594f3f0262cSandi
595f3f0262cSandi  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
596dbb00abcSEsther Brunner  io_saveFile($conf['changelog'],$logline,true);
597f3f0262cSandi}
598f3f0262cSandi
599f3f0262cSandi/**
600f3f0262cSandi * returns an array of recently changed files using the
601f3f0262cSandi * changelog
6025749f1ceSmatthiasgrimm * first   : first entry in array returned
603a39955b0Smatthiasgrimm * num     : return 'num' entries
60415fae107Sandi *
60515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
606f3f0262cSandi */
6078f1d587cSEsther Brunnerfunction getRecents($first,$num,$incdel=false,$ns='',$subNS=true){
608f3f0262cSandi  global $conf;
609f3f0262cSandi  $recent = array();
6105749f1ceSmatthiasgrimm  $names  = array();
6115749f1ceSmatthiasgrimm
6125749f1ceSmatthiasgrimm  if(!$num)
6135749f1ceSmatthiasgrimm    return $recent;
614f3f0262cSandi
615c1049928Sandi  if(!@is_readable($conf['changelog'])){
616c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
617c1049928Sandi    return $recent;
618c1049928Sandi  }
619c1049928Sandi
620f3f0262cSandi  $loglines = file($conf['changelog']);
621f3f0262cSandi  rsort($loglines); //reverse sort on timestamp
622f3f0262cSandi
623f3f0262cSandi  foreach ($loglines as $line){
624f3f0262cSandi    $line = rtrim($line);        //remove newline
625f3f0262cSandi    if(empty($line)) continue;   //skip empty lines
626f3f0262cSandi    $info = split("\t",$line);   //split into parts
627f3f0262cSandi    //add id if not in yet and file still exists and is allowed to read
6285749f1ceSmatthiasgrimm    if(!$names[$info[2]] &&
629f3f0262cSandi       (@file_exists(wikiFN($info[2])) || $incdel) &&
630f3f0262cSandi       (auth_quickaclcheck($info[2]) >= AUTH_READ)
631f3f0262cSandi      ){
632dbb00abcSEsther Brunner      // filter namespace
633dbb00abcSEsther Brunner      if (($ns) && (strpos($info[2],$ns.':') !== 0)) continue;
634dbb00abcSEsther Brunner
6358f1d587cSEsther Brunner      // exclude subnamespaces
6368f1d587cSEsther Brunner      if ((!$subNS) && (getNS($info[2]) != $ns)) continue;
6378f1d587cSEsther Brunner
6385749f1ceSmatthiasgrimm      $names[$info[2]] = 1;
6395749f1ceSmatthiasgrimm      if(--$first >= 0) continue;  /* skip "first" entries */
6405749f1ceSmatthiasgrimm
641f3f0262cSandi      $recent[$info[2]]['date'] = $info[0];
642f3f0262cSandi      $recent[$info[2]]['ip']   = $info[1];
643f3f0262cSandi      $recent[$info[2]]['user'] = $info[3];
644f3f0262cSandi      $recent[$info[2]]['sum']  = $info[4];
645f3f0262cSandi      $recent[$info[2]]['del']  = !@file_exists(wikiFN($info[2]));
646f3f0262cSandi    }
6475749f1ceSmatthiasgrimm    if(count($recent) >= $num){
648f3f0262cSandi      break; //finish if enough items found
649f3f0262cSandi    }
650f3f0262cSandi  }
651f3f0262cSandi  return $recent;
652f3f0262cSandi}
653f3f0262cSandi
654f3f0262cSandi/**
655652610a2Sandi * gets additonal informations for a certain pagerevison
656652610a2Sandi * from the changelog
657652610a2Sandi *
658652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
659652610a2Sandi */
660652610a2Sandifunction getRevisionInfo($id,$rev){
661652610a2Sandi  global $conf;
662258641c6Sandi
663258641c6Sandi  if(!$rev) return(null);
664258641c6Sandi
665c1049928Sandi  $info = array();
666c1049928Sandi  if(!@is_readable($conf['changelog'])){
667c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
668c1049928Sandi    return $recent;
669c1049928Sandi  }
670652610a2Sandi  $loglines = file($conf['changelog']);
671652610a2Sandi  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
672dc42ff59Sandi  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
673652610a2Sandi  $line = split("\t",$loglines[0]);
674652610a2Sandi  $info['date'] = $line[0];
675652610a2Sandi  $info['ip']   = $line[1];
676652610a2Sandi  $info['user'] = $line[3];
677652610a2Sandi  $info['sum']   = $line[4];
678652610a2Sandi  return $info;
679652610a2Sandi}
680652610a2Sandi
681652610a2Sandi/**
682f3f0262cSandi * Saves a wikitext by calling io_saveFile
68315fae107Sandi *
68415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
685f3f0262cSandi */
686f3f0262cSandifunction saveWikiText($id,$text,$summary){
687f3f0262cSandi  global $conf;
688f3f0262cSandi  global $lang;
689f3f0262cSandi  umask($conf['umask']);
690f3f0262cSandi  // ignore if no changes were made
691f3f0262cSandi  if($text == rawWiki($id,'')){
692f3f0262cSandi    return;
693f3f0262cSandi  }
694f3f0262cSandi
695f3f0262cSandi  $file = wikiFN($id);
696f3f0262cSandi  $old  = saveOldRevision($id);
697f3f0262cSandi
698f3f0262cSandi  if (empty($text)){
699e1f3d9e1SEsther Brunner    // remove empty file
700f3f0262cSandi    @unlink($file);
701e1f3d9e1SEsther Brunner    // remove any meta info
702e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
703e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
704e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
705b158d625SSteven Danz    }
706f3f0262cSandi    $del = true;
7073ce054b3Sandi    //autoset summary on deletion
7083ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
70953d6ccfeSandi    //remove empty namespaces
71053d6ccfeSandi    io_sweepNS($id);
711f3f0262cSandi  }else{
712f3f0262cSandi    // save file (datadir is created in io_saveFile)
713f3f0262cSandi    io_saveFile($file,$text);
714f3f0262cSandi    $del = false;
715f3f0262cSandi  }
716f3f0262cSandi
717652610a2Sandi  addLogEntry(@filemtime($file),$id,$summary);
71826a0801fSAndreas Gohr  // send notify mails
71926a0801fSAndreas Gohr  notify($id,'admin',$old,$summary);
72026a0801fSAndreas Gohr  notify($id,'subscribers',$old,$summary);
721f3f0262cSandi
722f3f0262cSandi  //purge cache on add by updating the purgefile
723f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
72498407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
725f3f0262cSandi  }
726f3f0262cSandi}
727f3f0262cSandi
728f3f0262cSandi/**
729f3f0262cSandi * moves the current version to the attic and returns its
730f3f0262cSandi * revision date
73115fae107Sandi *
73215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
733f3f0262cSandi */
734f3f0262cSandifunction saveOldRevision($id){
735f3f0262cSandi	global $conf;
736f3f0262cSandi  umask($conf['umask']);
737f3f0262cSandi  $oldf = wikiFN($id);
738f3f0262cSandi  if(!@file_exists($oldf)) return '';
739f3f0262cSandi  $date = filemtime($oldf);
740f3f0262cSandi  $newf = wikiFN($id,$date);
741f3f0262cSandi  if(substr($newf,-3)=='.gz'){
742f3f0262cSandi    io_saveFile($newf,rawWiki($id));
743f3f0262cSandi  }else{
744f3f0262cSandi    io_makeFileDir($newf);
745f3f0262cSandi    copy($oldf, $newf);
746f3f0262cSandi  }
747f3f0262cSandi  return $date;
748f3f0262cSandi}
749f3f0262cSandi
750f3f0262cSandi/**
75126a0801fSAndreas Gohr * Sends a notify mail on page change
75226a0801fSAndreas Gohr *
75326a0801fSAndreas Gohr * @param  string $id       The changed page
75426a0801fSAndreas Gohr * @param  string $who      Who to notify (admin|subscribers)
75526a0801fSAndreas Gohr * @param  int    $rev      Old page revision
75626a0801fSAndreas Gohr * @param  string $summary  What changed
75715fae107Sandi *
75815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
759f3f0262cSandi */
76026a0801fSAndreas Gohrfunction notify($id,$who,$rev='',$summary=''){
761f3f0262cSandi  global $lang;
762f3f0262cSandi  global $conf;
763b158d625SSteven Danz
76426a0801fSAndreas Gohr  // decide if there is something to do
76526a0801fSAndreas Gohr  if($who == 'admin'){
76626a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
767f3f0262cSandi    $text = rawLocale('mailtext');
76826a0801fSAndreas Gohr    $to   = $conf['notify'];
76926a0801fSAndreas Gohr    $bcc  = '';
77026a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
77126a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
77226a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
77326a0801fSAndreas Gohr    if(empty($bcc)) return;
77426a0801fSAndreas Gohr    $to   = '';
77526a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
77626a0801fSAndreas Gohr  }else{
77726a0801fSAndreas Gohr    return; //just to be safe
77826a0801fSAndreas Gohr  }
77926a0801fSAndreas Gohr
780f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
781f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
782f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
783f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
784ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
78526a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
78626a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
787ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
788f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
7897a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
790f3f0262cSandi
791f3f0262cSandi  if($rev){
792f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
793ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
794f3f0262cSandi    require_once("inc/DifferenceEngine.php");
795f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
796f3f0262cSandi                    split("\n",rawWiki($id)));
797f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
798f3f0262cSandi    $diff    = $dformat->format($df);
799f3f0262cSandi  }else{
800f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
801f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
802f3f0262cSandi    $diff = rawWiki($id);
803f3f0262cSandi  }
804f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
805241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
806f3f0262cSandi
80726a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
808f3f0262cSandi}
809f3f0262cSandi
81015fae107Sandi/**
81115fae107Sandi * Return a list of available page revisons
81215fae107Sandi *
81315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
81415fae107Sandi */
815f3f0262cSandifunction getRevisions($id){
816f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
817f3f0262cSandi  $revs = array();
818f3f0262cSandi  $clid = cleanID($id);
819f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
820493a6929SKobaYY  $clid = utf8_encodeFN($clid);
821f3f0262cSandi
822f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
823f3f0262cSandi    while (($file = readdir($dh)) !== false) {
824f3f0262cSandi      if (is_dir($revd.'/'.$file)) continue;
825f3f0262cSandi      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
826f3f0262cSandi        $revs[]=$match[1];
827f3f0262cSandi      }
828f3f0262cSandi    }
829f3f0262cSandi    closedir($dh);
830f3f0262cSandi  }
831f3f0262cSandi  rsort($revs);
832f3f0262cSandi  return $revs;
833f3f0262cSandi}
834f3f0262cSandi
835f3f0262cSandi/**
836f3f0262cSandi * extracts the query from a google referer
83715fae107Sandi *
8386b13307fSandi * @todo   should be more generic and support yahoo et al
83915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
840f3f0262cSandi */
841f3f0262cSandifunction getGoogleQuery(){
842f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
8435c3f206fSandi  if(!$url) return '';
844f3f0262cSandi
845f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
846f3f0262cSandi  $query = array();
847f3f0262cSandi  parse_str($url['query'],$query);
848f3f0262cSandi
849f3f0262cSandi  return $query['q'];
850f3f0262cSandi}
851f3f0262cSandi
852f3f0262cSandi/**
85315fae107Sandi * Try to set correct locale
85415fae107Sandi *
855095bfd5cSandi * @deprecated No longer used
85615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
857f3f0262cSandi */
858f3f0262cSandifunction setCorrectLocale(){
859f3f0262cSandi  global $conf;
860f3f0262cSandi  global $lang;
861f3f0262cSandi
862f3f0262cSandi  $enc = strtoupper($lang['encoding']);
863f3f0262cSandi  foreach ($lang['locales'] as $loc){
864f3f0262cSandi    //try locale
865f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
866f3f0262cSandi    //try loceale with encoding
867f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
868f3f0262cSandi  }
869f3f0262cSandi  //still here? try to set from environment
870f3f0262cSandi  @setlocale(LC_ALL,"");
871f3f0262cSandi}
872f3f0262cSandi
873f3f0262cSandi/**
874f3f0262cSandi * Return the human readable size of a file
875f3f0262cSandi *
876f3f0262cSandi * @param       int    $size   A file size
877f3f0262cSandi * @param       int    $dec    A number of decimal places
878f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
879f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
880f3f0262cSandi * @version     1.0.0
881f3f0262cSandi */
882f31d5b73Sandifunction filesize_h($size, $dec = 1){
883f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
884f3f0262cSandi  $count = count($sizes);
885f3f0262cSandi  $i = 0;
886f3f0262cSandi
887f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
888f3f0262cSandi    $size /= 1024;
889f3f0262cSandi    $i++;
890f3f0262cSandi  }
891f3f0262cSandi
892f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
893f3f0262cSandi}
894f3f0262cSandi
89515fae107Sandi/**
896*00a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
897*00a7b5adSEsther Brunner *
898*00a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
899*00a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
900*00a7b5adSEsther Brunner */
901*00a7b5adSEsther Brunnerfunction obfuscate($email) {
902*00a7b5adSEsther Brunner  global $conf;
903*00a7b5adSEsther Brunner
904*00a7b5adSEsther Brunner  switch ($conf['mailguard']) {
905*00a7b5adSEsther Brunner    case 'visible' :
906*00a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
907*00a7b5adSEsther Brunner      return strtr($email, $obfuscate);
908*00a7b5adSEsther Brunner
909*00a7b5adSEsther Brunner    case 'hex' :
910*00a7b5adSEsther Brunner      $encode = '';
911*00a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
912*00a7b5adSEsther Brunner      return $encode;
913*00a7b5adSEsther Brunner
914*00a7b5adSEsther Brunner    case 'none' :
915*00a7b5adSEsther Brunner    default :
916*00a7b5adSEsther Brunner      return $email;
917*00a7b5adSEsther Brunner  }
918*00a7b5adSEsther Brunner}
919*00a7b5adSEsther Brunner
920*00a7b5adSEsther Brunner/**
921dc57ef04Sandi * Return DokuWikis version
92215fae107Sandi *
92315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
92415fae107Sandi */
925f31d5b73Sandifunction getVersion(){
926f31d5b73Sandi  //import version string
927f31d5b73Sandi  if(@file_exists('VERSION')){
928f31d5b73Sandi    //official release
929ec052310Sandi    return 'Release '.trim(io_readfile('VERSION'));
930f31d5b73Sandi  }elseif(is_dir('_darcs')){
931f31d5b73Sandi    //darcs checkout
932f31d5b73Sandi    $inv = file('_darcs/inventory');
933f31d5b73Sandi    $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv);
934f31d5b73Sandi    $cur = array_pop($inv);
935f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
936f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
937f31d5b73Sandi  }else{
938f31d5b73Sandi    return 'snapshot?';
939f31d5b73Sandi  }
940f31d5b73Sandi}
941f31d5b73Sandi
942f31d5b73Sandi/**
943f31d5b73Sandi * Run a few sanity checks
944f31d5b73Sandi *
945f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
946f31d5b73Sandi */
947f3f0262cSandifunction check(){
948f3f0262cSandi  global $conf;
949f3f0262cSandi  global $INFO;
950f3f0262cSandi
951f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
952f31d5b73Sandi
95349022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
95449022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
95549022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
95649022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
95749022a38Sandi  }else{
95849022a38Sandi    msg('PHP version '.phpversion(),1);
95949022a38Sandi  }
96049022a38Sandi
961f3f0262cSandi  if(is_writable($conf['changelog'])){
962f3f0262cSandi    msg('Changelog is writable',1);
963f3f0262cSandi  }else{
964f3f0262cSandi    msg('Changelog is not writable',-1);
965f3f0262cSandi  }
966f3f0262cSandi
967f3f0262cSandi  if(is_writable($conf['datadir'])){
968f3f0262cSandi    msg('Datadir is writable',1);
969f3f0262cSandi  }else{
970f3f0262cSandi    msg('Datadir is not writable',-1);
971f3f0262cSandi  }
972f3f0262cSandi
973f3f0262cSandi  if(is_writable($conf['olddir'])){
974f3f0262cSandi    msg('Attic is writable',1);
975f3f0262cSandi  }else{
976f3f0262cSandi    msg('Attic is not writable',-1);
977f3f0262cSandi  }
978f3f0262cSandi
979f3f0262cSandi  if(is_writable($conf['mediadir'])){
980f3f0262cSandi    msg('Mediadir is writable',1);
981f3f0262cSandi  }else{
982f3f0262cSandi    msg('Mediadir is not writable',-1);
983f3f0262cSandi  }
984f3f0262cSandi
98598407a7aSandi  if(is_writable($conf['cachedir'])){
98698407a7aSandi    msg('Cachedir is writable',1);
98798407a7aSandi  }else{
98898407a7aSandi    msg('Cachedir is not writable',-1);
98998407a7aSandi  }
99098407a7aSandi
991e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
9928c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
993f3f0262cSandi  }else{
9948c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
995f3f0262cSandi  }
99693a9e835Sandi
99793a9e835Sandi  if(function_exists('mb_strpos')){
99893a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
99993a9e835Sandi      msg('mb_string extension is available but will not be used',0);
100093a9e835Sandi    }else{
100193a9e835Sandi      msg('mb_string extension is available and will be used',1);
100293a9e835Sandi    }
100393a9e835Sandi  }else{
100493a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
100593a9e835Sandi  }
1006f3f0262cSandi
1007f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1008f3f0262cSandi
1009f3f0262cSandi  if(is_writable($INFO['filepath'])){
1010f3f0262cSandi    msg('The current page is writable by the webserver',0);
1011f3f0262cSandi  }else{
1012f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1013f3f0262cSandi  }
1014f3f0262cSandi
1015f3f0262cSandi  if($INFO['writable']){
1016f3f0262cSandi    msg('The current page is writable by you',0);
1017f3f0262cSandi  }else{
1018f3f0262cSandi    msg('The current page is not writable you',0);
1019f3f0262cSandi  }
1020f3f0262cSandi}
1021340756e4Sandi
1022b158d625SSteven Danz/**
1023b158d625SSteven Danz * Let us know if a user is tracking a page
1024b158d625SSteven Danz *
10251380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1026b158d625SSteven Danz */
10271380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
10281380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
10291380fc45SAndreas Gohr  if (@file_exists($file)) {
1030b158d625SSteven Danz    $mlist = file($file);
10311380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
10321380fc45SAndreas Gohr    return is_int($pos);
1033b158d625SSteven Danz  }
10341380fc45SAndreas Gohr
1035b158d625SSteven Danz  return false;
1036b158d625SSteven Danz}
1037340756e4Sandi
1038f9eb5648Ssteven-danz/**
1039f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1040f9eb5648Ssteven-danz * users subscribed to a page
1041f9eb5648Ssteven-danz *
104226a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1043f9eb5648Ssteven-danz */
1044f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1045f9eb5648Ssteven-danz  global $conf;
1046f9eb5648Ssteven-danz
1047f9eb5648Ssteven-danz  $emails = '';
1048f9eb5648Ssteven-danz
104926a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
105026a0801fSAndreas Gohr
1051f9eb5648Ssteven-danz  $mlist = array();
1052f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1053f9eb5648Ssteven-danz  if (file_exists($file)) {
1054f9eb5648Ssteven-danz    $mlist = file($file);
1055f9eb5648Ssteven-danz  }
1056f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1057f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1058f9eb5648Ssteven-danz      $who = rtrim($who);
1059f9eb5648Ssteven-danz      $info = auth_getUserData($who);
1060f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1061f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1062f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1063f9eb5648Ssteven-danz          if (empty($emails)) {
1064f9eb5648Ssteven-danz            $emails = $info['mail'];
1065f9eb5648Ssteven-danz          } else {
1066f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1067f9eb5648Ssteven-danz          }
1068f9eb5648Ssteven-danz        }
1069f9eb5648Ssteven-danz      }
1070f9eb5648Ssteven-danz    }
1071f9eb5648Ssteven-danz  }
1072f9eb5648Ssteven-danz
1073f9eb5648Ssteven-danz  return $emails;
1074f9eb5648Ssteven-danz}
1075f9eb5648Ssteven-danz
107689541d4bSAndreas Gohr/**
107789541d4bSAndreas Gohr * Removes quoting backslashes
107889541d4bSAndreas Gohr *
107989541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
108089541d4bSAndreas Gohr */
108189541d4bSAndreas Gohrfunction unslash($string,$char="'"){
108289541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
108389541d4bSAndreas Gohr}
108489541d4bSAndreas Gohr
1085340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1086