xref: /dokuwiki/inc/common.php (revision d818621633e2c687264682f0504869858f9c780d)
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
9771726d78SBen Coburn  //load page meta data
9871726d78SBen Coburn  $info['meta'] = p_get_metadata($ID);
9971726d78SBen Coburn
100652610a2Sandi  //who's the editor
101652610a2Sandi  if($REV){
10271726d78SBen Coburn    $revinfo = getRevisionInfo($ID, $REV, 1024);
103652610a2Sandi  }else{
10471726d78SBen Coburn    $revinfo = $info['meta']['last_change'];
105652610a2Sandi  }
106652610a2Sandi  $info['ip']     = $revinfo['ip'];
107652610a2Sandi  $info['user']   = $revinfo['user'];
108652610a2Sandi  $info['sum']    = $revinfo['sum'];
10971726d78SBen Coburn  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
11071726d78SBen Coburn  // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor'].
11159f257aeSchris
11288f522e9Sandi  if($revinfo['user']){
11388f522e9Sandi    $info['editor'] = $revinfo['user'];
11488f522e9Sandi  }else{
11588f522e9Sandi    $info['editor'] = $revinfo['ip'];
11688f522e9Sandi  }
117652610a2Sandi
118ee4c4a1bSAndreas Gohr  // draft
119ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
120ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
121ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
122ee4c4a1bSAndreas Gohr      // remove stale draft
123ee4c4a1bSAndreas Gohr      @unlink($draft);
124ee4c4a1bSAndreas Gohr    }else{
125ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
126ee4c4a1bSAndreas Gohr    }
127ee4c4a1bSAndreas Gohr  }
128ee4c4a1bSAndreas Gohr
129f3f0262cSandi  return $info;
130f3f0262cSandi}
131f3f0262cSandi
132f3f0262cSandi/**
1332684e50aSAndreas Gohr * Build an string of URL parameters
1342684e50aSAndreas Gohr *
1352684e50aSAndreas Gohr * @author Andreas Gohr
1362684e50aSAndreas Gohr */
137b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
1382684e50aSAndreas Gohr  $url = '';
1392684e50aSAndreas Gohr  $amp = false;
1402684e50aSAndreas Gohr  foreach($params as $key => $val){
141b174aeaeSchris    if($amp) $url .= $sep;
1422684e50aSAndreas Gohr
1432684e50aSAndreas Gohr    $url .= $key.'=';
144b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1452684e50aSAndreas Gohr    $amp = true;
1462684e50aSAndreas Gohr  }
1472684e50aSAndreas Gohr  return $url;
1482684e50aSAndreas Gohr}
1492684e50aSAndreas Gohr
1502684e50aSAndreas Gohr/**
1512684e50aSAndreas Gohr * Build an string of html tag attributes
1522684e50aSAndreas Gohr *
1532684e50aSAndreas Gohr * @author Andreas Gohr
1542684e50aSAndreas Gohr */
1552684e50aSAndreas Gohrfunction buildAttributes($params){
1562684e50aSAndreas Gohr  $url = '';
1572684e50aSAndreas Gohr  foreach($params as $key => $val){
1582684e50aSAndreas Gohr    $url .= $key.'="';
1592684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1602684e50aSAndreas Gohr    $url .= '" ';
1612684e50aSAndreas Gohr  }
1622684e50aSAndreas Gohr  return $url;
1632684e50aSAndreas Gohr}
1642684e50aSAndreas Gohr
1652684e50aSAndreas Gohr
1662684e50aSAndreas Gohr/**
1670396becbSandi * print a message
1680396becbSandi *
1690396becbSandi * If HTTP headers were not sent yet the message is added
1700396becbSandi * to the global message array else it's printed directly
1710396becbSandi * using html_msgarea()
1720396becbSandi *
173f3f0262cSandi *
174f3f0262cSandi * Levels can be:
175f3f0262cSandi *
176f3f0262cSandi * -1 error
177f3f0262cSandi *  0 info
178f3f0262cSandi *  1 success
17915fae107Sandi *
18015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1810396becbSandi * @see    html_msgarea
182f3f0262cSandi */
1830d58d74eSAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){
184f3f0262cSandi  global $MSG;
185f3f0262cSandi  $errors[-1] = 'error';
186f3f0262cSandi  $errors[0]  = 'info';
187f3f0262cSandi  $errors[1]  = 'success';
188f3f0262cSandi
1890d58d74eSAndreas Gohr  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
1900d58d74eSAndreas Gohr
191cc20ad51Sandi  if(!headers_sent()){
192f3f0262cSandi    if(!isset($MSG)) $MSG = array();
193f3f0262cSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
1940396becbSandi  }else{
1950396becbSandi    $MSG = array();
1960396becbSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
197f62ea8a1Sandi    if(function_exists('html_msgarea')){
1980396becbSandi      html_msgarea();
199f62ea8a1Sandi    }else{
200f62ea8a1Sandi      print "ERROR($lvl) $message";
201f62ea8a1Sandi    }
2020396becbSandi  }
203f3f0262cSandi}
204f3f0262cSandi
205f3f0262cSandi/**
20615fae107Sandi * This builds the breadcrumb trail and returns it as array
20715fae107Sandi *
20815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
209f3f0262cSandi */
210f3f0262cSandifunction breadcrumbs(){
2118746e727Sandi  // we prepare the breadcrumbs early for quick session closing
2128746e727Sandi  static $crumbs = null;
2138746e727Sandi  if($crumbs != null) return $crumbs;
2148746e727Sandi
215f3f0262cSandi  global $ID;
216f3f0262cSandi  global $ACT;
217f3f0262cSandi  global $conf;
218f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
219f3f0262cSandi
220f3f0262cSandi  //first visit?
221f3f0262cSandi  if (!is_array($crumbs)){
222f3f0262cSandi    $crumbs = array();
223f3f0262cSandi  }
224f3f0262cSandi  //we only save on show and existing wiki documents
225a77f5846Sjan  $file = wikiFN($ID);
226a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
227f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
228f3f0262cSandi    return $crumbs;
229f3f0262cSandi  }
230a77f5846Sjan
231a77f5846Sjan  // page names
232a77f5846Sjan  $name = noNS($ID);
233a77f5846Sjan  if ($conf['useheading']) {
234a77f5846Sjan    // get page title
235bb0a59d4Sjan    $title = p_get_first_heading($ID);
236a77f5846Sjan    if ($title) {
237a77f5846Sjan      $name = $title;
238a77f5846Sjan    }
239a77f5846Sjan  }
240a77f5846Sjan
241f3f0262cSandi  //remove ID from array
242a77f5846Sjan  if (isset($crumbs[$ID])) {
243a77f5846Sjan    unset($crumbs[$ID]);
244f3f0262cSandi  }
245f3f0262cSandi
246f3f0262cSandi  //add to array
247a77f5846Sjan  $crumbs[$ID] = $name;
248f3f0262cSandi  //reduce size
249f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
250f3f0262cSandi    array_shift($crumbs);
251f3f0262cSandi  }
252f3f0262cSandi  //save to session
253f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
254f3f0262cSandi  return $crumbs;
255f3f0262cSandi}
256f3f0262cSandi
257f3f0262cSandi/**
25815fae107Sandi * Filter for page IDs
25915fae107Sandi *
260f3f0262cSandi * This is run on a ID before it is outputted somewhere
261f3f0262cSandi * currently used to replace the colon with something else
262f3f0262cSandi * on Windows systems and to have proper URL encoding
26315fae107Sandi *
26449c713a3Sandi * Urlencoding is ommitted when the second parameter is false
26549c713a3Sandi *
26615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
267f3f0262cSandi */
26849c713a3Sandifunction idfilter($id,$ue=true){
269f3f0262cSandi  global $conf;
270f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
271f3f0262cSandi    $id = strtr($id,':','/');
272f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
273f3f0262cSandi      $conf['userewrite']) {
274f3f0262cSandi    $id = strtr($id,':',';');
275f3f0262cSandi  }
27649c713a3Sandi  if($ue){
277b6c6979fSAndreas Gohr    $id = rawurlencode($id);
278f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
279f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
28049c713a3Sandi  }
281f3f0262cSandi  return $id;
282f3f0262cSandi}
283f3f0262cSandi
284f3f0262cSandi/**
285ed7b5f09Sandi * This builds a link to a wikipage
28615fae107Sandi *
2876c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2886c7843b5Sandi * given in $more
2896c7843b5Sandi *
29015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
291f3f0262cSandi */
292b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
293f3f0262cSandi  global $conf;
2946de3759aSAndreas Gohr  if(is_array($more)){
295b174aeaeSchris    $more = buildURLparams($more,$sep);
2966de3759aSAndreas Gohr  }else{
297b174aeaeSchris    $more = str_replace(',',$sep,$more);
2986de3759aSAndreas Gohr  }
299f3f0262cSandi
300f3f0262cSandi  $id    = idfilter($id);
301ed7b5f09Sandi  if($abs){
302ed7b5f09Sandi    $xlink = DOKU_URL;
303ed7b5f09Sandi  }else{
304ed7b5f09Sandi    $xlink = DOKU_BASE;
305ed7b5f09Sandi  }
306f3f0262cSandi
3076c7843b5Sandi  if($conf['userewrite'] == 2){
3086c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
3096c7843b5Sandi    if($more) $xlink .= '?'.$more;
3106c7843b5Sandi  }elseif($conf['userewrite']){
311f3f0262cSandi    $xlink .= $id;
312f3f0262cSandi    if($more) $xlink .= '?'.$more;
3136c7843b5Sandi  }else{
3146c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
315b174aeaeSchris    if($more) $xlink .= $sep.$more;
316f3f0262cSandi  }
317f3f0262cSandi
318f3f0262cSandi  return $xlink;
319f3f0262cSandi}
320f3f0262cSandi
321f3f0262cSandi/**
322f5c2808fSBen Coburn * This builds a link to an alternate page format
323f5c2808fSBen Coburn *
324f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
325f5c2808fSBen Coburn *
326f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
327f5c2808fSBen Coburn */
328f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
329f5c2808fSBen Coburn  global $conf;
330f5c2808fSBen Coburn  if(is_array($more)){
331f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
332f5c2808fSBen Coburn  }else{
333f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
334f5c2808fSBen Coburn  }
335f5c2808fSBen Coburn
336f5c2808fSBen Coburn  $format = rawurlencode($format);
337f5c2808fSBen Coburn  $id = idfilter($id);
338f5c2808fSBen Coburn  if($abs){
339f5c2808fSBen Coburn    $xlink = DOKU_URL;
340f5c2808fSBen Coburn  }else{
341f5c2808fSBen Coburn    $xlink = DOKU_BASE;
342f5c2808fSBen Coburn  }
343f5c2808fSBen Coburn
344f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
345f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
346f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
347f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
348f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
349f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
350f5c2808fSBen Coburn  }else{
351f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
352f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
353f5c2808fSBen Coburn  }
354f5c2808fSBen Coburn
355f5c2808fSBen Coburn  return $xlink;
356f5c2808fSBen Coburn}
357f5c2808fSBen Coburn
358f5c2808fSBen Coburn/**
3596de3759aSAndreas Gohr * Build a link to a media file
3606de3759aSAndreas Gohr *
3616de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
3626de3759aSAndreas Gohr */
363b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
3646de3759aSAndreas Gohr  global $conf;
3656de3759aSAndreas Gohr  if(is_array($more)){
366b174aeaeSchris    $more = buildURLparams($more,$sep);
3676de3759aSAndreas Gohr  }else{
368b174aeaeSchris    $more = str_replace(',',$sep,$more);
3696de3759aSAndreas Gohr  }
3706de3759aSAndreas Gohr
3716de3759aSAndreas Gohr  $xlink = DOKU_BASE;
3726de3759aSAndreas Gohr
3736de3759aSAndreas Gohr  // external URLs are always direct without rewriting
3746de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
3756de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
3766de3759aSAndreas Gohr    if($more){
3776de3759aSAndreas Gohr      $xlink .= '?'.$more;
378b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3796de3759aSAndreas Gohr    }else{
380b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3816de3759aSAndreas Gohr    }
3826de3759aSAndreas Gohr    return $xlink;
3836de3759aSAndreas Gohr  }
3846de3759aSAndreas Gohr
3856de3759aSAndreas Gohr  $id = idfilter($id);
3866de3759aSAndreas Gohr
3876de3759aSAndreas Gohr  // decide on scriptname
3886de3759aSAndreas Gohr  if($direct){
3896de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3906de3759aSAndreas Gohr      $script = '_media';
3916de3759aSAndreas Gohr    }else{
3926de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3936de3759aSAndreas Gohr    }
3946de3759aSAndreas Gohr  }else{
3956de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3966de3759aSAndreas Gohr      $script = '_detail';
3976de3759aSAndreas Gohr    }else{
3986de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3996de3759aSAndreas Gohr    }
4006de3759aSAndreas Gohr  }
4016de3759aSAndreas Gohr
4026de3759aSAndreas Gohr  // build URL based on rewrite mode
4036de3759aSAndreas Gohr   if($conf['userewrite']){
4046de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
4056de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
4066de3759aSAndreas Gohr   }else{
4076de3759aSAndreas Gohr     if($more){
408a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
409b174aeaeSchris       $xlink .= $sep.'media='.$id;
4106de3759aSAndreas Gohr     }else{
411a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
4126de3759aSAndreas Gohr     }
4136de3759aSAndreas Gohr   }
4146de3759aSAndreas Gohr
4156de3759aSAndreas Gohr  return $xlink;
4166de3759aSAndreas Gohr}
4176de3759aSAndreas Gohr
4186de3759aSAndreas Gohr
4196de3759aSAndreas Gohr
4206de3759aSAndreas Gohr/**
421f3f0262cSandi * Just builds a link to a script
42215fae107Sandi *
423ed7b5f09Sandi * @todo   maybe obsolete
42415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
425f3f0262cSandi */
426f3f0262cSandifunction script($script='doku.php'){
427ed7b5f09Sandi#  $link = getBaseURL();
428ed7b5f09Sandi#  $link .= $script;
429ed7b5f09Sandi#  return $link;
430ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
431f3f0262cSandi}
432f3f0262cSandi
433f3f0262cSandi/**
43415fae107Sandi * Spamcheck against wordlist
43515fae107Sandi *
436f3f0262cSandi * Checks the wikitext against a list of blocked expressions
437f3f0262cSandi * returns true if the text contains any bad words
43815fae107Sandi *
43915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
440f3f0262cSandi */
441f3f0262cSandifunction checkwordblock(){
442f3f0262cSandi  global $TEXT;
443f3f0262cSandi  global $conf;
444f3f0262cSandi
445f3f0262cSandi  if(!$conf['usewordblock']) return false;
446f3f0262cSandi
447b9ac8716Schris  $wordblocks = getWordblocks();
4483e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
4493e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
4503e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
4513e2965d7Sandi    //backreferences are used - the maximum is 99
4523e2965d7Sandi    //this is very bad performancewise and may even be too high still
4533e2965d7Sandi    $chunksize = 40;
4543e2965d7Sandi  }else{
455703f6fdeSandi    //read file in chunks of 600 - this should work around the
4563e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
457444b87a5SAndreas Gohr    $chunksize = 400;
4583e2965d7Sandi  }
459b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
460f3f0262cSandi    $re = array();
461f3f0262cSandi    #build regexp from blocks
462f3f0262cSandi    foreach($blocks as $block){
463f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
464f3f0262cSandi      $block = trim($block);
465f3f0262cSandi      if(empty($block)) continue;
466f3f0262cSandi      $re[]  = $block;
467f3f0262cSandi    }
468b9ac8716Schris    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
469b9ac8716Schris      return true;
470b9ac8716Schris    }
471703f6fdeSandi  }
472f3f0262cSandi  return false;
473f3f0262cSandi}
474f3f0262cSandi
475f3f0262cSandi/**
47615fae107Sandi * Return the IP of the client
47715fae107Sandi *
4786d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers
47915fae107Sandi *
4806d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned
4816d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return
4826d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X
4836d8affe6SAndreas Gohr * headers
4846d8affe6SAndreas Gohr *
4856d8affe6SAndreas Gohr * @param  boolean $single If set only a single IP is returned
48615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
487f3f0262cSandi */
4886d8affe6SAndreas Gohrfunction clientIP($single=false){
4896d8affe6SAndreas Gohr  $ip = array();
4906d8affe6SAndreas Gohr  $ip[] = $_SERVER['REMOTE_ADDR'];
4916d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_FORWARDED_FOR'])
4926d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
4936d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_REAL_IP'])
4946d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
4956d8affe6SAndreas Gohr
4966d8affe6SAndreas Gohr  // remove any non-IP stuff
4976d8affe6SAndreas Gohr  $cnt = count($ip);
4984ff28443Schris  $match = array();
4996d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
5004ff28443Schris    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
5014ff28443Schris      $ip[$i] = $match[0];
5024ff28443Schris    } else {
5034ff28443Schris      $ip[$i] = '';
5044ff28443Schris    }
5056d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
506f3f0262cSandi  }
5076d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
5086d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
5096d8affe6SAndreas Gohr
5106d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
5116d8affe6SAndreas Gohr
5126d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
5136d8affe6SAndreas Gohr  $ip = array_reverse($ip);
5146d8affe6SAndreas Gohr  foreach($ip as $i){
5156d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
5166d8affe6SAndreas Gohr      continue;
5176d8affe6SAndreas Gohr    }else{
5186d8affe6SAndreas Gohr      return $i;
5196d8affe6SAndreas Gohr    }
5206d8affe6SAndreas Gohr  }
5216d8affe6SAndreas Gohr  // still here? just use the first (last) address
5226d8affe6SAndreas Gohr  return $ip[0];
523f3f0262cSandi}
524f3f0262cSandi
525f3f0262cSandi/**
52615fae107Sandi * Checks if a given page is currently locked.
52715fae107Sandi *
528f3f0262cSandi * removes stale lockfiles
52915fae107Sandi *
53015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
531f3f0262cSandi */
532f3f0262cSandifunction checklock($id){
533f3f0262cSandi  global $conf;
534c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
535f3f0262cSandi
536f3f0262cSandi  //no lockfile
537f3f0262cSandi  if(!@file_exists($lock)) return false;
538f3f0262cSandi
539f3f0262cSandi  //lockfile expired
540f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
541*d8186216SBen Coburn    @unlink($lock);
542f3f0262cSandi    return false;
543f3f0262cSandi  }
544f3f0262cSandi
545f3f0262cSandi  //my own lock
546f3f0262cSandi  $ip = io_readFile($lock);
547f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
548f3f0262cSandi    return false;
549f3f0262cSandi  }
550f3f0262cSandi
551f3f0262cSandi  return $ip;
552f3f0262cSandi}
553f3f0262cSandi
554f3f0262cSandi/**
55515fae107Sandi * Lock a page for editing
55615fae107Sandi *
55715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
558f3f0262cSandi */
559f3f0262cSandifunction lock($id){
560c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
561f3f0262cSandi  if($_SERVER['REMOTE_USER']){
562f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
563f3f0262cSandi  }else{
564f3f0262cSandi    io_saveFile($lock,clientIP());
565f3f0262cSandi  }
566f3f0262cSandi}
567f3f0262cSandi
568f3f0262cSandi/**
56915fae107Sandi * Unlock a page if it was locked by the user
570f3f0262cSandi *
57115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
57215fae107Sandi * @return bool true if a lock was removed
573f3f0262cSandi */
574f3f0262cSandifunction unlock($id){
575c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
576f3f0262cSandi  if(@file_exists($lock)){
577f3f0262cSandi    $ip = io_readFile($lock);
578f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
579f3f0262cSandi      @unlink($lock);
580f3f0262cSandi      return true;
581f3f0262cSandi    }
582f3f0262cSandi  }
583f3f0262cSandi  return false;
584f3f0262cSandi}
585f3f0262cSandi
586f3f0262cSandi/**
587f3f0262cSandi * convert line ending to unix format
588f3f0262cSandi *
58915fae107Sandi * @see    formText() for 2crlf conversion
59015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
591f3f0262cSandi */
592f3f0262cSandifunction cleanText($text){
593f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
594f3f0262cSandi  return $text;
595f3f0262cSandi}
596f3f0262cSandi
597f3f0262cSandi/**
598f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
599f3f0262cSandi * It also converts line endings to Windows format which is
600f3f0262cSandi * pseudo standard for webforms.
601f3f0262cSandi *
60215fae107Sandi * @see    cleanText() for 2unix conversion
60315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
604f3f0262cSandi */
605f3f0262cSandifunction formText($text){
606f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
607f3f0262cSandi  return htmlspecialchars($text);
608f3f0262cSandi}
609f3f0262cSandi
610f3f0262cSandi/**
61115fae107Sandi * Returns the specified local text in raw format
61215fae107Sandi *
61315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
614f3f0262cSandi */
615f3f0262cSandifunction rawLocale($id){
616f3f0262cSandi  return io_readFile(localeFN($id));
617f3f0262cSandi}
618f3f0262cSandi
619f3f0262cSandi/**
620f3f0262cSandi * Returns the raw WikiText
62115fae107Sandi *
62215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
623f3f0262cSandi */
624f3f0262cSandifunction rawWiki($id,$rev=''){
625cc7d0c94SBen Coburn  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
626f3f0262cSandi}
627f3f0262cSandi
628f3f0262cSandi/**
6297146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
6307146cee2SAndreas Gohr *
6317146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6327146cee2SAndreas Gohr */
6337146cee2SAndreas Gohrfunction pageTemplate($id){
634a15ce62dSEsther Brunner  global $conf;
635a15ce62dSEsther Brunner  global $INFO;
636a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
637a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
638a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
639a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
640a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
641a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
642a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
643a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
644a15ce62dSEsther Brunner  return $tpl;
6457146cee2SAndreas Gohr}
6467146cee2SAndreas Gohr
6477146cee2SAndreas Gohr
6487146cee2SAndreas Gohr/**
64915fae107Sandi * Returns the raw Wiki Text in three slices.
65015fae107Sandi *
65115fae107Sandi * The range parameter needs to have the form "from-to"
65215cfe303Sandi * and gives the range of the section in bytes - no
65315cfe303Sandi * UTF-8 awareness is needed.
654f3f0262cSandi * The returned order is prefix, section and suffix.
65515fae107Sandi *
65615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
657f3f0262cSandi */
658f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
659f3f0262cSandi  list($from,$to) = split('-',$range,2);
660cc7d0c94SBen Coburn  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
661f3f0262cSandi  if(!$from) $from = 0;
662c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
663f3f0262cSandi
66415cfe303Sandi  $slices[0] = substr($text,0,$from-1);
66515cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
66615cfe303Sandi  $slices[2] = substr($text,$to);
667f3f0262cSandi
668f3f0262cSandi  return $slices;
669f3f0262cSandi}
670f3f0262cSandi
671f3f0262cSandi/**
67215fae107Sandi * Joins wiki text slices
67315fae107Sandi *
674f3f0262cSandi * function to join the text slices with correct lineendings again.
675f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
676f3f0262cSandi * lines between sections if needed (used on saving).
67715fae107Sandi *
67815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
679f3f0262cSandi */
680f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
681f3f0262cSandi
682f3f0262cSandi  if($pretty){
683f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
684f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
685f3f0262cSandi  }
686f3f0262cSandi
687f3f0262cSandi  if($pre) $pre .= "\n";
688f3f0262cSandi  if($suf) $text .= "\n";
689f3f0262cSandi  return $pre.$text.$suf;
690f3f0262cSandi}
691f3f0262cSandi
692f3f0262cSandi/**
69315fae107Sandi * print debug messages
69415fae107Sandi *
695f3f0262cSandi * little function to print the content of a var
69615fae107Sandi *
69715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
698f3f0262cSandi */
699f3f0262cSandifunction dbg($msg,$hidden=false){
700f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
701f3f0262cSandi  print_r($msg);
702f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
703f3f0262cSandi}
704f3f0262cSandi
705f3f0262cSandi/**
70663cb5853SAndreas Gohr * Print info to a log file
70763cb5853SAndreas Gohr *
70863cb5853SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
70963cb5853SAndreas Gohr */
71063cb5853SAndreas Gohrfunction dbglog($msg){
71163cb5853SAndreas Gohr  global $conf;
71263cb5853SAndreas Gohr  $file = $conf['cachedir'].'/debug.log';
71363cb5853SAndreas Gohr  $fh = fopen($file,'a');
71463cb5853SAndreas Gohr  if($fh){
71563cb5853SAndreas Gohr    fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
71663cb5853SAndreas Gohr    fclose($fh);
71763cb5853SAndreas Gohr  }
71863cb5853SAndreas Gohr}
71963cb5853SAndreas Gohr
72063cb5853SAndreas Gohr/**
72171726d78SBen Coburn * Add's an entry to the changelog and saves the metadata for the page
72215fae107Sandi *
72315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
72471726d78SBen Coburn * @author Esther Brunner <wikidesign@gmail.com>
72571726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
726f3f0262cSandi */
72771726d78SBen Coburnfunction addLogEntry($date, $id, $type='E', $summary='', $extra=''){
72871726d78SBen Coburn  global $conf, $INFO;
729c1049928Sandi
73071726d78SBen Coburn  $id = cleanid($id);
73171726d78SBen Coburn  $file = wikiFN($id);
73271726d78SBen Coburn  $created = @filectime($file);
73371726d78SBen Coburn  $minor = ($type==='e');
73471726d78SBen Coburn  $wasRemoved = ($type==='D');
735c1049928Sandi
736652610a2Sandi  if(!$date) $date = time(); //use current time if none supplied
737f3f0262cSandi  $remote = $_SERVER['REMOTE_ADDR'];
738f3f0262cSandi  $user   = $_SERVER['REMOTE_USER'];
739f3f0262cSandi
74071726d78SBen Coburn  $logline = array(
74171726d78SBen Coburn    'date'  => $date,
74271726d78SBen Coburn    'ip'    => $remote,
74371726d78SBen Coburn    'type'  => $type,
74471726d78SBen Coburn    'id'    => $id,
74571726d78SBen Coburn    'user'  => $user,
74671726d78SBen Coburn    'sum'   => $summary,
74771726d78SBen Coburn    'extra' => $extra
74871726d78SBen Coburn  );
74971726d78SBen Coburn
75071726d78SBen Coburn  // update metadata
75171726d78SBen Coburn  if (!$wasRemoved) {
75271726d78SBen Coburn    $meta = array();
75371726d78SBen Coburn    if (!$INFO['exists']){ // newly created
75471726d78SBen Coburn      $meta['date']['created'] = $created;
75571726d78SBen Coburn      if ($user) $meta['creator'] = $INFO['userinfo']['name'];
75671726d78SBen Coburn    } elseif (!$minor) {   // non-minor modification
75771726d78SBen Coburn      $meta['date']['modified'] = $date;
75871726d78SBen Coburn      if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name'];
75971726d78SBen Coburn    }
76071726d78SBen Coburn    $meta['last_change'] = $logline;
76171726d78SBen Coburn    p_set_metadata($id, $meta, true);
762b6912aeaSAndreas Gohr  }
763b6912aeaSAndreas Gohr
76471726d78SBen Coburn  // add changelog lines
76571726d78SBen Coburn  $logline = implode("\t", $logline)."\n";
76671726d78SBen Coburn  io_saveFile(metaFN($id,'.changes'),$logline,true); //page changelog
76771726d78SBen Coburn  io_saveFile($conf['changelog'],$logline,true); //global changelog cache
768b6912aeaSAndreas Gohr}
769b6912aeaSAndreas Gohr
770b6912aeaSAndreas Gohr/**
771d437bcc4SAndreas Gohr * Internal function used by getRecents
772d437bcc4SAndreas Gohr *
773d437bcc4SAndreas Gohr * don't call directly
774d437bcc4SAndreas Gohr *
775d437bcc4SAndreas Gohr * @see getRecents()
776d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
77771726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
778d437bcc4SAndreas Gohr */
779b6912aeaSAndreas Gohrfunction _handleRecent($line,$ns,$flags){
780d437bcc4SAndreas Gohr  static $seen  = array();         //caches seen pages and skip them
781d437bcc4SAndreas Gohr  if(empty($line)) return false;   //skip empty lines
782d437bcc4SAndreas Gohr
783d437bcc4SAndreas Gohr  // split the line into parts
78471726d78SBen Coburn  $recent = parseChangelogLine($line);
78571726d78SBen Coburn  if ($recent===false) { return false; }
786d437bcc4SAndreas Gohr
787d437bcc4SAndreas Gohr  // skip seen ones
78871726d78SBen Coburn  if(isset($seen[$recent['id']])) return false;
789b6912aeaSAndreas Gohr
790b6912aeaSAndreas Gohr  // skip minors
79171726d78SBen Coburn  if($recent['type']==='e' && ($flags & RECENTS_SKIP_MINORS)) return false;
792d437bcc4SAndreas Gohr
793d437bcc4SAndreas Gohr  // remember in seen to skip additional sights
79471726d78SBen Coburn  $seen[$recent['id']] = 1;
795d437bcc4SAndreas Gohr
7960dc92c6fSAndreas Gohr  // check if it's a hidden page
79771726d78SBen Coburn  if(isHiddenPage($recent['id'])) return false;
7980dc92c6fSAndreas Gohr
799d437bcc4SAndreas Gohr  // filter namespace
80071726d78SBen Coburn  if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false;
801d437bcc4SAndreas Gohr
802d437bcc4SAndreas Gohr  // exclude subnamespaces
80371726d78SBen Coburn  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false;
804d437bcc4SAndreas Gohr
805ae56bfb6SAndreas Gohr  // check ACL
80671726d78SBen Coburn  if (auth_quickaclcheck($recent['id']) < AUTH_READ) return false;
807ae56bfb6SAndreas Gohr
808d437bcc4SAndreas Gohr  // check existance
80971726d78SBen Coburn  if((!@file_exists(wikiFN($recent['id']))) && ($flags & RECENTS_SKIP_DELETED)) return false;
810d437bcc4SAndreas Gohr
811d437bcc4SAndreas Gohr  return $recent;
812d437bcc4SAndreas Gohr}
813d437bcc4SAndreas Gohr
814b6912aeaSAndreas Gohr
815d437bcc4SAndreas Gohr/**
816f3f0262cSandi * returns an array of recently changed files using the
817f3f0262cSandi * changelog
818d437bcc4SAndreas Gohr *
819b6912aeaSAndreas Gohr * The following constants can be used to control which changes are
820b6912aeaSAndreas Gohr * included. Add them together as needed.
821b6912aeaSAndreas Gohr *
822b6912aeaSAndreas Gohr * RECENTS_SKIP_DELETED   - don't include deleted pages
823b6912aeaSAndreas Gohr * RECENTS_SKIP_MINORS    - don't include minor changes
824b6912aeaSAndreas Gohr * RECENTS_SKIP_SUBSPACES - don't include subspaces
825b6912aeaSAndreas Gohr *
826d437bcc4SAndreas Gohr * @param int    $first   number of first entry returned (for paginating
827d437bcc4SAndreas Gohr * @param int    $num     return $num entries
828d437bcc4SAndreas Gohr * @param string $ns      restrict to given namespace
829b6912aeaSAndreas Gohr * @param bool   $flags   see above
83015fae107Sandi *
83171726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
832f3f0262cSandi */
833b6912aeaSAndreas Gohrfunction getRecents($first,$num,$ns='',$flags=0){
834f3f0262cSandi  global $conf;
835f3f0262cSandi  $recent = array();
836d437bcc4SAndreas Gohr  $count  = 0;
8375749f1ceSmatthiasgrimm
8385749f1ceSmatthiasgrimm  if(!$num)
8395749f1ceSmatthiasgrimm    return $recent;
840f3f0262cSandi
84171726d78SBen Coburn  // read all recent changes. (kept short)
84271726d78SBen Coburn  $lines = file($conf['changelog']);
843d437bcc4SAndreas Gohr
844d437bcc4SAndreas Gohr  // handle lines
84571726d78SBen Coburn  for($i = count($lines)-1; $i >= 0; $i--){
846b6912aeaSAndreas Gohr    $rec = _handleRecent($lines[$i], $ns, $flags);
847d437bcc4SAndreas Gohr    if($rec !== false) {
848d437bcc4SAndreas Gohr      if(--$first >= 0) continue; // skip first entries
849d437bcc4SAndreas Gohr      $recent[] = $rec;
850d437bcc4SAndreas Gohr      $count++;
85171726d78SBen Coburn      // break when we have enough entries
85271726d78SBen Coburn      if($count >= $num){ break; }
85371726d78SBen Coburn    }
85471726d78SBen Coburn  }
855d437bcc4SAndreas Gohr
856f3f0262cSandi  return $recent;
857f3f0262cSandi}
858f3f0262cSandi
859f3f0262cSandi/**
86071726d78SBen Coburn * parses a changelog line into it's components
86151815db0SYann *
862fb53bfe2SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
863652610a2Sandi */
86471726d78SBen Coburnfunction parseChangelogLine($line) {
86571726d78SBen Coburn  $tmp = explode("\t", $line);
86671726d78SBen Coburn    if ($tmp!==false && count($tmp)>1) {
86771726d78SBen Coburn      $info = array();
86871726d78SBen Coburn      $info['date']  = $tmp[0]; // unix timestamp
86971726d78SBen Coburn      $info['ip']    = $tmp[1]; // IPv4 address (127.0.0.1)
87071726d78SBen Coburn      $info['type']  = $tmp[2]; // log line type
87171726d78SBen Coburn      $info['id']    = $tmp[3]; // page id
87271726d78SBen Coburn      $info['user']  = $tmp[4]; // user name
87371726d78SBen Coburn      $info['sum']   = $tmp[5]; // edit summary (or action reason)
87471726d78SBen Coburn      $info['extra'] = rtrim($tmp[6], "\n"); // extra data (varies by line type)
87571726d78SBen Coburn      return $info;
87671726d78SBen Coburn  } else { return false; }
87771726d78SBen Coburn}
87871726d78SBen Coburn
87971726d78SBen Coburn/**
88071726d78SBen Coburn * Get the changelog information for a specific page id
88171726d78SBen Coburn * and revision (timestamp). Adjacent changelog lines
88271726d78SBen Coburn * are optimistically parsed and cached to speed up
88371726d78SBen Coburn * consecutive calls to getRevisionInfo. For large
88471726d78SBen Coburn * changelog files, only the chunk containing the
88571726d78SBen Coburn * requested changelog line is read.
88671726d78SBen Coburn *
88771726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
88871726d78SBen Coburn */
88971726d78SBen Coburnfunction getRevisionInfo($id, $rev, $chunk_size=8192) {
89071726d78SBen Coburn  global $cache_revinfo;
89171726d78SBen Coburn  $cache =& $cache_revinfo;
89271726d78SBen Coburn  if (!isset($cache[$id])) { $cache[$id] = array(); }
89371726d78SBen Coburn  $rev = max($rev, 0);
894258641c6Sandi
895fb53bfe2SBen Coburn  // check if it's already in the memory cache
89671726d78SBen Coburn  if (isset($cache[$id]) && isset($cache[$id][$rev])) {
897fb53bfe2SBen Coburn    return $cache[$id][$rev];
898fb53bfe2SBen Coburn  }
899fb53bfe2SBen Coburn
90071726d78SBen Coburn  $file = metaFN($id, '.changes');
901*d8186216SBen Coburn  if (!@file_exists($file)) { return false; }
90271726d78SBen Coburn  if (filesize($file)<$chunk_size || $chunk_size==0) {
90371726d78SBen Coburn    // read whole file
90471726d78SBen Coburn    $lines = file($file);
90571726d78SBen Coburn    if ($lines===false) { return false; }
906fb53bfe2SBen Coburn  } else {
90771726d78SBen Coburn    // read by chunk
90871726d78SBen Coburn    $fp = fopen($file, 'rb'); // "file pointer"
90971726d78SBen Coburn    if ($fp===false) { return false; }
91071726d78SBen Coburn    $head = 0;
91171726d78SBen Coburn    fseek($fp, 0, SEEK_END);
91271726d78SBen Coburn    $tail = ftell($fp);
91371726d78SBen Coburn    $finger = 0;
91471726d78SBen Coburn    $finger_rev = 0;
91571726d78SBen Coburn
91671726d78SBen Coburn    // find chunk
91771726d78SBen Coburn    while ($tail-$head>$chunk_size) {
91871726d78SBen Coburn      $finger = $head+floor(($tail-$head)/2.0);
91971726d78SBen Coburn      fseek($fp, $finger);
92071726d78SBen Coburn      fgets($fp); // slip the finger forward to a new line
92171726d78SBen Coburn      $finger = ftell($fp);
92271726d78SBen Coburn      $tmp = fgets($fp); // then read at that location
92371726d78SBen Coburn      $tmp = parseChangelogLine($tmp);
92471726d78SBen Coburn      $finger_rev = $tmp['date'];
92571726d78SBen Coburn      if ($finger==$head || $finger==$tail) { break; }
92671726d78SBen Coburn      if ($finger_rev>$rev) {
92771726d78SBen Coburn        $tail = $finger;
92871726d78SBen Coburn      } else {
92971726d78SBen Coburn        $head = $finger;
93071726d78SBen Coburn      }
93171726d78SBen Coburn    }
93271726d78SBen Coburn
93371726d78SBen Coburn    if ($tail-$head<1) {
93471726d78SBen Coburn      // cound not find chunk, assume requested rev is missing
93571726d78SBen Coburn      fclose($fp);
93671726d78SBen Coburn      return false;
93771726d78SBen Coburn    }
93871726d78SBen Coburn
93971726d78SBen Coburn    // read chunk
94071726d78SBen Coburn    $chunk = '';
94171726d78SBen Coburn    $chunk_size = max($tail-$head, 0); // found chunk size
94271726d78SBen Coburn    $got = 0;
94371726d78SBen Coburn    fseek($fp, $head);
94471726d78SBen Coburn    while ($got<$chunk_size && !feof($fp)) {
94571726d78SBen Coburn      $tmp = fread($fp, max($chunk_size-$got, 0));
94671726d78SBen Coburn      if ($tmp===false) { break; } //error state
94771726d78SBen Coburn      $got += strlen($tmp);
94871726d78SBen Coburn      $chunk .= $tmp;
94971726d78SBen Coburn    }
95071726d78SBen Coburn    $lines = explode("\n", $chunk);
95171726d78SBen Coburn    array_pop($lines); // remove trailing newline
95271726d78SBen Coburn    fclose($fp);
95371726d78SBen Coburn  }
95471726d78SBen Coburn
95571726d78SBen Coburn  // parse and cache changelog lines
95671726d78SBen Coburn  foreach ($lines as $value) {
95771726d78SBen Coburn    $tmp = parseChangelogLine($value);
95871726d78SBen Coburn    if ($tmp!==false) {
95971726d78SBen Coburn      $cache[$id][$tmp['date']] = $tmp;
96071726d78SBen Coburn    }
96171726d78SBen Coburn  }
96271726d78SBen Coburn  if (!isset($cache[$id][$rev])) { return false; }
96371726d78SBen Coburn  return $cache[$id][$rev];
96471726d78SBen Coburn}
96571726d78SBen Coburn
96671726d78SBen Coburn/**
96771726d78SBen Coburn * Return a list of page revisions numbers
96871726d78SBen Coburn * Does not guarantee that the revision exists in the attic,
96971726d78SBen Coburn * only that a line with the date exists in the changelog.
97071726d78SBen Coburn * By default the current revision is skipped.
97171726d78SBen Coburn *
97271726d78SBen Coburn * id:    the page of interest
97371726d78SBen Coburn * first: skip the first n changelog lines
97471726d78SBen Coburn * num:   number of revisions to return
97571726d78SBen Coburn *
97671726d78SBen Coburn * The current revision is automatically skipped when the page exists.
97771726d78SBen Coburn * See $INFO['meta']['last_change'] for the current revision.
97871726d78SBen Coburn *
97971726d78SBen Coburn * For efficiency, the log lines are parsed and cached for later
98071726d78SBen Coburn * calls to getRevisionInfo. Large changelog files are read
98171726d78SBen Coburn * backwards in chunks untill the requested number of changelog
98271726d78SBen Coburn * lines are recieved.
98371726d78SBen Coburn *
98471726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
98571726d78SBen Coburn */
98671726d78SBen Coburnfunction getRevisions($id, $first, $num, $chunk_size=8192) {
98771726d78SBen Coburn  global $cache_revinfo;
98871726d78SBen Coburn  $cache =& $cache_revinfo;
989fb53bfe2SBen Coburn  if (!isset($cache[$id])) { $cache[$id] = array(); }
99071726d78SBen Coburn
99171726d78SBen Coburn  $revs = array();
99271726d78SBen Coburn  $lines = array();
99371726d78SBen Coburn  $count  = 0;
99471726d78SBen Coburn  $file = metaFN($id, '.changes');
99571726d78SBen Coburn  $num = max($num, 0);
99671726d78SBen Coburn  $chunk_size = max($chunk_size, 0);
99771726d78SBen Coburn  if ($first<0) { $first = 0; }
998*d8186216SBen Coburn  else if (@file_exists(wikiFN($id))) {
99971726d78SBen Coburn     // skip current revision if the page exists
100071726d78SBen Coburn    $first = max($first+1, 0);
1001fb53bfe2SBen Coburn  }
1002fb53bfe2SBen Coburn
1003*d8186216SBen Coburn  if (!@file_exists($file)) { return $revs; }
100471726d78SBen Coburn  if (filesize($file)<$chunk_size || $chunk_size==0) {
100571726d78SBen Coburn    // read whole file
100671726d78SBen Coburn    $lines = file($file);
100771726d78SBen Coburn    if ($lines===false) { return $revs; }
100871726d78SBen Coburn  } else {
100971726d78SBen Coburn    // read chunks backwards
101071726d78SBen Coburn    $fp = fopen($file, 'rb'); // "file pointer"
101171726d78SBen Coburn    if ($fp===false) { return $revs; }
101271726d78SBen Coburn    fseek($fp, 0, SEEK_END);
101371726d78SBen Coburn    $tail = ftell($fp);
101471726d78SBen Coburn
101571726d78SBen Coburn    // chunk backwards
101671726d78SBen Coburn    $finger = max($tail-$chunk_size, 0);
101771726d78SBen Coburn    while ($count<$num+$first) {
101871726d78SBen Coburn      fseek($fp, $finger);
101971726d78SBen Coburn      if ($finger>0) {
102071726d78SBen Coburn        fgets($fp); // slip the finger forward to a new line
102171726d78SBen Coburn        $finger = ftell($fp);
1022652610a2Sandi      }
1023652610a2Sandi
102471726d78SBen Coburn      // read chunk
102571726d78SBen Coburn      if ($tail<=$finger) { break; }
102671726d78SBen Coburn      $chunk = '';
102771726d78SBen Coburn      $read_size = max($tail-$finger, 0); // found chunk size
102871726d78SBen Coburn      $got = 0;
102971726d78SBen Coburn      while ($got<$read_size && !feof($fp)) {
103071726d78SBen Coburn        $tmp = fread($fp, max($read_size-$got, 0));
103171726d78SBen Coburn        if ($tmp===false) { break; } //error state
103271726d78SBen Coburn        $got += strlen($tmp);
103371726d78SBen Coburn        $chunk .= $tmp;
103471726d78SBen Coburn      }
103571726d78SBen Coburn      $tmp = explode("\n", $chunk);
103671726d78SBen Coburn      array_pop($tmp); // remove trailing newline
103771726d78SBen Coburn
103871726d78SBen Coburn      // combine with previous chunk
103971726d78SBen Coburn      $count += count($tmp);
104071726d78SBen Coburn      $lines = array_merge($tmp, $lines);
104171726d78SBen Coburn
104271726d78SBen Coburn      // next chunk
104371726d78SBen Coburn      if ($finger==0) { break; } // already read all the lines
104471726d78SBen Coburn      else {
104571726d78SBen Coburn        $tail = $finger;
104671726d78SBen Coburn        $finger = max($tail-$chunk_size, 0);
104771726d78SBen Coburn      }
104871726d78SBen Coburn    }
104971726d78SBen Coburn    fclose($fp);
105071726d78SBen Coburn  }
105171726d78SBen Coburn
105271726d78SBen Coburn  // skip parsing extra lines
105371726d78SBen Coburn  $num = max(min(count($lines)-$first, $num), 0);
105471726d78SBen Coburn  if      ($first>0 && $num>0)  { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); }
105571726d78SBen Coburn  else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); }
105671726d78SBen Coburn  else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); }
105771726d78SBen Coburn
105871726d78SBen Coburn  // handle lines in reverse order
105971726d78SBen Coburn  for ($i = count($lines)-1; $i >= 0; $i--) {
106071726d78SBen Coburn    $tmp = parseChangelogLine($lines[$i]);
106171726d78SBen Coburn    if ($tmp!==false) {
106271726d78SBen Coburn      $cache[$id][$tmp['date']] = $tmp;
106371726d78SBen Coburn      $revs[] = $tmp['date'];
106471726d78SBen Coburn    }
106571726d78SBen Coburn  }
106671726d78SBen Coburn
106771726d78SBen Coburn  return $revs;
106871726d78SBen Coburn}
106951815db0SYann
1070652610a2Sandi/**
1071cc7d0c94SBen Coburn * Saves a wikitext by calling io_writeWikiPage
107215fae107Sandi *
107315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
107471726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
1075f3f0262cSandi */
1076b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
1077f3f0262cSandi  global $conf;
1078f3f0262cSandi  global $lang;
107971726d78SBen Coburn  global $REV;
1080f3f0262cSandi  // ignore if no changes were made
1081f3f0262cSandi  if($text == rawWiki($id,'')){
1082f3f0262cSandi    return;
1083f3f0262cSandi  }
1084f3f0262cSandi
1085f3f0262cSandi  $file = wikiFN($id);
1086f3f0262cSandi  $old  = saveOldRevision($id);
108771726d78SBen Coburn  $wasRemoved = empty($text);
1088*d8186216SBen Coburn  $wasCreated = !@file_exists($file);
108971726d78SBen Coburn  $wasReverted = ($REV==true);
1090f3f0262cSandi
109171726d78SBen Coburn  if ($wasRemoved){
1092e1f3d9e1SEsther Brunner    // remove empty file
1093f3f0262cSandi    @unlink($file);
109471726d78SBen Coburn    // remove old meta info...
1095e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
109671726d78SBen Coburn    $changelog = metaFN($id, '.changes');
1097e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
109871726d78SBen Coburn      // but keep per-page changelog to preserve page history
1099*d8186216SBen Coburn      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
1100b158d625SSteven Danz    }
1101f3f0262cSandi    $del = true;
11023ce054b3Sandi    // autoset summary on deletion
11033ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
110453d6ccfeSandi    // remove empty namespaces
1105cc7d0c94SBen Coburn    io_sweepNS($id, 'datadir');
1106cc7d0c94SBen Coburn    io_sweepNS($id, 'mediadir');
1107f3f0262cSandi  }else{
1108cc7d0c94SBen Coburn    // save file (namespace dir is created in io_writeWikiPage)
1109cc7d0c94SBen Coburn    io_writeWikiPage($file, $text, $id);
1110f3f0262cSandi    $del = false;
1111f3f0262cSandi  }
1112f3f0262cSandi
111371726d78SBen Coburn  // select changelog line type
111471726d78SBen Coburn  $extra = '';
111571726d78SBen Coburn  $type = 'E';
111671726d78SBen Coburn  if ($wasReverted) {
111771726d78SBen Coburn    $type = 'R';
111871726d78SBen Coburn    $extra = $REV;
111971726d78SBen Coburn  }
112071726d78SBen Coburn  else if ($wasCreated) { $type = 'C'; }
112171726d78SBen Coburn  else if ($wasRemoved) { $type = 'D'; }
112271726d78SBen Coburn  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
112371726d78SBen Coburn
112471726d78SBen Coburn  addLogEntry(@filemtime($file), $id, $type, $summary, $extra);
112526a0801fSAndreas Gohr  // send notify mails
112690033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
112790033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
1128f3f0262cSandi
1129f3f0262cSandi  //purge cache on add by updating the purgefile
1130f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
113198407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
1132f3f0262cSandi  }
1133f3f0262cSandi}
1134f3f0262cSandi
1135f3f0262cSandi/**
1136f3f0262cSandi * moves the current version to the attic and returns its
1137f3f0262cSandi * revision date
113815fae107Sandi *
113915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1140f3f0262cSandi */
1141f3f0262cSandifunction saveOldRevision($id){
1142f3f0262cSandi  global $conf;
1143f3f0262cSandi  $oldf = wikiFN($id);
1144f3f0262cSandi  if(!@file_exists($oldf)) return '';
1145f3f0262cSandi  $date = filemtime($oldf);
1146f3f0262cSandi  $newf = wikiFN($id,$date);
1147cc7d0c94SBen Coburn  io_writeWikiPage($newf, rawWiki($id), $id, $date);
1148f3f0262cSandi  return $date;
1149f3f0262cSandi}
1150f3f0262cSandi
1151f3f0262cSandi/**
115226a0801fSAndreas Gohr * Sends a notify mail on page change
115326a0801fSAndreas Gohr *
115426a0801fSAndreas Gohr * @param  string  $id       The changed page
115526a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
115626a0801fSAndreas Gohr * @param  int     $rev      Old page revision
115726a0801fSAndreas Gohr * @param  string  $summary  What changed
115890033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
115902a498e7Schris * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
116015fae107Sandi *
116115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1162f3f0262cSandi */
116302a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
1164f3f0262cSandi  global $lang;
1165f3f0262cSandi  global $conf;
1166b158d625SSteven Danz
116726a0801fSAndreas Gohr  // decide if there is something to do
116826a0801fSAndreas Gohr  if($who == 'admin'){
116926a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
1170f3f0262cSandi    $text = rawLocale('mailtext');
117126a0801fSAndreas Gohr    $to   = $conf['notify'];
117226a0801fSAndreas Gohr    $bcc  = '';
117326a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
117426a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
117590033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
117626a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
117726a0801fSAndreas Gohr    if(empty($bcc)) return;
117826a0801fSAndreas Gohr    $to   = '';
117926a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
1180a06e4bdbSSebastian Harl  }elseif($who == 'register'){
1181a06e4bdbSSebastian Harl    if(empty($conf['registernotify'])) return;
1182a06e4bdbSSebastian Harl    $text = rawLocale('registermail');
1183a06e4bdbSSebastian Harl    $to   = $conf['registernotify'];
1184a06e4bdbSSebastian Harl    $bcc  = '';
118526a0801fSAndreas Gohr  }else{
118626a0801fSAndreas Gohr    return; //just to be safe
118726a0801fSAndreas Gohr  }
118826a0801fSAndreas Gohr
1189f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
1190f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
1191f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
1192f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
1193ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
119426a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
119526a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
1196ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
1197f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
11987a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
1199f3f0262cSandi
120002a498e7Schris  foreach ($replace as $key => $substitution) {
120102a498e7Schris    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
120202a498e7Schris  }
120302a498e7Schris
1204a06e4bdbSSebastian Harl  if($who == 'register'){
1205a06e4bdbSSebastian Harl    $subject = $lang['mail_new_user'].' '.$summary;
1206a06e4bdbSSebastian Harl  }elseif($rev){
1207f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
1208ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
1209ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
1210f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
1211f3f0262cSandi                    split("\n",rawWiki($id)));
1212f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
1213f3f0262cSandi    $diff    = $dformat->format($df);
1214f3f0262cSandi  }else{
1215f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
1216f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
1217f3f0262cSandi    $diff = rawWiki($id);
1218f3f0262cSandi  }
1219f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
1220241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
1221f3f0262cSandi
122226a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
1223f3f0262cSandi}
1224f3f0262cSandi
122515fae107Sandi/**
1226f3f0262cSandi * extracts the query from a google referer
122715fae107Sandi *
12286b13307fSandi * @todo   should be more generic and support yahoo et al
122915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1230f3f0262cSandi */
1231f3f0262cSandifunction getGoogleQuery(){
1232f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
12335c3f206fSandi  if(!$url) return '';
1234f3f0262cSandi
1235f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
1236f3f0262cSandi  $query = array();
1237f3f0262cSandi  parse_str($url['query'],$query);
1238f3f0262cSandi
1239f3f0262cSandi  return $query['q'];
1240f3f0262cSandi}
1241f3f0262cSandi
1242f3f0262cSandi/**
124315fae107Sandi * Try to set correct locale
124415fae107Sandi *
1245095bfd5cSandi * @deprecated No longer used
124615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
1247f3f0262cSandi */
1248f3f0262cSandifunction setCorrectLocale(){
1249f3f0262cSandi  global $conf;
1250f3f0262cSandi  global $lang;
1251f3f0262cSandi
1252f3f0262cSandi  $enc = strtoupper($lang['encoding']);
1253f3f0262cSandi  foreach ($lang['locales'] as $loc){
1254f3f0262cSandi    //try locale
1255f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
1256f3f0262cSandi    //try loceale with encoding
1257f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
1258f3f0262cSandi  }
1259f3f0262cSandi  //still here? try to set from environment
1260f3f0262cSandi  @setlocale(LC_ALL,"");
1261f3f0262cSandi}
1262f3f0262cSandi
1263f3f0262cSandi/**
1264f3f0262cSandi * Return the human readable size of a file
1265f3f0262cSandi *
1266f3f0262cSandi * @param       int    $size   A file size
1267f3f0262cSandi * @param       int    $dec    A number of decimal places
1268f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
1269f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
1270f3f0262cSandi * @version     1.0.0
1271f3f0262cSandi */
1272f31d5b73Sandifunction filesize_h($size, $dec = 1){
1273f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
1274f3f0262cSandi  $count = count($sizes);
1275f3f0262cSandi  $i = 0;
1276f3f0262cSandi
1277f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
1278f3f0262cSandi    $size /= 1024;
1279f3f0262cSandi    $i++;
1280f3f0262cSandi  }
1281f3f0262cSandi
1282f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
1283f3f0262cSandi}
1284f3f0262cSandi
128515fae107Sandi/**
128600a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
128700a7b5adSEsther Brunner *
128800a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
128900a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
129000a7b5adSEsther Brunner */
129100a7b5adSEsther Brunnerfunction obfuscate($email) {
129200a7b5adSEsther Brunner  global $conf;
129300a7b5adSEsther Brunner
129400a7b5adSEsther Brunner  switch ($conf['mailguard']) {
129500a7b5adSEsther Brunner    case 'visible' :
129600a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
129700a7b5adSEsther Brunner      return strtr($email, $obfuscate);
129800a7b5adSEsther Brunner
129900a7b5adSEsther Brunner    case 'hex' :
130000a7b5adSEsther Brunner      $encode = '';
130100a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
130200a7b5adSEsther Brunner      return $encode;
130300a7b5adSEsther Brunner
130400a7b5adSEsther Brunner    case 'none' :
130500a7b5adSEsther Brunner    default :
130600a7b5adSEsther Brunner      return $email;
130700a7b5adSEsther Brunner  }
130800a7b5adSEsther Brunner}
130900a7b5adSEsther Brunner
131000a7b5adSEsther Brunner/**
1311dc57ef04Sandi * Return DokuWikis version
131215fae107Sandi *
131315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
131415fae107Sandi */
1315f31d5b73Sandifunction getVersion(){
1316f31d5b73Sandi  //import version string
1317f31d5b73Sandi  if(@file_exists('VERSION')){
1318f31d5b73Sandi    //official release
13190647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1320f31d5b73Sandi  }elseif(is_dir('_darcs')){
1321f31d5b73Sandi    //darcs checkout
1322f31d5b73Sandi    $inv = file('_darcs/inventory');
1323ae41559bSAndreas Gohr    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
1324f31d5b73Sandi    $cur = array_pop($inv);
1325f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1326f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1327f31d5b73Sandi  }else{
1328f31d5b73Sandi    return 'snapshot?';
1329f31d5b73Sandi  }
1330f31d5b73Sandi}
1331f31d5b73Sandi
1332f31d5b73Sandi/**
1333f31d5b73Sandi * Run a few sanity checks
1334f31d5b73Sandi *
1335f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
1336f31d5b73Sandi */
1337f3f0262cSandifunction check(){
1338f3f0262cSandi  global $conf;
1339f3f0262cSandi  global $INFO;
1340f3f0262cSandi
1341f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
1342f31d5b73Sandi
134349022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
134449022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
134549022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
134649022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
134749022a38Sandi  }else{
134849022a38Sandi    msg('PHP version '.phpversion(),1);
134949022a38Sandi  }
135049022a38Sandi
1351f3f0262cSandi  if(is_writable($conf['changelog'])){
1352f3f0262cSandi    msg('Changelog is writable',1);
1353f3f0262cSandi  }else{
1354*d8186216SBen Coburn    if (@file_exists($conf['changelog'])) {
1355f3f0262cSandi      msg('Changelog is not writable',-1);
1356f3f0262cSandi    }
135771726d78SBen Coburn  }
135871726d78SBen Coburn
1359*d8186216SBen Coburn  if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
136071726d78SBen Coburn    msg('Old changelog exists.', 0);
136171726d78SBen Coburn  }
136271726d78SBen Coburn
1363*d8186216SBen Coburn  if (@file_exists($conf['changelog'].'_failed')) {
136471726d78SBen Coburn    msg('Importing old changelog failed.', -1);
1365*d8186216SBen Coburn  } else if (@file_exists($conf['changelog'].'_importing')) {
136671726d78SBen Coburn    msg('Importing old changelog now.', 0);
1367*d8186216SBen Coburn  } else if (@file_exists($conf['changelog'].'_import_ok')) {
136871726d78SBen Coburn    msg('Old changelog imported.', 1);
136971726d78SBen Coburn  }
1370f3f0262cSandi
1371f3f0262cSandi  if(is_writable($conf['datadir'])){
1372f3f0262cSandi    msg('Datadir is writable',1);
1373f3f0262cSandi  }else{
1374f3f0262cSandi    msg('Datadir is not writable',-1);
1375f3f0262cSandi  }
1376f3f0262cSandi
1377f3f0262cSandi  if(is_writable($conf['olddir'])){
1378f3f0262cSandi    msg('Attic is writable',1);
1379f3f0262cSandi  }else{
1380f3f0262cSandi    msg('Attic is not writable',-1);
1381f3f0262cSandi  }
1382f3f0262cSandi
1383f3f0262cSandi  if(is_writable($conf['mediadir'])){
1384f3f0262cSandi    msg('Mediadir is writable',1);
1385f3f0262cSandi  }else{
1386f3f0262cSandi    msg('Mediadir is not writable',-1);
1387f3f0262cSandi  }
1388f3f0262cSandi
138998407a7aSandi  if(is_writable($conf['cachedir'])){
139098407a7aSandi    msg('Cachedir is writable',1);
139198407a7aSandi  }else{
139298407a7aSandi    msg('Cachedir is not writable',-1);
139398407a7aSandi  }
139498407a7aSandi
13957de6c234SAndreas Gohr  if(is_writable($conf['lockdir'])){
13967de6c234SAndreas Gohr    msg('Lockdir is writable',1);
13977de6c234SAndreas Gohr  }else{
13987de6c234SAndreas Gohr    msg('Lockdir is not writable',-1);
13997de6c234SAndreas Gohr  }
14007de6c234SAndreas Gohr
1401e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
14028c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1403f3f0262cSandi  }else{
14048c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1405f3f0262cSandi  }
140693a9e835Sandi
140793a9e835Sandi  if(function_exists('mb_strpos')){
140893a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
140993a9e835Sandi      msg('mb_string extension is available but will not be used',0);
141093a9e835Sandi    }else{
141193a9e835Sandi      msg('mb_string extension is available and will be used',1);
141293a9e835Sandi    }
141393a9e835Sandi  }else{
141493a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
141593a9e835Sandi  }
1416f42d1c75SAndreas Gohr
1417f42d1c75SAndreas Gohr  if($conf['allowdebug']){
1418f42d1c75SAndreas Gohr    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1419f42d1c75SAndreas Gohr  }else{
1420f42d1c75SAndreas Gohr    msg('Debugging support is disabled',1);
1421f42d1c75SAndreas Gohr  }
1422f3f0262cSandi
1423f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1424f3f0262cSandi
1425f3f0262cSandi  if(is_writable($INFO['filepath'])){
1426f3f0262cSandi    msg('The current page is writable by the webserver',0);
1427f3f0262cSandi  }else{
1428f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1429f3f0262cSandi  }
1430f3f0262cSandi
1431f3f0262cSandi  if($INFO['writable']){
1432f3f0262cSandi    msg('The current page is writable by you',0);
1433f3f0262cSandi  }else{
1434f3f0262cSandi    msg('The current page is not writable you',0);
1435f3f0262cSandi  }
1436f3f0262cSandi}
1437340756e4Sandi
1438b158d625SSteven Danz/**
1439b158d625SSteven Danz * Let us know if a user is tracking a page
1440b158d625SSteven Danz *
14411380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1442b158d625SSteven Danz */
14431380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
14441380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
14451380fc45SAndreas Gohr  if (@file_exists($file)) {
1446b158d625SSteven Danz    $mlist = file($file);
14471380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
14481380fc45SAndreas Gohr    return is_int($pos);
1449b158d625SSteven Danz  }
14501380fc45SAndreas Gohr
1451b158d625SSteven Danz  return false;
1452b158d625SSteven Danz}
1453340756e4Sandi
1454f9eb5648Ssteven-danz/**
1455f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1456f9eb5648Ssteven-danz * users subscribed to a page
1457f9eb5648Ssteven-danz *
145826a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1459f9eb5648Ssteven-danz */
1460f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1461f9eb5648Ssteven-danz  global $conf;
1462cd52f92dSchris  global $auth;
1463f9eb5648Ssteven-danz
1464f9eb5648Ssteven-danz  $emails = '';
1465f9eb5648Ssteven-danz
146626a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
146726a0801fSAndreas Gohr
1468f9eb5648Ssteven-danz  $mlist = array();
1469f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1470*d8186216SBen Coburn  if (@file_exists($file)) {
1471f9eb5648Ssteven-danz    $mlist = file($file);
1472f9eb5648Ssteven-danz  }
1473f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1474f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1475f9eb5648Ssteven-danz      $who = rtrim($who);
1476cd52f92dSchris      $info = $auth->getUserData($who);
1477f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1478f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1479f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1480f9eb5648Ssteven-danz          if (empty($emails)) {
1481f9eb5648Ssteven-danz            $emails = $info['mail'];
1482f9eb5648Ssteven-danz          } else {
1483f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1484f9eb5648Ssteven-danz          }
1485f9eb5648Ssteven-danz        }
1486f9eb5648Ssteven-danz      }
1487f9eb5648Ssteven-danz    }
1488f9eb5648Ssteven-danz  }
1489f9eb5648Ssteven-danz
1490f9eb5648Ssteven-danz  return $emails;
1491f9eb5648Ssteven-danz}
1492f9eb5648Ssteven-danz
149389541d4bSAndreas Gohr/**
149489541d4bSAndreas Gohr * Removes quoting backslashes
149589541d4bSAndreas Gohr *
149689541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
149789541d4bSAndreas Gohr */
149889541d4bSAndreas Gohrfunction unslash($string,$char="'"){
149989541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
150089541d4bSAndreas Gohr}
150189541d4bSAndreas Gohr
1502340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1503