xref: /dokuwiki/inc/common.php (revision 17ee7f669c94439a8c4526e5aba6c021922a9868)
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']);
39*17ee7f66SAndreas Gohr
40*17ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
41*17ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
42*17ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
43*17ee7f66SAndreas 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 */
972684e50aSAndreas Gohrfunction buildURLparams($params){
982684e50aSAndreas Gohr  $url = '';
992684e50aSAndreas Gohr  $amp = false;
1002684e50aSAndreas Gohr  foreach($params as $key => $val){
1012684e50aSAndreas Gohr    if($amp) $url .= '&amp;';
1022684e50aSAndreas Gohr
1032684e50aSAndreas Gohr    $url .= $key.'=';
1042684e50aSAndreas Gohr    $url .= urlencode($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){
235f3f0262cSandi    $id = urlencode($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 */
250ed7b5f09Sandifunction wl($id='',$more='',$abs=false){
251f3f0262cSandi  global $conf;
2526de3759aSAndreas Gohr  if(is_array($more)){
2536de3759aSAndreas Gohr    $more = buildURLparams($more);
2546de3759aSAndreas Gohr  }else{
255f3f0262cSandi    $more = str_replace(',','&amp;',$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;
2736c7843b5Sandi    if($more) $xlink .= '&amp;'.$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 */
2846de3759aSAndreas Gohrfunction ml($id='',$more='',$direct=true){
2856de3759aSAndreas Gohr  global $conf;
2866de3759aSAndreas Gohr  if(is_array($more)){
2876de3759aSAndreas Gohr    $more = buildURLparams($more);
2886de3759aSAndreas Gohr  }else{
2896de3759aSAndreas Gohr    $more = str_replace(',','&amp;',$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;
29948665d38SAndreas Gohr      $xlink .= '&amp;media='.urlencode($id);
3006de3759aSAndreas Gohr    }else{
30148665d38SAndreas Gohr      $xlink .= '?media='.urlencode($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;
3306de3759aSAndreas Gohr       $xlink .= '&amp;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
368e7cb32dcSAndreas Gohr  $blockfile = file(DOKU_CONF.'wordblock.conf');
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
3783e2965d7Sandi    $chunksize = 600;
3793e2965d7Sandi  }
3803e2965d7Sandi  while($blocks = array_splice($blockfile,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    }
389f3f0262cSandi    if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true;
390703f6fdeSandi  }
391f3f0262cSandi  return false;
392f3f0262cSandi}
393f3f0262cSandi
394f3f0262cSandi/**
39515fae107Sandi * Return the IP of the client
39615fae107Sandi *
39715fae107Sandi * Honours X-Forwarded-For Proxy Headers
39815fae107Sandi *
39915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
400f3f0262cSandi */
401f3f0262cSandifunction clientIP(){
402f3f0262cSandi  $my = $_SERVER['REMOTE_ADDR'];
403f3f0262cSandi  if($_SERVER['HTTP_X_FORWARDED_FOR']){
404f3f0262cSandi    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
405f3f0262cSandi  }
406f3f0262cSandi  return $my;
407f3f0262cSandi}
408f3f0262cSandi
409f3f0262cSandi/**
41015fae107Sandi * Checks if a given page is currently locked.
41115fae107Sandi *
412f3f0262cSandi * removes stale lockfiles
41315fae107Sandi *
41415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
415f3f0262cSandi */
416f3f0262cSandifunction checklock($id){
417f3f0262cSandi  global $conf;
418f3f0262cSandi  $lock = wikiFN($id).'.lock';
419f3f0262cSandi
420f3f0262cSandi  //no lockfile
421f3f0262cSandi  if(!@file_exists($lock)) return false;
422f3f0262cSandi
423f3f0262cSandi  //lockfile expired
424f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
425f3f0262cSandi    unlink($lock);
426f3f0262cSandi    return false;
427f3f0262cSandi  }
428f3f0262cSandi
429f3f0262cSandi  //my own lock
430f3f0262cSandi  $ip = io_readFile($lock);
431f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
432f3f0262cSandi    return false;
433f3f0262cSandi  }
434f3f0262cSandi
435f3f0262cSandi  return $ip;
436f3f0262cSandi}
437f3f0262cSandi
438f3f0262cSandi/**
43915fae107Sandi * Lock a page for editing
44015fae107Sandi *
44115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
442f3f0262cSandi */
443f3f0262cSandifunction lock($id){
444f3f0262cSandi  $lock = wikiFN($id).'.lock';
445f3f0262cSandi  if($_SERVER['REMOTE_USER']){
446f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
447f3f0262cSandi  }else{
448f3f0262cSandi    io_saveFile($lock,clientIP());
449f3f0262cSandi  }
450f3f0262cSandi}
451f3f0262cSandi
452f3f0262cSandi/**
45315fae107Sandi * Unlock a page if it was locked by the user
454f3f0262cSandi *
45515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
45615fae107Sandi * @return bool true if a lock was removed
457f3f0262cSandi */
458f3f0262cSandifunction unlock($id){
459f3f0262cSandi  $lock = wikiFN($id).'.lock';
460f3f0262cSandi  if(@file_exists($lock)){
461f3f0262cSandi    $ip = io_readFile($lock);
462f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
463f3f0262cSandi      @unlink($lock);
464f3f0262cSandi      return true;
465f3f0262cSandi    }
466f3f0262cSandi  }
467f3f0262cSandi  return false;
468f3f0262cSandi}
469f3f0262cSandi
470f3f0262cSandi/**
471f3f0262cSandi * convert line ending to unix format
472f3f0262cSandi *
47315fae107Sandi * @see    formText() for 2crlf conversion
47415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
475f3f0262cSandi */
476f3f0262cSandifunction cleanText($text){
477f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
478f3f0262cSandi  return $text;
479f3f0262cSandi}
480f3f0262cSandi
481f3f0262cSandi/**
482f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
483f3f0262cSandi * It also converts line endings to Windows format which is
484f3f0262cSandi * pseudo standard for webforms.
485f3f0262cSandi *
48615fae107Sandi * @see    cleanText() for 2unix conversion
48715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
488f3f0262cSandi */
489f3f0262cSandifunction formText($text){
490f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
491f3f0262cSandi  return htmlspecialchars($text);
492f3f0262cSandi}
493f3f0262cSandi
494f3f0262cSandi/**
49515fae107Sandi * Returns the specified local text in raw format
49615fae107Sandi *
49715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
498f3f0262cSandi */
499f3f0262cSandifunction rawLocale($id){
500f3f0262cSandi  return io_readFile(localeFN($id));
501f3f0262cSandi}
502f3f0262cSandi
503f3f0262cSandi/**
504f3f0262cSandi * Returns the raw WikiText
50515fae107Sandi *
50615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
507f3f0262cSandi */
508f3f0262cSandifunction rawWiki($id,$rev=''){
509f3f0262cSandi  return io_readFile(wikiFN($id,$rev));
510f3f0262cSandi}
511f3f0262cSandi
512f3f0262cSandi/**
5137146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
5147146cee2SAndreas Gohr *
5157146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5167146cee2SAndreas Gohr */
5177146cee2SAndreas Gohrfunction pageTemplate($id){
518a15ce62dSEsther Brunner  global $conf;
519a15ce62dSEsther Brunner  global $INFO;
520a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
521a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
522a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
523a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
524a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
525a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
526a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
527a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
528a15ce62dSEsther Brunner  return $tpl;
5297146cee2SAndreas Gohr}
5307146cee2SAndreas Gohr
5317146cee2SAndreas Gohr
5327146cee2SAndreas Gohr/**
53315fae107Sandi * Returns the raw Wiki Text in three slices.
53415fae107Sandi *
53515fae107Sandi * The range parameter needs to have the form "from-to"
53615cfe303Sandi * and gives the range of the section in bytes - no
53715cfe303Sandi * UTF-8 awareness is needed.
538f3f0262cSandi * The returned order is prefix, section and suffix.
53915fae107Sandi *
54015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
541f3f0262cSandi */
542f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
543f3f0262cSandi  list($from,$to) = split('-',$range,2);
544f3f0262cSandi  $text = io_readFile(wikiFN($id,$rev));
545f3f0262cSandi  if(!$from) $from = 0;
546c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
547f3f0262cSandi
54815cfe303Sandi  $slices[0] = substr($text,0,$from-1);
54915cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
55015cfe303Sandi  $slices[2] = substr($text,$to);
551f3f0262cSandi
552f3f0262cSandi  return $slices;
553f3f0262cSandi}
554f3f0262cSandi
555f3f0262cSandi/**
55615fae107Sandi * Joins wiki text slices
55715fae107Sandi *
558f3f0262cSandi * function to join the text slices with correct lineendings again.
559f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
560f3f0262cSandi * lines between sections if needed (used on saving).
56115fae107Sandi *
56215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
563f3f0262cSandi */
564f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
565f3f0262cSandi
566f3f0262cSandi  if($pretty){
567f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
568f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
569f3f0262cSandi  }
570f3f0262cSandi
571f3f0262cSandi  if($pre) $pre .= "\n";
572f3f0262cSandi  if($suf) $text .= "\n";
573f3f0262cSandi  return $pre.$text.$suf;
574f3f0262cSandi}
575f3f0262cSandi
576f3f0262cSandi/**
57715fae107Sandi * print debug messages
57815fae107Sandi *
579f3f0262cSandi * little function to print the content of a var
58015fae107Sandi *
58115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
582f3f0262cSandi */
583f3f0262cSandifunction dbg($msg,$hidden=false){
584f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
585f3f0262cSandi  print_r($msg);
586f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
587f3f0262cSandi}
588f3f0262cSandi
589f3f0262cSandi/**
590f3f0262cSandi * Add's an entry to the changelog
59115fae107Sandi *
59215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
593f3f0262cSandi */
594b6912aeaSAndreas Gohrfunction addLogEntry($date,$id,$summary='',$minor=false){
595f3f0262cSandi  global $conf;
596c1049928Sandi
597c1049928Sandi  if(!@is_writable($conf['changelog'])){
598c1049928Sandi    msg($conf['changelog'].' is not writable!',-1);
599c1049928Sandi    return;
600c1049928Sandi  }
601c1049928Sandi
602652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
603f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
604f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
605f3f0262cSandi
606b6912aeaSAndreas Gohr  if($conf['useacl'] && $user && $minor){
607b6912aeaSAndreas Gohr    $summary = '*'.$summary;
608b6912aeaSAndreas Gohr  }else{
609b6912aeaSAndreas Gohr    $summary = ' '.$summary;
610b6912aeaSAndreas Gohr  }
611b6912aeaSAndreas Gohr
612f3f0262cSandi  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
613dbb00abcSEsther Brunner  io_saveFile($conf['changelog'],$logline,true);
614f3f0262cSandi}
615f3f0262cSandi
616f3f0262cSandi/**
617b6912aeaSAndreas Gohr * Checks an summary entry if it was a minor edit
618b6912aeaSAndreas Gohr *
619b6912aeaSAndreas Gohr * The summary is cleaned of the marker char
620b6912aeaSAndreas Gohr *
621b6912aeaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
622b6912aeaSAndreas Gohr */
623b6912aeaSAndreas Gohrfunction isMinor(&$summary){
624b6912aeaSAndreas Gohr  if(substr($summary,0,1) == '*'){
625b6912aeaSAndreas Gohr    $summary = substr($summary,1);
626b6912aeaSAndreas Gohr    return true;
627b6912aeaSAndreas Gohr  }
628b6912aeaSAndreas Gohr  $summary = trim($summary);
629b6912aeaSAndreas Gohr  return false;
630b6912aeaSAndreas Gohr}
631b6912aeaSAndreas Gohr
632b6912aeaSAndreas Gohr/**
633d437bcc4SAndreas Gohr * Internal function used by getRecents
634d437bcc4SAndreas Gohr *
635d437bcc4SAndreas Gohr * don't call directly
636d437bcc4SAndreas Gohr *
637d437bcc4SAndreas Gohr * @see getRecents()
638d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
639d437bcc4SAndreas Gohr */
640b6912aeaSAndreas Gohrfunction _handleRecent($line,$ns,$flags){
641d437bcc4SAndreas Gohr  static $seen  = array();         //caches seen pages and skip them
642d437bcc4SAndreas Gohr  if(empty($line)) return false;   //skip empty lines
643d437bcc4SAndreas Gohr
644d437bcc4SAndreas Gohr  // split the line into parts
645d437bcc4SAndreas Gohr  list($dt,$ip,$id,$usr,$sum) = explode("\t",$line);
646d437bcc4SAndreas Gohr
647d437bcc4SAndreas Gohr  // skip seen ones
648d437bcc4SAndreas Gohr  if($seen[$id]) return false;
649b6912aeaSAndreas Gohr  $recent = array();
650b6912aeaSAndreas Gohr
651b6912aeaSAndreas Gohr  // check minors
652b6912aeaSAndreas Gohr  if(isMinor($sum)){
653b6912aeaSAndreas Gohr    // skip minors
654b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_MINORS) return false;
655b6912aeaSAndreas Gohr    $recent['minor'] = true;
656b6912aeaSAndreas Gohr  }else{
657b6912aeaSAndreas Gohr    $recent['minor'] = false;
658b6912aeaSAndreas Gohr  }
659d437bcc4SAndreas Gohr
660d437bcc4SAndreas Gohr  // remember in seen to skip additional sights
661d437bcc4SAndreas Gohr  $seen[$id] = 1;
662d437bcc4SAndreas Gohr
663d437bcc4SAndreas Gohr  // filter namespace
664d437bcc4SAndreas Gohr  if (($ns) && (strpos($id,$ns.':') !== 0)) return false;
665d437bcc4SAndreas Gohr
666d437bcc4SAndreas Gohr  // exclude subnamespaces
667b6912aeaSAndreas Gohr  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($id) != $ns)) return false;
668d437bcc4SAndreas Gohr
669ae56bfb6SAndreas Gohr  // check ACL
670ae56bfb6SAndreas Gohr  if (auth_quickaclcheck($id) < AUTH_READ) return false;
671ae56bfb6SAndreas Gohr
672d437bcc4SAndreas Gohr  // check existance
673d437bcc4SAndreas Gohr  if(!@file_exists(wikiFN($id))){
674b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_DELETED){
675d437bcc4SAndreas Gohr      return false;
676d437bcc4SAndreas Gohr    }else{
677d437bcc4SAndreas Gohr      $recent['del'] = true;
678d437bcc4SAndreas Gohr    }
679d437bcc4SAndreas Gohr  }else{
680d437bcc4SAndreas Gohr    $recent['del'] = false;
681d437bcc4SAndreas Gohr  }
682d437bcc4SAndreas Gohr
683d437bcc4SAndreas Gohr  $recent['id']   = $id;
684d437bcc4SAndreas Gohr  $recent['date'] = $dt;
685d437bcc4SAndreas Gohr  $recent['ip']   = $ip;
686d437bcc4SAndreas Gohr  $recent['user'] = $usr;
687d437bcc4SAndreas Gohr  $recent['sum']  = $sum;
688d437bcc4SAndreas Gohr
689d437bcc4SAndreas Gohr  return $recent;
690d437bcc4SAndreas Gohr}
691d437bcc4SAndreas Gohr
692b6912aeaSAndreas Gohr
693d437bcc4SAndreas Gohr/**
694f3f0262cSandi * returns an array of recently changed files using the
695f3f0262cSandi * changelog
696d437bcc4SAndreas Gohr *
697b6912aeaSAndreas Gohr * The following constants can be used to control which changes are
698b6912aeaSAndreas Gohr * included. Add them together as needed.
699b6912aeaSAndreas Gohr *
700b6912aeaSAndreas Gohr * RECENTS_SKIP_DELETED   - don't include deleted pages
701b6912aeaSAndreas Gohr * RECENTS_SKIP_MINORS    - don't include minor changes
702b6912aeaSAndreas Gohr * RECENTS_SKIP_SUBSPACES - don't include subspaces
703b6912aeaSAndreas Gohr *
704d437bcc4SAndreas Gohr * @param int    $first   number of first entry returned (for paginating
705d437bcc4SAndreas Gohr * @param int    $num     return $num entries
706d437bcc4SAndreas Gohr * @param string $ns      restrict to given namespace
707b6912aeaSAndreas Gohr * @param bool   $flags   see above
70815fae107Sandi *
70915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
710f3f0262cSandi */
711b6912aeaSAndreas Gohrfunction getRecents($first,$num,$ns='',$flags=0){
712f3f0262cSandi  global $conf;
713f3f0262cSandi  $recent = array();
714d437bcc4SAndreas Gohr  $count  = 0;
7155749f1ceSmatthiasgrimm
7165749f1ceSmatthiasgrimm  if(!$num)
7175749f1ceSmatthiasgrimm    return $recent;
718f3f0262cSandi
719c1049928Sandi  if(!@is_readable($conf['changelog'])){
720c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
721c1049928Sandi    return $recent;
722c1049928Sandi  }
723c1049928Sandi
724d437bcc4SAndreas Gohr  $fh  = fopen($conf['changelog'],'r');
725d437bcc4SAndreas Gohr  $buf = '';
726d437bcc4SAndreas Gohr  $csz = 4096;                              //chunksize
727d437bcc4SAndreas Gohr  fseek($fh,0,SEEK_END);                    // jump to the end
728d437bcc4SAndreas Gohr  $pos = ftell($fh);                        // position pointer
729f3f0262cSandi
730d437bcc4SAndreas Gohr  // now read backwards into buffer
731d437bcc4SAndreas Gohr  while($pos > 0){
732d437bcc4SAndreas Gohr    $pos -= $csz;                           // seek to previous chunk...
733d437bcc4SAndreas Gohr    if($pos < 0) $pos = 0;                  // ...or rest of file
734d437bcc4SAndreas Gohr    fseek($fh,$pos);
735dbb00abcSEsther Brunner
736d437bcc4SAndreas Gohr    $buf = fread($fh,$csz).$buf;            // prepend to buffer
7378f1d587cSEsther Brunner
738d437bcc4SAndreas Gohr    $lines = explode("\n",$buf);            // split buffer into lines
7395749f1ceSmatthiasgrimm
740d437bcc4SAndreas Gohr    if($pos > 0){
741d437bcc4SAndreas Gohr      $buf = array_shift($lines);           // first one may be still incomplete
742f3f0262cSandi    }
743d437bcc4SAndreas Gohr
744d437bcc4SAndreas Gohr    $cnt = count($lines);
745d437bcc4SAndreas Gohr    if(!$cnt) continue;                     // no lines yet
746d437bcc4SAndreas Gohr
747d437bcc4SAndreas Gohr    // handle lines
748d437bcc4SAndreas Gohr    for($i = $cnt-1; $i >= 0; $i--){
749b6912aeaSAndreas Gohr      $rec = _handleRecent($lines[$i],$ns,$flags);
750d437bcc4SAndreas Gohr      if($rec !== false){
751d437bcc4SAndreas Gohr        if(--$first >= 0) continue;         // skip first entries
752d437bcc4SAndreas Gohr        $recent[] = $rec;
753d437bcc4SAndreas Gohr        $count++;
754d437bcc4SAndreas Gohr
755d437bcc4SAndreas Gohr        // break while when we have enough entries
756d437bcc4SAndreas Gohr        if($count >= $num){
757d437bcc4SAndreas Gohr          $pos = 0; // will break the while loop
758d437bcc4SAndreas Gohr          break;    // will break the for loop
759f3f0262cSandi        }
760f3f0262cSandi      }
761d437bcc4SAndreas Gohr    }
762d437bcc4SAndreas Gohr  }// end of while
763d437bcc4SAndreas Gohr
764d437bcc4SAndreas Gohr  fclose($fh);
765f3f0262cSandi  return $recent;
766f3f0262cSandi}
767f3f0262cSandi
768f3f0262cSandi/**
769652610a2Sandi * gets additonal informations for a certain pagerevison
770652610a2Sandi * from the changelog
771652610a2Sandi *
772652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
773652610a2Sandi */
774652610a2Sandifunction getRevisionInfo($id,$rev){
775652610a2Sandi  global $conf;
776258641c6Sandi
777258641c6Sandi  if(!$rev) return(null);
778258641c6Sandi
779c1049928Sandi  $info = array();
780c1049928Sandi  if(!@is_readable($conf['changelog'])){
781c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
782c1049928Sandi    return $recent;
783c1049928Sandi  }
784652610a2Sandi  $loglines = file($conf['changelog']);
785652610a2Sandi  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
786dc42ff59Sandi  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
787652610a2Sandi  $line = split("\t",$loglines[0]);
788652610a2Sandi  $info['date']  = $line[0];
789652610a2Sandi  $info['ip']    = $line[1];
790652610a2Sandi  $info['user']  = $line[3];
791652610a2Sandi  $info['sum']   = $line[4];
792b6912aeaSAndreas Gohr  $info['minor'] = isMinor($info['sum']);
793652610a2Sandi  return $info;
794652610a2Sandi}
795652610a2Sandi
796652610a2Sandi/**
797f3f0262cSandi * Saves a wikitext by calling io_saveFile
79815fae107Sandi *
79915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
800f3f0262cSandi */
801b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
802f3f0262cSandi  global $conf;
803f3f0262cSandi  global $lang;
804f3f0262cSandi  umask($conf['umask']);
805f3f0262cSandi  // ignore if no changes were made
806f3f0262cSandi  if($text == rawWiki($id,'')){
807f3f0262cSandi    return;
808f3f0262cSandi  }
809f3f0262cSandi
810f3f0262cSandi  $file = wikiFN($id);
811f3f0262cSandi  $old  = saveOldRevision($id);
812f3f0262cSandi
813f3f0262cSandi  if (empty($text)){
814e1f3d9e1SEsther Brunner    // remove empty file
815f3f0262cSandi    @unlink($file);
816e1f3d9e1SEsther Brunner    // remove any meta info
817e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
818e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
819e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
820b158d625SSteven Danz    }
821f3f0262cSandi    $del = true;
8223ce054b3Sandi    //autoset summary on deletion
8233ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
82453d6ccfeSandi    //remove empty namespaces
82553d6ccfeSandi    io_sweepNS($id);
826f3f0262cSandi  }else{
827f3f0262cSandi    // save file (datadir is created in io_saveFile)
828f3f0262cSandi    io_saveFile($file,$text);
829f3f0262cSandi    $del = false;
830f3f0262cSandi  }
831f3f0262cSandi
832b6912aeaSAndreas Gohr  addLogEntry(@filemtime($file),$id,$summary,$minor);
83326a0801fSAndreas Gohr  // send notify mails
83490033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
83590033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
836f3f0262cSandi
837f3f0262cSandi  //purge cache on add by updating the purgefile
838f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
83998407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
840f3f0262cSandi  }
841f3f0262cSandi}
842f3f0262cSandi
843f3f0262cSandi/**
844f3f0262cSandi * moves the current version to the attic and returns its
845f3f0262cSandi * revision date
84615fae107Sandi *
84715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
848f3f0262cSandi */
849f3f0262cSandifunction saveOldRevision($id){
850f3f0262cSandi	global $conf;
851f3f0262cSandi  umask($conf['umask']);
852f3f0262cSandi  $oldf = wikiFN($id);
853f3f0262cSandi  if(!@file_exists($oldf)) return '';
854f3f0262cSandi  $date = filemtime($oldf);
855f3f0262cSandi  $newf = wikiFN($id,$date);
856f3f0262cSandi  if(substr($newf,-3)=='.gz'){
857f3f0262cSandi    io_saveFile($newf,rawWiki($id));
858f3f0262cSandi  }else{
859f3f0262cSandi    io_makeFileDir($newf);
860f3f0262cSandi    copy($oldf, $newf);
861f3f0262cSandi  }
862f3f0262cSandi  return $date;
863f3f0262cSandi}
864f3f0262cSandi
865f3f0262cSandi/**
86626a0801fSAndreas Gohr * Sends a notify mail on page change
86726a0801fSAndreas Gohr *
86826a0801fSAndreas Gohr * @param  string  $id       The changed page
86926a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
87026a0801fSAndreas Gohr * @param  int     $rev      Old page revision
87126a0801fSAndreas Gohr * @param  string  $summary  What changed
87290033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
87315fae107Sandi *
87415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
875f3f0262cSandi */
87690033e9dSAndreas Gohrfunction notify($id,$who,$rev='',$summary='',$minor=false){
877f3f0262cSandi  global $lang;
878f3f0262cSandi  global $conf;
879b158d625SSteven Danz
88026a0801fSAndreas Gohr  // decide if there is something to do
88126a0801fSAndreas Gohr  if($who == 'admin'){
88226a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
883f3f0262cSandi    $text = rawLocale('mailtext');
88426a0801fSAndreas Gohr    $to   = $conf['notify'];
88526a0801fSAndreas Gohr    $bcc  = '';
88626a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
88726a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
88890033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
88926a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
89026a0801fSAndreas Gohr    if(empty($bcc)) return;
89126a0801fSAndreas Gohr    $to   = '';
89226a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
89326a0801fSAndreas Gohr  }else{
89426a0801fSAndreas Gohr    return; //just to be safe
89526a0801fSAndreas Gohr  }
89626a0801fSAndreas Gohr
897f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
898f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
899f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
900f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
901ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
90226a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
90326a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
904ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
905f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
9067a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
907f3f0262cSandi
908f3f0262cSandi  if($rev){
909f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
910ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
911f3f0262cSandi    require_once("inc/DifferenceEngine.php");
912f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
913f3f0262cSandi                    split("\n",rawWiki($id)));
914f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
915f3f0262cSandi    $diff    = $dformat->format($df);
916f3f0262cSandi  }else{
917f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
918f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
919f3f0262cSandi    $diff = rawWiki($id);
920f3f0262cSandi  }
921f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
922241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
923f3f0262cSandi
92426a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
925f3f0262cSandi}
926f3f0262cSandi
92715fae107Sandi/**
92815fae107Sandi * Return a list of available page revisons
92915fae107Sandi *
93015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
93115fae107Sandi */
932f3f0262cSandifunction getRevisions($id){
933f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
934f3f0262cSandi  $revs = array();
935f3f0262cSandi  $clid = cleanID($id);
936f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
937493a6929SKobaYY  $clid = utf8_encodeFN($clid);
938f3f0262cSandi
939f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
940f3f0262cSandi    while (($file = readdir($dh)) !== false) {
941f3f0262cSandi      if (is_dir($revd.'/'.$file)) continue;
942f3f0262cSandi      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
943f3f0262cSandi        $revs[]=$match[1];
944f3f0262cSandi      }
945f3f0262cSandi    }
946f3f0262cSandi    closedir($dh);
947f3f0262cSandi  }
948f3f0262cSandi  rsort($revs);
949f3f0262cSandi  return $revs;
950f3f0262cSandi}
951f3f0262cSandi
952f3f0262cSandi/**
953f3f0262cSandi * extracts the query from a google referer
95415fae107Sandi *
9556b13307fSandi * @todo   should be more generic and support yahoo et al
95615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
957f3f0262cSandi */
958f3f0262cSandifunction getGoogleQuery(){
959f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
9605c3f206fSandi  if(!$url) return '';
961f3f0262cSandi
962f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
963f3f0262cSandi  $query = array();
964f3f0262cSandi  parse_str($url['query'],$query);
965f3f0262cSandi
966f3f0262cSandi  return $query['q'];
967f3f0262cSandi}
968f3f0262cSandi
969f3f0262cSandi/**
97015fae107Sandi * Try to set correct locale
97115fae107Sandi *
972095bfd5cSandi * @deprecated No longer used
97315fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
974f3f0262cSandi */
975f3f0262cSandifunction setCorrectLocale(){
976f3f0262cSandi  global $conf;
977f3f0262cSandi  global $lang;
978f3f0262cSandi
979f3f0262cSandi  $enc = strtoupper($lang['encoding']);
980f3f0262cSandi  foreach ($lang['locales'] as $loc){
981f3f0262cSandi    //try locale
982f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
983f3f0262cSandi    //try loceale with encoding
984f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
985f3f0262cSandi  }
986f3f0262cSandi  //still here? try to set from environment
987f3f0262cSandi  @setlocale(LC_ALL,"");
988f3f0262cSandi}
989f3f0262cSandi
990f3f0262cSandi/**
991f3f0262cSandi * Return the human readable size of a file
992f3f0262cSandi *
993f3f0262cSandi * @param       int    $size   A file size
994f3f0262cSandi * @param       int    $dec    A number of decimal places
995f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
996f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
997f3f0262cSandi * @version     1.0.0
998f3f0262cSandi */
999f31d5b73Sandifunction filesize_h($size, $dec = 1){
1000f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
1001f3f0262cSandi  $count = count($sizes);
1002f3f0262cSandi  $i = 0;
1003f3f0262cSandi
1004f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
1005f3f0262cSandi    $size /= 1024;
1006f3f0262cSandi    $i++;
1007f3f0262cSandi  }
1008f3f0262cSandi
1009f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
1010f3f0262cSandi}
1011f3f0262cSandi
101215fae107Sandi/**
101300a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
101400a7b5adSEsther Brunner *
101500a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
101600a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
101700a7b5adSEsther Brunner */
101800a7b5adSEsther Brunnerfunction obfuscate($email) {
101900a7b5adSEsther Brunner  global $conf;
102000a7b5adSEsther Brunner
102100a7b5adSEsther Brunner  switch ($conf['mailguard']) {
102200a7b5adSEsther Brunner    case 'visible' :
102300a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
102400a7b5adSEsther Brunner      return strtr($email, $obfuscate);
102500a7b5adSEsther Brunner
102600a7b5adSEsther Brunner    case 'hex' :
102700a7b5adSEsther Brunner      $encode = '';
102800a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
102900a7b5adSEsther Brunner      return $encode;
103000a7b5adSEsther Brunner
103100a7b5adSEsther Brunner    case 'none' :
103200a7b5adSEsther Brunner    default :
103300a7b5adSEsther Brunner      return $email;
103400a7b5adSEsther Brunner  }
103500a7b5adSEsther Brunner}
103600a7b5adSEsther Brunner
103700a7b5adSEsther Brunner/**
1038dc57ef04Sandi * Return DokuWikis version
103915fae107Sandi *
104015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
104115fae107Sandi */
1042f31d5b73Sandifunction getVersion(){
1043f31d5b73Sandi  //import version string
1044f31d5b73Sandi  if(@file_exists('VERSION')){
1045f31d5b73Sandi    //official release
10460647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1047f31d5b73Sandi  }elseif(is_dir('_darcs')){
1048f31d5b73Sandi    //darcs checkout
1049f31d5b73Sandi    $inv = file('_darcs/inventory');
1050f31d5b73Sandi    $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv);
1051f31d5b73Sandi    $cur = array_pop($inv);
1052f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1053f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1054f31d5b73Sandi  }else{
1055f31d5b73Sandi    return 'snapshot?';
1056f31d5b73Sandi  }
1057f31d5b73Sandi}
1058f31d5b73Sandi
1059f31d5b73Sandi/**
1060f31d5b73Sandi * Run a few sanity checks
1061f31d5b73Sandi *
1062f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
1063f31d5b73Sandi */
1064f3f0262cSandifunction check(){
1065f3f0262cSandi  global $conf;
1066f3f0262cSandi  global $INFO;
1067f3f0262cSandi
1068f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
1069f31d5b73Sandi
107049022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
107149022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
107249022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
107349022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
107449022a38Sandi  }else{
107549022a38Sandi    msg('PHP version '.phpversion(),1);
107649022a38Sandi  }
107749022a38Sandi
1078f3f0262cSandi  if(is_writable($conf['changelog'])){
1079f3f0262cSandi    msg('Changelog is writable',1);
1080f3f0262cSandi  }else{
1081f3f0262cSandi    msg('Changelog is not writable',-1);
1082f3f0262cSandi  }
1083f3f0262cSandi
1084f3f0262cSandi  if(is_writable($conf['datadir'])){
1085f3f0262cSandi    msg('Datadir is writable',1);
1086f3f0262cSandi  }else{
1087f3f0262cSandi    msg('Datadir is not writable',-1);
1088f3f0262cSandi  }
1089f3f0262cSandi
1090f3f0262cSandi  if(is_writable($conf['olddir'])){
1091f3f0262cSandi    msg('Attic is writable',1);
1092f3f0262cSandi  }else{
1093f3f0262cSandi    msg('Attic is not writable',-1);
1094f3f0262cSandi  }
1095f3f0262cSandi
1096f3f0262cSandi  if(is_writable($conf['mediadir'])){
1097f3f0262cSandi    msg('Mediadir is writable',1);
1098f3f0262cSandi  }else{
1099f3f0262cSandi    msg('Mediadir is not writable',-1);
1100f3f0262cSandi  }
1101f3f0262cSandi
110298407a7aSandi  if(is_writable($conf['cachedir'])){
110398407a7aSandi    msg('Cachedir is writable',1);
110498407a7aSandi  }else{
110598407a7aSandi    msg('Cachedir is not writable',-1);
110698407a7aSandi  }
110798407a7aSandi
1108e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
11098c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1110f3f0262cSandi  }else{
11118c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1112f3f0262cSandi  }
111393a9e835Sandi
111493a9e835Sandi  if(function_exists('mb_strpos')){
111593a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
111693a9e835Sandi      msg('mb_string extension is available but will not be used',0);
111793a9e835Sandi    }else{
111893a9e835Sandi      msg('mb_string extension is available and will be used',1);
111993a9e835Sandi    }
112093a9e835Sandi  }else{
112193a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
112293a9e835Sandi  }
1123f3f0262cSandi
1124f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1125f3f0262cSandi
1126f3f0262cSandi  if(is_writable($INFO['filepath'])){
1127f3f0262cSandi    msg('The current page is writable by the webserver',0);
1128f3f0262cSandi  }else{
1129f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1130f3f0262cSandi  }
1131f3f0262cSandi
1132f3f0262cSandi  if($INFO['writable']){
1133f3f0262cSandi    msg('The current page is writable by you',0);
1134f3f0262cSandi  }else{
1135f3f0262cSandi    msg('The current page is not writable you',0);
1136f3f0262cSandi  }
1137f3f0262cSandi}
1138340756e4Sandi
1139b158d625SSteven Danz/**
1140b158d625SSteven Danz * Let us know if a user is tracking a page
1141b158d625SSteven Danz *
11421380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1143b158d625SSteven Danz */
11441380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
11451380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
11461380fc45SAndreas Gohr  if (@file_exists($file)) {
1147b158d625SSteven Danz    $mlist = file($file);
11481380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
11491380fc45SAndreas Gohr    return is_int($pos);
1150b158d625SSteven Danz  }
11511380fc45SAndreas Gohr
1152b158d625SSteven Danz  return false;
1153b158d625SSteven Danz}
1154340756e4Sandi
1155f9eb5648Ssteven-danz/**
1156f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1157f9eb5648Ssteven-danz * users subscribed to a page
1158f9eb5648Ssteven-danz *
115926a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1160f9eb5648Ssteven-danz */
1161f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1162f9eb5648Ssteven-danz  global $conf;
1163f9eb5648Ssteven-danz
1164f9eb5648Ssteven-danz  $emails = '';
1165f9eb5648Ssteven-danz
116626a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
116726a0801fSAndreas Gohr
1168f9eb5648Ssteven-danz  $mlist = array();
1169f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1170f9eb5648Ssteven-danz  if (file_exists($file)) {
1171f9eb5648Ssteven-danz    $mlist = file($file);
1172f9eb5648Ssteven-danz  }
1173f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1174f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1175f9eb5648Ssteven-danz      $who = rtrim($who);
1176f9eb5648Ssteven-danz      $info = auth_getUserData($who);
1177f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1178f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1179f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1180f9eb5648Ssteven-danz          if (empty($emails)) {
1181f9eb5648Ssteven-danz            $emails = $info['mail'];
1182f9eb5648Ssteven-danz          } else {
1183f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1184f9eb5648Ssteven-danz          }
1185f9eb5648Ssteven-danz        }
1186f9eb5648Ssteven-danz      }
1187f9eb5648Ssteven-danz    }
1188f9eb5648Ssteven-danz  }
1189f9eb5648Ssteven-danz
1190f9eb5648Ssteven-danz  return $emails;
1191f9eb5648Ssteven-danz}
1192f9eb5648Ssteven-danz
119389541d4bSAndreas Gohr/**
119489541d4bSAndreas Gohr * Removes quoting backslashes
119589541d4bSAndreas Gohr *
119689541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
119789541d4bSAndreas Gohr */
119889541d4bSAndreas Gohrfunction unslash($string,$char="'"){
119989541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
120089541d4bSAndreas Gohr}
120189541d4bSAndreas Gohr
1202340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1203