xref: /dokuwiki/inc/common.php (revision 30d7d7185531fa4b68ece7f3b11ca855e1fd5b5a)
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
9ed7b5f09Sandiif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10e7cb32dcSAndreas Gohrrequire_once(DOKU_CONF.'dokuwiki.php');
11ed7b5f09Sandirequire_once(DOKU_INC.'inc/io.php');
127d559c7fSBen Coburnrequire_once(DOKU_INC.'inc/changelog.php');
13ed7b5f09Sandirequire_once(DOKU_INC.'inc/utf8.php');
14ed7b5f09Sandirequire_once(DOKU_INC.'inc/mail.php');
15c112d578Sandirequire_once(DOKU_INC.'inc/parserutils.php');
16c29dc6e4SAndreas Gohrrequire_once(DOKU_INC.'inc/infoutils.php');
17f3f0262cSandi
18f3f0262cSandi/**
19b6912aeaSAndreas Gohr * These constants are used with the recents function
20b6912aeaSAndreas Gohr */
21b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED',2);
22b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS',4);
23b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES',8);
24b6912aeaSAndreas Gohr
25b6912aeaSAndreas Gohr/**
26d5197206Schris * Wrapper around htmlspecialchars()
27d5197206Schris *
28d5197206Schris * @author Andreas Gohr <andi@splitbrain.org>
29d5197206Schris * @see    htmlspecialchars()
30d5197206Schris */
31d5197206Schrisfunction hsc($string){
32d5197206Schris  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
33d5197206Schris}
34d5197206Schris
35d5197206Schris/**
36d5197206Schris * print a newline terminated string
37d5197206Schris *
38d5197206Schris * You can give an indention as optional parameter
39d5197206Schris *
40d5197206Schris * @author Andreas Gohr <andi@splitbrain.org>
41d5197206Schris */
42d5197206Schrisfunction ptln($string,$intend=0){
43d5197206Schris  for($i=0; $i<$intend; $i++) print ' ';
4402b0b681SAndreas Gohr  echo "$string\n";
4502b0b681SAndreas Gohr}
4602b0b681SAndreas Gohr
4702b0b681SAndreas Gohr/**
4802b0b681SAndreas Gohr * strips control characters (<32) from the given string
4902b0b681SAndreas Gohr *
5002b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5102b0b681SAndreas Gohr */
5202b0b681SAndreas Gohrfunction stripctl($string){
5302b0b681SAndreas Gohr  return preg_replace('/[\x00-\x1F]+/s','',$string);
54d5197206Schris}
55d5197206Schris
56d5197206Schris/**
5715fae107Sandi * Return info about the current document as associative
58f3f0262cSandi * array.
5915fae107Sandi *
6015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
61f3f0262cSandi */
62f3f0262cSandifunction pageinfo(){
63f3f0262cSandi  global $ID;
64f3f0262cSandi  global $REV;
65f3f0262cSandi  global $USERINFO;
66f3f0262cSandi  global $conf;
67f3f0262cSandi
686afe8dcaSchris  // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml
696afe8dcaSchris  // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary
706afe8dcaSchris  $info['id'] = $ID;
716afe8dcaSchris  $info['rev'] = $REV;
726afe8dcaSchris
73f3f0262cSandi  if($_SERVER['REMOTE_USER']){
74f3f0262cSandi    $info['userinfo']   = $USERINFO;
75f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
761380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
77ee4c4a1bSAndreas Gohr    $info['client']     = $_SERVER['REMOTE_USER'];
7817ee7f66SAndreas Gohr
79f8cc712eSAndreas Gohr    // set info about manager/admin status
80f8cc712eSAndreas Gohr    $info['isadmin']   = false;
81f8cc712eSAndreas Gohr    $info['ismanager'] = false;
82f8cc712eSAndreas Gohr    if($info['perm'] == AUTH_ADMIN){
83f8cc712eSAndreas Gohr      $info['isadmin']   = true;
84f8cc712eSAndreas Gohr      $info['ismanager'] = true;
85f8cc712eSAndreas Gohr    }elseif(auth_ismanager()){
86f8cc712eSAndreas Gohr      $info['ismanager'] = true;
87f8cc712eSAndreas Gohr    }
88f8cc712eSAndreas Gohr
8917ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
9017ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
9117ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
9217ee7f66SAndreas Gohr    }
93ee4c4a1bSAndreas Gohr
94f3f0262cSandi  }else{
95f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
961380fc45SAndreas Gohr    $info['subscribed'] = false;
97ee4c4a1bSAndreas Gohr    $info['client']     = clientIP(true);
98f3f0262cSandi  }
99f3f0262cSandi
100f3f0262cSandi  $info['namespace'] = getNS($ID);
101f3f0262cSandi  $info['locked']    = checklock($ID);
1022ca9d91cSBen Coburn  $info['filepath']  = realpath(wikiFN($ID));
1032ca9d91cSBen Coburn  $info['exists']    = @file_exists($info['filepath']);
1042ca9d91cSBen Coburn  if($REV){
1052ca9d91cSBen Coburn    //check if current revision was meant
1062ca9d91cSBen Coburn    if($info['exists'] && (@filemtime($info['filepath'])==$REV)){
1072ca9d91cSBen Coburn      $REV = '';
1082ca9d91cSBen Coburn    }else{
1092ca9d91cSBen Coburn      //really use old revision
110f3f0262cSandi      $info['filepath'] = realpath(wikiFN($ID,$REV));
111f3f0262cSandi      $info['exists']   = @file_exists($info['filepath']);
112f3f0262cSandi    }
113f3f0262cSandi  }
114c112d578Sandi  $info['rev'] = $REV;
115f3f0262cSandi  if($info['exists']){
116f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
117f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
118f3f0262cSandi  }else{
119f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
120f3f0262cSandi  }
121f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
122f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
123f3f0262cSandi
12471726d78SBen Coburn  //load page meta data
12571726d78SBen Coburn  $info['meta'] = p_get_metadata($ID);
12671726d78SBen Coburn
127652610a2Sandi  //who's the editor
128652610a2Sandi  if($REV){
12971726d78SBen Coburn    $revinfo = getRevisionInfo($ID, $REV, 1024);
130652610a2Sandi  }else{
131bb4866bdSchris    $revinfo = isset($info['meta']['last_change']) ? $info['meta']['last_change'] : getRevisionInfo($ID,$info['lastmod'],1024);
132652610a2Sandi  }
133bb4866bdSchris
134652610a2Sandi  $info['ip']     = $revinfo['ip'];
135652610a2Sandi  $info['user']   = $revinfo['user'];
136652610a2Sandi  $info['sum']    = $revinfo['sum'];
13771726d78SBen Coburn  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
13871726d78SBen Coburn  // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor'].
13959f257aeSchris
14088f522e9Sandi  if($revinfo['user']){
14188f522e9Sandi    $info['editor'] = $revinfo['user'];
14288f522e9Sandi  }else{
14388f522e9Sandi    $info['editor'] = $revinfo['ip'];
14488f522e9Sandi  }
145652610a2Sandi
146ee4c4a1bSAndreas Gohr  // draft
147ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
148ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
149ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
150ee4c4a1bSAndreas Gohr      // remove stale draft
151ee4c4a1bSAndreas Gohr      @unlink($draft);
152ee4c4a1bSAndreas Gohr    }else{
153ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
154ee4c4a1bSAndreas Gohr    }
155ee4c4a1bSAndreas Gohr  }
156ee4c4a1bSAndreas Gohr
157f3f0262cSandi  return $info;
158f3f0262cSandi}
159f3f0262cSandi
160f3f0262cSandi/**
1612684e50aSAndreas Gohr * Build an string of URL parameters
1622684e50aSAndreas Gohr *
1632684e50aSAndreas Gohr * @author Andreas Gohr
1642684e50aSAndreas Gohr */
165b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
1662684e50aSAndreas Gohr  $url = '';
1672684e50aSAndreas Gohr  $amp = false;
1682684e50aSAndreas Gohr  foreach($params as $key => $val){
169b174aeaeSchris    if($amp) $url .= $sep;
1702684e50aSAndreas Gohr
1712684e50aSAndreas Gohr    $url .= $key.'=';
172b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1732684e50aSAndreas Gohr    $amp = true;
1742684e50aSAndreas Gohr  }
1752684e50aSAndreas Gohr  return $url;
1762684e50aSAndreas Gohr}
1772684e50aSAndreas Gohr
1782684e50aSAndreas Gohr/**
1792684e50aSAndreas Gohr * Build an string of html tag attributes
1802684e50aSAndreas Gohr *
1817bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded
1827bff22c0SAndreas Gohr *
1832684e50aSAndreas Gohr * @author Andreas Gohr
1842684e50aSAndreas Gohr */
1852684e50aSAndreas Gohrfunction buildAttributes($params){
1862684e50aSAndreas Gohr  $url = '';
1872684e50aSAndreas Gohr  foreach($params as $key => $val){
1887bff22c0SAndreas Gohr    if($key{0} == '_') continue;
1897bff22c0SAndreas Gohr
1902684e50aSAndreas Gohr    $url .= $key.'="';
1912684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1922684e50aSAndreas Gohr    $url .= '" ';
1932684e50aSAndreas Gohr  }
1942684e50aSAndreas Gohr  return $url;
1952684e50aSAndreas Gohr}
1962684e50aSAndreas Gohr
1972684e50aSAndreas Gohr
1982684e50aSAndreas Gohr/**
19915fae107Sandi * This builds the breadcrumb trail and returns it as array
20015fae107Sandi *
20115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
202f3f0262cSandi */
203f3f0262cSandifunction breadcrumbs(){
2048746e727Sandi  // we prepare the breadcrumbs early for quick session closing
2058746e727Sandi  static $crumbs = null;
2068746e727Sandi  if($crumbs != null) return $crumbs;
2078746e727Sandi
208f3f0262cSandi  global $ID;
209f3f0262cSandi  global $ACT;
210f3f0262cSandi  global $conf;
211e71ce681SAndreas Gohr  $crumbs = $_SESSION[DOKU_COOKIE]['bc'];
212f3f0262cSandi
213f3f0262cSandi  //first visit?
214f3f0262cSandi  if (!is_array($crumbs)){
215f3f0262cSandi    $crumbs = array();
216f3f0262cSandi  }
217f3f0262cSandi  //we only save on show and existing wiki documents
218a77f5846Sjan  $file = wikiFN($ID);
219a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
220e71ce681SAndreas Gohr    $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
221f3f0262cSandi    return $crumbs;
222f3f0262cSandi  }
223a77f5846Sjan
224a77f5846Sjan  // page names
225a77f5846Sjan  $name = noNS($ID);
226a77f5846Sjan  if ($conf['useheading']) {
227a77f5846Sjan    // get page title
228bb0a59d4Sjan    $title = p_get_first_heading($ID);
229a77f5846Sjan    if ($title) {
230a77f5846Sjan      $name = $title;
231a77f5846Sjan    }
232a77f5846Sjan  }
233a77f5846Sjan
234f3f0262cSandi  //remove ID from array
235a77f5846Sjan  if (isset($crumbs[$ID])) {
236a77f5846Sjan    unset($crumbs[$ID]);
237f3f0262cSandi  }
238f3f0262cSandi
239f3f0262cSandi  //add to array
240a77f5846Sjan  $crumbs[$ID] = $name;
241f3f0262cSandi  //reduce size
242f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
243f3f0262cSandi    array_shift($crumbs);
244f3f0262cSandi  }
245f3f0262cSandi  //save to session
246e71ce681SAndreas Gohr  $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
247f3f0262cSandi  return $crumbs;
248f3f0262cSandi}
249f3f0262cSandi
250f3f0262cSandi/**
25115fae107Sandi * Filter for page IDs
25215fae107Sandi *
253f3f0262cSandi * This is run on a ID before it is outputted somewhere
254f3f0262cSandi * currently used to replace the colon with something else
255f3f0262cSandi * on Windows systems and to have proper URL encoding
25615fae107Sandi *
25749c713a3Sandi * Urlencoding is ommitted when the second parameter is false
25849c713a3Sandi *
25915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
260f3f0262cSandi */
26149c713a3Sandifunction idfilter($id,$ue=true){
262f3f0262cSandi  global $conf;
263f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
264f3f0262cSandi    $id = strtr($id,':','/');
265f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
266f3f0262cSandi      $conf['userewrite']) {
267f3f0262cSandi    $id = strtr($id,':',';');
268f3f0262cSandi  }
26949c713a3Sandi  if($ue){
270b6c6979fSAndreas Gohr    $id = rawurlencode($id);
271f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
272f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
27349c713a3Sandi  }
274f3f0262cSandi  return $id;
275f3f0262cSandi}
276f3f0262cSandi
277f3f0262cSandi/**
278ed7b5f09Sandi * This builds a link to a wikipage
27915fae107Sandi *
2806c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2816c7843b5Sandi * given in $more
2826c7843b5Sandi *
28315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
284f3f0262cSandi */
285b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
286f3f0262cSandi  global $conf;
2876de3759aSAndreas Gohr  if(is_array($more)){
288b174aeaeSchris    $more = buildURLparams($more,$sep);
2896de3759aSAndreas Gohr  }else{
290b174aeaeSchris    $more = str_replace(',',$sep,$more);
2916de3759aSAndreas Gohr  }
292f3f0262cSandi
293f3f0262cSandi  $id    = idfilter($id);
294ed7b5f09Sandi  if($abs){
295ed7b5f09Sandi    $xlink = DOKU_URL;
296ed7b5f09Sandi  }else{
297ed7b5f09Sandi    $xlink = DOKU_BASE;
298ed7b5f09Sandi  }
299f3f0262cSandi
3006c7843b5Sandi  if($conf['userewrite'] == 2){
3016c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
3026c7843b5Sandi    if($more) $xlink .= '?'.$more;
3036c7843b5Sandi  }elseif($conf['userewrite']){
304f3f0262cSandi    $xlink .= $id;
305f3f0262cSandi    if($more) $xlink .= '?'.$more;
3066c7843b5Sandi  }else{
3076c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
308b174aeaeSchris    if($more) $xlink .= $sep.$more;
309f3f0262cSandi  }
310f3f0262cSandi
311f3f0262cSandi  return $xlink;
312f3f0262cSandi}
313f3f0262cSandi
314f3f0262cSandi/**
315f5c2808fSBen Coburn * This builds a link to an alternate page format
316f5c2808fSBen Coburn *
317f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
318f5c2808fSBen Coburn *
319f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
320f5c2808fSBen Coburn */
321f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
322f5c2808fSBen Coburn  global $conf;
323f5c2808fSBen Coburn  if(is_array($more)){
324f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
325f5c2808fSBen Coburn  }else{
326f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
327f5c2808fSBen Coburn  }
328f5c2808fSBen Coburn
329f5c2808fSBen Coburn  $format = rawurlencode($format);
330f5c2808fSBen Coburn  $id = idfilter($id);
331f5c2808fSBen Coburn  if($abs){
332f5c2808fSBen Coburn    $xlink = DOKU_URL;
333f5c2808fSBen Coburn  }else{
334f5c2808fSBen Coburn    $xlink = DOKU_BASE;
335f5c2808fSBen Coburn  }
336f5c2808fSBen Coburn
337f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
338f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
339f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
340f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
341f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
342f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
343f5c2808fSBen Coburn  }else{
344f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
345f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
346f5c2808fSBen Coburn  }
347f5c2808fSBen Coburn
348f5c2808fSBen Coburn  return $xlink;
349f5c2808fSBen Coburn}
350f5c2808fSBen Coburn
351f5c2808fSBen Coburn/**
3526de3759aSAndreas Gohr * Build a link to a media file
3536de3759aSAndreas Gohr *
3546de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
3556de3759aSAndreas Gohr */
356b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
3576de3759aSAndreas Gohr  global $conf;
3586de3759aSAndreas Gohr  if(is_array($more)){
359b174aeaeSchris    $more = buildURLparams($more,$sep);
3606de3759aSAndreas Gohr  }else{
361b174aeaeSchris    $more = str_replace(',',$sep,$more);
3626de3759aSAndreas Gohr  }
3636de3759aSAndreas Gohr
3646de3759aSAndreas Gohr  $xlink = DOKU_BASE;
3656de3759aSAndreas Gohr
3666de3759aSAndreas Gohr  // external URLs are always direct without rewriting
3676de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
3686de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
3696de3759aSAndreas Gohr    if($more){
3706de3759aSAndreas Gohr      $xlink .= '?'.$more;
371b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3726de3759aSAndreas Gohr    }else{
373b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3746de3759aSAndreas Gohr    }
3756de3759aSAndreas Gohr    return $xlink;
3766de3759aSAndreas Gohr  }
3776de3759aSAndreas Gohr
3786de3759aSAndreas Gohr  $id = idfilter($id);
3796de3759aSAndreas Gohr
3806de3759aSAndreas Gohr  // decide on scriptname
3816de3759aSAndreas Gohr  if($direct){
3826de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3836de3759aSAndreas Gohr      $script = '_media';
3846de3759aSAndreas Gohr    }else{
3856de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3866de3759aSAndreas Gohr    }
3876de3759aSAndreas Gohr  }else{
3886de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3896de3759aSAndreas Gohr      $script = '_detail';
3906de3759aSAndreas Gohr    }else{
3916de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3926de3759aSAndreas Gohr    }
3936de3759aSAndreas Gohr  }
3946de3759aSAndreas Gohr
3956de3759aSAndreas Gohr  // build URL based on rewrite mode
3966de3759aSAndreas Gohr   if($conf['userewrite']){
3976de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3986de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3996de3759aSAndreas Gohr   }else{
4006de3759aSAndreas Gohr     if($more){
401a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
402b174aeaeSchris       $xlink .= $sep.'media='.$id;
4036de3759aSAndreas Gohr     }else{
404a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
4056de3759aSAndreas Gohr     }
4066de3759aSAndreas Gohr   }
4076de3759aSAndreas Gohr
4086de3759aSAndreas Gohr  return $xlink;
4096de3759aSAndreas Gohr}
4106de3759aSAndreas Gohr
4116de3759aSAndreas Gohr
4126de3759aSAndreas Gohr
4136de3759aSAndreas Gohr/**
414f3f0262cSandi * Just builds a link to a script
41515fae107Sandi *
416ed7b5f09Sandi * @todo   maybe obsolete
41715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
418f3f0262cSandi */
419f3f0262cSandifunction script($script='doku.php'){
420ed7b5f09Sandi#  $link = getBaseURL();
421ed7b5f09Sandi#  $link .= $script;
422ed7b5f09Sandi#  return $link;
423ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
424f3f0262cSandi}
425f3f0262cSandi
426f3f0262cSandi/**
42715fae107Sandi * Spamcheck against wordlist
42815fae107Sandi *
429f3f0262cSandi * Checks the wikitext against a list of blocked expressions
430f3f0262cSandi * returns true if the text contains any bad words
43115fae107Sandi *
43215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
433f3f0262cSandi */
434f3f0262cSandifunction checkwordblock(){
435f3f0262cSandi  global $TEXT;
436f3f0262cSandi  global $conf;
437f3f0262cSandi
438f3f0262cSandi  if(!$conf['usewordblock']) return false;
439f3f0262cSandi
440041d1964SAndreas Gohr  // we prepare the text a tiny bit to prevent spammers circumventing URL checks
441041d1964SAndreas Gohr  $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i','\1http://\2 \2\3',$TEXT);
442041d1964SAndreas Gohr
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{
451a51d08efSAndreas Gohr    //read file in chunks of 200 - this should work around the
4523e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
453a51d08efSAndreas Gohr    $chunksize = 200;
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    }
464041d1964SAndreas Gohr    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'];
487bb4866bdSchris  if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
4886d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
489bb4866bdSchris  if(!empty($_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);
4944ff28443Schris  $match = array();
4956d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
4964ff28443Schris    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
4974ff28443Schris      $ip[$i] = $match[0];
4984ff28443Schris    } else {
4994ff28443Schris      $ip[$i] = '';
5004ff28443Schris    }
5016d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
502f3f0262cSandi  }
5036d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
5046d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
5056d8affe6SAndreas Gohr
5066d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
5076d8affe6SAndreas Gohr
5086d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
5096d8affe6SAndreas Gohr  $ip = array_reverse($ip);
5106d8affe6SAndreas Gohr  foreach($ip as $i){
5116d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
5126d8affe6SAndreas Gohr      continue;
5136d8affe6SAndreas Gohr    }else{
5146d8affe6SAndreas Gohr      return $i;
5156d8affe6SAndreas Gohr    }
5166d8affe6SAndreas Gohr  }
5176d8affe6SAndreas Gohr  // still here? just use the first (last) address
5186d8affe6SAndreas Gohr  return $ip[0];
519f3f0262cSandi}
520f3f0262cSandi
521f3f0262cSandi/**
52215fae107Sandi * Checks if a given page is currently locked.
52315fae107Sandi *
524f3f0262cSandi * removes stale lockfiles
52515fae107Sandi *
52615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
527f3f0262cSandi */
528f3f0262cSandifunction checklock($id){
529f3f0262cSandi  global $conf;
530c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
531f3f0262cSandi
532f3f0262cSandi  //no lockfile
533f3f0262cSandi  if(!@file_exists($lock)) return false;
534f3f0262cSandi
535f3f0262cSandi  //lockfile expired
536f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
537d8186216SBen Coburn    @unlink($lock);
538f3f0262cSandi    return false;
539f3f0262cSandi  }
540f3f0262cSandi
541f3f0262cSandi  //my own lock
542f3f0262cSandi  $ip = io_readFile($lock);
543f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
544f3f0262cSandi    return false;
545f3f0262cSandi  }
546f3f0262cSandi
547f3f0262cSandi  return $ip;
548f3f0262cSandi}
549f3f0262cSandi
550f3f0262cSandi/**
55115fae107Sandi * Lock a page for editing
55215fae107Sandi *
55315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
554f3f0262cSandi */
555f3f0262cSandifunction lock($id){
556c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
557f3f0262cSandi  if($_SERVER['REMOTE_USER']){
558f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
559f3f0262cSandi  }else{
560f3f0262cSandi    io_saveFile($lock,clientIP());
561f3f0262cSandi  }
562f3f0262cSandi}
563f3f0262cSandi
564f3f0262cSandi/**
56515fae107Sandi * Unlock a page if it was locked by the user
566f3f0262cSandi *
56715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
56815fae107Sandi * @return bool true if a lock was removed
569f3f0262cSandi */
570f3f0262cSandifunction unlock($id){
571c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
572f3f0262cSandi  if(@file_exists($lock)){
573f3f0262cSandi    $ip = io_readFile($lock);
574f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
575f3f0262cSandi      @unlink($lock);
576f3f0262cSandi      return true;
577f3f0262cSandi    }
578f3f0262cSandi  }
579f3f0262cSandi  return false;
580f3f0262cSandi}
581f3f0262cSandi
582f3f0262cSandi/**
583f3f0262cSandi * convert line ending to unix format
584f3f0262cSandi *
58515fae107Sandi * @see    formText() for 2crlf conversion
58615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
587f3f0262cSandi */
588f3f0262cSandifunction cleanText($text){
589f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
590f3f0262cSandi  return $text;
591f3f0262cSandi}
592f3f0262cSandi
593f3f0262cSandi/**
594f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
595f3f0262cSandi * It also converts line endings to Windows format which is
596f3f0262cSandi * pseudo standard for webforms.
597f3f0262cSandi *
59815fae107Sandi * @see    cleanText() for 2unix conversion
59915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
600f3f0262cSandi */
601f3f0262cSandifunction formText($text){
602f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
603f3f0262cSandi  return htmlspecialchars($text);
604f3f0262cSandi}
605f3f0262cSandi
606f3f0262cSandi/**
60715fae107Sandi * Returns the specified local text in raw format
60815fae107Sandi *
60915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
610f3f0262cSandi */
611f3f0262cSandifunction rawLocale($id){
612f3f0262cSandi  return io_readFile(localeFN($id));
613f3f0262cSandi}
614f3f0262cSandi
615f3f0262cSandi/**
616f3f0262cSandi * Returns the raw WikiText
61715fae107Sandi *
61815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
619f3f0262cSandi */
620f3f0262cSandifunction rawWiki($id,$rev=''){
621cc7d0c94SBen Coburn  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
622f3f0262cSandi}
623f3f0262cSandi
624f3f0262cSandi/**
6257146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
6267146cee2SAndreas Gohr *
6277146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6287146cee2SAndreas Gohr */
629b7d5a5f0SAndreas Gohrfunction pageTemplate($data){
630b7d5a5f0SAndreas Gohr  $id = $data[0];
631a15ce62dSEsther Brunner  global $conf;
632a15ce62dSEsther Brunner  global $INFO;
633a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
634a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
635a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
636a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
637a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
638a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
639a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
640a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
641a15ce62dSEsther Brunner  return $tpl;
6427146cee2SAndreas Gohr}
6437146cee2SAndreas Gohr
6447146cee2SAndreas Gohr
6457146cee2SAndreas Gohr/**
64615fae107Sandi * Returns the raw Wiki Text in three slices.
64715fae107Sandi *
64815fae107Sandi * The range parameter needs to have the form "from-to"
64915cfe303Sandi * and gives the range of the section in bytes - no
65015cfe303Sandi * UTF-8 awareness is needed.
651f3f0262cSandi * The returned order is prefix, section and suffix.
65215fae107Sandi *
65315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
654f3f0262cSandi */
655f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
656f3f0262cSandi  list($from,$to) = split('-',$range,2);
657cc7d0c94SBen Coburn  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
658f3f0262cSandi  if(!$from) $from = 0;
659c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
660f3f0262cSandi
66115cfe303Sandi  $slices[0] = substr($text,0,$from-1);
66215cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
66315cfe303Sandi  $slices[2] = substr($text,$to);
664f3f0262cSandi
665f3f0262cSandi  return $slices;
666f3f0262cSandi}
667f3f0262cSandi
668f3f0262cSandi/**
66915fae107Sandi * Joins wiki text slices
67015fae107Sandi *
671f3f0262cSandi * function to join the text slices with correct lineendings again.
672f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
673f3f0262cSandi * lines between sections if needed (used on saving).
67415fae107Sandi *
67515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
676f3f0262cSandi */
677f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
678f3f0262cSandi
679f3f0262cSandi  if($pretty){
680f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
681f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
682f3f0262cSandi  }
683f3f0262cSandi
684f3f0262cSandi  if($pre) $pre .= "\n";
685f3f0262cSandi  if($suf) $text .= "\n";
686f3f0262cSandi  return $pre.$text.$suf;
687f3f0262cSandi}
688f3f0262cSandi
689f3f0262cSandi/**
690a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage.
691a701424fSBen Coburn * Also directs changelog and attic updates.
69215fae107Sandi *
69315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
69471726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
695f3f0262cSandi */
696b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
697a701424fSBen Coburn  /* Note to developers:
698a701424fSBen Coburn     This code is subtle and delicate. Test the behavior of
699a701424fSBen Coburn     the attic and changelog with dokuwiki and external edits
700a701424fSBen Coburn     after any changes. External edits change the wiki page
701a701424fSBen Coburn     directly without using php or dokuwiki.
702a701424fSBen Coburn  */
703f3f0262cSandi  global $conf;
704f3f0262cSandi  global $lang;
70571726d78SBen Coburn  global $REV;
706f3f0262cSandi  // ignore if no changes were made
707f3f0262cSandi  if($text == rawWiki($id,'')){
708f3f0262cSandi    return;
709f3f0262cSandi  }
710f3f0262cSandi
711f3f0262cSandi  $file = wikiFN($id);
712a701424fSBen Coburn  $old = @filemtime($file); // from page
71371726d78SBen Coburn  $wasRemoved = empty($text);
714d8186216SBen Coburn  $wasCreated = !@file_exists($file);
71571726d78SBen Coburn  $wasReverted = ($REV==true);
716e45b34cdSBen Coburn  $newRev = false;
717a701424fSBen Coburn  $oldRev = getRevisions($id, -1, 1, 1024); // from changelog
718a701424fSBen Coburn  $oldRev = (int)(empty($oldRev)?0:$oldRev[0]);
719a701424fSBen Coburn  if(!@file_exists(wikiFN($id, $old)) && @file_exists($file) && $old>=$oldRev) {
72046844156SBen Coburn    // add old revision to the attic if missing
72146844156SBen Coburn    saveOldRevision($id);
72246844156SBen Coburn    // add a changelog entry if this edit came from outside dokuwiki
723a701424fSBen Coburn    if ($old>$oldRev) {
72446844156SBen Coburn      addLogEntry($old, $id);
72546844156SBen Coburn      // send notify mails
72646844156SBen Coburn      notify($id,'admin',$oldRev,'',false);
72746844156SBen Coburn      notify($id,'subscribers',$oldRev,'',false);
72846844156SBen Coburn      // remove soon to be stale instructions
72946844156SBen Coburn      $cache = new cache_instructions($id, $file);
73046844156SBen Coburn      $cache->removeCache();
73146844156SBen Coburn    }
73246844156SBen Coburn  }
733f3f0262cSandi
73471726d78SBen Coburn  if ($wasRemoved){
735e45b34cdSBen Coburn    // pre-save deleted revision
736e45b34cdSBen Coburn    @touch($file);
73746844156SBen Coburn    clearstatcache();
738e45b34cdSBen Coburn    $newRev = saveOldRevision($id);
739e1f3d9e1SEsther Brunner    // remove empty file
740f3f0262cSandi    @unlink($file);
74171726d78SBen Coburn    // remove old meta info...
742e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
74371726d78SBen Coburn    $changelog = metaFN($id, '.changes');
744e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
74571726d78SBen Coburn      // but keep per-page changelog to preserve page history
746d8186216SBen Coburn      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
747b158d625SSteven Danz    }
748f3f0262cSandi    $del = true;
7493ce054b3Sandi    // autoset summary on deletion
7503ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
75153d6ccfeSandi    // remove empty namespaces
752cc7d0c94SBen Coburn    io_sweepNS($id, 'datadir');
753cc7d0c94SBen Coburn    io_sweepNS($id, 'mediadir');
754f3f0262cSandi  }else{
755cc7d0c94SBen Coburn    // save file (namespace dir is created in io_writeWikiPage)
756cc7d0c94SBen Coburn    io_writeWikiPage($file, $text, $id);
75746844156SBen Coburn    // pre-save the revision, to keep the attic in sync
75846844156SBen Coburn    $newRev = saveOldRevision($id);
759f3f0262cSandi    $del = false;
760f3f0262cSandi  }
761f3f0262cSandi
76271726d78SBen Coburn  // select changelog line type
76371726d78SBen Coburn  $extra = '';
76471726d78SBen Coburn  $type = 'E';
76571726d78SBen Coburn  if ($wasReverted) {
76671726d78SBen Coburn    $type = 'R';
76771726d78SBen Coburn    $extra = $REV;
76871726d78SBen Coburn  }
76971726d78SBen Coburn  else if ($wasCreated) { $type = 'C'; }
77071726d78SBen Coburn  else if ($wasRemoved) { $type = 'D'; }
77171726d78SBen Coburn  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
77271726d78SBen Coburn
773e45b34cdSBen Coburn  addLogEntry($newRev, $id, $type, $summary, $extra);
77426a0801fSAndreas Gohr  // send notify mails
77590033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
77690033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
777f3f0262cSandi
778ce6b63d9Schris  // update the purgefile (timestamp of the last time anything within the wiki was changed)
77998407a7aSandi  io_saveFile($conf['cachedir'].'/purgefile',time());
780f3f0262cSandi}
781f3f0262cSandi
782f3f0262cSandi/**
783f3f0262cSandi * moves the current version to the attic and returns its
784f3f0262cSandi * revision date
78515fae107Sandi *
78615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
787f3f0262cSandi */
788f3f0262cSandifunction saveOldRevision($id){
789f3f0262cSandi  global $conf;
790f3f0262cSandi  $oldf = wikiFN($id);
791f3f0262cSandi  if(!@file_exists($oldf)) return '';
792f3f0262cSandi  $date = filemtime($oldf);
793f3f0262cSandi  $newf = wikiFN($id,$date);
794cc7d0c94SBen Coburn  io_writeWikiPage($newf, rawWiki($id), $id, $date);
795f3f0262cSandi  return $date;
796f3f0262cSandi}
797f3f0262cSandi
798f3f0262cSandi/**
79926a0801fSAndreas Gohr * Sends a notify mail on page change
80026a0801fSAndreas Gohr *
80126a0801fSAndreas Gohr * @param  string  $id       The changed page
80226a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
80326a0801fSAndreas Gohr * @param  int     $rev      Old page revision
80426a0801fSAndreas Gohr * @param  string  $summary  What changed
80590033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
80602a498e7Schris * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
80715fae107Sandi *
80815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
809f3f0262cSandi */
81002a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
811f3f0262cSandi  global $lang;
812f3f0262cSandi  global $conf;
813*30d7d718SMike Frysinger  global $INFO;
814b158d625SSteven Danz
81526a0801fSAndreas Gohr  // decide if there is something to do
81626a0801fSAndreas Gohr  if($who == 'admin'){
81726a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
818f3f0262cSandi    $text = rawLocale('mailtext');
81926a0801fSAndreas Gohr    $to   = $conf['notify'];
82026a0801fSAndreas Gohr    $bcc  = '';
82126a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
82226a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
82390033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
82426a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
82526a0801fSAndreas Gohr    if(empty($bcc)) return;
82626a0801fSAndreas Gohr    $to   = '';
82726a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
828a06e4bdbSSebastian Harl  }elseif($who == 'register'){
829a06e4bdbSSebastian Harl    if(empty($conf['registernotify'])) return;
830a06e4bdbSSebastian Harl    $text = rawLocale('registermail');
831a06e4bdbSSebastian Harl    $to   = $conf['registernotify'];
832a06e4bdbSSebastian Harl    $bcc  = '';
83326a0801fSAndreas Gohr  }else{
83426a0801fSAndreas Gohr    return; //just to be safe
83526a0801fSAndreas Gohr  }
83626a0801fSAndreas Gohr
837f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
838f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
839f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
840f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
841ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
84226a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
84326a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
844ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
845f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
8467a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
847f3f0262cSandi
84802a498e7Schris  foreach ($replace as $key => $substitution) {
84902a498e7Schris    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
85002a498e7Schris  }
85102a498e7Schris
852a06e4bdbSSebastian Harl  if($who == 'register'){
853a06e4bdbSSebastian Harl    $subject = $lang['mail_new_user'].' '.$summary;
854a06e4bdbSSebastian Harl  }elseif($rev){
855f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
856ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
857ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
858f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
859f3f0262cSandi                    split("\n",rawWiki($id)));
860f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
861f3f0262cSandi    $diff    = $dformat->format($df);
862f3f0262cSandi  }else{
863f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
864f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
865f3f0262cSandi    $diff = rawWiki($id);
866f3f0262cSandi  }
867f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
868241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
869f3f0262cSandi
870*30d7d718SMike Frysinger  $from = $conf['mailfrom'];
871*30d7d718SMike Frysinger  $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from);
872*30d7d718SMike Frysinger  $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from);
873*30d7d718SMike Frysinger  $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from);
874*30d7d718SMike Frysinger
875*30d7d718SMike Frysinger  mail_send($to,$subject,$text,$from,'',$bcc);
876f3f0262cSandi}
877f3f0262cSandi
87815fae107Sandi/**
879f3f0262cSandi * extracts the query from a google referer
88015fae107Sandi *
8816b13307fSandi * @todo   should be more generic and support yahoo et al
88215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
883f3f0262cSandi */
884f3f0262cSandifunction getGoogleQuery(){
885f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
8865c3f206fSandi  if(!$url) return '';
887f3f0262cSandi
888f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
889f3f0262cSandi  $query = array();
890f3f0262cSandi  parse_str($url['query'],$query);
891f3f0262cSandi
892f3f0262cSandi  return $query['q'];
893f3f0262cSandi}
894f3f0262cSandi
895f3f0262cSandi/**
89615fae107Sandi * Try to set correct locale
89715fae107Sandi *
898095bfd5cSandi * @deprecated No longer used
89915fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
900f3f0262cSandi */
901f3f0262cSandifunction setCorrectLocale(){
902f3f0262cSandi  global $conf;
903f3f0262cSandi  global $lang;
904f3f0262cSandi
905f3f0262cSandi  $enc = strtoupper($lang['encoding']);
906f3f0262cSandi  foreach ($lang['locales'] as $loc){
907f3f0262cSandi    //try locale
908f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
909f3f0262cSandi    //try loceale with encoding
910f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
911f3f0262cSandi  }
912f3f0262cSandi  //still here? try to set from environment
913f3f0262cSandi  @setlocale(LC_ALL,"");
914f3f0262cSandi}
915f3f0262cSandi
916f3f0262cSandi/**
917f3f0262cSandi * Return the human readable size of a file
918f3f0262cSandi *
919f3f0262cSandi * @param       int    $size   A file size
920f3f0262cSandi * @param       int    $dec    A number of decimal places
921f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
922f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
923f3f0262cSandi * @version     1.0.0
924f3f0262cSandi */
925f31d5b73Sandifunction filesize_h($size, $dec = 1){
926f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
927f3f0262cSandi  $count = count($sizes);
928f3f0262cSandi  $i = 0;
929f3f0262cSandi
930f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
931f3f0262cSandi    $size /= 1024;
932f3f0262cSandi    $i++;
933f3f0262cSandi  }
934f3f0262cSandi
935f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
936f3f0262cSandi}
937f3f0262cSandi
93815fae107Sandi/**
93900a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
94000a7b5adSEsther Brunner *
94100a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
94200a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
94300a7b5adSEsther Brunner */
94400a7b5adSEsther Brunnerfunction obfuscate($email) {
94500a7b5adSEsther Brunner  global $conf;
94600a7b5adSEsther Brunner
94700a7b5adSEsther Brunner  switch ($conf['mailguard']) {
94800a7b5adSEsther Brunner    case 'visible' :
94900a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
95000a7b5adSEsther Brunner      return strtr($email, $obfuscate);
95100a7b5adSEsther Brunner
95200a7b5adSEsther Brunner    case 'hex' :
95300a7b5adSEsther Brunner      $encode = '';
95400a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
95500a7b5adSEsther Brunner      return $encode;
95600a7b5adSEsther Brunner
95700a7b5adSEsther Brunner    case 'none' :
95800a7b5adSEsther Brunner    default :
95900a7b5adSEsther Brunner      return $email;
96000a7b5adSEsther Brunner  }
96100a7b5adSEsther Brunner}
96200a7b5adSEsther Brunner
96300a7b5adSEsther Brunner/**
964b158d625SSteven Danz * Let us know if a user is tracking a page
965b158d625SSteven Danz *
9661380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
967b158d625SSteven Danz */
9681380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
9691380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
9701380fc45SAndreas Gohr  if (@file_exists($file)) {
971b158d625SSteven Danz    $mlist = file($file);
9721380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
9731380fc45SAndreas Gohr    return is_int($pos);
974b158d625SSteven Danz  }
9751380fc45SAndreas Gohr
976b158d625SSteven Danz  return false;
977b158d625SSteven Danz}
978340756e4Sandi
979f9eb5648Ssteven-danz/**
980f9eb5648Ssteven-danz * Return a string with the email addresses of all the
981f9eb5648Ssteven-danz * users subscribed to a page
982f9eb5648Ssteven-danz *
98326a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
984f9eb5648Ssteven-danz */
985f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
986f9eb5648Ssteven-danz  global $conf;
987cd52f92dSchris  global $auth;
988f9eb5648Ssteven-danz
989f9eb5648Ssteven-danz  $emails = '';
990f9eb5648Ssteven-danz
99126a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
99226a0801fSAndreas Gohr
993f9eb5648Ssteven-danz  $mlist = array();
994f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
995d8186216SBen Coburn  if (@file_exists($file)) {
996f9eb5648Ssteven-danz    $mlist = file($file);
997f9eb5648Ssteven-danz  }
998f9eb5648Ssteven-danz  if(count($mlist) > 0) {
999f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1000f9eb5648Ssteven-danz      $who = rtrim($who);
1001cd52f92dSchris      $info = $auth->getUserData($who);
1002f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1003f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1004f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1005f9eb5648Ssteven-danz          if (empty($emails)) {
1006f9eb5648Ssteven-danz            $emails = $info['mail'];
1007f9eb5648Ssteven-danz          } else {
1008f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1009f9eb5648Ssteven-danz          }
1010f9eb5648Ssteven-danz        }
1011f9eb5648Ssteven-danz      }
1012f9eb5648Ssteven-danz    }
1013f9eb5648Ssteven-danz  }
1014f9eb5648Ssteven-danz
1015f9eb5648Ssteven-danz  return $emails;
1016f9eb5648Ssteven-danz}
1017f9eb5648Ssteven-danz
101889541d4bSAndreas Gohr/**
101989541d4bSAndreas Gohr * Removes quoting backslashes
102089541d4bSAndreas Gohr *
102189541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
102289541d4bSAndreas Gohr */
102389541d4bSAndreas Gohrfunction unslash($string,$char="'"){
102489541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
102589541d4bSAndreas Gohr}
102689541d4bSAndreas Gohr
1027340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1028