xref: /dokuwiki/inc/infoutils.php (revision dccd6b2bba7367e4d1d2d7aa84c9f9d15584b593)
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 */
824870174SAndreas Gohruse dokuwiki\Extension\Event;
924870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
1024870174SAndreas Gohruse dokuwiki\Debug\DebugHelper;
115a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
1231667ec6SAndreas Gohruse dokuwiki\Logger;
13198564abSMichael Große
146c5e3c5eSPhyif(!defined('DOKU_MESSAGEURL')){
156c5e3c5eSPhy    if(in_array('ssl', stream_get_transports())) {
166c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'https://update.dokuwiki.org/check/');
176c5e3c5eSPhy    }else{
186c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'http://update.dokuwiki.org/check/');
196c5e3c5eSPhy    }
206c5e3c5eSPhy}
21c29dc6e4SAndreas Gohr
22c29dc6e4SAndreas Gohr/**
23c29dc6e4SAndreas Gohr * Check for new messages from upstream
24c29dc6e4SAndreas Gohr *
25c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
26c29dc6e4SAndreas Gohr */
27d868eb89SAndreas Gohrfunction checkUpdateMessages()
28d868eb89SAndreas Gohr{
29c29dc6e4SAndreas Gohr    global $conf;
30c29dc6e4SAndreas Gohr    global $INFO;
31ef362bb8SAnika Henke    global $updateVersion;
32c29dc6e4SAndreas Gohr    if(!$conf['updatecheck']) return;
33f8cc712eSAndreas Gohr    if($conf['useacl'] && !$INFO['ismanager']) return;
34c29dc6e4SAndreas Gohr
3537b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
36c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
376c5e3c5eSPhy    $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https';
38c29dc6e4SAndreas Gohr
39c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
408d3d7569SAnika Henke    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
4163d9b820SAndreas Gohr        @touch($cf);
4231667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): downloading messages to ".$cf.($is_http?' (without SSL)':' (with SSL)'));
43c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
4463d9b820SAndreas Gohr        $http->timeout = 12;
4586c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL.$updateVersion);
4686c04d87SAngus Gratton        if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
4786c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
4886c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
4986c04d87SAngus Gratton            io_saveFile($cf, $resp);
508f1efc43SAndreas Gohr        } else {
5131667ec6SAndreas Gohr            Logger::debug("checkUpdateMessages(): unexpected HTTP response received", $http->error);
528f1efc43SAndreas Gohr        }
53c29dc6e4SAndreas Gohr    }else{
5431667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): messages up to date");
55c29dc6e4SAndreas Gohr    }
56c29dc6e4SAndreas Gohr
5786c04d87SAngus Gratton    $data = io_readFile($cf);
58c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
59c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n", $data);
60c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
61c29dc6e4SAndreas Gohr        if($msg) msg($msg, 2);
62c29dc6e4SAndreas Gohr    }
63c29dc6e4SAndreas Gohr}
64c29dc6e4SAndreas Gohr
65c29dc6e4SAndreas Gohr
66c29dc6e4SAndreas Gohr/**
6725c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
68c29dc6e4SAndreas Gohr *
69c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
70c29dc6e4SAndreas Gohr */
71d868eb89SAndreas Gohrfunction getVersionData()
72d868eb89SAndreas Gohr{
7324870174SAndreas Gohr    $version = [];
74c29dc6e4SAndreas Gohr    //import version string
7579e79377SAndreas Gohr    if(file_exists(DOKU_INC.'VERSION')){
76c29dc6e4SAndreas Gohr        //official release
77d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
7825c07f93SAnika Henke        $version['type'] = 'Release';
795cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
805cf31920SAndreas Gohr        $version['type'] = 'Git';
8125c07f93SAnika Henke        $version['date'] = 'unknown';
82e570ed43SAndreas Gohr
83b9f5205aSDamien Regad        // First try to get date and commit hash by calling Git
84b6f8a5c6SDamien Regad        if (function_exists('shell_exec')) {
85b6f8a5c6SDamien Regad            $commitInfo = shell_exec("git log -1 --pretty=format:'%h %cd' --date=short");
86b9f5205aSDamien Regad            if ($commitInfo) {
8724870174SAndreas Gohr                [$version['sha'], $date] = explode(' ', $commitInfo);
88067b4fb9SMichael Große                $version['date'] = hsc($date);
89b9f5205aSDamien Regad                return $version;
90b9f5205aSDamien Regad            }
91b9f5205aSDamien Regad        }
92b9f5205aSDamien Regad
93f519f9dbSMichael Große        // we cannot use git on the shell -- let's do it manually!
94b9f5205aSDamien Regad        if (file_exists(DOKU_INC . '.git/HEAD')) {
95f519f9dbSMichael Große            $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD'));
96f519f9dbSMichael Große            if (strpos($headCommit, 'ref: ') === 0) {
97f519f9dbSMichael Große                // it is something like `ref: refs/heads/master`
9809bf5d22SDamien Regad                $headCommit = substr($headCommit, 5);
9909bf5d22SDamien Regad                $pathToHead = DOKU_INC . '.git/' . $headCommit;
10009bf5d22SDamien Regad                if (file_exists($pathToHead)) {
10109bf5d22SDamien Regad                    $headCommit = trim(file_get_contents($pathToHead));
10209bf5d22SDamien Regad                } else {
10309bf5d22SDamien Regad                    $packedRefs = file_get_contents(DOKU_INC . '.git/packed-refs');
10409bf5d22SDamien Regad                    if (!preg_match("~([[:xdigit:]]+) $headCommit~", $packedRefs, $matches)) {
10509bf5d22SDamien Regad                        # ref not found in pack file
10609bf5d22SDamien Regad                        return $version;
107f519f9dbSMichael Große                    }
10809bf5d22SDamien Regad                    $headCommit = $matches[1];
10909bf5d22SDamien Regad                }
11009bf5d22SDamien Regad            }
11109bf5d22SDamien Regad            // At this point $headCommit is a SHA
112b6f8a5c6SDamien Regad            $version['sha'] = $headCommit;
113b6f8a5c6SDamien Regad
11409bf5d22SDamien Regad            // Get commit date from Git object
115f519f9dbSMichael Große            $subDir = substr($headCommit, 0, 2);
116f519f9dbSMichael Große            $fileName = substr($headCommit, 2);
117be2e462dSMichael Große            $gitCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName";
118a8ac20eaSGerrit Uitslag            if (file_exists($gitCommitObject) && function_exists('zlib_decode')) {
119be2e462dSMichael Große                $commit = zlib_decode(file_get_contents($gitCommitObject));
120f519f9dbSMichael Große                $committerLine = explode("\n", $commit)[3];
121f519f9dbSMichael Große                $committerData = explode(' ', $committerLine);
122f519f9dbSMichael Große                end($committerData);
123f519f9dbSMichael Große                $ts = prev($committerData);
124f519f9dbSMichael Große                if ($ts && $date = date('Y-m-d', $ts)) {
125f519f9dbSMichael Große                    $version['date'] = $date;
126f519f9dbSMichael Große                }
1275cf31920SAndreas Gohr            }
12825c07f93SAnika Henke        }
129c29dc6e4SAndreas Gohr    }else{
1306a34de2dSAnika Henke        global $updateVersion;
1316a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
13225c07f93SAnika Henke        $version['type'] = 'snapshot?';
133c29dc6e4SAndreas Gohr    }
1345cf31920SAndreas Gohr    return $version;
135c29dc6e4SAndreas Gohr}
136c29dc6e4SAndreas Gohr
137c29dc6e4SAndreas Gohr/**
13825c07f93SAnika Henke * Return DokuWiki's version (as a string)
13925c07f93SAnika Henke *
14025c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
14125c07f93SAnika Henke */
142d868eb89SAndreas Gohrfunction getVersion()
143d868eb89SAndreas Gohr{
14425c07f93SAnika Henke    $version = getVersionData();
14524870174SAndreas Gohr    $sha = empty($version['sha']) ? '' : ' (' . $version['sha'] . ')';
146b6f8a5c6SDamien Regad    return $version['type'] . ' ' . $version['date'] . $sha;
14725c07f93SAnika Henke}
14825c07f93SAnika Henke
14925c07f93SAnika Henke/**
150c29dc6e4SAndreas Gohr * Run a few sanity checks
151c29dc6e4SAndreas Gohr *
152c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
153c29dc6e4SAndreas Gohr */
154d868eb89SAndreas Gohrfunction check()
155d868eb89SAndreas Gohr{
156c29dc6e4SAndreas Gohr    global $conf;
157c29dc6e4SAndreas Gohr    global $INFO;
158585bf44eSChristopher Smith    /* @var Input $INPUT */
159585bf44eSChristopher Smith    global $INPUT;
160c29dc6e4SAndreas Gohr
1613f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']) {
162c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(), 1);
163c49393f5SAndreas Gohr        if(version_compare(phpversion(), '7.4.0', '<')){
164c49393f5SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 7.4+ needed)', -1);
165c29dc6e4SAndreas Gohr        }else{
166c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(), 1);
167c29dc6e4SAndreas Gohr        }
16824870174SAndreas Gohr    } elseif (version_compare(phpversion(), '7.4.0', '<')) {
16908d1a8dfSAndreas Gohr        msg('Your PHP version is too old', -1);
17008d1a8dfSAndreas Gohr    }
171c6e971ddSAndreas Gohr
17224870174SAndreas Gohr    $mem = php_to_byte(ini_get('memory_limit'));
17373038c47SAndreas Gohr    if($mem){
174eb2e46caSAndreas Gohr        if ($mem === -1) {
1758f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
17624870174SAndreas Gohr        } elseif ($mem < 16_777_216) {
1772b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
1782b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
17924870174SAndreas Gohr        } elseif ($mem < 20_971_520) {
1802b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
18164159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
18224870174SAndreas Gohr        } elseif ($mem < 33_554_432) {
1832b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
18464159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
18573038c47SAndreas Gohr        } else {
186c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
18773038c47SAndreas Gohr        }
18873038c47SAndreas Gohr    }
18973038c47SAndreas Gohr
190c29dc6e4SAndreas Gohr    if (is_writable($conf['changelog'])) {
191c29dc6e4SAndreas Gohr        msg('Changelog is writable', 1);
19224870174SAndreas Gohr    } elseif (file_exists($conf['changelog'])) {
193c29dc6e4SAndreas Gohr        msg('Changelog is not writable', -1);
194c29dc6e4SAndreas Gohr    }
195c29dc6e4SAndreas Gohr
19679e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1972cdbda06SAnika Henke        msg('Old changelog exists', 0);
198c29dc6e4SAndreas Gohr    }
199c29dc6e4SAndreas Gohr
20079e79377SAndreas Gohr    if (file_exists($conf['changelog'].'_failed')) {
2012cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
20279e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'].'_importing')) {
203c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
20479e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'].'_import_ok')) {
2052cdbda06SAnika Henke        msg('Old changelog imported', 1);
206c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
2072cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
208c29dc6e4SAndreas Gohr        }
209c29dc6e4SAndreas Gohr    }
210c29dc6e4SAndreas Gohr
2114c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
2124c7ecf15SGuy Brand        msg('conf directory is writable', 1);
2134c7ecf15SGuy Brand    }else{
2144c7ecf15SGuy Brand        msg('conf directory is not writable', -1);
2154c7ecf15SGuy Brand    }
2164c7ecf15SGuy Brand
2170d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
218defb7d57SAnika Henke        global $config_cascade;
219defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
220c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable', 1);
221c29dc6e4SAndreas Gohr        }else{
222c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable', 0);
223c29dc6e4SAndreas Gohr        }
2240d487d8fSAndreas Gohr    }
225c29dc6e4SAndreas Gohr
226c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
227c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
228c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used', 0);
229c29dc6e4SAndreas Gohr        }else{
230c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used', 1);
2314222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
2324222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled', -1);
2334222b898SAndreas Gohr            }
234c29dc6e4SAndreas Gohr        }
235c29dc6e4SAndreas Gohr    }else{
236c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used', 0);
237c29dc6e4SAndreas Gohr    }
238c29dc6e4SAndreas Gohr
2393161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
240a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
241a731ed1dSAndreas Gohr    }
2423161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
243a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
244a731ed1dSAndreas Gohr    }
245a731ed1dSAndreas Gohr
246e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
247e5ab313fSAndreas Gohr    if(!$loc){
248e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this', -1);
249e5ab313fSAndreas Gohr    }elseif(stripos($loc, 'utf') === false){
25064159a61SAndreas Gohr        msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale,
25164159a61SAndreas Gohr             you should fix this if you encounter problems.', 0);
252e5ab313fSAndreas Gohr    }else{
253e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
254e5ab313fSAndreas Gohr    }
255e5ab313fSAndreas Gohr
256c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
257c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0', -1);
258c29dc6e4SAndreas Gohr    }else{
259c29dc6e4SAndreas Gohr        msg('Debugging support is disabled', 1);
260c29dc6e4SAndreas Gohr    }
261c29dc6e4SAndreas Gohr
262a4231b8cSfiwswe    if(!empty($INFO['userinfo']['name'])){
263585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')', 0);
2648368419bSSyntaxseed        msg('You are part of the groups '.implode(', ', $INFO['userinfo']['grps']), 0);
2653d3c095dSMike Frysinger    }else{
2663d3c095dSMike Frysinger        msg('You are currently not logged in', 0);
2673d3c095dSMike Frysinger    }
2683d3c095dSMike Frysinger
269c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'], 0);
270c29dc6e4SAndreas Gohr
271322ad074SAndreas Gohr    if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) {
272322ad074SAndreas Gohr        msg('The current page is writable by the webserver', 1);
273322ad074SAndreas Gohr    } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) {
274322ad074SAndreas Gohr        msg('The current page can be created by the webserver', 1);
275c29dc6e4SAndreas Gohr    } else {
276322ad074SAndreas Gohr        msg('The current page is not writable by the webserver', -1);
277c29dc6e4SAndreas Gohr    }
278c29dc6e4SAndreas Gohr
279c29dc6e4SAndreas Gohr    if ($INFO['writable']) {
280322ad074SAndreas Gohr        msg('The current page is writable by you', 1);
281c29dc6e4SAndreas Gohr    } else {
282322ad074SAndreas Gohr        msg('The current page is not writable by you', -1);
283c29dc6e4SAndreas Gohr    }
2843b1dfc83SAndreas Gohr
28526f7dbf5SMichael Hamann    // Check for corrupted search index
28626f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
28726f7dbf5SMichael Hamann    $index_corrupted = false;
28826f7dbf5SMichael Hamann    foreach ($lengths as $length) {
28924870174SAndreas Gohr        if (count(idx_getIndex('w', $length)) !== count(idx_getIndex('i', $length))) {
29026f7dbf5SMichael Hamann            $index_corrupted = true;
29126f7dbf5SMichael Hamann            break;
29226f7dbf5SMichael Hamann        }
29326f7dbf5SMichael Hamann    }
29426f7dbf5SMichael Hamann
29526f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
29624870174SAndreas Gohr        if (count(idx_getIndex($index.'_w', '')) !== count(idx_getIndex($index.'_i', ''))) {
29726f7dbf5SMichael Hamann            $index_corrupted = true;
29826f7dbf5SMichael Hamann            break;
29926f7dbf5SMichael Hamann        }
30026f7dbf5SMichael Hamann    }
30126f7dbf5SMichael Hamann
302d6c7b502SAndreas Gohr    if($index_corrupted) {
303d6c7b502SAndreas Gohr        msg(
304d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
3053d94d9edSMichael Hamann                probably needs to be rebuilt. See
30626f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
307*dccd6b2bSAndreas Gohr                for ways to rebuild the search index.',
308*dccd6b2bSAndreas Gohr            -1
309d6c7b502SAndreas Gohr        );
310d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
3113d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
312d6c7b502SAndreas Gohr    } else {
313d6c7b502SAndreas Gohr        msg(
314d6c7b502SAndreas Gohr            'The search index is empty. See
31526f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
3163d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
317d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
318d6c7b502SAndreas Gohr        );
319d6c7b502SAndreas Gohr    }
320d6c7b502SAndreas Gohr
321d6c7b502SAndreas Gohr    // rough time check
322d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
323d6c7b502SAndreas Gohr    $http->max_redirect = 0;
324d6c7b502SAndreas Gohr    $http->timeout = 3;
325d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
326d6c7b502SAndreas Gohr    $now = time();
327d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
328d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
329d6c7b502SAndreas Gohr        $diff = $time - $now;
330d6c7b502SAndreas Gohr
331d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
332d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
333d6c7b502SAndreas Gohr        } else {
33464159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
33564159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
336d6c7b502SAndreas Gohr        }
337d6c7b502SAndreas Gohr    }
338d6c7b502SAndreas Gohr
339c29dc6e4SAndreas Gohr}
340c29dc6e4SAndreas Gohr
341c29dc6e4SAndreas Gohr/**
34246028c4cSAndreas Gohr * Display a message to the user
343c29dc6e4SAndreas Gohr *
344c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
345c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
346c29dc6e4SAndreas Gohr * using html_msgarea()
347c29dc6e4SAndreas Gohr *
3480e20480fSPhy * Triggers INFOUTIL_MSG_SHOW
3490e20480fSPhy *
35046028c4cSAndreas Gohr * @see    html_msgarea()
3516164d900SAndreas Gohr * @param string $message
3526164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3536164d900SAndreas Gohr * @param string $line  line number
3546164d900SAndreas Gohr * @param string $file  file number
3556164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3566164d900SAndreas Gohr */
357d868eb89SAndreas Gohrfunction msg($message, $lvl = 0, $line = '', $file = '', $allow = MSG_PUBLIC)
358d868eb89SAndreas Gohr{
359cc58224cSMichael Hamann    global $MSG, $MSG_shown;
360556e996eSAndreas Gohr    static $errors = [
361556e996eSAndreas Gohr        -1 => 'error',
362556e996eSAndreas Gohr        0 => 'info',
363556e996eSAndreas Gohr        1 => 'success',
364556e996eSAndreas Gohr        2 => 'notify',
365556e996eSAndreas Gohr    ];
366c29dc6e4SAndreas Gohr
367556e996eSAndreas Gohr    $msgdata = [
3680e20480fSPhy        'msg' => $message,
369556e996eSAndreas Gohr        'lvl' => $errors[$lvl],
3700e20480fSPhy        'allow' => $allow,
3710e20480fSPhy        'line' => $line,
3720e20480fSPhy        'file' => $file,
373556e996eSAndreas Gohr    ];
3740e20480fSPhy
37524870174SAndreas Gohr    $evt = new Event('INFOUTIL_MSG_SHOW', $msgdata);
3760e20480fSPhy    if ($evt->advise_before()) {
3770e20480fSPhy        /* Show msg normally - event could suppress message show */
3780e20480fSPhy        if($msgdata['line'] || $msgdata['file']) {
37924870174SAndreas Gohr            $basename = PhpString::basename($msgdata['file']);
3800e20480fSPhy            $msgdata['msg'] .=' ['.$basename.':'.$msgdata['line'].']';
3810e20480fSPhy        }
382c29dc6e4SAndreas Gohr
38324870174SAndreas Gohr        if(!isset($MSG)) $MSG = [];
3840e20480fSPhy        $MSG[] = $msgdata;
385cc58224cSMichael Hamann        if(isset($MSG_shown) || headers_sent()){
386c29dc6e4SAndreas Gohr            if(function_exists('html_msgarea')){
387c29dc6e4SAndreas Gohr                html_msgarea();
388c29dc6e4SAndreas Gohr            }else{
3890e20480fSPhy                print "ERROR(".$msgdata['lvl'].") ".$msgdata['msg']."\n";
390c29dc6e4SAndreas Gohr            }
39169266de5SDominik Eckelmann            unset($GLOBALS['MSG']);
392c29dc6e4SAndreas Gohr        }
393c29dc6e4SAndreas Gohr    }
3940e20480fSPhy    $evt->advise_after();
3950e20480fSPhy    unset($evt);
3960e20480fSPhy}
397f755f9abSChristopher Smith/**
398f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
399f755f9abSChristopher Smith * in the $msg data structure
400f755f9abSChristopher Smith *
401f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
402f755f9abSChristopher Smith *                         msg   => string, the message
403f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
404f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
405f755f9abSChristopher Smith *                                       see MSG_* constants
4066164d900SAndreas Gohr * @return bool
407f755f9abSChristopher Smith */
408d868eb89SAndreas Gohrfunction info_msg_allowed($msg)
409d868eb89SAndreas Gohr{
410d3bae478SChristopher Smith    global $INFO, $auth;
411d3bae478SChristopher Smith
412d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
413f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
414d3bae478SChristopher Smith
415d3bae478SChristopher Smith    // restricted msg, but no authentication
416d3bae478SChristopher Smith    if (empty($auth)) return false;
417d3bae478SChristopher Smith
418f755f9abSChristopher Smith    switch ($msg['allow']){
419d3bae478SChristopher Smith        case MSG_USERS_ONLY:
420d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
421d3bae478SChristopher Smith
422d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
423d3bae478SChristopher Smith            return $INFO['ismanager'];
424d3bae478SChristopher Smith
425d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
426d3bae478SChristopher Smith            return $INFO['isadmin'];
427d3bae478SChristopher Smith
428d3bae478SChristopher Smith        default:
429*dccd6b2bSAndreas Gohr            trigger_error(
430*dccd6b2bSAndreas Gohr                'invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"',
431*dccd6b2bSAndreas Gohr                E_USER_WARNING
432*dccd6b2bSAndreas Gohr            );
433d3bae478SChristopher Smith            return $INFO['isadmin'];
434d3bae478SChristopher Smith    }
435d3bae478SChristopher Smith}
436d3bae478SChristopher Smith
437c29dc6e4SAndreas Gohr/**
438c29dc6e4SAndreas Gohr * print debug messages
439c29dc6e4SAndreas Gohr *
440c29dc6e4SAndreas Gohr * little function to print the content of a var
441c29dc6e4SAndreas Gohr *
442c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
443f50a239bSTakamura *
444f50a239bSTakamura * @param string $msg
445f50a239bSTakamura * @param bool $hidden
446c29dc6e4SAndreas Gohr */
447d868eb89SAndreas Gohrfunction dbg($msg, $hidden = false)
448d868eb89SAndreas Gohr{
44913493794SAndreas Gohr    if($hidden){
45013493794SAndreas Gohr        echo "<!--\n";
451c29dc6e4SAndreas Gohr        print_r($msg);
45213493794SAndreas Gohr        echo "\n-->";
45313493794SAndreas Gohr    }else{
45413493794SAndreas Gohr        echo '<pre class="dbg">';
45513493794SAndreas Gohr        echo hsc(print_r($msg, true));
45613493794SAndreas Gohr        echo '</pre>';
45713493794SAndreas Gohr    }
458c29dc6e4SAndreas Gohr}
459c29dc6e4SAndreas Gohr
460c29dc6e4SAndreas Gohr/**
461cad4fbf6SAndreas Gohr * Print info to debug log file
462c29dc6e4SAndreas Gohr *
463c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4640ecde6ceSAndreas Gohr * @deprecated 2020-08-13
465f50a239bSTakamura * @param string $msg
466f50a239bSTakamura * @param string $header
467c29dc6e4SAndreas Gohr */
468d868eb89SAndreas Gohrfunction dbglog($msg, $header = '')
469d868eb89SAndreas Gohr{
4700ecde6ceSAndreas Gohr    dbg_deprecated('\\dokuwiki\\Logger');
471585bf44eSChristopher Smith
4720ecde6ceSAndreas Gohr    // was the msg as single line string? use it as header
473cad4fbf6SAndreas Gohr    if($header === '' && is_string($msg) && strpos($msg, "\n") === false) {
4740ecde6ceSAndreas Gohr        $header = $msg;
4750ecde6ceSAndreas Gohr        $msg = '';
476c7408a63SAndreas Gohr    }
477c7408a63SAndreas Gohr
47831667ec6SAndreas Gohr    Logger::getInstance(Logger::LOG_DEBUG)->log(
479*dccd6b2bSAndreas Gohr        $header,
480*dccd6b2bSAndreas Gohr        $msg
4810ecde6ceSAndreas Gohr    );
482c29dc6e4SAndreas Gohr}
483c29dc6e4SAndreas Gohr
484db09e31eSAndreas Gohr/**
4851419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4861419a485SAndreas Gohr *
4871419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
48885331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
4891419a485SAndreas Gohr */
490d868eb89SAndreas Gohrfunction dbg_deprecated($alternative = '')
491d868eb89SAndreas Gohr{
49224870174SAndreas Gohr    DebugHelper::dbgDeprecatedFunction($alternative, 2);
49344455016SAndreas Gohr}
4941419a485SAndreas Gohr
4951419a485SAndreas Gohr/**
496db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
497db09e31eSAndreas Gohr *
498db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
499db09e31eSAndreas Gohr */
500d868eb89SAndreas Gohrfunction dbg_backtrace()
501d868eb89SAndreas Gohr{
502db09e31eSAndreas Gohr    // Get backtrace
503db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
504db09e31eSAndreas Gohr
505db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
506db09e31eSAndreas Gohr    array_shift($backtrace);
507db09e31eSAndreas Gohr
508db09e31eSAndreas Gohr    // Iterate backtrace
50924870174SAndreas Gohr    $calls = [];
510db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
511db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
512db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
513db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
514db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
515db09e31eSAndreas Gohr
51624870174SAndreas Gohr        $params = [];
517db09e31eSAndreas Gohr        if (isset($call['args'])){
5188259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
5198259f1aaSAndreas Gohr                if(is_object($arg)){
5208259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
5218259f1aaSAndreas Gohr                }elseif(is_array($arg)){
5228259f1aaSAndreas Gohr                    $params[] = '[Array]';
5238259f1aaSAndreas Gohr                }elseif(is_null($arg)){
52459bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
5258259f1aaSAndreas Gohr                }else{
52624870174SAndreas Gohr                    $params[] = '"'.$arg.'"';
527db09e31eSAndreas Gohr                }
5288259f1aaSAndreas Gohr            }
5298259f1aaSAndreas Gohr        }
5308259f1aaSAndreas Gohr        $params = implode(', ', $params);
531db09e31eSAndreas Gohr
532*dccd6b2bSAndreas Gohr        $calls[$depth - $i] = sprintf(
533*dccd6b2bSAndreas Gohr            '%s(%s) called at %s',
534db09e31eSAndreas Gohr            $function,
535db09e31eSAndreas Gohr            str_replace("\n", '\n', $params),
536*dccd6b2bSAndreas Gohr            $location
537*dccd6b2bSAndreas Gohr        );
538db09e31eSAndreas Gohr    }
539db09e31eSAndreas Gohr    ksort($calls);
540db09e31eSAndreas Gohr
541db09e31eSAndreas Gohr    return implode("\n", $calls);
542db09e31eSAndreas Gohr}
543db09e31eSAndreas Gohr
54424297a69SAndreas Gohr/**
54524297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
54624297a69SAndreas Gohr *
54724297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
54824297a69SAndreas Gohr * debug output
54924297a69SAndreas Gohr *
55024297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
551f50a239bSTakamura *
552f50a239bSTakamura * @param array $data
55324297a69SAndreas Gohr */
554d868eb89SAndreas Gohrfunction debug_guard(&$data)
555d868eb89SAndreas Gohr{
55624297a69SAndreas Gohr    foreach($data as $key => $value){
55724297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i', $key)){
55824297a69SAndreas Gohr            $data[$key] = '***';
55924297a69SAndreas Gohr            continue;
56024297a69SAndreas Gohr        }
56124297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
56224297a69SAndreas Gohr    }
56324297a69SAndreas Gohr}
564