xref: /dokuwiki/inc/common.php (revision c29dc6e4219a920b505ef667b82d4f601b34e52b)
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');
16*c29dc6e4SAndreas 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 ' ';
44d5197206Schris  print"$string\n";
45d5197206Schris}
46d5197206Schris
47d5197206Schris/**
4815fae107Sandi * Return info about the current document as associative
49f3f0262cSandi * array.
5015fae107Sandi *
5115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
52f3f0262cSandi */
53f3f0262cSandifunction pageinfo(){
54f3f0262cSandi  global $ID;
55f3f0262cSandi  global $REV;
56f3f0262cSandi  global $USERINFO;
57f3f0262cSandi  global $conf;
58f3f0262cSandi
59f3f0262cSandi  if($_SERVER['REMOTE_USER']){
60f3f0262cSandi    $info['userinfo']   = $USERINFO;
61f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
621380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
63ee4c4a1bSAndreas Gohr    $info['client']     = $_SERVER['REMOTE_USER'];
6417ee7f66SAndreas Gohr
6517ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
6617ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
6717ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
6817ee7f66SAndreas Gohr    }
69ee4c4a1bSAndreas Gohr
70f3f0262cSandi  }else{
71f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
721380fc45SAndreas Gohr    $info['subscribed'] = false;
73ee4c4a1bSAndreas Gohr    $info['client']     = clientIP(true);
74f3f0262cSandi  }
75f3f0262cSandi
76f3f0262cSandi  $info['namespace'] = getNS($ID);
77f3f0262cSandi  $info['locked']    = checklock($ID);
78f3f0262cSandi  $info['filepath']  = realpath(wikiFN($ID,$REV));
79f3f0262cSandi  $info['exists']    = @file_exists($info['filepath']);
80f3f0262cSandi  if($REV && !$info['exists']){
81f3f0262cSandi    //check if current revision was meant
82f3f0262cSandi    $cur = wikiFN($ID);
83f3f0262cSandi    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
84f3f0262cSandi      $info['filepath'] = realpath($cur);
85f3f0262cSandi      $info['exists']   = true;
86f3f0262cSandi      $REV = '';
87f3f0262cSandi    }
88f3f0262cSandi  }
89c112d578Sandi  $info['rev'] = $REV;
90f3f0262cSandi  if($info['exists']){
91f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
92f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
93f3f0262cSandi  }else{
94f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
95f3f0262cSandi  }
96f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
97f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
98f3f0262cSandi
9971726d78SBen Coburn  //load page meta data
10071726d78SBen Coburn  $info['meta'] = p_get_metadata($ID);
10171726d78SBen Coburn
102652610a2Sandi  //who's the editor
103652610a2Sandi  if($REV){
10471726d78SBen Coburn    $revinfo = getRevisionInfo($ID, $REV, 1024);
105652610a2Sandi  }else{
10671726d78SBen Coburn    $revinfo = $info['meta']['last_change'];
107652610a2Sandi  }
108652610a2Sandi  $info['ip']     = $revinfo['ip'];
109652610a2Sandi  $info['user']   = $revinfo['user'];
110652610a2Sandi  $info['sum']    = $revinfo['sum'];
11171726d78SBen Coburn  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
11271726d78SBen Coburn  // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor'].
11359f257aeSchris
11488f522e9Sandi  if($revinfo['user']){
11588f522e9Sandi    $info['editor'] = $revinfo['user'];
11688f522e9Sandi  }else{
11788f522e9Sandi    $info['editor'] = $revinfo['ip'];
11888f522e9Sandi  }
119652610a2Sandi
120ee4c4a1bSAndreas Gohr  // draft
121ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
122ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
123ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
124ee4c4a1bSAndreas Gohr      // remove stale draft
125ee4c4a1bSAndreas Gohr      @unlink($draft);
126ee4c4a1bSAndreas Gohr    }else{
127ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
128ee4c4a1bSAndreas Gohr    }
129ee4c4a1bSAndreas Gohr  }
130ee4c4a1bSAndreas Gohr
131f3f0262cSandi  return $info;
132f3f0262cSandi}
133f3f0262cSandi
134f3f0262cSandi/**
1352684e50aSAndreas Gohr * Build an string of URL parameters
1362684e50aSAndreas Gohr *
1372684e50aSAndreas Gohr * @author Andreas Gohr
1382684e50aSAndreas Gohr */
139b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
1402684e50aSAndreas Gohr  $url = '';
1412684e50aSAndreas Gohr  $amp = false;
1422684e50aSAndreas Gohr  foreach($params as $key => $val){
143b174aeaeSchris    if($amp) $url .= $sep;
1442684e50aSAndreas Gohr
1452684e50aSAndreas Gohr    $url .= $key.'=';
146b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1472684e50aSAndreas Gohr    $amp = true;
1482684e50aSAndreas Gohr  }
1492684e50aSAndreas Gohr  return $url;
1502684e50aSAndreas Gohr}
1512684e50aSAndreas Gohr
1522684e50aSAndreas Gohr/**
1532684e50aSAndreas Gohr * Build an string of html tag attributes
1542684e50aSAndreas Gohr *
1557bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded
1567bff22c0SAndreas Gohr *
1572684e50aSAndreas Gohr * @author Andreas Gohr
1582684e50aSAndreas Gohr */
1592684e50aSAndreas Gohrfunction buildAttributes($params){
1602684e50aSAndreas Gohr  $url = '';
1612684e50aSAndreas Gohr  foreach($params as $key => $val){
1627bff22c0SAndreas Gohr    if($key{0} == '_') continue;
1637bff22c0SAndreas Gohr
1642684e50aSAndreas Gohr    $url .= $key.'="';
1652684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1662684e50aSAndreas Gohr    $url .= '" ';
1672684e50aSAndreas Gohr  }
1682684e50aSAndreas Gohr  return $url;
1692684e50aSAndreas Gohr}
1702684e50aSAndreas Gohr
1712684e50aSAndreas Gohr
1722684e50aSAndreas Gohr/**
17315fae107Sandi * This builds the breadcrumb trail and returns it as array
17415fae107Sandi *
17515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
176f3f0262cSandi */
177f3f0262cSandifunction breadcrumbs(){
1788746e727Sandi  // we prepare the breadcrumbs early for quick session closing
1798746e727Sandi  static $crumbs = null;
1808746e727Sandi  if($crumbs != null) return $crumbs;
1818746e727Sandi
182f3f0262cSandi  global $ID;
183f3f0262cSandi  global $ACT;
184f3f0262cSandi  global $conf;
185f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
186f3f0262cSandi
187f3f0262cSandi  //first visit?
188f3f0262cSandi  if (!is_array($crumbs)){
189f3f0262cSandi    $crumbs = array();
190f3f0262cSandi  }
191f3f0262cSandi  //we only save on show and existing wiki documents
192a77f5846Sjan  $file = wikiFN($ID);
193a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
194f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
195f3f0262cSandi    return $crumbs;
196f3f0262cSandi  }
197a77f5846Sjan
198a77f5846Sjan  // page names
199a77f5846Sjan  $name = noNS($ID);
200a77f5846Sjan  if ($conf['useheading']) {
201a77f5846Sjan    // get page title
202bb0a59d4Sjan    $title = p_get_first_heading($ID);
203a77f5846Sjan    if ($title) {
204a77f5846Sjan      $name = $title;
205a77f5846Sjan    }
206a77f5846Sjan  }
207a77f5846Sjan
208f3f0262cSandi  //remove ID from array
209a77f5846Sjan  if (isset($crumbs[$ID])) {
210a77f5846Sjan    unset($crumbs[$ID]);
211f3f0262cSandi  }
212f3f0262cSandi
213f3f0262cSandi  //add to array
214a77f5846Sjan  $crumbs[$ID] = $name;
215f3f0262cSandi  //reduce size
216f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
217f3f0262cSandi    array_shift($crumbs);
218f3f0262cSandi  }
219f3f0262cSandi  //save to session
220f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
221f3f0262cSandi  return $crumbs;
222f3f0262cSandi}
223f3f0262cSandi
224f3f0262cSandi/**
22515fae107Sandi * Filter for page IDs
22615fae107Sandi *
227f3f0262cSandi * This is run on a ID before it is outputted somewhere
228f3f0262cSandi * currently used to replace the colon with something else
229f3f0262cSandi * on Windows systems and to have proper URL encoding
23015fae107Sandi *
23149c713a3Sandi * Urlencoding is ommitted when the second parameter is false
23249c713a3Sandi *
23315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
234f3f0262cSandi */
23549c713a3Sandifunction idfilter($id,$ue=true){
236f3f0262cSandi  global $conf;
237f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
238f3f0262cSandi    $id = strtr($id,':','/');
239f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
240f3f0262cSandi      $conf['userewrite']) {
241f3f0262cSandi    $id = strtr($id,':',';');
242f3f0262cSandi  }
24349c713a3Sandi  if($ue){
244b6c6979fSAndreas Gohr    $id = rawurlencode($id);
245f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
246f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
24749c713a3Sandi  }
248f3f0262cSandi  return $id;
249f3f0262cSandi}
250f3f0262cSandi
251f3f0262cSandi/**
252ed7b5f09Sandi * This builds a link to a wikipage
25315fae107Sandi *
2546c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2556c7843b5Sandi * given in $more
2566c7843b5Sandi *
25715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
258f3f0262cSandi */
259b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
260f3f0262cSandi  global $conf;
2616de3759aSAndreas Gohr  if(is_array($more)){
262b174aeaeSchris    $more = buildURLparams($more,$sep);
2636de3759aSAndreas Gohr  }else{
264b174aeaeSchris    $more = str_replace(',',$sep,$more);
2656de3759aSAndreas Gohr  }
266f3f0262cSandi
267f3f0262cSandi  $id    = idfilter($id);
268ed7b5f09Sandi  if($abs){
269ed7b5f09Sandi    $xlink = DOKU_URL;
270ed7b5f09Sandi  }else{
271ed7b5f09Sandi    $xlink = DOKU_BASE;
272ed7b5f09Sandi  }
273f3f0262cSandi
2746c7843b5Sandi  if($conf['userewrite'] == 2){
2756c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
2766c7843b5Sandi    if($more) $xlink .= '?'.$more;
2776c7843b5Sandi  }elseif($conf['userewrite']){
278f3f0262cSandi    $xlink .= $id;
279f3f0262cSandi    if($more) $xlink .= '?'.$more;
2806c7843b5Sandi  }else{
2816c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
282b174aeaeSchris    if($more) $xlink .= $sep.$more;
283f3f0262cSandi  }
284f3f0262cSandi
285f3f0262cSandi  return $xlink;
286f3f0262cSandi}
287f3f0262cSandi
288f3f0262cSandi/**
289f5c2808fSBen Coburn * This builds a link to an alternate page format
290f5c2808fSBen Coburn *
291f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
292f5c2808fSBen Coburn *
293f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
294f5c2808fSBen Coburn */
295f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
296f5c2808fSBen Coburn  global $conf;
297f5c2808fSBen Coburn  if(is_array($more)){
298f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
299f5c2808fSBen Coburn  }else{
300f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
301f5c2808fSBen Coburn  }
302f5c2808fSBen Coburn
303f5c2808fSBen Coburn  $format = rawurlencode($format);
304f5c2808fSBen Coburn  $id = idfilter($id);
305f5c2808fSBen Coburn  if($abs){
306f5c2808fSBen Coburn    $xlink = DOKU_URL;
307f5c2808fSBen Coburn  }else{
308f5c2808fSBen Coburn    $xlink = DOKU_BASE;
309f5c2808fSBen Coburn  }
310f5c2808fSBen Coburn
311f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
312f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
313f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
314f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
315f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
316f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
317f5c2808fSBen Coburn  }else{
318f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
319f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
320f5c2808fSBen Coburn  }
321f5c2808fSBen Coburn
322f5c2808fSBen Coburn  return $xlink;
323f5c2808fSBen Coburn}
324f5c2808fSBen Coburn
325f5c2808fSBen Coburn/**
3266de3759aSAndreas Gohr * Build a link to a media file
3276de3759aSAndreas Gohr *
3286de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
3296de3759aSAndreas Gohr */
330b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
3316de3759aSAndreas Gohr  global $conf;
3326de3759aSAndreas Gohr  if(is_array($more)){
333b174aeaeSchris    $more = buildURLparams($more,$sep);
3346de3759aSAndreas Gohr  }else{
335b174aeaeSchris    $more = str_replace(',',$sep,$more);
3366de3759aSAndreas Gohr  }
3376de3759aSAndreas Gohr
3386de3759aSAndreas Gohr  $xlink = DOKU_BASE;
3396de3759aSAndreas Gohr
3406de3759aSAndreas Gohr  // external URLs are always direct without rewriting
3416de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
3426de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
3436de3759aSAndreas Gohr    if($more){
3446de3759aSAndreas Gohr      $xlink .= '?'.$more;
345b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3466de3759aSAndreas Gohr    }else{
347b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3486de3759aSAndreas Gohr    }
3496de3759aSAndreas Gohr    return $xlink;
3506de3759aSAndreas Gohr  }
3516de3759aSAndreas Gohr
3526de3759aSAndreas Gohr  $id = idfilter($id);
3536de3759aSAndreas Gohr
3546de3759aSAndreas Gohr  // decide on scriptname
3556de3759aSAndreas Gohr  if($direct){
3566de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3576de3759aSAndreas Gohr      $script = '_media';
3586de3759aSAndreas Gohr    }else{
3596de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3606de3759aSAndreas Gohr    }
3616de3759aSAndreas Gohr  }else{
3626de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3636de3759aSAndreas Gohr      $script = '_detail';
3646de3759aSAndreas Gohr    }else{
3656de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3666de3759aSAndreas Gohr    }
3676de3759aSAndreas Gohr  }
3686de3759aSAndreas Gohr
3696de3759aSAndreas Gohr  // build URL based on rewrite mode
3706de3759aSAndreas Gohr   if($conf['userewrite']){
3716de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3726de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3736de3759aSAndreas Gohr   }else{
3746de3759aSAndreas Gohr     if($more){
375a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
376b174aeaeSchris       $xlink .= $sep.'media='.$id;
3776de3759aSAndreas Gohr     }else{
378a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
3796de3759aSAndreas Gohr     }
3806de3759aSAndreas Gohr   }
3816de3759aSAndreas Gohr
3826de3759aSAndreas Gohr  return $xlink;
3836de3759aSAndreas Gohr}
3846de3759aSAndreas Gohr
3856de3759aSAndreas Gohr
3866de3759aSAndreas Gohr
3876de3759aSAndreas Gohr/**
388f3f0262cSandi * Just builds a link to a script
38915fae107Sandi *
390ed7b5f09Sandi * @todo   maybe obsolete
39115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
392f3f0262cSandi */
393f3f0262cSandifunction script($script='doku.php'){
394ed7b5f09Sandi#  $link = getBaseURL();
395ed7b5f09Sandi#  $link .= $script;
396ed7b5f09Sandi#  return $link;
397ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
398f3f0262cSandi}
399f3f0262cSandi
400f3f0262cSandi/**
40115fae107Sandi * Spamcheck against wordlist
40215fae107Sandi *
403f3f0262cSandi * Checks the wikitext against a list of blocked expressions
404f3f0262cSandi * returns true if the text contains any bad words
40515fae107Sandi *
40615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
407f3f0262cSandi */
408f3f0262cSandifunction checkwordblock(){
409f3f0262cSandi  global $TEXT;
410f3f0262cSandi  global $conf;
411f3f0262cSandi
412f3f0262cSandi  if(!$conf['usewordblock']) return false;
413f3f0262cSandi
414b9ac8716Schris  $wordblocks = getWordblocks();
4153e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
4163e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
4173e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
4183e2965d7Sandi    //backreferences are used - the maximum is 99
4193e2965d7Sandi    //this is very bad performancewise and may even be too high still
4203e2965d7Sandi    $chunksize = 40;
4213e2965d7Sandi  }else{
422703f6fdeSandi    //read file in chunks of 600 - this should work around the
4233e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
424444b87a5SAndreas Gohr    $chunksize = 400;
4253e2965d7Sandi  }
426b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
427f3f0262cSandi    $re = array();
428f3f0262cSandi    #build regexp from blocks
429f3f0262cSandi    foreach($blocks as $block){
430f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
431f3f0262cSandi      $block = trim($block);
432f3f0262cSandi      if(empty($block)) continue;
433f3f0262cSandi      $re[]  = $block;
434f3f0262cSandi    }
435b9ac8716Schris    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
436b9ac8716Schris      return true;
437b9ac8716Schris    }
438703f6fdeSandi  }
439f3f0262cSandi  return false;
440f3f0262cSandi}
441f3f0262cSandi
442f3f0262cSandi/**
44315fae107Sandi * Return the IP of the client
44415fae107Sandi *
4456d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers
44615fae107Sandi *
4476d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned
4486d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return
4496d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X
4506d8affe6SAndreas Gohr * headers
4516d8affe6SAndreas Gohr *
4526d8affe6SAndreas Gohr * @param  boolean $single If set only a single IP is returned
45315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
454f3f0262cSandi */
4556d8affe6SAndreas Gohrfunction clientIP($single=false){
4566d8affe6SAndreas Gohr  $ip = array();
4576d8affe6SAndreas Gohr  $ip[] = $_SERVER['REMOTE_ADDR'];
4586d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_FORWARDED_FOR'])
4596d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
4606d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_REAL_IP'])
4616d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
4626d8affe6SAndreas Gohr
4636d8affe6SAndreas Gohr  // remove any non-IP stuff
4646d8affe6SAndreas Gohr  $cnt = count($ip);
4654ff28443Schris  $match = array();
4666d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
4674ff28443Schris    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
4684ff28443Schris      $ip[$i] = $match[0];
4694ff28443Schris    } else {
4704ff28443Schris      $ip[$i] = '';
4714ff28443Schris    }
4726d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
473f3f0262cSandi  }
4746d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
4756d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
4766d8affe6SAndreas Gohr
4776d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
4786d8affe6SAndreas Gohr
4796d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
4806d8affe6SAndreas Gohr  $ip = array_reverse($ip);
4816d8affe6SAndreas Gohr  foreach($ip as $i){
4826d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
4836d8affe6SAndreas Gohr      continue;
4846d8affe6SAndreas Gohr    }else{
4856d8affe6SAndreas Gohr      return $i;
4866d8affe6SAndreas Gohr    }
4876d8affe6SAndreas Gohr  }
4886d8affe6SAndreas Gohr  // still here? just use the first (last) address
4896d8affe6SAndreas Gohr  return $ip[0];
490f3f0262cSandi}
491f3f0262cSandi
492f3f0262cSandi/**
49315fae107Sandi * Checks if a given page is currently locked.
49415fae107Sandi *
495f3f0262cSandi * removes stale lockfiles
49615fae107Sandi *
49715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
498f3f0262cSandi */
499f3f0262cSandifunction checklock($id){
500f3f0262cSandi  global $conf;
501c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
502f3f0262cSandi
503f3f0262cSandi  //no lockfile
504f3f0262cSandi  if(!@file_exists($lock)) return false;
505f3f0262cSandi
506f3f0262cSandi  //lockfile expired
507f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
508d8186216SBen Coburn    @unlink($lock);
509f3f0262cSandi    return false;
510f3f0262cSandi  }
511f3f0262cSandi
512f3f0262cSandi  //my own lock
513f3f0262cSandi  $ip = io_readFile($lock);
514f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
515f3f0262cSandi    return false;
516f3f0262cSandi  }
517f3f0262cSandi
518f3f0262cSandi  return $ip;
519f3f0262cSandi}
520f3f0262cSandi
521f3f0262cSandi/**
52215fae107Sandi * Lock a page for editing
52315fae107Sandi *
52415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
525f3f0262cSandi */
526f3f0262cSandifunction lock($id){
527c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
528f3f0262cSandi  if($_SERVER['REMOTE_USER']){
529f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
530f3f0262cSandi  }else{
531f3f0262cSandi    io_saveFile($lock,clientIP());
532f3f0262cSandi  }
533f3f0262cSandi}
534f3f0262cSandi
535f3f0262cSandi/**
53615fae107Sandi * Unlock a page if it was locked by the user
537f3f0262cSandi *
53815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
53915fae107Sandi * @return bool true if a lock was removed
540f3f0262cSandi */
541f3f0262cSandifunction unlock($id){
542c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
543f3f0262cSandi  if(@file_exists($lock)){
544f3f0262cSandi    $ip = io_readFile($lock);
545f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
546f3f0262cSandi      @unlink($lock);
547f3f0262cSandi      return true;
548f3f0262cSandi    }
549f3f0262cSandi  }
550f3f0262cSandi  return false;
551f3f0262cSandi}
552f3f0262cSandi
553f3f0262cSandi/**
554f3f0262cSandi * convert line ending to unix format
555f3f0262cSandi *
55615fae107Sandi * @see    formText() for 2crlf conversion
55715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
558f3f0262cSandi */
559f3f0262cSandifunction cleanText($text){
560f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
561f3f0262cSandi  return $text;
562f3f0262cSandi}
563f3f0262cSandi
564f3f0262cSandi/**
565f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
566f3f0262cSandi * It also converts line endings to Windows format which is
567f3f0262cSandi * pseudo standard for webforms.
568f3f0262cSandi *
56915fae107Sandi * @see    cleanText() for 2unix conversion
57015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
571f3f0262cSandi */
572f3f0262cSandifunction formText($text){
573f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
574f3f0262cSandi  return htmlspecialchars($text);
575f3f0262cSandi}
576f3f0262cSandi
577f3f0262cSandi/**
57815fae107Sandi * Returns the specified local text in raw format
57915fae107Sandi *
58015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
581f3f0262cSandi */
582f3f0262cSandifunction rawLocale($id){
583f3f0262cSandi  return io_readFile(localeFN($id));
584f3f0262cSandi}
585f3f0262cSandi
586f3f0262cSandi/**
587f3f0262cSandi * Returns the raw WikiText
58815fae107Sandi *
58915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
590f3f0262cSandi */
591f3f0262cSandifunction rawWiki($id,$rev=''){
592cc7d0c94SBen Coburn  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
593f3f0262cSandi}
594f3f0262cSandi
595f3f0262cSandi/**
5967146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
5977146cee2SAndreas Gohr *
5987146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5997146cee2SAndreas Gohr */
600b7d5a5f0SAndreas Gohrfunction pageTemplate($data){
601b7d5a5f0SAndreas Gohr  $id = $data[0];
602a15ce62dSEsther Brunner  global $conf;
603a15ce62dSEsther Brunner  global $INFO;
604a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
605a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
606a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
607a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
608a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
609a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
610a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
611a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
612a15ce62dSEsther Brunner  return $tpl;
6137146cee2SAndreas Gohr}
6147146cee2SAndreas Gohr
6157146cee2SAndreas Gohr
6167146cee2SAndreas Gohr/**
61715fae107Sandi * Returns the raw Wiki Text in three slices.
61815fae107Sandi *
61915fae107Sandi * The range parameter needs to have the form "from-to"
62015cfe303Sandi * and gives the range of the section in bytes - no
62115cfe303Sandi * UTF-8 awareness is needed.
622f3f0262cSandi * The returned order is prefix, section and suffix.
62315fae107Sandi *
62415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
625f3f0262cSandi */
626f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
627f3f0262cSandi  list($from,$to) = split('-',$range,2);
628cc7d0c94SBen Coburn  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
629f3f0262cSandi  if(!$from) $from = 0;
630c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
631f3f0262cSandi
63215cfe303Sandi  $slices[0] = substr($text,0,$from-1);
63315cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
63415cfe303Sandi  $slices[2] = substr($text,$to);
635f3f0262cSandi
636f3f0262cSandi  return $slices;
637f3f0262cSandi}
638f3f0262cSandi
639f3f0262cSandi/**
64015fae107Sandi * Joins wiki text slices
64115fae107Sandi *
642f3f0262cSandi * function to join the text slices with correct lineendings again.
643f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
644f3f0262cSandi * lines between sections if needed (used on saving).
64515fae107Sandi *
64615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
647f3f0262cSandi */
648f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
649f3f0262cSandi
650f3f0262cSandi  if($pretty){
651f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
652f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
653f3f0262cSandi  }
654f3f0262cSandi
655f3f0262cSandi  if($pre) $pre .= "\n";
656f3f0262cSandi  if($suf) $text .= "\n";
657f3f0262cSandi  return $pre.$text.$suf;
658f3f0262cSandi}
659f3f0262cSandi
660f3f0262cSandi/**
661cc7d0c94SBen Coburn * Saves a wikitext by calling io_writeWikiPage
66215fae107Sandi *
66315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
66471726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
665f3f0262cSandi */
666b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
667f3f0262cSandi  global $conf;
668f3f0262cSandi  global $lang;
66971726d78SBen Coburn  global $REV;
670f3f0262cSandi  // ignore if no changes were made
671f3f0262cSandi  if($text == rawWiki($id,'')){
672f3f0262cSandi    return;
673f3f0262cSandi  }
674f3f0262cSandi
675f3f0262cSandi  $file = wikiFN($id);
676f3f0262cSandi  $old  = saveOldRevision($id);
67771726d78SBen Coburn  $wasRemoved = empty($text);
678d8186216SBen Coburn  $wasCreated = !@file_exists($file);
67971726d78SBen Coburn  $wasReverted = ($REV==true);
680e45b34cdSBen Coburn  $newRev = false;
681f3f0262cSandi
68271726d78SBen Coburn  if ($wasRemoved){
683e45b34cdSBen Coburn    // pre-save deleted revision
684e45b34cdSBen Coburn    @touch($file);
685e45b34cdSBen Coburn    $newRev = saveOldRevision($id);
686e1f3d9e1SEsther Brunner    // remove empty file
687f3f0262cSandi    @unlink($file);
68871726d78SBen Coburn    // remove old meta info...
689e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
69071726d78SBen Coburn    $changelog = metaFN($id, '.changes');
691e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
69271726d78SBen Coburn      // but keep per-page changelog to preserve page history
693d8186216SBen Coburn      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
694b158d625SSteven Danz    }
695f3f0262cSandi    $del = true;
6963ce054b3Sandi    // autoset summary on deletion
6973ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
69853d6ccfeSandi    // remove empty namespaces
699cc7d0c94SBen Coburn    io_sweepNS($id, 'datadir');
700cc7d0c94SBen Coburn    io_sweepNS($id, 'mediadir');
701f3f0262cSandi  }else{
702cc7d0c94SBen Coburn    // save file (namespace dir is created in io_writeWikiPage)
703cc7d0c94SBen Coburn    io_writeWikiPage($file, $text, $id);
704e45b34cdSBen Coburn    $newRev = @filemtime($file);
705f3f0262cSandi    $del = false;
706f3f0262cSandi  }
707f3f0262cSandi
70871726d78SBen Coburn  // select changelog line type
70971726d78SBen Coburn  $extra = '';
71071726d78SBen Coburn  $type = 'E';
71171726d78SBen Coburn  if ($wasReverted) {
71271726d78SBen Coburn    $type = 'R';
71371726d78SBen Coburn    $extra = $REV;
71471726d78SBen Coburn  }
71571726d78SBen Coburn  else if ($wasCreated) { $type = 'C'; }
71671726d78SBen Coburn  else if ($wasRemoved) { $type = 'D'; }
71771726d78SBen Coburn  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
71871726d78SBen Coburn
719e45b34cdSBen Coburn  addLogEntry($newRev, $id, $type, $summary, $extra);
72026a0801fSAndreas Gohr  // send notify mails
72190033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
72290033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
723f3f0262cSandi
724f3f0262cSandi  //purge cache on add by updating the purgefile
725f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
72698407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
727f3f0262cSandi  }
728f3f0262cSandi}
729f3f0262cSandi
730f3f0262cSandi/**
731f3f0262cSandi * moves the current version to the attic and returns its
732f3f0262cSandi * revision date
73315fae107Sandi *
73415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
735f3f0262cSandi */
736f3f0262cSandifunction saveOldRevision($id){
737f3f0262cSandi  global $conf;
738f3f0262cSandi  $oldf = wikiFN($id);
739f3f0262cSandi  if(!@file_exists($oldf)) return '';
740f3f0262cSandi  $date = filemtime($oldf);
741f3f0262cSandi  $newf = wikiFN($id,$date);
742cc7d0c94SBen Coburn  io_writeWikiPage($newf, rawWiki($id), $id, $date);
743f3f0262cSandi  return $date;
744f3f0262cSandi}
745f3f0262cSandi
746f3f0262cSandi/**
74726a0801fSAndreas Gohr * Sends a notify mail on page change
74826a0801fSAndreas Gohr *
74926a0801fSAndreas Gohr * @param  string  $id       The changed page
75026a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
75126a0801fSAndreas Gohr * @param  int     $rev      Old page revision
75226a0801fSAndreas Gohr * @param  string  $summary  What changed
75390033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
75402a498e7Schris * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
75515fae107Sandi *
75615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
757f3f0262cSandi */
75802a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
759f3f0262cSandi  global $lang;
760f3f0262cSandi  global $conf;
761b158d625SSteven Danz
76226a0801fSAndreas Gohr  // decide if there is something to do
76326a0801fSAndreas Gohr  if($who == 'admin'){
76426a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
765f3f0262cSandi    $text = rawLocale('mailtext');
76626a0801fSAndreas Gohr    $to   = $conf['notify'];
76726a0801fSAndreas Gohr    $bcc  = '';
76826a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
76926a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
77090033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
77126a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
77226a0801fSAndreas Gohr    if(empty($bcc)) return;
77326a0801fSAndreas Gohr    $to   = '';
77426a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
775a06e4bdbSSebastian Harl  }elseif($who == 'register'){
776a06e4bdbSSebastian Harl    if(empty($conf['registernotify'])) return;
777a06e4bdbSSebastian Harl    $text = rawLocale('registermail');
778a06e4bdbSSebastian Harl    $to   = $conf['registernotify'];
779a06e4bdbSSebastian Harl    $bcc  = '';
78026a0801fSAndreas Gohr  }else{
78126a0801fSAndreas Gohr    return; //just to be safe
78226a0801fSAndreas Gohr  }
78326a0801fSAndreas Gohr
784f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
785f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
786f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
787f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
788ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
78926a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
79026a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
791ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
792f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
7937a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
794f3f0262cSandi
79502a498e7Schris  foreach ($replace as $key => $substitution) {
79602a498e7Schris    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
79702a498e7Schris  }
79802a498e7Schris
799a06e4bdbSSebastian Harl  if($who == 'register'){
800a06e4bdbSSebastian Harl    $subject = $lang['mail_new_user'].' '.$summary;
801a06e4bdbSSebastian Harl  }elseif($rev){
802f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
803ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
804ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
805f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
806f3f0262cSandi                    split("\n",rawWiki($id)));
807f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
808f3f0262cSandi    $diff    = $dformat->format($df);
809f3f0262cSandi  }else{
810f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
811f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
812f3f0262cSandi    $diff = rawWiki($id);
813f3f0262cSandi  }
814f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
815241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
816f3f0262cSandi
81726a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
818f3f0262cSandi}
819f3f0262cSandi
82015fae107Sandi/**
821f3f0262cSandi * extracts the query from a google referer
82215fae107Sandi *
8236b13307fSandi * @todo   should be more generic and support yahoo et al
82415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
825f3f0262cSandi */
826f3f0262cSandifunction getGoogleQuery(){
827f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
8285c3f206fSandi  if(!$url) return '';
829f3f0262cSandi
830f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
831f3f0262cSandi  $query = array();
832f3f0262cSandi  parse_str($url['query'],$query);
833f3f0262cSandi
834f3f0262cSandi  return $query['q'];
835f3f0262cSandi}
836f3f0262cSandi
837f3f0262cSandi/**
83815fae107Sandi * Try to set correct locale
83915fae107Sandi *
840095bfd5cSandi * @deprecated No longer used
84115fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
842f3f0262cSandi */
843f3f0262cSandifunction setCorrectLocale(){
844f3f0262cSandi  global $conf;
845f3f0262cSandi  global $lang;
846f3f0262cSandi
847f3f0262cSandi  $enc = strtoupper($lang['encoding']);
848f3f0262cSandi  foreach ($lang['locales'] as $loc){
849f3f0262cSandi    //try locale
850f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
851f3f0262cSandi    //try loceale with encoding
852f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
853f3f0262cSandi  }
854f3f0262cSandi  //still here? try to set from environment
855f3f0262cSandi  @setlocale(LC_ALL,"");
856f3f0262cSandi}
857f3f0262cSandi
858f3f0262cSandi/**
859f3f0262cSandi * Return the human readable size of a file
860f3f0262cSandi *
861f3f0262cSandi * @param       int    $size   A file size
862f3f0262cSandi * @param       int    $dec    A number of decimal places
863f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
864f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
865f3f0262cSandi * @version     1.0.0
866f3f0262cSandi */
867f31d5b73Sandifunction filesize_h($size, $dec = 1){
868f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
869f3f0262cSandi  $count = count($sizes);
870f3f0262cSandi  $i = 0;
871f3f0262cSandi
872f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
873f3f0262cSandi    $size /= 1024;
874f3f0262cSandi    $i++;
875f3f0262cSandi  }
876f3f0262cSandi
877f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
878f3f0262cSandi}
879f3f0262cSandi
88015fae107Sandi/**
88100a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
88200a7b5adSEsther Brunner *
88300a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
88400a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
88500a7b5adSEsther Brunner */
88600a7b5adSEsther Brunnerfunction obfuscate($email) {
88700a7b5adSEsther Brunner  global $conf;
88800a7b5adSEsther Brunner
88900a7b5adSEsther Brunner  switch ($conf['mailguard']) {
89000a7b5adSEsther Brunner    case 'visible' :
89100a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
89200a7b5adSEsther Brunner      return strtr($email, $obfuscate);
89300a7b5adSEsther Brunner
89400a7b5adSEsther Brunner    case 'hex' :
89500a7b5adSEsther Brunner      $encode = '';
89600a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
89700a7b5adSEsther Brunner      return $encode;
89800a7b5adSEsther Brunner
89900a7b5adSEsther Brunner    case 'none' :
90000a7b5adSEsther Brunner    default :
90100a7b5adSEsther Brunner      return $email;
90200a7b5adSEsther Brunner  }
90300a7b5adSEsther Brunner}
90400a7b5adSEsther Brunner
90500a7b5adSEsther Brunner/**
906b158d625SSteven Danz * Let us know if a user is tracking a page
907b158d625SSteven Danz *
9081380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
909b158d625SSteven Danz */
9101380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
9111380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
9121380fc45SAndreas Gohr  if (@file_exists($file)) {
913b158d625SSteven Danz    $mlist = file($file);
9141380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
9151380fc45SAndreas Gohr    return is_int($pos);
916b158d625SSteven Danz  }
9171380fc45SAndreas Gohr
918b158d625SSteven Danz  return false;
919b158d625SSteven Danz}
920340756e4Sandi
921f9eb5648Ssteven-danz/**
922f9eb5648Ssteven-danz * Return a string with the email addresses of all the
923f9eb5648Ssteven-danz * users subscribed to a page
924f9eb5648Ssteven-danz *
92526a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
926f9eb5648Ssteven-danz */
927f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
928f9eb5648Ssteven-danz  global $conf;
929cd52f92dSchris  global $auth;
930f9eb5648Ssteven-danz
931f9eb5648Ssteven-danz  $emails = '';
932f9eb5648Ssteven-danz
93326a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
93426a0801fSAndreas Gohr
935f9eb5648Ssteven-danz  $mlist = array();
936f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
937d8186216SBen Coburn  if (@file_exists($file)) {
938f9eb5648Ssteven-danz    $mlist = file($file);
939f9eb5648Ssteven-danz  }
940f9eb5648Ssteven-danz  if(count($mlist) > 0) {
941f9eb5648Ssteven-danz    foreach ($mlist as $who) {
942f9eb5648Ssteven-danz      $who = rtrim($who);
943cd52f92dSchris      $info = $auth->getUserData($who);
944f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
945f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
946f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
947f9eb5648Ssteven-danz          if (empty($emails)) {
948f9eb5648Ssteven-danz            $emails = $info['mail'];
949f9eb5648Ssteven-danz          } else {
950f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
951f9eb5648Ssteven-danz          }
952f9eb5648Ssteven-danz        }
953f9eb5648Ssteven-danz      }
954f9eb5648Ssteven-danz    }
955f9eb5648Ssteven-danz  }
956f9eb5648Ssteven-danz
957f9eb5648Ssteven-danz  return $emails;
958f9eb5648Ssteven-danz}
959f9eb5648Ssteven-danz
96089541d4bSAndreas Gohr/**
96189541d4bSAndreas Gohr * Removes quoting backslashes
96289541d4bSAndreas Gohr *
96389541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
96489541d4bSAndreas Gohr */
96589541d4bSAndreas Gohrfunction unslash($string,$char="'"){
96689541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
96789541d4bSAndreas Gohr}
96889541d4bSAndreas Gohr
969340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
970