xref: /dokuwiki/inc/common.php (revision 63211f61be6c6109a6858dc974e91facec45aafd)
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
900976812SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',fullpath(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 */
4225ec097bSChris Smithfunction ptln($string,$indent=0){
4325ec097bSChris Smith  echo str_repeat(' ', $indent)."$string\n";
4402b0b681SAndreas Gohr}
4502b0b681SAndreas Gohr
4602b0b681SAndreas Gohr/**
4702b0b681SAndreas Gohr * strips control characters (<32) from the given string
4802b0b681SAndreas Gohr *
4902b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
5002b0b681SAndreas Gohr */
5102b0b681SAndreas Gohrfunction stripctl($string){
5202b0b681SAndreas Gohr  return preg_replace('/[\x00-\x1F]+/s','',$string);
53d5197206Schris}
54d5197206Schris
55d5197206Schris/**
56634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention
57634d7150SAndreas Gohr *
58634d7150SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
59634d7150SAndreas Gohr * @link    http://en.wikipedia.org/wiki/Cross-site_request_forgery
60634d7150SAndreas Gohr * @link    http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html
61634d7150SAndreas Gohr * @return  string
62634d7150SAndreas Gohr */
63634d7150SAndreas Gohrfunction getSecurityToken(){
64634d7150SAndreas Gohr  return md5(auth_cookiesalt().session_id());
65634d7150SAndreas Gohr}
66634d7150SAndreas Gohr
67634d7150SAndreas Gohr/**
68634d7150SAndreas Gohr * Check the secret CSRF token
69634d7150SAndreas Gohr */
70634d7150SAndreas Gohrfunction checkSecurityToken($token=null){
71634d7150SAndreas Gohr  if(is_null($token)) $token = $_REQUEST['sectok'];
72634d7150SAndreas Gohr  if(getSecurityToken() != $token){
73634d7150SAndreas Gohr    msg('Security Token did not match. Possible CSRF attack.',-1);
74634d7150SAndreas Gohr    return false;
75634d7150SAndreas Gohr  }
76634d7150SAndreas Gohr  return true;
77634d7150SAndreas Gohr}
78634d7150SAndreas Gohr
79634d7150SAndreas Gohr/**
80634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token
81634d7150SAndreas Gohr *
82634d7150SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
83634d7150SAndreas Gohr */
84634d7150SAndreas Gohrfunction formSecurityToken($print=true){
85634d7150SAndreas Gohr  $ret = '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'."\n";
86634d7150SAndreas Gohr  if($print){
87634d7150SAndreas Gohr    echo $ret;
88634d7150SAndreas Gohr  }else{
89634d7150SAndreas Gohr    return $ret;
90634d7150SAndreas Gohr  }
91634d7150SAndreas Gohr}
92634d7150SAndreas Gohr
93634d7150SAndreas Gohr/**
9415fae107Sandi * Return info about the current document as associative
95f3f0262cSandi * array.
9615fae107Sandi *
9715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
98f3f0262cSandi */
99f3f0262cSandifunction pageinfo(){
100f3f0262cSandi  global $ID;
101f3f0262cSandi  global $REV;
102f3f0262cSandi  global $USERINFO;
103f3f0262cSandi  global $conf;
104f3f0262cSandi
1056afe8dcaSchris  // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml
1066afe8dcaSchris  // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary
1076afe8dcaSchris  $info['id'] = $ID;
1086afe8dcaSchris  $info['rev'] = $REV;
1096afe8dcaSchris
110f3f0262cSandi  if($_SERVER['REMOTE_USER']){
111f3f0262cSandi    $info['userinfo']   = $USERINFO;
112f3f0262cSandi    $info['perm']       = auth_quickaclcheck($ID);
1131380fc45SAndreas Gohr    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
114ee4c4a1bSAndreas Gohr    $info['client']     = $_SERVER['REMOTE_USER'];
11517ee7f66SAndreas Gohr
116f8cc712eSAndreas Gohr    // set info about manager/admin status
117f8cc712eSAndreas Gohr    $info['isadmin']   = false;
118f8cc712eSAndreas Gohr    $info['ismanager'] = false;
119f8cc712eSAndreas Gohr    if($info['perm'] == AUTH_ADMIN){
120f8cc712eSAndreas Gohr      $info['isadmin']   = true;
121f8cc712eSAndreas Gohr      $info['ismanager'] = true;
122f8cc712eSAndreas Gohr    }elseif(auth_ismanager()){
123f8cc712eSAndreas Gohr      $info['ismanager'] = true;
124f8cc712eSAndreas Gohr    }
125f8cc712eSAndreas Gohr
12617ee7f66SAndreas Gohr    // if some outside auth were used only REMOTE_USER is set
12717ee7f66SAndreas Gohr    if(!$info['userinfo']['name']){
12817ee7f66SAndreas Gohr      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
12917ee7f66SAndreas Gohr    }
130ee4c4a1bSAndreas Gohr
131f3f0262cSandi  }else{
132f3f0262cSandi    $info['perm']       = auth_aclcheck($ID,'',null);
1331380fc45SAndreas Gohr    $info['subscribed'] = false;
134ee4c4a1bSAndreas Gohr    $info['client']     = clientIP(true);
135f3f0262cSandi  }
136f3f0262cSandi
137f3f0262cSandi  $info['namespace'] = getNS($ID);
138f3f0262cSandi  $info['locked']    = checklock($ID);
13900976812SAndreas Gohr  $info['filepath']  = fullpath(wikiFN($ID));
1402ca9d91cSBen Coburn  $info['exists']    = @file_exists($info['filepath']);
1412ca9d91cSBen Coburn  if($REV){
1422ca9d91cSBen Coburn    //check if current revision was meant
1432ca9d91cSBen Coburn    if($info['exists'] && (@filemtime($info['filepath'])==$REV)){
1442ca9d91cSBen Coburn      $REV = '';
1452ca9d91cSBen Coburn    }else{
1462ca9d91cSBen Coburn      //really use old revision
14700976812SAndreas Gohr      $info['filepath'] = fullpath(wikiFN($ID,$REV));
148f3f0262cSandi      $info['exists']   = @file_exists($info['filepath']);
149f3f0262cSandi    }
150f3f0262cSandi  }
151c112d578Sandi  $info['rev'] = $REV;
152f3f0262cSandi  if($info['exists']){
153f3f0262cSandi    $info['writable'] = (is_writable($info['filepath']) &&
154f3f0262cSandi                         ($info['perm'] >= AUTH_EDIT));
155f3f0262cSandi  }else{
156f3f0262cSandi    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
157f3f0262cSandi  }
158f3f0262cSandi  $info['editable']  = ($info['writable'] && empty($info['lock']));
159f3f0262cSandi  $info['lastmod']   = @filemtime($info['filepath']);
160f3f0262cSandi
16171726d78SBen Coburn  //load page meta data
16271726d78SBen Coburn  $info['meta'] = p_get_metadata($ID);
16371726d78SBen Coburn
164652610a2Sandi  //who's the editor
165652610a2Sandi  if($REV){
16671726d78SBen Coburn    $revinfo = getRevisionInfo($ID, $REV, 1024);
167652610a2Sandi  }else{
168aa27cf05SAndreas Gohr    if (is_array($info['meta']['last_change'])) {
169aa27cf05SAndreas Gohr       $revinfo = $info['meta']['last_change'];
170aa27cf05SAndreas Gohr    } else {
171cd00a034SBen Coburn      $revinfo = getRevisionInfo($ID, $info['lastmod'], 1024);
172cd00a034SBen Coburn      // cache most recent changelog line in metadata if missing and still valid
173cd00a034SBen Coburn      if ($revinfo!==false) {
174cd00a034SBen Coburn        $info['meta']['last_change'] = $revinfo;
175cd00a034SBen Coburn        p_set_metadata($ID, array('last_change' => $revinfo));
176cd00a034SBen Coburn      }
177cd00a034SBen Coburn    }
178cd00a034SBen Coburn  }
179cd00a034SBen Coburn  //and check for an external edit
180cd00a034SBen Coburn  if($revinfo!==false && $revinfo['date']!=$info['lastmod']){
181cd00a034SBen Coburn    // cached changelog line no longer valid
182cd00a034SBen Coburn    $revinfo = false;
183cd00a034SBen Coburn    $info['meta']['last_change'] = $revinfo;
184cd00a034SBen Coburn    p_set_metadata($ID, array('last_change' => $revinfo));
185652610a2Sandi  }
186bb4866bdSchris
187652610a2Sandi  $info['ip']     = $revinfo['ip'];
188652610a2Sandi  $info['user']   = $revinfo['user'];
189652610a2Sandi  $info['sum']    = $revinfo['sum'];
19071726d78SBen Coburn  // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID.
191ebf1501fSBen Coburn  // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor'].
19259f257aeSchris
19388f522e9Sandi  if($revinfo['user']){
19488f522e9Sandi    $info['editor'] = $revinfo['user'];
19588f522e9Sandi  }else{
19688f522e9Sandi    $info['editor'] = $revinfo['ip'];
19788f522e9Sandi  }
198652610a2Sandi
199ee4c4a1bSAndreas Gohr  // draft
200ee4c4a1bSAndreas Gohr  $draft = getCacheName($info['client'].$ID,'.draft');
201ee4c4a1bSAndreas Gohr  if(@file_exists($draft)){
202ee4c4a1bSAndreas Gohr    if(@filemtime($draft) < @filemtime(wikiFN($ID))){
203ee4c4a1bSAndreas Gohr      // remove stale draft
204ee4c4a1bSAndreas Gohr      @unlink($draft);
205ee4c4a1bSAndreas Gohr    }else{
206ee4c4a1bSAndreas Gohr      $info['draft'] = $draft;
207ee4c4a1bSAndreas Gohr    }
208ee4c4a1bSAndreas Gohr  }
209ee4c4a1bSAndreas Gohr
210f3f0262cSandi  return $info;
211f3f0262cSandi}
212f3f0262cSandi
213f3f0262cSandi/**
2142684e50aSAndreas Gohr * Build an string of URL parameters
2152684e50aSAndreas Gohr *
2162684e50aSAndreas Gohr * @author Andreas Gohr
2172684e50aSAndreas Gohr */
218b174aeaeSchrisfunction buildURLparams($params, $sep='&amp;'){
2192684e50aSAndreas Gohr  $url = '';
2202684e50aSAndreas Gohr  $amp = false;
2212684e50aSAndreas Gohr  foreach($params as $key => $val){
222b174aeaeSchris    if($amp) $url .= $sep;
2232684e50aSAndreas Gohr
2242684e50aSAndreas Gohr    $url .= $key.'=';
225b6c6979fSAndreas Gohr    $url .= rawurlencode($val);
2262684e50aSAndreas Gohr    $amp = true;
2272684e50aSAndreas Gohr  }
2282684e50aSAndreas Gohr  return $url;
2292684e50aSAndreas Gohr}
2302684e50aSAndreas Gohr
2312684e50aSAndreas Gohr/**
2322684e50aSAndreas Gohr * Build an string of html tag attributes
2332684e50aSAndreas Gohr *
2347bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded
2357bff22c0SAndreas Gohr *
2362684e50aSAndreas Gohr * @author Andreas Gohr
2372684e50aSAndreas Gohr */
2384b030ce7SAndreas Gohrfunction buildAttributes($params,$skipempty=false){
2392684e50aSAndreas Gohr  $url = '';
2402684e50aSAndreas Gohr  foreach($params as $key => $val){
2417bff22c0SAndreas Gohr    if($key{0} == '_') continue;
242b1c94f1dSAndreas Gohr    if($val === '' && $skipempty) continue;
2437bff22c0SAndreas Gohr
2442684e50aSAndreas Gohr    $url .= $key.'="';
2452684e50aSAndreas Gohr    $url .= htmlspecialchars ($val);
2462684e50aSAndreas Gohr    $url .= '" ';
2472684e50aSAndreas Gohr  }
2482684e50aSAndreas Gohr  return $url;
2492684e50aSAndreas Gohr}
2502684e50aSAndreas Gohr
2512684e50aSAndreas Gohr
2522684e50aSAndreas Gohr/**
25315fae107Sandi * This builds the breadcrumb trail and returns it as array
25415fae107Sandi *
25515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
256f3f0262cSandi */
257f3f0262cSandifunction breadcrumbs(){
2588746e727Sandi  // we prepare the breadcrumbs early for quick session closing
2598746e727Sandi  static $crumbs = null;
2608746e727Sandi  if($crumbs != null) return $crumbs;
2618746e727Sandi
262f3f0262cSandi  global $ID;
263f3f0262cSandi  global $ACT;
264f3f0262cSandi  global $conf;
265e71ce681SAndreas Gohr  $crumbs = $_SESSION[DOKU_COOKIE]['bc'];
266f3f0262cSandi
267f3f0262cSandi  //first visit?
268f3f0262cSandi  if (!is_array($crumbs)){
269f3f0262cSandi    $crumbs = array();
270f3f0262cSandi  }
271f3f0262cSandi  //we only save on show and existing wiki documents
272a77f5846Sjan  $file = wikiFN($ID);
273a77f5846Sjan  if($ACT != 'show' || !@file_exists($file)){
274e71ce681SAndreas Gohr    $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
275f3f0262cSandi    return $crumbs;
276f3f0262cSandi  }
277a77f5846Sjan
278a77f5846Sjan  // page names
2791a84a0f3SAnika Henke  $name = noNSorNS($ID);
280a77f5846Sjan  if ($conf['useheading']) {
281a77f5846Sjan    // get page title
282955cd091SChris Smith    $title = p_get_first_heading($ID,true);
283a77f5846Sjan    if ($title) {
284a77f5846Sjan      $name = $title;
285a77f5846Sjan    }
286a77f5846Sjan  }
287a77f5846Sjan
288f3f0262cSandi  //remove ID from array
289a77f5846Sjan  if (isset($crumbs[$ID])) {
290a77f5846Sjan    unset($crumbs[$ID]);
291f3f0262cSandi  }
292f3f0262cSandi
293f3f0262cSandi  //add to array
294a77f5846Sjan  $crumbs[$ID] = $name;
295f3f0262cSandi  //reduce size
296f3f0262cSandi  while(count($crumbs) > $conf['breadcrumbs']){
297f3f0262cSandi    array_shift($crumbs);
298f3f0262cSandi  }
299f3f0262cSandi  //save to session
300e71ce681SAndreas Gohr  $_SESSION[DOKU_COOKIE]['bc'] = $crumbs;
301f3f0262cSandi  return $crumbs;
302f3f0262cSandi}
303f3f0262cSandi
304f3f0262cSandi/**
30515fae107Sandi * Filter for page IDs
30615fae107Sandi *
307f3f0262cSandi * This is run on a ID before it is outputted somewhere
308f3f0262cSandi * currently used to replace the colon with something else
309f3f0262cSandi * on Windows systems and to have proper URL encoding
31015fae107Sandi *
31149c713a3Sandi * Urlencoding is ommitted when the second parameter is false
31249c713a3Sandi *
31315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
314f3f0262cSandi */
31549c713a3Sandifunction idfilter($id,$ue=true){
316f3f0262cSandi  global $conf;
317f3f0262cSandi  if ($conf['useslash'] && $conf['userewrite']){
318f3f0262cSandi    $id = strtr($id,':','/');
319f3f0262cSandi  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
320f3f0262cSandi      $conf['userewrite']) {
321f3f0262cSandi    $id = strtr($id,':',';');
322f3f0262cSandi  }
32349c713a3Sandi  if($ue){
324b6c6979fSAndreas Gohr    $id = rawurlencode($id);
325f3f0262cSandi    $id = str_replace('%3A',':',$id); //keep as colon
326f3f0262cSandi    $id = str_replace('%2F','/',$id); //keep as slash
32749c713a3Sandi  }
328f3f0262cSandi  return $id;
329f3f0262cSandi}
330f3f0262cSandi
331f3f0262cSandi/**
332ed7b5f09Sandi * This builds a link to a wikipage
33315fae107Sandi *
3346c7843b5Sandi * It handles URL rewriting and adds additional parameter if
3356c7843b5Sandi * given in $more
3366c7843b5Sandi *
33715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
338f3f0262cSandi */
339b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&amp;'){
340f3f0262cSandi  global $conf;
3416de3759aSAndreas Gohr  if(is_array($more)){
342b174aeaeSchris    $more = buildURLparams($more,$sep);
3436de3759aSAndreas Gohr  }else{
344b174aeaeSchris    $more = str_replace(',',$sep,$more);
3456de3759aSAndreas Gohr  }
346f3f0262cSandi
347f3f0262cSandi  $id    = idfilter($id);
348ed7b5f09Sandi  if($abs){
349ed7b5f09Sandi    $xlink = DOKU_URL;
350ed7b5f09Sandi  }else{
351ed7b5f09Sandi    $xlink = DOKU_BASE;
352ed7b5f09Sandi  }
353f3f0262cSandi
3546c7843b5Sandi  if($conf['userewrite'] == 2){
3556c7843b5Sandi    $xlink .= DOKU_SCRIPT.'/'.$id;
3566c7843b5Sandi    if($more) $xlink .= '?'.$more;
3576c7843b5Sandi  }elseif($conf['userewrite']){
358f3f0262cSandi    $xlink .= $id;
359f3f0262cSandi    if($more) $xlink .= '?'.$more;
3606c7843b5Sandi  }else{
3616c7843b5Sandi    $xlink .= DOKU_SCRIPT.'?id='.$id;
362b174aeaeSchris    if($more) $xlink .= $sep.$more;
363f3f0262cSandi  }
364f3f0262cSandi
365f3f0262cSandi  return $xlink;
366f3f0262cSandi}
367f3f0262cSandi
368f3f0262cSandi/**
369f5c2808fSBen Coburn * This builds a link to an alternate page format
370f5c2808fSBen Coburn *
371f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl().
372f5c2808fSBen Coburn *
373f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
374f5c2808fSBen Coburn */
375f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&amp;'){
376f5c2808fSBen Coburn  global $conf;
377f5c2808fSBen Coburn  if(is_array($more)){
378f5c2808fSBen Coburn    $more = buildURLparams($more,$sep);
379f5c2808fSBen Coburn  }else{
380f5c2808fSBen Coburn    $more = str_replace(',',$sep,$more);
381f5c2808fSBen Coburn  }
382f5c2808fSBen Coburn
383f5c2808fSBen Coburn  $format = rawurlencode($format);
384f5c2808fSBen Coburn  $id = idfilter($id);
385f5c2808fSBen Coburn  if($abs){
386f5c2808fSBen Coburn    $xlink = DOKU_URL;
387f5c2808fSBen Coburn  }else{
388f5c2808fSBen Coburn    $xlink = DOKU_BASE;
389f5c2808fSBen Coburn  }
390f5c2808fSBen Coburn
391f5c2808fSBen Coburn  if($conf['userewrite'] == 2){
392f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format;
393f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
394f5c2808fSBen Coburn  }elseif($conf['userewrite'] == 1){
395f5c2808fSBen Coburn    $xlink .= '_export/'.$format.'/'.$id;
396f5c2808fSBen Coburn    if($more) $xlink .= '?'.$more;
397f5c2808fSBen Coburn  }else{
398f5c2808fSBen Coburn    $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id;
399f5c2808fSBen Coburn    if($more) $xlink .= $sep.$more;
400f5c2808fSBen Coburn  }
401f5c2808fSBen Coburn
402f5c2808fSBen Coburn  return $xlink;
403f5c2808fSBen Coburn}
404f5c2808fSBen Coburn
405f5c2808fSBen Coburn/**
4066de3759aSAndreas Gohr * Build a link to a media file
4076de3759aSAndreas Gohr *
4086de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false
4096de3759aSAndreas Gohr */
41055b2b31bSAndreas Gohrfunction ml($id='',$more='',$direct=true,$sep='&amp;',$abs=false){
4116de3759aSAndreas Gohr  global $conf;
4126de3759aSAndreas Gohr  if(is_array($more)){
413b174aeaeSchris    $more = buildURLparams($more,$sep);
4146de3759aSAndreas Gohr  }else{
415b174aeaeSchris    $more = str_replace(',',$sep,$more);
4166de3759aSAndreas Gohr  }
4176de3759aSAndreas Gohr
41855b2b31bSAndreas Gohr  if($abs){
41955b2b31bSAndreas Gohr    $xlink = DOKU_URL;
42055b2b31bSAndreas Gohr  }else{
4216de3759aSAndreas Gohr    $xlink = DOKU_BASE;
42255b2b31bSAndreas Gohr  }
4236de3759aSAndreas Gohr
4246de3759aSAndreas Gohr  // external URLs are always direct without rewriting
4256de3759aSAndreas Gohr  if(preg_match('#^(https?|ftp)://#i',$id)){
4266de3759aSAndreas Gohr    $xlink .= 'lib/exe/fetch.php';
4276de3759aSAndreas Gohr    if($more){
4286de3759aSAndreas Gohr      $xlink .= '?'.$more;
429b174aeaeSchris      $xlink .= $sep.'media='.rawurlencode($id);
4306de3759aSAndreas Gohr    }else{
431b6c6979fSAndreas Gohr      $xlink .= '?media='.rawurlencode($id);
4326de3759aSAndreas Gohr    }
4336de3759aSAndreas Gohr    return $xlink;
4346de3759aSAndreas Gohr  }
4356de3759aSAndreas Gohr
4366de3759aSAndreas Gohr  $id = idfilter($id);
4376de3759aSAndreas Gohr
4386de3759aSAndreas Gohr  // decide on scriptname
4396de3759aSAndreas Gohr  if($direct){
4406de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
4416de3759aSAndreas Gohr      $script = '_media';
4426de3759aSAndreas Gohr    }else{
4436de3759aSAndreas Gohr      $script = 'lib/exe/fetch.php';
4446de3759aSAndreas Gohr    }
4456de3759aSAndreas Gohr  }else{
4466de3759aSAndreas Gohr    if($conf['userewrite'] == 1){
4476de3759aSAndreas Gohr      $script = '_detail';
4486de3759aSAndreas Gohr    }else{
4496de3759aSAndreas Gohr      $script = 'lib/exe/detail.php';
4506de3759aSAndreas Gohr    }
4516de3759aSAndreas Gohr  }
4526de3759aSAndreas Gohr
4536de3759aSAndreas Gohr  // build URL based on rewrite mode
4546de3759aSAndreas Gohr   if($conf['userewrite']){
4556de3759aSAndreas Gohr     $xlink .= $script.'/'.$id;
4566de3759aSAndreas Gohr     if($more) $xlink .= '?'.$more;
4576de3759aSAndreas Gohr   }else{
4586de3759aSAndreas Gohr     if($more){
459a99d3236SEsther Brunner       $xlink .= $script.'?'.$more;
460b174aeaeSchris       $xlink .= $sep.'media='.$id;
4616de3759aSAndreas Gohr     }else{
462a99d3236SEsther Brunner       $xlink .= $script.'?media='.$id;
4636de3759aSAndreas Gohr     }
4646de3759aSAndreas Gohr   }
4656de3759aSAndreas Gohr
4666de3759aSAndreas Gohr  return $xlink;
4676de3759aSAndreas Gohr}
4686de3759aSAndreas Gohr
4696de3759aSAndreas Gohr
4706de3759aSAndreas Gohr
4716de3759aSAndreas Gohr/**
472f3f0262cSandi * Just builds a link to a script
47315fae107Sandi *
474ed7b5f09Sandi * @todo   maybe obsolete
47515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
476f3f0262cSandi */
477f3f0262cSandifunction script($script='doku.php'){
478ed7b5f09Sandi#  $link = getBaseURL();
479ed7b5f09Sandi#  $link .= $script;
480ed7b5f09Sandi#  return $link;
481ed7b5f09Sandi  return DOKU_BASE.DOKU_SCRIPT;
482f3f0262cSandi}
483f3f0262cSandi
484f3f0262cSandi/**
48515fae107Sandi * Spamcheck against wordlist
48615fae107Sandi *
487f3f0262cSandi * Checks the wikitext against a list of blocked expressions
488f3f0262cSandi * returns true if the text contains any bad words
48915fae107Sandi *
49015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
491f3f0262cSandi */
492f3f0262cSandifunction checkwordblock(){
493f3f0262cSandi  global $TEXT;
494f3f0262cSandi  global $conf;
495f3f0262cSandi
496f3f0262cSandi  if(!$conf['usewordblock']) return false;
497f3f0262cSandi
498041d1964SAndreas Gohr  // we prepare the text a tiny bit to prevent spammers circumventing URL checks
499041d1964SAndreas Gohr  $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i','\1http://\2 \2\3',$TEXT);
500041d1964SAndreas Gohr
501b9ac8716Schris  $wordblocks = getWordblocks();
5023e2965d7Sandi  //how many lines to read at once (to work around some PCRE limits)
5033e2965d7Sandi  if(version_compare(phpversion(),'4.3.0','<')){
5043e2965d7Sandi    //old versions of PCRE define a maximum of parenthesises even if no
5053e2965d7Sandi    //backreferences are used - the maximum is 99
5063e2965d7Sandi    //this is very bad performancewise and may even be too high still
5073e2965d7Sandi    $chunksize = 40;
5083e2965d7Sandi  }else{
509a51d08efSAndreas Gohr    //read file in chunks of 200 - this should work around the
5103e2965d7Sandi    //MAX_PATTERN_SIZE in modern PCRE
511a51d08efSAndreas Gohr    $chunksize = 200;
5123e2965d7Sandi  }
513b9ac8716Schris  while($blocks = array_splice($wordblocks,0,$chunksize)){
514f3f0262cSandi    $re = array();
515f3f0262cSandi    #build regexp from blocks
516f3f0262cSandi    foreach($blocks as $block){
517f3f0262cSandi      $block = preg_replace('/#.*$/','',$block);
518f3f0262cSandi      $block = trim($block);
519f3f0262cSandi      if(empty($block)) continue;
520f3f0262cSandi      $re[]  = $block;
521f3f0262cSandi    }
522c2525298SAndreas Gohr    if(count($re) && preg_match('#('.join('|',$re).')#si',$text)) {
523b9ac8716Schris      return true;
524b9ac8716Schris    }
525703f6fdeSandi  }
526f3f0262cSandi  return false;
527f3f0262cSandi}
528f3f0262cSandi
529f3f0262cSandi/**
53015fae107Sandi * Return the IP of the client
53115fae107Sandi *
5326d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers
53315fae107Sandi *
5346d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned
5356d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return
5366d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X
5376d8affe6SAndreas Gohr * headers
5386d8affe6SAndreas Gohr *
5396d8affe6SAndreas Gohr * @param  boolean $single If set only a single IP is returned
54015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
541f3f0262cSandi */
5426d8affe6SAndreas Gohrfunction clientIP($single=false){
5436d8affe6SAndreas Gohr  $ip = array();
5446d8affe6SAndreas Gohr  $ip[] = $_SERVER['REMOTE_ADDR'];
545bb4866bdSchris  if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
5466d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']));
547bb4866bdSchris  if(!empty($_SERVER['HTTP_X_REAL_IP']))
5486d8affe6SAndreas Gohr    $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP']));
5496d8affe6SAndreas Gohr
5506d8affe6SAndreas Gohr  // remove any non-IP stuff
5516d8affe6SAndreas Gohr  $cnt = count($ip);
5524ff28443Schris  $match = array();
5536d8affe6SAndreas Gohr  for($i=0; $i<$cnt; $i++){
5544ff28443Schris    if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) {
5554ff28443Schris      $ip[$i] = $match[0];
5564ff28443Schris    } else {
5574ff28443Schris      $ip[$i] = '';
5584ff28443Schris    }
5596d8affe6SAndreas Gohr    if(empty($ip[$i])) unset($ip[$i]);
560f3f0262cSandi  }
5616d8affe6SAndreas Gohr  $ip = array_values(array_unique($ip));
5626d8affe6SAndreas Gohr  if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP
5636d8affe6SAndreas Gohr
5646d8affe6SAndreas Gohr  if(!$single) return join(',',$ip);
5656d8affe6SAndreas Gohr
5666d8affe6SAndreas Gohr  // decide which IP to use, trying to avoid local addresses
5676d8affe6SAndreas Gohr  $ip = array_reverse($ip);
5686d8affe6SAndreas Gohr  foreach($ip as $i){
5696d8affe6SAndreas Gohr    if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){
5706d8affe6SAndreas Gohr      continue;
5716d8affe6SAndreas Gohr    }else{
5726d8affe6SAndreas Gohr      return $i;
5736d8affe6SAndreas Gohr    }
5746d8affe6SAndreas Gohr  }
5756d8affe6SAndreas Gohr  // still here? just use the first (last) address
5766d8affe6SAndreas Gohr  return $ip[0];
577f3f0262cSandi}
578f3f0262cSandi
579f3f0262cSandi/**
580*63211f61SGlen Harris * Convert one or more comma separated IPs to hostnames
581*63211f61SGlen Harris *
582*63211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org>
583*63211f61SGlen Harris * @returns a comma separated list of hostnames
584*63211f61SGlen Harris */
585*63211f61SGlen Harrisfunction gethostsbyaddrs($ips){
586*63211f61SGlen Harris  $hosts = array();
587*63211f61SGlen Harris  $ips = explode(',',$ips);
588*63211f61SGlen Harris  foreach($ip as $ip){
589*63211f61SGlen Harris    $host[] = gethostbyaddr(trim($ip));
590*63211f61SGlen Harris  }
591*63211f61SGlen Harris  return join(',',$host);;
592*63211f61SGlen Harris}
593*63211f61SGlen Harris
594*63211f61SGlen Harris/**
59515fae107Sandi * Checks if a given page is currently locked.
59615fae107Sandi *
597f3f0262cSandi * removes stale lockfiles
59815fae107Sandi *
59915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
600f3f0262cSandi */
601f3f0262cSandifunction checklock($id){
602f3f0262cSandi  global $conf;
603c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
604f3f0262cSandi
605f3f0262cSandi  //no lockfile
606f3f0262cSandi  if(!@file_exists($lock)) return false;
607f3f0262cSandi
608f3f0262cSandi  //lockfile expired
609f3f0262cSandi  if((time() - filemtime($lock)) > $conf['locktime']){
610d8186216SBen Coburn    @unlink($lock);
611f3f0262cSandi    return false;
612f3f0262cSandi  }
613f3f0262cSandi
614f3f0262cSandi  //my own lock
615f3f0262cSandi  $ip = io_readFile($lock);
616f3f0262cSandi  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
617f3f0262cSandi    return false;
618f3f0262cSandi  }
619f3f0262cSandi
620f3f0262cSandi  return $ip;
621f3f0262cSandi}
622f3f0262cSandi
623f3f0262cSandi/**
62415fae107Sandi * Lock a page for editing
62515fae107Sandi *
62615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
627f3f0262cSandi */
628f3f0262cSandifunction lock($id){
629c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
630f3f0262cSandi  if($_SERVER['REMOTE_USER']){
631f3f0262cSandi    io_saveFile($lock,$_SERVER['REMOTE_USER']);
632f3f0262cSandi  }else{
633f3f0262cSandi    io_saveFile($lock,clientIP());
634f3f0262cSandi  }
635f3f0262cSandi}
636f3f0262cSandi
637f3f0262cSandi/**
63815fae107Sandi * Unlock a page if it was locked by the user
639f3f0262cSandi *
64015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
64115fae107Sandi * @return bool true if a lock was removed
642f3f0262cSandi */
643f3f0262cSandifunction unlock($id){
644c9b4bd1eSBen Coburn  $lock = wikiLockFN($id);
645f3f0262cSandi  if(@file_exists($lock)){
646f3f0262cSandi    $ip = io_readFile($lock);
647f3f0262cSandi    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
648f3f0262cSandi      @unlink($lock);
649f3f0262cSandi      return true;
650f3f0262cSandi    }
651f3f0262cSandi  }
652f3f0262cSandi  return false;
653f3f0262cSandi}
654f3f0262cSandi
655f3f0262cSandi/**
656f3f0262cSandi * convert line ending to unix format
657f3f0262cSandi *
65815fae107Sandi * @see    formText() for 2crlf conversion
65915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
660f3f0262cSandi */
661f3f0262cSandifunction cleanText($text){
662f3f0262cSandi  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
663f3f0262cSandi  return $text;
664f3f0262cSandi}
665f3f0262cSandi
666f3f0262cSandi/**
667f3f0262cSandi * Prepares text for print in Webforms by encoding special chars.
668f3f0262cSandi * It also converts line endings to Windows format which is
669f3f0262cSandi * pseudo standard for webforms.
670f3f0262cSandi *
67115fae107Sandi * @see    cleanText() for 2unix conversion
67215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
673f3f0262cSandi */
674f3f0262cSandifunction formText($text){
6755b7d45a5SAndreas Gohr  $text = str_replace("\012","\015\012",$text);
676f3f0262cSandi  return htmlspecialchars($text);
677f3f0262cSandi}
678f3f0262cSandi
679f3f0262cSandi/**
68015fae107Sandi * Returns the specified local text in raw format
68115fae107Sandi *
68215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
683f3f0262cSandi */
684f3f0262cSandifunction rawLocale($id){
685f3f0262cSandi  return io_readFile(localeFN($id));
686f3f0262cSandi}
687f3f0262cSandi
688f3f0262cSandi/**
689f3f0262cSandi * Returns the raw WikiText
69015fae107Sandi *
69115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
692f3f0262cSandi */
693f3f0262cSandifunction rawWiki($id,$rev=''){
694cc7d0c94SBen Coburn  return io_readWikiPage(wikiFN($id, $rev), $id, $rev);
695f3f0262cSandi}
696f3f0262cSandi
697f3f0262cSandi/**
6987146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace
6997146cee2SAndreas Gohr *
7007146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
7017146cee2SAndreas Gohr */
702b7d5a5f0SAndreas Gohrfunction pageTemplate($data){
703b7d5a5f0SAndreas Gohr  $id = $data[0];
704a15ce62dSEsther Brunner  global $conf;
705a15ce62dSEsther Brunner  global $INFO;
706a15ce62dSEsther Brunner  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
707a15ce62dSEsther Brunner  $tpl = str_replace('@ID@',$id,$tpl);
708a15ce62dSEsther Brunner  $tpl = str_replace('@NS@',getNS($id),$tpl);
709a15ce62dSEsther Brunner  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
710a15ce62dSEsther Brunner  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
711a15ce62dSEsther Brunner  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
712a15ce62dSEsther Brunner  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
713a15ce62dSEsther Brunner  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
714a15ce62dSEsther Brunner  return $tpl;
7157146cee2SAndreas Gohr}
7167146cee2SAndreas Gohr
7177146cee2SAndreas Gohr
7187146cee2SAndreas Gohr/**
71915fae107Sandi * Returns the raw Wiki Text in three slices.
72015fae107Sandi *
72115fae107Sandi * The range parameter needs to have the form "from-to"
72215cfe303Sandi * and gives the range of the section in bytes - no
72315cfe303Sandi * UTF-8 awareness is needed.
724f3f0262cSandi * The returned order is prefix, section and suffix.
72515fae107Sandi *
72615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
727f3f0262cSandi */
728f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){
729f3f0262cSandi  list($from,$to) = split('-',$range,2);
730cc7d0c94SBen Coburn  $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev);
731f3f0262cSandi  if(!$from) $from = 0;
732c3d8e19bSandi  if(!$to)   $to   = strlen($text)+1;
733f3f0262cSandi
73415cfe303Sandi  $slices[0] = substr($text,0,$from-1);
73515cfe303Sandi  $slices[1] = substr($text,$from-1,$to-$from);
73615cfe303Sandi  $slices[2] = substr($text,$to);
737f3f0262cSandi
738f3f0262cSandi  return $slices;
739f3f0262cSandi}
740f3f0262cSandi
741f3f0262cSandi/**
74215fae107Sandi * Joins wiki text slices
74315fae107Sandi *
744f3f0262cSandi * function to join the text slices with correct lineendings again.
745f3f0262cSandi * When the pretty parameter is set to true it adds additional empty
746f3f0262cSandi * lines between sections if needed (used on saving).
74715fae107Sandi *
74815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
749f3f0262cSandi */
750f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){
751f3f0262cSandi
752f3f0262cSandi  if($pretty){
753f3f0262cSandi    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
754f3f0262cSandi    if($suf && substr($text,-1) != "\n") $text .= "\n";
755f3f0262cSandi  }
756f3f0262cSandi
757f3f0262cSandi  if($pre) $pre .= "\n";
758f3f0262cSandi  if($suf) $text .= "\n";
759f3f0262cSandi  return $pre.$text.$suf;
760f3f0262cSandi}
761f3f0262cSandi
762f3f0262cSandi/**
763a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage.
764a701424fSBen Coburn * Also directs changelog and attic updates.
76515fae107Sandi *
76615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
76771726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
768f3f0262cSandi */
769b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){
770a701424fSBen Coburn  /* Note to developers:
771a701424fSBen Coburn     This code is subtle and delicate. Test the behavior of
772a701424fSBen Coburn     the attic and changelog with dokuwiki and external edits
773a701424fSBen Coburn     after any changes. External edits change the wiki page
774a701424fSBen Coburn     directly without using php or dokuwiki.
775a701424fSBen Coburn  */
776f3f0262cSandi  global $conf;
777f3f0262cSandi  global $lang;
77871726d78SBen Coburn  global $REV;
779f3f0262cSandi  // ignore if no changes were made
780f3f0262cSandi  if($text == rawWiki($id,'')){
781f3f0262cSandi    return;
782f3f0262cSandi  }
783f3f0262cSandi
784f3f0262cSandi  $file = wikiFN($id);
785a701424fSBen Coburn  $old = @filemtime($file); // from page
78671726d78SBen Coburn  $wasRemoved = empty($text);
787d8186216SBen Coburn  $wasCreated = !@file_exists($file);
78871726d78SBen Coburn  $wasReverted = ($REV==true);
789e45b34cdSBen Coburn  $newRev = false;
790a701424fSBen Coburn  $oldRev = getRevisions($id, -1, 1, 1024); // from changelog
791a701424fSBen Coburn  $oldRev = (int)(empty($oldRev)?0:$oldRev[0]);
792a701424fSBen Coburn  if(!@file_exists(wikiFN($id, $old)) && @file_exists($file) && $old>=$oldRev) {
79346844156SBen Coburn    // add old revision to the attic if missing
79446844156SBen Coburn    saveOldRevision($id);
79546844156SBen Coburn    // add a changelog entry if this edit came from outside dokuwiki
796a701424fSBen Coburn    if ($old>$oldRev) {
797ebf1501fSBen Coburn      addLogEntry($old, $id, DOKU_CHANGE_TYPE_EDIT, $lang['external_edit'], '', array('ExternalEdit'=>true));
79846844156SBen Coburn      // remove soon to be stale instructions
79946844156SBen Coburn      $cache = new cache_instructions($id, $file);
80046844156SBen Coburn      $cache->removeCache();
80146844156SBen Coburn    }
80246844156SBen Coburn  }
803f3f0262cSandi
80471726d78SBen Coburn  if ($wasRemoved){
80530725328SGabriel Birke    // Send "update" event with empty data, so plugins can react to page deletion
80630725328SGabriel Birke    $data = array(array($file, '', false), getNS($id), noNS($id), false);
80730725328SGabriel Birke    trigger_event('IO_WIKIPAGE_WRITE', $data);
808e45b34cdSBen Coburn    // pre-save deleted revision
809e45b34cdSBen Coburn    @touch($file);
81046844156SBen Coburn    clearstatcache();
811e45b34cdSBen Coburn    $newRev = saveOldRevision($id);
812e1f3d9e1SEsther Brunner    // remove empty file
813f3f0262cSandi    @unlink($file);
81471726d78SBen Coburn    // remove old meta info...
815e1f3d9e1SEsther Brunner    $mfiles = metaFiles($id);
81671726d78SBen Coburn    $changelog = metaFN($id, '.changes');
817e1f3d9e1SEsther Brunner    foreach ($mfiles as $mfile) {
81871726d78SBen Coburn      // but keep per-page changelog to preserve page history
819d8186216SBen Coburn      if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); }
820b158d625SSteven Danz    }
821f3f0262cSandi    $del = true;
8223ce054b3Sandi    // autoset summary on deletion
8233ce054b3Sandi    if(empty($summary)) $summary = $lang['deleted'];
82453d6ccfeSandi    // remove empty namespaces
825cc7d0c94SBen Coburn    io_sweepNS($id, 'datadir');
826cc7d0c94SBen Coburn    io_sweepNS($id, 'mediadir');
827f3f0262cSandi  }else{
828cc7d0c94SBen Coburn    // save file (namespace dir is created in io_writeWikiPage)
829cc7d0c94SBen Coburn    io_writeWikiPage($file, $text, $id);
83046844156SBen Coburn    // pre-save the revision, to keep the attic in sync
83146844156SBen Coburn    $newRev = saveOldRevision($id);
832f3f0262cSandi    $del = false;
833f3f0262cSandi  }
834f3f0262cSandi
83571726d78SBen Coburn  // select changelog line type
83671726d78SBen Coburn  $extra = '';
837ebf1501fSBen Coburn  $type = DOKU_CHANGE_TYPE_EDIT;
83871726d78SBen Coburn  if ($wasReverted) {
839ebf1501fSBen Coburn    $type = DOKU_CHANGE_TYPE_REVERT;
84071726d78SBen Coburn    $extra = $REV;
84171726d78SBen Coburn  }
842ebf1501fSBen Coburn  else if ($wasCreated) { $type = DOKU_CHANGE_TYPE_CREATE; }
843ebf1501fSBen Coburn  else if ($wasRemoved) { $type = DOKU_CHANGE_TYPE_DELETE; }
844ebf1501fSBen Coburn  else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = DOKU_CHANGE_TYPE_MINOR_EDIT; } //minor edits only for logged in users
84571726d78SBen Coburn
846e45b34cdSBen Coburn  addLogEntry($newRev, $id, $type, $summary, $extra);
84726a0801fSAndreas Gohr  // send notify mails
84890033e9dSAndreas Gohr  notify($id,'admin',$old,$summary,$minor);
84990033e9dSAndreas Gohr  notify($id,'subscribers',$old,$summary,$minor);
850f3f0262cSandi
851ce6b63d9Schris  // update the purgefile (timestamp of the last time anything within the wiki was changed)
85298407a7aSandi  io_saveFile($conf['cachedir'].'/purgefile',time());
853f3f0262cSandi}
854f3f0262cSandi
855f3f0262cSandi/**
856f3f0262cSandi * moves the current version to the attic and returns its
857f3f0262cSandi * revision date
85815fae107Sandi *
85915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
860f3f0262cSandi */
861f3f0262cSandifunction saveOldRevision($id){
862f3f0262cSandi  global $conf;
863f3f0262cSandi  $oldf = wikiFN($id);
864f3f0262cSandi  if(!@file_exists($oldf)) return '';
865f3f0262cSandi  $date = filemtime($oldf);
866f3f0262cSandi  $newf = wikiFN($id,$date);
867cc7d0c94SBen Coburn  io_writeWikiPage($newf, rawWiki($id), $id, $date);
868f3f0262cSandi  return $date;
869f3f0262cSandi}
870f3f0262cSandi
871f3f0262cSandi/**
87226a0801fSAndreas Gohr * Sends a notify mail on page change
87326a0801fSAndreas Gohr *
87426a0801fSAndreas Gohr * @param  string  $id       The changed page
87526a0801fSAndreas Gohr * @param  string  $who      Who to notify (admin|subscribers)
87626a0801fSAndreas Gohr * @param  int     $rev      Old page revision
87726a0801fSAndreas Gohr * @param  string  $summary  What changed
87890033e9dSAndreas Gohr * @param  boolean $minor    Is this a minor edit?
87902a498e7Schris * @param  array   $replace  Additional string substitutions, @KEY@ to be replaced by value
88015fae107Sandi *
88115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
882f3f0262cSandi */
88302a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){
884f3f0262cSandi  global $lang;
885f3f0262cSandi  global $conf;
88630d7d718SMike Frysinger  global $INFO;
887b158d625SSteven Danz
88826a0801fSAndreas Gohr  // decide if there is something to do
88926a0801fSAndreas Gohr  if($who == 'admin'){
89026a0801fSAndreas Gohr    if(empty($conf['notify'])) return; //notify enabled?
891f3f0262cSandi    $text = rawLocale('mailtext');
89226a0801fSAndreas Gohr    $to   = $conf['notify'];
89326a0801fSAndreas Gohr    $bcc  = '';
89426a0801fSAndreas Gohr  }elseif($who == 'subscribers'){
89526a0801fSAndreas Gohr    if(!$conf['subscribers']) return; //subscribers enabled?
89690033e9dSAndreas Gohr    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
89726a0801fSAndreas Gohr    $bcc  = subscriber_addresslist($id);
89826a0801fSAndreas Gohr    if(empty($bcc)) return;
89926a0801fSAndreas Gohr    $to   = '';
90026a0801fSAndreas Gohr    $text = rawLocale('subscribermail');
901a06e4bdbSSebastian Harl  }elseif($who == 'register'){
902a06e4bdbSSebastian Harl    if(empty($conf['registernotify'])) return;
903a06e4bdbSSebastian Harl    $text = rawLocale('registermail');
904a06e4bdbSSebastian Harl    $to   = $conf['registernotify'];
905a06e4bdbSSebastian Harl    $bcc  = '';
90626a0801fSAndreas Gohr  }else{
90726a0801fSAndreas Gohr    return; //just to be safe
90826a0801fSAndreas Gohr  }
90926a0801fSAndreas Gohr
910*63211f61SGlen Harris  $ip   = clientIP();
911f3f0262cSandi  $text = str_replace('@DATE@',date($conf['dformat']),$text);
912f3f0262cSandi  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
913*63211f61SGlen Harris  $text = str_replace('@IPADDRESS@',$ip,$text);
914*63211f61SGlen Harris  $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text);
915c9321d91SAndreas Gohr  $text = str_replace('@NEWPAGE@',wl($id,'',true,'&'),$text);
91626a0801fSAndreas Gohr  $text = str_replace('@PAGE@',$id,$text);
91726a0801fSAndreas Gohr  $text = str_replace('@TITLE@',$conf['title'],$text);
918ed7b5f09Sandi  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
919f3f0262cSandi  $text = str_replace('@SUMMARY@',$summary,$text);
9207a82afdcSandi  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
921f3f0262cSandi
92202a498e7Schris  foreach ($replace as $key => $substitution) {
92302a498e7Schris    $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
92402a498e7Schris  }
92502a498e7Schris
926a06e4bdbSSebastian Harl  if($who == 'register'){
927a06e4bdbSSebastian Harl    $subject = $lang['mail_new_user'].' '.$summary;
928a06e4bdbSSebastian Harl  }elseif($rev){
929f3f0262cSandi    $subject = $lang['mail_changed'].' '.$id;
930c9321d91SAndreas Gohr    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true,'&'),$text);
931ccdfa6c0SAndreas Gohr    require_once(DOKU_INC.'inc/DifferenceEngine.php');
932f3f0262cSandi    $df  = new Diff(split("\n",rawWiki($id,$rev)),
933f3f0262cSandi                    split("\n",rawWiki($id)));
934f3f0262cSandi    $dformat = new UnifiedDiffFormatter();
935f3f0262cSandi    $diff    = $dformat->format($df);
936f3f0262cSandi  }else{
937f3f0262cSandi    $subject=$lang['mail_newpage'].' '.$id;
938f3f0262cSandi    $text = str_replace('@OLDPAGE@','none',$text);
939f3f0262cSandi    $diff = rawWiki($id);
940f3f0262cSandi  }
941f3f0262cSandi  $text = str_replace('@DIFF@',$diff,$text);
942241f3a36Sandi  $subject = '['.$conf['title'].'] '.$subject;
943f3f0262cSandi
94430d7d718SMike Frysinger  $from = $conf['mailfrom'];
94530d7d718SMike Frysinger  $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from);
94630d7d718SMike Frysinger  $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from);
94730d7d718SMike Frysinger  $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from);
94830d7d718SMike Frysinger
94930d7d718SMike Frysinger  mail_send($to,$subject,$text,$from,'',$bcc);
950f3f0262cSandi}
951f3f0262cSandi
95215fae107Sandi/**
95371f7bde7SAndreas Gohr * extracts the query from a search engine referrer
95415fae107Sandi *
95515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org>
95671f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com>
957f3f0262cSandi */
958f3f0262cSandifunction getGoogleQuery(){
959f3f0262cSandi  $url = parse_url($_SERVER['HTTP_REFERER']);
9605c3f206fSandi  if(!$url) return '';
961f3f0262cSandi
962f3f0262cSandi  $query = array();
963f3f0262cSandi  parse_str($url['query'],$query);
96471f7bde7SAndreas Gohr  if(isset($query['q']))
96571f7bde7SAndreas Gohr    return $query['q'];        // google, live/msn, aol, ask, altavista, alltheweb, gigablast
96671f7bde7SAndreas Gohr  elseif(isset($query['p']))
96771f7bde7SAndreas Gohr    return $query['p'];        // yahoo
96871f7bde7SAndreas Gohr  elseif(isset($query['query']))
96971f7bde7SAndreas Gohr    return $query['query'];    // lycos, netscape, clusty, hotbot
97071f7bde7SAndreas Gohr  elseif(preg_match("#a9\.com#i",$url['host'])) // a9
97171f7bde7SAndreas Gohr    return urldecode(ltrim($url['path'],'/'));
972f3f0262cSandi
97371f7bde7SAndreas Gohr  return '';
974f3f0262cSandi}
975f3f0262cSandi
976f3f0262cSandi/**
97715fae107Sandi * Try to set correct locale
97815fae107Sandi *
979095bfd5cSandi * @deprecated No longer used
98015fae107Sandi * @author     Andreas Gohr <andi@splitbrain.org>
981f3f0262cSandi */
982f3f0262cSandifunction setCorrectLocale(){
983f3f0262cSandi  global $conf;
984f3f0262cSandi  global $lang;
985f3f0262cSandi
986f3f0262cSandi  $enc = strtoupper($lang['encoding']);
987f3f0262cSandi  foreach ($lang['locales'] as $loc){
988f3f0262cSandi    //try locale
989f3f0262cSandi    if(@setlocale(LC_ALL,$loc)) return;
990f3f0262cSandi    //try loceale with encoding
991f3f0262cSandi    if(@setlocale(LC_ALL,"$loc.$enc")) return;
992f3f0262cSandi  }
993f3f0262cSandi  //still here? try to set from environment
994f3f0262cSandi  @setlocale(LC_ALL,"");
995f3f0262cSandi}
996f3f0262cSandi
997f3f0262cSandi/**
998f3f0262cSandi * Return the human readable size of a file
999f3f0262cSandi *
1000f3f0262cSandi * @param       int    $size   A file size
1001f3f0262cSandi * @param       int    $dec    A number of decimal places
1002f3f0262cSandi * @author      Martin Benjamin <b.martin@cybernet.ch>
1003f3f0262cSandi * @author      Aidan Lister <aidan@php.net>
1004f3f0262cSandi * @version     1.0.0
1005f3f0262cSandi */
1006f31d5b73Sandifunction filesize_h($size, $dec = 1){
1007f3f0262cSandi  $sizes = array('B', 'KB', 'MB', 'GB');
1008f3f0262cSandi  $count = count($sizes);
1009f3f0262cSandi  $i = 0;
1010f3f0262cSandi
1011f3f0262cSandi  while ($size >= 1024 && ($i < $count - 1)) {
1012f3f0262cSandi    $size /= 1024;
1013f3f0262cSandi    $i++;
1014f3f0262cSandi  }
1015f3f0262cSandi
1016f3f0262cSandi  return round($size, $dec) . ' ' . $sizes[$i];
1017f3f0262cSandi}
1018f3f0262cSandi
101915fae107Sandi/**
102000a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting
102100a7b5adSEsther Brunner *
102200a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com>
102300a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk>
102400a7b5adSEsther Brunner */
102500a7b5adSEsther Brunnerfunction obfuscate($email) {
102600a7b5adSEsther Brunner  global $conf;
102700a7b5adSEsther Brunner
102800a7b5adSEsther Brunner  switch ($conf['mailguard']) {
102900a7b5adSEsther Brunner    case 'visible' :
103000a7b5adSEsther Brunner      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
103100a7b5adSEsther Brunner      return strtr($email, $obfuscate);
103200a7b5adSEsther Brunner
103300a7b5adSEsther Brunner    case 'hex' :
103400a7b5adSEsther Brunner      $encode = '';
103500a7b5adSEsther Brunner      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
103600a7b5adSEsther Brunner      return $encode;
103700a7b5adSEsther Brunner
103800a7b5adSEsther Brunner    case 'none' :
103900a7b5adSEsther Brunner    default :
104000a7b5adSEsther Brunner      return $email;
104100a7b5adSEsther Brunner  }
104200a7b5adSEsther Brunner}
104300a7b5adSEsther Brunner
104400a7b5adSEsther Brunner/**
1045b158d625SSteven Danz * Let us know if a user is tracking a page
1046b158d625SSteven Danz *
10471380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1048b158d625SSteven Danz */
10491380fc45SAndreas Gohrfunction is_subscribed($id,$uid){
10501380fc45SAndreas Gohr  $file=metaFN($id,'.mlist');
10511380fc45SAndreas Gohr  if (@file_exists($file)) {
1052b158d625SSteven Danz    $mlist = file($file);
10531380fc45SAndreas Gohr    $pos = array_search($uid."\n",$mlist);
10541380fc45SAndreas Gohr    return is_int($pos);
1055b158d625SSteven Danz  }
10561380fc45SAndreas Gohr
1057b158d625SSteven Danz  return false;
1058b158d625SSteven Danz}
1059340756e4Sandi
1060f9eb5648Ssteven-danz/**
1061f9eb5648Ssteven-danz * Return a string with the email addresses of all the
1062f9eb5648Ssteven-danz * users subscribed to a page
1063f9eb5648Ssteven-danz *
106426a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com>
1065f9eb5648Ssteven-danz */
1066f9eb5648Ssteven-danzfunction subscriber_addresslist($id){
1067f9eb5648Ssteven-danz  global $conf;
1068cd52f92dSchris  global $auth;
1069f9eb5648Ssteven-danz
1070f9eb5648Ssteven-danz  $emails = '';
1071f9eb5648Ssteven-danz
107226a0801fSAndreas Gohr  if (!$conf['subscribers']) return;
107326a0801fSAndreas Gohr
1074f9eb5648Ssteven-danz  $mlist = array();
1075f9eb5648Ssteven-danz  $file=metaFN($id,'.mlist');
1076d8186216SBen Coburn  if (@file_exists($file)) {
1077f9eb5648Ssteven-danz    $mlist = file($file);
1078f9eb5648Ssteven-danz  }
1079f9eb5648Ssteven-danz  if(count($mlist) > 0) {
1080f9eb5648Ssteven-danz    foreach ($mlist as $who) {
1081f9eb5648Ssteven-danz      $who = rtrim($who);
1082cd52f92dSchris      $info = $auth->getUserData($who);
1083c1791678SAndreas Gohr      if($info === false) continue;
1084f9eb5648Ssteven-danz      $level = auth_aclcheck($id,$who,$info['grps']);
1085f9eb5648Ssteven-danz      if ($level >= AUTH_READ) {
1086f9eb5648Ssteven-danz        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1087f9eb5648Ssteven-danz          if (empty($emails)) {
1088f9eb5648Ssteven-danz            $emails = $info['mail'];
1089f9eb5648Ssteven-danz          } else {
1090f9eb5648Ssteven-danz            $emails = "$emails,".$info['mail'];
1091f9eb5648Ssteven-danz          }
1092f9eb5648Ssteven-danz        }
1093f9eb5648Ssteven-danz      }
1094f9eb5648Ssteven-danz    }
1095f9eb5648Ssteven-danz  }
1096f9eb5648Ssteven-danz
1097f9eb5648Ssteven-danz  return $emails;
1098f9eb5648Ssteven-danz}
1099f9eb5648Ssteven-danz
110089541d4bSAndreas Gohr/**
110189541d4bSAndreas Gohr * Removes quoting backslashes
110289541d4bSAndreas Gohr *
110389541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
110489541d4bSAndreas Gohr */
110589541d4bSAndreas Gohrfunction unslash($string,$char="'"){
110689541d4bSAndreas Gohr  return str_replace('\\'.$char,$char,$string);
110789541d4bSAndreas Gohr}
110889541d4bSAndreas Gohr
1109340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
1110