xref: /dokuwiki/inc/common.php (revision b7d5a5f06d3e336fcc46ccebff28de2b214996ad)
1ed7b5f09Sandi<?php
215fae107Sandi/**
315fae107Sandi * Common DokuWiki functions
415fae107Sandi *
515fae107Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
615fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
715fae107Sandi */
815fae107Sandi
9ed7b5f09Sandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10e7cb32dcSAndreas Gohr  require_once(DOKU_CONF.'dokuwiki.php');
11ed7b5f09Sandi  require_once(DOKU_INC.'inc/io.php');
127d559c7fSBen Coburn  require_once(DOKU_INC.'inc/changelog.php');
13ed7b5f09Sandi  require_once(DOKU_INC.'inc/utf8.php');
14ed7b5f09Sandi  require_once(DOKU_INC.'inc/mail.php');
15c112d578Sandi  require_once(DOKU_INC.'inc/parserutils.php');
16f3f0262cSandi
17f3f0262cSandi/**
18b6912aeaSAndreas Gohr * These constants are used with the recents function
19b6912aeaSAndreas Gohr */
20b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED',2);
21b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS',4);
22b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES',8);
23b6912aeaSAndreas Gohr
24b6912aeaSAndreas Gohr/**
25d5197206Schris * Wrapper around htmlspecialchars()
26d5197206Schris *
27d5197206Schris * @author Andreas Gohr <andi@splitbrain.org>
28d5197206Schris * @see    htmlspecialchars()
29d5197206Schris */
30d5197206Schrisfunction hsc($string){
31d5197206Schris  return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
32d5197206Schris}
33d5197206Schris
34d5197206Schris/**
35d5197206Schris * print a newline terminated string
36d5197206Schris *
37d5197206Schris * You can give an indention as optional parameter
38d5197206Schris *
39d5197206Schris * @author Andreas Gohr <andi@splitbrain.org>
40d5197206Schris */
41d5197206Schrisfunction ptln($string,$intend=0){
42d5197206Schris  for($i=0; $i<$intend; $i++) print ' ';
43d5197206Schris  print"$string\n";
44d5197206Schris}
45d5197206Schris
46d5197206Schris/**
4715fae107Sandi * Return info about the current document as associative
48f3f0262cSandi * array.
4915fae107Sandi *
5015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
51f3f0262cSandi */
52f3f0262cSandifunction pageinfo(){
53f3f0262cSandi  global $ID;
54f3f0262cSandi  global $REV;
55f3f0262cSandi  global $USERINFO;
56f3f0262cSandi  global $conf;
57f3f0262cSandi
58f3f0262cSandi  if($_SERVER['REMOTE_USER']){
59f3f0262cSandi    $info['userinfo']   = $USERINFO;
60f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
611380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
62ee4c4a1bSAndreas Gohr    $info['client']     = $_SERVER['REMOTE_USER'];
6317ee7f66SAndreas Gohr
6417ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
6517ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
6617ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
6717ee7f66SAndreas Gohr    }
68ee4c4a1bSAndreas Gohr
69f3f0262cSandi  }else{
70f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
711380fc45SAndreas Gohr    $info['subscribed'] = false;
72ee4c4a1bSAndreas Gohr    $info['client']     = clientIP(true);
73f3f0262cSandi  }
74f3f0262cSandi
75f3f0262cSandi  $info['namespace'] = getNS($ID);
76f3f0262cSandi  $info['locked']    = checklock($ID);
77f3f0262cSandi  $info['filepath']  = realpath(wikiFN($ID,$REV));
78f3f0262cSandi  $info['exists']    = @file_exists($info['filepath']);
79f3f0262cSandi  if($REV && !$info['exists']){
80f3f0262cSandi    //check if current revision was meant
81f3f0262cSandi    $cur = wikiFN($ID);
82f3f0262cSandi    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
83f3f0262cSandi      $info['filepath'] = realpath($cur);
84f3f0262cSandi      $info['exists']   = true;
85f3f0262cSandi      $REV = '';
86f3f0262cSandi    }
87f3f0262cSandi  }
88c112d578Sandi  $info['rev'] = $REV;
89f3f0262cSandi  if($info['exists']){
90f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
91f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
92f3f0262cSandi  }else{
93f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
94f3f0262cSandi  }
95f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
96f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
97f3f0262cSandi
9871726d78SBen Coburn  //load page meta data
9971726d78SBen Coburn  $info['meta'] = p_get_metadata($ID);
10071726d78SBen Coburn
101652610a2Sandi  //who's the editor
102652610a2Sandi  if($REV){
10371726d78SBen Coburn    $revinfo = getRevisionInfo($ID, $REV, 1024);
104652610a2Sandi  }else{
10571726d78SBen Coburn    $revinfo = $info['meta']['last_change'];
106652610a2Sandi  }
107652610a2Sandi  $info['ip']     = $revinfo['ip'];
108652610a2Sandi  $info['user']   = $revinfo['user'];
109652610a2Sandi  $info['sum']    = $revinfo['sum'];
11071726d78SBen Coburn  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
11171726d78SBen Coburn  // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor'].
11259f257aeSchris
11388f522e9Sandi  if($revinfo['user']){
11488f522e9Sandi    $info['editor'] = $revinfo['user'];
11588f522e9Sandi  }else{
11688f522e9Sandi    $info['editor'] = $revinfo['ip'];
11788f522e9Sandi  }
118652610a2Sandi
119ee4c4a1bSAndreas Gohr  // draft
120ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
121ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
122ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
123ee4c4a1bSAndreas Gohr      // remove stale draft
124ee4c4a1bSAndreas Gohr      @unlink($draft);
125ee4c4a1bSAndreas Gohr    }else{
126ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
127ee4c4a1bSAndreas Gohr    }
128ee4c4a1bSAndreas Gohr  }
129ee4c4a1bSAndreas Gohr
130f3f0262cSandi  return $info;
131f3f0262cSandi}
132f3f0262cSandi
133f3f0262cSandi/**
1342684e50aSAndreas Gohr * Build an string of URL parameters
1352684e50aSAndreas Gohr *
1362684e50aSAndreas Gohr * @author Andreas Gohr
1372684e50aSAndreas Gohr */
138b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
1392684e50aSAndreas Gohr  $url = '';
1402684e50aSAndreas Gohr  $amp = false;
1412684e50aSAndreas Gohr  foreach($params as $key => $val){
142b174aeaeSchris    if($amp) $url .= $sep;
1432684e50aSAndreas Gohr
1442684e50aSAndreas Gohr    $url .= $key.'=';
145b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
1462684e50aSAndreas Gohr    $amp = true;
1472684e50aSAndreas Gohr  }
1482684e50aSAndreas Gohr  return $url;
1492684e50aSAndreas Gohr}
1502684e50aSAndreas Gohr
1512684e50aSAndreas Gohr/**
1522684e50aSAndreas Gohr * Build an string of html tag attributes
1532684e50aSAndreas Gohr *
1542684e50aSAndreas Gohr * @author Andreas Gohr
1552684e50aSAndreas Gohr */
1562684e50aSAndreas Gohrfunction buildAttributes($params){
1572684e50aSAndreas Gohr  $url = '';
1582684e50aSAndreas Gohr  foreach($params as $key => $val){
1592684e50aSAndreas Gohr    $url .= $key.'="';
1602684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
1612684e50aSAndreas Gohr    $url .= '" ';
1622684e50aSAndreas Gohr  }
1632684e50aSAndreas Gohr  return $url;
1642684e50aSAndreas Gohr}
1652684e50aSAndreas Gohr
1662684e50aSAndreas Gohr
1672684e50aSAndreas Gohr/**
1680396becbSandi * print a message
1690396becbSandi *
1700396becbSandi * If HTTP headers were not sent yet the message is added
1710396becbSandi * to the global message array else it's printed directly
1720396becbSandi * using html_msgarea()
1730396becbSandi *
174f3f0262cSandi *
175f3f0262cSandi * Levels can be:
176f3f0262cSandi *
177f3f0262cSandi * -1 error
178f3f0262cSandi *  0 info
179f3f0262cSandi *  1 success
18015fae107Sandi *
18115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
1820396becbSandi * @see    html_msgarea
183f3f0262cSandi */
1840d58d74eSAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){
185f3f0262cSandi  global $MSG;
186f3f0262cSandi  $errors[-1] = 'error';
187f3f0262cSandi  $errors[0]  = 'info';
188f3f0262cSandi  $errors[1]  = 'success';
189f3f0262cSandi
1900d58d74eSAndreas Gohr  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
1910d58d74eSAndreas Gohr
192cc20ad51Sandi  if(!headers_sent()){
193f3f0262cSandi    if(!isset($MSG)) $MSG = array();
194f3f0262cSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
1950396becbSandi  }else{
1960396becbSandi    $MSG = array();
1970396becbSandi    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
198f62ea8a1Sandi    if(function_exists('html_msgarea')){
1990396becbSandi      html_msgarea();
200f62ea8a1Sandi    }else{
201f62ea8a1Sandi      print "ERROR($lvl) $message";
202f62ea8a1Sandi    }
2030396becbSandi  }
204f3f0262cSandi}
205f3f0262cSandi
206f3f0262cSandi/**
20715fae107Sandi * This builds the breadcrumb trail and returns it as array
20815fae107Sandi *
20915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
210f3f0262cSandi */
211f3f0262cSandifunction breadcrumbs(){
2128746e727Sandi  // we prepare the breadcrumbs early for quick session closing
2138746e727Sandi  static $crumbs = null;
2148746e727Sandi  if($crumbs != null) return $crumbs;
2158746e727Sandi
216f3f0262cSandi  global $ID;
217f3f0262cSandi  global $ACT;
218f3f0262cSandi  global $conf;
219f3f0262cSandi  $crumbs = $_SESSION[$conf['title']]['bc'];
220f3f0262cSandi
221f3f0262cSandi  //first visit?
222f3f0262cSandi  if (!is_array($crumbs)){
223f3f0262cSandi    $crumbs = array();
224f3f0262cSandi  }
225f3f0262cSandi  //we only save on show and existing wiki documents
226a77f5846Sjan  $file = wikiFN($ID);
227a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
228f3f0262cSandi    $_SESSION[$conf['title']]['bc'] = $crumbs;
229f3f0262cSandi    return $crumbs;
230f3f0262cSandi  }
231a77f5846Sjan
232a77f5846Sjan  // page names
233a77f5846Sjan  $name = noNS($ID);
234a77f5846Sjan  if ($conf['useheading']) {
235a77f5846Sjan    // get page title
236bb0a59d4Sjan    $title = p_get_first_heading($ID);
237a77f5846Sjan    if ($title) {
238a77f5846Sjan      $name = $title;
239a77f5846Sjan    }
240a77f5846Sjan  }
241a77f5846Sjan
242f3f0262cSandi  //remove ID from array
243a77f5846Sjan  if (isset($crumbs[$ID])) {
244a77f5846Sjan    unset($crumbs[$ID]);
245f3f0262cSandi  }
246f3f0262cSandi
247f3f0262cSandi  //add to array
248a77f5846Sjan  $crumbs[$ID] = $name;
249f3f0262cSandi  //reduce size
250f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
251f3f0262cSandi    array_shift($crumbs);
252f3f0262cSandi  }
253f3f0262cSandi  //save to session
254f3f0262cSandi  $_SESSION[$conf['title']]['bc'] = $crumbs;
255f3f0262cSandi  return $crumbs;
256f3f0262cSandi}
257f3f0262cSandi
258f3f0262cSandi/**
25915fae107Sandi * Filter for page IDs
26015fae107Sandi *
261f3f0262cSandi * This is run on a ID before it is outputted somewhere
262f3f0262cSandi * currently used to replace the colon with something else
263f3f0262cSandi * on Windows systems and to have proper URL encoding
26415fae107Sandi *
26549c713a3Sandi * Urlencoding is ommitted when the second parameter is false
26649c713a3Sandi *
26715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
268f3f0262cSandi */
26949c713a3Sandifunction idfilter($id,$ue=true){
270f3f0262cSandi  global $conf;
271f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
272f3f0262cSandi    $id = strtr($id,':','/');
273f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
274f3f0262cSandi      $conf['userewrite']) {
275f3f0262cSandi    $id = strtr($id,':',';');
276f3f0262cSandi  }
27749c713a3Sandi  if($ue){
278b6c6979fSAndreas Gohr    $id = rawurlencode($id);
279f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
280f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
28149c713a3Sandi  }
282f3f0262cSandi  return $id;
283f3f0262cSandi}
284f3f0262cSandi
285f3f0262cSandi/**
286ed7b5f09Sandi * This builds a link to a wikipage
28715fae107Sandi *
2886c7843b5Sandi * It handles URL rewriting and adds additional parameter if
2896c7843b5Sandi * given in $more
2906c7843b5Sandi *
29115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
292f3f0262cSandi */
293b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
294f3f0262cSandi  global $conf;
2956de3759aSAndreas Gohr  if(is_array($more)){
296b174aeaeSchris    $more = buildURLparams($more,$sep);
2976de3759aSAndreas Gohr  }else{
298b174aeaeSchris    $more = str_replace(',',$sep,$more);
2996de3759aSAndreas Gohr  }
300f3f0262cSandi
301f3f0262cSandi  $id    = idfilter($id);
302ed7b5f09Sandi  if($abs){
303ed7b5f09Sandi    $xlink = DOKU_URL;
304ed7b5f09Sandi  }else{
305ed7b5f09Sandi    $xlink = DOKU_BASE;
306ed7b5f09Sandi  }
307f3f0262cSandi
3086c7843b5Sandi  if($conf['userewrite'] == 2){
3096c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
3106c7843b5Sandi    if($more) $xlink .= '?'.$more;
3116c7843b5Sandi  }elseif($conf['userewrite']){
312f3f0262cSandi    $xlink .= $id;
313f3f0262cSandi    if($more) $xlink .= '?'.$more;
3146c7843b5Sandi  }else{
3156c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
316b174aeaeSchris    if($more) $xlink .= $sep.$more;
317f3f0262cSandi  }
318f3f0262cSandi
319f3f0262cSandi  return $xlink;
320f3f0262cSandi}
321f3f0262cSandi
322f3f0262cSandi/**
323f5c2808fSBen Coburn * This builds a link to an alternate page format
324f5c2808fSBen Coburn *
325f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
326f5c2808fSBen Coburn *
327f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
328f5c2808fSBen Coburn */
329f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
330f5c2808fSBen Coburn  global $conf;
331f5c2808fSBen Coburn  if(is_array($more)){
332f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
333f5c2808fSBen Coburn  }else{
334f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
335f5c2808fSBen Coburn  }
336f5c2808fSBen Coburn
337f5c2808fSBen Coburn  $format = rawurlencode($format);
338f5c2808fSBen Coburn  $id = idfilter($id);
339f5c2808fSBen Coburn  if($abs){
340f5c2808fSBen Coburn    $xlink = DOKU_URL;
341f5c2808fSBen Coburn  }else{
342f5c2808fSBen Coburn    $xlink = DOKU_BASE;
343f5c2808fSBen Coburn  }
344f5c2808fSBen Coburn
345f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
346f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
347f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
348f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
349f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
350f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
351f5c2808fSBen Coburn  }else{
352f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
353f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
354f5c2808fSBen Coburn  }
355f5c2808fSBen Coburn
356f5c2808fSBen Coburn  return $xlink;
357f5c2808fSBen Coburn}
358f5c2808fSBen Coburn
359f5c2808fSBen Coburn/**
3606de3759aSAndreas Gohr * Build a link to a media file
3616de3759aSAndreas Gohr *
3626de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
3636de3759aSAndreas Gohr */
364b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&amp;'){
3656de3759aSAndreas Gohr  global $conf;
3666de3759aSAndreas Gohr  if(is_array($more)){
367b174aeaeSchris    $more = buildURLparams($more,$sep);
3686de3759aSAndreas Gohr  }else{
369b174aeaeSchris    $more = str_replace(',',$sep,$more);
3706de3759aSAndreas Gohr  }
3716de3759aSAndreas Gohr
3726de3759aSAndreas Gohr  $xlink = DOKU_BASE;
3736de3759aSAndreas Gohr
3746de3759aSAndreas Gohr  // external URLs are always direct without rewriting
3756de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
3766de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
3776de3759aSAndreas Gohr    if($more){
3786de3759aSAndreas Gohr      $xlink .= '?'.$more;
379b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
3806de3759aSAndreas Gohr    }else{
381b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
3826de3759aSAndreas Gohr    }
3836de3759aSAndreas Gohr    return $xlink;
3846de3759aSAndreas Gohr  }
3856de3759aSAndreas Gohr
3866de3759aSAndreas Gohr  $id = idfilter($id);
3876de3759aSAndreas Gohr
3886de3759aSAndreas Gohr  // decide on scriptname
3896de3759aSAndreas Gohr  if($direct){
3906de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3916de3759aSAndreas Gohr      $script = '_media';
3926de3759aSAndreas Gohr    }else{
3936de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
3946de3759aSAndreas Gohr    }
3956de3759aSAndreas Gohr  }else{
3966de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
3976de3759aSAndreas Gohr      $script = '_detail';
3986de3759aSAndreas Gohr    }else{
3996de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
4006de3759aSAndreas Gohr    }
4016de3759aSAndreas Gohr  }
4026de3759aSAndreas Gohr
4036de3759aSAndreas Gohr  // build URL based on rewrite mode
4046de3759aSAndreas Gohr   if($conf['userewrite']){
4056de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
4066de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
4076de3759aSAndreas Gohr   }else{
4086de3759aSAndreas Gohr     if($more){
409a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
410b174aeaeSchris       $xlink .= $sep.'media='.$id;
4116de3759aSAndreas Gohr     }else{
412a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
4136de3759aSAndreas Gohr     }
4146de3759aSAndreas Gohr   }
4156de3759aSAndreas Gohr
4166de3759aSAndreas Gohr  return $xlink;
4176de3759aSAndreas Gohr}
4186de3759aSAndreas Gohr
4196de3759aSAndreas Gohr
4206de3759aSAndreas Gohr
4216de3759aSAndreas Gohr/**
422f3f0262cSandi * Just builds a link to a script
42315fae107Sandi *
424ed7b5f09Sandi * @todo   maybe obsolete
42515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
426f3f0262cSandi */
427f3f0262cSandifunction script($script='doku.php'){
428ed7b5f09Sandi#  $link = getBaseURL();
429ed7b5f09Sandi#  $link .= $script;
430ed7b5f09Sandi#  return $link;
431ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
432f3f0262cSandi}
433f3f0262cSandi
434f3f0262cSandi/**
43515fae107Sandi * Spamcheck against wordlist
43615fae107Sandi *
437f3f0262cSandi * Checks the wikitext against a list of blocked expressions
438f3f0262cSandi * returns true if the text contains any bad words
43915fae107Sandi *
44015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
441f3f0262cSandi */
442f3f0262cSandifunction checkwordblock(){
443f3f0262cSandi  global $TEXT;
444f3f0262cSandi  global $conf;
445f3f0262cSandi
446f3f0262cSandi  if(!$conf['usewordblock']) return false;
447f3f0262cSandi
448b9ac8716Schris  $wordblocks = getWordblocks();
4493e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
4503e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
4513e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
4523e2965d7Sandi    //backreferences are used - the maximum is 99
4533e2965d7Sandi    //this is very bad performancewise and may even be too high still
4543e2965d7Sandi    $chunksize = 40;
4553e2965d7Sandi  }else{
456703f6fdeSandi    //read file in chunks of 600 - this should work around the
4573e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
458444b87a5SAndreas Gohr    $chunksize = 400;
4593e2965d7Sandi  }
460b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
461f3f0262cSandi    $re = array();
462f3f0262cSandi    #build regexp from blocks
463f3f0262cSandi    foreach($blocks as $block){
464f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
465f3f0262cSandi      $block = trim($block);
466f3f0262cSandi      if(empty($block)) continue;
467f3f0262cSandi      $re[]  = $block;
468f3f0262cSandi    }
469b9ac8716Schris    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
470b9ac8716Schris      return true;
471b9ac8716Schris    }
472703f6fdeSandi  }
473f3f0262cSandi  return false;
474f3f0262cSandi}
475f3f0262cSandi
476f3f0262cSandi/**
47715fae107Sandi * Return the IP of the client
47815fae107Sandi *
4796d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers
48015fae107Sandi *
4816d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned
4826d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return
4836d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X
4846d8affe6SAndreas Gohr * headers
4856d8affe6SAndreas Gohr *
4866d8affe6SAndreas Gohr * @param  boolean $single If set only a single IP is returned
48715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
488f3f0262cSandi */
4896d8affe6SAndreas Gohrfunction clientIP($single=false){
4906d8affe6SAndreas Gohr  $ip = array();
4916d8affe6SAndreas Gohr  $ip[] = $_SERVER['REMOTE_ADDR'];
4926d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_FORWARDED_FOR'])
4936d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
4946d8affe6SAndreas Gohr  if($_SERVER['HTTP_X_REAL_IP'])
4956d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
4966d8affe6SAndreas Gohr
4976d8affe6SAndreas Gohr  // remove any non-IP stuff
4986d8affe6SAndreas Gohr  $cnt = count($ip);
4994ff28443Schris  $match = array();
5006d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
5014ff28443Schris    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
5024ff28443Schris      $ip[$i] = $match[0];
5034ff28443Schris    } else {
5044ff28443Schris      $ip[$i] = '';
5054ff28443Schris    }
5066d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
507f3f0262cSandi  }
5086d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
5096d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
5106d8affe6SAndreas Gohr
5116d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
5126d8affe6SAndreas Gohr
5136d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
5146d8affe6SAndreas Gohr  $ip = array_reverse($ip);
5156d8affe6SAndreas Gohr  foreach($ip as $i){
5166d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
5176d8affe6SAndreas Gohr      continue;
5186d8affe6SAndreas Gohr    }else{
5196d8affe6SAndreas Gohr      return $i;
5206d8affe6SAndreas Gohr    }
5216d8affe6SAndreas Gohr  }
5226d8affe6SAndreas Gohr  // still here? just use the first (last) address
5236d8affe6SAndreas Gohr  return $ip[0];
524f3f0262cSandi}
525f3f0262cSandi
526f3f0262cSandi/**
52715fae107Sandi * Checks if a given page is currently locked.
52815fae107Sandi *
529f3f0262cSandi * removes stale lockfiles
53015fae107Sandi *
53115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
532f3f0262cSandi */
533f3f0262cSandifunction checklock($id){
534f3f0262cSandi  global $conf;
535c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
536f3f0262cSandi
537f3f0262cSandi  //no lockfile
538f3f0262cSandi  if(!@file_exists($lock)) return false;
539f3f0262cSandi
540f3f0262cSandi  //lockfile expired
541f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
542d8186216SBen Coburn    @unlink($lock);
543f3f0262cSandi    return false;
544f3f0262cSandi  }
545f3f0262cSandi
546f3f0262cSandi  //my own lock
547f3f0262cSandi  $ip = io_readFile($lock);
548f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
549f3f0262cSandi    return false;
550f3f0262cSandi  }
551f3f0262cSandi
552f3f0262cSandi  return $ip;
553f3f0262cSandi}
554f3f0262cSandi
555f3f0262cSandi/**
55615fae107Sandi * Lock a page for editing
55715fae107Sandi *
55815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
559f3f0262cSandi */
560f3f0262cSandifunction lock($id){
561c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
562f3f0262cSandi  if($_SERVER['REMOTE_USER']){
563f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
564f3f0262cSandi  }else{
565f3f0262cSandi    io_saveFile($lock,clientIP());
566f3f0262cSandi  }
567f3f0262cSandi}
568f3f0262cSandi
569f3f0262cSandi/**
57015fae107Sandi * Unlock a page if it was locked by the user
571f3f0262cSandi *
57215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
57315fae107Sandi * @return bool true if a lock was removed
574f3f0262cSandi */
575f3f0262cSandifunction unlock($id){
576c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
577f3f0262cSandi  if(@file_exists($lock)){
578f3f0262cSandi    $ip = io_readFile($lock);
579f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
580f3f0262cSandi      @unlink($lock);
581f3f0262cSandi      return true;
582f3f0262cSandi    }
583f3f0262cSandi  }
584f3f0262cSandi  return false;
585f3f0262cSandi}
586f3f0262cSandi
587f3f0262cSandi/**
588f3f0262cSandi * convert line ending to unix format
589f3f0262cSandi *
59015fae107Sandi * @see    formText() for 2crlf conversion
59115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
592f3f0262cSandi */
593f3f0262cSandifunction cleanText($text){
594f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
595f3f0262cSandi  return $text;
596f3f0262cSandi}
597f3f0262cSandi
598f3f0262cSandi/**
599f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
600f3f0262cSandi * It also converts line endings to Windows format which is
601f3f0262cSandi * pseudo standard for webforms.
602f3f0262cSandi *
60315fae107Sandi * @see    cleanText() for 2unix conversion
60415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
605f3f0262cSandi */
606f3f0262cSandifunction formText($text){
607f3f0262cSandi  $text = preg_replace("/\012/","\015\012",$text);
608f3f0262cSandi  return htmlspecialchars($text);
609f3f0262cSandi}
610f3f0262cSandi
611f3f0262cSandi/**
61215fae107Sandi * Returns the specified local text in raw format
61315fae107Sandi *
61415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
615f3f0262cSandi */
616f3f0262cSandifunction rawLocale($id){
617f3f0262cSandi  return io_readFile(localeFN($id));
618f3f0262cSandi}
619f3f0262cSandi
620f3f0262cSandi/**
621f3f0262cSandi * Returns the raw WikiText
62215fae107Sandi *
62315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
624f3f0262cSandi */
625f3f0262cSandifunction rawWiki($id,$rev=''){
626cc7d0c94SBen Coburn  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
627f3f0262cSandi}
628f3f0262cSandi
629f3f0262cSandi/**
6307146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
6317146cee2SAndreas Gohr *
6327146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
6337146cee2SAndreas Gohr */
634*b7d5a5f0SAndreas Gohrfunction pageTemplate($data){
635*b7d5a5f0SAndreas Gohr  $id = $data[0];
636a15ce62dSEsther Brunner  global $conf;
637a15ce62dSEsther Brunner  global $INFO;
638a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
639a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
640a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
641a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
642a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
643a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
644a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
645a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
646a15ce62dSEsther Brunner  return $tpl;
6477146cee2SAndreas Gohr}
6487146cee2SAndreas Gohr
6497146cee2SAndreas Gohr
6507146cee2SAndreas Gohr/**
65115fae107Sandi * Returns the raw Wiki Text in three slices.
65215fae107Sandi *
65315fae107Sandi * The range parameter needs to have the form "from-to"
65415cfe303Sandi * and gives the range of the section in bytes - no
65515cfe303Sandi * UTF-8 awareness is needed.
656f3f0262cSandi * The returned order is prefix, section and suffix.
65715fae107Sandi *
65815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
659f3f0262cSandi */
660f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
661f3f0262cSandi  list($from,$to) = split('-',$range,2);
662cc7d0c94SBen Coburn  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
663f3f0262cSandi  if(!$from) $from = 0;
664c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
665f3f0262cSandi
66615cfe303Sandi  $slices[0] = substr($text,0,$from-1);
66715cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
66815cfe303Sandi  $slices[2] = substr($text,$to);
669f3f0262cSandi
670f3f0262cSandi  return $slices;
671f3f0262cSandi}
672f3f0262cSandi
673f3f0262cSandi/**
67415fae107Sandi * Joins wiki text slices
67515fae107Sandi *
676f3f0262cSandi * function to join the text slices with correct lineendings again.
677f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
678f3f0262cSandi * lines between sections if needed (used on saving).
67915fae107Sandi *
68015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
681f3f0262cSandi */
682f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
683f3f0262cSandi
684f3f0262cSandi  if($pretty){
685f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
686f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
687f3f0262cSandi  }
688f3f0262cSandi
689f3f0262cSandi  if($pre) $pre .= "\n";
690f3f0262cSandi  if($suf) $text .= "\n";
691f3f0262cSandi  return $pre.$text.$suf;
692f3f0262cSandi}
693f3f0262cSandi
694f3f0262cSandi/**
69515fae107Sandi * print debug messages
69615fae107Sandi *
697f3f0262cSandi * little function to print the content of a var
69815fae107Sandi *
69915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
700f3f0262cSandi */
701f3f0262cSandifunction dbg($msg,$hidden=false){
702f3f0262cSandi  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
703f3f0262cSandi  print_r($msg);
704f3f0262cSandi  (!$hidden) ? print '</pre>' : print "\n-->";
705f3f0262cSandi}
706f3f0262cSandi
707f3f0262cSandi/**
70863cb5853SAndreas Gohr * Print info to a log file
70963cb5853SAndreas Gohr *
71063cb5853SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
71163cb5853SAndreas Gohr */
71263cb5853SAndreas Gohrfunction dbglog($msg){
71363cb5853SAndreas Gohr  global $conf;
71463cb5853SAndreas Gohr  $file = $conf['cachedir'].'/debug.log';
71563cb5853SAndreas Gohr  $fh = fopen($file,'a');
71663cb5853SAndreas Gohr  if($fh){
71763cb5853SAndreas Gohr    fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
71863cb5853SAndreas Gohr    fclose($fh);
71963cb5853SAndreas Gohr  }
72063cb5853SAndreas Gohr}
72163cb5853SAndreas Gohr
72263cb5853SAndreas Gohr/**
723cc7d0c94SBen Coburn * Saves a wikitext by calling io_writeWikiPage
72415fae107Sandi *
72515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
72671726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
727f3f0262cSandi */
728b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
729f3f0262cSandi  global $conf;
730f3f0262cSandi  global $lang;
73171726d78SBen Coburn  global $REV;
732f3f0262cSandi  // ignore if no changes were made
733f3f0262cSandi  if($text == rawWiki($id,'')){
734f3f0262cSandi    return;
735f3f0262cSandi  }
736f3f0262cSandi
737f3f0262cSandi  $file = wikiFN($id);
738f3f0262cSandi  $old  = saveOldRevision($id);
73971726d78SBen Coburn  $wasRemoved = empty($text);
740d8186216SBen Coburn  $wasCreated = !@file_exists($file);
74171726d78SBen Coburn  $wasReverted = ($REV==true);
742e45b34cdSBen Coburn  $newRev = false;
743f3f0262cSandi
74471726d78SBen Coburn  if ($wasRemoved){
745e45b34cdSBen Coburn    // pre-save deleted revision
746e45b34cdSBen Coburn    @touch($file);
747e45b34cdSBen Coburn    $newRev = saveOldRevision($id);
748e1f3d9e1SEsther Brunner    // remove empty file
749f3f0262cSandi    @unlink($file);
75071726d78SBen Coburn    // remove old meta info...
751e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
75271726d78SBen Coburn    $changelog = metaFN($id, '.changes');
753e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
75471726d78SBen Coburn      // but keep per-page changelog to preserve page history
755d8186216SBen Coburn      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
756b158d625SSteven Danz    }
757f3f0262cSandi    $del = true;
7583ce054b3Sandi    // autoset summary on deletion
7593ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
76053d6ccfeSandi    // remove empty namespaces
761cc7d0c94SBen Coburn    io_sweepNS($id, 'datadir');
762cc7d0c94SBen Coburn    io_sweepNS($id, 'mediadir');
763f3f0262cSandi  }else{
764cc7d0c94SBen Coburn    // save file (namespace dir is created in io_writeWikiPage)
765cc7d0c94SBen Coburn    io_writeWikiPage($file, $text, $id);
766e45b34cdSBen Coburn    $newRev = @filemtime($file);
767f3f0262cSandi    $del = false;
768f3f0262cSandi  }
769f3f0262cSandi
77071726d78SBen Coburn  // select changelog line type
77171726d78SBen Coburn  $extra = '';
77271726d78SBen Coburn  $type = 'E';
77371726d78SBen Coburn  if ($wasReverted) {
77471726d78SBen Coburn    $type = 'R';
77571726d78SBen Coburn    $extra = $REV;
77671726d78SBen Coburn  }
77771726d78SBen Coburn  else if ($wasCreated) { $type = 'C'; }
77871726d78SBen Coburn  else if ($wasRemoved) { $type = 'D'; }
77971726d78SBen Coburn  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users
78071726d78SBen Coburn
781e45b34cdSBen Coburn  addLogEntry($newRev, $id, $type, $summary, $extra);
78226a0801fSAndreas Gohr  // send notify mails
78390033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
78490033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
785f3f0262cSandi
786f3f0262cSandi  //purge cache on add by updating the purgefile
787f3f0262cSandi  if($conf['purgeonadd'] && (!$old || $del)){
78898407a7aSandi    io_saveFile($conf['cachedir'].'/purgefile',time());
789f3f0262cSandi  }
790f3f0262cSandi}
791f3f0262cSandi
792f3f0262cSandi/**
793f3f0262cSandi * moves the current version to the attic and returns its
794f3f0262cSandi * revision date
79515fae107Sandi *
79615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
797f3f0262cSandi */
798f3f0262cSandifunction saveOldRevision($id){
799f3f0262cSandi  global $conf;
800f3f0262cSandi  $oldf = wikiFN($id);
801f3f0262cSandi  if(!@file_exists($oldf)) return '';
802f3f0262cSandi  $date = filemtime($oldf);
803f3f0262cSandi  $newf = wikiFN($id,$date);
804cc7d0c94SBen Coburn  io_writeWikiPage($newf, rawWiki($id), $id, $date);
805f3f0262cSandi  return $date;
806f3f0262cSandi}
807f3f0262cSandi
808f3f0262cSandi/**
80926a0801fSAndreas Gohr * Sends a notify mail on page change
81026a0801fSAndreas Gohr *
81126a0801fSAndreas Gohr * @param  string  $id       The changed page
81226a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
81326a0801fSAndreas Gohr * @param  int     $rev      Old page revision
81426a0801fSAndreas Gohr * @param  string  $summary  What changed
81590033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
81602a498e7Schris * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
81715fae107Sandi *
81815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
819f3f0262cSandi */
82002a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
821f3f0262cSandi  global $lang;
822f3f0262cSandi  global $conf;
823b158d625SSteven Danz
82426a0801fSAndreas Gohr  // decide if there is something to do
82526a0801fSAndreas Gohr  if($who == 'admin'){
82626a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
827f3f0262cSandi    $text = rawLocale('mailtext');
82826a0801fSAndreas Gohr    $to   = $conf['notify'];
82926a0801fSAndreas Gohr    $bcc  = '';
83026a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
83126a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
83290033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
83326a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
83426a0801fSAndreas Gohr    if(empty($bcc)) return;
83526a0801fSAndreas Gohr    $to   = '';
83626a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
837a06e4bdbSSebastian Harl  }elseif($who == 'register'){
838a06e4bdbSSebastian Harl    if(empty($conf['registernotify'])) return;
839a06e4bdbSSebastian Harl    $text = rawLocale('registermail');
840a06e4bdbSSebastian Harl    $to   = $conf['registernotify'];
841a06e4bdbSSebastian Harl    $bcc  = '';
84226a0801fSAndreas Gohr  }else{
84326a0801fSAndreas Gohr    return; //just to be safe
84426a0801fSAndreas Gohr  }
84526a0801fSAndreas Gohr
846f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
847f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
848f3f0262cSandi  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
849f3f0262cSandi  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
850ed7b5f09Sandi  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
85126a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
85226a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
853ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
854f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
8557a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
856f3f0262cSandi
85702a498e7Schris  foreach ($replace as $key => $substitution) {
85802a498e7Schris    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
85902a498e7Schris  }
86002a498e7Schris
861a06e4bdbSSebastian Harl  if($who == 'register'){
862a06e4bdbSSebastian Harl    $subject = $lang['mail_new_user'].' '.$summary;
863a06e4bdbSSebastian Harl  }elseif($rev){
864f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
865ed7b5f09Sandi    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
866ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
867f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
868f3f0262cSandi                    split("\n",rawWiki($id)));
869f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
870f3f0262cSandi    $diff    = $dformat->format($df);
871f3f0262cSandi  }else{
872f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
873f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
874f3f0262cSandi    $diff = rawWiki($id);
875f3f0262cSandi  }
876f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
877241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
878f3f0262cSandi
87926a0801fSAndreas Gohr  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
880f3f0262cSandi}
881f3f0262cSandi
88215fae107Sandi/**
883f3f0262cSandi * extracts the query from a google referer
88415fae107Sandi *
8856b13307fSandi * @todo   should be more generic and support yahoo et al
88615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
887f3f0262cSandi */
888f3f0262cSandifunction getGoogleQuery(){
889f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
8905c3f206fSandi  if(!$url) return '';
891f3f0262cSandi
892f3f0262cSandi  if(!preg_match("#google\.#i",$url['host'])) return '';
893f3f0262cSandi  $query = array();
894f3f0262cSandi  parse_str($url['query'],$query);
895f3f0262cSandi
896f3f0262cSandi  return $query['q'];
897f3f0262cSandi}
898f3f0262cSandi
899f3f0262cSandi/**
90015fae107Sandi * Try to set correct locale
90115fae107Sandi *
902095bfd5cSandi * @deprecated No longer used
90315fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
904f3f0262cSandi */
905f3f0262cSandifunction setCorrectLocale(){
906f3f0262cSandi  global $conf;
907f3f0262cSandi  global $lang;
908f3f0262cSandi
909f3f0262cSandi  $enc = strtoupper($lang['encoding']);
910f3f0262cSandi  foreach ($lang['locales'] as $loc){
911f3f0262cSandi    //try locale
912f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
913f3f0262cSandi    //try loceale with encoding
914f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
915f3f0262cSandi  }
916f3f0262cSandi  //still here? try to set from environment
917f3f0262cSandi  @setlocale(LC_ALL,"");
918f3f0262cSandi}
919f3f0262cSandi
920f3f0262cSandi/**
921f3f0262cSandi * Return the human readable size of a file
922f3f0262cSandi *
923f3f0262cSandi * @param       int    $size   A file size
924f3f0262cSandi * @param       int    $dec    A number of decimal places
925f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
926f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
927f3f0262cSandi * @version     1.0.0
928f3f0262cSandi */
929f31d5b73Sandifunction filesize_h($size, $dec = 1){
930f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
931f3f0262cSandi  $count = count($sizes);
932f3f0262cSandi  $i = 0;
933f3f0262cSandi
934f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
935f3f0262cSandi    $size /= 1024;
936f3f0262cSandi    $i++;
937f3f0262cSandi  }
938f3f0262cSandi
939f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
940f3f0262cSandi}
941f3f0262cSandi
94215fae107Sandi/**
94300a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
94400a7b5adSEsther Brunner *
94500a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
94600a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
94700a7b5adSEsther Brunner */
94800a7b5adSEsther Brunnerfunction obfuscate($email) {
94900a7b5adSEsther Brunner  global $conf;
95000a7b5adSEsther Brunner
95100a7b5adSEsther Brunner  switch ($conf['mailguard']) {
95200a7b5adSEsther Brunner    case 'visible' :
95300a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
95400a7b5adSEsther Brunner      return strtr($email, $obfuscate);
95500a7b5adSEsther Brunner
95600a7b5adSEsther Brunner    case 'hex' :
95700a7b5adSEsther Brunner      $encode = '';
95800a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
95900a7b5adSEsther Brunner      return $encode;
96000a7b5adSEsther Brunner
96100a7b5adSEsther Brunner    case 'none' :
96200a7b5adSEsther Brunner    default :
96300a7b5adSEsther Brunner      return $email;
96400a7b5adSEsther Brunner  }
96500a7b5adSEsther Brunner}
96600a7b5adSEsther Brunner
96700a7b5adSEsther Brunner/**
968dc57ef04Sandi * Return DokuWikis version
96915fae107Sandi *
97015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
97115fae107Sandi */
972f31d5b73Sandifunction getVersion(){
973f31d5b73Sandi  //import version string
974f31d5b73Sandi  if(@file_exists('VERSION')){
975f31d5b73Sandi    //official release
9760647ce3bSAndreas Gohr    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
977f31d5b73Sandi  }elseif(is_dir('_darcs')){
978f31d5b73Sandi    //darcs checkout
979f31d5b73Sandi    $inv = file('_darcs/inventory');
980ae41559bSAndreas Gohr    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
981f31d5b73Sandi    $cur = array_pop($inv);
982f31d5b73Sandi    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
983f31d5b73Sandi    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
984f31d5b73Sandi  }else{
985f31d5b73Sandi    return 'snapshot?';
986f31d5b73Sandi  }
987f31d5b73Sandi}
988f31d5b73Sandi
989f31d5b73Sandi/**
990f31d5b73Sandi * Run a few sanity checks
991f31d5b73Sandi *
992f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org>
993f31d5b73Sandi */
994f3f0262cSandifunction check(){
995f3f0262cSandi  global $conf;
996f3f0262cSandi  global $INFO;
997f3f0262cSandi
998f31d5b73Sandi  msg('DokuWiki version: '.getVersion(),1);
999f31d5b73Sandi
100049022a38Sandi  if(version_compare(phpversion(),'4.3.0','<')){
100149022a38Sandi    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
100249022a38Sandi  }elseif(version_compare(phpversion(),'4.3.10','<')){
100349022a38Sandi    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
100449022a38Sandi  }else{
100549022a38Sandi    msg('PHP version '.phpversion(),1);
100649022a38Sandi  }
100749022a38Sandi
1008f3f0262cSandi  if(is_writable($conf['changelog'])){
1009f3f0262cSandi    msg('Changelog is writable',1);
1010f3f0262cSandi  }else{
1011d8186216SBen Coburn    if (@file_exists($conf['changelog'])) {
1012f3f0262cSandi      msg('Changelog is not writable',-1);
1013f3f0262cSandi    }
101471726d78SBen Coburn  }
101571726d78SBen Coburn
1016d8186216SBen Coburn  if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
101771726d78SBen Coburn    msg('Old changelog exists.', 0);
101871726d78SBen Coburn  }
101971726d78SBen Coburn
1020d8186216SBen Coburn  if (@file_exists($conf['changelog'].'_failed')) {
102171726d78SBen Coburn    msg('Importing old changelog failed.', -1);
1022d8186216SBen Coburn  } else if (@file_exists($conf['changelog'].'_importing')) {
102371726d78SBen Coburn    msg('Importing old changelog now.', 0);
1024d8186216SBen Coburn  } else if (@file_exists($conf['changelog'].'_import_ok')) {
102571726d78SBen Coburn    msg('Old changelog imported.', 1);
1026e45b34cdSBen Coburn    if (!plugin_isdisabled('importoldchangelog')) {
1027e45b34cdSBen Coburn      msg('Importoldchangelog plugin not disabled after import.', -1);
1028e45b34cdSBen Coburn    }
102971726d78SBen Coburn  }
1030f3f0262cSandi
1031f3f0262cSandi  if(is_writable($conf['datadir'])){
1032f3f0262cSandi    msg('Datadir is writable',1);
1033f3f0262cSandi  }else{
1034f3f0262cSandi    msg('Datadir is not writable',-1);
1035f3f0262cSandi  }
1036f3f0262cSandi
1037f3f0262cSandi  if(is_writable($conf['olddir'])){
1038f3f0262cSandi    msg('Attic is writable',1);
1039f3f0262cSandi  }else{
1040f3f0262cSandi    msg('Attic is not writable',-1);
1041f3f0262cSandi  }
1042f3f0262cSandi
1043f3f0262cSandi  if(is_writable($conf['mediadir'])){
1044f3f0262cSandi    msg('Mediadir is writable',1);
1045f3f0262cSandi  }else{
1046f3f0262cSandi    msg('Mediadir is not writable',-1);
1047f3f0262cSandi  }
1048f3f0262cSandi
104998407a7aSandi  if(is_writable($conf['cachedir'])){
105098407a7aSandi    msg('Cachedir is writable',1);
105198407a7aSandi  }else{
105298407a7aSandi    msg('Cachedir is not writable',-1);
105398407a7aSandi  }
105498407a7aSandi
10557de6c234SAndreas Gohr  if(is_writable($conf['lockdir'])){
10567de6c234SAndreas Gohr    msg('Lockdir is writable',1);
10577de6c234SAndreas Gohr  }else{
10587de6c234SAndreas Gohr    msg('Lockdir is not writable',-1);
10597de6c234SAndreas Gohr  }
10607de6c234SAndreas Gohr
1061e7cb32dcSAndreas Gohr  if(is_writable(DOKU_CONF.'users.auth.php')){
10628c4f28e8Sjan    msg('conf/users.auth.php is writable',1);
1063f3f0262cSandi  }else{
10648c4f28e8Sjan    msg('conf/users.auth.php is not writable',0);
1065f3f0262cSandi  }
106693a9e835Sandi
106793a9e835Sandi  if(function_exists('mb_strpos')){
106893a9e835Sandi    if(defined('UTF8_NOMBSTRING')){
106993a9e835Sandi      msg('mb_string extension is available but will not be used',0);
107093a9e835Sandi    }else{
107193a9e835Sandi      msg('mb_string extension is available and will be used',1);
107293a9e835Sandi    }
107393a9e835Sandi  }else{
107493a9e835Sandi    msg('mb_string extension not available - PHP only replacements will be used',0);
107593a9e835Sandi  }
1076f42d1c75SAndreas Gohr
1077f42d1c75SAndreas Gohr  if($conf['allowdebug']){
1078f42d1c75SAndreas Gohr    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1079f42d1c75SAndreas Gohr  }else{
1080f42d1c75SAndreas Gohr    msg('Debugging support is disabled',1);
1081f42d1c75SAndreas Gohr  }
1082f3f0262cSandi
1083f3f0262cSandi  msg('Your current permission for this page is '.$INFO['perm'],0);
1084f3f0262cSandi
1085f3f0262cSandi  if(is_writable($INFO['filepath'])){
1086f3f0262cSandi    msg('The current page is writable by the webserver',0);
1087f3f0262cSandi  }else{
1088f3f0262cSandi    msg('The current page is not writable by the webserver',0);
1089f3f0262cSandi  }
1090f3f0262cSandi
1091f3f0262cSandi  if($INFO['writable']){
1092f3f0262cSandi    msg('The current page is writable by you',0);
1093f3f0262cSandi  }else{
1094f3f0262cSandi    msg('The current page is not writable you',0);
1095f3f0262cSandi  }
1096f3f0262cSandi}
1097340756e4Sandi
1098b158d625SSteven Danz/**
1099b158d625SSteven Danz * Let us know if a user is tracking a page
1100b158d625SSteven Danz *
11011380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1102b158d625SSteven Danz */
11031380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
11041380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
11051380fc45SAndreas Gohr  if (@file_exists($file)) {
1106b158d625SSteven Danz    $mlist = file($file);
11071380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
11081380fc45SAndreas Gohr    return is_int($pos);
1109b158d625SSteven Danz  }
11101380fc45SAndreas Gohr
1111b158d625SSteven Danz  return false;
1112b158d625SSteven Danz}
1113340756e4Sandi
1114f9eb5648Ssteven-danz/**
1115f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1116f9eb5648Ssteven-danz * users subscribed to a page
1117f9eb5648Ssteven-danz *
111826a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1119f9eb5648Ssteven-danz */
1120f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1121f9eb5648Ssteven-danz  global $conf;
1122cd52f92dSchris  global $auth;
1123f9eb5648Ssteven-danz
1124f9eb5648Ssteven-danz  $emails = '';
1125f9eb5648Ssteven-danz
112626a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
112726a0801fSAndreas Gohr
1128f9eb5648Ssteven-danz  $mlist = array();
1129f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1130d8186216SBen Coburn  if (@file_exists($file)) {
1131f9eb5648Ssteven-danz    $mlist = file($file);
1132f9eb5648Ssteven-danz  }
1133f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1134f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1135f9eb5648Ssteven-danz      $who = rtrim($who);
1136cd52f92dSchris      $info = $auth->getUserData($who);
1137f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1138f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1139f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1140f9eb5648Ssteven-danz          if (empty($emails)) {
1141f9eb5648Ssteven-danz            $emails = $info['mail'];
1142f9eb5648Ssteven-danz          } else {
1143f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1144f9eb5648Ssteven-danz          }
1145f9eb5648Ssteven-danz        }
1146f9eb5648Ssteven-danz      }
1147f9eb5648Ssteven-danz    }
1148f9eb5648Ssteven-danz  }
1149f9eb5648Ssteven-danz
1150f9eb5648Ssteven-danz  return $emails;
1151f9eb5648Ssteven-danz}
1152f9eb5648Ssteven-danz
115389541d4bSAndreas Gohr/**
115489541d4bSAndreas Gohr * Removes quoting backslashes
115589541d4bSAndreas Gohr *
115689541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
115789541d4bSAndreas Gohr */
115889541d4bSAndreas Gohrfunction unslash($string,$char="'"){
115989541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
116089541d4bSAndreas Gohr}
116189541d4bSAndreas Gohr
1162340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1163