xref: /dokuwiki/inc/common.php (revision a99d323627999434881d8f05a493ebdbfaa4eadf)
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){
318*a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
3196de3759aSAndreas Gohr       $xlink .= '&amp;media='.$id;
3206de3759aSAndreas Gohr     }else{
321*a99d3236SEsther 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){
5077146cee2SAndreas Gohr  return io_readFile(dirname(wikiFN($id)).'/_template.txt');
5087146cee2SAndreas Gohr}
5097146cee2SAndreas Gohr
5107146cee2SAndreas Gohr
5117146cee2SAndreas Gohr/**
51215fae107Sandi * Returns the raw Wiki Text in three slices.
51315fae107Sandi *
51415fae107Sandi * The range parameter needs to have the form "from-to"
51515cfe303Sandi * and gives the range of the section in bytes - no
51615cfe303Sandi * UTF-8 awareness is needed.
517f3f0262cSandi * The returned order is prefix, section and suffix.
51815fae107Sandi *
51915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
520f3f0262cSandi */
521f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
522f3f0262cSandi  list($from,$to) = split('-',$range,2);
523f3f0262cSandi  $text = io_readFile(wikiFN($id,$rev));
524f3f0262cSandi  if(!$from) $from = 0;
525c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
526f3f0262cSandi
52715cfe303Sandi  $slices[0] = substr($text,0,$from-1);
52815cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
52915cfe303Sandi  $slices[2] = substr($text,$to);
530f3f0262cSandi
531f3f0262cSandi  return $slices;
532f3f0262cSandi}
533f3f0262cSandi
534f3f0262cSandi/**
53515fae107Sandi * Joins wiki text slices
53615fae107Sandi *
537f3f0262cSandi * function to join the text slices with correct lineendings again.
538f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
539f3f0262cSandi * lines between sections if needed (used on saving).
54015fae107Sandi *
54115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
542f3f0262cSandi */
543f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
544f3f0262cSandi
545f3f0262cSandi  if($pretty){
546f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
547f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
548f3f0262cSandi  }
549f3f0262cSandi
550f3f0262cSandi  if($pre) $pre .= "\n";
551f3f0262cSandi  if($suf) $text .= "\n";
552f3f0262cSandi  return $pre.$text.$suf;
553f3f0262cSandi}
554f3f0262cSandi
555f3f0262cSandi/**
55615fae107Sandi * print debug messages
55715fae107Sandi *
558f3f0262cSandi * little function to print the content of a var
55915fae107Sandi *
56015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
561f3f0262cSandi */
562f3f0262cSandifunction dbg($msg,$hidden=false){
563f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
564f3f0262cSandi  print_r($msg);
565f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
566f3f0262cSandi}
567f3f0262cSandi
568f3f0262cSandi/**
569f3f0262cSandi * Add's an entry to the changelog
57015fae107Sandi *
57115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
572f3f0262cSandi */
573652610a2Sandifunction addLogEntry($date,$id,$summary=""){
574f3f0262cSandi  global $conf;
575c1049928Sandi  $id     = cleanID($id);//FIXME not needed anymore?
576c1049928Sandi
577c1049928Sandi  if(!@is_writable($conf['changelog'])){
578c1049928Sandi    msg($conf['changelog'].' is not writable!',-1);
579c1049928Sandi    return;
580c1049928Sandi  }
581c1049928Sandi
582652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
583f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
584f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
585f3f0262cSandi
586f3f0262cSandi  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
587f3f0262cSandi
588c1049928Sandi  //FIXME: use adjusted io_saveFile instead
589f3f0262cSandi  $fh = fopen($conf['changelog'],'a');
590f3f0262cSandi  if($fh){
591f3f0262cSandi    fwrite($fh,$logline);
592f3f0262cSandi    fclose($fh);
593f3f0262cSandi  }
594f3f0262cSandi}
595f3f0262cSandi
596f3f0262cSandi/**
597f3f0262cSandi * returns an array of recently changed files using the
598f3f0262cSandi * changelog
5995749f1ceSmatthiasgrimm * first   : first entry in array returned
600a39955b0Smatthiasgrimm * num     : return 'num' entries
60115fae107Sandi *
60215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
603f3f0262cSandi */
6045749f1ceSmatthiasgrimmfunction getRecents($first,$num,$incdel=false){
605f3f0262cSandi  global $conf;
606f3f0262cSandi  $recent = array();
6075749f1ceSmatthiasgrimm  $names  = array();
6085749f1ceSmatthiasgrimm
6095749f1ceSmatthiasgrimm  if(!$num)
6105749f1ceSmatthiasgrimm    return $recent;
611f3f0262cSandi
612c1049928Sandi  if(!@is_readable($conf['changelog'])){
613c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
614c1049928Sandi    return $recent;
615c1049928Sandi  }
616c1049928Sandi
617f3f0262cSandi  $loglines = file($conf['changelog']);
618f3f0262cSandi  rsort($loglines); //reverse sort on timestamp
619f3f0262cSandi
620f3f0262cSandi  foreach ($loglines as $line){
621f3f0262cSandi    $line = rtrim($line);        //remove newline
622f3f0262cSandi    if(empty($line)) continue;   //skip empty lines
623f3f0262cSandi    $info = split("\t",$line);   //split into parts
624f3f0262cSandi    //add id if not in yet and file still exists and is allowed to read
6255749f1ceSmatthiasgrimm    if(!$names[$info[2]] &&
626f3f0262cSandi       (@file_exists(wikiFN($info[2])) || $incdel) &&
627f3f0262cSandi       (auth_quickaclcheck($info[2]) >= AUTH_READ)
628f3f0262cSandi      ){
6295749f1ceSmatthiasgrimm      $names[$info[2]] = 1;
6305749f1ceSmatthiasgrimm      if(--$first >= 0) continue;  /* skip "first" entries */
6315749f1ceSmatthiasgrimm
632f3f0262cSandi      $recent[$info[2]]['date'] = $info[0];
633f3f0262cSandi      $recent[$info[2]]['ip']   = $info[1];
634f3f0262cSandi      $recent[$info[2]]['user'] = $info[3];
635f3f0262cSandi      $recent[$info[2]]['sum']  = $info[4];
636f3f0262cSandi      $recent[$info[2]]['del']  = !@file_exists(wikiFN($info[2]));
637f3f0262cSandi    }
6385749f1ceSmatthiasgrimm    if(count($recent) >= $num){
639f3f0262cSandi      break; //finish if enough items found
640f3f0262cSandi    }
641f3f0262cSandi  }
642f3f0262cSandi  return $recent;
643f3f0262cSandi}
644f3f0262cSandi
645f3f0262cSandi/**
646652610a2Sandi * gets additonal informations for a certain pagerevison
647652610a2Sandi * from the changelog
648652610a2Sandi *
649652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
650652610a2Sandi */
651652610a2Sandifunction getRevisionInfo($id,$rev){
652652610a2Sandi  global $conf;
653258641c6Sandi
654258641c6Sandi  if(!$rev) return(null);
655258641c6Sandi
656c1049928Sandi  $info = array();
657c1049928Sandi  if(!@is_readable($conf['changelog'])){
658c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
659c1049928Sandi    return $recent;
660c1049928Sandi  }
661652610a2Sandi  $loglines = file($conf['changelog']);
662652610a2Sandi  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
663dc42ff59Sandi  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
664652610a2Sandi  $line = split("\t",$loglines[0]);
665652610a2Sandi  $info['date'] = $line[0];
666652610a2Sandi  $info['ip']   = $line[1];
667652610a2Sandi  $info['user'] = $line[3];
668652610a2Sandi  $info['sum']   = $line[4];
669652610a2Sandi  return $info;
670652610a2Sandi}
671652610a2Sandi
672652610a2Sandi/**
673f3f0262cSandi * Saves a wikitext by calling io_saveFile
67415fae107Sandi *
67515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
676f3f0262cSandi */
677f3f0262cSandifunction saveWikiText($id,$text,$summary){
678f3f0262cSandi  global $conf;
679f3f0262cSandi  global $lang;
680f3f0262cSandi  umask($conf['umask']);
681f3f0262cSandi  // ignore if no changes were made
682f3f0262cSandi  if($text == rawWiki($id,'')){
683f3f0262cSandi    return;
684f3f0262cSandi  }
685f3f0262cSandi
686f3f0262cSandi  $file = wikiFN($id);
687f3f0262cSandi  $old  = saveOldRevision($id);
688f3f0262cSandi
689f3f0262cSandi  if (empty($text)){
690e1f3d9e1SEsther Brunner    // remove empty file
691f3f0262cSandi    @unlink($file);
692e1f3d9e1SEsther Brunner    // remove any meta info
693e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
694e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
695e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
696b158d625SSteven Danz    }
697f3f0262cSandi    $del = true;
6983ce054b3Sandi    //autoset summary on deletion
6993ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
70053d6ccfeSandi    //remove empty namespaces
70153d6ccfeSandi    io_sweepNS($id);
702f3f0262cSandi  }else{
703f3f0262cSandi    // save file (datadir is created in io_saveFile)
704f3f0262cSandi    io_saveFile($file,$text);
705f3f0262cSandi    $del = false;
706f3f0262cSandi  }
707f3f0262cSandi
708652610a2Sandi  addLogEntry(@filemtime($file),$id,$summary);
70926a0801fSAndreas Gohr  // send notify mails
71026a0801fSAndreas Gohr  notify($id,'admin',$old,$summary);
71126a0801fSAndreas Gohr  notify($id,'subscribers',$old,$summary);
712f3f0262cSandi
713f3f0262cSandi  //purge cache on add by updating the purgefile
714f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
71598407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
716f3f0262cSandi  }
717f3f0262cSandi}
718f3f0262cSandi
719f3f0262cSandi/**
720f3f0262cSandi * moves the current version to the attic and returns its
721f3f0262cSandi * revision date
72215fae107Sandi *
72315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
724f3f0262cSandi */
725f3f0262cSandifunction saveOldRevision($id){
726f3f0262cSandi	global $conf;
727f3f0262cSandi  umask($conf['umask']);
728f3f0262cSandi  $oldf = wikiFN($id);
729f3f0262cSandi  if(!@file_exists($oldf)) return '';
730f3f0262cSandi  $date = filemtime($oldf);
731f3f0262cSandi  $newf = wikiFN($id,$date);
732f3f0262cSandi  if(substr($newf,-3)=='.gz'){
733f3f0262cSandi    io_saveFile($newf,rawWiki($id));
734f3f0262cSandi  }else{
735f3f0262cSandi    io_makeFileDir($newf);
736f3f0262cSandi    copy($oldf, $newf);
737f3f0262cSandi  }
738f3f0262cSandi  return $date;
739f3f0262cSandi}
740f3f0262cSandi
741f3f0262cSandi/**
74226a0801fSAndreas Gohr * Sends a notify mail on page change
74326a0801fSAndreas Gohr *
74426a0801fSAndreas Gohr * @param  string $id       The changed page
74526a0801fSAndreas Gohr * @param  string $who      Who to notify (admin|subscribers)
74626a0801fSAndreas Gohr * @param  int    $rev      Old page revision
74726a0801fSAndreas Gohr * @param  string $summary  What changed
74815fae107Sandi *
74915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
750f3f0262cSandi */
75126a0801fSAndreas Gohrfunction notify($id,$who,$rev='',$summary=''){
752f3f0262cSandi  global $lang;
753f3f0262cSandi  global $conf;
754b158d625SSteven Danz
75526a0801fSAndreas Gohr  // decide if there is something to do
75626a0801fSAndreas Gohr  if($who == 'admin'){
75726a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
758f3f0262cSandi    $text = rawLocale('mailtext');
75926a0801fSAndreas Gohr    $to   = $conf['notify'];
76026a0801fSAndreas Gohr    $bcc  = '';
76126a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
76226a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
76326a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
76426a0801fSAndreas Gohr    if(empty($bcc)) return;
76526a0801fSAndreas Gohr    $to   = '';
76626a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
76726a0801fSAndreas Gohr  }else{
76826a0801fSAndreas Gohr    return; //just to be safe
76926a0801fSAndreas Gohr  }
77026a0801fSAndreas Gohr
771f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
772f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
773f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
774f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
775ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
77626a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
77726a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
778ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
779f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
7807a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
781f3f0262cSandi
782f3f0262cSandi  if($rev){
783f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
784ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
785f3f0262cSandi    require_once("inc/DifferenceEngine.php");
786f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
787f3f0262cSandi                    split("\n",rawWiki($id)));
788f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
789f3f0262cSandi    $diff    = $dformat->format($df);
790f3f0262cSandi  }else{
791f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
792f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
793f3f0262cSandi    $diff = rawWiki($id);
794f3f0262cSandi  }
795f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
796241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
797f3f0262cSandi
79826a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
799f3f0262cSandi}
800f3f0262cSandi
80115fae107Sandi/**
80215fae107Sandi * Return a list of available page revisons
80315fae107Sandi *
80415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
80515fae107Sandi */
806f3f0262cSandifunction getRevisions($id){
807f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
808f3f0262cSandi  $revs = array();
809f3f0262cSandi  $clid = cleanID($id);
810f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
811493a6929SKobaYY  $clid = utf8_encodeFN($clid);
812f3f0262cSandi
813f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
814f3f0262cSandi    while (($file = readdir($dh)) !== false) {
815f3f0262cSandi      if (is_dir($revd.'/'.$file)) continue;
816f3f0262cSandi      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
817f3f0262cSandi        $revs[]=$match[1];
818f3f0262cSandi      }
819f3f0262cSandi    }
820f3f0262cSandi    closedir($dh);
821f3f0262cSandi  }
822f3f0262cSandi  rsort($revs);
823f3f0262cSandi  return $revs;
824f3f0262cSandi}
825f3f0262cSandi
826f3f0262cSandi/**
827f3f0262cSandi * extracts the query from a google referer
82815fae107Sandi *
8296b13307fSandi * @todo   should be more generic and support yahoo et al
83015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
831f3f0262cSandi */
832f3f0262cSandifunction getGoogleQuery(){
833f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
8345c3f206fSandi  if(!$url) return '';
835f3f0262cSandi
836f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
837f3f0262cSandi  $query = array();
838f3f0262cSandi  parse_str($url['query'],$query);
839f3f0262cSandi
840f3f0262cSandi  return $query['q'];
841f3f0262cSandi}
842f3f0262cSandi
843f3f0262cSandi/**
84415fae107Sandi * Try to set correct locale
84515fae107Sandi *
846095bfd5cSandi * @deprecated No longer used
84715fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
848f3f0262cSandi */
849f3f0262cSandifunction setCorrectLocale(){
850f3f0262cSandi  global $conf;
851f3f0262cSandi  global $lang;
852f3f0262cSandi
853f3f0262cSandi  $enc = strtoupper($lang['encoding']);
854f3f0262cSandi  foreach ($lang['locales'] as $loc){
855f3f0262cSandi    //try locale
856f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
857f3f0262cSandi    //try loceale with encoding
858f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
859f3f0262cSandi  }
860f3f0262cSandi  //still here? try to set from environment
861f3f0262cSandi  @setlocale(LC_ALL,"");
862f3f0262cSandi}
863f3f0262cSandi
864f3f0262cSandi/**
865f3f0262cSandi * Return the human readable size of a file
866f3f0262cSandi *
867f3f0262cSandi * @param       int    $size   A file size
868f3f0262cSandi * @param       int    $dec    A number of decimal places
869f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
870f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
871f3f0262cSandi * @version     1.0.0
872f3f0262cSandi */
873f31d5b73Sandifunction filesize_h($size, $dec = 1){
874f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
875f3f0262cSandi  $count = count($sizes);
876f3f0262cSandi  $i = 0;
877f3f0262cSandi
878f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
879f3f0262cSandi    $size /= 1024;
880f3f0262cSandi    $i++;
881f3f0262cSandi  }
882f3f0262cSandi
883f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
884f3f0262cSandi}
885f3f0262cSandi
88615fae107Sandi/**
887dc57ef04Sandi * Return DokuWikis version
88815fae107Sandi *
88915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
89015fae107Sandi */
891f31d5b73Sandifunction getVersion(){
892f31d5b73Sandi  //import version string
893f31d5b73Sandi  if(@file_exists('VERSION')){
894f31d5b73Sandi    //official release
895ec052310Sandi    return 'Release '.trim(io_readfile('VERSION'));
896f31d5b73Sandi  }elseif(is_dir('_darcs')){
897f31d5b73Sandi    //darcs checkout
898f31d5b73Sandi    $inv = file('_darcs/inventory');
899f31d5b73Sandi    $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv);
900f31d5b73Sandi    $cur = array_pop($inv);
901f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
902f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
903f31d5b73Sandi  }else{
904f31d5b73Sandi    return 'snapshot?';
905f31d5b73Sandi  }
906f31d5b73Sandi}
907f31d5b73Sandi
908f31d5b73Sandi/**
909f31d5b73Sandi * Run a few sanity checks
910f31d5b73Sandi *
911f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
912f31d5b73Sandi */
913f3f0262cSandifunction check(){
914f3f0262cSandi  global $conf;
915f3f0262cSandi  global $INFO;
916f3f0262cSandi
917f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
918f31d5b73Sandi
91949022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
92049022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
92149022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
92249022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
92349022a38Sandi  }else{
92449022a38Sandi    msg('PHP version '.phpversion(),1);
92549022a38Sandi  }
92649022a38Sandi
927f3f0262cSandi  if(is_writable($conf['changelog'])){
928f3f0262cSandi    msg('Changelog is writable',1);
929f3f0262cSandi  }else{
930f3f0262cSandi    msg('Changelog is not writable',-1);
931f3f0262cSandi  }
932f3f0262cSandi
933f3f0262cSandi  if(is_writable($conf['datadir'])){
934f3f0262cSandi    msg('Datadir is writable',1);
935f3f0262cSandi  }else{
936f3f0262cSandi    msg('Datadir is not writable',-1);
937f3f0262cSandi  }
938f3f0262cSandi
939f3f0262cSandi  if(is_writable($conf['olddir'])){
940f3f0262cSandi    msg('Attic is writable',1);
941f3f0262cSandi  }else{
942f3f0262cSandi    msg('Attic is not writable',-1);
943f3f0262cSandi  }
944f3f0262cSandi
945f3f0262cSandi  if(is_writable($conf['mediadir'])){
946f3f0262cSandi    msg('Mediadir is writable',1);
947f3f0262cSandi  }else{
948f3f0262cSandi    msg('Mediadir is not writable',-1);
949f3f0262cSandi  }
950f3f0262cSandi
95198407a7aSandi  if(is_writable($conf['cachedir'])){
95298407a7aSandi    msg('Cachedir is writable',1);
95398407a7aSandi  }else{
95498407a7aSandi    msg('Cachedir is not writable',-1);
95598407a7aSandi  }
95698407a7aSandi
957e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
9588c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
959f3f0262cSandi  }else{
9608c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
961f3f0262cSandi  }
96293a9e835Sandi
96393a9e835Sandi  if(function_exists('mb_strpos')){
96493a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
96593a9e835Sandi      msg('mb_string extension is available but will not be used',0);
96693a9e835Sandi    }else{
96793a9e835Sandi      msg('mb_string extension is available and will be used',1);
96893a9e835Sandi    }
96993a9e835Sandi  }else{
97093a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
97193a9e835Sandi  }
972f3f0262cSandi
973f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
974f3f0262cSandi
975f3f0262cSandi  if(is_writable($INFO['filepath'])){
976f3f0262cSandi    msg('The current page is writable by the webserver',0);
977f3f0262cSandi  }else{
978f3f0262cSandi    msg('The current page is not writable by the webserver',0);
979f3f0262cSandi  }
980f3f0262cSandi
981f3f0262cSandi  if($INFO['writable']){
982f3f0262cSandi    msg('The current page is writable by you',0);
983f3f0262cSandi  }else{
984f3f0262cSandi    msg('The current page is not writable you',0);
985f3f0262cSandi  }
986f3f0262cSandi}
987340756e4Sandi
988b158d625SSteven Danz/**
989b158d625SSteven Danz * Let us know if a user is tracking a page
990b158d625SSteven Danz *
9911380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
992b158d625SSteven Danz */
9931380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
9941380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
9951380fc45SAndreas Gohr  if (@file_exists($file)) {
996b158d625SSteven Danz    $mlist = file($file);
9971380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
9981380fc45SAndreas Gohr    return is_int($pos);
999b158d625SSteven Danz  }
10001380fc45SAndreas Gohr
1001b158d625SSteven Danz  return false;
1002b158d625SSteven Danz}
1003340756e4Sandi
1004f9eb5648Ssteven-danz/**
1005f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1006f9eb5648Ssteven-danz * users subscribed to a page
1007f9eb5648Ssteven-danz *
100826a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1009f9eb5648Ssteven-danz */
1010f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1011f9eb5648Ssteven-danz  global $conf;
1012f9eb5648Ssteven-danz
1013f9eb5648Ssteven-danz  $emails = '';
1014f9eb5648Ssteven-danz
101526a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
101626a0801fSAndreas Gohr
1017f9eb5648Ssteven-danz  $mlist = array();
1018f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1019f9eb5648Ssteven-danz  if (file_exists($file)) {
1020f9eb5648Ssteven-danz    $mlist = file($file);
1021f9eb5648Ssteven-danz  }
1022f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1023f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1024f9eb5648Ssteven-danz      $who = rtrim($who);
1025f9eb5648Ssteven-danz      $info = auth_getUserData($who);
1026f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1027f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1028f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1029f9eb5648Ssteven-danz          if (empty($emails)) {
1030f9eb5648Ssteven-danz            $emails = $info['mail'];
1031f9eb5648Ssteven-danz          } else {
1032f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1033f9eb5648Ssteven-danz          }
1034f9eb5648Ssteven-danz        }
1035f9eb5648Ssteven-danz      }
1036f9eb5648Ssteven-danz    }
1037f9eb5648Ssteven-danz  }
1038f9eb5648Ssteven-danz
1039f9eb5648Ssteven-danz  return $emails;
1040f9eb5648Ssteven-danz}
1041f9eb5648Ssteven-danz
1042340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1043