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