xref: /dokuwiki/inc/common.php (revision b0702bc0721b4ab46dd01cad67f23fa0766c97e9)
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  //read file in chunks of 600 - this should work around the
334  //MAX_PATTERN_SIZE in PCRE
335  while($blocks = array_splice($blockfile,0,600)){
336    $re = array();
337    #build regexp from blocks
338    foreach($blocks as $block){
339      $block = preg_replace('/#.*$/','',$block);
340      $block = trim($block);
341      if(empty($block)) continue;
342      $re[]  = $block;
343    }
344    if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true;
345  }
346  return false;
347}
348
349/**
350 * Return the IP of the client
351 *
352 * Honours X-Forwarded-For Proxy Headers
353 *
354 * @author Andreas Gohr <andi@splitbrain.org>
355 */
356function clientIP(){
357  $my = $_SERVER['REMOTE_ADDR'];
358  if($_SERVER['HTTP_X_FORWARDED_FOR']){
359    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
360  }
361  return $my;
362}
363
364/**
365 * Checks if a given page is currently locked.
366 *
367 * removes stale lockfiles
368 *
369 * @author Andreas Gohr <andi@splitbrain.org>
370 */
371function checklock($id){
372  global $conf;
373  $lock = wikiFN($id).'.lock';
374
375  //no lockfile
376  if(!@file_exists($lock)) return false;
377
378  //lockfile expired
379  if((time() - filemtime($lock)) > $conf['locktime']){
380    unlink($lock);
381    return false;
382  }
383
384  //my own lock
385  $ip = io_readFile($lock);
386  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
387    return false;
388  }
389
390  return $ip;
391}
392
393/**
394 * Lock a page for editing
395 *
396 * @author Andreas Gohr <andi@splitbrain.org>
397 */
398function lock($id){
399  $lock = wikiFN($id).'.lock';
400  if($_SERVER['REMOTE_USER']){
401    io_saveFile($lock,$_SERVER['REMOTE_USER']);
402  }else{
403    io_saveFile($lock,clientIP());
404  }
405}
406
407/**
408 * Unlock a page if it was locked by the user
409 *
410 * @author Andreas Gohr <andi@splitbrain.org>
411 * @return bool true if a lock was removed
412 */
413function unlock($id){
414  $lock = wikiFN($id).'.lock';
415  if(@file_exists($lock)){
416    $ip = io_readFile($lock);
417    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
418      @unlink($lock);
419      return true;
420    }
421  }
422  return false;
423}
424
425/**
426 * Remove unwanted chars from ID
427 *
428 * Cleans a given ID to only use allowed characters. Accented characters are
429 * converted to unaccented ones
430 *
431 * @author Andreas Gohr <andi@splitbrain.org>
432 */
433function cleanID($id){
434  global $conf;
435  global $lang;
436  $id = trim($id);
437  $id = utf8_strtolower($id);
438
439  //alternative namespace seperator
440  $id = strtr($id,';',':');
441  if($conf['useslash']) $id = strtr($id,'/',':');
442
443  if($conf['deaccent']) $id = utf8_deaccent($id,-1);
444
445  //remove specials (only ascii specials are removed)
446  $id = preg_replace('#[\x00-\x20 !"§$%&()\[\]{}\\?`\'\#~*+=,<>\|^°@µ¹²³¼½¬]#u','_',$id);
447
448  //clean up
449  $id = preg_replace('#__#','_',$id);
450  $id = preg_replace('#:+#',':',$id);
451  $id = trim($id,':._-');
452  $id = preg_replace('#:[:\._\-]+#',':',$id);
453
454  return($id);
455}
456
457/**
458 * returns the full path to the datafile specified by ID and
459 * optional revision
460 *
461 * The filename is URL encoded to protect Unicode chars
462 *
463 * @author Andreas Gohr <andi@splitbrain.org>
464 */
465function wikiFN($id,$rev=''){
466  global $conf;
467  $id = cleanID($id);
468  $id = str_replace(':','/',$id);
469  if(empty($rev)){
470    $fn = $conf['datadir'].'/'.$id.'.txt';
471  }else{
472    $fn = $conf['olddir'].'/'.$id.'.'.$rev.'.txt';
473    if($conf['usegzip'] && !@file_exists($fn)){
474      //return gzip if enabled and plaintext doesn't exist
475      $fn .= '.gz';
476    }
477  }
478  $fn = utf8_encodeFN($fn);
479  return $fn;
480}
481
482/**
483 * Returns the full filepath to a localized textfile if local
484 * version isn't found the english one is returned
485 *
486 * @author Andreas Gohr <andi@splitbrain.org>
487 */
488function localeFN($id){
489  global $conf;
490  $file = './lang/'.$conf['lang'].'/'.$id.'.txt';
491  if(!@file_exists($file)){
492    //fall back to english
493    $file = './lang/en/'.$id.'.txt';
494  }
495  return cleanText($file);
496}
497
498/**
499 * convert line ending to unix format
500 *
501 * @see    formText() for 2crlf conversion
502 * @author Andreas Gohr <andi@splitbrain.org>
503 */
504function cleanText($text){
505  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
506  return $text;
507}
508
509/**
510 * Prepares text for print in Webforms by encoding special chars.
511 * It also converts line endings to Windows format which is
512 * pseudo standard for webforms.
513 *
514 * @see    cleanText() for 2unix conversion
515 * @author Andreas Gohr <andi@splitbrain.org>
516 */
517function formText($text){
518  $text = preg_replace("/\012/","\015\012",$text);
519  return htmlspecialchars($text);
520}
521
522/**
523 * Returns the specified local text in parsed format
524 *
525 * @author Andreas Gohr <andi@splitbrain.org>
526 */
527function parsedLocale($id){
528  //disable section editing
529  global $parser;
530  $se = $parser['secedit'];
531  $parser['secedit'] = false;
532  //fetch parsed locale
533  $html = io_cacheParse(localeFN($id));
534  //reset section editing
535  $parser['secedit'] = $se;
536  return $html;
537}
538
539/**
540 * Returns the specified local text in raw format
541 *
542 * @author Andreas Gohr <andi@splitbrain.org>
543 */
544function rawLocale($id){
545  return io_readFile(localeFN($id));
546}
547
548
549/**
550 * Returns the parsed Wikitext for the given id and revision.
551 *
552 * If $excuse is true an explanation is returned if the file
553 * wasn't found
554 *
555 * @author Andreas Gohr <andi@splitbrain.org>
556 */
557function parsedWiki($id,$rev='',$excuse=true){
558  $file = wikiFN($id,$rev);
559  $ret  = '';
560
561  //ensure $id is in global $ID (needed for parsing)
562  global $ID;
563  $ID = $id;
564
565  if($rev){
566    if(@file_exists($file)){
567      $ret = parse(io_readFile($file));
568    }elseif($excuse){
569      $ret = parsedLocale('norev');
570    }
571  }else{
572    if(@file_exists($file)){
573      $ret = io_cacheParse($file);
574    }elseif($excuse){
575      $ret = parsedLocale('newpage');
576    }
577  }
578  return $ret;
579}
580
581/**
582 * Returns the raw WikiText
583 *
584 * @author Andreas Gohr <andi@splitbrain.org>
585 */
586function rawWiki($id,$rev=''){
587  return io_readFile(wikiFN($id,$rev));
588}
589
590/**
591 * Returns the raw Wiki Text in three slices.
592 *
593 * The range parameter needs to have the form "from-to"
594 * and gives the range of the section.
595 * The returned order is prefix, section and suffix.
596 *
597 * @author Andreas Gohr <andi@splitbrain.org>
598 */
599function rawWikiSlices($range,$id,$rev=''){
600  list($from,$to) = split('-',$range,2);
601  $text = io_readFile(wikiFN($id,$rev));
602  $text = split("\n",$text);
603  if(!$from) $from = 0;
604  if(!$to)   $to   = count($text);
605
606  $slices[0] = join("\n",array_slice($text,0,$from));
607  $slices[1] = join("\n",array_slice($text,$from,$to + 1  - $from));
608  $slices[2] = join("\n",array_slice($text,$to+1));
609
610  return $slices;
611}
612
613/**
614 * Joins wiki text slices
615 *
616 * function to join the text slices with correct lineendings again.
617 * When the pretty parameter is set to true it adds additional empty
618 * lines between sections if needed (used on saving).
619 *
620 * @author Andreas Gohr <andi@splitbrain.org>
621 */
622function con($pre,$text,$suf,$pretty=false){
623
624  if($pretty){
625    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
626    if($suf && substr($text,-1) != "\n") $text .= "\n";
627  }
628
629  if($pre) $pre .= "\n";
630  if($suf) $text .= "\n";
631  return $pre.$text.$suf;
632}
633
634/**
635 * print debug messages
636 *
637 * little function to print the content of a var
638 *
639 * @author Andreas Gohr <andi@splitbrain.org>
640 */
641function dbg($msg,$hidden=false){
642  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
643  print_r($msg);
644  (!$hidden) ? print '</pre>' : print "\n-->";
645}
646
647/**
648 * Add's an entry to the changelog
649 *
650 * @author Andreas Gohr <andi@splitbrain.org>
651 */
652function addLogEntry($date,$id,$summary=""){
653  global $conf;
654  $id     = cleanID($id);
655  if(!$date) $date = time(); //use current time if none supplied
656  $remote = $_SERVER['REMOTE_ADDR'];
657  $user   = $_SERVER['REMOTE_USER'];
658
659  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
660
661  $fh = fopen($conf['changelog'],'a');
662  if($fh){
663    fwrite($fh,$logline);
664    fclose($fh);
665  }
666}
667
668/**
669 * returns an array of recently changed files using the
670 * changelog
671 *
672 * @author Andreas Gohr <andi@splitbrain.org>
673 */
674function getRecents($num=0,$incdel=false){
675  global $conf;
676  $recent = array();
677  if(!$num) $num = $conf['recent'];
678
679  $loglines = file($conf['changelog']);
680  rsort($loglines); //reverse sort on timestamp
681
682  foreach ($loglines as $line){
683    $line = rtrim($line);        //remove newline
684    if(empty($line)) continue;   //skip empty lines
685    $info = split("\t",$line);   //split into parts
686    //add id if not in yet and file still exists and is allowed to read
687    if(!$recent[$info[2]] &&
688       (@file_exists(wikiFN($info[2])) || $incdel) &&
689       (auth_quickaclcheck($info[2]) >= AUTH_READ)
690      ){
691      $recent[$info[2]]['date'] = $info[0];
692      $recent[$info[2]]['ip']   = $info[1];
693      $recent[$info[2]]['user'] = $info[3];
694      $recent[$info[2]]['sum']  = $info[4];
695      $recent[$info[2]]['del']  = !@file_exists(wikiFN($info[2]));
696    }
697    if(count($recent) >= $num){
698      break; //finish if enough items found
699    }
700  }
701  return $recent;
702}
703
704/**
705 * gets additonal informations for a certain pagerevison
706 * from the changelog
707 *
708 * @author Andreas Gohr <andi@splitbrain.org>
709 */
710function getRevisionInfo($id,$rev){
711  global $conf;
712  $loglines = file($conf['changelog']);
713  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
714  rsort($loglines); //reverse sort on timestamp (shouldn't be needed)
715  $line = split("\t",$loglines[0]);
716  $info['date'] = $line[0];
717  $info['ip']   = $line[1];
718  $info['user'] = $line[3];
719  $info['sum']   = $line[4];
720  return $info;
721}
722
723/**
724 * Saves a wikitext by calling io_saveFile
725 *
726 * @author Andreas Gohr <andi@splitbrain.org>
727 */
728function saveWikiText($id,$text,$summary){
729  global $conf;
730  global $lang;
731  umask($conf['umask']);
732  // ignore if no changes were made
733  if($text == rawWiki($id,'')){
734    return;
735  }
736
737  $file = wikiFN($id);
738  $old  = saveOldRevision($id);
739
740  if (empty($text)){
741    // remove empty files
742    @unlink($file);
743    $del = true;
744    $summary = $lang['deleted']; //autoset summary on deletion
745  }else{
746    // save file (datadir is created in io_saveFile)
747    io_saveFile($file,$text);
748    $del = false;
749  }
750
751  addLogEntry(@filemtime($file),$id,$summary);
752  notify($id,$old,$summary);
753
754  //purge cache on add by updating the purgefile
755  if($conf['purgeonadd'] && (!$old || $del)){
756    io_saveFile($conf['datadir'].'/.cache/purgefile',time());
757  }
758}
759
760/**
761 * moves the current version to the attic and returns its
762 * revision date
763 *
764 * @author Andreas Gohr <andi@splitbrain.org>
765 */
766function saveOldRevision($id){
767	global $conf;
768  umask($conf['umask']);
769  $oldf = wikiFN($id);
770  if(!@file_exists($oldf)) return '';
771  $date = filemtime($oldf);
772  $newf = wikiFN($id,$date);
773  if(substr($newf,-3)=='.gz'){
774    io_saveFile($newf,rawWiki($id));
775  }else{
776    io_makeFileDir($newf);
777    copy($oldf, $newf);
778  }
779  return $date;
780}
781
782/**
783 * Sends a notify mail to the wikiadmin when a page was
784 * changed
785 *
786 * @author Andreas Gohr <andi@splitbrain.org>
787 */
788function notify($id,$rev="",$summary=""){
789  global $lang;
790  global $conf;
791  $hdrs ='';
792  if(empty($conf['notify'])) return; //notify enabled?
793
794  $text = rawLocale('mailtext');
795  $text = str_replace('@DATE@',date($conf['dformat']),$text);
796  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
797  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
798  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
799  $text = str_replace('@NEWPAGE@',wl($id,'','doku.php',true),$text);
800  $text = str_replace('@DOKUWIKIURL@',getBaseURL(true),$text);
801  $text = str_replace('@SUMMARY@',$summary,$text);
802  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
803
804  if($rev){
805    $subject = $lang['mail_changed'].' '.$id;
806    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",'doku.php',true),$text);
807    require_once("inc/DifferenceEngine.php");
808    $df  = new Diff(split("\n",rawWiki($id,$rev)),
809                    split("\n",rawWiki($id)));
810    $dformat = new UnifiedDiffFormatter();
811    $diff    = $dformat->format($df);
812  }else{
813    $subject=$lang['mail_newpage'].' '.$id;
814    $text = str_replace('@OLDPAGE@','none',$text);
815    $diff = rawWiki($id);
816  }
817  $text = str_replace('@DIFF@',$diff,$text);
818
819  mail_send($conf['notify'],$subject,$text,$conf['mailfrom']);
820}
821
822/**
823 * Return a list of available page revisons
824 *
825 * @author Andreas Gohr <andi@splitbrain.org>
826 */
827function getRevisions($id){
828  $revd = dirname(wikiFN($id,'foo'));
829  $revs = array();
830  $clid = cleanID($id);
831  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
832
833  if (is_dir($revd) && $dh = opendir($revd)) {
834    while (($file = readdir($dh)) !== false) {
835      if (is_dir($revd.'/'.$file)) continue;
836      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
837        $revs[]=$match[1];
838      }
839    }
840    closedir($dh);
841  }
842  rsort($revs);
843  return $revs;
844}
845
846/**
847 * downloads a file from the net and saves it to the given location
848 *
849 * @author Andreas Gohr <andi@splitbrain.org>
850 */
851function download($url,$file){
852  $fp = @fopen($url,"rb");
853  if(!$fp) return false;
854
855  while(!feof($fp)){
856    $cont.= fread($fp,1024);
857  }
858  fclose($fp);
859
860  $fp2 = @fopen($file,"w");
861  if(!$fp2) return false;
862  fwrite($fp2,$cont);
863  fclose($fp2);
864  return true;
865}
866
867/**
868 * extracts the query from a google referer
869 *
870 * @author Andreas Gohr <andi@splitbrain.org>
871 */
872function getGoogleQuery(){
873  $url = parse_url($_SERVER['HTTP_REFERER']);
874
875  if(!preg_match("#google\.#i",$url['host'])) return '';
876  $query = array();
877  parse_str($url['query'],$query);
878
879  return $query['q'];
880}
881
882/**
883 * Try to set correct locale
884 *
885 * @deprecated No longer used
886 * @author     Andreas Gohr <andi@splitbrain.org>
887 */
888function setCorrectLocale(){
889  global $conf;
890  global $lang;
891
892  $enc = strtoupper($lang['encoding']);
893  foreach ($lang['locales'] as $loc){
894    //try locale
895    if(@setlocale(LC_ALL,$loc)) return;
896    //try loceale with encoding
897    if(@setlocale(LC_ALL,"$loc.$enc")) return;
898  }
899  //still here? try to set from environment
900  @setlocale(LC_ALL,"");
901}
902
903/**
904 * Return the human readable size of a file
905 *
906 * @param       int    $size   A file size
907 * @param       int    $dec    A number of decimal places
908 * @author      Martin Benjamin <b.martin@cybernet.ch>
909 * @author      Aidan Lister <aidan@php.net>
910 * @version     1.0.0
911 */
912function filesize_h($size, $dec = 1){
913  $sizes = array('B', 'KB', 'MB', 'GB');
914  $count = count($sizes);
915  $i = 0;
916
917  while ($size >= 1024 && ($i < $count - 1)) {
918    $size /= 1024;
919    $i++;
920  }
921
922  return round($size, $dec) . ' ' . $sizes[$i];
923}
924
925/**
926 * Run a few sanity checks
927 *
928 * @author Andreas Gohr <andi@splitbrain.org>
929 */
930function getVersion(){
931  //import version string
932  if(@file_exists('VERSION')){
933    //official release
934    return 'Release '.io_readfile('VERSION');
935  }elseif(is_dir('_darcs')){
936    //darcs checkout
937    $inv = file('_darcs/inventory');
938    $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv);
939    $cur = array_pop($inv);
940    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
941    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
942  }else{
943    return 'snapshot?';
944  }
945}
946
947/**
948 * Run a few sanity checks
949 *
950 * @author Andreas Gohr <andi@splitbrain.org>
951 */
952function check(){
953  global $conf;
954  global $INFO;
955
956  msg('DokuWiki version: '.getVersion(),1);
957
958  if(version_compare(phpversion(),'4.3.0','<')){
959    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
960  }elseif(version_compare(phpversion(),'4.3.10','<')){
961    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
962  }else{
963    msg('PHP version '.phpversion(),1);
964  }
965
966  if(is_writable($conf['changelog'])){
967    msg('Changelog is writable',1);
968  }else{
969    msg('Changelog is not writable',-1);
970  }
971
972  if(is_writable($conf['datadir'])){
973    msg('Datadir is writable',1);
974  }else{
975    msg('Datadir is not writable',-1);
976  }
977
978  if(is_writable($conf['olddir'])){
979    msg('Attic is writable',1);
980  }else{
981    msg('Attic is not writable',-1);
982  }
983
984  if(is_writable($conf['mediadir'])){
985    msg('Mediadir is writable',1);
986  }else{
987    msg('Mediadir is not writable',-1);
988  }
989
990  if(is_writable('conf/users.auth')){
991    msg('conf/users.auth is writable',1);
992  }else{
993    msg('conf/users.auth is not writable',0);
994  }
995
996  if(function_exists('mb_strpos')){
997    if(defined('UTF8_NOMBSTRING')){
998      msg('mb_string extension is available but will not be used',0);
999    }else{
1000      msg('mb_string extension is available and will be used',1);
1001    }
1002  }else{
1003    msg('mb_string extension not available - PHP only replacements will be used',0);
1004  }
1005
1006  msg('Your current permission for this page is '.$INFO['perm'],0);
1007
1008  if(is_writable($INFO['filepath'])){
1009    msg('The current page is writable by the webserver',0);
1010  }else{
1011    msg('The current page is not writable by the webserver',0);
1012  }
1013
1014  if($INFO['writable']){
1015    msg('The current page is writable by you',0);
1016  }else{
1017    msg('The current page is not writable you',0);
1018  }
1019}
1020?>
1021