xref: /dokuwiki/inc/common.php (revision e71ce681bdae620477f46591279412a0143b6b0e)
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 ' ';
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{
106bb4866bdSchris    $revinfo = isset($info['meta']['last_change']) ? $info['meta']['last_change'] : getRevisionInfo($ID,$info['lastmod'],1024);
107652610a2Sandi  }
108bb4866bdSchris
109652610a2Sandi  $info['ip']     = $revinfo['ip'];
110652610a2Sandi  $info['user']   = $revinfo['user'];
111652610a2Sandi  $info['sum']    = $revinfo['sum'];
11271726d78SBen Coburn  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
11371726d78SBen Coburn  // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor'].
11459f257aeSchris
11588f522e9Sandi  if($revinfo['user']){
11688f522e9Sandi    $info['editor'] = $revinfo['user'];
11788f522e9Sandi  }else{
11888f522e9Sandi    $info['editor'] = $revinfo['ip'];
11988f522e9Sandi  }
120652610a2Sandi
121ee4c4a1bSAndreas Gohr  // draft
122ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
123ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
124ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
125ee4c4a1bSAndreas Gohr      // remove stale draft
126ee4c4a1bSAndreas Gohr      @unlink($draft);
127ee4c4a1bSAndreas Gohr    }else{
128ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
129ee4c4a1bSAndreas Gohr    }
130ee4c4a1bSAndreas Gohr  }
131ee4c4a1bSAndreas Gohr
132f3f0262cSandi  return $info;
133f3f0262cSandi}
134f3f0262cSandi
135f3f0262cSandi/**
1362684e50aSAndreas Gohr * Build an string of URL parameters
1372684e50aSAndreas Gohr *
1382684e50aSAndreas Gohr * @author Andreas Gohr
1392684e50aSAndreas Gohr */
140b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
1412684e50aSAndreas Gohr  $url = '';
1422684e50aSAndreas Gohr  $amp = false;
1432684e50aSAndreas Gohr  foreach($params as $key => $val){
144b174aeaeSchris    if($amp) $url .= $sep;
1452684e50aSAndreas Gohr
1462684e50aSAndreas Gohr    $url .= $key.'=';
147b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1482684e50aSAndreas Gohr    $amp = true;
1492684e50aSAndreas Gohr  }
1502684e50aSAndreas Gohr  return $url;
1512684e50aSAndreas Gohr}
1522684e50aSAndreas Gohr
1532684e50aSAndreas Gohr/**
1542684e50aSAndreas Gohr * Build an string of html tag attributes
1552684e50aSAndreas Gohr *
1567bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded
1577bff22c0SAndreas Gohr *
1582684e50aSAndreas Gohr * @author Andreas Gohr
1592684e50aSAndreas Gohr */
1602684e50aSAndreas Gohrfunction buildAttributes($params){
1612684e50aSAndreas Gohr  $url = '';
1622684e50aSAndreas Gohr  foreach($params as $key => $val){
1637bff22c0SAndreas Gohr    if($key{0} == '_') continue;
1647bff22c0SAndreas Gohr
1652684e50aSAndreas Gohr    $url .= $key.'="';
1662684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1672684e50aSAndreas Gohr    $url .= '" ';
1682684e50aSAndreas Gohr  }
1692684e50aSAndreas Gohr  return $url;
1702684e50aSAndreas Gohr}
1712684e50aSAndreas Gohr
1722684e50aSAndreas Gohr
1732684e50aSAndreas Gohr/**
17415fae107Sandi * This builds the breadcrumb trail and returns it as array
17515fae107Sandi *
17615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
177f3f0262cSandi */
178f3f0262cSandifunction breadcrumbs(){
1798746e727Sandi  // we prepare the breadcrumbs early for quick session closing
1808746e727Sandi  static $crumbs = null;
1818746e727Sandi  if($crumbs != null) return $crumbs;
1828746e727Sandi
183f3f0262cSandi  global $ID;
184f3f0262cSandi  global $ACT;
185f3f0262cSandi  global $conf;
186*e71ce681SAndreas Gohr  $crumbs = $_SESSION[DOKU_COOKIE]['bc'];
187f3f0262cSandi
188f3f0262cSandi  //first visit?
189f3f0262cSandi  if (!is_array($crumbs)){
190f3f0262cSandi    $crumbs = array();
191f3f0262cSandi  }
192f3f0262cSandi  //we only save on show and existing wiki documents
193a77f5846Sjan  $file = wikiFN($ID);
194a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
195*e71ce681SAndreas Gohr    $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
196f3f0262cSandi    return $crumbs;
197f3f0262cSandi  }
198a77f5846Sjan
199a77f5846Sjan  // page names
200a77f5846Sjan  $name = noNS($ID);
201a77f5846Sjan  if ($conf['useheading']) {
202a77f5846Sjan    // get page title
203bb0a59d4Sjan    $title = p_get_first_heading($ID);
204a77f5846Sjan    if ($title) {
205a77f5846Sjan      $name = $title;
206a77f5846Sjan    }
207a77f5846Sjan  }
208a77f5846Sjan
209f3f0262cSandi  //remove ID from array
210a77f5846Sjan  if (isset($crumbs[$ID])) {
211a77f5846Sjan    unset($crumbs[$ID]);
212f3f0262cSandi  }
213f3f0262cSandi
214f3f0262cSandi  //add to array
215a77f5846Sjan  $crumbs[$ID] = $name;
216f3f0262cSandi  //reduce size
217f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
218f3f0262cSandi    array_shift($crumbs);
219f3f0262cSandi  }
220f3f0262cSandi  //save to session
221*e71ce681SAndreas Gohr  $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
222f3f0262cSandi  return $crumbs;
223f3f0262cSandi}
224f3f0262cSandi
225f3f0262cSandi/**
22615fae107Sandi * Filter for page IDs
22715fae107Sandi *
228f3f0262cSandi * This is run on a ID before it is outputted somewhere
229f3f0262cSandi * currently used to replace the colon with something else
230f3f0262cSandi * on Windows systems and to have proper URL encoding
23115fae107Sandi *
23249c713a3Sandi * Urlencoding is ommitted when the second parameter is false
23349c713a3Sandi *
23415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
235f3f0262cSandi */
23649c713a3Sandifunction idfilter($id,$ue=true){
237f3f0262cSandi  global $conf;
238f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
239f3f0262cSandi    $id = strtr($id,':','/');
240f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
241f3f0262cSandi      $conf['userewrite']) {
242f3f0262cSandi    $id = strtr($id,':',';');
243f3f0262cSandi  }
24449c713a3Sandi  if($ue){
245b6c6979fSAndreas Gohr    $id = rawurlencode($id);
246f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
247f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
24849c713a3Sandi  }
249f3f0262cSandi  return $id;
250f3f0262cSandi}
251f3f0262cSandi
252f3f0262cSandi/**
253ed7b5f09Sandi * This builds a link to a wikipage
25415fae107Sandi *
2556c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2566c7843b5Sandi * given in $more
2576c7843b5Sandi *
25815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
259f3f0262cSandi */
260b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
261f3f0262cSandi  global $conf;
2626de3759aSAndreas Gohr  if(is_array($more)){
263b174aeaeSchris    $more = buildURLparams($more,$sep);
2646de3759aSAndreas Gohr  }else{
265b174aeaeSchris    $more = str_replace(',',$sep,$more);
2666de3759aSAndreas Gohr  }
267f3f0262cSandi
268f3f0262cSandi  $id    = idfilter($id);
269ed7b5f09Sandi  if($abs){
270ed7b5f09Sandi    $xlink = DOKU_URL;
271ed7b5f09Sandi  }else{
272ed7b5f09Sandi    $xlink = DOKU_BASE;
273ed7b5f09Sandi  }
274f3f0262cSandi
2756c7843b5Sandi  if($conf['userewrite'] == 2){
2766c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
2776c7843b5Sandi    if($more) $xlink .= '?'.$more;
2786c7843b5Sandi  }elseif($conf['userewrite']){
279f3f0262cSandi    $xlink .= $id;
280f3f0262cSandi    if($more) $xlink .= '?'.$more;
2816c7843b5Sandi  }else{
2826c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
283b174aeaeSchris    if($more) $xlink .= $sep.$more;
284f3f0262cSandi  }
285f3f0262cSandi
286f3f0262cSandi  return $xlink;
287f3f0262cSandi}
288f3f0262cSandi
289f3f0262cSandi/**
290f5c2808fSBen Coburn * This builds a link to an alternate page format
291f5c2808fSBen Coburn *
292f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
293f5c2808fSBen Coburn *
294f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
295f5c2808fSBen Coburn */
296f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
297f5c2808fSBen Coburn  global $conf;
298f5c2808fSBen Coburn  if(is_array($more)){
299f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
300f5c2808fSBen Coburn  }else{
301f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
302f5c2808fSBen Coburn  }
303f5c2808fSBen Coburn
304f5c2808fSBen Coburn  $format = rawurlencode($format);
305f5c2808fSBen Coburn  $id = idfilter($id);
306f5c2808fSBen Coburn  if($abs){
307f5c2808fSBen Coburn    $xlink = DOKU_URL;
308f5c2808fSBen Coburn  }else{
309f5c2808fSBen Coburn    $xlink = DOKU_BASE;
310f5c2808fSBen Coburn  }
311f5c2808fSBen Coburn
312f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
313f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
314f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
315f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
316f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
317f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
318f5c2808fSBen Coburn  }else{
319f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
320f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
321f5c2808fSBen Coburn  }
322f5c2808fSBen Coburn
323f5c2808fSBen Coburn  return $xlink;
324f5c2808fSBen Coburn}
325f5c2808fSBen Coburn
326f5c2808fSBen Coburn/**
3276de3759aSAndreas Gohr * Build a link to a media file
3286de3759aSAndreas Gohr *
3296de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
3306de3759aSAndreas Gohr */
331b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
3326de3759aSAndreas Gohr  global $conf;
3336de3759aSAndreas Gohr  if(is_array($more)){
334b174aeaeSchris    $more = buildURLparams($more,$sep);
3356de3759aSAndreas Gohr  }else{
336b174aeaeSchris    $more = str_replace(',',$sep,$more);
3376de3759aSAndreas Gohr  }
3386de3759aSAndreas Gohr
3396de3759aSAndreas Gohr  $xlink = DOKU_BASE;
3406de3759aSAndreas Gohr
3416de3759aSAndreas Gohr  // external URLs are always direct without rewriting
3426de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
3436de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
3446de3759aSAndreas Gohr    if($more){
3456de3759aSAndreas Gohr      $xlink .= '?'.$more;
346b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3476de3759aSAndreas Gohr    }else{
348b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3496de3759aSAndreas Gohr    }
3506de3759aSAndreas Gohr    return $xlink;
3516de3759aSAndreas Gohr  }
3526de3759aSAndreas Gohr
3536de3759aSAndreas Gohr  $id = idfilter($id);
3546de3759aSAndreas Gohr
3556de3759aSAndreas Gohr  // decide on scriptname
3566de3759aSAndreas Gohr  if($direct){
3576de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3586de3759aSAndreas Gohr      $script = '_media';
3596de3759aSAndreas Gohr    }else{
3606de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3616de3759aSAndreas Gohr    }
3626de3759aSAndreas Gohr  }else{
3636de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3646de3759aSAndreas Gohr      $script = '_detail';
3656de3759aSAndreas Gohr    }else{
3666de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3676de3759aSAndreas Gohr    }
3686de3759aSAndreas Gohr  }
3696de3759aSAndreas Gohr
3706de3759aSAndreas Gohr  // build URL based on rewrite mode
3716de3759aSAndreas Gohr   if($conf['userewrite']){
3726de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3736de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3746de3759aSAndreas Gohr   }else{
3756de3759aSAndreas Gohr     if($more){
376a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
377b174aeaeSchris       $xlink .= $sep.'media='.$id;
3786de3759aSAndreas Gohr     }else{
379a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
3806de3759aSAndreas Gohr     }
3816de3759aSAndreas Gohr   }
3826de3759aSAndreas Gohr
3836de3759aSAndreas Gohr  return $xlink;
3846de3759aSAndreas Gohr}
3856de3759aSAndreas Gohr
3866de3759aSAndreas Gohr
3876de3759aSAndreas Gohr
3886de3759aSAndreas Gohr/**
389f3f0262cSandi * Just builds a link to a script
39015fae107Sandi *
391ed7b5f09Sandi * @todo   maybe obsolete
39215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
393f3f0262cSandi */
394f3f0262cSandifunction script($script='doku.php'){
395ed7b5f09Sandi#  $link = getBaseURL();
396ed7b5f09Sandi#  $link .= $script;
397ed7b5f09Sandi#  return $link;
398ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
399f3f0262cSandi}
400f3f0262cSandi
401f3f0262cSandi/**
40215fae107Sandi * Spamcheck against wordlist
40315fae107Sandi *
404f3f0262cSandi * Checks the wikitext against a list of blocked expressions
405f3f0262cSandi * returns true if the text contains any bad words
40615fae107Sandi *
40715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
408f3f0262cSandi */
409f3f0262cSandifunction checkwordblock(){
410f3f0262cSandi  global $TEXT;
411f3f0262cSandi  global $conf;
412f3f0262cSandi
413f3f0262cSandi  if(!$conf['usewordblock']) return false;
414f3f0262cSandi
415041d1964SAndreas Gohr  // we prepare the text a tiny bit to prevent spammers circumventing URL checks
416041d1964SAndreas Gohr  $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i','\1http://\2 \2\3',$TEXT);
417041d1964SAndreas Gohr
418b9ac8716Schris  $wordblocks = getWordblocks();
4193e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
4203e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
4213e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
4223e2965d7Sandi    //backreferences are used - the maximum is 99
4233e2965d7Sandi    //this is very bad performancewise and may even be too high still
4243e2965d7Sandi    $chunksize = 40;
4253e2965d7Sandi  }else{
426703f6fdeSandi    //read file in chunks of 600 - this should work around the
4273e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
428444b87a5SAndreas Gohr    $chunksize = 400;
4293e2965d7Sandi  }
430b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
431f3f0262cSandi    $re = array();
432f3f0262cSandi    #build regexp from blocks
433f3f0262cSandi    foreach($blocks as $block){
434f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
435f3f0262cSandi      $block = trim($block);
436f3f0262cSandi      if(empty($block)) continue;
437f3f0262cSandi      $re[]  = $block;
438f3f0262cSandi    }
439041d1964SAndreas Gohr    if(preg_match('#('.join('|',$re).')#si',$text, $match=array())) {
440b9ac8716Schris      return true;
441b9ac8716Schris    }
442703f6fdeSandi  }
443f3f0262cSandi  return false;
444f3f0262cSandi}
445f3f0262cSandi
446f3f0262cSandi/**
44715fae107Sandi * Return the IP of the client
44815fae107Sandi *
4496d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers
45015fae107Sandi *
4516d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned
4526d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return
4536d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X
4546d8affe6SAndreas Gohr * headers
4556d8affe6SAndreas Gohr *
4566d8affe6SAndreas Gohr * @param  boolean $single If set only a single IP is returned
45715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
458f3f0262cSandi */
4596d8affe6SAndreas Gohrfunction clientIP($single=false){
4606d8affe6SAndreas Gohr  $ip = array();
4616d8affe6SAndreas Gohr  $ip[] = $_SERVER['REMOTE_ADDR'];
462bb4866bdSchris  if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
4636d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
464bb4866bdSchris  if(!empty($_SERVER['HTTP_X_REAL_IP']))
4656d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
4666d8affe6SAndreas Gohr
4676d8affe6SAndreas Gohr  // remove any non-IP stuff
4686d8affe6SAndreas Gohr  $cnt = count($ip);
4694ff28443Schris  $match = array();
4706d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
4714ff28443Schris    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
4724ff28443Schris      $ip[$i] = $match[0];
4734ff28443Schris    } else {
4744ff28443Schris      $ip[$i] = '';
4754ff28443Schris    }
4766d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
477f3f0262cSandi  }
4786d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
4796d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
4806d8affe6SAndreas Gohr
4816d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
4826d8affe6SAndreas Gohr
4836d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
4846d8affe6SAndreas Gohr  $ip = array_reverse($ip);
4856d8affe6SAndreas Gohr  foreach($ip as $i){
4866d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
4876d8affe6SAndreas Gohr      continue;
4886d8affe6SAndreas Gohr    }else{
4896d8affe6SAndreas Gohr      return $i;
4906d8affe6SAndreas Gohr    }
4916d8affe6SAndreas Gohr  }
4926d8affe6SAndreas Gohr  // still here? just use the first (last) address
4936d8affe6SAndreas Gohr  return $ip[0];
494f3f0262cSandi}
495f3f0262cSandi
496f3f0262cSandi/**
49715fae107Sandi * Checks if a given page is currently locked.
49815fae107Sandi *
499f3f0262cSandi * removes stale lockfiles
50015fae107Sandi *
50115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
502f3f0262cSandi */
503f3f0262cSandifunction checklock($id){
504f3f0262cSandi  global $conf;
505c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
506f3f0262cSandi
507f3f0262cSandi  //no lockfile
508f3f0262cSandi  if(!@file_exists($lock)) return false;
509f3f0262cSandi
510f3f0262cSandi  //lockfile expired
511f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
512d8186216SBen Coburn    @unlink($lock);
513f3f0262cSandi    return false;
514f3f0262cSandi  }
515f3f0262cSandi
516f3f0262cSandi  //my own lock
517f3f0262cSandi  $ip = io_readFile($lock);
518f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
519f3f0262cSandi    return false;
520f3f0262cSandi  }
521f3f0262cSandi
522f3f0262cSandi  return $ip;
523f3f0262cSandi}
524f3f0262cSandi
525f3f0262cSandi/**
52615fae107Sandi * Lock a page for editing
52715fae107Sandi *
52815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
529f3f0262cSandi */
530f3f0262cSandifunction lock($id){
531c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
532f3f0262cSandi  if($_SERVER['REMOTE_USER']){
533f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
534f3f0262cSandi  }else{
535f3f0262cSandi    io_saveFile($lock,clientIP());
536f3f0262cSandi  }
537f3f0262cSandi}
538f3f0262cSandi
539f3f0262cSandi/**
54015fae107Sandi * Unlock a page if it was locked by the user
541f3f0262cSandi *
54215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
54315fae107Sandi * @return bool true if a lock was removed
544f3f0262cSandi */
545f3f0262cSandifunction unlock($id){
546c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
547f3f0262cSandi  if(@file_exists($lock)){
548f3f0262cSandi    $ip = io_readFile($lock);
549f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
550f3f0262cSandi      @unlink($lock);
551f3f0262cSandi      return true;
552f3f0262cSandi    }
553f3f0262cSandi  }
554f3f0262cSandi  return false;
555f3f0262cSandi}
556f3f0262cSandi
557f3f0262cSandi/**
558f3f0262cSandi * convert line ending to unix format
559f3f0262cSandi *
56015fae107Sandi * @see    formText() for 2crlf conversion
56115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
562f3f0262cSandi */
563f3f0262cSandifunction cleanText($text){
564f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
565f3f0262cSandi  return $text;
566f3f0262cSandi}
567f3f0262cSandi
568f3f0262cSandi/**
569f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
570f3f0262cSandi * It also converts line endings to Windows format which is
571f3f0262cSandi * pseudo standard for webforms.
572f3f0262cSandi *
57315fae107Sandi * @see    cleanText() for 2unix conversion
57415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
575f3f0262cSandi */
576f3f0262cSandifunction formText($text){
577f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
578f3f0262cSandi  return htmlspecialchars($text);
579f3f0262cSandi}
580f3f0262cSandi
581f3f0262cSandi/**
58215fae107Sandi * Returns the specified local text in raw format
58315fae107Sandi *
58415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
585f3f0262cSandi */
586f3f0262cSandifunction rawLocale($id){
587f3f0262cSandi  return io_readFile(localeFN($id));
588f3f0262cSandi}
589f3f0262cSandi
590f3f0262cSandi/**
591f3f0262cSandi * Returns the raw WikiText
59215fae107Sandi *
59315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
594f3f0262cSandi */
595f3f0262cSandifunction rawWiki($id,$rev=''){
596cc7d0c94SBen Coburn  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
597f3f0262cSandi}
598f3f0262cSandi
599f3f0262cSandi/**
6007146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
6017146cee2SAndreas Gohr *
6027146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6037146cee2SAndreas Gohr */
604b7d5a5f0SAndreas Gohrfunction pageTemplate($data){
605b7d5a5f0SAndreas Gohr  $id = $data[0];
606a15ce62dSEsther Brunner  global $conf;
607a15ce62dSEsther Brunner  global $INFO;
608a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
609a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
610a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
611a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
612a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
613a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
614a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
615a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
616a15ce62dSEsther Brunner  return $tpl;
6177146cee2SAndreas Gohr}
6187146cee2SAndreas Gohr
6197146cee2SAndreas Gohr
6207146cee2SAndreas Gohr/**
62115fae107Sandi * Returns the raw Wiki Text in three slices.
62215fae107Sandi *
62315fae107Sandi * The range parameter needs to have the form "from-to"
62415cfe303Sandi * and gives the range of the section in bytes - no
62515cfe303Sandi * UTF-8 awareness is needed.
626f3f0262cSandi * The returned order is prefix, section and suffix.
62715fae107Sandi *
62815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
629f3f0262cSandi */
630f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
631f3f0262cSandi  list($from,$to) = split('-',$range,2);
632cc7d0c94SBen Coburn  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
633f3f0262cSandi  if(!$from) $from = 0;
634c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
635f3f0262cSandi
63615cfe303Sandi  $slices[0] = substr($text,0,$from-1);
63715cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
63815cfe303Sandi  $slices[2] = substr($text,$to);
639f3f0262cSandi
640f3f0262cSandi  return $slices;
641f3f0262cSandi}
642f3f0262cSandi
643f3f0262cSandi/**
64415fae107Sandi * Joins wiki text slices
64515fae107Sandi *
646f3f0262cSandi * function to join the text slices with correct lineendings again.
647f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
648f3f0262cSandi * lines between sections if needed (used on saving).
64915fae107Sandi *
65015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
651f3f0262cSandi */
652f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
653f3f0262cSandi
654f3f0262cSandi  if($pretty){
655f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
656f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
657f3f0262cSandi  }
658f3f0262cSandi
659f3f0262cSandi  if($pre) $pre .= "\n";
660f3f0262cSandi  if($suf) $text .= "\n";
661f3f0262cSandi  return $pre.$text.$suf;
662f3f0262cSandi}
663f3f0262cSandi
664f3f0262cSandi/**
665cc7d0c94SBen Coburn * Saves a wikitext by calling io_writeWikiPage
66615fae107Sandi *
66715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
66871726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
669f3f0262cSandi */
670b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
671f3f0262cSandi  global $conf;
672f3f0262cSandi  global $lang;
67371726d78SBen Coburn  global $REV;
674f3f0262cSandi  // ignore if no changes were made
675f3f0262cSandi  if($text == rawWiki($id,'')){
676f3f0262cSandi    return;
677f3f0262cSandi  }
678f3f0262cSandi
679f3f0262cSandi  $file = wikiFN($id);
680f3f0262cSandi  $old  = saveOldRevision($id);
68171726d78SBen Coburn  $wasRemoved = empty($text);
682d8186216SBen Coburn  $wasCreated = !@file_exists($file);
68371726d78SBen Coburn  $wasReverted = ($REV==true);
684e45b34cdSBen Coburn  $newRev = false;
685f3f0262cSandi
68671726d78SBen Coburn  if ($wasRemoved){
687e45b34cdSBen Coburn    // pre-save deleted revision
688e45b34cdSBen Coburn    @touch($file);
689e45b34cdSBen Coburn    $newRev = saveOldRevision($id);
690e1f3d9e1SEsther Brunner    // remove empty file
691f3f0262cSandi    @unlink($file);
69271726d78SBen Coburn    // remove old meta info...
693e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
69471726d78SBen Coburn    $changelog = metaFN($id, '.changes');
695e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
69671726d78SBen Coburn      // but keep per-page changelog to preserve page history
697d8186216SBen Coburn      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
698b158d625SSteven Danz    }
699f3f0262cSandi    $del = true;
7003ce054b3Sandi    // autoset summary on deletion
7013ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
70253d6ccfeSandi    // remove empty namespaces
703cc7d0c94SBen Coburn    io_sweepNS($id, 'datadir');
704cc7d0c94SBen Coburn    io_sweepNS($id, 'mediadir');
705f3f0262cSandi  }else{
706cc7d0c94SBen Coburn    // save file (namespace dir is created in io_writeWikiPage)
707cc7d0c94SBen Coburn    io_writeWikiPage($file, $text, $id);
708e45b34cdSBen Coburn    $newRev = @filemtime($file);
709f3f0262cSandi    $del = false;
710f3f0262cSandi  }
711f3f0262cSandi
71271726d78SBen Coburn  // select changelog line type
71371726d78SBen Coburn  $extra = '';
71471726d78SBen Coburn  $type = 'E';
71571726d78SBen Coburn  if ($wasReverted) {
71671726d78SBen Coburn    $type = 'R';
71771726d78SBen Coburn    $extra = $REV;
71871726d78SBen Coburn  }
71971726d78SBen Coburn  else if ($wasCreated) { $type = 'C'; }
72071726d78SBen Coburn  else if ($wasRemoved) { $type = 'D'; }
72171726d78SBen Coburn  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
72271726d78SBen Coburn
723e45b34cdSBen Coburn  addLogEntry($newRev, $id, $type, $summary, $extra);
72426a0801fSAndreas Gohr  // send notify mails
72590033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
72690033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
727f3f0262cSandi
728ce6b63d9Schris  // update the purgefile (timestamp of the last time anything within the wiki was changed)
72998407a7aSandi  io_saveFile($conf['cachedir'].'/purgefile',time());
730f3f0262cSandi}
731f3f0262cSandi
732f3f0262cSandi/**
733f3f0262cSandi * moves the current version to the attic and returns its
734f3f0262cSandi * revision date
73515fae107Sandi *
73615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
737f3f0262cSandi */
738f3f0262cSandifunction saveOldRevision($id){
739f3f0262cSandi  global $conf;
740f3f0262cSandi  $oldf = wikiFN($id);
741f3f0262cSandi  if(!@file_exists($oldf)) return '';
742f3f0262cSandi  $date = filemtime($oldf);
743f3f0262cSandi  $newf = wikiFN($id,$date);
744cc7d0c94SBen Coburn  io_writeWikiPage($newf, rawWiki($id), $id, $date);
745f3f0262cSandi  return $date;
746f3f0262cSandi}
747f3f0262cSandi
748f3f0262cSandi/**
74926a0801fSAndreas Gohr * Sends a notify mail on page change
75026a0801fSAndreas Gohr *
75126a0801fSAndreas Gohr * @param  string  $id       The changed page
75226a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
75326a0801fSAndreas Gohr * @param  int     $rev      Old page revision
75426a0801fSAndreas Gohr * @param  string  $summary  What changed
75590033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
75602a498e7Schris * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
75715fae107Sandi *
75815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
759f3f0262cSandi */
76002a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
761f3f0262cSandi  global $lang;
762f3f0262cSandi  global $conf;
763b158d625SSteven Danz
76426a0801fSAndreas Gohr  // decide if there is something to do
76526a0801fSAndreas Gohr  if($who == 'admin'){
76626a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
767f3f0262cSandi    $text = rawLocale('mailtext');
76826a0801fSAndreas Gohr    $to   = $conf['notify'];
76926a0801fSAndreas Gohr    $bcc  = '';
77026a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
77126a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
77290033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
77326a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
77426a0801fSAndreas Gohr    if(empty($bcc)) return;
77526a0801fSAndreas Gohr    $to   = '';
77626a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
777a06e4bdbSSebastian Harl  }elseif($who == 'register'){
778a06e4bdbSSebastian Harl    if(empty($conf['registernotify'])) return;
779a06e4bdbSSebastian Harl    $text = rawLocale('registermail');
780a06e4bdbSSebastian Harl    $to   = $conf['registernotify'];
781a06e4bdbSSebastian Harl    $bcc  = '';
78226a0801fSAndreas Gohr  }else{
78326a0801fSAndreas Gohr    return; //just to be safe
78426a0801fSAndreas Gohr  }
78526a0801fSAndreas Gohr
786f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
787f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
788f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
789f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
790ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
79126a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
79226a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
793ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
794f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
7957a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
796f3f0262cSandi
79702a498e7Schris  foreach ($replace as $key => $substitution) {
79802a498e7Schris    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
79902a498e7Schris  }
80002a498e7Schris
801a06e4bdbSSebastian Harl  if($who == 'register'){
802a06e4bdbSSebastian Harl    $subject = $lang['mail_new_user'].' '.$summary;
803a06e4bdbSSebastian Harl  }elseif($rev){
804f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
805ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
806ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
807f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
808f3f0262cSandi                    split("\n",rawWiki($id)));
809f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
810f3f0262cSandi    $diff    = $dformat->format($df);
811f3f0262cSandi  }else{
812f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
813f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
814f3f0262cSandi    $diff = rawWiki($id);
815f3f0262cSandi  }
816f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
817241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
818f3f0262cSandi
81926a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
820f3f0262cSandi}
821f3f0262cSandi
82215fae107Sandi/**
823f3f0262cSandi * extracts the query from a google referer
82415fae107Sandi *
8256b13307fSandi * @todo   should be more generic and support yahoo et al
82615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
827f3f0262cSandi */
828f3f0262cSandifunction getGoogleQuery(){
829f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
8305c3f206fSandi  if(!$url) return '';
831f3f0262cSandi
832f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
833f3f0262cSandi  $query = array();
834f3f0262cSandi  parse_str($url['query'],$query);
835f3f0262cSandi
836f3f0262cSandi  return $query['q'];
837f3f0262cSandi}
838f3f0262cSandi
839f3f0262cSandi/**
84015fae107Sandi * Try to set correct locale
84115fae107Sandi *
842095bfd5cSandi * @deprecated No longer used
84315fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
844f3f0262cSandi */
845f3f0262cSandifunction setCorrectLocale(){
846f3f0262cSandi  global $conf;
847f3f0262cSandi  global $lang;
848f3f0262cSandi
849f3f0262cSandi  $enc = strtoupper($lang['encoding']);
850f3f0262cSandi  foreach ($lang['locales'] as $loc){
851f3f0262cSandi    //try locale
852f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
853f3f0262cSandi    //try loceale with encoding
854f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
855f3f0262cSandi  }
856f3f0262cSandi  //still here? try to set from environment
857f3f0262cSandi  @setlocale(LC_ALL,"");
858f3f0262cSandi}
859f3f0262cSandi
860f3f0262cSandi/**
861f3f0262cSandi * Return the human readable size of a file
862f3f0262cSandi *
863f3f0262cSandi * @param       int    $size   A file size
864f3f0262cSandi * @param       int    $dec    A number of decimal places
865f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
866f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
867f3f0262cSandi * @version     1.0.0
868f3f0262cSandi */
869f31d5b73Sandifunction filesize_h($size, $dec = 1){
870f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
871f3f0262cSandi  $count = count($sizes);
872f3f0262cSandi  $i = 0;
873f3f0262cSandi
874f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
875f3f0262cSandi    $size /= 1024;
876f3f0262cSandi    $i++;
877f3f0262cSandi  }
878f3f0262cSandi
879f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
880f3f0262cSandi}
881f3f0262cSandi
88215fae107Sandi/**
88300a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
88400a7b5adSEsther Brunner *
88500a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
88600a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
88700a7b5adSEsther Brunner */
88800a7b5adSEsther Brunnerfunction obfuscate($email) {
88900a7b5adSEsther Brunner  global $conf;
89000a7b5adSEsther Brunner
89100a7b5adSEsther Brunner  switch ($conf['mailguard']) {
89200a7b5adSEsther Brunner    case 'visible' :
89300a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
89400a7b5adSEsther Brunner      return strtr($email, $obfuscate);
89500a7b5adSEsther Brunner
89600a7b5adSEsther Brunner    case 'hex' :
89700a7b5adSEsther Brunner      $encode = '';
89800a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
89900a7b5adSEsther Brunner      return $encode;
90000a7b5adSEsther Brunner
90100a7b5adSEsther Brunner    case 'none' :
90200a7b5adSEsther Brunner    default :
90300a7b5adSEsther Brunner      return $email;
90400a7b5adSEsther Brunner  }
90500a7b5adSEsther Brunner}
90600a7b5adSEsther Brunner
90700a7b5adSEsther Brunner/**
908b158d625SSteven Danz * Let us know if a user is tracking a page
909b158d625SSteven Danz *
9101380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
911b158d625SSteven Danz */
9121380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
9131380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
9141380fc45SAndreas Gohr  if (@file_exists($file)) {
915b158d625SSteven Danz    $mlist = file($file);
9161380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
9171380fc45SAndreas Gohr    return is_int($pos);
918b158d625SSteven Danz  }
9191380fc45SAndreas Gohr
920b158d625SSteven Danz  return false;
921b158d625SSteven Danz}
922340756e4Sandi
923f9eb5648Ssteven-danz/**
924f9eb5648Ssteven-danz * Return a string with the email addresses of all the
925f9eb5648Ssteven-danz * users subscribed to a page
926f9eb5648Ssteven-danz *
92726a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
928f9eb5648Ssteven-danz */
929f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
930f9eb5648Ssteven-danz  global $conf;
931cd52f92dSchris  global $auth;
932f9eb5648Ssteven-danz
933f9eb5648Ssteven-danz  $emails = '';
934f9eb5648Ssteven-danz
93526a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
93626a0801fSAndreas Gohr
937f9eb5648Ssteven-danz  $mlist = array();
938f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
939d8186216SBen Coburn  if (@file_exists($file)) {
940f9eb5648Ssteven-danz    $mlist = file($file);
941f9eb5648Ssteven-danz  }
942f9eb5648Ssteven-danz  if(count($mlist) > 0) {
943f9eb5648Ssteven-danz    foreach ($mlist as $who) {
944f9eb5648Ssteven-danz      $who = rtrim($who);
945cd52f92dSchris      $info = $auth->getUserData($who);
946f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
947f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
948f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
949f9eb5648Ssteven-danz          if (empty($emails)) {
950f9eb5648Ssteven-danz            $emails = $info['mail'];
951f9eb5648Ssteven-danz          } else {
952f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
953f9eb5648Ssteven-danz          }
954f9eb5648Ssteven-danz        }
955f9eb5648Ssteven-danz      }
956f9eb5648Ssteven-danz    }
957f9eb5648Ssteven-danz  }
958f9eb5648Ssteven-danz
959f9eb5648Ssteven-danz  return $emails;
960f9eb5648Ssteven-danz}
961f9eb5648Ssteven-danz
96289541d4bSAndreas Gohr/**
96389541d4bSAndreas Gohr * Removes quoting backslashes
96489541d4bSAndreas Gohr *
96589541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
96689541d4bSAndreas Gohr */
96789541d4bSAndreas Gohrfunction unslash($string,$char="'"){
96889541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
96989541d4bSAndreas Gohr}
97089541d4bSAndreas Gohr
971340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
972