xref: /dokuwiki/inc/common.php (revision 3b97c5ec44702052661770859b3f2c7a3d32b538)
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/**
24d5197206Schris * Wrapper around htmlspecialchars()
25d5197206Schris *
26d5197206Schris * @author Andreas Gohr <andi@splitbrain.org>
27d5197206Schris * @see    htmlspecialchars()
28d5197206Schris */
29d5197206Schrisfunction hsc($string){
30d5197206Schris  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
31d5197206Schris}
32d5197206Schris
33d5197206Schris/**
34d5197206Schris * print a newline terminated string
35d5197206Schris *
36d5197206Schris * You can give an indention as optional parameter
37d5197206Schris *
38d5197206Schris * @author Andreas Gohr <andi@splitbrain.org>
39d5197206Schris */
40d5197206Schrisfunction ptln($string,$intend=0){
41d5197206Schris  for($i=0; $i<$intend; $i++) print ' ';
42d5197206Schris  print"$string\n";
43d5197206Schris}
44d5197206Schris
45d5197206Schris/**
4615fae107Sandi * Return info about the current document as associative
47f3f0262cSandi * array.
4815fae107Sandi *
4915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
50f3f0262cSandi */
51f3f0262cSandifunction pageinfo(){
52f3f0262cSandi  global $ID;
53f3f0262cSandi  global $REV;
54f3f0262cSandi  global $USERINFO;
55f3f0262cSandi  global $conf;
56f3f0262cSandi
57f3f0262cSandi  if($_SERVER['REMOTE_USER']){
58f3f0262cSandi    $info['userinfo']   = $USERINFO;
59f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
601380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
61ee4c4a1bSAndreas Gohr    $info['client']     = $_SERVER['REMOTE_USER'];
6217ee7f66SAndreas Gohr
6317ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
6417ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
6517ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
6617ee7f66SAndreas Gohr    }
67ee4c4a1bSAndreas Gohr
68f3f0262cSandi  }else{
69f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
701380fc45SAndreas Gohr    $info['subscribed'] = false;
71ee4c4a1bSAndreas Gohr    $info['client']     = clientIP(true);
72f3f0262cSandi  }
73f3f0262cSandi
74f3f0262cSandi  $info['namespace'] = getNS($ID);
75f3f0262cSandi  $info['locked']    = checklock($ID);
76f3f0262cSandi  $info['filepath']  = realpath(wikiFN($ID,$REV));
77f3f0262cSandi  $info['exists']    = @file_exists($info['filepath']);
78f3f0262cSandi  if($REV && !$info['exists']){
79f3f0262cSandi    //check if current revision was meant
80f3f0262cSandi    $cur = wikiFN($ID);
81f3f0262cSandi    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
82f3f0262cSandi      $info['filepath'] = realpath($cur);
83f3f0262cSandi      $info['exists']   = true;
84f3f0262cSandi      $REV = '';
85f3f0262cSandi    }
86f3f0262cSandi  }
87c112d578Sandi  $info['rev'] = $REV;
88f3f0262cSandi  if($info['exists']){
89f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
90f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
91f3f0262cSandi  }else{
92f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
93f3f0262cSandi  }
94f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
95f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
96f3f0262cSandi
97652610a2Sandi  //who's the editor
98652610a2Sandi  if($REV){
99fb53bfe2SBen Coburn    $revinfo = getRevisionInfo($ID,$REV,false);
100652610a2Sandi  }else{
101fb53bfe2SBen Coburn    $revinfo = getRevisionInfo($ID,$info['lastmod'],false);
102652610a2Sandi  }
103652610a2Sandi  $info['ip']     = $revinfo['ip'];
104652610a2Sandi  $info['user']   = $revinfo['user'];
105652610a2Sandi  $info['sum']    = $revinfo['sum'];
106b6912aeaSAndreas Gohr  $info['minor']  = $revinfo['minor'];
10759f257aeSchris
10888f522e9Sandi  if($revinfo['user']){
10988f522e9Sandi    $info['editor'] = $revinfo['user'];
11088f522e9Sandi  }else{
11188f522e9Sandi    $info['editor'] = $revinfo['ip'];
11288f522e9Sandi  }
113652610a2Sandi
114ee4c4a1bSAndreas Gohr  // draft
115ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
116ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
117ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
118ee4c4a1bSAndreas Gohr      // remove stale draft
119ee4c4a1bSAndreas Gohr      @unlink($draft);
120ee4c4a1bSAndreas Gohr    }else{
121ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
122ee4c4a1bSAndreas Gohr    }
123ee4c4a1bSAndreas Gohr  }
124ee4c4a1bSAndreas Gohr
125f3f0262cSandi  return $info;
126f3f0262cSandi}
127f3f0262cSandi
128f3f0262cSandi/**
1292684e50aSAndreas Gohr * Build an string of URL parameters
1302684e50aSAndreas Gohr *
1312684e50aSAndreas Gohr * @author Andreas Gohr
1322684e50aSAndreas Gohr */
133b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
1342684e50aSAndreas Gohr  $url = '';
1352684e50aSAndreas Gohr  $amp = false;
1362684e50aSAndreas Gohr  foreach($params as $key => $val){
137b174aeaeSchris    if($amp) $url .= $sep;
1382684e50aSAndreas Gohr
1392684e50aSAndreas Gohr    $url .= $key.'=';
140b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1412684e50aSAndreas Gohr    $amp = true;
1422684e50aSAndreas Gohr  }
1432684e50aSAndreas Gohr  return $url;
1442684e50aSAndreas Gohr}
1452684e50aSAndreas Gohr
1462684e50aSAndreas Gohr/**
1472684e50aSAndreas Gohr * Build an string of html tag attributes
1482684e50aSAndreas Gohr *
1492684e50aSAndreas Gohr * @author Andreas Gohr
1502684e50aSAndreas Gohr */
1512684e50aSAndreas Gohrfunction buildAttributes($params){
1522684e50aSAndreas Gohr  $url = '';
1532684e50aSAndreas Gohr  foreach($params as $key => $val){
1542684e50aSAndreas Gohr    $url .= $key.'="';
1552684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1562684e50aSAndreas Gohr    $url .= '" ';
1572684e50aSAndreas Gohr  }
1582684e50aSAndreas Gohr  return $url;
1592684e50aSAndreas Gohr}
1602684e50aSAndreas Gohr
1612684e50aSAndreas Gohr
1622684e50aSAndreas Gohr/**
1630396becbSandi * print a message
1640396becbSandi *
1650396becbSandi * If HTTP headers were not sent yet the message is added
1660396becbSandi * to the global message array else it's printed directly
1670396becbSandi * using html_msgarea()
1680396becbSandi *
169f3f0262cSandi *
170f3f0262cSandi * Levels can be:
171f3f0262cSandi *
172f3f0262cSandi * -1 error
173f3f0262cSandi *  0 info
174f3f0262cSandi *  1 success
17515fae107Sandi *
17615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1770396becbSandi * @see    html_msgarea
178f3f0262cSandi */
1790d58d74eSAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){
180f3f0262cSandi  global $MSG;
181f3f0262cSandi  $errors[-1] = 'error';
182f3f0262cSandi  $errors[0]  = 'info';
183f3f0262cSandi  $errors[1]  = 'success';
184f3f0262cSandi
1850d58d74eSAndreas Gohr  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
1860d58d74eSAndreas Gohr
187cc20ad51Sandi  if(!headers_sent()){
188f3f0262cSandi    if(!isset($MSG)) $MSG = array();
189f3f0262cSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
1900396becbSandi  }else{
1910396becbSandi    $MSG = array();
1920396becbSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
193f62ea8a1Sandi    if(function_exists('html_msgarea')){
1940396becbSandi      html_msgarea();
195f62ea8a1Sandi    }else{
196f62ea8a1Sandi      print "ERROR($lvl) $message";
197f62ea8a1Sandi    }
1980396becbSandi  }
199f3f0262cSandi}
200f3f0262cSandi
201f3f0262cSandi/**
20215fae107Sandi * This builds the breadcrumb trail and returns it as array
20315fae107Sandi *
20415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
205f3f0262cSandi */
206f3f0262cSandifunction breadcrumbs(){
2078746e727Sandi  // we prepare the breadcrumbs early for quick session closing
2088746e727Sandi  static $crumbs = null;
2098746e727Sandi  if($crumbs != null) return $crumbs;
2108746e727Sandi
211f3f0262cSandi  global $ID;
212f3f0262cSandi  global $ACT;
213f3f0262cSandi  global $conf;
214f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
215f3f0262cSandi
216f3f0262cSandi  //first visit?
217f3f0262cSandi  if (!is_array($crumbs)){
218f3f0262cSandi    $crumbs = array();
219f3f0262cSandi  }
220f3f0262cSandi  //we only save on show and existing wiki documents
221a77f5846Sjan  $file = wikiFN($ID);
222a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
223f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
224f3f0262cSandi    return $crumbs;
225f3f0262cSandi  }
226a77f5846Sjan
227a77f5846Sjan  // page names
228a77f5846Sjan  $name = noNS($ID);
229a77f5846Sjan  if ($conf['useheading']) {
230a77f5846Sjan    // get page title
231bb0a59d4Sjan    $title = p_get_first_heading($ID);
232a77f5846Sjan    if ($title) {
233a77f5846Sjan      $name = $title;
234a77f5846Sjan    }
235a77f5846Sjan  }
236a77f5846Sjan
237f3f0262cSandi  //remove ID from array
238a77f5846Sjan  if (isset($crumbs[$ID])) {
239a77f5846Sjan    unset($crumbs[$ID]);
240f3f0262cSandi  }
241f3f0262cSandi
242f3f0262cSandi  //add to array
243a77f5846Sjan  $crumbs[$ID] = $name;
244f3f0262cSandi  //reduce size
245f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
246f3f0262cSandi    array_shift($crumbs);
247f3f0262cSandi  }
248f3f0262cSandi  //save to session
249f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
250f3f0262cSandi  return $crumbs;
251f3f0262cSandi}
252f3f0262cSandi
253f3f0262cSandi/**
25415fae107Sandi * Filter for page IDs
25515fae107Sandi *
256f3f0262cSandi * This is run on a ID before it is outputted somewhere
257f3f0262cSandi * currently used to replace the colon with something else
258f3f0262cSandi * on Windows systems and to have proper URL encoding
25915fae107Sandi *
26049c713a3Sandi * Urlencoding is ommitted when the second parameter is false
26149c713a3Sandi *
26215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
263f3f0262cSandi */
26449c713a3Sandifunction idfilter($id,$ue=true){
265f3f0262cSandi  global $conf;
266f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
267f3f0262cSandi    $id = strtr($id,':','/');
268f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
269f3f0262cSandi      $conf['userewrite']) {
270f3f0262cSandi    $id = strtr($id,':',';');
271f3f0262cSandi  }
27249c713a3Sandi  if($ue){
273b6c6979fSAndreas Gohr    $id = rawurlencode($id);
274f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
275f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
27649c713a3Sandi  }
277f3f0262cSandi  return $id;
278f3f0262cSandi}
279f3f0262cSandi
280f3f0262cSandi/**
281ed7b5f09Sandi * This builds a link to a wikipage
28215fae107Sandi *
2836c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2846c7843b5Sandi * given in $more
2856c7843b5Sandi *
28615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
287f3f0262cSandi */
288b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
289f3f0262cSandi  global $conf;
2906de3759aSAndreas Gohr  if(is_array($more)){
291b174aeaeSchris    $more = buildURLparams($more,$sep);
2926de3759aSAndreas Gohr  }else{
293b174aeaeSchris    $more = str_replace(',',$sep,$more);
2946de3759aSAndreas Gohr  }
295f3f0262cSandi
296f3f0262cSandi  $id    = idfilter($id);
297ed7b5f09Sandi  if($abs){
298ed7b5f09Sandi    $xlink = DOKU_URL;
299ed7b5f09Sandi  }else{
300ed7b5f09Sandi    $xlink = DOKU_BASE;
301ed7b5f09Sandi  }
302f3f0262cSandi
3036c7843b5Sandi  if($conf['userewrite'] == 2){
3046c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
3056c7843b5Sandi    if($more) $xlink .= '?'.$more;
3066c7843b5Sandi  }elseif($conf['userewrite']){
307f3f0262cSandi    $xlink .= $id;
308f3f0262cSandi    if($more) $xlink .= '?'.$more;
3096c7843b5Sandi  }else{
3106c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
311b174aeaeSchris    if($more) $xlink .= $sep.$more;
312f3f0262cSandi  }
313f3f0262cSandi
314f3f0262cSandi  return $xlink;
315f3f0262cSandi}
316f3f0262cSandi
317f3f0262cSandi/**
318f5c2808fSBen Coburn * This builds a link to an alternate page format
319f5c2808fSBen Coburn *
320f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
321f5c2808fSBen Coburn *
322f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
323f5c2808fSBen Coburn */
324f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
325f5c2808fSBen Coburn  global $conf;
326f5c2808fSBen Coburn  if(is_array($more)){
327f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
328f5c2808fSBen Coburn  }else{
329f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
330f5c2808fSBen Coburn  }
331f5c2808fSBen Coburn
332f5c2808fSBen Coburn  $format = rawurlencode($format);
333f5c2808fSBen Coburn  $id = idfilter($id);
334f5c2808fSBen Coburn  if($abs){
335f5c2808fSBen Coburn    $xlink = DOKU_URL;
336f5c2808fSBen Coburn  }else{
337f5c2808fSBen Coburn    $xlink = DOKU_BASE;
338f5c2808fSBen Coburn  }
339f5c2808fSBen Coburn
340f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
341f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
342f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
343f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
344f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
345f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
346f5c2808fSBen Coburn  }else{
347f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
348f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
349f5c2808fSBen Coburn  }
350f5c2808fSBen Coburn
351f5c2808fSBen Coburn  return $xlink;
352f5c2808fSBen Coburn}
353f5c2808fSBen Coburn
354f5c2808fSBen Coburn/**
3556de3759aSAndreas Gohr * Build a link to a media file
3566de3759aSAndreas Gohr *
3576de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
3586de3759aSAndreas Gohr */
359b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
3606de3759aSAndreas Gohr  global $conf;
3616de3759aSAndreas Gohr  if(is_array($more)){
362b174aeaeSchris    $more = buildURLparams($more,$sep);
3636de3759aSAndreas Gohr  }else{
364b174aeaeSchris    $more = str_replace(',',$sep,$more);
3656de3759aSAndreas Gohr  }
3666de3759aSAndreas Gohr
3676de3759aSAndreas Gohr  $xlink = DOKU_BASE;
3686de3759aSAndreas Gohr
3696de3759aSAndreas Gohr  // external URLs are always direct without rewriting
3706de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
3716de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
3726de3759aSAndreas Gohr    if($more){
3736de3759aSAndreas Gohr      $xlink .= '?'.$more;
374b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3756de3759aSAndreas Gohr    }else{
376b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3776de3759aSAndreas Gohr    }
3786de3759aSAndreas Gohr    return $xlink;
3796de3759aSAndreas Gohr  }
3806de3759aSAndreas Gohr
3816de3759aSAndreas Gohr  $id = idfilter($id);
3826de3759aSAndreas Gohr
3836de3759aSAndreas Gohr  // decide on scriptname
3846de3759aSAndreas Gohr  if($direct){
3856de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3866de3759aSAndreas Gohr      $script = '_media';
3876de3759aSAndreas Gohr    }else{
3886de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3896de3759aSAndreas Gohr    }
3906de3759aSAndreas Gohr  }else{
3916de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3926de3759aSAndreas Gohr      $script = '_detail';
3936de3759aSAndreas Gohr    }else{
3946de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3956de3759aSAndreas Gohr    }
3966de3759aSAndreas Gohr  }
3976de3759aSAndreas Gohr
3986de3759aSAndreas Gohr  // build URL based on rewrite mode
3996de3759aSAndreas Gohr   if($conf['userewrite']){
4006de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
4016de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
4026de3759aSAndreas Gohr   }else{
4036de3759aSAndreas Gohr     if($more){
404a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
405b174aeaeSchris       $xlink .= $sep.'media='.$id;
4066de3759aSAndreas Gohr     }else{
407a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
4086de3759aSAndreas Gohr     }
4096de3759aSAndreas Gohr   }
4106de3759aSAndreas Gohr
4116de3759aSAndreas Gohr  return $xlink;
4126de3759aSAndreas Gohr}
4136de3759aSAndreas Gohr
4146de3759aSAndreas Gohr
4156de3759aSAndreas Gohr
4166de3759aSAndreas Gohr/**
417f3f0262cSandi * Just builds a link to a script
41815fae107Sandi *
419ed7b5f09Sandi * @todo   maybe obsolete
42015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
421f3f0262cSandi */
422f3f0262cSandifunction script($script='doku.php'){
423ed7b5f09Sandi#  $link = getBaseURL();
424ed7b5f09Sandi#  $link .= $script;
425ed7b5f09Sandi#  return $link;
426ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
427f3f0262cSandi}
428f3f0262cSandi
429f3f0262cSandi/**
43015fae107Sandi * Spamcheck against wordlist
43115fae107Sandi *
432f3f0262cSandi * Checks the wikitext against a list of blocked expressions
433f3f0262cSandi * returns true if the text contains any bad words
43415fae107Sandi *
43515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
436f3f0262cSandi */
437f3f0262cSandifunction checkwordblock(){
438f3f0262cSandi  global $TEXT;
439f3f0262cSandi  global $conf;
440f3f0262cSandi
441f3f0262cSandi  if(!$conf['usewordblock']) return false;
442f3f0262cSandi
443b9ac8716Schris  $wordblocks = getWordblocks();
4443e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
4453e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
4463e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
4473e2965d7Sandi    //backreferences are used - the maximum is 99
4483e2965d7Sandi    //this is very bad performancewise and may even be too high still
4493e2965d7Sandi    $chunksize = 40;
4503e2965d7Sandi  }else{
451703f6fdeSandi    //read file in chunks of 600 - this should work around the
4523e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
453444b87a5SAndreas Gohr    $chunksize = 400;
4543e2965d7Sandi  }
455b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
456f3f0262cSandi    $re = array();
457f3f0262cSandi    #build regexp from blocks
458f3f0262cSandi    foreach($blocks as $block){
459f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
460f3f0262cSandi      $block = trim($block);
461f3f0262cSandi      if(empty($block)) continue;
462f3f0262cSandi      $re[]  = $block;
463f3f0262cSandi    }
464b9ac8716Schris    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
465b9ac8716Schris      return true;
466b9ac8716Schris    }
467703f6fdeSandi  }
468f3f0262cSandi  return false;
469f3f0262cSandi}
470f3f0262cSandi
471f3f0262cSandi/**
47215fae107Sandi * Return the IP of the client
47315fae107Sandi *
4746d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers
47515fae107Sandi *
4766d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned
4776d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return
4786d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X
4796d8affe6SAndreas Gohr * headers
4806d8affe6SAndreas Gohr *
4816d8affe6SAndreas Gohr * @param  boolean $single If set only a single IP is returned
48215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
483f3f0262cSandi */
4846d8affe6SAndreas Gohrfunction clientIP($single=false){
4856d8affe6SAndreas Gohr  $ip = array();
4866d8affe6SAndreas Gohr  $ip[] = $_SERVER['REMOTE_ADDR'];
4876d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_FORWARDED_FOR'])
4886d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
4896d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_REAL_IP'])
4906d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
4916d8affe6SAndreas Gohr
4926d8affe6SAndreas Gohr  // remove any non-IP stuff
4936d8affe6SAndreas Gohr  $cnt = count($ip);
4946d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
4956d8affe6SAndreas Gohr    $ip[$i] = preg_replace('/[^0-9\.]+/','',$ip[$i]);
4966d8affe6SAndreas Gohr    if(!preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i])) $ip[$i] = '';
4976d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
498f3f0262cSandi  }
4996d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
5006d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
5016d8affe6SAndreas Gohr
5026d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
5036d8affe6SAndreas Gohr
5046d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
5056d8affe6SAndreas Gohr  $ip = array_reverse($ip);
5066d8affe6SAndreas Gohr  foreach($ip as $i){
5076d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
5086d8affe6SAndreas Gohr      continue;
5096d8affe6SAndreas Gohr    }else{
5106d8affe6SAndreas Gohr      return $i;
5116d8affe6SAndreas Gohr    }
5126d8affe6SAndreas Gohr  }
5136d8affe6SAndreas Gohr  // still here? just use the first (last) address
5146d8affe6SAndreas Gohr  return $ip[0];
515f3f0262cSandi}
516f3f0262cSandi
517f3f0262cSandi/**
51815fae107Sandi * Checks if a given page is currently locked.
51915fae107Sandi *
520f3f0262cSandi * removes stale lockfiles
52115fae107Sandi *
52215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
523f3f0262cSandi */
524f3f0262cSandifunction checklock($id){
525f3f0262cSandi  global $conf;
526f3f0262cSandi  $lock = wikiFN($id).'.lock';
527f3f0262cSandi
528f3f0262cSandi  //no lockfile
529f3f0262cSandi  if(!@file_exists($lock)) return false;
530f3f0262cSandi
531f3f0262cSandi  //lockfile expired
532f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
533f3f0262cSandi    unlink($lock);
534f3f0262cSandi    return false;
535f3f0262cSandi  }
536f3f0262cSandi
537f3f0262cSandi  //my own lock
538f3f0262cSandi  $ip = io_readFile($lock);
539f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
540f3f0262cSandi    return false;
541f3f0262cSandi  }
542f3f0262cSandi
543f3f0262cSandi  return $ip;
544f3f0262cSandi}
545f3f0262cSandi
546f3f0262cSandi/**
54715fae107Sandi * Lock a page for editing
54815fae107Sandi *
54915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
550f3f0262cSandi */
551f3f0262cSandifunction lock($id){
552f3f0262cSandi  $lock = wikiFN($id).'.lock';
553f3f0262cSandi  if($_SERVER['REMOTE_USER']){
554f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
555f3f0262cSandi  }else{
556f3f0262cSandi    io_saveFile($lock,clientIP());
557f3f0262cSandi  }
558f3f0262cSandi}
559f3f0262cSandi
560f3f0262cSandi/**
56115fae107Sandi * Unlock a page if it was locked by the user
562f3f0262cSandi *
56315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
56415fae107Sandi * @return bool true if a lock was removed
565f3f0262cSandi */
566f3f0262cSandifunction unlock($id){
567f3f0262cSandi  $lock = wikiFN($id).'.lock';
568f3f0262cSandi  if(@file_exists($lock)){
569f3f0262cSandi    $ip = io_readFile($lock);
570f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
571f3f0262cSandi      @unlink($lock);
572f3f0262cSandi      return true;
573f3f0262cSandi    }
574f3f0262cSandi  }
575f3f0262cSandi  return false;
576f3f0262cSandi}
577f3f0262cSandi
578f3f0262cSandi/**
579f3f0262cSandi * convert line ending to unix format
580f3f0262cSandi *
58115fae107Sandi * @see    formText() for 2crlf conversion
58215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
583f3f0262cSandi */
584f3f0262cSandifunction cleanText($text){
585f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
586f3f0262cSandi  return $text;
587f3f0262cSandi}
588f3f0262cSandi
589f3f0262cSandi/**
590f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
591f3f0262cSandi * It also converts line endings to Windows format which is
592f3f0262cSandi * pseudo standard for webforms.
593f3f0262cSandi *
59415fae107Sandi * @see    cleanText() for 2unix conversion
59515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
596f3f0262cSandi */
597f3f0262cSandifunction formText($text){
598f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
599f3f0262cSandi  return htmlspecialchars($text);
600f3f0262cSandi}
601f3f0262cSandi
602f3f0262cSandi/**
60315fae107Sandi * Returns the specified local text in raw format
60415fae107Sandi *
60515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
606f3f0262cSandi */
607f3f0262cSandifunction rawLocale($id){
608f3f0262cSandi  return io_readFile(localeFN($id));
609f3f0262cSandi}
610f3f0262cSandi
611f3f0262cSandi/**
612f3f0262cSandi * Returns the raw WikiText
61315fae107Sandi *
61415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
615f3f0262cSandi */
616f3f0262cSandifunction rawWiki($id,$rev=''){
617f3f0262cSandi  return io_readFile(wikiFN($id,$rev));
618f3f0262cSandi}
619f3f0262cSandi
620f3f0262cSandi/**
6217146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
6227146cee2SAndreas Gohr *
6237146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6247146cee2SAndreas Gohr */
6257146cee2SAndreas Gohrfunction pageTemplate($id){
626a15ce62dSEsther Brunner  global $conf;
627a15ce62dSEsther Brunner  global $INFO;
628a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
629a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
630a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
631a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
632a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
633a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
634a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
635a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
636a15ce62dSEsther Brunner  return $tpl;
6377146cee2SAndreas Gohr}
6387146cee2SAndreas Gohr
6397146cee2SAndreas Gohr
6407146cee2SAndreas Gohr/**
64115fae107Sandi * Returns the raw Wiki Text in three slices.
64215fae107Sandi *
64315fae107Sandi * The range parameter needs to have the form "from-to"
64415cfe303Sandi * and gives the range of the section in bytes - no
64515cfe303Sandi * UTF-8 awareness is needed.
646f3f0262cSandi * The returned order is prefix, section and suffix.
64715fae107Sandi *
64815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
649f3f0262cSandi */
650f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
651f3f0262cSandi  list($from,$to) = split('-',$range,2);
652f3f0262cSandi  $text = io_readFile(wikiFN($id,$rev));
653f3f0262cSandi  if(!$from) $from = 0;
654c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
655f3f0262cSandi
65615cfe303Sandi  $slices[0] = substr($text,0,$from-1);
65715cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
65815cfe303Sandi  $slices[2] = substr($text,$to);
659f3f0262cSandi
660f3f0262cSandi  return $slices;
661f3f0262cSandi}
662f3f0262cSandi
663f3f0262cSandi/**
66415fae107Sandi * Joins wiki text slices
66515fae107Sandi *
666f3f0262cSandi * function to join the text slices with correct lineendings again.
667f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
668f3f0262cSandi * lines between sections if needed (used on saving).
66915fae107Sandi *
67015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
671f3f0262cSandi */
672f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
673f3f0262cSandi
674f3f0262cSandi  if($pretty){
675f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
676f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
677f3f0262cSandi  }
678f3f0262cSandi
679f3f0262cSandi  if($pre) $pre .= "\n";
680f3f0262cSandi  if($suf) $text .= "\n";
681f3f0262cSandi  return $pre.$text.$suf;
682f3f0262cSandi}
683f3f0262cSandi
684f3f0262cSandi/**
68515fae107Sandi * print debug messages
68615fae107Sandi *
687f3f0262cSandi * little function to print the content of a var
68815fae107Sandi *
68915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
690f3f0262cSandi */
691f3f0262cSandifunction dbg($msg,$hidden=false){
692f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
693f3f0262cSandi  print_r($msg);
694f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
695f3f0262cSandi}
696f3f0262cSandi
697f3f0262cSandi/**
698f3f0262cSandi * Add's an entry to the changelog
69915fae107Sandi *
70015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
701f3f0262cSandi */
702b6912aeaSAndreas Gohrfunction addLogEntry($date,$id,$summary='',$minor=false){
703f3f0262cSandi  global $conf;
704c1049928Sandi
705c1049928Sandi  if(!@is_writable($conf['changelog'])){
706c1049928Sandi    msg($conf['changelog'].' is not writable!',-1);
707c1049928Sandi    return;
708c1049928Sandi  }
709c1049928Sandi
710652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
711f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
712f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
713f3f0262cSandi
714b6912aeaSAndreas Gohr  if($conf['useacl'] && $user && $minor){
715b6912aeaSAndreas Gohr    $summary = '*'.$summary;
716b6912aeaSAndreas Gohr  }else{
717b6912aeaSAndreas Gohr    $summary = ' '.$summary;
718b6912aeaSAndreas Gohr  }
719b6912aeaSAndreas Gohr
720f3f0262cSandi  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
721dbb00abcSEsther Brunner  io_saveFile($conf['changelog'],$logline,true);
722f3f0262cSandi}
723f3f0262cSandi
724f3f0262cSandi/**
725b6912aeaSAndreas Gohr * Checks an summary entry if it was a minor edit
726b6912aeaSAndreas Gohr *
727b6912aeaSAndreas Gohr * The summary is cleaned of the marker char
728b6912aeaSAndreas Gohr *
729b6912aeaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
730b6912aeaSAndreas Gohr */
731b6912aeaSAndreas Gohrfunction isMinor(&$summary){
732b6912aeaSAndreas Gohr  if(substr($summary,0,1) == '*'){
733b6912aeaSAndreas Gohr    $summary = substr($summary,1);
734b6912aeaSAndreas Gohr    return true;
735b6912aeaSAndreas Gohr  }
736b6912aeaSAndreas Gohr  $summary = trim($summary);
737b6912aeaSAndreas Gohr  return false;
738b6912aeaSAndreas Gohr}
739b6912aeaSAndreas Gohr
740b6912aeaSAndreas Gohr/**
741d437bcc4SAndreas Gohr * Internal function used by getRecents
742d437bcc4SAndreas Gohr *
743d437bcc4SAndreas Gohr * don't call directly
744d437bcc4SAndreas Gohr *
745d437bcc4SAndreas Gohr * @see getRecents()
746d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
747d437bcc4SAndreas Gohr */
748b6912aeaSAndreas Gohrfunction _handleRecent($line,$ns,$flags){
749d437bcc4SAndreas Gohr  static $seen  = array();         //caches seen pages and skip them
750d437bcc4SAndreas Gohr  if(empty($line)) return false;   //skip empty lines
751d437bcc4SAndreas Gohr
752d437bcc4SAndreas Gohr  // split the line into parts
753d437bcc4SAndreas Gohr  list($dt,$ip,$id,$usr,$sum) = explode("\t",$line);
754d437bcc4SAndreas Gohr
755d437bcc4SAndreas Gohr  // skip seen ones
756d437bcc4SAndreas Gohr  if($seen[$id]) return false;
757b6912aeaSAndreas Gohr  $recent = array();
758b6912aeaSAndreas Gohr
759b6912aeaSAndreas Gohr  // check minors
760b6912aeaSAndreas Gohr  if(isMinor($sum)){
761b6912aeaSAndreas Gohr    // skip minors
762b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_MINORS) return false;
763b6912aeaSAndreas Gohr    $recent['minor'] = true;
764b6912aeaSAndreas Gohr  }else{
765b6912aeaSAndreas Gohr    $recent['minor'] = false;
766b6912aeaSAndreas Gohr  }
767d437bcc4SAndreas Gohr
768d437bcc4SAndreas Gohr  // remember in seen to skip additional sights
769d437bcc4SAndreas Gohr  $seen[$id] = 1;
770d437bcc4SAndreas Gohr
7710dc92c6fSAndreas Gohr  // check if it's a hidden page
7720dc92c6fSAndreas Gohr  if(isHiddenPage($id)) return false;
7730dc92c6fSAndreas Gohr
774d437bcc4SAndreas Gohr  // filter namespace
775d437bcc4SAndreas Gohr  if (($ns) && (strpos($id,$ns.':') !== 0)) return false;
776d437bcc4SAndreas Gohr
777d437bcc4SAndreas Gohr  // exclude subnamespaces
778b6912aeaSAndreas Gohr  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($id) != $ns)) return false;
779d437bcc4SAndreas Gohr
780ae56bfb6SAndreas Gohr  // check ACL
781ae56bfb6SAndreas Gohr  if (auth_quickaclcheck($id) < AUTH_READ) return false;
782ae56bfb6SAndreas Gohr
783d437bcc4SAndreas Gohr  // check existance
784d437bcc4SAndreas Gohr  if(!@file_exists(wikiFN($id))){
785b6912aeaSAndreas Gohr    if($flags & RECENTS_SKIP_DELETED){
786d437bcc4SAndreas Gohr      return false;
787d437bcc4SAndreas Gohr    }else{
788d437bcc4SAndreas Gohr      $recent['del'] = true;
789d437bcc4SAndreas Gohr    }
790d437bcc4SAndreas Gohr  }else{
791d437bcc4SAndreas Gohr    $recent['del'] = false;
792d437bcc4SAndreas Gohr  }
793d437bcc4SAndreas Gohr
794d437bcc4SAndreas Gohr  $recent['id']   = $id;
795d437bcc4SAndreas Gohr  $recent['date'] = $dt;
796d437bcc4SAndreas Gohr  $recent['ip']   = $ip;
797d437bcc4SAndreas Gohr  $recent['user'] = $usr;
798d437bcc4SAndreas Gohr  $recent['sum']  = $sum;
799d437bcc4SAndreas Gohr
800d437bcc4SAndreas Gohr  return $recent;
801d437bcc4SAndreas Gohr}
802d437bcc4SAndreas Gohr
803b6912aeaSAndreas Gohr
804d437bcc4SAndreas Gohr/**
805f3f0262cSandi * returns an array of recently changed files using the
806f3f0262cSandi * changelog
807d437bcc4SAndreas Gohr *
808b6912aeaSAndreas Gohr * The following constants can be used to control which changes are
809b6912aeaSAndreas Gohr * included. Add them together as needed.
810b6912aeaSAndreas Gohr *
811b6912aeaSAndreas Gohr * RECENTS_SKIP_DELETED   - don't include deleted pages
812b6912aeaSAndreas Gohr * RECENTS_SKIP_MINORS    - don't include minor changes
813b6912aeaSAndreas Gohr * RECENTS_SKIP_SUBSPACES - don't include subspaces
814b6912aeaSAndreas Gohr *
815d437bcc4SAndreas Gohr * @param int    $first   number of first entry returned (for paginating
816d437bcc4SAndreas Gohr * @param int    $num     return $num entries
817d437bcc4SAndreas Gohr * @param string $ns      restrict to given namespace
818b6912aeaSAndreas Gohr * @param bool   $flags   see above
81915fae107Sandi *
82015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
821f3f0262cSandi */
822b6912aeaSAndreas Gohrfunction getRecents($first,$num,$ns='',$flags=0){
823f3f0262cSandi  global $conf;
824f3f0262cSandi  $recent = array();
825d437bcc4SAndreas Gohr  $count  = 0;
8265749f1ceSmatthiasgrimm
8275749f1ceSmatthiasgrimm  if(!$num)
8285749f1ceSmatthiasgrimm    return $recent;
829f3f0262cSandi
830c1049928Sandi  if(!@is_readable($conf['changelog'])){
831c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
832c1049928Sandi    return $recent;
833c1049928Sandi  }
834c1049928Sandi
835d437bcc4SAndreas Gohr  $fh  = fopen($conf['changelog'],'r');
836d437bcc4SAndreas Gohr  $buf = '';
837d437bcc4SAndreas Gohr  $csz = 4096;                              //chunksize
838d437bcc4SAndreas Gohr  fseek($fh,0,SEEK_END);                    // jump to the end
839d437bcc4SAndreas Gohr  $pos = ftell($fh);                        // position pointer
840f3f0262cSandi
841d437bcc4SAndreas Gohr  // now read backwards into buffer
842d437bcc4SAndreas Gohr  while($pos > 0){
843d437bcc4SAndreas Gohr    $pos -= $csz;                           // seek to previous chunk...
844f6c1156dSWolfgang Ocker    if($pos < 0) {                          // ...or rest of file
845f6c1156dSWolfgang Ocker      $csz += $pos;
846f6c1156dSWolfgang Ocker      $pos = 0;
847f6c1156dSWolfgang Ocker    }
848f6c1156dSWolfgang Ocker
849d437bcc4SAndreas Gohr    fseek($fh,$pos);
850dbb00abcSEsther Brunner
851d437bcc4SAndreas Gohr    $buf = fread($fh,$csz).$buf;            // prepend to buffer
8528f1d587cSEsther Brunner
853d437bcc4SAndreas Gohr    $lines = explode("\n",$buf);            // split buffer into lines
8545749f1ceSmatthiasgrimm
855d437bcc4SAndreas Gohr    if($pos > 0){
856d437bcc4SAndreas Gohr      $buf = array_shift($lines);           // first one may be still incomplete
857f3f0262cSandi    }
858d437bcc4SAndreas Gohr
859d437bcc4SAndreas Gohr    $cnt = count($lines);
860d437bcc4SAndreas Gohr    if(!$cnt) continue;                     // no lines yet
861d437bcc4SAndreas Gohr
862d437bcc4SAndreas Gohr    // handle lines
863d437bcc4SAndreas Gohr    for($i = $cnt-1; $i >= 0; $i--){
864b6912aeaSAndreas Gohr      $rec = _handleRecent($lines[$i],$ns,$flags);
865d437bcc4SAndreas Gohr      if($rec !== false){
866d437bcc4SAndreas Gohr        if(--$first >= 0) continue;         // skip first entries
867d437bcc4SAndreas Gohr        $recent[] = $rec;
868d437bcc4SAndreas Gohr        $count++;
869d437bcc4SAndreas Gohr
870d437bcc4SAndreas Gohr        // break while when we have enough entries
871d437bcc4SAndreas Gohr        if($count >= $num){
872d437bcc4SAndreas Gohr          $pos = 0; // will break the while loop
873d437bcc4SAndreas Gohr          break;    // will break the for loop
874f3f0262cSandi        }
875f3f0262cSandi      }
876d437bcc4SAndreas Gohr    }
877d437bcc4SAndreas Gohr  }// end of while
878d437bcc4SAndreas Gohr
879d437bcc4SAndreas Gohr  fclose($fh);
880f3f0262cSandi  return $recent;
881f3f0262cSandi}
882f3f0262cSandi
883f3f0262cSandi/**
88451815db0SYann * Compare the logline $a to the timestamp $b
88551815db0SYann * @author Yann Hamon <yann.hamon@mandragor.org>
88651815db0SYann * @return integer 0 if the logline has timestamp $b, <0 if the timestam
88751815db0SYann *         of $a is greater than $b, >0 else.
88851815db0SYann */
88951815db0SYannfunction hasTimestamp($a, $b)
89051815db0SYann{
89151815db0SYann  if (strpos($a, $b) === 0)
89251815db0SYann    return 0;
89351815db0SYann  else
89451815db0SYann    return strcmp ($a, $b);
89551815db0SYann}
89651815db0SYann
89751815db0SYann/**
89851815db0SYann * performs a dichotomic search on an array using
89951815db0SYann * a custom compare function
90051815db0SYann *
90151815db0SYann * @author Yann Hamon <yann.hamon@mandragor.org>
90251815db0SYann */
90351815db0SYannfunction array_dichotomic_search($ar, $value, $compareFunc) {
90451815db0SYann  $value = trim($value);
90551815db0SYann  if (!$ar || !$value || !$compareFunc) return (null);
90651815db0SYann  $len = count($ar);
90751815db0SYann
90851815db0SYann  $l = 0;
90951815db0SYann  $r = $len-1;
91051815db0SYann
91151815db0SYann  do {
91251815db0SYann    $i = floor(($l+$r)/2);
91351815db0SYann    if ($compareFunc($ar[$i], $value)<0)
91451815db0SYann      $l = $i+1;
91551815db0SYann    else
91651815db0SYann     $r = $i-1;
91751815db0SYann  } while ($compareFunc($ar[$i], $value)!=0 && $l<=$r);
91851815db0SYann
91951815db0SYann  if ($compareFunc($ar[$i], $value)==0)
92051815db0SYann    return $i;
92151815db0SYann  else
92251815db0SYann    return -1;
92351815db0SYann}
92451815db0SYann
92551815db0SYann/**
926652610a2Sandi * gets additonal informations for a certain pagerevison
927652610a2Sandi * from the changelog
928652610a2Sandi *
929652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org>
93051815db0SYann * @author Yann Hamon <yann.hamon@mandragor.org>
931fb53bfe2SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
932652610a2Sandi */
933fb53bfe2SBen Coburnfunction getRevisionInfo($id,$rev,$mem_cache=true){
934652610a2Sandi  global $conf;
935fb53bfe2SBen Coburn  global $doku_temporary_revinfo_cache;
936fb53bfe2SBen Coburn  $cache =& $doku_temporary_revinfo_cache;
937258641c6Sandi  if(!$rev) return(null);
938258641c6Sandi
939fb53bfe2SBen Coburn  // check if it's already in the memory cache
940fb53bfe2SBen Coburn  if (is_array($cache) && isset($cache[$id]) && isset($cache[$id][$rev])) {
941fb53bfe2SBen Coburn    return $cache[$id][$rev];
942fb53bfe2SBen Coburn  }
943fb53bfe2SBen Coburn
944c1049928Sandi  $info = array();
945c1049928Sandi  if(!@is_readable($conf['changelog'])){
946c1049928Sandi    msg($conf['changelog'].' is not readable',-1);
947c1049928Sandi    return $recent;
948c1049928Sandi  }
949652610a2Sandi  $loglines = file($conf['changelog']);
95051815db0SYann
951fb53bfe2SBen Coburn  if (!$mem_cache) {
95251815db0SYann    // Search for a line with a matching timestamp
953fb53bfe2SBen Coburn    $index = array_dichotomic_search($loglines, $rev, 'hasTimestamp');
95451815db0SYann    if ($index == -1)
95551815db0SYann      return;
95651815db0SYann
95751815db0SYann    // The following code is necessary when there is more than
95851815db0SYann    // one line with one same timestamp
95951815db0SYann    $loglines_matching = array();
9602e2fda08SYann    for ($i=$index-1;$i>=0 && hasTimestamp($loglines[$i], $rev) == 0; $i--)
96151815db0SYann      $loglines_matching[] = $loglines[$i];
962fb53bfe2SBen Coburn    $loglines_matching = array_reverse($loglines_matching);
963fb53bfe2SBen Coburn    $loglines_matching[] =  $loglines[$index];
9642e2fda08SYann    $logsize = count($loglines);
9652e2fda08SYann    for ($i=$index+1;$i<$logsize && hasTimestamp($loglines[$i], $rev) == 0; $i++)
96651815db0SYann      $loglines_matching[] = $loglines[$i];
96751815db0SYann
968fb53bfe2SBen Coburn    // pull off the line most recent line with the right id
969fb53bfe2SBen Coburn    $loglines_matching = array_reverse($loglines_matching); //newest first
970fb53bfe2SBen Coburn    foreach ($loglines_matching as $logline) {
971fb53bfe2SBen Coburn      $line = explode("\t", $logline);
972fb53bfe2SBen Coburn      if ($line[2]==$id) {
973652610a2Sandi        $info['date']  = $line[0];
974652610a2Sandi        $info['ip']    = $line[1];
975652610a2Sandi        $info['user']  = $line[3];
976652610a2Sandi        $info['sum']   = $line[4];
977b6912aeaSAndreas Gohr        $info['minor'] = isMinor($info['sum']);
978fb53bfe2SBen Coburn        break;
979fb53bfe2SBen Coburn      }
980fb53bfe2SBen Coburn    }
981fb53bfe2SBen Coburn  } else {
982fb53bfe2SBen Coburn    // load and cache all the lines with the right id
983fb53bfe2SBen Coburn    if(!is_array($cache)) { $cache = array(); }
984fb53bfe2SBen Coburn    if (!isset($cache[$id])) { $cache[$id] = array(); }
985fb53bfe2SBen Coburn    foreach ($loglines as $logline) {
986fb53bfe2SBen Coburn      $start = strpos($logline, "\t", strpos($logline, "\t")+1)+1;
987fb53bfe2SBen Coburn      $end = strpos($logline, "\t", $start);
988fb53bfe2SBen Coburn      if (substr($logline, $start, $end-$start)==$id) {
989fb53bfe2SBen Coburn        $line = explode("\t", $logline);
990fb53bfe2SBen Coburn        $info = array();
991fb53bfe2SBen Coburn        $info['date']  = $line[0];
992fb53bfe2SBen Coburn        $info['ip']    = $line[1];
993fb53bfe2SBen Coburn        $info['user']  = $line[3];
994fb53bfe2SBen Coburn        $info['sum']   = $line[4];
995fb53bfe2SBen Coburn        $info['minor'] = isMinor($info['sum']);
996fb53bfe2SBen Coburn        $cache[$id][$info['date']] = $info;
997fb53bfe2SBen Coburn      }
998fb53bfe2SBen Coburn    }
999fb53bfe2SBen Coburn    $info = $cache[$id][$rev];
1000fb53bfe2SBen Coburn  }
1001fb53bfe2SBen Coburn
1002652610a2Sandi  return $info;
1003652610a2Sandi}
1004652610a2Sandi
100551815db0SYann
1006652610a2Sandi/**
1007f3f0262cSandi * Saves a wikitext by calling io_saveFile
100815fae107Sandi *
100915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1010f3f0262cSandi */
1011b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
1012f3f0262cSandi  global $conf;
1013f3f0262cSandi  global $lang;
1014f3f0262cSandi  // ignore if no changes were made
1015f3f0262cSandi  if($text == rawWiki($id,'')){
1016f3f0262cSandi    return;
1017f3f0262cSandi  }
1018f3f0262cSandi
1019f3f0262cSandi  $file = wikiFN($id);
1020f3f0262cSandi  $old  = saveOldRevision($id);
1021f3f0262cSandi
1022f3f0262cSandi  if (empty($text)){
1023e1f3d9e1SEsther Brunner    // remove empty file
1024f3f0262cSandi    @unlink($file);
1025e1f3d9e1SEsther Brunner    // remove any meta info
1026e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
1027e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
1028e1f3d9e1SEsther Brunner      if (file_exists($mfile)) @unlink($mfile);
1029b158d625SSteven Danz    }
1030f3f0262cSandi    $del = true;
10313ce054b3Sandi    // autoset summary on deletion
10323ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
1033f864871eSAndreas Gohr    // unlock early
1034f864871eSAndreas Gohr    unlock($id);
103553d6ccfeSandi    // remove empty namespaces
103653d6ccfeSandi    io_sweepNS($id);
1037f3f0262cSandi  }else{
1038f3f0262cSandi    // save file (datadir is created in io_saveFile)
1039f3f0262cSandi    io_saveFile($file,$text);
104039a89382SEsther Brunner    saveMetadata($id, $file, $minor);
1041f3f0262cSandi    $del = false;
1042f3f0262cSandi  }
1043f3f0262cSandi
1044b6912aeaSAndreas Gohr  addLogEntry(@filemtime($file),$id,$summary,$minor);
104526a0801fSAndreas Gohr  // send notify mails
104690033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
104790033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
1048f3f0262cSandi
1049f3f0262cSandi  //purge cache on add by updating the purgefile
1050f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
105198407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
1052f3f0262cSandi  }
1053f3f0262cSandi}
1054f3f0262cSandi
1055f3f0262cSandi/**
105639a89382SEsther Brunner * saves the metadata for a page
105739a89382SEsther Brunner *
105839a89382SEsther Brunner * @author Esther Brunner <wikidesign@gmail.com>
105939a89382SEsther Brunner */
106039a89382SEsther Brunnerfunction saveMetadata($id, $file, $minor){
106139a89382SEsther Brunner  global $INFO;
106239a89382SEsther Brunner
106339a89382SEsther Brunner  $user = $_SERVER['REMOTE_USER'];
106439a89382SEsther Brunner
106539a89382SEsther Brunner  $meta = array();
106639a89382SEsther Brunner  if (!$INFO['exists']){ // newly created
106739a89382SEsther Brunner    $meta['date']['created'] = @filectime($file);
106839a89382SEsther Brunner    if ($user) $meta['creator'] = $INFO['userinfo']['name'];
106939a89382SEsther Brunner  } elseif (!$minor) {   // non-minor modification
107039a89382SEsther Brunner    $meta['date']['modified'] = @filemtime($file);
107139a89382SEsther Brunner    if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name'];
107239a89382SEsther Brunner  }
107339a89382SEsther Brunner  p_set_metadata($id, $meta, true);
107439a89382SEsther Brunner}
107539a89382SEsther Brunner
107639a89382SEsther Brunner/**
1077f3f0262cSandi * moves the current version to the attic and returns its
1078f3f0262cSandi * revision date
107915fae107Sandi *
108015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1081f3f0262cSandi */
1082f3f0262cSandifunction saveOldRevision($id){
1083f3f0262cSandi  global $conf;
1084f3f0262cSandi  $oldf = wikiFN($id);
1085f3f0262cSandi  if(!@file_exists($oldf)) return '';
1086f3f0262cSandi  $date = filemtime($oldf);
1087f3f0262cSandi  $newf = wikiFN($id,$date);
1088f3f0262cSandi  if(substr($newf,-3)=='.gz'){
1089f3f0262cSandi    io_saveFile($newf,rawWiki($id));
1090f3f0262cSandi  }else{
1091f3f0262cSandi    io_makeFileDir($newf);
1092f3f0262cSandi    copy($oldf, $newf);
1093f3f0262cSandi  }
1094f3f0262cSandi  return $date;
1095f3f0262cSandi}
1096f3f0262cSandi
1097f3f0262cSandi/**
109826a0801fSAndreas Gohr * Sends a notify mail on page change
109926a0801fSAndreas Gohr *
110026a0801fSAndreas Gohr * @param  string  $id       The changed page
110126a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
110226a0801fSAndreas Gohr * @param  int     $rev      Old page revision
110326a0801fSAndreas Gohr * @param  string  $summary  What changed
110490033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
110515fae107Sandi *
110615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1107f3f0262cSandi */
110890033e9dSAndreas Gohrfunction notify($id,$who,$rev='',$summary='',$minor=false){
1109f3f0262cSandi  global $lang;
1110f3f0262cSandi  global $conf;
1111b158d625SSteven Danz
111226a0801fSAndreas Gohr  // decide if there is something to do
111326a0801fSAndreas Gohr  if($who == 'admin'){
111426a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
1115f3f0262cSandi    $text = rawLocale('mailtext');
111626a0801fSAndreas Gohr    $to   = $conf['notify'];
111726a0801fSAndreas Gohr    $bcc  = '';
111826a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
111926a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
112090033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
112126a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
112226a0801fSAndreas Gohr    if(empty($bcc)) return;
112326a0801fSAndreas Gohr    $to   = '';
112426a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
112526a0801fSAndreas Gohr  }else{
112626a0801fSAndreas Gohr    return; //just to be safe
112726a0801fSAndreas Gohr  }
112826a0801fSAndreas Gohr
1129f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
1130f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
1131f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
1132f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
1133ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
113426a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
113526a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
1136ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
1137f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
11387a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
1139f3f0262cSandi
1140f3f0262cSandi  if($rev){
1141f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
1142ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
1143ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
1144f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
1145f3f0262cSandi                    split("\n",rawWiki($id)));
1146f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
1147f3f0262cSandi    $diff    = $dformat->format($df);
1148f3f0262cSandi  }else{
1149f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
1150f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
1151f3f0262cSandi    $diff = rawWiki($id);
1152f3f0262cSandi  }
1153f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
1154241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
1155f3f0262cSandi
115626a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
1157f3f0262cSandi}
1158f3f0262cSandi
115915fae107Sandi/**
116015fae107Sandi * Return a list of available page revisons
116115fae107Sandi *
116215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
116315fae107Sandi */
1164f3f0262cSandifunction getRevisions($id){
1165f3f0262cSandi  $revd = dirname(wikiFN($id,'foo'));
1166f3f0262cSandi  $revs = array();
1167f3f0262cSandi  $clid = cleanID($id);
1168f3f0262cSandi  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
1169493a6929SKobaYY  $clid = utf8_encodeFN($clid);
11706f6c2468SBen Coburn  $clid_len = strlen($clid);
1171f3f0262cSandi  if (is_dir($revd) && $dh = opendir($revd)) {
1172f3f0262cSandi    while (($file = readdir($dh)) !== false) {
11736f6c2468SBen Coburn      if (substr($file, 0, $clid_len)===$clid) {
1174*3b97c5ecSchris        $p = @strpos($file, '.', $clid_len+1);
11756f6c2468SBen Coburn        if (!$p===false) {
11766f6c2468SBen Coburn          $revs[] = substr($file, $clid_len+1, $p-$clid_len-1);
11776f6c2468SBen Coburn        }
1178f3f0262cSandi      }
1179f3f0262cSandi    }
1180f3f0262cSandi    closedir($dh);
1181f3f0262cSandi  }
1182f3f0262cSandi  rsort($revs);
1183f3f0262cSandi  return $revs;
1184f3f0262cSandi}
1185f3f0262cSandi
1186f3f0262cSandi/**
1187f3f0262cSandi * extracts the query from a google referer
118815fae107Sandi *
11896b13307fSandi * @todo   should be more generic and support yahoo et al
119015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1191f3f0262cSandi */
1192f3f0262cSandifunction getGoogleQuery(){
1193f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
11945c3f206fSandi  if(!$url) return '';
1195f3f0262cSandi
1196f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
1197f3f0262cSandi  $query = array();
1198f3f0262cSandi  parse_str($url['query'],$query);
1199f3f0262cSandi
1200f3f0262cSandi  return $query['q'];
1201f3f0262cSandi}
1202f3f0262cSandi
1203f3f0262cSandi/**
120415fae107Sandi * Try to set correct locale
120515fae107Sandi *
1206095bfd5cSandi * @deprecated No longer used
120715fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
1208f3f0262cSandi */
1209f3f0262cSandifunction setCorrectLocale(){
1210f3f0262cSandi  global $conf;
1211f3f0262cSandi  global $lang;
1212f3f0262cSandi
1213f3f0262cSandi  $enc = strtoupper($lang['encoding']);
1214f3f0262cSandi  foreach ($lang['locales'] as $loc){
1215f3f0262cSandi    //try locale
1216f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
1217f3f0262cSandi    //try loceale with encoding
1218f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
1219f3f0262cSandi  }
1220f3f0262cSandi  //still here? try to set from environment
1221f3f0262cSandi  @setlocale(LC_ALL,"");
1222f3f0262cSandi}
1223f3f0262cSandi
1224f3f0262cSandi/**
1225f3f0262cSandi * Return the human readable size of a file
1226f3f0262cSandi *
1227f3f0262cSandi * @param       int    $size   A file size
1228f3f0262cSandi * @param       int    $dec    A number of decimal places
1229f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
1230f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
1231f3f0262cSandi * @version     1.0.0
1232f3f0262cSandi */
1233f31d5b73Sandifunction filesize_h($size, $dec = 1){
1234f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
1235f3f0262cSandi  $count = count($sizes);
1236f3f0262cSandi  $i = 0;
1237f3f0262cSandi
1238f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
1239f3f0262cSandi    $size /= 1024;
1240f3f0262cSandi    $i++;
1241f3f0262cSandi  }
1242f3f0262cSandi
1243f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
1244f3f0262cSandi}
1245f3f0262cSandi
124615fae107Sandi/**
124700a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
124800a7b5adSEsther Brunner *
124900a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
125000a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
125100a7b5adSEsther Brunner */
125200a7b5adSEsther Brunnerfunction obfuscate($email) {
125300a7b5adSEsther Brunner  global $conf;
125400a7b5adSEsther Brunner
125500a7b5adSEsther Brunner  switch ($conf['mailguard']) {
125600a7b5adSEsther Brunner    case 'visible' :
125700a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
125800a7b5adSEsther Brunner      return strtr($email, $obfuscate);
125900a7b5adSEsther Brunner
126000a7b5adSEsther Brunner    case 'hex' :
126100a7b5adSEsther Brunner      $encode = '';
126200a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
126300a7b5adSEsther Brunner      return $encode;
126400a7b5adSEsther Brunner
126500a7b5adSEsther Brunner    case 'none' :
126600a7b5adSEsther Brunner    default :
126700a7b5adSEsther Brunner      return $email;
126800a7b5adSEsther Brunner  }
126900a7b5adSEsther Brunner}
127000a7b5adSEsther Brunner
127100a7b5adSEsther Brunner/**
1272dc57ef04Sandi * Return DokuWikis version
127315fae107Sandi *
127415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
127515fae107Sandi */
1276f31d5b73Sandifunction getVersion(){
1277f31d5b73Sandi  //import version string
1278f31d5b73Sandi  if(@file_exists('VERSION')){
1279f31d5b73Sandi    //official release
12800647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1281f31d5b73Sandi  }elseif(is_dir('_darcs')){
1282f31d5b73Sandi    //darcs checkout
1283f31d5b73Sandi    $inv = file('_darcs/inventory');
1284ae41559bSAndreas Gohr    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
1285f31d5b73Sandi    $cur = array_pop($inv);
1286f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1287f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1288f31d5b73Sandi  }else{
1289f31d5b73Sandi    return 'snapshot?';
1290f31d5b73Sandi  }
1291f31d5b73Sandi}
1292f31d5b73Sandi
1293f31d5b73Sandi/**
1294f31d5b73Sandi * Run a few sanity checks
1295f31d5b73Sandi *
1296f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
1297f31d5b73Sandi */
1298f3f0262cSandifunction check(){
1299f3f0262cSandi  global $conf;
1300f3f0262cSandi  global $INFO;
1301f3f0262cSandi
1302f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
1303f31d5b73Sandi
130449022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
130549022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
130649022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
130749022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
130849022a38Sandi  }else{
130949022a38Sandi    msg('PHP version '.phpversion(),1);
131049022a38Sandi  }
131149022a38Sandi
1312f3f0262cSandi  if(is_writable($conf['changelog'])){
1313f3f0262cSandi    msg('Changelog is writable',1);
1314f3f0262cSandi  }else{
1315f3f0262cSandi    msg('Changelog is not writable',-1);
1316f3f0262cSandi  }
1317f3f0262cSandi
1318f3f0262cSandi  if(is_writable($conf['datadir'])){
1319f3f0262cSandi    msg('Datadir is writable',1);
1320f3f0262cSandi  }else{
1321f3f0262cSandi    msg('Datadir is not writable',-1);
1322f3f0262cSandi  }
1323f3f0262cSandi
1324f3f0262cSandi  if(is_writable($conf['olddir'])){
1325f3f0262cSandi    msg('Attic is writable',1);
1326f3f0262cSandi  }else{
1327f3f0262cSandi    msg('Attic is not writable',-1);
1328f3f0262cSandi  }
1329f3f0262cSandi
1330f3f0262cSandi  if(is_writable($conf['mediadir'])){
1331f3f0262cSandi    msg('Mediadir is writable',1);
1332f3f0262cSandi  }else{
1333f3f0262cSandi    msg('Mediadir is not writable',-1);
1334f3f0262cSandi  }
1335f3f0262cSandi
133698407a7aSandi  if(is_writable($conf['cachedir'])){
133798407a7aSandi    msg('Cachedir is writable',1);
133898407a7aSandi  }else{
133998407a7aSandi    msg('Cachedir is not writable',-1);
134098407a7aSandi  }
134198407a7aSandi
13427de6c234SAndreas Gohr  if(is_writable($conf['lockdir'])){
13437de6c234SAndreas Gohr    msg('Lockdir is writable',1);
13447de6c234SAndreas Gohr  }else{
13457de6c234SAndreas Gohr    msg('Lockdir is not writable',-1);
13467de6c234SAndreas Gohr  }
13477de6c234SAndreas Gohr
1348e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
13498c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1350f3f0262cSandi  }else{
13518c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1352f3f0262cSandi  }
135393a9e835Sandi
135493a9e835Sandi  if(function_exists('mb_strpos')){
135593a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
135693a9e835Sandi      msg('mb_string extension is available but will not be used',0);
135793a9e835Sandi    }else{
135893a9e835Sandi      msg('mb_string extension is available and will be used',1);
135993a9e835Sandi    }
136093a9e835Sandi  }else{
136193a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
136293a9e835Sandi  }
1363f42d1c75SAndreas Gohr
1364f42d1c75SAndreas Gohr  if($conf['allowdebug']){
1365f42d1c75SAndreas Gohr    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1366f42d1c75SAndreas Gohr  }else{
1367f42d1c75SAndreas Gohr    msg('Debugging support is disabled',1);
1368f42d1c75SAndreas Gohr  }
1369f3f0262cSandi
1370f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1371f3f0262cSandi
1372f3f0262cSandi  if(is_writable($INFO['filepath'])){
1373f3f0262cSandi    msg('The current page is writable by the webserver',0);
1374f3f0262cSandi  }else{
1375f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1376f3f0262cSandi  }
1377f3f0262cSandi
1378f3f0262cSandi  if($INFO['writable']){
1379f3f0262cSandi    msg('The current page is writable by you',0);
1380f3f0262cSandi  }else{
1381f3f0262cSandi    msg('The current page is not writable you',0);
1382f3f0262cSandi  }
1383f3f0262cSandi}
1384340756e4Sandi
1385b158d625SSteven Danz/**
1386b158d625SSteven Danz * Let us know if a user is tracking a page
1387b158d625SSteven Danz *
13881380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1389b158d625SSteven Danz */
13901380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
13911380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
13921380fc45SAndreas Gohr  if (@file_exists($file)) {
1393b158d625SSteven Danz    $mlist = file($file);
13941380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
13951380fc45SAndreas Gohr    return is_int($pos);
1396b158d625SSteven Danz  }
13971380fc45SAndreas Gohr
1398b158d625SSteven Danz  return false;
1399b158d625SSteven Danz}
1400340756e4Sandi
1401f9eb5648Ssteven-danz/**
1402f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1403f9eb5648Ssteven-danz * users subscribed to a page
1404f9eb5648Ssteven-danz *
140526a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1406f9eb5648Ssteven-danz */
1407f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1408f9eb5648Ssteven-danz  global $conf;
1409cd52f92dSchris  global $auth;
1410f9eb5648Ssteven-danz
1411f9eb5648Ssteven-danz  $emails = '';
1412f9eb5648Ssteven-danz
141326a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
141426a0801fSAndreas Gohr
1415f9eb5648Ssteven-danz  $mlist = array();
1416f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1417f9eb5648Ssteven-danz  if (file_exists($file)) {
1418f9eb5648Ssteven-danz    $mlist = file($file);
1419f9eb5648Ssteven-danz  }
1420f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1421f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1422f9eb5648Ssteven-danz      $who = rtrim($who);
1423cd52f92dSchris      $info = $auth->getUserData($who);
1424f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1425f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1426f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1427f9eb5648Ssteven-danz          if (empty($emails)) {
1428f9eb5648Ssteven-danz            $emails = $info['mail'];
1429f9eb5648Ssteven-danz          } else {
1430f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1431f9eb5648Ssteven-danz          }
1432f9eb5648Ssteven-danz        }
1433f9eb5648Ssteven-danz      }
1434f9eb5648Ssteven-danz    }
1435f9eb5648Ssteven-danz  }
1436f9eb5648Ssteven-danz
1437f9eb5648Ssteven-danz  return $emails;
1438f9eb5648Ssteven-danz}
1439f9eb5648Ssteven-danz
144089541d4bSAndreas Gohr/**
144189541d4bSAndreas Gohr * Removes quoting backslashes
144289541d4bSAndreas Gohr *
144389541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
144489541d4bSAndreas Gohr */
144589541d4bSAndreas Gohrfunction unslash($string,$char="'"){
144689541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
144789541d4bSAndreas Gohr}
144889541d4bSAndreas Gohr
1449340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1450