xref: /dokuwiki/inc/infoutils.php (revision cad4fbf6e21bd4a053f1f42b9e40c74f1cfaeab6)
1c29dc6e4SAndreas Gohr<?php
2c29dc6e4SAndreas Gohr/**
3c29dc6e4SAndreas Gohr * Information and debugging functions
4c29dc6e4SAndreas Gohr *
5c29dc6e4SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6c29dc6e4SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
7c29dc6e4SAndreas Gohr */
8198564abSMichael Große
95a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
1031667ec6SAndreas Gohruse dokuwiki\Logger;
11198564abSMichael Große
126c5e3c5eSPhyif(!defined('DOKU_MESSAGEURL')){
136c5e3c5eSPhy    if(in_array('ssl', stream_get_transports())) {
146c5e3c5eSPhy        define('DOKU_MESSAGEURL','https://update.dokuwiki.org/check/');
156c5e3c5eSPhy    }else{
166c5e3c5eSPhy        define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
176c5e3c5eSPhy    }
186c5e3c5eSPhy}
19c29dc6e4SAndreas Gohr
20c29dc6e4SAndreas Gohr/**
21c29dc6e4SAndreas Gohr * Check for new messages from upstream
22c29dc6e4SAndreas Gohr *
23c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
24c29dc6e4SAndreas Gohr */
25c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){
26c29dc6e4SAndreas Gohr    global $conf;
27c29dc6e4SAndreas Gohr    global $INFO;
28ef362bb8SAnika Henke    global $updateVersion;
29c29dc6e4SAndreas Gohr    if(!$conf['updatecheck']) return;
30f8cc712eSAndreas Gohr    if($conf['useacl'] && !$INFO['ismanager']) return;
31c29dc6e4SAndreas Gohr
3237b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
33c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
346c5e3c5eSPhy    $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https';
35c29dc6e4SAndreas Gohr
36c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
378d3d7569SAnika Henke    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
3863d9b820SAndreas Gohr        @touch($cf);
3931667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): downloading messages to ".$cf.($is_http?' (without SSL)':' (with SSL)'));
40c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
4163d9b820SAndreas Gohr        $http->timeout = 12;
4286c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL.$updateVersion);
4386c04d87SAngus Gratton        if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
4486c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
4586c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
4686c04d87SAngus Gratton            io_saveFile($cf,$resp);
478f1efc43SAndreas Gohr        } else {
4831667ec6SAndreas Gohr            Logger::debug("checkUpdateMessages(): unexpected HTTP response received", $http->error);
498f1efc43SAndreas Gohr        }
50c29dc6e4SAndreas Gohr    }else{
5131667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): messages up to date");
52c29dc6e4SAndreas Gohr    }
53c29dc6e4SAndreas Gohr
5486c04d87SAngus Gratton    $data = io_readFile($cf);
55c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
56c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n",$data);
57c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
58c29dc6e4SAndreas Gohr        if($msg) msg($msg,2);
59c29dc6e4SAndreas Gohr    }
60c29dc6e4SAndreas Gohr}
61c29dc6e4SAndreas Gohr
62c29dc6e4SAndreas Gohr
63c29dc6e4SAndreas Gohr/**
6425c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
65c29dc6e4SAndreas Gohr *
66c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
67c29dc6e4SAndreas Gohr */
6825c07f93SAnika Henkefunction getVersionData(){
6925c07f93SAnika Henke    $version = array();
70c29dc6e4SAndreas Gohr    //import version string
7179e79377SAndreas Gohr    if(file_exists(DOKU_INC.'VERSION')){
72c29dc6e4SAndreas Gohr        //official release
73d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
7425c07f93SAnika Henke        $version['type'] = 'Release';
755cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
765cf31920SAndreas Gohr        $version['type'] = 'Git';
7725c07f93SAnika Henke        $version['date'] = 'unknown';
78e570ed43SAndreas Gohr
795cf31920SAndreas Gohr        $inventory = DOKU_INC.'.git/logs/HEAD';
805cf31920SAndreas Gohr        if(is_file($inventory)){
81e570ed43SAndreas Gohr            $sz   = filesize($inventory);
825cf31920SAndreas Gohr            $seek = max(0,$sz-2000); // read from back of the file
83e570ed43SAndreas Gohr            $fh   = fopen($inventory,'rb');
84c29dc6e4SAndreas Gohr            fseek($fh,$seek);
85c29dc6e4SAndreas Gohr            $chunk = fread($fh,2000);
86c29dc6e4SAndreas Gohr            fclose($fh);
875cf31920SAndreas Gohr            $chunk = trim($chunk);
88b838050eSPiyush Mishra            $chunk = @array_pop(explode("\n",$chunk));   //last log line
89b838050eSPiyush Mishra            $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg
905cf31920SAndreas Gohr            $chunk = explode(" ",$chunk);
915cf31920SAndreas Gohr            array_pop($chunk); //strip timezone
925cf31920SAndreas Gohr            $date = date('Y-m-d',array_pop($chunk));
935cf31920SAndreas Gohr            if($date) $version['date'] = $date;
945cf31920SAndreas Gohr        }
95c29dc6e4SAndreas Gohr    }else{
966a34de2dSAnika Henke        global $updateVersion;
976a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
9825c07f93SAnika Henke        $version['type'] = 'snapshot?';
99c29dc6e4SAndreas Gohr    }
1005cf31920SAndreas Gohr    return $version;
101c29dc6e4SAndreas Gohr}
102c29dc6e4SAndreas Gohr
103c29dc6e4SAndreas Gohr/**
10425c07f93SAnika Henke * Return DokuWiki's version (as a string)
10525c07f93SAnika Henke *
10625c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
10725c07f93SAnika Henke */
10825c07f93SAnika Henkefunction getVersion(){
10925c07f93SAnika Henke    $version = getVersionData();
11025c07f93SAnika Henke    return $version['type'].' '.$version['date'];
11125c07f93SAnika Henke}
11225c07f93SAnika Henke
11325c07f93SAnika Henke/**
114c29dc6e4SAndreas Gohr * Run a few sanity checks
115c29dc6e4SAndreas Gohr *
116c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
117c29dc6e4SAndreas Gohr */
118c29dc6e4SAndreas Gohrfunction check(){
119c29dc6e4SAndreas Gohr    global $conf;
120c29dc6e4SAndreas Gohr    global $INFO;
121585bf44eSChristopher Smith    /* @var Input $INPUT */
122585bf44eSChristopher Smith    global $INPUT;
123c29dc6e4SAndreas Gohr
1243f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
125c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
126c29dc6e4SAndreas Gohr
1273f874cb3SAndreas Gohr        if(version_compare(phpversion(),'7.2.0','<')){
1283f874cb3SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 7.2+ needed)',-1);
129c29dc6e4SAndreas Gohr        }else{
130c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
131c29dc6e4SAndreas Gohr        }
13208d1a8dfSAndreas Gohr    } else {
1333f874cb3SAndreas Gohr        if(version_compare(phpversion(),'7.2.0','<')){
13408d1a8dfSAndreas Gohr            msg('Your PHP version is too old',-1);
13508d1a8dfSAndreas Gohr        }
13608d1a8dfSAndreas Gohr    }
137c6e971ddSAndreas Gohr
138c6e971ddSAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
13973038c47SAndreas Gohr    if($mem){
140eb2e46caSAndreas Gohr        if ($mem === -1) {
1418f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
1428f8499faSpeterfromearth        } else if ($mem < 16777216) {
1432b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
1442b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
14573038c47SAndreas Gohr        } else if ($mem < 20971520) {
1462b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
14764159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
14873038c47SAndreas Gohr        } else if ($mem < 33554432) {
1492b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
15064159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
15173038c47SAndreas Gohr        } else {
152c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
15373038c47SAndreas Gohr        }
15473038c47SAndreas Gohr    }
15573038c47SAndreas Gohr
156c29dc6e4SAndreas Gohr    if(is_writable($conf['changelog'])){
157c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
158c29dc6e4SAndreas Gohr    }else{
15979e79377SAndreas Gohr        if (file_exists($conf['changelog'])) {
160c29dc6e4SAndreas Gohr            msg('Changelog is not writable',-1);
161c29dc6e4SAndreas Gohr        }
162c29dc6e4SAndreas Gohr    }
163c29dc6e4SAndreas Gohr
16479e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1652cdbda06SAnika Henke        msg('Old changelog exists', 0);
166c29dc6e4SAndreas Gohr    }
167c29dc6e4SAndreas Gohr
16879e79377SAndreas Gohr    if (file_exists($conf['changelog'].'_failed')) {
1692cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
17079e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_importing')) {
171c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
17279e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_import_ok')) {
1732cdbda06SAnika Henke        msg('Old changelog imported', 1);
174c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
1752cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
176c29dc6e4SAndreas Gohr        }
177c29dc6e4SAndreas Gohr    }
178c29dc6e4SAndreas Gohr
1794c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
1804c7ecf15SGuy Brand        msg('conf directory is writable',1);
1814c7ecf15SGuy Brand    }else{
1824c7ecf15SGuy Brand        msg('conf directory is not writable',-1);
1834c7ecf15SGuy Brand    }
1844c7ecf15SGuy Brand
1850d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
186defb7d57SAnika Henke        global $config_cascade;
187defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
188c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
189c29dc6e4SAndreas Gohr        }else{
190c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
191c29dc6e4SAndreas Gohr        }
1920d487d8fSAndreas Gohr    }
193c29dc6e4SAndreas Gohr
194c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
195c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
196c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
197c29dc6e4SAndreas Gohr        }else{
198c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
1994222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
2004222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
2014222b898SAndreas Gohr            }
202c29dc6e4SAndreas Gohr        }
203c29dc6e4SAndreas Gohr    }else{
204c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
205c29dc6e4SAndreas Gohr    }
206c29dc6e4SAndreas Gohr
2073161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
208a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
209a731ed1dSAndreas Gohr    }
2103161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
211a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
212a731ed1dSAndreas Gohr    }
213a731ed1dSAndreas Gohr
214e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
215e5ab313fSAndreas Gohr    if(!$loc){
216e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this',-1);
217e5ab313fSAndreas Gohr    }elseif(stripos($loc,'utf') === false){
21864159a61SAndreas Gohr        msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale,
21964159a61SAndreas Gohr             you should fix this if you encounter problems.',0);
220e5ab313fSAndreas Gohr    }else{
221e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
222e5ab313fSAndreas Gohr    }
223e5ab313fSAndreas Gohr
224c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
225c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
226c29dc6e4SAndreas Gohr    }else{
227c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
228c29dc6e4SAndreas Gohr    }
229c29dc6e4SAndreas Gohr
2303d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
231585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
232c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2333d3c095dSMike Frysinger    }else{
2343d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2353d3c095dSMike Frysinger    }
2363d3c095dSMike Frysinger
237c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
238c29dc6e4SAndreas Gohr
239322ad074SAndreas Gohr    if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) {
240322ad074SAndreas Gohr        msg('The current page is writable by the webserver', 1);
241322ad074SAndreas Gohr    } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) {
242322ad074SAndreas Gohr        msg('The current page can be created by the webserver', 1);
243c29dc6e4SAndreas Gohr    } else {
244322ad074SAndreas Gohr        msg('The current page is not writable by the webserver', -1);
245c29dc6e4SAndreas Gohr    }
246c29dc6e4SAndreas Gohr
247c29dc6e4SAndreas Gohr    if ($INFO['writable']) {
248322ad074SAndreas Gohr        msg('The current page is writable by you', 1);
249c29dc6e4SAndreas Gohr    } else {
250322ad074SAndreas Gohr        msg('The current page is not writable by you', -1);
251c29dc6e4SAndreas Gohr    }
2523b1dfc83SAndreas Gohr
25326f7dbf5SMichael Hamann    // Check for corrupted search index
25426f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
25526f7dbf5SMichael Hamann    $index_corrupted = false;
25626f7dbf5SMichael Hamann    foreach ($lengths as $length) {
25726f7dbf5SMichael Hamann        if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
25826f7dbf5SMichael Hamann            $index_corrupted = true;
25926f7dbf5SMichael Hamann            break;
26026f7dbf5SMichael Hamann        }
26126f7dbf5SMichael Hamann    }
26226f7dbf5SMichael Hamann
26326f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
26426f7dbf5SMichael Hamann        if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
26526f7dbf5SMichael Hamann            $index_corrupted = true;
26626f7dbf5SMichael Hamann            break;
26726f7dbf5SMichael Hamann        }
26826f7dbf5SMichael Hamann    }
26926f7dbf5SMichael Hamann
270d6c7b502SAndreas Gohr    if($index_corrupted) {
271d6c7b502SAndreas Gohr        msg(
272d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
2733d94d9edSMichael Hamann                probably needs to be rebuilt. See
27426f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
275d6c7b502SAndreas Gohr                for ways to rebuild the search index.', -1
276d6c7b502SAndreas Gohr        );
277d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
2783d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
279d6c7b502SAndreas Gohr    } else {
280d6c7b502SAndreas Gohr        msg(
281d6c7b502SAndreas Gohr            'The search index is empty. See
28226f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2833d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
284d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
285d6c7b502SAndreas Gohr        );
286d6c7b502SAndreas Gohr    }
287d6c7b502SAndreas Gohr
288d6c7b502SAndreas Gohr    // rough time check
289d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
290d6c7b502SAndreas Gohr    $http->max_redirect = 0;
291d6c7b502SAndreas Gohr    $http->timeout = 3;
292d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
293d6c7b502SAndreas Gohr    $now = time();
294d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
295d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
296d6c7b502SAndreas Gohr        $diff = $time - $now;
297d6c7b502SAndreas Gohr
298d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
299d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
300d6c7b502SAndreas Gohr        } else {
30164159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
30264159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
303d6c7b502SAndreas Gohr        }
304d6c7b502SAndreas Gohr    }
305d6c7b502SAndreas Gohr
306c29dc6e4SAndreas Gohr}
307c29dc6e4SAndreas Gohr
308c29dc6e4SAndreas Gohr/**
30946028c4cSAndreas Gohr * Display a message to the user
310c29dc6e4SAndreas Gohr *
311c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
312c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
313c29dc6e4SAndreas Gohr * using html_msgarea()
314c29dc6e4SAndreas Gohr *
3150e20480fSPhy * Triggers INFOUTIL_MSG_SHOW
3160e20480fSPhy *
31746028c4cSAndreas Gohr * @see    html_msgarea()
3186164d900SAndreas Gohr * @param string $message
3196164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3206164d900SAndreas Gohr * @param string $line  line number
3216164d900SAndreas Gohr * @param string $file  file number
3226164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3236164d900SAndreas Gohr */
324f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
325cc58224cSMichael Hamann    global $MSG, $MSG_shown;
326556e996eSAndreas Gohr    static $errors = [
327556e996eSAndreas Gohr        -1 => 'error',
328556e996eSAndreas Gohr        0 => 'info',
329556e996eSAndreas Gohr        1 => 'success',
330556e996eSAndreas Gohr        2 => 'notify',
331556e996eSAndreas Gohr    ];
332c29dc6e4SAndreas Gohr
333556e996eSAndreas Gohr    $msgdata = [
3340e20480fSPhy        'msg' => $message,
335556e996eSAndreas Gohr        'lvl' => $errors[$lvl],
3360e20480fSPhy        'allow' => $allow,
3370e20480fSPhy        'line' => $line,
3380e20480fSPhy        'file' => $file,
339556e996eSAndreas Gohr    ];
3400e20480fSPhy
3410e20480fSPhy    $evt = new \dokuwiki\Extension\Event('INFOUTIL_MSG_SHOW', $msgdata);
3420e20480fSPhy    if ($evt->advise_before()) {
3430e20480fSPhy        /* Show msg normally - event could suppress message show */
3440e20480fSPhy        if($msgdata['line'] || $msgdata['file']) {
3450e20480fSPhy            $basename = \dokuwiki\Utf8\PhpString::basename($msgdata['file']);
3460e20480fSPhy            $msgdata['msg'] .=' ['.$basename.':'.$msgdata['line'].']';
3470e20480fSPhy        }
348c29dc6e4SAndreas Gohr
349c29dc6e4SAndreas Gohr        if(!isset($MSG)) $MSG = array();
3500e20480fSPhy        $MSG[] = $msgdata;
351cc58224cSMichael Hamann        if(isset($MSG_shown) || headers_sent()){
352c29dc6e4SAndreas Gohr            if(function_exists('html_msgarea')){
353c29dc6e4SAndreas Gohr                html_msgarea();
354c29dc6e4SAndreas Gohr            }else{
3550e20480fSPhy                print "ERROR(".$msgdata['lvl'].") ".$msgdata['msg']."\n";
356c29dc6e4SAndreas Gohr            }
35769266de5SDominik Eckelmann            unset($GLOBALS['MSG']);
358c29dc6e4SAndreas Gohr        }
359c29dc6e4SAndreas Gohr    }
3600e20480fSPhy    $evt->advise_after();
3610e20480fSPhy    unset($evt);
3620e20480fSPhy}
363f755f9abSChristopher Smith/**
364f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
365f755f9abSChristopher Smith * in the $msg data structure
366f755f9abSChristopher Smith *
367f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
368f755f9abSChristopher Smith *                         msg   => string, the message
369f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
370f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
371f755f9abSChristopher Smith *                                       see MSG_* constants
3726164d900SAndreas Gohr * @return bool
373f755f9abSChristopher Smith */
374f755f9abSChristopher Smithfunction info_msg_allowed($msg){
375d3bae478SChristopher Smith    global $INFO, $auth;
376d3bae478SChristopher Smith
377d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
378f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
379d3bae478SChristopher Smith
380d3bae478SChristopher Smith    // restricted msg, but no authentication
381d3bae478SChristopher Smith    if (empty($auth)) return false;
382d3bae478SChristopher Smith
383f755f9abSChristopher Smith    switch ($msg['allow']){
384d3bae478SChristopher Smith        case MSG_USERS_ONLY:
385d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
386d3bae478SChristopher Smith
387d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
388d3bae478SChristopher Smith            return $INFO['ismanager'];
389d3bae478SChristopher Smith
390d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
391d3bae478SChristopher Smith            return $INFO['isadmin'];
392d3bae478SChristopher Smith
393d3bae478SChristopher Smith        default:
39464159a61SAndreas Gohr            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"',
39564159a61SAndreas Gohr                          E_USER_WARNING);
396d3bae478SChristopher Smith            return $INFO['isadmin'];
397d3bae478SChristopher Smith    }
398d3bae478SChristopher Smith
399d3bae478SChristopher Smith    return false;
400d3bae478SChristopher Smith}
401d3bae478SChristopher Smith
402c29dc6e4SAndreas Gohr/**
403c29dc6e4SAndreas Gohr * print debug messages
404c29dc6e4SAndreas Gohr *
405c29dc6e4SAndreas Gohr * little function to print the content of a var
406c29dc6e4SAndreas Gohr *
407c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
408f50a239bSTakamura *
409f50a239bSTakamura * @param string $msg
410f50a239bSTakamura * @param bool $hidden
411c29dc6e4SAndreas Gohr */
412c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
41313493794SAndreas Gohr    if($hidden){
41413493794SAndreas Gohr        echo "<!--\n";
415c29dc6e4SAndreas Gohr        print_r($msg);
41613493794SAndreas Gohr        echo "\n-->";
41713493794SAndreas Gohr    }else{
41813493794SAndreas Gohr        echo '<pre class="dbg">';
41913493794SAndreas Gohr        echo hsc(print_r($msg,true));
42013493794SAndreas Gohr        echo '</pre>';
42113493794SAndreas Gohr    }
422c29dc6e4SAndreas Gohr}
423c29dc6e4SAndreas Gohr
424c29dc6e4SAndreas Gohr/**
425*cad4fbf6SAndreas Gohr * Print info to debug log file
426c29dc6e4SAndreas Gohr *
427c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4280ecde6ceSAndreas Gohr * @deprecated 2020-08-13
429f50a239bSTakamura * @param string $msg
430f50a239bSTakamura * @param string $header
431c29dc6e4SAndreas Gohr */
4320699d739SAndreas Gohrfunction dbglog($msg,$header=''){
4330ecde6ceSAndreas Gohr    dbg_deprecated('\\dokuwiki\\Logger');
434585bf44eSChristopher Smith
4350ecde6ceSAndreas Gohr    // was the msg as single line string? use it as header
436*cad4fbf6SAndreas Gohr    if($header === '' && is_string($msg) && strpos($msg, "\n") === false) {
4370ecde6ceSAndreas Gohr        $header = $msg;
4380ecde6ceSAndreas Gohr        $msg = '';
439c29dc6e4SAndreas Gohr    }
4400ecde6ceSAndreas Gohr
44131667ec6SAndreas Gohr    Logger::getInstance(Logger::LOG_DEBUG)->log(
4420ecde6ceSAndreas Gohr        $header, $msg
4430ecde6ceSAndreas Gohr    );
444c29dc6e4SAndreas Gohr}
445c29dc6e4SAndreas Gohr
446db09e31eSAndreas Gohr/**
4471419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4481419a485SAndreas Gohr *
4491419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
45085331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
4511419a485SAndreas Gohr */
4521419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') {
4530c5eb5e2SMichael Große    \dokuwiki\Debug\DebugHelper::dbgDeprecatedFunction($alternative, 2);
45444455016SAndreas Gohr}
4551419a485SAndreas Gohr
4561419a485SAndreas Gohr/**
457db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
458db09e31eSAndreas Gohr *
459db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
460db09e31eSAndreas Gohr */
461db09e31eSAndreas Gohrfunction dbg_backtrace(){
462db09e31eSAndreas Gohr    // Get backtrace
463db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
464db09e31eSAndreas Gohr
465db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
466db09e31eSAndreas Gohr    array_shift($backtrace);
467db09e31eSAndreas Gohr
468db09e31eSAndreas Gohr    // Iterate backtrace
469db09e31eSAndreas Gohr    $calls = array();
470db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
471db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
472db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
473db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
474db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
475db09e31eSAndreas Gohr
4768259f1aaSAndreas Gohr        $params = array();
477db09e31eSAndreas Gohr        if (isset($call['args'])){
4788259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
4798259f1aaSAndreas Gohr                if(is_object($arg)){
4808259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
4818259f1aaSAndreas Gohr                }elseif(is_array($arg)){
4828259f1aaSAndreas Gohr                    $params[] = '[Array]';
4838259f1aaSAndreas Gohr                }elseif(is_null($arg)){
48459bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
4858259f1aaSAndreas Gohr                }else{
4868259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
487db09e31eSAndreas Gohr                }
4888259f1aaSAndreas Gohr            }
4898259f1aaSAndreas Gohr        }
4908259f1aaSAndreas Gohr        $params = implode(', ',$params);
491db09e31eSAndreas Gohr
4928259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
493db09e31eSAndreas Gohr                $function,
494db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
495db09e31eSAndreas Gohr                $location);
496db09e31eSAndreas Gohr    }
497db09e31eSAndreas Gohr    ksort($calls);
498db09e31eSAndreas Gohr
499db09e31eSAndreas Gohr    return implode("\n", $calls);
500db09e31eSAndreas Gohr}
501db09e31eSAndreas Gohr
50224297a69SAndreas Gohr/**
50324297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
50424297a69SAndreas Gohr *
50524297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
50624297a69SAndreas Gohr * debug output
50724297a69SAndreas Gohr *
50824297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
509f50a239bSTakamura *
510f50a239bSTakamura * @param array $data
51124297a69SAndreas Gohr */
51224297a69SAndreas Gohrfunction debug_guard(&$data){
51324297a69SAndreas Gohr    foreach($data as $key => $value){
51424297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
51524297a69SAndreas Gohr            $data[$key] = '***';
51624297a69SAndreas Gohr            continue;
51724297a69SAndreas Gohr        }
51824297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
51924297a69SAndreas Gohr    }
52024297a69SAndreas Gohr}
521