xref: /dokuwiki/inc/common.php (revision 0d58d74eab8881c7e3e9b7e99e90b87b51ec87b4)
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 */
143*0d58d74eSAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){
144f3f0262cSandi  global $MSG;
145f3f0262cSandi  $errors[-1] = 'error';
146f3f0262cSandi  $errors[0]  = 'info';
147f3f0262cSandi  $errors[1]  = 'success';
148f3f0262cSandi
149*0d58d74eSAndreas Gohr  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
150*0d58d74eSAndreas Gohr
151cc20ad51Sandi  if(!headers_sent()){
152f3f0262cSandi    if(!isset($MSG)) $MSG = array();
153f3f0262cSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
1540396becbSandi  }else{
1550396becbSandi    $MSG = array();
1560396becbSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
157f62ea8a1Sandi    if(function_exists('html_msgarea')){
1580396becbSandi      html_msgarea();
159f62ea8a1Sandi    }else{
160f62ea8a1Sandi      print "ERROR($lvl) $message";
161f62ea8a1Sandi    }
1620396becbSandi  }
163f3f0262cSandi}
164f3f0262cSandi
165f3f0262cSandi/**
16615fae107Sandi * This builds the breadcrumb trail and returns it as array
16715fae107Sandi *
16815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
169f3f0262cSandi */
170f3f0262cSandifunction breadcrumbs(){
1718746e727Sandi  // we prepare the breadcrumbs early for quick session closing
1728746e727Sandi  static $crumbs = null;
1738746e727Sandi  if($crumbs != null) return $crumbs;
1748746e727Sandi
175f3f0262cSandi  global $ID;
176f3f0262cSandi  global $ACT;
177f3f0262cSandi  global $conf;
178f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
179f3f0262cSandi
180f3f0262cSandi  //first visit?
181f3f0262cSandi  if (!is_array($crumbs)){
182f3f0262cSandi    $crumbs = array();
183f3f0262cSandi  }
184f3f0262cSandi  //we only save on show and existing wiki documents
185a77f5846Sjan  $file = wikiFN($ID);
186a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
187f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
188f3f0262cSandi    return $crumbs;
189f3f0262cSandi  }
190a77f5846Sjan
191a77f5846Sjan  // page names
192a77f5846Sjan  $name = noNS($ID);
193a77f5846Sjan  if ($conf['useheading']) {
194a77f5846Sjan    // get page title
195bb0a59d4Sjan    $title = p_get_first_heading($ID);
196a77f5846Sjan    if ($title) {
197a77f5846Sjan      $name = $title;
198a77f5846Sjan    }
199a77f5846Sjan  }
200a77f5846Sjan
201f3f0262cSandi  //remove ID from array
202a77f5846Sjan  if (isset($crumbs[$ID])) {
203a77f5846Sjan    unset($crumbs[$ID]);
204f3f0262cSandi  }
205f3f0262cSandi
206f3f0262cSandi  //add to array
207a77f5846Sjan  $crumbs[$ID] = $name;
208f3f0262cSandi  //reduce size
209f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
210f3f0262cSandi    array_shift($crumbs);
211f3f0262cSandi  }
212f3f0262cSandi  //save to session
213f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
214f3f0262cSandi  return $crumbs;
215f3f0262cSandi}
216f3f0262cSandi
217f3f0262cSandi/**
21815fae107Sandi * Filter for page IDs
21915fae107Sandi *
220f3f0262cSandi * This is run on a ID before it is outputted somewhere
221f3f0262cSandi * currently used to replace the colon with something else
222f3f0262cSandi * on Windows systems and to have proper URL encoding
22315fae107Sandi *
22449c713a3Sandi * Urlencoding is ommitted when the second parameter is false
22549c713a3Sandi *
22615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
227f3f0262cSandi */
22849c713a3Sandifunction idfilter($id,$ue=true){
229f3f0262cSandi  global $conf;
230f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
231f3f0262cSandi    $id = strtr($id,':','/');
232f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
233f3f0262cSandi      $conf['userewrite']) {
234f3f0262cSandi    $id = strtr($id,':',';');
235f3f0262cSandi  }
23649c713a3Sandi  if($ue){
237b6c6979fSAndreas Gohr    $id = rawurlencode($id);
238f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
239f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
24049c713a3Sandi  }
241f3f0262cSandi  return $id;
242f3f0262cSandi}
243f3f0262cSandi
244f3f0262cSandi/**
245ed7b5f09Sandi * This builds a link to a wikipage
24615fae107Sandi *
2476c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2486c7843b5Sandi * given in $more
2496c7843b5Sandi *
25015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
251f3f0262cSandi */
252b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
253f3f0262cSandi  global $conf;
2546de3759aSAndreas Gohr  if(is_array($more)){
255b174aeaeSchris    $more = buildURLparams($more,$sep);
2566de3759aSAndreas Gohr  }else{
257b174aeaeSchris    $more = str_replace(',',$sep,$more);
2586de3759aSAndreas Gohr  }
259f3f0262cSandi
260f3f0262cSandi  $id    = idfilter($id);
261ed7b5f09Sandi  if($abs){
262ed7b5f09Sandi    $xlink = DOKU_URL;
263ed7b5f09Sandi  }else{
264ed7b5f09Sandi    $xlink = DOKU_BASE;
265ed7b5f09Sandi  }
266f3f0262cSandi
2676c7843b5Sandi  if($conf['userewrite'] == 2){
2686c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
2696c7843b5Sandi    if($more) $xlink .= '?'.$more;
2706c7843b5Sandi  }elseif($conf['userewrite']){
271f3f0262cSandi    $xlink .= $id;
272f3f0262cSandi    if($more) $xlink .= '?'.$more;
2736c7843b5Sandi  }else{
2746c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
275b174aeaeSchris    if($more) $xlink .= $sep.$more;
276f3f0262cSandi  }
277f3f0262cSandi
278f3f0262cSandi  return $xlink;
279f3f0262cSandi}
280f3f0262cSandi
281f3f0262cSandi/**
2826de3759aSAndreas Gohr * Build a link to a media file
2836de3759aSAndreas Gohr *
2846de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
2856de3759aSAndreas Gohr */
286b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
2876de3759aSAndreas Gohr  global $conf;
2886de3759aSAndreas Gohr  if(is_array($more)){
289b174aeaeSchris    $more = buildURLparams($more,$sep);
2906de3759aSAndreas Gohr  }else{
291b174aeaeSchris    $more = str_replace(',',$sep,$more);
2926de3759aSAndreas Gohr  }
2936de3759aSAndreas Gohr
2946de3759aSAndreas Gohr  $xlink = DOKU_BASE;
2956de3759aSAndreas Gohr
2966de3759aSAndreas Gohr  // external URLs are always direct without rewriting
2976de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
2986de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
2996de3759aSAndreas Gohr    if($more){
3006de3759aSAndreas Gohr      $xlink .= '?'.$more;
301b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3026de3759aSAndreas Gohr    }else{
303b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3046de3759aSAndreas Gohr    }
3056de3759aSAndreas Gohr    return $xlink;
3066de3759aSAndreas Gohr  }
3076de3759aSAndreas Gohr
3086de3759aSAndreas Gohr  $id = idfilter($id);
3096de3759aSAndreas Gohr
3106de3759aSAndreas Gohr  // decide on scriptname
3116de3759aSAndreas Gohr  if($direct){
3126de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3136de3759aSAndreas Gohr      $script = '_media';
3146de3759aSAndreas Gohr    }else{
3156de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3166de3759aSAndreas Gohr    }
3176de3759aSAndreas Gohr  }else{
3186de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3196de3759aSAndreas Gohr      $script = '_detail';
3206de3759aSAndreas Gohr    }else{
3216de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3226de3759aSAndreas Gohr    }
3236de3759aSAndreas Gohr  }
3246de3759aSAndreas Gohr
3256de3759aSAndreas Gohr  // build URL based on rewrite mode
3266de3759aSAndreas Gohr   if($conf['userewrite']){
3276de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3286de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3296de3759aSAndreas Gohr   }else{
3306de3759aSAndreas Gohr     if($more){
331a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
332b174aeaeSchris       $xlink .= $sep.'media='.$id;
3336de3759aSAndreas Gohr     }else{
334a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
3356de3759aSAndreas Gohr     }
3366de3759aSAndreas Gohr   }
3376de3759aSAndreas Gohr
3386de3759aSAndreas Gohr  return $xlink;
3396de3759aSAndreas Gohr}
3406de3759aSAndreas Gohr
3416de3759aSAndreas Gohr
3426de3759aSAndreas Gohr
3436de3759aSAndreas Gohr/**
344f3f0262cSandi * Just builds a link to a script
34515fae107Sandi *
346ed7b5f09Sandi * @todo   maybe obsolete
34715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
348f3f0262cSandi */
349f3f0262cSandifunction script($script='doku.php'){
350ed7b5f09Sandi#  $link = getBaseURL();
351ed7b5f09Sandi#  $link .= $script;
352ed7b5f09Sandi#  return $link;
353ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
354f3f0262cSandi}
355f3f0262cSandi
356f3f0262cSandi/**
35715fae107Sandi * Spamcheck against wordlist
35815fae107Sandi *
359f3f0262cSandi * Checks the wikitext against a list of blocked expressions
360f3f0262cSandi * returns true if the text contains any bad words
36115fae107Sandi *
36215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
363f3f0262cSandi */
364f3f0262cSandifunction checkwordblock(){
365f3f0262cSandi  global $TEXT;
366f3f0262cSandi  global $conf;
367f3f0262cSandi
368f3f0262cSandi  if(!$conf['usewordblock']) return false;
369f3f0262cSandi
370b9ac8716Schris  $wordblocks = getWordblocks();
3713e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
3723e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
3733e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
3743e2965d7Sandi    //backreferences are used - the maximum is 99
3753e2965d7Sandi    //this is very bad performancewise and may even be too high still
3763e2965d7Sandi    $chunksize = 40;
3773e2965d7Sandi  }else{
378703f6fdeSandi    //read file in chunks of 600 - this should work around the
3793e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
380444b87a5SAndreas Gohr    $chunksize = 400;
3813e2965d7Sandi  }
382b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
383f3f0262cSandi    $re = array();
384f3f0262cSandi    #build regexp from blocks
385f3f0262cSandi    foreach($blocks as $block){
386f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
387f3f0262cSandi      $block = trim($block);
388f3f0262cSandi      if(empty($block)) continue;
389f3f0262cSandi      $re[]  = $block;
390f3f0262cSandi    }
391b9ac8716Schris    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
392b9ac8716Schris      return true;
393b9ac8716Schris    }
394703f6fdeSandi  }
395f3f0262cSandi  return false;
396f3f0262cSandi}
397f3f0262cSandi
398f3f0262cSandi/**
39915fae107Sandi * Return the IP of the client
40015fae107Sandi *
40115fae107Sandi * Honours X-Forwarded-For Proxy Headers
40215fae107Sandi *
40315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
404f3f0262cSandi */
405f3f0262cSandifunction clientIP(){
406f3f0262cSandi  $my = $_SERVER['REMOTE_ADDR'];
407f3f0262cSandi  if($_SERVER['HTTP_X_FORWARDED_FOR']){
408f3f0262cSandi    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
409f3f0262cSandi  }
410f3f0262cSandi  return $my;
411f3f0262cSandi}
412f3f0262cSandi
413f3f0262cSandi/**
41415fae107Sandi * Checks if a given page is currently locked.
41515fae107Sandi *
416f3f0262cSandi * removes stale lockfiles
41715fae107Sandi *
41815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
419f3f0262cSandi */
420f3f0262cSandifunction checklock($id){
421f3f0262cSandi  global $conf;
422f3f0262cSandi  $lock = wikiFN($id).'.lock';
423f3f0262cSandi
424f3f0262cSandi  //no lockfile
425f3f0262cSandi  if(!@file_exists($lock)) return false;
426f3f0262cSandi
427f3f0262cSandi  //lockfile expired
428f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
429f3f0262cSandi    unlink($lock);
430f3f0262cSandi    return false;
431f3f0262cSandi  }
432f3f0262cSandi
433f3f0262cSandi  //my own lock
434f3f0262cSandi  $ip = io_readFile($lock);
435f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
436f3f0262cSandi    return false;
437f3f0262cSandi  }
438f3f0262cSandi
439f3f0262cSandi  return $ip;
440f3f0262cSandi}
441f3f0262cSandi
442f3f0262cSandi/**
44315fae107Sandi * Lock a page for editing
44415fae107Sandi *
44515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
446f3f0262cSandi */
447f3f0262cSandifunction lock($id){
448f3f0262cSandi  $lock = wikiFN($id).'.lock';
449f3f0262cSandi  if($_SERVER['REMOTE_USER']){
450f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
451f3f0262cSandi  }else{
452f3f0262cSandi    io_saveFile($lock,clientIP());
453f3f0262cSandi  }
454f3f0262cSandi}
455f3f0262cSandi
456f3f0262cSandi/**
45715fae107Sandi * Unlock a page if it was locked by the user
458f3f0262cSandi *
45915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
46015fae107Sandi * @return bool true if a lock was removed
461f3f0262cSandi */
462f3f0262cSandifunction unlock($id){
463f3f0262cSandi  $lock = wikiFN($id).'.lock';
464f3f0262cSandi  if(@file_exists($lock)){
465f3f0262cSandi    $ip = io_readFile($lock);
466f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
467f3f0262cSandi      @unlink($lock);
468f3f0262cSandi      return true;
469f3f0262cSandi    }
470f3f0262cSandi  }
471f3f0262cSandi  return false;
472f3f0262cSandi}
473f3f0262cSandi
474f3f0262cSandi/**
475f3f0262cSandi * convert line ending to unix format
476f3f0262cSandi *
47715fae107Sandi * @see    formText() for 2crlf conversion
47815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
479f3f0262cSandi */
480f3f0262cSandifunction cleanText($text){
481f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
482f3f0262cSandi  return $text;
483f3f0262cSandi}
484f3f0262cSandi
485f3f0262cSandi/**
486f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
487f3f0262cSandi * It also converts line endings to Windows format which is
488f3f0262cSandi * pseudo standard for webforms.
489f3f0262cSandi *
49015fae107Sandi * @see    cleanText() for 2unix conversion
49115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
492f3f0262cSandi */
493f3f0262cSandifunction formText($text){
494f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
495f3f0262cSandi  return htmlspecialchars($text);
496f3f0262cSandi}
497f3f0262cSandi
498f3f0262cSandi/**
49915fae107Sandi * Returns the specified local text in raw format
50015fae107Sandi *
50115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
502f3f0262cSandi */
503f3f0262cSandifunction rawLocale($id){
504f3f0262cSandi  return io_readFile(localeFN($id));
505f3f0262cSandi}
506f3f0262cSandi
507f3f0262cSandi/**
508f3f0262cSandi * Returns the raw WikiText
50915fae107Sandi *
51015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
511f3f0262cSandi */
512f3f0262cSandifunction rawWiki($id,$rev=''){
513f3f0262cSandi  return io_readFile(wikiFN($id,$rev));
514f3f0262cSandi}
515f3f0262cSandi
516f3f0262cSandi/**
5177146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
5187146cee2SAndreas Gohr *
5197146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5207146cee2SAndreas Gohr */
5217146cee2SAndreas Gohrfunction pageTemplate($id){
522a15ce62dSEsther Brunner  global $conf;
523a15ce62dSEsther Brunner  global $INFO;
524a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
525a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
526a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
527a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
528a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
529a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
530a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
531a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
532a15ce62dSEsther Brunner  return $tpl;
5337146cee2SAndreas Gohr}
5347146cee2SAndreas Gohr
5357146cee2SAndreas Gohr
5367146cee2SAndreas Gohr/**
53715fae107Sandi * Returns the raw Wiki Text in three slices.
53815fae107Sandi *
53915fae107Sandi * The range parameter needs to have the form "from-to"
54015cfe303Sandi * and gives the range of the section in bytes - no
54115cfe303Sandi * UTF-8 awareness is needed.
542f3f0262cSandi * The returned order is prefix, section and suffix.
54315fae107Sandi *
54415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
545f3f0262cSandi */
546f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
547f3f0262cSandi  list($from,$to) = split('-',$range,2);
548f3f0262cSandi  $text = io_readFile(wikiFN($id,$rev));
549f3f0262cSandi  if(!$from) $from = 0;
550c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
551f3f0262cSandi
55215cfe303Sandi  $slices[0] = substr($text,0,$from-1);
55315cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
55415cfe303Sandi  $slices[2] = substr($text,$to);
555f3f0262cSandi
556f3f0262cSandi  return $slices;
557f3f0262cSandi}
558f3f0262cSandi
559f3f0262cSandi/**
56015fae107Sandi * Joins wiki text slices
56115fae107Sandi *
562f3f0262cSandi * function to join the text slices with correct lineendings again.
563f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
564f3f0262cSandi * lines between sections if needed (used on saving).
56515fae107Sandi *
56615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
567f3f0262cSandi */
568f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
569f3f0262cSandi
570f3f0262cSandi  if($pretty){
571f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
572f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
573f3f0262cSandi  }
574f3f0262cSandi
575f3f0262cSandi  if($pre) $pre .= "\n";
576f3f0262cSandi  if($suf) $text .= "\n";
577f3f0262cSandi  return $pre.$text.$suf;
578f3f0262cSandi}
579f3f0262cSandi
580f3f0262cSandi/**
58115fae107Sandi * print debug messages
58215fae107Sandi *
583f3f0262cSandi * little function to print the content of a var
58415fae107Sandi *
58515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
586f3f0262cSandi */
587f3f0262cSandifunction dbg($msg,$hidden=false){
588f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
589f3f0262cSandi  print_r($msg);
590f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
591f3f0262cSandi}
592f3f0262cSandi
593f3f0262cSandi/**
594f3f0262cSandi * Add's an entry to the changelog
59515fae107Sandi *
59615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
597f3f0262cSandi */
598b6912aeaSAndreas Gohrfunction addLogEntry($date,$id,$summary='',$minor=false){
599f3f0262cSandi  global $conf;
600c1049928Sandi
601c1049928Sandi  if(!@is_writable($conf['changelog'])){
602c1049928Sandi    msg($conf['changelog'].' is not writable!',-1);
603c1049928Sandi    return;
604c1049928Sandi  }
605c1049928Sandi
606652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
607f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
608f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
609f3f0262cSandi
610b6912aeaSAndreas Gohr  if($conf['useacl'] && $user && $minor){
611b6912aeaSAndreas Gohr    $summary = '*'.$summary;
612b6912aeaSAndreas Gohr  }else{
613b6912aeaSAndreas Gohr    $summary = ' '.$summary;
614b6912aeaSAndreas Gohr  }
615b6912aeaSAndreas Gohr
616f3f0262cSandi  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
617dbb00abcSEsther Brunner  io_saveFile($conf['changelog'],$logline,true);
618f3f0262cSandi}
619f3f0262cSandi
620f3f0262cSandi/**
621b6912aeaSAndreas Gohr * Checks an summary entry if it was a minor edit
622b6912aeaSAndreas Gohr *
623b6912aeaSAndreas Gohr * The summary is cleaned of the marker char
624b6912aeaSAndreas Gohr *
625b6912aeaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
626b6912aeaSAndreas Gohr */
627b6912aeaSAndreas Gohrfunction isMinor(&$summary){
628b6912aeaSAndreas Gohr  if(substr($summary,0,1) == '*'){
629b6912aeaSAndreas Gohr    $summary = substr($summary,1);
630b6912aeaSAndreas Gohr    return true;
631b6912aeaSAndreas Gohr  }
632b6912aeaSAndreas Gohr  $summary = trim($summary);
633b6912aeaSAndreas Gohr  return false;
634b6912aeaSAndreas Gohr}
635b6912aeaSAndreas Gohr
636b6912aeaSAndreas Gohr/**
637d437bcc4SAndreas Gohr * Internal function used by getRecents
638d437bcc4SAndreas Gohr *
639d437bcc4SAndreas Gohr * don't call directly
640d437bcc4SAndreas Gohr *
641d437bcc4SAndreas Gohr * @see getRecents()
642d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
643d437bcc4SAndreas Gohr */
644b6912aeaSAndreas Gohrfunction _handleRecent($line,$ns,$flags){
645d437bcc4SAndreas Gohr  static $seen  = array();         //caches seen pages and skip them
646d437bcc4SAndreas Gohr  if(empty($line)) return false;   //skip empty lines
647d437bcc4SAndreas Gohr
648d437bcc4SAndreas Gohr  // split the line into parts
649d437bcc4SAndreas Gohr  list($dt,$ip,$id,$usr,$sum) = explode("\t",$line);
650d437bcc4SAndreas Gohr
651d437bcc4SAndreas Gohr  // skip seen ones
652d437bcc4SAndreas Gohr  if($seen[$id]) return false;
653b6912aeaSAndreas Gohr  $recent = array();
654b6912aeaSAndreas Gohr
655b6912aeaSAndreas Gohr  // check minors
656b6912aeaSAndreas Gohr  if(isMinor($sum)){
657b6912aeaSAndreas Gohr    // skip minors
658b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_MINORS) return false;
659b6912aeaSAndreas Gohr    $recent['minor'] = true;
660b6912aeaSAndreas Gohr  }else{
661b6912aeaSAndreas Gohr    $recent['minor'] = false;
662b6912aeaSAndreas Gohr  }
663d437bcc4SAndreas Gohr
664d437bcc4SAndreas Gohr  // remember in seen to skip additional sights
665d437bcc4SAndreas Gohr  $seen[$id] = 1;
666d437bcc4SAndreas Gohr
6670dc92c6fSAndreas Gohr  // check if it's a hidden page
6680dc92c6fSAndreas Gohr  if(isHiddenPage($id)) return false;
6690dc92c6fSAndreas Gohr
670d437bcc4SAndreas Gohr  // filter namespace
671d437bcc4SAndreas Gohr  if (($ns) && (strpos($id,$ns.':') !== 0)) return false;
672d437bcc4SAndreas Gohr
673d437bcc4SAndreas Gohr  // exclude subnamespaces
674b6912aeaSAndreas Gohr  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($id) != $ns)) return false;
675d437bcc4SAndreas Gohr
676ae56bfb6SAndreas Gohr  // check ACL
677ae56bfb6SAndreas Gohr  if (auth_quickaclcheck($id) < AUTH_READ) return false;
678ae56bfb6SAndreas Gohr
679d437bcc4SAndreas Gohr  // check existance
680d437bcc4SAndreas Gohr  if(!@file_exists(wikiFN($id))){
681b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_DELETED){
682d437bcc4SAndreas Gohr      return false;
683d437bcc4SAndreas Gohr    }else{
684d437bcc4SAndreas Gohr      $recent['del'] = true;
685d437bcc4SAndreas Gohr    }
686d437bcc4SAndreas Gohr  }else{
687d437bcc4SAndreas Gohr    $recent['del'] = false;
688d437bcc4SAndreas Gohr  }
689d437bcc4SAndreas Gohr
690d437bcc4SAndreas Gohr  $recent['id']   = $id;
691d437bcc4SAndreas Gohr  $recent['date'] = $dt;
692d437bcc4SAndreas Gohr  $recent['ip']   = $ip;
693d437bcc4SAndreas Gohr  $recent['user'] = $usr;
694d437bcc4SAndreas Gohr  $recent['sum']  = $sum;
695d437bcc4SAndreas Gohr
696d437bcc4SAndreas Gohr  return $recent;
697d437bcc4SAndreas Gohr}
698d437bcc4SAndreas Gohr
699b6912aeaSAndreas Gohr
700d437bcc4SAndreas Gohr/**
701f3f0262cSandi * returns an array of recently changed files using the
702f3f0262cSandi * changelog
703d437bcc4SAndreas Gohr *
704b6912aeaSAndreas Gohr * The following constants can be used to control which changes are
705b6912aeaSAndreas Gohr * included. Add them together as needed.
706b6912aeaSAndreas Gohr *
707b6912aeaSAndreas Gohr * RECENTS_SKIP_DELETED   - don't include deleted pages
708b6912aeaSAndreas Gohr * RECENTS_SKIP_MINORS    - don't include minor changes
709b6912aeaSAndreas Gohr * RECENTS_SKIP_SUBSPACES - don't include subspaces
710b6912aeaSAndreas Gohr *
711d437bcc4SAndreas Gohr * @param int    $first   number of first entry returned (for paginating
712d437bcc4SAndreas Gohr * @param int    $num     return $num entries
713d437bcc4SAndreas Gohr * @param string $ns      restrict to given namespace
714b6912aeaSAndreas Gohr * @param bool   $flags   see above
71515fae107Sandi *
71615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
717f3f0262cSandi */
718b6912aeaSAndreas Gohrfunction getRecents($first,$num,$ns='',$flags=0){
719f3f0262cSandi  global $conf;
720f3f0262cSandi  $recent = array();
721d437bcc4SAndreas Gohr  $count  = 0;
7225749f1ceSmatthiasgrimm
7235749f1ceSmatthiasgrimm  if(!$num)
7245749f1ceSmatthiasgrimm    return $recent;
725f3f0262cSandi
726c1049928Sandi  if(!@is_readable($conf['changelog'])){
727c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
728c1049928Sandi    return $recent;
729c1049928Sandi  }
730c1049928Sandi
731d437bcc4SAndreas Gohr  $fh  = fopen($conf['changelog'],'r');
732d437bcc4SAndreas Gohr  $buf = '';
733d437bcc4SAndreas Gohr  $csz = 4096;                              //chunksize
734d437bcc4SAndreas Gohr  fseek($fh,0,SEEK_END);                    // jump to the end
735d437bcc4SAndreas Gohr  $pos = ftell($fh);                        // position pointer
736f3f0262cSandi
737d437bcc4SAndreas Gohr  // now read backwards into buffer
738d437bcc4SAndreas Gohr  while($pos > 0){
739d437bcc4SAndreas Gohr    $pos -= $csz;                           // seek to previous chunk...
740f6c1156dSWolfgang Ocker    if($pos < 0) {                          // ...or rest of file
741f6c1156dSWolfgang Ocker      $csz += $pos;
742f6c1156dSWolfgang Ocker      $pos = 0;
743f6c1156dSWolfgang Ocker    }
744f6c1156dSWolfgang Ocker
745d437bcc4SAndreas Gohr    fseek($fh,$pos);
746dbb00abcSEsther Brunner
747d437bcc4SAndreas Gohr    $buf = fread($fh,$csz).$buf;            // prepend to buffer
7488f1d587cSEsther Brunner
749d437bcc4SAndreas Gohr    $lines = explode("\n",$buf);            // split buffer into lines
7505749f1ceSmatthiasgrimm
751d437bcc4SAndreas Gohr    if($pos > 0){
752d437bcc4SAndreas Gohr      $buf = array_shift($lines);           // first one may be still incomplete
753f3f0262cSandi    }
754d437bcc4SAndreas Gohr
755d437bcc4SAndreas Gohr    $cnt = count($lines);
756d437bcc4SAndreas Gohr    if(!$cnt) continue;                     // no lines yet
757d437bcc4SAndreas Gohr
758d437bcc4SAndreas Gohr    // handle lines
759d437bcc4SAndreas Gohr    for($i = $cnt-1; $i >= 0; $i--){
760b6912aeaSAndreas Gohr      $rec = _handleRecent($lines[$i],$ns,$flags);
761d437bcc4SAndreas Gohr      if($rec !== false){
762d437bcc4SAndreas Gohr        if(--$first >= 0) continue;         // skip first entries
763d437bcc4SAndreas Gohr        $recent[] = $rec;
764d437bcc4SAndreas Gohr        $count++;
765d437bcc4SAndreas Gohr
766d437bcc4SAndreas Gohr        // break while when we have enough entries
767d437bcc4SAndreas Gohr        if($count >= $num){
768d437bcc4SAndreas Gohr          $pos = 0; // will break the while loop
769d437bcc4SAndreas Gohr          break;    // will break the for loop
770f3f0262cSandi        }
771f3f0262cSandi      }
772d437bcc4SAndreas Gohr    }
773d437bcc4SAndreas Gohr  }// end of while
774d437bcc4SAndreas Gohr
775d437bcc4SAndreas Gohr  fclose($fh);
776f3f0262cSandi  return $recent;
777f3f0262cSandi}
778f3f0262cSandi
779f3f0262cSandi/**
780652610a2Sandi * gets additonal informations for a certain pagerevison
781652610a2Sandi * from the changelog
782652610a2Sandi *
783652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
784652610a2Sandi */
785652610a2Sandifunction getRevisionInfo($id,$rev){
786652610a2Sandi  global $conf;
787258641c6Sandi
788258641c6Sandi  if(!$rev) return(null);
789258641c6Sandi
790c1049928Sandi  $info = array();
791c1049928Sandi  if(!@is_readable($conf['changelog'])){
792c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
793c1049928Sandi    return $recent;
794c1049928Sandi  }
795652610a2Sandi  $loglines = file($conf['changelog']);
796652610a2Sandi  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
797dc42ff59Sandi  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
798652610a2Sandi  $line = split("\t",$loglines[0]);
799652610a2Sandi  $info['date']  = $line[0];
800652610a2Sandi  $info['ip']    = $line[1];
801652610a2Sandi  $info['user']  = $line[3];
802652610a2Sandi  $info['sum']   = $line[4];
803b6912aeaSAndreas Gohr  $info['minor'] = isMinor($info['sum']);
804652610a2Sandi  return $info;
805652610a2Sandi}
806652610a2Sandi
807652610a2Sandi/**
808f3f0262cSandi * Saves a wikitext by calling io_saveFile
80915fae107Sandi *
81015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
811f3f0262cSandi */
812b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
813f3f0262cSandi  global $conf;
814f3f0262cSandi  global $lang;
815f3f0262cSandi  // ignore if no changes were made
816f3f0262cSandi  if($text == rawWiki($id,'')){
817f3f0262cSandi    return;
818f3f0262cSandi  }
819f3f0262cSandi
820f3f0262cSandi  $file = wikiFN($id);
821f3f0262cSandi  $old  = saveOldRevision($id);
822f3f0262cSandi
823f3f0262cSandi  if (empty($text)){
824e1f3d9e1SEsther Brunner    // remove empty file
825f3f0262cSandi    @unlink($file);
826e1f3d9e1SEsther Brunner    // remove any meta info
827e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
828e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
829e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
830b158d625SSteven Danz    }
831f3f0262cSandi    $del = true;
8323ce054b3Sandi    // autoset summary on deletion
8333ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
834f864871eSAndreas Gohr    // unlock early
835f864871eSAndreas Gohr    unlock($id);
83653d6ccfeSandi    // remove empty namespaces
83753d6ccfeSandi    io_sweepNS($id);
838f3f0262cSandi  }else{
839f3f0262cSandi    // save file (datadir is created in io_saveFile)
840f3f0262cSandi    io_saveFile($file,$text);
841f3f0262cSandi    $del = false;
842f3f0262cSandi  }
843f3f0262cSandi
844b6912aeaSAndreas Gohr  addLogEntry(@filemtime($file),$id,$summary,$minor);
84526a0801fSAndreas Gohr  // send notify mails
84690033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
84790033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
848f3f0262cSandi
849f3f0262cSandi  //purge cache on add by updating the purgefile
850f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
85198407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
852f3f0262cSandi  }
853f3f0262cSandi}
854f3f0262cSandi
855f3f0262cSandi/**
856f3f0262cSandi * moves the current version to the attic and returns its
857f3f0262cSandi * revision date
85815fae107Sandi *
85915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
860f3f0262cSandi */
861f3f0262cSandifunction saveOldRevision($id){
862f3f0262cSandi  global $conf;
863f3f0262cSandi  $oldf = wikiFN($id);
864f3f0262cSandi  if(!@file_exists($oldf)) return '';
865f3f0262cSandi  $date = filemtime($oldf);
866f3f0262cSandi  $newf = wikiFN($id,$date);
867f3f0262cSandi  if(substr($newf,-3)=='.gz'){
868f3f0262cSandi    io_saveFile($newf,rawWiki($id));
869f3f0262cSandi  }else{
870f3f0262cSandi    io_makeFileDir($newf);
871f3f0262cSandi    copy($oldf, $newf);
872f3f0262cSandi  }
873f3f0262cSandi  return $date;
874f3f0262cSandi}
875f3f0262cSandi
876f3f0262cSandi/**
87726a0801fSAndreas Gohr * Sends a notify mail on page change
87826a0801fSAndreas Gohr *
87926a0801fSAndreas Gohr * @param  string  $id       The changed page
88026a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
88126a0801fSAndreas Gohr * @param  int     $rev      Old page revision
88226a0801fSAndreas Gohr * @param  string  $summary  What changed
88390033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
88415fae107Sandi *
88515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
886f3f0262cSandi */
88790033e9dSAndreas Gohrfunction notify($id,$who,$rev='',$summary='',$minor=false){
888f3f0262cSandi  global $lang;
889f3f0262cSandi  global $conf;
890b158d625SSteven Danz
89126a0801fSAndreas Gohr  // decide if there is something to do
89226a0801fSAndreas Gohr  if($who == 'admin'){
89326a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
894f3f0262cSandi    $text = rawLocale('mailtext');
89526a0801fSAndreas Gohr    $to   = $conf['notify'];
89626a0801fSAndreas Gohr    $bcc  = '';
89726a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
89826a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
89990033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
90026a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
90126a0801fSAndreas Gohr    if(empty($bcc)) return;
90226a0801fSAndreas Gohr    $to   = '';
90326a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
90426a0801fSAndreas Gohr  }else{
90526a0801fSAndreas Gohr    return; //just to be safe
90626a0801fSAndreas Gohr  }
90726a0801fSAndreas Gohr
908f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
909f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
910f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
911f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
912ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
91326a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
91426a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
915ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
916f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
9177a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
918f3f0262cSandi
919f3f0262cSandi  if($rev){
920f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
921ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
922ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
923f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
924f3f0262cSandi                    split("\n",rawWiki($id)));
925f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
926f3f0262cSandi    $diff    = $dformat->format($df);
927f3f0262cSandi  }else{
928f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
929f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
930f3f0262cSandi    $diff = rawWiki($id);
931f3f0262cSandi  }
932f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
933241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
934f3f0262cSandi
93526a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
936f3f0262cSandi}
937f3f0262cSandi
93815fae107Sandi/**
93915fae107Sandi * Return a list of available page revisons
94015fae107Sandi *
94115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
94215fae107Sandi */
943f3f0262cSandifunction getRevisions($id){
944f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
945f3f0262cSandi  $revs = array();
946f3f0262cSandi  $clid = cleanID($id);
947f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
948493a6929SKobaYY  $clid = utf8_encodeFN($clid);
949f3f0262cSandi
950f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
951f3f0262cSandi    while (($file = readdir($dh)) !== false) {
952f3f0262cSandi      if (is_dir($revd.'/'.$file)) continue;
953f3f0262cSandi      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
954f3f0262cSandi        $revs[]=$match[1];
955f3f0262cSandi      }
956f3f0262cSandi    }
957f3f0262cSandi    closedir($dh);
958f3f0262cSandi  }
959f3f0262cSandi  rsort($revs);
960f3f0262cSandi  return $revs;
961f3f0262cSandi}
962f3f0262cSandi
963f3f0262cSandi/**
964f3f0262cSandi * extracts the query from a google referer
96515fae107Sandi *
9666b13307fSandi * @todo   should be more generic and support yahoo et al
96715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
968f3f0262cSandi */
969f3f0262cSandifunction getGoogleQuery(){
970f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
9715c3f206fSandi  if(!$url) return '';
972f3f0262cSandi
973f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
974f3f0262cSandi  $query = array();
975f3f0262cSandi  parse_str($url['query'],$query);
976f3f0262cSandi
977f3f0262cSandi  return $query['q'];
978f3f0262cSandi}
979f3f0262cSandi
980f3f0262cSandi/**
98115fae107Sandi * Try to set correct locale
98215fae107Sandi *
983095bfd5cSandi * @deprecated No longer used
98415fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
985f3f0262cSandi */
986f3f0262cSandifunction setCorrectLocale(){
987f3f0262cSandi  global $conf;
988f3f0262cSandi  global $lang;
989f3f0262cSandi
990f3f0262cSandi  $enc = strtoupper($lang['encoding']);
991f3f0262cSandi  foreach ($lang['locales'] as $loc){
992f3f0262cSandi    //try locale
993f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
994f3f0262cSandi    //try loceale with encoding
995f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
996f3f0262cSandi  }
997f3f0262cSandi  //still here? try to set from environment
998f3f0262cSandi  @setlocale(LC_ALL,"");
999f3f0262cSandi}
1000f3f0262cSandi
1001f3f0262cSandi/**
1002f3f0262cSandi * Return the human readable size of a file
1003f3f0262cSandi *
1004f3f0262cSandi * @param       int    $size   A file size
1005f3f0262cSandi * @param       int    $dec    A number of decimal places
1006f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
1007f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
1008f3f0262cSandi * @version     1.0.0
1009f3f0262cSandi */
1010f31d5b73Sandifunction filesize_h($size, $dec = 1){
1011f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
1012f3f0262cSandi  $count = count($sizes);
1013f3f0262cSandi  $i = 0;
1014f3f0262cSandi
1015f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
1016f3f0262cSandi    $size /= 1024;
1017f3f0262cSandi    $i++;
1018f3f0262cSandi  }
1019f3f0262cSandi
1020f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
1021f3f0262cSandi}
1022f3f0262cSandi
102315fae107Sandi/**
102400a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
102500a7b5adSEsther Brunner *
102600a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
102700a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
102800a7b5adSEsther Brunner */
102900a7b5adSEsther Brunnerfunction obfuscate($email) {
103000a7b5adSEsther Brunner  global $conf;
103100a7b5adSEsther Brunner
103200a7b5adSEsther Brunner  switch ($conf['mailguard']) {
103300a7b5adSEsther Brunner    case 'visible' :
103400a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
103500a7b5adSEsther Brunner      return strtr($email, $obfuscate);
103600a7b5adSEsther Brunner
103700a7b5adSEsther Brunner    case 'hex' :
103800a7b5adSEsther Brunner      $encode = '';
103900a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
104000a7b5adSEsther Brunner      return $encode;
104100a7b5adSEsther Brunner
104200a7b5adSEsther Brunner    case 'none' :
104300a7b5adSEsther Brunner    default :
104400a7b5adSEsther Brunner      return $email;
104500a7b5adSEsther Brunner  }
104600a7b5adSEsther Brunner}
104700a7b5adSEsther Brunner
104800a7b5adSEsther Brunner/**
1049dc57ef04Sandi * Return DokuWikis version
105015fae107Sandi *
105115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
105215fae107Sandi */
1053f31d5b73Sandifunction getVersion(){
1054f31d5b73Sandi  //import version string
1055f31d5b73Sandi  if(@file_exists('VERSION')){
1056f31d5b73Sandi    //official release
10570647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1058f31d5b73Sandi  }elseif(is_dir('_darcs')){
1059f31d5b73Sandi    //darcs checkout
1060f31d5b73Sandi    $inv = file('_darcs/inventory');
1061ae41559bSAndreas Gohr    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
1062f31d5b73Sandi    $cur = array_pop($inv);
1063f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1064f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1065f31d5b73Sandi  }else{
1066f31d5b73Sandi    return 'snapshot?';
1067f31d5b73Sandi  }
1068f31d5b73Sandi}
1069f31d5b73Sandi
1070f31d5b73Sandi/**
1071f31d5b73Sandi * Run a few sanity checks
1072f31d5b73Sandi *
1073f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
1074f31d5b73Sandi */
1075f3f0262cSandifunction check(){
1076f3f0262cSandi  global $conf;
1077f3f0262cSandi  global $INFO;
1078f3f0262cSandi
1079f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
1080f31d5b73Sandi
108149022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
108249022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
108349022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
108449022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
108549022a38Sandi  }else{
108649022a38Sandi    msg('PHP version '.phpversion(),1);
108749022a38Sandi  }
108849022a38Sandi
1089f3f0262cSandi  if(is_writable($conf['changelog'])){
1090f3f0262cSandi    msg('Changelog is writable',1);
1091f3f0262cSandi  }else{
1092f3f0262cSandi    msg('Changelog is not writable',-1);
1093f3f0262cSandi  }
1094f3f0262cSandi
1095f3f0262cSandi  if(is_writable($conf['datadir'])){
1096f3f0262cSandi    msg('Datadir is writable',1);
1097f3f0262cSandi  }else{
1098f3f0262cSandi    msg('Datadir is not writable',-1);
1099f3f0262cSandi  }
1100f3f0262cSandi
1101f3f0262cSandi  if(is_writable($conf['olddir'])){
1102f3f0262cSandi    msg('Attic is writable',1);
1103f3f0262cSandi  }else{
1104f3f0262cSandi    msg('Attic is not writable',-1);
1105f3f0262cSandi  }
1106f3f0262cSandi
1107f3f0262cSandi  if(is_writable($conf['mediadir'])){
1108f3f0262cSandi    msg('Mediadir is writable',1);
1109f3f0262cSandi  }else{
1110f3f0262cSandi    msg('Mediadir is not writable',-1);
1111f3f0262cSandi  }
1112f3f0262cSandi
111398407a7aSandi  if(is_writable($conf['cachedir'])){
111498407a7aSandi    msg('Cachedir is writable',1);
111598407a7aSandi  }else{
111698407a7aSandi    msg('Cachedir is not writable',-1);
111798407a7aSandi  }
111898407a7aSandi
1119e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
11208c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1121f3f0262cSandi  }else{
11228c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1123f3f0262cSandi  }
112493a9e835Sandi
112593a9e835Sandi  if(function_exists('mb_strpos')){
112693a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
112793a9e835Sandi      msg('mb_string extension is available but will not be used',0);
112893a9e835Sandi    }else{
112993a9e835Sandi      msg('mb_string extension is available and will be used',1);
113093a9e835Sandi    }
113193a9e835Sandi  }else{
113293a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
113393a9e835Sandi  }
1134f42d1c75SAndreas Gohr
1135f42d1c75SAndreas Gohr  if($conf['allowdebug']){
1136f42d1c75SAndreas Gohr    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1137f42d1c75SAndreas Gohr  }else{
1138f42d1c75SAndreas Gohr    msg('Debugging support is disabled',1);
1139f42d1c75SAndreas Gohr  }
1140f3f0262cSandi
1141f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1142f3f0262cSandi
1143f3f0262cSandi  if(is_writable($INFO['filepath'])){
1144f3f0262cSandi    msg('The current page is writable by the webserver',0);
1145f3f0262cSandi  }else{
1146f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1147f3f0262cSandi  }
1148f3f0262cSandi
1149f3f0262cSandi  if($INFO['writable']){
1150f3f0262cSandi    msg('The current page is writable by you',0);
1151f3f0262cSandi  }else{
1152f3f0262cSandi    msg('The current page is not writable you',0);
1153f3f0262cSandi  }
1154f3f0262cSandi}
1155340756e4Sandi
1156b158d625SSteven Danz/**
1157b158d625SSteven Danz * Let us know if a user is tracking a page
1158b158d625SSteven Danz *
11591380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1160b158d625SSteven Danz */
11611380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
11621380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
11631380fc45SAndreas Gohr  if (@file_exists($file)) {
1164b158d625SSteven Danz    $mlist = file($file);
11651380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
11661380fc45SAndreas Gohr    return is_int($pos);
1167b158d625SSteven Danz  }
11681380fc45SAndreas Gohr
1169b158d625SSteven Danz  return false;
1170b158d625SSteven Danz}
1171340756e4Sandi
1172f9eb5648Ssteven-danz/**
1173f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1174f9eb5648Ssteven-danz * users subscribed to a page
1175f9eb5648Ssteven-danz *
117626a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1177f9eb5648Ssteven-danz */
1178f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1179f9eb5648Ssteven-danz  global $conf;
1180cd52f92dSchris  global $auth;
1181f9eb5648Ssteven-danz
1182f9eb5648Ssteven-danz  $emails = '';
1183f9eb5648Ssteven-danz
118426a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
118526a0801fSAndreas Gohr
1186f9eb5648Ssteven-danz  $mlist = array();
1187f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1188f9eb5648Ssteven-danz  if (file_exists($file)) {
1189f9eb5648Ssteven-danz    $mlist = file($file);
1190f9eb5648Ssteven-danz  }
1191f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1192f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1193f9eb5648Ssteven-danz      $who = rtrim($who);
1194cd52f92dSchris      $info = $auth->getUserData($who);
1195f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1196f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1197f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1198f9eb5648Ssteven-danz          if (empty($emails)) {
1199f9eb5648Ssteven-danz            $emails = $info['mail'];
1200f9eb5648Ssteven-danz          } else {
1201f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1202f9eb5648Ssteven-danz          }
1203f9eb5648Ssteven-danz        }
1204f9eb5648Ssteven-danz      }
1205f9eb5648Ssteven-danz    }
1206f9eb5648Ssteven-danz  }
1207f9eb5648Ssteven-danz
1208f9eb5648Ssteven-danz  return $emails;
1209f9eb5648Ssteven-danz}
1210f9eb5648Ssteven-danz
121189541d4bSAndreas Gohr/**
121289541d4bSAndreas Gohr * Removes quoting backslashes
121389541d4bSAndreas Gohr *
121489541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
121589541d4bSAndreas Gohr */
121689541d4bSAndreas Gohrfunction unslash($string,$char="'"){
121789541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
121889541d4bSAndreas Gohr}
121989541d4bSAndreas Gohr
1220340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1221