xref: /dokuwiki/inc/common.php (revision 6afe8dca1f7bd2a9ca21fb2dca6fef12ded423e1)
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
59*6afe8dcaSchris  // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml
60*6afe8dcaSchris  // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary
61*6afe8dcaSchris  $info['id'] = $ID;
62*6afe8dcaSchris  $info['rev'] = $REV;
63*6afe8dcaSchris
64f3f0262cSandi  if($_SERVER['REMOTE_USER']){
65f3f0262cSandi    $info['userinfo']   = $USERINFO;
66f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
671380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
68ee4c4a1bSAndreas Gohr    $info['client']     = $_SERVER['REMOTE_USER'];
6917ee7f66SAndreas Gohr
7017ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
7117ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
7217ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
7317ee7f66SAndreas Gohr    }
74ee4c4a1bSAndreas Gohr
75f3f0262cSandi  }else{
76f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
771380fc45SAndreas Gohr    $info['subscribed'] = false;
78ee4c4a1bSAndreas Gohr    $info['client']     = clientIP(true);
79f3f0262cSandi  }
80f3f0262cSandi
81f3f0262cSandi  $info['namespace'] = getNS($ID);
82f3f0262cSandi  $info['locked']    = checklock($ID);
83f3f0262cSandi  $info['filepath']  = realpath(wikiFN($ID,$REV));
84f3f0262cSandi  $info['exists']    = @file_exists($info['filepath']);
85f3f0262cSandi  if($REV && !$info['exists']){
86f3f0262cSandi    //check if current revision was meant
87f3f0262cSandi    $cur = wikiFN($ID);
88f3f0262cSandi    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
89f3f0262cSandi      $info['filepath'] = realpath($cur);
90f3f0262cSandi      $info['exists']   = true;
91f3f0262cSandi      $REV = '';
92f3f0262cSandi    }
93f3f0262cSandi  }
94c112d578Sandi  $info['rev'] = $REV;
95f3f0262cSandi  if($info['exists']){
96f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
97f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
98f3f0262cSandi  }else{
99f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
100f3f0262cSandi  }
101f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
102f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
103f3f0262cSandi
10471726d78SBen Coburn  //load page meta data
10571726d78SBen Coburn  $info['meta'] = p_get_metadata($ID);
10671726d78SBen Coburn
107652610a2Sandi  //who's the editor
108652610a2Sandi  if($REV){
10971726d78SBen Coburn    $revinfo = getRevisionInfo($ID, $REV, 1024);
110652610a2Sandi  }else{
111bb4866bdSchris    $revinfo = isset($info['meta']['last_change']) ? $info['meta']['last_change'] : getRevisionInfo($ID,$info['lastmod'],1024);
112652610a2Sandi  }
113bb4866bdSchris
114652610a2Sandi  $info['ip']     = $revinfo['ip'];
115652610a2Sandi  $info['user']   = $revinfo['user'];
116652610a2Sandi  $info['sum']    = $revinfo['sum'];
11771726d78SBen Coburn  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
11871726d78SBen Coburn  // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor'].
11959f257aeSchris
12088f522e9Sandi  if($revinfo['user']){
12188f522e9Sandi    $info['editor'] = $revinfo['user'];
12288f522e9Sandi  }else{
12388f522e9Sandi    $info['editor'] = $revinfo['ip'];
12488f522e9Sandi  }
125652610a2Sandi
126ee4c4a1bSAndreas Gohr  // draft
127ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
128ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
129ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
130ee4c4a1bSAndreas Gohr      // remove stale draft
131ee4c4a1bSAndreas Gohr      @unlink($draft);
132ee4c4a1bSAndreas Gohr    }else{
133ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
134ee4c4a1bSAndreas Gohr    }
135ee4c4a1bSAndreas Gohr  }
136ee4c4a1bSAndreas Gohr
137f3f0262cSandi  return $info;
138f3f0262cSandi}
139f3f0262cSandi
140f3f0262cSandi/**
1412684e50aSAndreas Gohr * Build an string of URL parameters
1422684e50aSAndreas Gohr *
1432684e50aSAndreas Gohr * @author Andreas Gohr
1442684e50aSAndreas Gohr */
145b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
1462684e50aSAndreas Gohr  $url = '';
1472684e50aSAndreas Gohr  $amp = false;
1482684e50aSAndreas Gohr  foreach($params as $key => $val){
149b174aeaeSchris    if($amp) $url .= $sep;
1502684e50aSAndreas Gohr
1512684e50aSAndreas Gohr    $url .= $key.'=';
152b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1532684e50aSAndreas Gohr    $amp = true;
1542684e50aSAndreas Gohr  }
1552684e50aSAndreas Gohr  return $url;
1562684e50aSAndreas Gohr}
1572684e50aSAndreas Gohr
1582684e50aSAndreas Gohr/**
1592684e50aSAndreas Gohr * Build an string of html tag attributes
1602684e50aSAndreas Gohr *
1617bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded
1627bff22c0SAndreas Gohr *
1632684e50aSAndreas Gohr * @author Andreas Gohr
1642684e50aSAndreas Gohr */
1652684e50aSAndreas Gohrfunction buildAttributes($params){
1662684e50aSAndreas Gohr  $url = '';
1672684e50aSAndreas Gohr  foreach($params as $key => $val){
1687bff22c0SAndreas Gohr    if($key{0} == '_') continue;
1697bff22c0SAndreas Gohr
1702684e50aSAndreas Gohr    $url .= $key.'="';
1712684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1722684e50aSAndreas Gohr    $url .= '" ';
1732684e50aSAndreas Gohr  }
1742684e50aSAndreas Gohr  return $url;
1752684e50aSAndreas Gohr}
1762684e50aSAndreas Gohr
1772684e50aSAndreas Gohr
1782684e50aSAndreas Gohr/**
17915fae107Sandi * This builds the breadcrumb trail and returns it as array
18015fae107Sandi *
18115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
182f3f0262cSandi */
183f3f0262cSandifunction breadcrumbs(){
1848746e727Sandi  // we prepare the breadcrumbs early for quick session closing
1858746e727Sandi  static $crumbs = null;
1868746e727Sandi  if($crumbs != null) return $crumbs;
1878746e727Sandi
188f3f0262cSandi  global $ID;
189f3f0262cSandi  global $ACT;
190f3f0262cSandi  global $conf;
191e71ce681SAndreas Gohr  $crumbs = $_SESSION[DOKU_COOKIE]['bc'];
192f3f0262cSandi
193f3f0262cSandi  //first visit?
194f3f0262cSandi  if (!is_array($crumbs)){
195f3f0262cSandi    $crumbs = array();
196f3f0262cSandi  }
197f3f0262cSandi  //we only save on show and existing wiki documents
198a77f5846Sjan  $file = wikiFN($ID);
199a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
200e71ce681SAndreas Gohr    $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
201f3f0262cSandi    return $crumbs;
202f3f0262cSandi  }
203a77f5846Sjan
204a77f5846Sjan  // page names
205a77f5846Sjan  $name = noNS($ID);
206a77f5846Sjan  if ($conf['useheading']) {
207a77f5846Sjan    // get page title
208bb0a59d4Sjan    $title = p_get_first_heading($ID);
209a77f5846Sjan    if ($title) {
210a77f5846Sjan      $name = $title;
211a77f5846Sjan    }
212a77f5846Sjan  }
213a77f5846Sjan
214f3f0262cSandi  //remove ID from array
215a77f5846Sjan  if (isset($crumbs[$ID])) {
216a77f5846Sjan    unset($crumbs[$ID]);
217f3f0262cSandi  }
218f3f0262cSandi
219f3f0262cSandi  //add to array
220a77f5846Sjan  $crumbs[$ID] = $name;
221f3f0262cSandi  //reduce size
222f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
223f3f0262cSandi    array_shift($crumbs);
224f3f0262cSandi  }
225f3f0262cSandi  //save to session
226e71ce681SAndreas Gohr  $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
227f3f0262cSandi  return $crumbs;
228f3f0262cSandi}
229f3f0262cSandi
230f3f0262cSandi/**
23115fae107Sandi * Filter for page IDs
23215fae107Sandi *
233f3f0262cSandi * This is run on a ID before it is outputted somewhere
234f3f0262cSandi * currently used to replace the colon with something else
235f3f0262cSandi * on Windows systems and to have proper URL encoding
23615fae107Sandi *
23749c713a3Sandi * Urlencoding is ommitted when the second parameter is false
23849c713a3Sandi *
23915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
240f3f0262cSandi */
24149c713a3Sandifunction idfilter($id,$ue=true){
242f3f0262cSandi  global $conf;
243f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
244f3f0262cSandi    $id = strtr($id,':','/');
245f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
246f3f0262cSandi      $conf['userewrite']) {
247f3f0262cSandi    $id = strtr($id,':',';');
248f3f0262cSandi  }
24949c713a3Sandi  if($ue){
250b6c6979fSAndreas Gohr    $id = rawurlencode($id);
251f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
252f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
25349c713a3Sandi  }
254f3f0262cSandi  return $id;
255f3f0262cSandi}
256f3f0262cSandi
257f3f0262cSandi/**
258ed7b5f09Sandi * This builds a link to a wikipage
25915fae107Sandi *
2606c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2616c7843b5Sandi * given in $more
2626c7843b5Sandi *
26315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
264f3f0262cSandi */
265b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
266f3f0262cSandi  global $conf;
2676de3759aSAndreas Gohr  if(is_array($more)){
268b174aeaeSchris    $more = buildURLparams($more,$sep);
2696de3759aSAndreas Gohr  }else{
270b174aeaeSchris    $more = str_replace(',',$sep,$more);
2716de3759aSAndreas Gohr  }
272f3f0262cSandi
273f3f0262cSandi  $id    = idfilter($id);
274ed7b5f09Sandi  if($abs){
275ed7b5f09Sandi    $xlink = DOKU_URL;
276ed7b5f09Sandi  }else{
277ed7b5f09Sandi    $xlink = DOKU_BASE;
278ed7b5f09Sandi  }
279f3f0262cSandi
2806c7843b5Sandi  if($conf['userewrite'] == 2){
2816c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
2826c7843b5Sandi    if($more) $xlink .= '?'.$more;
2836c7843b5Sandi  }elseif($conf['userewrite']){
284f3f0262cSandi    $xlink .= $id;
285f3f0262cSandi    if($more) $xlink .= '?'.$more;
2866c7843b5Sandi  }else{
2876c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
288b174aeaeSchris    if($more) $xlink .= $sep.$more;
289f3f0262cSandi  }
290f3f0262cSandi
291f3f0262cSandi  return $xlink;
292f3f0262cSandi}
293f3f0262cSandi
294f3f0262cSandi/**
295f5c2808fSBen Coburn * This builds a link to an alternate page format
296f5c2808fSBen Coburn *
297f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
298f5c2808fSBen Coburn *
299f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
300f5c2808fSBen Coburn */
301f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
302f5c2808fSBen Coburn  global $conf;
303f5c2808fSBen Coburn  if(is_array($more)){
304f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
305f5c2808fSBen Coburn  }else{
306f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
307f5c2808fSBen Coburn  }
308f5c2808fSBen Coburn
309f5c2808fSBen Coburn  $format = rawurlencode($format);
310f5c2808fSBen Coburn  $id = idfilter($id);
311f5c2808fSBen Coburn  if($abs){
312f5c2808fSBen Coburn    $xlink = DOKU_URL;
313f5c2808fSBen Coburn  }else{
314f5c2808fSBen Coburn    $xlink = DOKU_BASE;
315f5c2808fSBen Coburn  }
316f5c2808fSBen Coburn
317f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
318f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
319f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
320f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
321f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
322f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
323f5c2808fSBen Coburn  }else{
324f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
325f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
326f5c2808fSBen Coburn  }
327f5c2808fSBen Coburn
328f5c2808fSBen Coburn  return $xlink;
329f5c2808fSBen Coburn}
330f5c2808fSBen Coburn
331f5c2808fSBen Coburn/**
3326de3759aSAndreas Gohr * Build a link to a media file
3336de3759aSAndreas Gohr *
3346de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
3356de3759aSAndreas Gohr */
336b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
3376de3759aSAndreas Gohr  global $conf;
3386de3759aSAndreas Gohr  if(is_array($more)){
339b174aeaeSchris    $more = buildURLparams($more,$sep);
3406de3759aSAndreas Gohr  }else{
341b174aeaeSchris    $more = str_replace(',',$sep,$more);
3426de3759aSAndreas Gohr  }
3436de3759aSAndreas Gohr
3446de3759aSAndreas Gohr  $xlink = DOKU_BASE;
3456de3759aSAndreas Gohr
3466de3759aSAndreas Gohr  // external URLs are always direct without rewriting
3476de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
3486de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
3496de3759aSAndreas Gohr    if($more){
3506de3759aSAndreas Gohr      $xlink .= '?'.$more;
351b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3526de3759aSAndreas Gohr    }else{
353b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3546de3759aSAndreas Gohr    }
3556de3759aSAndreas Gohr    return $xlink;
3566de3759aSAndreas Gohr  }
3576de3759aSAndreas Gohr
3586de3759aSAndreas Gohr  $id = idfilter($id);
3596de3759aSAndreas Gohr
3606de3759aSAndreas Gohr  // decide on scriptname
3616de3759aSAndreas Gohr  if($direct){
3626de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3636de3759aSAndreas Gohr      $script = '_media';
3646de3759aSAndreas Gohr    }else{
3656de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3666de3759aSAndreas Gohr    }
3676de3759aSAndreas Gohr  }else{
3686de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3696de3759aSAndreas Gohr      $script = '_detail';
3706de3759aSAndreas Gohr    }else{
3716de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
3726de3759aSAndreas Gohr    }
3736de3759aSAndreas Gohr  }
3746de3759aSAndreas Gohr
3756de3759aSAndreas Gohr  // build URL based on rewrite mode
3766de3759aSAndreas Gohr   if($conf['userewrite']){
3776de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
3786de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
3796de3759aSAndreas Gohr   }else{
3806de3759aSAndreas Gohr     if($more){
381a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
382b174aeaeSchris       $xlink .= $sep.'media='.$id;
3836de3759aSAndreas Gohr     }else{
384a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
3856de3759aSAndreas Gohr     }
3866de3759aSAndreas Gohr   }
3876de3759aSAndreas Gohr
3886de3759aSAndreas Gohr  return $xlink;
3896de3759aSAndreas Gohr}
3906de3759aSAndreas Gohr
3916de3759aSAndreas Gohr
3926de3759aSAndreas Gohr
3936de3759aSAndreas Gohr/**
394f3f0262cSandi * Just builds a link to a script
39515fae107Sandi *
396ed7b5f09Sandi * @todo   maybe obsolete
39715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
398f3f0262cSandi */
399f3f0262cSandifunction script($script='doku.php'){
400ed7b5f09Sandi#  $link = getBaseURL();
401ed7b5f09Sandi#  $link .= $script;
402ed7b5f09Sandi#  return $link;
403ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
404f3f0262cSandi}
405f3f0262cSandi
406f3f0262cSandi/**
40715fae107Sandi * Spamcheck against wordlist
40815fae107Sandi *
409f3f0262cSandi * Checks the wikitext against a list of blocked expressions
410f3f0262cSandi * returns true if the text contains any bad words
41115fae107Sandi *
41215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
413f3f0262cSandi */
414f3f0262cSandifunction checkwordblock(){
415f3f0262cSandi  global $TEXT;
416f3f0262cSandi  global $conf;
417f3f0262cSandi
418f3f0262cSandi  if(!$conf['usewordblock']) return false;
419f3f0262cSandi
420041d1964SAndreas Gohr  // we prepare the text a tiny bit to prevent spammers circumventing URL checks
421041d1964SAndreas Gohr  $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i','\1http://\2 \2\3',$TEXT);
422041d1964SAndreas Gohr
423b9ac8716Schris  $wordblocks = getWordblocks();
4243e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
4253e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
4263e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
4273e2965d7Sandi    //backreferences are used - the maximum is 99
4283e2965d7Sandi    //this is very bad performancewise and may even be too high still
4293e2965d7Sandi    $chunksize = 40;
4303e2965d7Sandi  }else{
431703f6fdeSandi    //read file in chunks of 600 - this should work around the
4323e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
433444b87a5SAndreas Gohr    $chunksize = 400;
4343e2965d7Sandi  }
435b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
436f3f0262cSandi    $re = array();
437f3f0262cSandi    #build regexp from blocks
438f3f0262cSandi    foreach($blocks as $block){
439f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
440f3f0262cSandi      $block = trim($block);
441f3f0262cSandi      if(empty($block)) continue;
442f3f0262cSandi      $re[]  = $block;
443f3f0262cSandi    }
444041d1964SAndreas Gohr    if(preg_match('#('.join('|',$re).')#si',$text, $match=array())) {
445b9ac8716Schris      return true;
446b9ac8716Schris    }
447703f6fdeSandi  }
448f3f0262cSandi  return false;
449f3f0262cSandi}
450f3f0262cSandi
451f3f0262cSandi/**
45215fae107Sandi * Return the IP of the client
45315fae107Sandi *
4546d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers
45515fae107Sandi *
4566d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned
4576d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return
4586d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X
4596d8affe6SAndreas Gohr * headers
4606d8affe6SAndreas Gohr *
4616d8affe6SAndreas Gohr * @param  boolean $single If set only a single IP is returned
46215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
463f3f0262cSandi */
4646d8affe6SAndreas Gohrfunction clientIP($single=false){
4656d8affe6SAndreas Gohr  $ip = array();
4666d8affe6SAndreas Gohr  $ip[] = $_SERVER['REMOTE_ADDR'];
467bb4866bdSchris  if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
4686d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
469bb4866bdSchris  if(!empty($_SERVER['HTTP_X_REAL_IP']))
4706d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
4716d8affe6SAndreas Gohr
4726d8affe6SAndreas Gohr  // remove any non-IP stuff
4736d8affe6SAndreas Gohr  $cnt = count($ip);
4744ff28443Schris  $match = array();
4756d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
4764ff28443Schris    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
4774ff28443Schris      $ip[$i] = $match[0];
4784ff28443Schris    } else {
4794ff28443Schris      $ip[$i] = '';
4804ff28443Schris    }
4816d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
482f3f0262cSandi  }
4836d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
4846d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
4856d8affe6SAndreas Gohr
4866d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
4876d8affe6SAndreas Gohr
4886d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
4896d8affe6SAndreas Gohr  $ip = array_reverse($ip);
4906d8affe6SAndreas Gohr  foreach($ip as $i){
4916d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
4926d8affe6SAndreas Gohr      continue;
4936d8affe6SAndreas Gohr    }else{
4946d8affe6SAndreas Gohr      return $i;
4956d8affe6SAndreas Gohr    }
4966d8affe6SAndreas Gohr  }
4976d8affe6SAndreas Gohr  // still here? just use the first (last) address
4986d8affe6SAndreas Gohr  return $ip[0];
499f3f0262cSandi}
500f3f0262cSandi
501f3f0262cSandi/**
50215fae107Sandi * Checks if a given page is currently locked.
50315fae107Sandi *
504f3f0262cSandi * removes stale lockfiles
50515fae107Sandi *
50615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
507f3f0262cSandi */
508f3f0262cSandifunction checklock($id){
509f3f0262cSandi  global $conf;
510c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
511f3f0262cSandi
512f3f0262cSandi  //no lockfile
513f3f0262cSandi  if(!@file_exists($lock)) return false;
514f3f0262cSandi
515f3f0262cSandi  //lockfile expired
516f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
517d8186216SBen Coburn    @unlink($lock);
518f3f0262cSandi    return false;
519f3f0262cSandi  }
520f3f0262cSandi
521f3f0262cSandi  //my own lock
522f3f0262cSandi  $ip = io_readFile($lock);
523f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
524f3f0262cSandi    return false;
525f3f0262cSandi  }
526f3f0262cSandi
527f3f0262cSandi  return $ip;
528f3f0262cSandi}
529f3f0262cSandi
530f3f0262cSandi/**
53115fae107Sandi * Lock a page for editing
53215fae107Sandi *
53315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
534f3f0262cSandi */
535f3f0262cSandifunction lock($id){
536c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
537f3f0262cSandi  if($_SERVER['REMOTE_USER']){
538f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
539f3f0262cSandi  }else{
540f3f0262cSandi    io_saveFile($lock,clientIP());
541f3f0262cSandi  }
542f3f0262cSandi}
543f3f0262cSandi
544f3f0262cSandi/**
54515fae107Sandi * Unlock a page if it was locked by the user
546f3f0262cSandi *
54715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
54815fae107Sandi * @return bool true if a lock was removed
549f3f0262cSandi */
550f3f0262cSandifunction unlock($id){
551c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
552f3f0262cSandi  if(@file_exists($lock)){
553f3f0262cSandi    $ip = io_readFile($lock);
554f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
555f3f0262cSandi      @unlink($lock);
556f3f0262cSandi      return true;
557f3f0262cSandi    }
558f3f0262cSandi  }
559f3f0262cSandi  return false;
560f3f0262cSandi}
561f3f0262cSandi
562f3f0262cSandi/**
563f3f0262cSandi * convert line ending to unix format
564f3f0262cSandi *
56515fae107Sandi * @see    formText() for 2crlf conversion
56615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
567f3f0262cSandi */
568f3f0262cSandifunction cleanText($text){
569f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
570f3f0262cSandi  return $text;
571f3f0262cSandi}
572f3f0262cSandi
573f3f0262cSandi/**
574f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
575f3f0262cSandi * It also converts line endings to Windows format which is
576f3f0262cSandi * pseudo standard for webforms.
577f3f0262cSandi *
57815fae107Sandi * @see    cleanText() for 2unix conversion
57915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
580f3f0262cSandi */
581f3f0262cSandifunction formText($text){
582f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
583f3f0262cSandi  return htmlspecialchars($text);
584f3f0262cSandi}
585f3f0262cSandi
586f3f0262cSandi/**
58715fae107Sandi * Returns the specified local text in raw format
58815fae107Sandi *
58915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
590f3f0262cSandi */
591f3f0262cSandifunction rawLocale($id){
592f3f0262cSandi  return io_readFile(localeFN($id));
593f3f0262cSandi}
594f3f0262cSandi
595f3f0262cSandi/**
596f3f0262cSandi * Returns the raw WikiText
59715fae107Sandi *
59815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
599f3f0262cSandi */
600f3f0262cSandifunction rawWiki($id,$rev=''){
601cc7d0c94SBen Coburn  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
602f3f0262cSandi}
603f3f0262cSandi
604f3f0262cSandi/**
6057146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
6067146cee2SAndreas Gohr *
6077146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6087146cee2SAndreas Gohr */
609b7d5a5f0SAndreas Gohrfunction pageTemplate($data){
610b7d5a5f0SAndreas Gohr  $id = $data[0];
611a15ce62dSEsther Brunner  global $conf;
612a15ce62dSEsther Brunner  global $INFO;
613a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
614a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
615a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
616a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
617a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
618a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
619a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
620a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
621a15ce62dSEsther Brunner  return $tpl;
6227146cee2SAndreas Gohr}
6237146cee2SAndreas Gohr
6247146cee2SAndreas Gohr
6257146cee2SAndreas Gohr/**
62615fae107Sandi * Returns the raw Wiki Text in three slices.
62715fae107Sandi *
62815fae107Sandi * The range parameter needs to have the form "from-to"
62915cfe303Sandi * and gives the range of the section in bytes - no
63015cfe303Sandi * UTF-8 awareness is needed.
631f3f0262cSandi * The returned order is prefix, section and suffix.
63215fae107Sandi *
63315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
634f3f0262cSandi */
635f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
636f3f0262cSandi  list($from,$to) = split('-',$range,2);
637cc7d0c94SBen Coburn  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
638f3f0262cSandi  if(!$from) $from = 0;
639c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
640f3f0262cSandi
64115cfe303Sandi  $slices[0] = substr($text,0,$from-1);
64215cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
64315cfe303Sandi  $slices[2] = substr($text,$to);
644f3f0262cSandi
645f3f0262cSandi  return $slices;
646f3f0262cSandi}
647f3f0262cSandi
648f3f0262cSandi/**
64915fae107Sandi * Joins wiki text slices
65015fae107Sandi *
651f3f0262cSandi * function to join the text slices with correct lineendings again.
652f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
653f3f0262cSandi * lines between sections if needed (used on saving).
65415fae107Sandi *
65515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
656f3f0262cSandi */
657f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
658f3f0262cSandi
659f3f0262cSandi  if($pretty){
660f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
661f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
662f3f0262cSandi  }
663f3f0262cSandi
664f3f0262cSandi  if($pre) $pre .= "\n";
665f3f0262cSandi  if($suf) $text .= "\n";
666f3f0262cSandi  return $pre.$text.$suf;
667f3f0262cSandi}
668f3f0262cSandi
669f3f0262cSandi/**
670cc7d0c94SBen Coburn * Saves a wikitext by calling io_writeWikiPage
67115fae107Sandi *
67215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
67371726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
674f3f0262cSandi */
675b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
676f3f0262cSandi  global $conf;
677f3f0262cSandi  global $lang;
67871726d78SBen Coburn  global $REV;
679f3f0262cSandi  // ignore if no changes were made
680f3f0262cSandi  if($text == rawWiki($id,'')){
681f3f0262cSandi    return;
682f3f0262cSandi  }
683f3f0262cSandi
684f3f0262cSandi  $file = wikiFN($id);
685f3f0262cSandi  $old  = saveOldRevision($id);
68671726d78SBen Coburn  $wasRemoved = empty($text);
687d8186216SBen Coburn  $wasCreated = !@file_exists($file);
68871726d78SBen Coburn  $wasReverted = ($REV==true);
689e45b34cdSBen Coburn  $newRev = false;
690f3f0262cSandi
69171726d78SBen Coburn  if ($wasRemoved){
692e45b34cdSBen Coburn    // pre-save deleted revision
693e45b34cdSBen Coburn    @touch($file);
694e45b34cdSBen Coburn    $newRev = saveOldRevision($id);
695e1f3d9e1SEsther Brunner    // remove empty file
696f3f0262cSandi    @unlink($file);
69771726d78SBen Coburn    // remove old meta info...
698e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
69971726d78SBen Coburn    $changelog = metaFN($id, '.changes');
700e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
70171726d78SBen Coburn      // but keep per-page changelog to preserve page history
702d8186216SBen Coburn      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
703b158d625SSteven Danz    }
704f3f0262cSandi    $del = true;
7053ce054b3Sandi    // autoset summary on deletion
7063ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
70753d6ccfeSandi    // remove empty namespaces
708cc7d0c94SBen Coburn    io_sweepNS($id, 'datadir');
709cc7d0c94SBen Coburn    io_sweepNS($id, 'mediadir');
710f3f0262cSandi  }else{
711cc7d0c94SBen Coburn    // save file (namespace dir is created in io_writeWikiPage)
712cc7d0c94SBen Coburn    io_writeWikiPage($file, $text, $id);
713e45b34cdSBen Coburn    $newRev = @filemtime($file);
714f3f0262cSandi    $del = false;
715f3f0262cSandi  }
716f3f0262cSandi
71771726d78SBen Coburn  // select changelog line type
71871726d78SBen Coburn  $extra = '';
71971726d78SBen Coburn  $type = 'E';
72071726d78SBen Coburn  if ($wasReverted) {
72171726d78SBen Coburn    $type = 'R';
72271726d78SBen Coburn    $extra = $REV;
72371726d78SBen Coburn  }
72471726d78SBen Coburn  else if ($wasCreated) { $type = 'C'; }
72571726d78SBen Coburn  else if ($wasRemoved) { $type = 'D'; }
72671726d78SBen Coburn  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
72771726d78SBen Coburn
728e45b34cdSBen Coburn  addLogEntry($newRev, $id, $type, $summary, $extra);
72926a0801fSAndreas Gohr  // send notify mails
73090033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
73190033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
732f3f0262cSandi
733ce6b63d9Schris  // update the purgefile (timestamp of the last time anything within the wiki was changed)
73498407a7aSandi  io_saveFile($conf['cachedir'].'/purgefile',time());
735f3f0262cSandi}
736f3f0262cSandi
737f3f0262cSandi/**
738f3f0262cSandi * moves the current version to the attic and returns its
739f3f0262cSandi * revision date
74015fae107Sandi *
74115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
742f3f0262cSandi */
743f3f0262cSandifunction saveOldRevision($id){
744f3f0262cSandi  global $conf;
745f3f0262cSandi  $oldf = wikiFN($id);
746f3f0262cSandi  if(!@file_exists($oldf)) return '';
747f3f0262cSandi  $date = filemtime($oldf);
748f3f0262cSandi  $newf = wikiFN($id,$date);
749cc7d0c94SBen Coburn  io_writeWikiPage($newf, rawWiki($id), $id, $date);
750f3f0262cSandi  return $date;
751f3f0262cSandi}
752f3f0262cSandi
753f3f0262cSandi/**
75426a0801fSAndreas Gohr * Sends a notify mail on page change
75526a0801fSAndreas Gohr *
75626a0801fSAndreas Gohr * @param  string  $id       The changed page
75726a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
75826a0801fSAndreas Gohr * @param  int     $rev      Old page revision
75926a0801fSAndreas Gohr * @param  string  $summary  What changed
76090033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
76102a498e7Schris * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
76215fae107Sandi *
76315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
764f3f0262cSandi */
76502a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
766f3f0262cSandi  global $lang;
767f3f0262cSandi  global $conf;
768b158d625SSteven Danz
76926a0801fSAndreas Gohr  // decide if there is something to do
77026a0801fSAndreas Gohr  if($who == 'admin'){
77126a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
772f3f0262cSandi    $text = rawLocale('mailtext');
77326a0801fSAndreas Gohr    $to   = $conf['notify'];
77426a0801fSAndreas Gohr    $bcc  = '';
77526a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
77626a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
77790033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
77826a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
77926a0801fSAndreas Gohr    if(empty($bcc)) return;
78026a0801fSAndreas Gohr    $to   = '';
78126a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
782a06e4bdbSSebastian Harl  }elseif($who == 'register'){
783a06e4bdbSSebastian Harl    if(empty($conf['registernotify'])) return;
784a06e4bdbSSebastian Harl    $text = rawLocale('registermail');
785a06e4bdbSSebastian Harl    $to   = $conf['registernotify'];
786a06e4bdbSSebastian Harl    $bcc  = '';
78726a0801fSAndreas Gohr  }else{
78826a0801fSAndreas Gohr    return; //just to be safe
78926a0801fSAndreas Gohr  }
79026a0801fSAndreas Gohr
791f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
792f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
793f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
794f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
795ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
79626a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
79726a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
798ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
799f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
8007a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
801f3f0262cSandi
80202a498e7Schris  foreach ($replace as $key => $substitution) {
80302a498e7Schris    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
80402a498e7Schris  }
80502a498e7Schris
806a06e4bdbSSebastian Harl  if($who == 'register'){
807a06e4bdbSSebastian Harl    $subject = $lang['mail_new_user'].' '.$summary;
808a06e4bdbSSebastian Harl  }elseif($rev){
809f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
810ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
811ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
812f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
813f3f0262cSandi                    split("\n",rawWiki($id)));
814f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
815f3f0262cSandi    $diff    = $dformat->format($df);
816f3f0262cSandi  }else{
817f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
818f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
819f3f0262cSandi    $diff = rawWiki($id);
820f3f0262cSandi  }
821f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
822241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
823f3f0262cSandi
82426a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
825f3f0262cSandi}
826f3f0262cSandi
82715fae107Sandi/**
828f3f0262cSandi * extracts the query from a google referer
82915fae107Sandi *
8306b13307fSandi * @todo   should be more generic and support yahoo et al
83115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
832f3f0262cSandi */
833f3f0262cSandifunction getGoogleQuery(){
834f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
8355c3f206fSandi  if(!$url) return '';
836f3f0262cSandi
837f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
838f3f0262cSandi  $query = array();
839f3f0262cSandi  parse_str($url['query'],$query);
840f3f0262cSandi
841f3f0262cSandi  return $query['q'];
842f3f0262cSandi}
843f3f0262cSandi
844f3f0262cSandi/**
84515fae107Sandi * Try to set correct locale
84615fae107Sandi *
847095bfd5cSandi * @deprecated No longer used
84815fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
849f3f0262cSandi */
850f3f0262cSandifunction setCorrectLocale(){
851f3f0262cSandi  global $conf;
852f3f0262cSandi  global $lang;
853f3f0262cSandi
854f3f0262cSandi  $enc = strtoupper($lang['encoding']);
855f3f0262cSandi  foreach ($lang['locales'] as $loc){
856f3f0262cSandi    //try locale
857f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
858f3f0262cSandi    //try loceale with encoding
859f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
860f3f0262cSandi  }
861f3f0262cSandi  //still here? try to set from environment
862f3f0262cSandi  @setlocale(LC_ALL,"");
863f3f0262cSandi}
864f3f0262cSandi
865f3f0262cSandi/**
866f3f0262cSandi * Return the human readable size of a file
867f3f0262cSandi *
868f3f0262cSandi * @param       int    $size   A file size
869f3f0262cSandi * @param       int    $dec    A number of decimal places
870f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
871f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
872f3f0262cSandi * @version     1.0.0
873f3f0262cSandi */
874f31d5b73Sandifunction filesize_h($size, $dec = 1){
875f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
876f3f0262cSandi  $count = count($sizes);
877f3f0262cSandi  $i = 0;
878f3f0262cSandi
879f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
880f3f0262cSandi    $size /= 1024;
881f3f0262cSandi    $i++;
882f3f0262cSandi  }
883f3f0262cSandi
884f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
885f3f0262cSandi}
886f3f0262cSandi
88715fae107Sandi/**
88800a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
88900a7b5adSEsther Brunner *
89000a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
89100a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
89200a7b5adSEsther Brunner */
89300a7b5adSEsther Brunnerfunction obfuscate($email) {
89400a7b5adSEsther Brunner  global $conf;
89500a7b5adSEsther Brunner
89600a7b5adSEsther Brunner  switch ($conf['mailguard']) {
89700a7b5adSEsther Brunner    case 'visible' :
89800a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
89900a7b5adSEsther Brunner      return strtr($email, $obfuscate);
90000a7b5adSEsther Brunner
90100a7b5adSEsther Brunner    case 'hex' :
90200a7b5adSEsther Brunner      $encode = '';
90300a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
90400a7b5adSEsther Brunner      return $encode;
90500a7b5adSEsther Brunner
90600a7b5adSEsther Brunner    case 'none' :
90700a7b5adSEsther Brunner    default :
90800a7b5adSEsther Brunner      return $email;
90900a7b5adSEsther Brunner  }
91000a7b5adSEsther Brunner}
91100a7b5adSEsther Brunner
91200a7b5adSEsther Brunner/**
913b158d625SSteven Danz * Let us know if a user is tracking a page
914b158d625SSteven Danz *
9151380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
916b158d625SSteven Danz */
9171380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
9181380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
9191380fc45SAndreas Gohr  if (@file_exists($file)) {
920b158d625SSteven Danz    $mlist = file($file);
9211380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
9221380fc45SAndreas Gohr    return is_int($pos);
923b158d625SSteven Danz  }
9241380fc45SAndreas Gohr
925b158d625SSteven Danz  return false;
926b158d625SSteven Danz}
927340756e4Sandi
928f9eb5648Ssteven-danz/**
929f9eb5648Ssteven-danz * Return a string with the email addresses of all the
930f9eb5648Ssteven-danz * users subscribed to a page
931f9eb5648Ssteven-danz *
93226a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
933f9eb5648Ssteven-danz */
934f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
935f9eb5648Ssteven-danz  global $conf;
936cd52f92dSchris  global $auth;
937f9eb5648Ssteven-danz
938f9eb5648Ssteven-danz  $emails = '';
939f9eb5648Ssteven-danz
94026a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
94126a0801fSAndreas Gohr
942f9eb5648Ssteven-danz  $mlist = array();
943f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
944d8186216SBen Coburn  if (@file_exists($file)) {
945f9eb5648Ssteven-danz    $mlist = file($file);
946f9eb5648Ssteven-danz  }
947f9eb5648Ssteven-danz  if(count($mlist) > 0) {
948f9eb5648Ssteven-danz    foreach ($mlist as $who) {
949f9eb5648Ssteven-danz      $who = rtrim($who);
950cd52f92dSchris      $info = $auth->getUserData($who);
951f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
952f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
953f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
954f9eb5648Ssteven-danz          if (empty($emails)) {
955f9eb5648Ssteven-danz            $emails = $info['mail'];
956f9eb5648Ssteven-danz          } else {
957f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
958f9eb5648Ssteven-danz          }
959f9eb5648Ssteven-danz        }
960f9eb5648Ssteven-danz      }
961f9eb5648Ssteven-danz    }
962f9eb5648Ssteven-danz  }
963f9eb5648Ssteven-danz
964f9eb5648Ssteven-danz  return $emails;
965f9eb5648Ssteven-danz}
966f9eb5648Ssteven-danz
96789541d4bSAndreas Gohr/**
96889541d4bSAndreas Gohr * Removes quoting backslashes
96989541d4bSAndreas Gohr *
97089541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
97189541d4bSAndreas Gohr */
97289541d4bSAndreas Gohrfunction unslash($string,$char="'"){
97389541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
97489541d4bSAndreas Gohr}
97589541d4bSAndreas Gohr
976340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
977