xref: /dokuwiki/inc/common.php (revision 8ed45f3a4c95bf8c64968ec2afb15dcd1f7d17b3)
1<?php
2/**
3 * Common DokuWiki functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10  require_once(DOKU_CONF.'dokuwiki.php');
11  require_once(DOKU_INC.'inc/io.php');
12  require_once(DOKU_INC.'inc/utf8.php');
13  require_once(DOKU_INC.'inc/mail.php');
14  require_once(DOKU_INC.'inc/parserutils.php');
15
16/**
17 * These constants are used with the recents function
18 */
19define('RECENTS_SKIP_DELETED',2);
20define('RECENTS_SKIP_MINORS',4);
21define('RECENTS_SKIP_SUBSPACES',8);
22
23/**
24 * Return info about the current document as associative
25 * array.
26 *
27 * @author Andreas Gohr <andi@splitbrain.org>
28 */
29function pageinfo(){
30  global $ID;
31  global $REV;
32  global $USERINFO;
33  global $conf;
34
35  if($_SERVER['REMOTE_USER']){
36    $info['userinfo']   = $USERINFO;
37    $info['perm']       = auth_quickaclcheck($ID);
38    $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']);
39
40    // if some outside auth were used only REMOTE_USER is set
41    if(!$info['userinfo']['name']){
42      $info['userinfo']['name'] = $_SERVER['REMOTE_USER'];
43    }
44  }else{
45    $info['perm']       = auth_aclcheck($ID,'',null);
46    $info['subscribed'] = false;
47  }
48
49  $info['namespace'] = getNS($ID);
50  $info['locked']    = checklock($ID);
51  $info['filepath']  = realpath(wikiFN($ID,$REV));
52  $info['exists']    = @file_exists($info['filepath']);
53  if($REV && !$info['exists']){
54    //check if current revision was meant
55    $cur = wikiFN($ID);
56    if(@file_exists($cur) && (@filemtime($cur) == $REV)){
57      $info['filepath'] = realpath($cur);
58      $info['exists']   = true;
59      $REV = '';
60    }
61  }
62  $info['rev'] = $REV;
63  if($info['exists']){
64    $info['writable'] = (is_writable($info['filepath']) &&
65                         ($info['perm'] >= AUTH_EDIT));
66  }else{
67    $info['writable'] = ($info['perm'] >= AUTH_CREATE);
68  }
69  $info['editable']  = ($info['writable'] && empty($info['lock']));
70  $info['lastmod']   = @filemtime($info['filepath']);
71
72  //who's the editor
73  if($REV){
74    $revinfo = getRevisionInfo($ID,$REV);
75  }else{
76    $revinfo = getRevisionInfo($ID,$info['lastmod']);
77  }
78  $info['ip']     = $revinfo['ip'];
79  $info['user']   = $revinfo['user'];
80  $info['sum']    = $revinfo['sum'];
81  $info['minor']  = $revinfo['minor'];
82
83  if($revinfo['user']){
84    $info['editor'] = $revinfo['user'];
85  }else{
86    $info['editor'] = $revinfo['ip'];
87  }
88
89  return $info;
90}
91
92/**
93 * Build an string of URL parameters
94 *
95 * @author Andreas Gohr
96 */
97function buildURLparams($params, $sep='&amp;'){
98  $url = '';
99  $amp = false;
100  foreach($params as $key => $val){
101    if($amp) $url .= $sep;
102
103    $url .= $key.'=';
104    $url .= rawurlencode($val);
105    $amp = true;
106  }
107  return $url;
108}
109
110/**
111 * Build an string of html tag attributes
112 *
113 * @author Andreas Gohr
114 */
115function buildAttributes($params){
116  $url = '';
117  foreach($params as $key => $val){
118    $url .= $key.'="';
119    $url .= htmlspecialchars ($val);
120    $url .= '" ';
121  }
122  return $url;
123}
124
125
126/**
127 * print a message
128 *
129 * If HTTP headers were not sent yet the message is added
130 * to the global message array else it's printed directly
131 * using html_msgarea()
132 *
133 *
134 * Levels can be:
135 *
136 * -1 error
137 *  0 info
138 *  1 success
139 *
140 * @author Andreas Gohr <andi@splitbrain.org>
141 * @see    html_msgarea
142 */
143function msg($message,$lvl=0){
144  global $MSG;
145  $errors[-1] = 'error';
146  $errors[0]  = 'info';
147  $errors[1]  = 'success';
148
149  if(!headers_sent()){
150    if(!isset($MSG)) $MSG = array();
151    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
152  }else{
153    $MSG = array();
154    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
155    if(function_exists('html_msgarea')){
156      html_msgarea();
157    }else{
158      print "ERROR($lvl) $message";
159    }
160  }
161}
162
163/**
164 * This builds the breadcrumb trail and returns it as array
165 *
166 * @author Andreas Gohr <andi@splitbrain.org>
167 */
168function breadcrumbs(){
169  // we prepare the breadcrumbs early for quick session closing
170  static $crumbs = null;
171  if($crumbs != null) return $crumbs;
172
173  global $ID;
174  global $ACT;
175  global $conf;
176  $crumbs = $_SESSION[$conf['title']]['bc'];
177
178  //first visit?
179  if (!is_array($crumbs)){
180    $crumbs = array();
181  }
182  //we only save on show and existing wiki documents
183  $file = wikiFN($ID);
184  if($ACT != 'show' || !@file_exists($file)){
185    $_SESSION[$conf['title']]['bc'] = $crumbs;
186    return $crumbs;
187  }
188
189  // page names
190  $name = noNS($ID);
191  if ($conf['useheading']) {
192    // get page title
193    $title = p_get_first_heading($ID);
194    if ($title) {
195      $name = $title;
196    }
197  }
198
199  //remove ID from array
200  if (isset($crumbs[$ID])) {
201    unset($crumbs[$ID]);
202  }
203
204  //add to array
205  $crumbs[$ID] = $name;
206  //reduce size
207  while(count($crumbs) > $conf['breadcrumbs']){
208    array_shift($crumbs);
209  }
210  //save to session
211  $_SESSION[$conf['title']]['bc'] = $crumbs;
212  return $crumbs;
213}
214
215/**
216 * Filter for page IDs
217 *
218 * This is run on a ID before it is outputted somewhere
219 * currently used to replace the colon with something else
220 * on Windows systems and to have proper URL encoding
221 *
222 * Urlencoding is ommitted when the second parameter is false
223 *
224 * @author Andreas Gohr <andi@splitbrain.org>
225 */
226function idfilter($id,$ue=true){
227  global $conf;
228  if ($conf['useslash'] && $conf['userewrite']){
229    $id = strtr($id,':','/');
230  }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' &&
231      $conf['userewrite']) {
232    $id = strtr($id,':',';');
233  }
234  if($ue){
235    $id = rawurlencode($id);
236    $id = str_replace('%3A',':',$id); //keep as colon
237    $id = str_replace('%2F','/',$id); //keep as slash
238  }
239  return $id;
240}
241
242/**
243 * This builds a link to a wikipage
244 *
245 * It handles URL rewriting and adds additional parameter if
246 * given in $more
247 *
248 * @author Andreas Gohr <andi@splitbrain.org>
249 */
250function wl($id='',$more='',$abs=false,$sep='&amp;'){
251  global $conf;
252  if(is_array($more)){
253    $more = buildURLparams($more,$sep);
254  }else{
255    $more = str_replace(',',$sep,$more);
256  }
257
258  $id    = idfilter($id);
259  if($abs){
260    $xlink = DOKU_URL;
261  }else{
262    $xlink = DOKU_BASE;
263  }
264
265  if($conf['userewrite'] == 2){
266    $xlink .= DOKU_SCRIPT.'/'.$id;
267    if($more) $xlink .= '?'.$more;
268  }elseif($conf['userewrite']){
269    $xlink .= $id;
270    if($more) $xlink .= '?'.$more;
271  }else{
272    $xlink .= DOKU_SCRIPT.'?id='.$id;
273    if($more) $xlink .= $sep.$more;
274  }
275
276  return $xlink;
277}
278
279/**
280 * Build a link to a media file
281 *
282 * Will return a link to the detail page if $direct is false
283 */
284function ml($id='',$more='',$direct=true,$sep='&amp;'){
285  global $conf;
286  if(is_array($more)){
287    $more = buildURLparams($more,$sep);
288  }else{
289    $more = str_replace(',',$sep,$more);
290  }
291
292  $xlink = DOKU_BASE;
293
294  // external URLs are always direct without rewriting
295  if(preg_match('#^(https?|ftp)://#i',$id)){
296    $xlink .= 'lib/exe/fetch.php';
297    if($more){
298      $xlink .= '?'.$more;
299      $xlink .= $sep.'media='.rawurlencode($id);
300    }else{
301      $xlink .= '?media='.rawurlencode($id);
302    }
303    return $xlink;
304  }
305
306  $id = idfilter($id);
307
308  // decide on scriptname
309  if($direct){
310    if($conf['userewrite'] == 1){
311      $script = '_media';
312    }else{
313      $script = 'lib/exe/fetch.php';
314    }
315  }else{
316    if($conf['userewrite'] == 1){
317      $script = '_detail';
318    }else{
319      $script = 'lib/exe/detail.php';
320    }
321  }
322
323  // build URL based on rewrite mode
324   if($conf['userewrite']){
325     $xlink .= $script.'/'.$id;
326     if($more) $xlink .= '?'.$more;
327   }else{
328     if($more){
329       $xlink .= $script.'?'.$more;
330       $xlink .= $sep.'media='.$id;
331     }else{
332       $xlink .= $script.'?media='.$id;
333     }
334   }
335
336  return $xlink;
337}
338
339
340
341/**
342 * Just builds a link to a script
343 *
344 * @todo   maybe obsolete
345 * @author Andreas Gohr <andi@splitbrain.org>
346 */
347function script($script='doku.php'){
348#  $link = getBaseURL();
349#  $link .= $script;
350#  return $link;
351  return DOKU_BASE.DOKU_SCRIPT;
352}
353
354/**
355 * Spamcheck against wordlist
356 *
357 * Checks the wikitext against a list of blocked expressions
358 * returns true if the text contains any bad words
359 *
360 * @author Andreas Gohr <andi@splitbrain.org>
361 */
362function checkwordblock(){
363  global $TEXT;
364  global $conf;
365
366  if(!$conf['usewordblock']) return false;
367
368  $wordblocks = getWordblocks();
369  //how many lines to read at once (to work around some PCRE limits)
370  if(version_compare(phpversion(),'4.3.0','<')){
371    //old versions of PCRE define a maximum of parenthesises even if no
372    //backreferences are used - the maximum is 99
373    //this is very bad performancewise and may even be too high still
374    $chunksize = 40;
375  }else{
376    //read file in chunks of 600 - this should work around the
377    //MAX_PATTERN_SIZE in modern PCRE
378    $chunksize = 400;
379  }
380  while($blocks = array_splice($wordblocks,0,$chunksize)){
381    $re = array();
382    #build regexp from blocks
383    foreach($blocks as $block){
384      $block = preg_replace('/#.*$/','',$block);
385      $block = trim($block);
386      if(empty($block)) continue;
387      $re[]  = $block;
388    }
389    if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) {
390      return true;
391    }
392  }
393  return false;
394}
395
396/**
397 * Return the IP of the client
398 *
399 * Honours X-Forwarded-For Proxy Headers
400 *
401 * @author Andreas Gohr <andi@splitbrain.org>
402 */
403function clientIP(){
404  $my = $_SERVER['REMOTE_ADDR'];
405  if($_SERVER['HTTP_X_FORWARDED_FOR']){
406    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
407  }
408  return $my;
409}
410
411/**
412 * Checks if a given page is currently locked.
413 *
414 * removes stale lockfiles
415 *
416 * @author Andreas Gohr <andi@splitbrain.org>
417 */
418function checklock($id){
419  global $conf;
420  $lock = wikiFN($id).'.lock';
421
422  //no lockfile
423  if(!@file_exists($lock)) return false;
424
425  //lockfile expired
426  if((time() - filemtime($lock)) > $conf['locktime']){
427    unlink($lock);
428    return false;
429  }
430
431  //my own lock
432  $ip = io_readFile($lock);
433  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
434    return false;
435  }
436
437  return $ip;
438}
439
440/**
441 * Lock a page for editing
442 *
443 * @author Andreas Gohr <andi@splitbrain.org>
444 */
445function lock($id){
446  $lock = wikiFN($id).'.lock';
447  if($_SERVER['REMOTE_USER']){
448    io_saveFile($lock,$_SERVER['REMOTE_USER']);
449  }else{
450    io_saveFile($lock,clientIP());
451  }
452}
453
454/**
455 * Unlock a page if it was locked by the user
456 *
457 * @author Andreas Gohr <andi@splitbrain.org>
458 * @return bool true if a lock was removed
459 */
460function unlock($id){
461  $lock = wikiFN($id).'.lock';
462  if(@file_exists($lock)){
463    $ip = io_readFile($lock);
464    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
465      @unlink($lock);
466      return true;
467    }
468  }
469  return false;
470}
471
472/**
473 * convert line ending to unix format
474 *
475 * @see    formText() for 2crlf conversion
476 * @author Andreas Gohr <andi@splitbrain.org>
477 */
478function cleanText($text){
479  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
480  return $text;
481}
482
483/**
484 * Prepares text for print in Webforms by encoding special chars.
485 * It also converts line endings to Windows format which is
486 * pseudo standard for webforms.
487 *
488 * @see    cleanText() for 2unix conversion
489 * @author Andreas Gohr <andi@splitbrain.org>
490 */
491function formText($text){
492  $text = preg_replace("/\012/","\015\012",$text);
493  return htmlspecialchars($text);
494}
495
496/**
497 * Returns the specified local text in raw format
498 *
499 * @author Andreas Gohr <andi@splitbrain.org>
500 */
501function rawLocale($id){
502  return io_readFile(localeFN($id));
503}
504
505/**
506 * Returns the raw WikiText
507 *
508 * @author Andreas Gohr <andi@splitbrain.org>
509 */
510function rawWiki($id,$rev=''){
511  return io_readFile(wikiFN($id,$rev));
512}
513
514/**
515 * Returns the pagetemplate contents for the ID's namespace
516 *
517 * @author Andreas Gohr <andi@splitbrain.org>
518 */
519function pageTemplate($id){
520  global $conf;
521  global $INFO;
522  $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt');
523  $tpl = str_replace('@ID@',$id,$tpl);
524  $tpl = str_replace('@NS@',getNS($id),$tpl);
525  $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl);
526  $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl);
527  $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl);
528  $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl);
529  $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl);
530  return $tpl;
531}
532
533
534/**
535 * Returns the raw Wiki Text in three slices.
536 *
537 * The range parameter needs to have the form "from-to"
538 * and gives the range of the section in bytes - no
539 * UTF-8 awareness is needed.
540 * The returned order is prefix, section and suffix.
541 *
542 * @author Andreas Gohr <andi@splitbrain.org>
543 */
544function rawWikiSlices($range,$id,$rev=''){
545  list($from,$to) = split('-',$range,2);
546  $text = io_readFile(wikiFN($id,$rev));
547  if(!$from) $from = 0;
548  if(!$to)   $to   = strlen($text)+1;
549
550  $slices[0] = substr($text,0,$from-1);
551  $slices[1] = substr($text,$from-1,$to-$from);
552  $slices[2] = substr($text,$to);
553
554  return $slices;
555}
556
557/**
558 * Joins wiki text slices
559 *
560 * function to join the text slices with correct lineendings again.
561 * When the pretty parameter is set to true it adds additional empty
562 * lines between sections if needed (used on saving).
563 *
564 * @author Andreas Gohr <andi@splitbrain.org>
565 */
566function con($pre,$text,$suf,$pretty=false){
567
568  if($pretty){
569    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
570    if($suf && substr($text,-1) != "\n") $text .= "\n";
571  }
572
573  if($pre) $pre .= "\n";
574  if($suf) $text .= "\n";
575  return $pre.$text.$suf;
576}
577
578/**
579 * print debug messages
580 *
581 * little function to print the content of a var
582 *
583 * @author Andreas Gohr <andi@splitbrain.org>
584 */
585function dbg($msg,$hidden=false){
586  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
587  print_r($msg);
588  (!$hidden) ? print '</pre>' : print "\n-->";
589}
590
591/**
592 * Add's an entry to the changelog
593 *
594 * @author Andreas Gohr <andi@splitbrain.org>
595 */
596function addLogEntry($date,$id,$summary='',$minor=false){
597  global $conf;
598
599  if(!@is_writable($conf['changelog'])){
600    msg($conf['changelog'].' is not writable!',-1);
601    return;
602  }
603
604  if(!$date) $date = time(); //use current time if none supplied
605  $remote = $_SERVER['REMOTE_ADDR'];
606  $user   = $_SERVER['REMOTE_USER'];
607
608  if($conf['useacl'] && $user && $minor){
609    $summary = '*'.$summary;
610  }else{
611    $summary = ' '.$summary;
612  }
613
614  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
615  io_saveFile($conf['changelog'],$logline,true);
616}
617
618/**
619 * Checks an summary entry if it was a minor edit
620 *
621 * The summary is cleaned of the marker char
622 *
623 * @author Andreas Gohr <andi@splitbrain.org>
624 */
625function isMinor(&$summary){
626  if(substr($summary,0,1) == '*'){
627    $summary = substr($summary,1);
628    return true;
629  }
630  $summary = trim($summary);
631  return false;
632}
633
634/**
635 * Internal function used by getRecents
636 *
637 * don't call directly
638 *
639 * @see getRecents()
640 * @author Andreas Gohr <andi@splitbrain.org>
641 */
642function _handleRecent($line,$ns,$flags){
643  static $seen  = array();         //caches seen pages and skip them
644  if(empty($line)) return false;   //skip empty lines
645
646  // split the line into parts
647  list($dt,$ip,$id,$usr,$sum) = explode("\t",$line);
648
649  // skip seen ones
650  if($seen[$id]) return false;
651  $recent = array();
652
653  // check minors
654  if(isMinor($sum)){
655    // skip minors
656    if($flags & RECENTS_SKIP_MINORS) return false;
657    $recent['minor'] = true;
658  }else{
659    $recent['minor'] = false;
660  }
661
662  // remember in seen to skip additional sights
663  $seen[$id] = 1;
664
665  // check if it's a hidden page
666  if(isHiddenPage($id)) return false;
667
668  // filter namespace
669  if (($ns) && (strpos($id,$ns.':') !== 0)) return false;
670
671  // exclude subnamespaces
672  if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($id) != $ns)) return false;
673
674  // check ACL
675  if (auth_quickaclcheck($id) < AUTH_READ) return false;
676
677  // check existance
678  if(!@file_exists(wikiFN($id))){
679    if($flags & RECENTS_SKIP_DELETED){
680      return false;
681    }else{
682      $recent['del'] = true;
683    }
684  }else{
685    $recent['del'] = false;
686  }
687
688  $recent['id']   = $id;
689  $recent['date'] = $dt;
690  $recent['ip']   = $ip;
691  $recent['user'] = $usr;
692  $recent['sum']  = $sum;
693
694  return $recent;
695}
696
697
698/**
699 * returns an array of recently changed files using the
700 * changelog
701 *
702 * The following constants can be used to control which changes are
703 * included. Add them together as needed.
704 *
705 * RECENTS_SKIP_DELETED   - don't include deleted pages
706 * RECENTS_SKIP_MINORS    - don't include minor changes
707 * RECENTS_SKIP_SUBSPACES - don't include subspaces
708 *
709 * @param int    $first   number of first entry returned (for paginating
710 * @param int    $num     return $num entries
711 * @param string $ns      restrict to given namespace
712 * @param bool   $flags   see above
713 *
714 * @author Andreas Gohr <andi@splitbrain.org>
715 */
716function getRecents($first,$num,$ns='',$flags=0){
717  global $conf;
718  $recent = array();
719  $count  = 0;
720
721  if(!$num)
722    return $recent;
723
724  if(!@is_readable($conf['changelog'])){
725    msg($conf['changelog'].' is not readable',-1);
726    return $recent;
727  }
728
729  $fh  = fopen($conf['changelog'],'r');
730  $buf = '';
731  $csz = 4096;                              //chunksize
732  fseek($fh,0,SEEK_END);                    // jump to the end
733  $pos = ftell($fh);                        // position pointer
734
735  // now read backwards into buffer
736  while($pos > 0){
737    $pos -= $csz;                           // seek to previous chunk...
738    if($pos < 0) {                          // ...or rest of file
739      $csz += $pos;
740      $pos = 0;
741    }
742
743    fseek($fh,$pos);
744
745    $buf = fread($fh,$csz).$buf;            // prepend to buffer
746
747    $lines = explode("\n",$buf);            // split buffer into lines
748
749    if($pos > 0){
750      $buf = array_shift($lines);           // first one may be still incomplete
751    }
752
753    $cnt = count($lines);
754    if(!$cnt) continue;                     // no lines yet
755
756    // handle lines
757    for($i = $cnt-1; $i >= 0; $i--){
758      $rec = _handleRecent($lines[$i],$ns,$flags);
759      if($rec !== false){
760        if(--$first >= 0) continue;         // skip first entries
761        $recent[] = $rec;
762        $count++;
763
764        // break while when we have enough entries
765        if($count >= $num){
766          $pos = 0; // will break the while loop
767          break;    // will break the for loop
768        }
769      }
770    }
771  }// end of while
772
773  fclose($fh);
774  return $recent;
775}
776
777/**
778 * gets additonal informations for a certain pagerevison
779 * from the changelog
780 *
781 * @author Andreas Gohr <andi@splitbrain.org>
782 */
783function getRevisionInfo($id,$rev){
784  global $conf;
785
786  if(!$rev) return(null);
787
788  $info = array();
789  if(!@is_readable($conf['changelog'])){
790    msg($conf['changelog'].' is not readable',-1);
791    return $recent;
792  }
793  $loglines = file($conf['changelog']);
794  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
795  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
796  $line = split("\t",$loglines[0]);
797  $info['date']  = $line[0];
798  $info['ip']    = $line[1];
799  $info['user']  = $line[3];
800  $info['sum']   = $line[4];
801  $info['minor'] = isMinor($info['sum']);
802  return $info;
803}
804
805/**
806 * Saves a wikitext by calling io_saveFile
807 *
808 * @author Andreas Gohr <andi@splitbrain.org>
809 */
810function saveWikiText($id,$text,$summary,$minor=false){
811  global $conf;
812  global $lang;
813  // ignore if no changes were made
814  if($text == rawWiki($id,'')){
815    return;
816  }
817
818  $file = wikiFN($id);
819  $old  = saveOldRevision($id);
820
821  if (empty($text)){
822    // remove empty file
823    @unlink($file);
824    // remove any meta info
825    $mfiles = metaFiles($id);
826    foreach ($mfiles as $mfile) {
827      if (file_exists($mfile)) @unlink($mfile);
828    }
829    $del = true;
830    //autoset summary on deletion
831    if(empty($summary)) $summary = $lang['deleted'];
832    //remove empty namespaces
833    io_sweepNS($id);
834  }else{
835    // save file (datadir is created in io_saveFile)
836    io_saveFile($file,$text);
837    $del = false;
838  }
839
840  addLogEntry(@filemtime($file),$id,$summary,$minor);
841  // send notify mails
842  notify($id,'admin',$old,$summary,$minor);
843  notify($id,'subscribers',$old,$summary,$minor);
844
845  //purge cache on add by updating the purgefile
846  if($conf['purgeonadd'] && (!$old || $del)){
847    io_saveFile($conf['cachedir'].'/purgefile',time());
848  }
849}
850
851/**
852 * moves the current version to the attic and returns its
853 * revision date
854 *
855 * @author Andreas Gohr <andi@splitbrain.org>
856 */
857function saveOldRevision($id){
858  global $conf;
859  $oldf = wikiFN($id);
860  if(!@file_exists($oldf)) return '';
861  $date = filemtime($oldf);
862  $newf = wikiFN($id,$date);
863  if(substr($newf,-3)=='.gz'){
864    io_saveFile($newf,rawWiki($id));
865  }else{
866    io_makeFileDir($newf);
867    copy($oldf, $newf);
868  }
869  return $date;
870}
871
872/**
873 * Sends a notify mail on page change
874 *
875 * @param  string  $id       The changed page
876 * @param  string  $who      Who to notify (admin|subscribers)
877 * @param  int     $rev      Old page revision
878 * @param  string  $summary  What changed
879 * @param  boolean $minor    Is this a minor edit?
880 *
881 * @author Andreas Gohr <andi@splitbrain.org>
882 */
883function notify($id,$who,$rev='',$summary='',$minor=false){
884  global $lang;
885  global $conf;
886
887  // decide if there is something to do
888  if($who == 'admin'){
889    if(empty($conf['notify'])) return; //notify enabled?
890    $text = rawLocale('mailtext');
891    $to   = $conf['notify'];
892    $bcc  = '';
893  }elseif($who == 'subscribers'){
894    if(!$conf['subscribers']) return; //subscribers enabled?
895    if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors
896    $bcc  = subscriber_addresslist($id);
897    if(empty($bcc)) return;
898    $to   = '';
899    $text = rawLocale('subscribermail');
900  }else{
901    return; //just to be safe
902  }
903
904  $text = str_replace('@DATE@',date($conf['dformat']),$text);
905  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
906  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
907  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
908  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
909  $text = str_replace('@PAGE@',$id,$text);
910  $text = str_replace('@TITLE@',$conf['title'],$text);
911  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
912  $text = str_replace('@SUMMARY@',$summary,$text);
913  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
914
915  if($rev){
916    $subject = $lang['mail_changed'].' '.$id;
917    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
918    require_once(DOKU_INC.'inc/DifferenceEngine.php');
919    $df  = new Diff(split("\n",rawWiki($id,$rev)),
920                    split("\n",rawWiki($id)));
921    $dformat = new UnifiedDiffFormatter();
922    $diff    = $dformat->format($df);
923  }else{
924    $subject=$lang['mail_newpage'].' '.$id;
925    $text = str_replace('@OLDPAGE@','none',$text);
926    $diff = rawWiki($id);
927  }
928  $text = str_replace('@DIFF@',$diff,$text);
929  $subject = '['.$conf['title'].'] '.$subject;
930
931  mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc);
932}
933
934/**
935 * Return a list of available page revisons
936 *
937 * @author Andreas Gohr <andi@splitbrain.org>
938 */
939function getRevisions($id){
940  $revd = dirname(wikiFN($id,'foo'));
941  $revs = array();
942  $clid = cleanID($id);
943  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
944  $clid = utf8_encodeFN($clid);
945
946  if (is_dir($revd) && $dh = opendir($revd)) {
947    while (($file = readdir($dh)) !== false) {
948      if (is_dir($revd.'/'.$file)) continue;
949      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
950        $revs[]=$match[1];
951      }
952    }
953    closedir($dh);
954  }
955  rsort($revs);
956  return $revs;
957}
958
959/**
960 * extracts the query from a google referer
961 *
962 * @todo   should be more generic and support yahoo et al
963 * @author Andreas Gohr <andi@splitbrain.org>
964 */
965function getGoogleQuery(){
966  $url = parse_url($_SERVER['HTTP_REFERER']);
967  if(!$url) return '';
968
969  if(!preg_match("#google\.#i",$url['host'])) return '';
970  $query = array();
971  parse_str($url['query'],$query);
972
973  return $query['q'];
974}
975
976/**
977 * Try to set correct locale
978 *
979 * @deprecated No longer used
980 * @author     Andreas Gohr <andi@splitbrain.org>
981 */
982function setCorrectLocale(){
983  global $conf;
984  global $lang;
985
986  $enc = strtoupper($lang['encoding']);
987  foreach ($lang['locales'] as $loc){
988    //try locale
989    if(@setlocale(LC_ALL,$loc)) return;
990    //try loceale with encoding
991    if(@setlocale(LC_ALL,"$loc.$enc")) return;
992  }
993  //still here? try to set from environment
994  @setlocale(LC_ALL,"");
995}
996
997/**
998 * Return the human readable size of a file
999 *
1000 * @param       int    $size   A file size
1001 * @param       int    $dec    A number of decimal places
1002 * @author      Martin Benjamin <b.martin@cybernet.ch>
1003 * @author      Aidan Lister <aidan@php.net>
1004 * @version     1.0.0
1005 */
1006function filesize_h($size, $dec = 1){
1007  $sizes = array('B', 'KB', 'MB', 'GB');
1008  $count = count($sizes);
1009  $i = 0;
1010
1011  while ($size >= 1024 && ($i < $count - 1)) {
1012    $size /= 1024;
1013    $i++;
1014  }
1015
1016  return round($size, $dec) . ' ' . $sizes[$i];
1017}
1018
1019/**
1020 * return an obfuscated email address in line with $conf['mailguard'] setting
1021 *
1022 * @author Harry Fuecks <hfuecks@gmail.com>
1023 * @author Christopher Smith <chris@jalakai.co.uk>
1024 */
1025function obfuscate($email) {
1026  global $conf;
1027
1028  switch ($conf['mailguard']) {
1029    case 'visible' :
1030      $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ');
1031      return strtr($email, $obfuscate);
1032
1033    case 'hex' :
1034      $encode = '';
1035      for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';';
1036      return $encode;
1037
1038    case 'none' :
1039    default :
1040      return $email;
1041  }
1042}
1043
1044/**
1045 * Return DokuWikis version
1046 *
1047 * @author Andreas Gohr <andi@splitbrain.org>
1048 */
1049function getVersion(){
1050  //import version string
1051  if(@file_exists('VERSION')){
1052    //official release
1053    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
1054  }elseif(is_dir('_darcs')){
1055    //darcs checkout
1056    $inv = file('_darcs/inventory');
1057    $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv);
1058    $cur = array_pop($inv);
1059    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
1060    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
1061  }else{
1062    return 'snapshot?';
1063  }
1064}
1065
1066/**
1067 * Run a few sanity checks
1068 *
1069 * @author Andreas Gohr <andi@splitbrain.org>
1070 */
1071function check(){
1072  global $conf;
1073  global $INFO;
1074
1075  msg('DokuWiki version: '.getVersion(),1);
1076
1077  if(version_compare(phpversion(),'4.3.0','<')){
1078    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
1079  }elseif(version_compare(phpversion(),'4.3.10','<')){
1080    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
1081  }else{
1082    msg('PHP version '.phpversion(),1);
1083  }
1084
1085  if(is_writable($conf['changelog'])){
1086    msg('Changelog is writable',1);
1087  }else{
1088    msg('Changelog is not writable',-1);
1089  }
1090
1091  if(is_writable($conf['datadir'])){
1092    msg('Datadir is writable',1);
1093  }else{
1094    msg('Datadir is not writable',-1);
1095  }
1096
1097  if(is_writable($conf['olddir'])){
1098    msg('Attic is writable',1);
1099  }else{
1100    msg('Attic is not writable',-1);
1101  }
1102
1103  if(is_writable($conf['mediadir'])){
1104    msg('Mediadir is writable',1);
1105  }else{
1106    msg('Mediadir is not writable',-1);
1107  }
1108
1109  if(is_writable($conf['cachedir'])){
1110    msg('Cachedir is writable',1);
1111  }else{
1112    msg('Cachedir is not writable',-1);
1113  }
1114
1115  if(is_writable(DOKU_CONF.'users.auth.php')){
1116    msg('conf/users.auth.php is writable',1);
1117  }else{
1118    msg('conf/users.auth.php is not writable',0);
1119  }
1120
1121  if(function_exists('mb_strpos')){
1122    if(defined('UTF8_NOMBSTRING')){
1123      msg('mb_string extension is available but will not be used',0);
1124    }else{
1125      msg('mb_string extension is available and will be used',1);
1126    }
1127  }else{
1128    msg('mb_string extension not available - PHP only replacements will be used',0);
1129  }
1130
1131  if($conf['allowdebug']){
1132    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
1133  }else{
1134    msg('Debugging support is disabled',1);
1135  }
1136
1137  msg('Your current permission for this page is '.$INFO['perm'],0);
1138
1139  if(is_writable($INFO['filepath'])){
1140    msg('The current page is writable by the webserver',0);
1141  }else{
1142    msg('The current page is not writable by the webserver',0);
1143  }
1144
1145  if($INFO['writable']){
1146    msg('The current page is writable by you',0);
1147  }else{
1148    msg('The current page is not writable you',0);
1149  }
1150}
1151
1152/**
1153 * Let us know if a user is tracking a page
1154 *
1155 * @author Andreas Gohr <andi@splitbrain.org>
1156 */
1157function is_subscribed($id,$uid){
1158  $file=metaFN($id,'.mlist');
1159  if (@file_exists($file)) {
1160    $mlist = file($file);
1161    $pos = array_search($uid."\n",$mlist);
1162    return is_int($pos);
1163  }
1164
1165  return false;
1166}
1167
1168/**
1169 * Return a string with the email addresses of all the
1170 * users subscribed to a page
1171 *
1172 * @author Steven Danz <steven-danz@kc.rr.com>
1173 */
1174function subscriber_addresslist($id){
1175  global $conf;
1176  global $auth;
1177
1178  $emails = '';
1179
1180  if (!$conf['subscribers']) return;
1181
1182  $mlist = array();
1183  $file=metaFN($id,'.mlist');
1184  if (file_exists($file)) {
1185    $mlist = file($file);
1186  }
1187  if(count($mlist) > 0) {
1188    foreach ($mlist as $who) {
1189      $who = rtrim($who);
1190      $info = $auth->getUserData($who);
1191      $level = auth_aclcheck($id,$who,$info['grps']);
1192      if ($level >= AUTH_READ) {
1193        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
1194          if (empty($emails)) {
1195            $emails = $info['mail'];
1196          } else {
1197            $emails = "$emails,".$info['mail'];
1198          }
1199        }
1200      }
1201    }
1202  }
1203
1204  return $emails;
1205}
1206
1207/**
1208 * Removes quoting backslashes
1209 *
1210 * @author Andreas Gohr <andi@splitbrain.org>
1211 */
1212function unslash($string,$char="'"){
1213  return str_replace('\\'.$char,$char,$string);
1214}
1215
1216//Setup VIM: ex: et ts=2 enc=utf-8 :
1217