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