xref: /dokuwiki/inc/common.php (revision b158d625b53833ef391800a991ad93d965d9425e)
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['user']     = $_SERVER['REMOTE_USER'];
30    $info['userinfo'] = $USERINFO;
31    $info['perm']     = auth_quickaclcheck($ID);
32  }else{
33    $info['user']     = '';
34    $info['perm']     = auth_aclcheck($ID,'',null);
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  $info['editor'] = $revinfo['ip'];
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  $more = str_replace(',','&amp;',$more);
240
241  $id    = idfilter($id);
242  if($abs){
243    $xlink = DOKU_URL;
244  }else{
245    $xlink = DOKU_BASE;
246  }
247
248  if($conf['userewrite'] == 2){
249    $xlink .= DOKU_SCRIPT.'/'.$id;
250    if($more) $xlink .= '?'.$more;
251  }elseif($conf['userewrite']){
252    $xlink .= $id;
253    if($more) $xlink .= '?'.$more;
254  }else{
255    $xlink .= DOKU_SCRIPT.'?id='.$id;
256    if($more) $xlink .= '&amp;'.$more;
257  }
258
259  return $xlink;
260}
261
262/**
263 * Just builds a link to a script
264 *
265 * @todo   maybe obsolete
266 * @author Andreas Gohr <andi@splitbrain.org>
267 */
268function script($script='doku.php'){
269#  $link = getBaseURL();
270#  $link .= $script;
271#  return $link;
272  return DOKU_BASE.DOKU_SCRIPT;
273}
274
275/**
276 * Spamcheck against wordlist
277 *
278 * Checks the wikitext against a list of blocked expressions
279 * returns true if the text contains any bad words
280 *
281 * @author Andreas Gohr <andi@splitbrain.org>
282 */
283function checkwordblock(){
284  global $TEXT;
285  global $conf;
286
287  if(!$conf['usewordblock']) return false;
288
289  $blockfile = file(DOKU_CONF.'wordblock.conf');
290  //how many lines to read at once (to work around some PCRE limits)
291  if(version_compare(phpversion(),'4.3.0','<')){
292    //old versions of PCRE define a maximum of parenthesises even if no
293    //backreferences are used - the maximum is 99
294    //this is very bad performancewise and may even be too high still
295    $chunksize = 40;
296  }else{
297    //read file in chunks of 600 - this should work around the
298    //MAX_PATTERN_SIZE in modern PCRE
299    $chunksize = 600;
300  }
301  while($blocks = array_splice($blockfile,0,$chunksize)){
302    $re = array();
303    #build regexp from blocks
304    foreach($blocks as $block){
305      $block = preg_replace('/#.*$/','',$block);
306      $block = trim($block);
307      if(empty($block)) continue;
308      $re[]  = $block;
309    }
310    if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true;
311  }
312  return false;
313}
314
315/**
316 * Return the IP of the client
317 *
318 * Honours X-Forwarded-For Proxy Headers
319 *
320 * @author Andreas Gohr <andi@splitbrain.org>
321 */
322function clientIP(){
323  $my = $_SERVER['REMOTE_ADDR'];
324  if($_SERVER['HTTP_X_FORWARDED_FOR']){
325    $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')';
326  }
327  return $my;
328}
329
330/**
331 * Checks if a given page is currently locked.
332 *
333 * removes stale lockfiles
334 *
335 * @author Andreas Gohr <andi@splitbrain.org>
336 */
337function checklock($id){
338  global $conf;
339  $lock = wikiFN($id).'.lock';
340
341  //no lockfile
342  if(!@file_exists($lock)) return false;
343
344  //lockfile expired
345  if((time() - filemtime($lock)) > $conf['locktime']){
346    unlink($lock);
347    return false;
348  }
349
350  //my own lock
351  $ip = io_readFile($lock);
352  if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
353    return false;
354  }
355
356  return $ip;
357}
358
359/**
360 * Lock a page for editing
361 *
362 * @author Andreas Gohr <andi@splitbrain.org>
363 */
364function lock($id){
365  $lock = wikiFN($id).'.lock';
366  if($_SERVER['REMOTE_USER']){
367    io_saveFile($lock,$_SERVER['REMOTE_USER']);
368  }else{
369    io_saveFile($lock,clientIP());
370  }
371}
372
373/**
374 * Unlock a page if it was locked by the user
375 *
376 * @author Andreas Gohr <andi@splitbrain.org>
377 * @return bool true if a lock was removed
378 */
379function unlock($id){
380  $lock = wikiFN($id).'.lock';
381  if(@file_exists($lock)){
382    $ip = io_readFile($lock);
383    if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){
384      @unlink($lock);
385      return true;
386    }
387  }
388  return false;
389}
390
391/**
392 * convert line ending to unix format
393 *
394 * @see    formText() for 2crlf conversion
395 * @author Andreas Gohr <andi@splitbrain.org>
396 */
397function cleanText($text){
398  $text = preg_replace("/(\015\012)|(\015)/","\012",$text);
399  return $text;
400}
401
402/**
403 * Prepares text for print in Webforms by encoding special chars.
404 * It also converts line endings to Windows format which is
405 * pseudo standard for webforms.
406 *
407 * @see    cleanText() for 2unix conversion
408 * @author Andreas Gohr <andi@splitbrain.org>
409 */
410function formText($text){
411  $text = preg_replace("/\012/","\015\012",$text);
412  return htmlspecialchars($text);
413}
414
415/**
416 * Returns the specified local text in raw format
417 *
418 * @author Andreas Gohr <andi@splitbrain.org>
419 */
420function rawLocale($id){
421  return io_readFile(localeFN($id));
422}
423
424/**
425 * Returns the raw WikiText
426 *
427 * @author Andreas Gohr <andi@splitbrain.org>
428 */
429function rawWiki($id,$rev=''){
430  return io_readFile(wikiFN($id,$rev));
431}
432
433/**
434 * Returns the pagetemplate contents for the ID's namespace
435 *
436 * @author Andreas Gohr <andi@splitbrain.org>
437 */
438function pageTemplate($id){
439  return io_readFile(dirname(wikiFN($id)).'/_template.txt');
440}
441
442
443/**
444 * Returns the raw Wiki Text in three slices.
445 *
446 * The range parameter needs to have the form "from-to"
447 * and gives the range of the section in bytes - no
448 * UTF-8 awareness is needed.
449 * The returned order is prefix, section and suffix.
450 *
451 * @author Andreas Gohr <andi@splitbrain.org>
452 */
453function rawWikiSlices($range,$id,$rev=''){
454  list($from,$to) = split('-',$range,2);
455  $text = io_readFile(wikiFN($id,$rev));
456  if(!$from) $from = 0;
457  if(!$to)   $to   = strlen($text)+1;
458
459  $slices[0] = substr($text,0,$from-1);
460  $slices[1] = substr($text,$from-1,$to-$from);
461  $slices[2] = substr($text,$to);
462
463  return $slices;
464}
465
466/**
467 * Joins wiki text slices
468 *
469 * function to join the text slices with correct lineendings again.
470 * When the pretty parameter is set to true it adds additional empty
471 * lines between sections if needed (used on saving).
472 *
473 * @author Andreas Gohr <andi@splitbrain.org>
474 */
475function con($pre,$text,$suf,$pretty=false){
476
477  if($pretty){
478    if($pre && substr($pre,-1) != "\n") $pre .= "\n";
479    if($suf && substr($text,-1) != "\n") $text .= "\n";
480  }
481
482  if($pre) $pre .= "\n";
483  if($suf) $text .= "\n";
484  return $pre.$text.$suf;
485}
486
487/**
488 * print debug messages
489 *
490 * little function to print the content of a var
491 *
492 * @author Andreas Gohr <andi@splitbrain.org>
493 */
494function dbg($msg,$hidden=false){
495  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
496  print_r($msg);
497  (!$hidden) ? print '</pre>' : print "\n-->";
498}
499
500/**
501 * Add's an entry to the changelog
502 *
503 * @author Andreas Gohr <andi@splitbrain.org>
504 */
505function addLogEntry($date,$id,$summary=""){
506  global $conf;
507  $id     = cleanID($id);//FIXME not needed anymore?
508
509  if(!@is_writable($conf['changelog'])){
510    msg($conf['changelog'].' is not writable!',-1);
511    return;
512  }
513
514  if(!$date) $date = time(); //use current time if none supplied
515  $remote = $_SERVER['REMOTE_ADDR'];
516  $user   = $_SERVER['REMOTE_USER'];
517
518  $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n";
519
520  //FIXME: use adjusted io_saveFile instead
521  $fh = fopen($conf['changelog'],'a');
522  if($fh){
523    fwrite($fh,$logline);
524    fclose($fh);
525  }
526}
527
528/**
529 * returns an array of recently changed files using the
530 * changelog
531 * first   : first entry in array returned
532 * num     : return 'num' entries
533 *
534 * @author Andreas Gohr <andi@splitbrain.org>
535 */
536function getRecents($first,$num,$incdel=false){
537  global $conf;
538  $recent = array();
539  $names  = array();
540
541  if(!$num)
542    return $recent;
543
544  if(!@is_readable($conf['changelog'])){
545    msg($conf['changelog'].' is not readable',-1);
546    return $recent;
547  }
548
549  $loglines = file($conf['changelog']);
550  rsort($loglines); //reverse sort on timestamp
551
552  foreach ($loglines as $line){
553    $line = rtrim($line);        //remove newline
554    if(empty($line)) continue;   //skip empty lines
555    $info = split("\t",$line);   //split into parts
556    //add id if not in yet and file still exists and is allowed to read
557    if(!$names[$info[2]] &&
558       (@file_exists(wikiFN($info[2])) || $incdel) &&
559       (auth_quickaclcheck($info[2]) >= AUTH_READ)
560      ){
561      $names[$info[2]] = 1;
562      if(--$first >= 0) continue;  /* skip "first" entries */
563
564      $recent[$info[2]]['date'] = $info[0];
565      $recent[$info[2]]['ip']   = $info[1];
566      $recent[$info[2]]['user'] = $info[3];
567      $recent[$info[2]]['sum']  = $info[4];
568      $recent[$info[2]]['del']  = !@file_exists(wikiFN($info[2]));
569    }
570    if(count($recent) >= $num){
571      break; //finish if enough items found
572    }
573  }
574  return $recent;
575}
576
577/**
578 * gets additonal informations for a certain pagerevison
579 * from the changelog
580 *
581 * @author Andreas Gohr <andi@splitbrain.org>
582 */
583function getRevisionInfo($id,$rev){
584  global $conf;
585
586  if(!$rev) return(null);
587
588  $info = array();
589  if(!@is_readable($conf['changelog'])){
590    msg($conf['changelog'].' is not readable',-1);
591    return $recent;
592  }
593  $loglines = file($conf['changelog']);
594  $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines);
595  $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed)
596  $line = split("\t",$loglines[0]);
597  $info['date'] = $line[0];
598  $info['ip']   = $line[1];
599  $info['user'] = $line[3];
600  $info['sum']   = $line[4];
601  return $info;
602}
603
604/**
605 * Saves a wikitext by calling io_saveFile
606 *
607 * @author Andreas Gohr <andi@splitbrain.org>
608 */
609function saveWikiText($id,$text,$summary){
610  global $conf;
611  global $lang;
612  umask($conf['umask']);
613  // ignore if no changes were made
614  if($text == rawWiki($id,'')){
615    return;
616  }
617
618  $file = wikiFN($id);
619  $old  = saveOldRevision($id);
620
621  if (empty($text)){
622    // remove empty files
623    @unlink($file);
624    $mfile=wikiMN($id);
625    if (file_exists($mfile)) {
626      @unlink($mfile);
627    }
628    $del = true;
629    //autoset summary on deletion
630    if(empty($summary)) $summary = $lang['deleted'];
631    //remove empty namespaces
632    io_sweepNS($id);
633  }else{
634    // save file (datadir is created in io_saveFile)
635    io_saveFile($file,$text);
636    $del = false;
637  }
638
639  addLogEntry(@filemtime($file),$id,$summary);
640  notify($id,$old,$summary);
641
642  //purge cache on add by updating the purgefile
643  if($conf['purgeonadd'] && (!$old || $del)){
644    io_saveFile($conf['cachedir'].'/purgefile',time());
645  }
646}
647
648/**
649 * moves the current version to the attic and returns its
650 * revision date
651 *
652 * @author Andreas Gohr <andi@splitbrain.org>
653 */
654function saveOldRevision($id){
655	global $conf;
656  umask($conf['umask']);
657  $oldf = wikiFN($id);
658  if(!@file_exists($oldf)) return '';
659  $date = filemtime($oldf);
660  $newf = wikiFN($id,$date);
661  if(substr($newf,-3)=='.gz'){
662    io_saveFile($newf,rawWiki($id));
663  }else{
664    io_makeFileDir($newf);
665    copy($oldf, $newf);
666  }
667  return $date;
668}
669
670/**
671 * Sends a notify mail to the wikiadmin when a page was
672 * changed
673 *
674 * @author Andreas Gohr <andi@splitbrain.org>
675 */
676function notify($id,$rev="",$summary=""){
677  global $lang;
678  global $conf;
679  $hdrs ='';
680
681  $mlist = array();
682
683  $file=wikiMN($id);
684  if (file_exists($file)) {
685    $mlist = file($file);
686  }
687
688  if(empty($conf['notify']) && count($mlist) == 0) return; //notify enabled?
689
690  $text = rawLocale('mailtext');
691  $text = str_replace('@DATE@',date($conf['dformat']),$text);
692  $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
693  $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
694  $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
695  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
696  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
697  $text = str_replace('@SUMMARY@',$summary,$text);
698  $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
699
700  if($rev){
701    $subject = $lang['mail_changed'].' '.$id;
702    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
703    require_once("inc/DifferenceEngine.php");
704    $df  = new Diff(split("\n",rawWiki($id,$rev)),
705                    split("\n",rawWiki($id)));
706    $dformat = new UnifiedDiffFormatter();
707    $diff    = $dformat->format($df);
708  }else{
709    $subject=$lang['mail_newpage'].' '.$id;
710    $text = str_replace('@OLDPAGE@','none',$text);
711    $diff = rawWiki($id);
712  }
713  $text = str_replace('@DIFF@',$diff,$text);
714  $subject = '['.$conf['title'].'] '.$subject;
715
716  $bcc = '';
717  if(count($mlist) > 0) {
718    foreach ($mlist as $who) {
719      $who = rtrim($who);
720      $info = auth_getUserData($who);
721      $level = auth_aclcheck($id,$who,$info['grps']);
722      if ($level >= AUTH_READ) {
723        if (strcasecmp($info['mail'],$conf['notify']) != 0) {
724          if (empty($bcc)) {
725            $bcc = $info['mail'];
726          } else {
727            $bcc = "$bcc,".$info['mail'];
728          }
729        }
730      }
731    }
732  }
733
734  mail_send($conf['notify'],$subject,$text,$conf['mailfrom'],'',$bcc);
735}
736
737/**
738 * Return a list of available page revisons
739 *
740 * @author Andreas Gohr <andi@splitbrain.org>
741 */
742function getRevisions($id){
743  $revd = dirname(wikiFN($id,'foo'));
744  $revs = array();
745  $clid = cleanID($id);
746  if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path
747  $clid = utf8_encodeFN($clid);
748
749  if (is_dir($revd) && $dh = opendir($revd)) {
750    while (($file = readdir($dh)) !== false) {
751      if (is_dir($revd.'/'.$file)) continue;
752      if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){
753        $revs[]=$match[1];
754      }
755    }
756    closedir($dh);
757  }
758  rsort($revs);
759  return $revs;
760}
761
762/**
763 * extracts the query from a google referer
764 *
765 * @todo   should be more generic and support yahoo et al
766 * @author Andreas Gohr <andi@splitbrain.org>
767 */
768function getGoogleQuery(){
769  $url = parse_url($_SERVER['HTTP_REFERER']);
770  if(!$url) return '';
771
772  if(!preg_match("#google\.#i",$url['host'])) return '';
773  $query = array();
774  parse_str($url['query'],$query);
775
776  return $query['q'];
777}
778
779/**
780 * Try to set correct locale
781 *
782 * @deprecated No longer used
783 * @author     Andreas Gohr <andi@splitbrain.org>
784 */
785function setCorrectLocale(){
786  global $conf;
787  global $lang;
788
789  $enc = strtoupper($lang['encoding']);
790  foreach ($lang['locales'] as $loc){
791    //try locale
792    if(@setlocale(LC_ALL,$loc)) return;
793    //try loceale with encoding
794    if(@setlocale(LC_ALL,"$loc.$enc")) return;
795  }
796  //still here? try to set from environment
797  @setlocale(LC_ALL,"");
798}
799
800/**
801 * Return the human readable size of a file
802 *
803 * @param       int    $size   A file size
804 * @param       int    $dec    A number of decimal places
805 * @author      Martin Benjamin <b.martin@cybernet.ch>
806 * @author      Aidan Lister <aidan@php.net>
807 * @version     1.0.0
808 */
809function filesize_h($size, $dec = 1){
810  $sizes = array('B', 'KB', 'MB', 'GB');
811  $count = count($sizes);
812  $i = 0;
813
814  while ($size >= 1024 && ($i < $count - 1)) {
815    $size /= 1024;
816    $i++;
817  }
818
819  return round($size, $dec) . ' ' . $sizes[$i];
820}
821
822/**
823 * Return DokuWikis version
824 *
825 * @author Andreas Gohr <andi@splitbrain.org>
826 */
827function getVersion(){
828  //import version string
829  if(@file_exists('VERSION')){
830    //official release
831    return 'Release '.trim(io_readfile('VERSION'));
832  }elseif(is_dir('_darcs')){
833    //darcs checkout
834    $inv = file('_darcs/inventory');
835    $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv);
836    $cur = array_pop($inv);
837    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
838    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
839  }else{
840    return 'snapshot?';
841  }
842}
843
844/**
845 * Run a few sanity checks
846 *
847 * @author Andreas Gohr <andi@splitbrain.org>
848 */
849function check(){
850  global $conf;
851  global $INFO;
852
853  msg('DokuWiki version: '.getVersion(),1);
854
855  if(version_compare(phpversion(),'4.3.0','<')){
856    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1);
857  }elseif(version_compare(phpversion(),'4.3.10','<')){
858    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
859  }else{
860    msg('PHP version '.phpversion(),1);
861  }
862
863  if(is_writable($conf['changelog'])){
864    msg('Changelog is writable',1);
865  }else{
866    msg('Changelog is not writable',-1);
867  }
868
869  if(is_writable($conf['datadir'])){
870    msg('Datadir is writable',1);
871  }else{
872    msg('Datadir is not writable',-1);
873  }
874
875  if(is_writable($conf['olddir'])){
876    msg('Attic is writable',1);
877  }else{
878    msg('Attic is not writable',-1);
879  }
880
881  if(is_writable($conf['mediadir'])){
882    msg('Mediadir is writable',1);
883  }else{
884    msg('Mediadir is not writable',-1);
885  }
886
887  if(is_writable($conf['cachedir'])){
888    msg('Cachedir is writable',1);
889  }else{
890    msg('Cachedir is not writable',-1);
891  }
892
893  if(is_writable(DOKU_CONF.'users.auth.php')){
894    msg('conf/users.auth.php is writable',1);
895  }else{
896    msg('conf/users.auth.php is not writable',0);
897  }
898
899  if(function_exists('mb_strpos')){
900    if(defined('UTF8_NOMBSTRING')){
901      msg('mb_string extension is available but will not be used',0);
902    }else{
903      msg('mb_string extension is available and will be used',1);
904    }
905  }else{
906    msg('mb_string extension not available - PHP only replacements will be used',0);
907  }
908
909  msg('Your current permission for this page is '.$INFO['perm'],0);
910
911  if(is_writable($INFO['filepath'])){
912    msg('The current page is writable by the webserver',0);
913  }else{
914    msg('The current page is not writable by the webserver',0);
915  }
916
917  if($INFO['writable']){
918    msg('The current page is writable by you',0);
919  }else{
920    msg('The current page is not writable you',0);
921  }
922}
923
924/**
925 * Let us know if a user is tracking a page
926 *
927 * @author Steven Danz <steven-danz@kc.rr.com>
928 */
929function tracking($id,$uid){
930  $file=wikiMN($id);
931  if (file_exists($file)) {
932    $mlist = file($file);
933    foreach ($mlist as $who) {
934      $who = rtrim($who);
935      if ($who==$uid) {
936        return true;
937      }
938    }
939  }
940  return false;
941}
942
943//Setup VIM: ex: et ts=2 enc=utf-8 :
944