xref: /dokuwiki/inc/common.php (revision f864871e67a7343d5cdce00da0eb50f8138fba15)
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/**
17b6912aeaSAndreas Gohr * These constants are used with the recents function
18b6912aeaSAndreas Gohr */
19b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED',2);
20b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS',4);
21b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES',8);
22b6912aeaSAndreas Gohr
23b6912aeaSAndreas Gohr/**
2415fae107Sandi * Return info about the current document as associative
25f3f0262cSandi * array.
2615fae107Sandi *
2715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
28f3f0262cSandi */
29f3f0262cSandifunction pageinfo(){
30f3f0262cSandi  global $ID;
31f3f0262cSandi  global $REV;
32f3f0262cSandi  global $USERINFO;
33f3f0262cSandi  global $conf;
34f3f0262cSandi
35f3f0262cSandi  if($_SERVER['REMOTE_USER']){
36f3f0262cSandi    $info['userinfo']   = $USERINFO;
37f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
381380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
3917ee7f66SAndreas Gohr
4017ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
4117ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
4217ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
4317ee7f66SAndreas Gohr    }
44f3f0262cSandi  }else{
45f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
461380fc45SAndreas Gohr    $info['subscribed'] = false;
47f3f0262cSandi  }
48f3f0262cSandi
49f3f0262cSandi  $info['namespace'] = getNS($ID);
50f3f0262cSandi  $info['locked']    = checklock($ID);
51f3f0262cSandi  $info['filepath']  = realpath(wikiFN($ID,$REV));
52f3f0262cSandi  $info['exists']    = @file_exists($info['filepath']);
53f3f0262cSandi  if($REV && !$info['exists']){
54f3f0262cSandi    //check if current revision was meant
55f3f0262cSandi    $cur = wikiFN($ID);
56f3f0262cSandi    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
57f3f0262cSandi      $info['filepath'] = realpath($cur);
58f3f0262cSandi      $info['exists']   = true;
59f3f0262cSandi      $REV = '';
60f3f0262cSandi    }
61f3f0262cSandi  }
62c112d578Sandi  $info['rev'] = $REV;
63f3f0262cSandi  if($info['exists']){
64f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
65f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
66f3f0262cSandi  }else{
67f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
68f3f0262cSandi  }
69f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
70f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
71f3f0262cSandi
72652610a2Sandi  //who's the editor
73652610a2Sandi  if($REV){
74652610a2Sandi    $revinfo = getRevisionInfo($ID,$REV);
75652610a2Sandi  }else{
76652610a2Sandi    $revinfo = getRevisionInfo($ID,$info['lastmod']);
77652610a2Sandi  }
78652610a2Sandi  $info['ip']     = $revinfo['ip'];
79652610a2Sandi  $info['user']   = $revinfo['user'];
80652610a2Sandi  $info['sum']    = $revinfo['sum'];
81b6912aeaSAndreas Gohr  $info['minor']  = $revinfo['minor'];
8259f257aeSchris
8388f522e9Sandi  if($revinfo['user']){
8488f522e9Sandi    $info['editor'] = $revinfo['user'];
8588f522e9Sandi  }else{
8688f522e9Sandi    $info['editor'] = $revinfo['ip'];
8788f522e9Sandi  }
88652610a2Sandi
89f3f0262cSandi  return $info;
90f3f0262cSandi}
91f3f0262cSandi
92f3f0262cSandi/**
932684e50aSAndreas Gohr * Build an string of URL parameters
942684e50aSAndreas Gohr *
952684e50aSAndreas Gohr * @author Andreas Gohr
962684e50aSAndreas Gohr */
97b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
982684e50aSAndreas Gohr  $url = '';
992684e50aSAndreas Gohr  $amp = false;
1002684e50aSAndreas Gohr  foreach($params as $key => $val){
101b174aeaeSchris    if($amp) $url .= $sep;
1022684e50aSAndreas Gohr
1032684e50aSAndreas Gohr    $url .= $key.'=';
104b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1052684e50aSAndreas Gohr    $amp = true;
1062684e50aSAndreas Gohr  }
1072684e50aSAndreas Gohr  return $url;
1082684e50aSAndreas Gohr}
1092684e50aSAndreas Gohr
1102684e50aSAndreas Gohr/**
1112684e50aSAndreas Gohr * Build an string of html tag attributes
1122684e50aSAndreas Gohr *
1132684e50aSAndreas Gohr * @author Andreas Gohr
1142684e50aSAndreas Gohr */
1152684e50aSAndreas Gohrfunction buildAttributes($params){
1162684e50aSAndreas Gohr  $url = '';
1172684e50aSAndreas Gohr  foreach($params as $key => $val){
1182684e50aSAndreas Gohr    $url .= $key.'="';
1192684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1202684e50aSAndreas Gohr    $url .= '" ';
1212684e50aSAndreas Gohr  }
1222684e50aSAndreas Gohr  return $url;
1232684e50aSAndreas Gohr}
1242684e50aSAndreas Gohr
1252684e50aSAndreas Gohr
1262684e50aSAndreas Gohr/**
1270396becbSandi * print a message
1280396becbSandi *
1290396becbSandi * If HTTP headers were not sent yet the message is added
1300396becbSandi * to the global message array else it's printed directly
1310396becbSandi * using html_msgarea()
1320396becbSandi *
133f3f0262cSandi *
134f3f0262cSandi * Levels can be:
135f3f0262cSandi *
136f3f0262cSandi * -1 error
137f3f0262cSandi *  0 info
138f3f0262cSandi *  1 success
13915fae107Sandi *
14015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1410396becbSandi * @see    html_msgarea
142f3f0262cSandi */
143f3f0262cSandifunction msg($message,$lvl=0){
144f3f0262cSandi  global $MSG;
145f3f0262cSandi  $errors[-1] = 'error';
146f3f0262cSandi  $errors[0]  = 'info';
147f3f0262cSandi  $errors[1]  = 'success';
148f3f0262cSandi
149cc20ad51Sandi  if(!headers_sent()){
150f3f0262cSandi    if(!isset($MSG)) $MSG = array();
151f3f0262cSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
1520396becbSandi  }else{
1530396becbSandi    $MSG = array();
1540396becbSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
155f62ea8a1Sandi    if(function_exists('html_msgarea')){
1560396becbSandi      html_msgarea();
157f62ea8a1Sandi    }else{
158f62ea8a1Sandi      print "ERROR($lvl) $message";
159f62ea8a1Sandi    }
1600396becbSandi  }
161f3f0262cSandi}
162f3f0262cSandi
163f3f0262cSandi/**
16415fae107Sandi * This builds the breadcrumb trail and returns it as array
16515fae107Sandi *
16615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
167f3f0262cSandi */
168f3f0262cSandifunction breadcrumbs(){
1698746e727Sandi  // we prepare the breadcrumbs early for quick session closing
1708746e727Sandi  static $crumbs = null;
1718746e727Sandi  if($crumbs != null) return $crumbs;
1728746e727Sandi
173f3f0262cSandi  global $ID;
174f3f0262cSandi  global $ACT;
175f3f0262cSandi  global $conf;
176f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
177f3f0262cSandi
178f3f0262cSandi  //first visit?
179f3f0262cSandi  if (!is_array($crumbs)){
180f3f0262cSandi    $crumbs = array();
181f3f0262cSandi  }
182f3f0262cSandi  //we only save on show and existing wiki documents
183a77f5846Sjan  $file = wikiFN($ID);
184a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
185f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
186f3f0262cSandi    return $crumbs;
187f3f0262cSandi  }
188a77f5846Sjan
189a77f5846Sjan  // page names
190a77f5846Sjan  $name = noNS($ID);
191a77f5846Sjan  if ($conf['useheading']) {
192a77f5846Sjan    // get page title
193bb0a59d4Sjan    $title = p_get_first_heading($ID);
194a77f5846Sjan    if ($title) {
195a77f5846Sjan      $name = $title;
196a77f5846Sjan    }
197a77f5846Sjan  }
198a77f5846Sjan
199f3f0262cSandi  //remove ID from array
200a77f5846Sjan  if (isset($crumbs[$ID])) {
201a77f5846Sjan    unset($crumbs[$ID]);
202f3f0262cSandi  }
203f3f0262cSandi
204f3f0262cSandi  //add to array
205a77f5846Sjan  $crumbs[$ID] = $name;
206f3f0262cSandi  //reduce size
207f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
208f3f0262cSandi    array_shift($crumbs);
209f3f0262cSandi  }
210f3f0262cSandi  //save to session
211f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
212f3f0262cSandi  return $crumbs;
213f3f0262cSandi}
214f3f0262cSandi
215f3f0262cSandi/**
21615fae107Sandi * Filter for page IDs
21715fae107Sandi *
218f3f0262cSandi * This is run on a ID before it is outputted somewhere
219f3f0262cSandi * currently used to replace the colon with something else
220f3f0262cSandi * on Windows systems and to have proper URL encoding
22115fae107Sandi *
22249c713a3Sandi * Urlencoding is ommitted when the second parameter is false
22349c713a3Sandi *
22415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
225f3f0262cSandi */
22649c713a3Sandifunction idfilter($id,$ue=true){
227f3f0262cSandi  global $conf;
228f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
229f3f0262cSandi    $id = strtr($id,':','/');
230f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
231f3f0262cSandi      $conf['userewrite']) {
232f3f0262cSandi    $id = strtr($id,':',';');
233f3f0262cSandi  }
23449c713a3Sandi  if($ue){
235b6c6979fSAndreas Gohr    $id = rawurlencode($id);
236f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
237f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
23849c713a3Sandi  }
239f3f0262cSandi  return $id;
240f3f0262cSandi}
241f3f0262cSandi
242f3f0262cSandi/**
243ed7b5f09Sandi * This builds a link to a wikipage
24415fae107Sandi *
2456c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2466c7843b5Sandi * given in $more
2476c7843b5Sandi *
24815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
249f3f0262cSandi */
250b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
251f3f0262cSandi  global $conf;
2526de3759aSAndreas Gohr  if(is_array($more)){
253b174aeaeSchris    $more = buildURLparams($more,$sep);
2546de3759aSAndreas Gohr  }else{
255b174aeaeSchris    $more = str_replace(',',$sep,$more);
2566de3759aSAndreas Gohr  }
257f3f0262cSandi
258f3f0262cSandi  $id    = idfilter($id);
259ed7b5f09Sandi  if($abs){
260ed7b5f09Sandi    $xlink = DOKU_URL;
261ed7b5f09Sandi  }else{
262ed7b5f09Sandi    $xlink = DOKU_BASE;
263ed7b5f09Sandi  }
264f3f0262cSandi
2656c7843b5Sandi  if($conf['userewrite'] == 2){
2666c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
2676c7843b5Sandi    if($more) $xlink .= '?'.$more;
2686c7843b5Sandi  }elseif($conf['userewrite']){
269f3f0262cSandi    $xlink .= $id;
270f3f0262cSandi    if($more) $xlink .= '?'.$more;
2716c7843b5Sandi  }else{
2726c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
273b174aeaeSchris    if($more) $xlink .= $sep.$more;
274f3f0262cSandi  }
275f3f0262cSandi
276f3f0262cSandi  return $xlink;
277f3f0262cSandi}
278f3f0262cSandi
279f3f0262cSandi/**
2806de3759aSAndreas Gohr * Build a link to a media file
2816de3759aSAndreas Gohr *
2826de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
2836de3759aSAndreas Gohr */
284b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
2856de3759aSAndreas Gohr  global $conf;
2866de3759aSAndreas Gohr  if(is_array($more)){
287b174aeaeSchris    $more = buildURLparams($more,$sep);
2886de3759aSAndreas Gohr  }else{
289b174aeaeSchris    $more = str_replace(',',$sep,$more);
2906de3759aSAndreas Gohr  }
2916de3759aSAndreas Gohr
2926de3759aSAndreas Gohr  $xlink = DOKU_BASE;
2936de3759aSAndreas Gohr
2946de3759aSAndreas Gohr  // external URLs are always direct without rewriting
2956de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
2966de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
2976de3759aSAndreas Gohr    if($more){
2986de3759aSAndreas Gohr      $xlink .= '?'.$more;
299b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3006de3759aSAndreas Gohr    }else{
301b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3026de3759aSAndreas Gohr    }
3036de3759aSAndreas Gohr    return $xlink;
3046de3759aSAndreas Gohr  }
3056de3759aSAndreas Gohr
3066de3759aSAndreas Gohr  $id = idfilter($id);
3076de3759aSAndreas Gohr
3086de3759aSAndreas Gohr  // decide on scriptname
3096de3759aSAndreas Gohr  if($direct){
3106de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3116de3759aSAndreas Gohr      $script = '_media';
3126de3759aSAndreas Gohr    }else{
3136de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3146de3759aSAndreas Gohr    }
3156de3759aSAndreas Gohr  }else{
3166de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3176de3759aSAndreas Gohr      $script = '_detail';
3186de3759aSAndreas Gohr    }else{
3196de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3206de3759aSAndreas Gohr    }
3216de3759aSAndreas Gohr  }
3226de3759aSAndreas Gohr
3236de3759aSAndreas Gohr  // build URL based on rewrite mode
3246de3759aSAndreas Gohr   if($conf['userewrite']){
3256de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3266de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3276de3759aSAndreas Gohr   }else{
3286de3759aSAndreas Gohr     if($more){
329a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
330b174aeaeSchris       $xlink .= $sep.'media='.$id;
3316de3759aSAndreas Gohr     }else{
332a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
3336de3759aSAndreas Gohr     }
3346de3759aSAndreas Gohr   }
3356de3759aSAndreas Gohr
3366de3759aSAndreas Gohr  return $xlink;
3376de3759aSAndreas Gohr}
3386de3759aSAndreas Gohr
3396de3759aSAndreas Gohr
3406de3759aSAndreas Gohr
3416de3759aSAndreas Gohr/**
342f3f0262cSandi * Just builds a link to a script
34315fae107Sandi *
344ed7b5f09Sandi * @todo   maybe obsolete
34515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
346f3f0262cSandi */
347f3f0262cSandifunction script($script='doku.php'){
348ed7b5f09Sandi#  $link = getBaseURL();
349ed7b5f09Sandi#  $link .= $script;
350ed7b5f09Sandi#  return $link;
351ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
352f3f0262cSandi}
353f3f0262cSandi
354f3f0262cSandi/**
35515fae107Sandi * Spamcheck against wordlist
35615fae107Sandi *
357f3f0262cSandi * Checks the wikitext against a list of blocked expressions
358f3f0262cSandi * returns true if the text contains any bad words
35915fae107Sandi *
36015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
361f3f0262cSandi */
362f3f0262cSandifunction checkwordblock(){
363f3f0262cSandi  global $TEXT;
364f3f0262cSandi  global $conf;
365f3f0262cSandi
366f3f0262cSandi  if(!$conf['usewordblock']) return false;
367f3f0262cSandi
368b9ac8716Schris  $wordblocks = getWordblocks();
3693e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
3703e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
3713e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
3723e2965d7Sandi    //backreferences are used - the maximum is 99
3733e2965d7Sandi    //this is very bad performancewise and may even be too high still
3743e2965d7Sandi    $chunksize = 40;
3753e2965d7Sandi  }else{
376703f6fdeSandi    //read file in chunks of 600 - this should work around the
3773e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
378444b87a5SAndreas Gohr    $chunksize = 400;
3793e2965d7Sandi  }
380b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
381f3f0262cSandi    $re = array();
382f3f0262cSandi    #build regexp from blocks
383f3f0262cSandi    foreach($blocks as $block){
384f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
385f3f0262cSandi      $block = trim($block);
386f3f0262cSandi      if(empty($block)) continue;
387f3f0262cSandi      $re[]  = $block;
388f3f0262cSandi    }
389b9ac8716Schris    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
390b9ac8716Schris      return true;
391b9ac8716Schris    }
392703f6fdeSandi  }
393f3f0262cSandi  return false;
394f3f0262cSandi}
395f3f0262cSandi
396f3f0262cSandi/**
39715fae107Sandi * Return the IP of the client
39815fae107Sandi *
39915fae107Sandi * Honours X-Forwarded-For Proxy Headers
40015fae107Sandi *
40115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
402f3f0262cSandi */
403f3f0262cSandifunction clientIP(){
404f3f0262cSandi  $my = $_SERVER['REMOTE_ADDR'];
405f3f0262cSandi  if($_SERVER['HTTP_X_FORWARDED_FOR']){
406f3f0262cSandi    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
407f3f0262cSandi  }
408f3f0262cSandi  return $my;
409f3f0262cSandi}
410f3f0262cSandi
411f3f0262cSandi/**
41215fae107Sandi * Checks if a given page is currently locked.
41315fae107Sandi *
414f3f0262cSandi * removes stale lockfiles
41515fae107Sandi *
41615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
417f3f0262cSandi */
418f3f0262cSandifunction checklock($id){
419f3f0262cSandi  global $conf;
420f3f0262cSandi  $lock = wikiFN($id).'.lock';
421f3f0262cSandi
422f3f0262cSandi  //no lockfile
423f3f0262cSandi  if(!@file_exists($lock)) return false;
424f3f0262cSandi
425f3f0262cSandi  //lockfile expired
426f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
427f3f0262cSandi    unlink($lock);
428f3f0262cSandi    return false;
429f3f0262cSandi  }
430f3f0262cSandi
431f3f0262cSandi  //my own lock
432f3f0262cSandi  $ip = io_readFile($lock);
433f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
434f3f0262cSandi    return false;
435f3f0262cSandi  }
436f3f0262cSandi
437f3f0262cSandi  return $ip;
438f3f0262cSandi}
439f3f0262cSandi
440f3f0262cSandi/**
44115fae107Sandi * Lock a page for editing
44215fae107Sandi *
44315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
444f3f0262cSandi */
445f3f0262cSandifunction lock($id){
446f3f0262cSandi  $lock = wikiFN($id).'.lock';
447f3f0262cSandi  if($_SERVER['REMOTE_USER']){
448f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
449f3f0262cSandi  }else{
450f3f0262cSandi    io_saveFile($lock,clientIP());
451f3f0262cSandi  }
452f3f0262cSandi}
453f3f0262cSandi
454f3f0262cSandi/**
45515fae107Sandi * Unlock a page if it was locked by the user
456f3f0262cSandi *
45715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
45815fae107Sandi * @return bool true if a lock was removed
459f3f0262cSandi */
460f3f0262cSandifunction unlock($id){
461f3f0262cSandi  $lock = wikiFN($id).'.lock';
462f3f0262cSandi  if(@file_exists($lock)){
463f3f0262cSandi    $ip = io_readFile($lock);
464f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
465f3f0262cSandi      @unlink($lock);
466f3f0262cSandi      return true;
467f3f0262cSandi    }
468f3f0262cSandi  }
469f3f0262cSandi  return false;
470f3f0262cSandi}
471f3f0262cSandi
472f3f0262cSandi/**
473f3f0262cSandi * convert line ending to unix format
474f3f0262cSandi *
47515fae107Sandi * @see    formText() for 2crlf conversion
47615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
477f3f0262cSandi */
478f3f0262cSandifunction cleanText($text){
479f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
480f3f0262cSandi  return $text;
481f3f0262cSandi}
482f3f0262cSandi
483f3f0262cSandi/**
484f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
485f3f0262cSandi * It also converts line endings to Windows format which is
486f3f0262cSandi * pseudo standard for webforms.
487f3f0262cSandi *
48815fae107Sandi * @see    cleanText() for 2unix conversion
48915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
490f3f0262cSandi */
491f3f0262cSandifunction formText($text){
492f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
493f3f0262cSandi  return htmlspecialchars($text);
494f3f0262cSandi}
495f3f0262cSandi
496f3f0262cSandi/**
49715fae107Sandi * Returns the specified local text in raw format
49815fae107Sandi *
49915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
500f3f0262cSandi */
501f3f0262cSandifunction rawLocale($id){
502f3f0262cSandi  return io_readFile(localeFN($id));
503f3f0262cSandi}
504f3f0262cSandi
505f3f0262cSandi/**
506f3f0262cSandi * Returns the raw WikiText
50715fae107Sandi *
50815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
509f3f0262cSandi */
510f3f0262cSandifunction rawWiki($id,$rev=''){
511f3f0262cSandi  return io_readFile(wikiFN($id,$rev));
512f3f0262cSandi}
513f3f0262cSandi
514f3f0262cSandi/**
5157146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
5167146cee2SAndreas Gohr *
5177146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5187146cee2SAndreas Gohr */
5197146cee2SAndreas Gohrfunction pageTemplate($id){
520a15ce62dSEsther Brunner  global $conf;
521a15ce62dSEsther Brunner  global $INFO;
522a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
523a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
524a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
525a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
526a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
527a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
528a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
529a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
530a15ce62dSEsther Brunner  return $tpl;
5317146cee2SAndreas Gohr}
5327146cee2SAndreas Gohr
5337146cee2SAndreas Gohr
5347146cee2SAndreas Gohr/**
53515fae107Sandi * Returns the raw Wiki Text in three slices.
53615fae107Sandi *
53715fae107Sandi * The range parameter needs to have the form "from-to"
53815cfe303Sandi * and gives the range of the section in bytes - no
53915cfe303Sandi * UTF-8 awareness is needed.
540f3f0262cSandi * The returned order is prefix, section and suffix.
54115fae107Sandi *
54215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
543f3f0262cSandi */
544f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
545f3f0262cSandi  list($from,$to) = split('-',$range,2);
546f3f0262cSandi  $text = io_readFile(wikiFN($id,$rev));
547f3f0262cSandi  if(!$from) $from = 0;
548c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
549f3f0262cSandi
55015cfe303Sandi  $slices[0] = substr($text,0,$from-1);
55115cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
55215cfe303Sandi  $slices[2] = substr($text,$to);
553f3f0262cSandi
554f3f0262cSandi  return $slices;
555f3f0262cSandi}
556f3f0262cSandi
557f3f0262cSandi/**
55815fae107Sandi * Joins wiki text slices
55915fae107Sandi *
560f3f0262cSandi * function to join the text slices with correct lineendings again.
561f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
562f3f0262cSandi * lines between sections if needed (used on saving).
56315fae107Sandi *
56415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
565f3f0262cSandi */
566f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
567f3f0262cSandi
568f3f0262cSandi  if($pretty){
569f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
570f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
571f3f0262cSandi  }
572f3f0262cSandi
573f3f0262cSandi  if($pre) $pre .= "\n";
574f3f0262cSandi  if($suf) $text .= "\n";
575f3f0262cSandi  return $pre.$text.$suf;
576f3f0262cSandi}
577f3f0262cSandi
578f3f0262cSandi/**
57915fae107Sandi * print debug messages
58015fae107Sandi *
581f3f0262cSandi * little function to print the content of a var
58215fae107Sandi *
58315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
584f3f0262cSandi */
585f3f0262cSandifunction dbg($msg,$hidden=false){
586f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
587f3f0262cSandi  print_r($msg);
588f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
589f3f0262cSandi}
590f3f0262cSandi
591f3f0262cSandi/**
592f3f0262cSandi * Add's an entry to the changelog
59315fae107Sandi *
59415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
595f3f0262cSandi */
596b6912aeaSAndreas Gohrfunction addLogEntry($date,$id,$summary='',$minor=false){
597f3f0262cSandi  global $conf;
598c1049928Sandi
599c1049928Sandi  if(!@is_writable($conf['changelog'])){
600c1049928Sandi    msg($conf['changelog'].' is not writable!',-1);
601c1049928Sandi    return;
602c1049928Sandi  }
603c1049928Sandi
604652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
605f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
606f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
607f3f0262cSandi
608b6912aeaSAndreas Gohr  if($conf['useacl'] && $user && $minor){
609b6912aeaSAndreas Gohr    $summary = '*'.$summary;
610b6912aeaSAndreas Gohr  }else{
611b6912aeaSAndreas Gohr    $summary = ' '.$summary;
612b6912aeaSAndreas Gohr  }
613b6912aeaSAndreas Gohr
614f3f0262cSandi  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
615dbb00abcSEsther Brunner  io_saveFile($conf['changelog'],$logline,true);
616f3f0262cSandi}
617f3f0262cSandi
618f3f0262cSandi/**
619b6912aeaSAndreas Gohr * Checks an summary entry if it was a minor edit
620b6912aeaSAndreas Gohr *
621b6912aeaSAndreas Gohr * The summary is cleaned of the marker char
622b6912aeaSAndreas Gohr *
623b6912aeaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
624b6912aeaSAndreas Gohr */
625b6912aeaSAndreas Gohrfunction isMinor(&$summary){
626b6912aeaSAndreas Gohr  if(substr($summary,0,1) == '*'){
627b6912aeaSAndreas Gohr    $summary = substr($summary,1);
628b6912aeaSAndreas Gohr    return true;
629b6912aeaSAndreas Gohr  }
630b6912aeaSAndreas Gohr  $summary = trim($summary);
631b6912aeaSAndreas Gohr  return false;
632b6912aeaSAndreas Gohr}
633b6912aeaSAndreas Gohr
634b6912aeaSAndreas Gohr/**
635d437bcc4SAndreas Gohr * Internal function used by getRecents
636d437bcc4SAndreas Gohr *
637d437bcc4SAndreas Gohr * don't call directly
638d437bcc4SAndreas Gohr *
639d437bcc4SAndreas Gohr * @see getRecents()
640d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
641d437bcc4SAndreas Gohr */
642b6912aeaSAndreas Gohrfunction _handleRecent($line,$ns,$flags){
643d437bcc4SAndreas Gohr  static $seen  = array();         //caches seen pages and skip them
644d437bcc4SAndreas Gohr  if(empty($line)) return false;   //skip empty lines
645d437bcc4SAndreas Gohr
646d437bcc4SAndreas Gohr  // split the line into parts
647d437bcc4SAndreas Gohr  list($dt,$ip,$id,$usr,$sum) = explode("\t",$line);
648d437bcc4SAndreas Gohr
649d437bcc4SAndreas Gohr  // skip seen ones
650d437bcc4SAndreas Gohr  if($seen[$id]) return false;
651b6912aeaSAndreas Gohr  $recent = array();
652b6912aeaSAndreas Gohr
653b6912aeaSAndreas Gohr  // check minors
654b6912aeaSAndreas Gohr  if(isMinor($sum)){
655b6912aeaSAndreas Gohr    // skip minors
656b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_MINORS) return false;
657b6912aeaSAndreas Gohr    $recent['minor'] = true;
658b6912aeaSAndreas Gohr  }else{
659b6912aeaSAndreas Gohr    $recent['minor'] = false;
660b6912aeaSAndreas Gohr  }
661d437bcc4SAndreas Gohr
662d437bcc4SAndreas Gohr  // remember in seen to skip additional sights
663d437bcc4SAndreas Gohr  $seen[$id] = 1;
664d437bcc4SAndreas Gohr
6650dc92c6fSAndreas Gohr  // check if it's a hidden page
6660dc92c6fSAndreas Gohr  if(isHiddenPage($id)) return false;
6670dc92c6fSAndreas Gohr
668d437bcc4SAndreas Gohr  // filter namespace
669d437bcc4SAndreas Gohr  if (($ns) && (strpos($id,$ns.':') !== 0)) return false;
670d437bcc4SAndreas Gohr
671d437bcc4SAndreas Gohr  // exclude subnamespaces
672b6912aeaSAndreas Gohr  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($id) != $ns)) return false;
673d437bcc4SAndreas Gohr
674ae56bfb6SAndreas Gohr  // check ACL
675ae56bfb6SAndreas Gohr  if (auth_quickaclcheck($id) < AUTH_READ) return false;
676ae56bfb6SAndreas Gohr
677d437bcc4SAndreas Gohr  // check existance
678d437bcc4SAndreas Gohr  if(!@file_exists(wikiFN($id))){
679b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_DELETED){
680d437bcc4SAndreas Gohr      return false;
681d437bcc4SAndreas Gohr    }else{
682d437bcc4SAndreas Gohr      $recent['del'] = true;
683d437bcc4SAndreas Gohr    }
684d437bcc4SAndreas Gohr  }else{
685d437bcc4SAndreas Gohr    $recent['del'] = false;
686d437bcc4SAndreas Gohr  }
687d437bcc4SAndreas Gohr
688d437bcc4SAndreas Gohr  $recent['id']   = $id;
689d437bcc4SAndreas Gohr  $recent['date'] = $dt;
690d437bcc4SAndreas Gohr  $recent['ip']   = $ip;
691d437bcc4SAndreas Gohr  $recent['user'] = $usr;
692d437bcc4SAndreas Gohr  $recent['sum']  = $sum;
693d437bcc4SAndreas Gohr
694d437bcc4SAndreas Gohr  return $recent;
695d437bcc4SAndreas Gohr}
696d437bcc4SAndreas Gohr
697b6912aeaSAndreas Gohr
698d437bcc4SAndreas Gohr/**
699f3f0262cSandi * returns an array of recently changed files using the
700f3f0262cSandi * changelog
701d437bcc4SAndreas Gohr *
702b6912aeaSAndreas Gohr * The following constants can be used to control which changes are
703b6912aeaSAndreas Gohr * included. Add them together as needed.
704b6912aeaSAndreas Gohr *
705b6912aeaSAndreas Gohr * RECENTS_SKIP_DELETED   - don't include deleted pages
706b6912aeaSAndreas Gohr * RECENTS_SKIP_MINORS    - don't include minor changes
707b6912aeaSAndreas Gohr * RECENTS_SKIP_SUBSPACES - don't include subspaces
708b6912aeaSAndreas Gohr *
709d437bcc4SAndreas Gohr * @param int    $first   number of first entry returned (for paginating
710d437bcc4SAndreas Gohr * @param int    $num     return $num entries
711d437bcc4SAndreas Gohr * @param string $ns      restrict to given namespace
712b6912aeaSAndreas Gohr * @param bool   $flags   see above
71315fae107Sandi *
71415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
715f3f0262cSandi */
716b6912aeaSAndreas Gohrfunction getRecents($first,$num,$ns='',$flags=0){
717f3f0262cSandi  global $conf;
718f3f0262cSandi  $recent = array();
719d437bcc4SAndreas Gohr  $count  = 0;
7205749f1ceSmatthiasgrimm
7215749f1ceSmatthiasgrimm  if(!$num)
7225749f1ceSmatthiasgrimm    return $recent;
723f3f0262cSandi
724c1049928Sandi  if(!@is_readable($conf['changelog'])){
725c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
726c1049928Sandi    return $recent;
727c1049928Sandi  }
728c1049928Sandi
729d437bcc4SAndreas Gohr  $fh  = fopen($conf['changelog'],'r');
730d437bcc4SAndreas Gohr  $buf = '';
731d437bcc4SAndreas Gohr  $csz = 4096;                              //chunksize
732d437bcc4SAndreas Gohr  fseek($fh,0,SEEK_END);                    // jump to the end
733d437bcc4SAndreas Gohr  $pos = ftell($fh);                        // position pointer
734f3f0262cSandi
735d437bcc4SAndreas Gohr  // now read backwards into buffer
736d437bcc4SAndreas Gohr  while($pos > 0){
737d437bcc4SAndreas Gohr    $pos -= $csz;                           // seek to previous chunk...
738f6c1156dSWolfgang Ocker    if($pos < 0) {                          // ...or rest of file
739f6c1156dSWolfgang Ocker      $csz += $pos;
740f6c1156dSWolfgang Ocker      $pos = 0;
741f6c1156dSWolfgang Ocker    }
742f6c1156dSWolfgang Ocker
743d437bcc4SAndreas Gohr    fseek($fh,$pos);
744dbb00abcSEsther Brunner
745d437bcc4SAndreas Gohr    $buf = fread($fh,$csz).$buf;            // prepend to buffer
7468f1d587cSEsther Brunner
747d437bcc4SAndreas Gohr    $lines = explode("\n",$buf);            // split buffer into lines
7485749f1ceSmatthiasgrimm
749d437bcc4SAndreas Gohr    if($pos > 0){
750d437bcc4SAndreas Gohr      $buf = array_shift($lines);           // first one may be still incomplete
751f3f0262cSandi    }
752d437bcc4SAndreas Gohr
753d437bcc4SAndreas Gohr    $cnt = count($lines);
754d437bcc4SAndreas Gohr    if(!$cnt) continue;                     // no lines yet
755d437bcc4SAndreas Gohr
756d437bcc4SAndreas Gohr    // handle lines
757d437bcc4SAndreas Gohr    for($i = $cnt-1; $i >= 0; $i--){
758b6912aeaSAndreas Gohr      $rec = _handleRecent($lines[$i],$ns,$flags);
759d437bcc4SAndreas Gohr      if($rec !== false){
760d437bcc4SAndreas Gohr        if(--$first >= 0) continue;         // skip first entries
761d437bcc4SAndreas Gohr        $recent[] = $rec;
762d437bcc4SAndreas Gohr        $count++;
763d437bcc4SAndreas Gohr
764d437bcc4SAndreas Gohr        // break while when we have enough entries
765d437bcc4SAndreas Gohr        if($count >= $num){
766d437bcc4SAndreas Gohr          $pos = 0; // will break the while loop
767d437bcc4SAndreas Gohr          break;    // will break the for loop
768f3f0262cSandi        }
769f3f0262cSandi      }
770d437bcc4SAndreas Gohr    }
771d437bcc4SAndreas Gohr  }// end of while
772d437bcc4SAndreas Gohr
773d437bcc4SAndreas Gohr  fclose($fh);
774f3f0262cSandi  return $recent;
775f3f0262cSandi}
776f3f0262cSandi
777f3f0262cSandi/**
778652610a2Sandi * gets additonal informations for a certain pagerevison
779652610a2Sandi * from the changelog
780652610a2Sandi *
781652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
782652610a2Sandi */
783652610a2Sandifunction getRevisionInfo($id,$rev){
784652610a2Sandi  global $conf;
785258641c6Sandi
786258641c6Sandi  if(!$rev) return(null);
787258641c6Sandi
788c1049928Sandi  $info = array();
789c1049928Sandi  if(!@is_readable($conf['changelog'])){
790c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
791c1049928Sandi    return $recent;
792c1049928Sandi  }
793652610a2Sandi  $loglines = file($conf['changelog']);
794652610a2Sandi  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
795dc42ff59Sandi  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
796652610a2Sandi  $line = split("\t",$loglines[0]);
797652610a2Sandi  $info['date']  = $line[0];
798652610a2Sandi  $info['ip']    = $line[1];
799652610a2Sandi  $info['user']  = $line[3];
800652610a2Sandi  $info['sum']   = $line[4];
801b6912aeaSAndreas Gohr  $info['minor'] = isMinor($info['sum']);
802652610a2Sandi  return $info;
803652610a2Sandi}
804652610a2Sandi
805652610a2Sandi/**
806f3f0262cSandi * Saves a wikitext by calling io_saveFile
80715fae107Sandi *
80815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
809f3f0262cSandi */
810b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
811f3f0262cSandi  global $conf;
812f3f0262cSandi  global $lang;
813f3f0262cSandi  // ignore if no changes were made
814f3f0262cSandi  if($text == rawWiki($id,'')){
815f3f0262cSandi    return;
816f3f0262cSandi  }
817f3f0262cSandi
818f3f0262cSandi  $file = wikiFN($id);
819f3f0262cSandi  $old  = saveOldRevision($id);
820f3f0262cSandi
821f3f0262cSandi  if (empty($text)){
822e1f3d9e1SEsther Brunner    // remove empty file
823f3f0262cSandi    @unlink($file);
824e1f3d9e1SEsther Brunner    // remove any meta info
825e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
826e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
827e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
828b158d625SSteven Danz    }
829f3f0262cSandi    $del = true;
8303ce054b3Sandi    // autoset summary on deletion
8313ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
832*f864871eSAndreas Gohr    // unlock early
833*f864871eSAndreas Gohr    unlock($id);
83453d6ccfeSandi    // remove empty namespaces
83553d6ccfeSandi    io_sweepNS($id);
836f3f0262cSandi  }else{
837f3f0262cSandi    // save file (datadir is created in io_saveFile)
838f3f0262cSandi    io_saveFile($file,$text);
839f3f0262cSandi    $del = false;
840f3f0262cSandi  }
841f3f0262cSandi
842b6912aeaSAndreas Gohr  addLogEntry(@filemtime($file),$id,$summary,$minor);
84326a0801fSAndreas Gohr  // send notify mails
84490033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
84590033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
846f3f0262cSandi
847f3f0262cSandi  //purge cache on add by updating the purgefile
848f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
84998407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
850f3f0262cSandi  }
851f3f0262cSandi}
852f3f0262cSandi
853f3f0262cSandi/**
854f3f0262cSandi * moves the current version to the attic and returns its
855f3f0262cSandi * revision date
85615fae107Sandi *
85715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
858f3f0262cSandi */
859f3f0262cSandifunction saveOldRevision($id){
860f3f0262cSandi  global $conf;
861f3f0262cSandi  $oldf = wikiFN($id);
862f3f0262cSandi  if(!@file_exists($oldf)) return '';
863f3f0262cSandi  $date = filemtime($oldf);
864f3f0262cSandi  $newf = wikiFN($id,$date);
865f3f0262cSandi  if(substr($newf,-3)=='.gz'){
866f3f0262cSandi    io_saveFile($newf,rawWiki($id));
867f3f0262cSandi  }else{
868f3f0262cSandi    io_makeFileDir($newf);
869f3f0262cSandi    copy($oldf, $newf);
870f3f0262cSandi  }
871f3f0262cSandi  return $date;
872f3f0262cSandi}
873f3f0262cSandi
874f3f0262cSandi/**
87526a0801fSAndreas Gohr * Sends a notify mail on page change
87626a0801fSAndreas Gohr *
87726a0801fSAndreas Gohr * @param  string  $id       The changed page
87826a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
87926a0801fSAndreas Gohr * @param  int     $rev      Old page revision
88026a0801fSAndreas Gohr * @param  string  $summary  What changed
88190033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
88215fae107Sandi *
88315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
884f3f0262cSandi */
88590033e9dSAndreas Gohrfunction notify($id,$who,$rev='',$summary='',$minor=false){
886f3f0262cSandi  global $lang;
887f3f0262cSandi  global $conf;
888b158d625SSteven Danz
88926a0801fSAndreas Gohr  // decide if there is something to do
89026a0801fSAndreas Gohr  if($who == 'admin'){
89126a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
892f3f0262cSandi    $text = rawLocale('mailtext');
89326a0801fSAndreas Gohr    $to   = $conf['notify'];
89426a0801fSAndreas Gohr    $bcc  = '';
89526a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
89626a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
89790033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
89826a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
89926a0801fSAndreas Gohr    if(empty($bcc)) return;
90026a0801fSAndreas Gohr    $to   = '';
90126a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
90226a0801fSAndreas Gohr  }else{
90326a0801fSAndreas Gohr    return; //just to be safe
90426a0801fSAndreas Gohr  }
90526a0801fSAndreas Gohr
906f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
907f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
908f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
909f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
910ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
91126a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
91226a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
913ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
914f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
9157a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
916f3f0262cSandi
917f3f0262cSandi  if($rev){
918f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
919ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
920ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
921f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
922f3f0262cSandi                    split("\n",rawWiki($id)));
923f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
924f3f0262cSandi    $diff    = $dformat->format($df);
925f3f0262cSandi  }else{
926f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
927f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
928f3f0262cSandi    $diff = rawWiki($id);
929f3f0262cSandi  }
930f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
931241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
932f3f0262cSandi
93326a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
934f3f0262cSandi}
935f3f0262cSandi
93615fae107Sandi/**
93715fae107Sandi * Return a list of available page revisons
93815fae107Sandi *
93915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
94015fae107Sandi */
941f3f0262cSandifunction getRevisions($id){
942f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
943f3f0262cSandi  $revs = array();
944f3f0262cSandi  $clid = cleanID($id);
945f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
946493a6929SKobaYY  $clid = utf8_encodeFN($clid);
947f3f0262cSandi
948f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
949f3f0262cSandi    while (($file = readdir($dh)) !== false) {
950f3f0262cSandi      if (is_dir($revd.'/'.$file)) continue;
951f3f0262cSandi      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
952f3f0262cSandi        $revs[]=$match[1];
953f3f0262cSandi      }
954f3f0262cSandi    }
955f3f0262cSandi    closedir($dh);
956f3f0262cSandi  }
957f3f0262cSandi  rsort($revs);
958f3f0262cSandi  return $revs;
959f3f0262cSandi}
960f3f0262cSandi
961f3f0262cSandi/**
962f3f0262cSandi * extracts the query from a google referer
96315fae107Sandi *
9646b13307fSandi * @todo   should be more generic and support yahoo et al
96515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
966f3f0262cSandi */
967f3f0262cSandifunction getGoogleQuery(){
968f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
9695c3f206fSandi  if(!$url) return '';
970f3f0262cSandi
971f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
972f3f0262cSandi  $query = array();
973f3f0262cSandi  parse_str($url['query'],$query);
974f3f0262cSandi
975f3f0262cSandi  return $query['q'];
976f3f0262cSandi}
977f3f0262cSandi
978f3f0262cSandi/**
97915fae107Sandi * Try to set correct locale
98015fae107Sandi *
981095bfd5cSandi * @deprecated No longer used
98215fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
983f3f0262cSandi */
984f3f0262cSandifunction setCorrectLocale(){
985f3f0262cSandi  global $conf;
986f3f0262cSandi  global $lang;
987f3f0262cSandi
988f3f0262cSandi  $enc = strtoupper($lang['encoding']);
989f3f0262cSandi  foreach ($lang['locales'] as $loc){
990f3f0262cSandi    //try locale
991f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
992f3f0262cSandi    //try loceale with encoding
993f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
994f3f0262cSandi  }
995f3f0262cSandi  //still here? try to set from environment
996f3f0262cSandi  @setlocale(LC_ALL,"");
997f3f0262cSandi}
998f3f0262cSandi
999f3f0262cSandi/**
1000f3f0262cSandi * Return the human readable size of a file
1001f3f0262cSandi *
1002f3f0262cSandi * @param       int    $size   A file size
1003f3f0262cSandi * @param       int    $dec    A number of decimal places
1004f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
1005f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
1006f3f0262cSandi * @version     1.0.0
1007f3f0262cSandi */
1008f31d5b73Sandifunction filesize_h($size, $dec = 1){
1009f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
1010f3f0262cSandi  $count = count($sizes);
1011f3f0262cSandi  $i = 0;
1012f3f0262cSandi
1013f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
1014f3f0262cSandi    $size /= 1024;
1015f3f0262cSandi    $i++;
1016f3f0262cSandi  }
1017f3f0262cSandi
1018f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
1019f3f0262cSandi}
1020f3f0262cSandi
102115fae107Sandi/**
102200a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
102300a7b5adSEsther Brunner *
102400a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
102500a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
102600a7b5adSEsther Brunner */
102700a7b5adSEsther Brunnerfunction obfuscate($email) {
102800a7b5adSEsther Brunner  global $conf;
102900a7b5adSEsther Brunner
103000a7b5adSEsther Brunner  switch ($conf['mailguard']) {
103100a7b5adSEsther Brunner    case 'visible' :
103200a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
103300a7b5adSEsther Brunner      return strtr($email, $obfuscate);
103400a7b5adSEsther Brunner
103500a7b5adSEsther Brunner    case 'hex' :
103600a7b5adSEsther Brunner      $encode = '';
103700a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
103800a7b5adSEsther Brunner      return $encode;
103900a7b5adSEsther Brunner
104000a7b5adSEsther Brunner    case 'none' :
104100a7b5adSEsther Brunner    default :
104200a7b5adSEsther Brunner      return $email;
104300a7b5adSEsther Brunner  }
104400a7b5adSEsther Brunner}
104500a7b5adSEsther Brunner
104600a7b5adSEsther Brunner/**
1047dc57ef04Sandi * Return DokuWikis version
104815fae107Sandi *
104915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
105015fae107Sandi */
1051f31d5b73Sandifunction getVersion(){
1052f31d5b73Sandi  //import version string
1053f31d5b73Sandi  if(@file_exists('VERSION')){
1054f31d5b73Sandi    //official release
10550647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1056f31d5b73Sandi  }elseif(is_dir('_darcs')){
1057f31d5b73Sandi    //darcs checkout
1058f31d5b73Sandi    $inv = file('_darcs/inventory');
1059ae41559bSAndreas Gohr    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
1060f31d5b73Sandi    $cur = array_pop($inv);
1061f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1062f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1063f31d5b73Sandi  }else{
1064f31d5b73Sandi    return 'snapshot?';
1065f31d5b73Sandi  }
1066f31d5b73Sandi}
1067f31d5b73Sandi
1068f31d5b73Sandi/**
1069f31d5b73Sandi * Run a few sanity checks
1070f31d5b73Sandi *
1071f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
1072f31d5b73Sandi */
1073f3f0262cSandifunction check(){
1074f3f0262cSandi  global $conf;
1075f3f0262cSandi  global $INFO;
1076f3f0262cSandi
1077f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
1078f31d5b73Sandi
107949022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
108049022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
108149022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
108249022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
108349022a38Sandi  }else{
108449022a38Sandi    msg('PHP version '.phpversion(),1);
108549022a38Sandi  }
108649022a38Sandi
1087f3f0262cSandi  if(is_writable($conf['changelog'])){
1088f3f0262cSandi    msg('Changelog is writable',1);
1089f3f0262cSandi  }else{
1090f3f0262cSandi    msg('Changelog is not writable',-1);
1091f3f0262cSandi  }
1092f3f0262cSandi
1093f3f0262cSandi  if(is_writable($conf['datadir'])){
1094f3f0262cSandi    msg('Datadir is writable',1);
1095f3f0262cSandi  }else{
1096f3f0262cSandi    msg('Datadir is not writable',-1);
1097f3f0262cSandi  }
1098f3f0262cSandi
1099f3f0262cSandi  if(is_writable($conf['olddir'])){
1100f3f0262cSandi    msg('Attic is writable',1);
1101f3f0262cSandi  }else{
1102f3f0262cSandi    msg('Attic is not writable',-1);
1103f3f0262cSandi  }
1104f3f0262cSandi
1105f3f0262cSandi  if(is_writable($conf['mediadir'])){
1106f3f0262cSandi    msg('Mediadir is writable',1);
1107f3f0262cSandi  }else{
1108f3f0262cSandi    msg('Mediadir is not writable',-1);
1109f3f0262cSandi  }
1110f3f0262cSandi
111198407a7aSandi  if(is_writable($conf['cachedir'])){
111298407a7aSandi    msg('Cachedir is writable',1);
111398407a7aSandi  }else{
111498407a7aSandi    msg('Cachedir is not writable',-1);
111598407a7aSandi  }
111698407a7aSandi
1117e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
11188c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1119f3f0262cSandi  }else{
11208c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1121f3f0262cSandi  }
112293a9e835Sandi
112393a9e835Sandi  if(function_exists('mb_strpos')){
112493a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
112593a9e835Sandi      msg('mb_string extension is available but will not be used',0);
112693a9e835Sandi    }else{
112793a9e835Sandi      msg('mb_string extension is available and will be used',1);
112893a9e835Sandi    }
112993a9e835Sandi  }else{
113093a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
113193a9e835Sandi  }
1132f42d1c75SAndreas Gohr
1133f42d1c75SAndreas Gohr  if($conf['allowdebug']){
1134f42d1c75SAndreas Gohr    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1135f42d1c75SAndreas Gohr  }else{
1136f42d1c75SAndreas Gohr    msg('Debugging support is disabled',1);
1137f42d1c75SAndreas Gohr  }
1138f3f0262cSandi
1139f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1140f3f0262cSandi
1141f3f0262cSandi  if(is_writable($INFO['filepath'])){
1142f3f0262cSandi    msg('The current page is writable by the webserver',0);
1143f3f0262cSandi  }else{
1144f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1145f3f0262cSandi  }
1146f3f0262cSandi
1147f3f0262cSandi  if($INFO['writable']){
1148f3f0262cSandi    msg('The current page is writable by you',0);
1149f3f0262cSandi  }else{
1150f3f0262cSandi    msg('The current page is not writable you',0);
1151f3f0262cSandi  }
1152f3f0262cSandi}
1153340756e4Sandi
1154b158d625SSteven Danz/**
1155b158d625SSteven Danz * Let us know if a user is tracking a page
1156b158d625SSteven Danz *
11571380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1158b158d625SSteven Danz */
11591380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
11601380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
11611380fc45SAndreas Gohr  if (@file_exists($file)) {
1162b158d625SSteven Danz    $mlist = file($file);
11631380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
11641380fc45SAndreas Gohr    return is_int($pos);
1165b158d625SSteven Danz  }
11661380fc45SAndreas Gohr
1167b158d625SSteven Danz  return false;
1168b158d625SSteven Danz}
1169340756e4Sandi
1170f9eb5648Ssteven-danz/**
1171f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1172f9eb5648Ssteven-danz * users subscribed to a page
1173f9eb5648Ssteven-danz *
117426a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1175f9eb5648Ssteven-danz */
1176f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1177f9eb5648Ssteven-danz  global $conf;
1178cd52f92dSchris  global $auth;
1179f9eb5648Ssteven-danz
1180f9eb5648Ssteven-danz  $emails = '';
1181f9eb5648Ssteven-danz
118226a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
118326a0801fSAndreas Gohr
1184f9eb5648Ssteven-danz  $mlist = array();
1185f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1186f9eb5648Ssteven-danz  if (file_exists($file)) {
1187f9eb5648Ssteven-danz    $mlist = file($file);
1188f9eb5648Ssteven-danz  }
1189f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1190f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1191f9eb5648Ssteven-danz      $who = rtrim($who);
1192cd52f92dSchris      $info = $auth->getUserData($who);
1193f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1194f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1195f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1196f9eb5648Ssteven-danz          if (empty($emails)) {
1197f9eb5648Ssteven-danz            $emails = $info['mail'];
1198f9eb5648Ssteven-danz          } else {
1199f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1200f9eb5648Ssteven-danz          }
1201f9eb5648Ssteven-danz        }
1202f9eb5648Ssteven-danz      }
1203f9eb5648Ssteven-danz    }
1204f9eb5648Ssteven-danz  }
1205f9eb5648Ssteven-danz
1206f9eb5648Ssteven-danz  return $emails;
1207f9eb5648Ssteven-danz}
1208f9eb5648Ssteven-danz
120989541d4bSAndreas Gohr/**
121089541d4bSAndreas Gohr * Removes quoting backslashes
121189541d4bSAndreas Gohr *
121289541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
121389541d4bSAndreas Gohr */
121489541d4bSAndreas Gohrfunction unslash($string,$char="'"){
121589541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
121689541d4bSAndreas Gohr}
121789541d4bSAndreas Gohr
1218340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1219