xref: /dokuwiki/inc/infoutils.php (revision 15f699ac4db38c7098b4ae4cd0782dff13d46637)
1<?php
2/**
3 * Information and debugging functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9use dokuwiki\HTTP\DokuHTTPClient;
10use dokuwiki\Search\MetadataIndex;
11use dokuwiki\Search\FulltextIndex;
12use dokuwiki\Utf8;
13
14if(!defined('DOKU_MESSAGEURL')){
15    if(in_array('ssl', stream_get_transports())) {
16        define('DOKU_MESSAGEURL','https://update.dokuwiki.org/check/');
17    }else{
18        define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
19    }
20}
21
22/**
23 * Check for new messages from upstream
24 *
25 * @author Andreas Gohr <andi@splitbrain.org>
26 */
27function checkUpdateMessages(){
28    global $conf;
29    global $INFO;
30    global $updateVersion;
31    if(!$conf['updatecheck']) return;
32    if($conf['useacl'] && !$INFO['ismanager']) return;
33
34    $cf = getCacheName($updateVersion, '.updmsg');
35    $lm = @filemtime($cf);
36    $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https';
37
38    // check if new messages needs to be fetched
39    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
40        @touch($cf);
41        dbglog("checkUpdateMessages(): downloading messages to ".$cf.($is_http?' (without SSL)':' (with SSL)'));
42        $http = new DokuHTTPClient();
43        $http->timeout = 12;
44        $resp = $http->get(DOKU_MESSAGEURL.$updateVersion);
45        if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
46            // basic sanity check that this is either an empty string response (ie "no messages")
47            // or it looks like one of our messages, not WiFi login or other interposed response
48            io_saveFile($cf,$resp);
49        } else {
50            dbglog("checkUpdateMessages(): unexpected HTTP response received");
51        }
52    }else{
53        dbglog("checkUpdateMessages(): messages up to date");
54    }
55
56    $data = io_readFile($cf);
57    // show messages through the usual message mechanism
58    $msgs = explode("\n%\n",$data);
59    foreach($msgs as $msg){
60        if($msg) msg($msg,2);
61    }
62}
63
64
65/**
66 * Return DokuWiki's version (split up in date and type)
67 *
68 * @author Andreas Gohr <andi@splitbrain.org>
69 */
70function getVersionData(){
71    $version = array();
72    //import version string
73    if(file_exists(DOKU_INC.'VERSION')){
74        //official release
75        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
76        $version['type'] = 'Release';
77    }elseif(is_dir(DOKU_INC.'.git')){
78        $version['type'] = 'Git';
79        $version['date'] = 'unknown';
80
81        $inventory = DOKU_INC.'.git/logs/HEAD';
82        if(is_file($inventory)){
83            $sz   = filesize($inventory);
84            $seek = max(0,$sz-2000); // read from back of the file
85            $fh   = fopen($inventory,'rb');
86            fseek($fh,$seek);
87            $chunk = fread($fh,2000);
88            fclose($fh);
89            $chunk = trim($chunk);
90            $chunk = @array_pop(explode("\n",$chunk));   //last log line
91            $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg
92            $chunk = explode(" ",$chunk);
93            array_pop($chunk); //strip timezone
94            $date = date('Y-m-d',array_pop($chunk));
95            if($date) $version['date'] = $date;
96        }
97    }else{
98        global $updateVersion;
99        $version['date'] = 'update version '.$updateVersion;
100        $version['type'] = 'snapshot?';
101    }
102    return $version;
103}
104
105/**
106 * Return DokuWiki's version (as a string)
107 *
108 * @author Anika Henke <anika@selfthinker.org>
109 */
110function getVersion(){
111    $version = getVersionData();
112    return $version['type'].' '.$version['date'];
113}
114
115/**
116 * Run a few sanity checks
117 *
118 * @author Andreas Gohr <andi@splitbrain.org>
119 */
120function check(){
121    global $conf;
122    global $INFO;
123    /* @var Input $INPUT */
124    global $INPUT;
125
126    if ($INFO['isadmin'] || $INFO['ismanager']){
127        msg('DokuWiki version: '.getVersion(),1);
128
129        if(version_compare(phpversion(),'7.2.0','<')){
130            msg('Your PHP version is too old ('.phpversion().' vs. 7.2+ needed)',-1);
131        }else{
132            msg('PHP version '.phpversion(),1);
133        }
134    } else {
135        if(version_compare(phpversion(),'7.2.0','<')){
136            msg('Your PHP version is too old',-1);
137        }
138    }
139
140    $mem = (int) php_to_byte(ini_get('memory_limit'));
141    if($mem){
142        if ($mem === -1) {
143            msg('PHP memory is unlimited', 1);
144        } else if ($mem < 16777216) {
145            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
146            Increase memory_limit in php.ini', -1);
147        } else if ($mem < 20971520) {
148            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
149                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
150        } else if ($mem < 33554432) {
151            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
152                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
153        } else {
154            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
155        }
156    }
157
158    if(is_writable($conf['changelog'])){
159        msg('Changelog is writable',1);
160    }else{
161        if (file_exists($conf['changelog'])) {
162            msg('Changelog is not writable',-1);
163        }
164    }
165
166    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
167        msg('Old changelog exists', 0);
168    }
169
170    if (file_exists($conf['changelog'].'_failed')) {
171        msg('Importing old changelog failed', -1);
172    } else if (file_exists($conf['changelog'].'_importing')) {
173        msg('Importing old changelog now.', 0);
174    } else if (file_exists($conf['changelog'].'_import_ok')) {
175        msg('Old changelog imported', 1);
176        if (!plugin_isdisabled('importoldchangelog')) {
177            msg('Importoldchangelog plugin not disabled after import', -1);
178        }
179    }
180
181    if(is_writable(DOKU_CONF)){
182        msg('conf directory is writable',1);
183    }else{
184        msg('conf directory is not writable',-1);
185    }
186
187    if($conf['authtype'] == 'plain'){
188        global $config_cascade;
189        if(is_writable($config_cascade['plainauth.users']['default'])){
190            msg('conf/users.auth.php is writable',1);
191        }else{
192            msg('conf/users.auth.php is not writable',0);
193        }
194    }
195
196    if(function_exists('mb_strpos')){
197        if(defined('UTF8_NOMBSTRING')){
198            msg('mb_string extension is available but will not be used',0);
199        }else{
200            msg('mb_string extension is available and will be used',1);
201            if(ini_get('mbstring.func_overload') != 0){
202                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
203            }
204        }
205    }else{
206        msg('mb_string extension not available - PHP only replacements will be used',0);
207    }
208
209    if (!UTF8_PREGSUPPORT) {
210        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
211    }
212    if (!UTF8_PROPERTYSUPPORT) {
213        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
214    }
215
216    $loc = setlocale(LC_ALL, 0);
217    if(!$loc){
218        msg('No valid locale is set for your PHP setup. You should fix this',-1);
219    }elseif(stripos($loc,'utf') === false){
220        msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale,
221             you should fix this if you encounter problems.',0);
222    }else{
223        msg('Valid locale '.hsc($loc).' found.', 1);
224    }
225
226    if($conf['allowdebug']){
227        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
228    }else{
229        msg('Debugging support is disabled',1);
230    }
231
232    if($INFO['userinfo']['name']){
233        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
234        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
235    }else{
236        msg('You are currently not logged in',0);
237    }
238
239    msg('Your current permission for this page is '.$INFO['perm'],0);
240
241    if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) {
242        msg('The current page is writable by the webserver', 1);
243    } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) {
244        msg('The current page can be created by the webserver', 1);
245    } else {
246        msg('The current page is not writable by the webserver', -1);
247    }
248
249    if ($INFO['writable']) {
250        msg('The current page is writable by you', 1);
251    } else {
252        msg('The current page is not writable by you', -1);
253    }
254
255    // Check for corrupted fulltext search index
256    $FulltextIndex = FulltextIndex::getInstance();
257    $lengths = $FulltextIndex->listIndexLengths();
258    $index_corrupted = false;
259    foreach ($lengths as $length) {
260        if (count($FulltextIndex->getIndex('w', $length)) != count($FulltextIndex->getIndex('i', $length))) {
261            $index_corrupted = true;
262            break;
263        }
264    }
265
266    // Check for corrupted metadata index
267    $MetadataIndex = MetadataIndex::getInstance();
268    foreach ($MetadataIndex->getIndex('metadata', '') as $name) {
269        if (count($MetadataIndex->getIndex($name.'_w', '')) != count($MetadataIndex->getIndex($name.'_i', ''))) {
270            $index_corrupted = true;
271            break;
272        }
273    }
274
275    if($index_corrupted) {
276        msg(
277            'The search index is corrupted. It might produce wrong results and most
278                probably needs to be rebuilt. See
279                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
280                for ways to rebuild the search index.', -1
281        );
282    } elseif(!empty($lengths)) {
283        msg('The search index seems to be working', 1);
284    } else {
285        msg(
286            'The search index is empty. See
287                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
288                for help on how to fix the search index. If the default indexer
289                isn\'t used or the wiki is actually empty this is normal.'
290        );
291    }
292
293    // rough time check
294    $http = new DokuHTTPClient();
295    $http->max_redirect = 0;
296    $http->timeout = 3;
297    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
298    $now = time();
299    if(isset($http->resp_headers['date'])) {
300        $time = strtotime($http->resp_headers['date']);
301        $diff = $time - $now;
302
303        if(abs($diff) < 4) {
304            msg("Server time seems to be okay. Diff: {$diff}s", 1);
305        } else {
306            msg("Your server's clock seems to be out of sync!
307                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
308        }
309    }
310
311}
312
313/**
314 * Display a message to the user
315 *
316 * If HTTP headers were not sent yet the message is added
317 * to the global message array else it's printed directly
318 * using html_msgarea()
319 *
320 * Triggers INFOUTIL_MSG_SHOW
321 *
322 * @see    html_msgarea()
323 * @param string $message
324 * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
325 * @param string $line  line number
326 * @param string $file  file number
327 * @param int    $allow who's allowed to see the message, see MSG_* constants
328 */
329function msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
330    global $MSG, $MSG_shown;
331    static $errors = [
332        -1 => 'error',
333        0 => 'info',
334        1 => 'success',
335        2 => 'notify',
336    ];
337
338    $msgdata = [
339        'msg' => $message,
340        'lvl' => $errors[$lvl],
341        'allow' => $allow,
342        'line' => $line,
343        'file' => $file,
344    ];
345
346    $evt = new \dokuwiki\Extension\Event('INFOUTIL_MSG_SHOW', $msgdata);
347    if ($evt->advise_before()) {
348        /* Show msg normally - event could suppress message show */
349        if($msgdata['line'] || $msgdata['file']) {
350            $basename = \dokuwiki\Utf8\PhpString::basename($msgdata['file']);
351            $msgdata['msg'] .=' ['.$basename.':'.$msgdata['line'].']';
352        }
353
354        if(!isset($MSG)) $MSG = array();
355        $MSG[] = $msgdata;
356        if(isset($MSG_shown) || headers_sent()){
357            if(function_exists('html_msgarea')){
358                html_msgarea();
359            }else{
360                print "ERROR(".$msgdata['lvl'].") ".$msgdata['msg']."\n";
361            }
362            unset($GLOBALS['MSG']);
363        }
364    }
365    $evt->advise_after();
366    unset($evt);
367}
368/**
369 * Determine whether the current user is allowed to view the message
370 * in the $msg data structure
371 *
372 * @param  $msg   array    dokuwiki msg structure
373 *                         msg   => string, the message
374 *                         lvl   => int, level of the message (see msg() function)
375 *                         allow => int, flag used to determine who is allowed to see the message
376 *                                       see MSG_* constants
377 * @return bool
378 */
379function info_msg_allowed($msg){
380    global $INFO, $auth;
381
382    // is the message public? - everyone and anyone can see it
383    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
384
385    // restricted msg, but no authentication
386    if (empty($auth)) return false;
387
388    switch ($msg['allow']){
389        case MSG_USERS_ONLY:
390            return !empty($INFO['userinfo']);
391
392        case MSG_MANAGERS_ONLY:
393            return $INFO['ismanager'];
394
395        case MSG_ADMINS_ONLY:
396            return $INFO['isadmin'];
397
398        default:
399            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"',
400                          E_USER_WARNING);
401            return $INFO['isadmin'];
402    }
403
404    return false;
405}
406
407/**
408 * print debug messages
409 *
410 * little function to print the content of a var
411 *
412 * @author Andreas Gohr <andi@splitbrain.org>
413 *
414 * @param string $msg
415 * @param bool $hidden
416 */
417function dbg($msg,$hidden=false){
418    if($hidden){
419        echo "<!--\n";
420        print_r($msg);
421        echo "\n-->";
422    }else{
423        echo '<pre class="dbg">';
424        echo hsc(print_r($msg,true));
425        echo '</pre>';
426    }
427}
428
429/**
430 * Print info to a log file
431 *
432 * @author Andreas Gohr <andi@splitbrain.org>
433 *
434 * @param string $msg
435 * @param string $header
436 */
437function dbglog($msg,$header=''){
438    global $conf;
439    /* @var Input $INPUT */
440    global $INPUT;
441
442    // The debug log isn't automatically cleaned thus only write it when
443    // debugging has been enabled by the user.
444    if($conf['allowdebug'] !== 1) return;
445    if(is_object($msg) || is_array($msg)){
446        $msg = print_r($msg,true);
447    }
448
449    if($header) $msg = "$header\n$msg";
450
451    $file = $conf['cachedir'].'/debug.log';
452    $fh = fopen($file,'a');
453    if($fh){
454        fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n");
455        fclose($fh);
456    }
457}
458
459/**
460 * Log accesses to deprecated fucntions to the debug log
461 *
462 * @param string $alternative The function or method that should be used instead
463 * @triggers INFO_DEPRECATION_LOG
464 */
465function dbg_deprecated($alternative = '') {
466    \dokuwiki\Debug\DebugHelper::dbgDeprecatedFunction($alternative, 2);
467}
468
469/**
470 * Print a reversed, prettyprinted backtrace
471 *
472 * @author Gary Owen <gary_owen@bigfoot.com>
473 */
474function dbg_backtrace(){
475    // Get backtrace
476    $backtrace = debug_backtrace();
477
478    // Unset call to debug_print_backtrace
479    array_shift($backtrace);
480
481    // Iterate backtrace
482    $calls = array();
483    $depth = count($backtrace) - 1;
484    foreach ($backtrace as $i => $call) {
485        $location = $call['file'] . ':' . $call['line'];
486        $function = (isset($call['class'])) ?
487            $call['class'] . $call['type'] . $call['function'] : $call['function'];
488
489        $params = array();
490        if (isset($call['args'])){
491            foreach($call['args'] as $arg){
492                if(is_object($arg)){
493                    $params[] = '[Object '.get_class($arg).']';
494                }elseif(is_array($arg)){
495                    $params[] = '[Array]';
496                }elseif(is_null($arg)){
497                    $params[] = '[NULL]';
498                }else{
499                    $params[] = (string) '"'.$arg.'"';
500                }
501            }
502        }
503        $params = implode(', ',$params);
504
505        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
506                $function,
507                str_replace("\n", '\n', $params),
508                $location);
509    }
510    ksort($calls);
511
512    return implode("\n", $calls);
513}
514
515/**
516 * Remove all data from an array where the key seems to point to sensitive data
517 *
518 * This is used to remove passwords, mail addresses and similar data from the
519 * debug output
520 *
521 * @author Andreas Gohr <andi@splitbrain.org>
522 *
523 * @param array $data
524 */
525function debug_guard(&$data){
526    foreach($data as $key => $value){
527        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
528            $data[$key] = '***';
529            continue;
530        }
531        if(is_array($value)) debug_guard($data[$key]);
532    }
533}
534