xref: /dokuwiki/inc/infoutils.php (revision f5be2fc019d63c3f5c01aa170aea6802d3f387c2)
1c29dc6e4SAndreas Gohr<?php
2d4f83172SAndreas Gohr
3c29dc6e4SAndreas Gohr/**
4c29dc6e4SAndreas Gohr * Information and debugging functions
5c29dc6e4SAndreas Gohr *
6c29dc6e4SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7c29dc6e4SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
8c29dc6e4SAndreas Gohr */
9d4f83172SAndreas Gohr
1024870174SAndreas Gohruse dokuwiki\Extension\Event;
1124870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
1224870174SAndreas Gohruse dokuwiki\Debug\DebugHelper;
135a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
1431667ec6SAndreas Gohruse dokuwiki\Logger;
15198564abSMichael Große
166c5e3c5eSPhyif (!defined('DOKU_MESSAGEURL')) {
176c5e3c5eSPhy    if (in_array('ssl', stream_get_transports())) {
186c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'https://update.dokuwiki.org/check/');
196c5e3c5eSPhy    } else {
206c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'http://update.dokuwiki.org/check/');
216c5e3c5eSPhy    }
226c5e3c5eSPhy}
23c29dc6e4SAndreas Gohr
24c29dc6e4SAndreas Gohr/**
25c29dc6e4SAndreas Gohr * Check for new messages from upstream
26c29dc6e4SAndreas Gohr *
27c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
28c29dc6e4SAndreas Gohr */
29d868eb89SAndreas Gohrfunction checkUpdateMessages()
30d868eb89SAndreas Gohr{
31c29dc6e4SAndreas Gohr    global $conf;
32c29dc6e4SAndreas Gohr    global $INFO;
33ef362bb8SAnika Henke    global $updateVersion;
34c29dc6e4SAndreas Gohr    if (!$conf['updatecheck']) return;
35f8cc712eSAndreas Gohr    if ($conf['useacl'] && !$INFO['ismanager']) return;
36c29dc6e4SAndreas Gohr
3737b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
38c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
396c5e3c5eSPhy    $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https';
40c29dc6e4SAndreas Gohr
41c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
428d3d7569SAnika Henke    if ($lm < time() - (60 * 60 * 24) || $lm < @filemtime(DOKU_INC . DOKU_SCRIPT)) {
4363d9b820SAndreas Gohr        @touch($cf);
4490fb952cSAndreas Gohr        Logger::debug(
4590fb952cSAndreas Gohr            sprintf(
4690fb952cSAndreas Gohr                'checkUpdateMessages(): downloading messages to %s%s',
4790fb952cSAndreas Gohr                $cf,
4890fb952cSAndreas Gohr                $is_http ? ' (without SSL)' : ' (with SSL)'
4990fb952cSAndreas Gohr            )
5090fb952cSAndreas Gohr        );
51c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
5263d9b820SAndreas Gohr        $http->timeout = 12;
5386c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL . $updateVersion);
5486c04d87SAngus Gratton        if (is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
5586c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
5686c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
5786c04d87SAngus Gratton            io_saveFile($cf, $resp);
588f1efc43SAndreas Gohr        } else {
5931667ec6SAndreas Gohr            Logger::debug("checkUpdateMessages(): unexpected HTTP response received", $http->error);
608f1efc43SAndreas Gohr        }
61c29dc6e4SAndreas Gohr    } else {
6231667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): messages up to date");
63c29dc6e4SAndreas Gohr    }
64c29dc6e4SAndreas Gohr
6586c04d87SAngus Gratton    $data = io_readFile($cf);
66c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
67c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n", $data);
68c29dc6e4SAndreas Gohr    foreach ($msgs as $msg) {
69c29dc6e4SAndreas Gohr        if ($msg) msg($msg, 2);
70c29dc6e4SAndreas Gohr    }
71c29dc6e4SAndreas Gohr}
72c29dc6e4SAndreas Gohr
73c29dc6e4SAndreas Gohr
74c29dc6e4SAndreas Gohr/**
7525c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
76c29dc6e4SAndreas Gohr *
77c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
78c29dc6e4SAndreas Gohr */
79d868eb89SAndreas Gohrfunction getVersionData()
80d868eb89SAndreas Gohr{
8124870174SAndreas Gohr    $version = [];
82c29dc6e4SAndreas Gohr    //import version string
8379e79377SAndreas Gohr    if (file_exists(DOKU_INC . 'VERSION')) {
84c29dc6e4SAndreas Gohr        //official release
85d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC . 'VERSION'));
8625c07f93SAnika Henke        $version['type'] = 'Release';
875cf31920SAndreas Gohr    } elseif (is_dir(DOKU_INC . '.git')) {
885cf31920SAndreas Gohr        $version['type'] = 'Git';
8925c07f93SAnika Henke        $version['date'] = 'unknown';
90e570ed43SAndreas Gohr
91b9f5205aSDamien Regad        // First try to get date and commit hash by calling Git
92b6f8a5c6SDamien Regad        if (function_exists('shell_exec')) {
93b6f8a5c6SDamien Regad            $commitInfo = shell_exec("git log -1 --pretty=format:'%h %cd' --date=short");
94b9f5205aSDamien Regad            if ($commitInfo) {
9524870174SAndreas Gohr                [$version['sha'], $date] = explode(' ', $commitInfo);
96067b4fb9SMichael Große                $version['date'] = hsc($date);
97b9f5205aSDamien Regad                return $version;
98b9f5205aSDamien Regad            }
99b9f5205aSDamien Regad        }
100b9f5205aSDamien Regad
101f519f9dbSMichael Große        // we cannot use git on the shell -- let's do it manually!
102b9f5205aSDamien Regad        if (file_exists(DOKU_INC . '.git/HEAD')) {
103f519f9dbSMichael Große            $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD'));
104f519f9dbSMichael Große            if (strpos($headCommit, 'ref: ') === 0) {
105f519f9dbSMichael Große                // it is something like `ref: refs/heads/master`
10609bf5d22SDamien Regad                $headCommit = substr($headCommit, 5);
10709bf5d22SDamien Regad                $pathToHead = DOKU_INC . '.git/' . $headCommit;
10809bf5d22SDamien Regad                if (file_exists($pathToHead)) {
10909bf5d22SDamien Regad                    $headCommit = trim(file_get_contents($pathToHead));
11009bf5d22SDamien Regad                } else {
11109bf5d22SDamien Regad                    $packedRefs = file_get_contents(DOKU_INC . '.git/packed-refs');
11209bf5d22SDamien Regad                    if (!preg_match("~([[:xdigit:]]+) $headCommit~", $packedRefs, $matches)) {
11309bf5d22SDamien Regad                        # ref not found in pack file
11409bf5d22SDamien Regad                        return $version;
115f519f9dbSMichael Große                    }
11609bf5d22SDamien Regad                    $headCommit = $matches[1];
11709bf5d22SDamien Regad                }
11809bf5d22SDamien Regad            }
11909bf5d22SDamien Regad            // At this point $headCommit is a SHA
120b6f8a5c6SDamien Regad            $version['sha'] = $headCommit;
121b6f8a5c6SDamien Regad
12209bf5d22SDamien Regad            // Get commit date from Git object
123f519f9dbSMichael Große            $subDir = substr($headCommit, 0, 2);
124f519f9dbSMichael Große            $fileName = substr($headCommit, 2);
125be2e462dSMichael Große            $gitCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName";
126a8ac20eaSGerrit Uitslag            if (file_exists($gitCommitObject) && function_exists('zlib_decode')) {
127be2e462dSMichael Große                $commit = zlib_decode(file_get_contents($gitCommitObject));
128f519f9dbSMichael Große                $committerLine = explode("\n", $commit)[3];
129f519f9dbSMichael Große                $committerData = explode(' ', $committerLine);
130f519f9dbSMichael Große                end($committerData);
131f519f9dbSMichael Große                $ts = prev($committerData);
132f519f9dbSMichael Große                if ($ts && $date = date('Y-m-d', $ts)) {
133f519f9dbSMichael Große                    $version['date'] = $date;
134f519f9dbSMichael Große                }
1355cf31920SAndreas Gohr            }
13625c07f93SAnika Henke        }
137c29dc6e4SAndreas Gohr    } else {
1386a34de2dSAnika Henke        global $updateVersion;
1396a34de2dSAnika Henke        $version['date'] = 'update version ' . $updateVersion;
14025c07f93SAnika Henke        $version['type'] = 'snapshot?';
141c29dc6e4SAndreas Gohr    }
1425cf31920SAndreas Gohr    return $version;
143c29dc6e4SAndreas Gohr}
144c29dc6e4SAndreas Gohr
145c29dc6e4SAndreas Gohr/**
14625c07f93SAnika Henke * Return DokuWiki's version (as a string)
14725c07f93SAnika Henke *
14825c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
14925c07f93SAnika Henke */
150d868eb89SAndreas Gohrfunction getVersion()
151d868eb89SAndreas Gohr{
15225c07f93SAnika Henke    $version = getVersionData();
15324870174SAndreas Gohr    $sha = empty($version['sha']) ? '' : ' (' . $version['sha'] . ')';
154b6f8a5c6SDamien Regad    return $version['type'] . ' ' . $version['date'] . $sha;
15525c07f93SAnika Henke}
15625c07f93SAnika Henke
15725c07f93SAnika Henke/**
158c29dc6e4SAndreas Gohr * Run a few sanity checks
159c29dc6e4SAndreas Gohr *
160c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
161c29dc6e4SAndreas Gohr */
162d868eb89SAndreas Gohrfunction check()
163d868eb89SAndreas Gohr{
164c29dc6e4SAndreas Gohr    global $conf;
165c29dc6e4SAndreas Gohr    global $INFO;
166585bf44eSChristopher Smith    /* @var Input $INPUT */
167585bf44eSChristopher Smith    global $INPUT;
168c29dc6e4SAndreas Gohr
1693f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']) {
170c29dc6e4SAndreas Gohr        msg('DokuWiki version: ' . getVersion(), 1);
171c49393f5SAndreas Gohr        if (version_compare(phpversion(), '7.4.0', '<')) {
172c49393f5SAndreas Gohr            msg('Your PHP version is too old (' . phpversion() . ' vs. 7.4+ needed)', -1);
173c29dc6e4SAndreas Gohr        } else {
174c29dc6e4SAndreas Gohr            msg('PHP version ' . phpversion(), 1);
175c29dc6e4SAndreas Gohr        }
17624870174SAndreas Gohr    } elseif (version_compare(phpversion(), '7.4.0', '<')) {
17708d1a8dfSAndreas Gohr        msg('Your PHP version is too old', -1);
17808d1a8dfSAndreas Gohr    }
179c6e971ddSAndreas Gohr
18024870174SAndreas Gohr    $mem = php_to_byte(ini_get('memory_limit'));
18173038c47SAndreas Gohr    if ($mem) {
182eb2e46caSAndreas Gohr        if ($mem === -1) {
1838f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
18424870174SAndreas Gohr        } elseif ($mem < 16_777_216) {
1852b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
1862b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
18724870174SAndreas Gohr        } elseif ($mem < 20_971_520) {
1882b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
18964159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
19024870174SAndreas Gohr        } elseif ($mem < 33_554_432) {
1912b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
19264159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
19373038c47SAndreas Gohr        } else {
194c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
19573038c47SAndreas Gohr        }
19673038c47SAndreas Gohr    }
19773038c47SAndreas Gohr
198c29dc6e4SAndreas Gohr    if (is_writable($conf['changelog'])) {
199c29dc6e4SAndreas Gohr        msg('Changelog is writable', 1);
20024870174SAndreas Gohr    } elseif (file_exists($conf['changelog'])) {
201c29dc6e4SAndreas Gohr        msg('Changelog is not writable', -1);
202c29dc6e4SAndreas Gohr    }
203c29dc6e4SAndreas Gohr
20479e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
2052cdbda06SAnika Henke        msg('Old changelog exists', 0);
206c29dc6e4SAndreas Gohr    }
207c29dc6e4SAndreas Gohr
20879e79377SAndreas Gohr    if (file_exists($conf['changelog'] . '_failed')) {
2092cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
21079e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'] . '_importing')) {
211c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
21279e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'] . '_import_ok')) {
2132cdbda06SAnika Henke        msg('Old changelog imported', 1);
214c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
2152cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
216c29dc6e4SAndreas Gohr        }
217c29dc6e4SAndreas Gohr    }
218c29dc6e4SAndreas Gohr
2194c7ecf15SGuy Brand    if (is_writable(DOKU_CONF)) {
2204c7ecf15SGuy Brand        msg('conf directory is writable', 1);
2214c7ecf15SGuy Brand    } else {
2224c7ecf15SGuy Brand        msg('conf directory is not writable', -1);
2234c7ecf15SGuy Brand    }
2244c7ecf15SGuy Brand
2250d487d8fSAndreas Gohr    if ($conf['authtype'] == 'plain') {
226defb7d57SAnika Henke        global $config_cascade;
227defb7d57SAnika Henke        if (is_writable($config_cascade['plainauth.users']['default'])) {
228c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable', 1);
229c29dc6e4SAndreas Gohr        } else {
230c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable', 0);
231c29dc6e4SAndreas Gohr        }
2320d487d8fSAndreas Gohr    }
233c29dc6e4SAndreas Gohr
234c29dc6e4SAndreas Gohr    if (function_exists('mb_strpos')) {
235c29dc6e4SAndreas Gohr        if (defined('UTF8_NOMBSTRING')) {
236c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used', 0);
237c29dc6e4SAndreas Gohr        } else {
238c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used', 1);
2394222b898SAndreas Gohr            if (ini_get('mbstring.func_overload') != 0) {
2404222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled', -1);
2414222b898SAndreas Gohr            }
242c29dc6e4SAndreas Gohr        }
243c29dc6e4SAndreas Gohr    } else {
244c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used', 0);
245c29dc6e4SAndreas Gohr    }
246c29dc6e4SAndreas Gohr
2473161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
248a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
249a731ed1dSAndreas Gohr    }
2503161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
251a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
252a731ed1dSAndreas Gohr    }
253a731ed1dSAndreas Gohr
254e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
255e5ab313fSAndreas Gohr    if (!$loc) {
256e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this', -1);
257e5ab313fSAndreas Gohr    } elseif (stripos($loc, 'utf') === false) {
25864159a61SAndreas Gohr        msg('Your locale <code>' . hsc($loc) . '</code> seems not to be a UTF-8 locale,
25964159a61SAndreas Gohr             you should fix this if you encounter problems.', 0);
260e5ab313fSAndreas Gohr    } else {
261e5ab313fSAndreas Gohr        msg('Valid locale ' . hsc($loc) . ' found.', 1);
262e5ab313fSAndreas Gohr    }
263e5ab313fSAndreas Gohr
264c29dc6e4SAndreas Gohr    if ($conf['allowdebug']) {
265c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0', -1);
266c29dc6e4SAndreas Gohr    } else {
267c29dc6e4SAndreas Gohr        msg('Debugging support is disabled', 1);
268c29dc6e4SAndreas Gohr    }
269c29dc6e4SAndreas Gohr
270a4231b8cSfiwswe    if (!empty($INFO['userinfo']['name'])) {
27190fb952cSAndreas Gohr        msg(sprintf(
27290fb952cSAndreas Gohr            "You are currently logged in as %s (%s)",
27390fb952cSAndreas Gohr            $INPUT->server->str('REMOTE_USER'),
27490fb952cSAndreas Gohr            $INFO['userinfo']['name']
27590fb952cSAndreas Gohr        ), 0);
2768368419bSSyntaxseed        msg('You are part of the groups ' . implode(', ', $INFO['userinfo']['grps']), 0);
2773d3c095dSMike Frysinger    } else {
2783d3c095dSMike Frysinger        msg('You are currently not logged in', 0);
2793d3c095dSMike Frysinger    }
2803d3c095dSMike Frysinger
281c29dc6e4SAndreas Gohr    msg('Your current permission for this page is ' . $INFO['perm'], 0);
282c29dc6e4SAndreas Gohr
283322ad074SAndreas Gohr    if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) {
284322ad074SAndreas Gohr        msg('The current page is writable by the webserver', 1);
285322ad074SAndreas Gohr    } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) {
286322ad074SAndreas Gohr        msg('The current page can be created by the webserver', 1);
287c29dc6e4SAndreas Gohr    } else {
288322ad074SAndreas Gohr        msg('The current page is not writable by the webserver', -1);
289c29dc6e4SAndreas Gohr    }
290c29dc6e4SAndreas Gohr
291c29dc6e4SAndreas Gohr    if ($INFO['writable']) {
292322ad074SAndreas Gohr        msg('The current page is writable by you', 1);
293c29dc6e4SAndreas Gohr    } else {
294322ad074SAndreas Gohr        msg('The current page is not writable by you', -1);
295c29dc6e4SAndreas Gohr    }
2963b1dfc83SAndreas Gohr
29726f7dbf5SMichael Hamann    // Check for corrupted search index
29826f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
29926f7dbf5SMichael Hamann    $index_corrupted = false;
30026f7dbf5SMichael Hamann    foreach ($lengths as $length) {
30124870174SAndreas Gohr        if (count(idx_getIndex('w', $length)) !== count(idx_getIndex('i', $length))) {
30226f7dbf5SMichael Hamann            $index_corrupted = true;
30326f7dbf5SMichael Hamann            break;
30426f7dbf5SMichael Hamann        }
30526f7dbf5SMichael Hamann    }
30626f7dbf5SMichael Hamann
30726f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
30824870174SAndreas Gohr        if (count(idx_getIndex($index . '_w', '')) !== count(idx_getIndex($index . '_i', ''))) {
30926f7dbf5SMichael Hamann            $index_corrupted = true;
31026f7dbf5SMichael Hamann            break;
31126f7dbf5SMichael Hamann        }
31226f7dbf5SMichael Hamann    }
31326f7dbf5SMichael Hamann
314d6c7b502SAndreas Gohr    if ($index_corrupted) {
315d6c7b502SAndreas Gohr        msg(
316d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
3173d94d9edSMichael Hamann                probably needs to be rebuilt. See
318*f5be2fc0SGerrit Uitslag                <a href="https://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
319dccd6b2bSAndreas Gohr                for ways to rebuild the search index.',
320dccd6b2bSAndreas Gohr            -1
321d6c7b502SAndreas Gohr        );
322d6c7b502SAndreas Gohr    } elseif (!empty($lengths)) {
3233d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
324d6c7b502SAndreas Gohr    } else {
325d6c7b502SAndreas Gohr        msg(
326d6c7b502SAndreas Gohr            'The search index is empty. See
327*f5be2fc0SGerrit Uitslag                <a href="https://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
3283d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
329d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
330d6c7b502SAndreas Gohr        );
331d6c7b502SAndreas Gohr    }
332d6c7b502SAndreas Gohr
333d6c7b502SAndreas Gohr    // rough time check
334d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
335d6c7b502SAndreas Gohr    $http->max_redirect = 0;
336d6c7b502SAndreas Gohr    $http->timeout = 3;
337*f5be2fc0SGerrit Uitslag    $http->sendRequest('https://www.dokuwiki.org', '', 'HEAD');
338d6c7b502SAndreas Gohr    $now = time();
339d6c7b502SAndreas Gohr    if (isset($http->resp_headers['date'])) {
340d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
341d6c7b502SAndreas Gohr        $diff = $time - $now;
342d6c7b502SAndreas Gohr
343d6c7b502SAndreas Gohr        if (abs($diff) < 4) {
344d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
345d6c7b502SAndreas Gohr        } else {
34664159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
34764159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
348d6c7b502SAndreas Gohr        }
349d6c7b502SAndreas Gohr    }
350c29dc6e4SAndreas Gohr}
351c29dc6e4SAndreas Gohr
352c29dc6e4SAndreas Gohr/**
35346028c4cSAndreas Gohr * Display a message to the user
354c29dc6e4SAndreas Gohr *
355c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
356c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
357c29dc6e4SAndreas Gohr * using html_msgarea()
358c29dc6e4SAndreas Gohr *
3590e20480fSPhy * Triggers INFOUTIL_MSG_SHOW
3600e20480fSPhy *
3616164d900SAndreas Gohr * @param string $message
3626164d900SAndreas Gohr * @param int $lvl -1 = error, 0 = info, 1 = success, 2 = notify
3636164d900SAndreas Gohr * @param string $line line number
3646164d900SAndreas Gohr * @param string $file file number
3656164d900SAndreas Gohr * @param int $allow who's allowed to see the message, see MSG_* constants
366*f5be2fc0SGerrit Uitslag * @see html_msgarea()
3676164d900SAndreas Gohr */
368d868eb89SAndreas Gohrfunction msg($message, $lvl = 0, $line = '', $file = '', $allow = MSG_PUBLIC)
369d868eb89SAndreas Gohr{
370cc58224cSMichael Hamann    global $MSG, $MSG_shown;
371556e996eSAndreas Gohr    static $errors = [
372556e996eSAndreas Gohr        -1 => 'error',
373556e996eSAndreas Gohr        0 => 'info',
374556e996eSAndreas Gohr        1 => 'success',
375556e996eSAndreas Gohr        2 => 'notify',
376556e996eSAndreas Gohr    ];
377c29dc6e4SAndreas Gohr
378556e996eSAndreas Gohr    $msgdata = [
3790e20480fSPhy        'msg' => $message,
380556e996eSAndreas Gohr        'lvl' => $errors[$lvl],
3810e20480fSPhy        'allow' => $allow,
3820e20480fSPhy        'line' => $line,
3830e20480fSPhy        'file' => $file,
384556e996eSAndreas Gohr    ];
3850e20480fSPhy
38624870174SAndreas Gohr    $evt = new Event('INFOUTIL_MSG_SHOW', $msgdata);
3870e20480fSPhy    if ($evt->advise_before()) {
3880e20480fSPhy        /* Show msg normally - event could suppress message show */
3890e20480fSPhy        if ($msgdata['line'] || $msgdata['file']) {
39024870174SAndreas Gohr            $basename = PhpString::basename($msgdata['file']);
3910e20480fSPhy            $msgdata['msg'] .= ' [' . $basename . ':' . $msgdata['line'] . ']';
3920e20480fSPhy        }
393c29dc6e4SAndreas Gohr
39424870174SAndreas Gohr        if (!isset($MSG)) $MSG = [];
3950e20480fSPhy        $MSG[] = $msgdata;
396cc58224cSMichael Hamann        if (isset($MSG_shown) || headers_sent()) {
397c29dc6e4SAndreas Gohr            if (function_exists('html_msgarea')) {
398c29dc6e4SAndreas Gohr                html_msgarea();
399c29dc6e4SAndreas Gohr            } else {
40026dfc232SAndreas Gohr                echo "ERROR(" . $msgdata['lvl'] . ") " . $msgdata['msg'] . "\n";
401c29dc6e4SAndreas Gohr            }
40269266de5SDominik Eckelmann            unset($GLOBALS['MSG']);
403c29dc6e4SAndreas Gohr        }
404c29dc6e4SAndreas Gohr    }
4050e20480fSPhy    $evt->advise_after();
4060e20480fSPhy    unset($evt);
4070e20480fSPhy}
408*f5be2fc0SGerrit Uitslag
409f755f9abSChristopher Smith/**
410f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
411f755f9abSChristopher Smith * in the $msg data structure
412f755f9abSChristopher Smith *
413*f5be2fc0SGerrit Uitslag * @param array $msg dokuwiki msg structure:
414*f5be2fc0SGerrit Uitslag *              msg   => string, the message;
415*f5be2fc0SGerrit Uitslag *              lvl   => int, level of the message (see msg() function);
416*f5be2fc0SGerrit Uitslag *              allow => int, flag used to determine who is allowed to see the message, see MSG_* constants
4176164d900SAndreas Gohr * @return bool
418f755f9abSChristopher Smith */
419d868eb89SAndreas Gohrfunction info_msg_allowed($msg)
420d868eb89SAndreas Gohr{
421d3bae478SChristopher Smith    global $INFO, $auth;
422d3bae478SChristopher Smith
423d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
424f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
425d3bae478SChristopher Smith
426d3bae478SChristopher Smith    // restricted msg, but no authentication
427d3bae478SChristopher Smith    if (empty($auth)) return false;
428d3bae478SChristopher Smith
429f755f9abSChristopher Smith    switch ($msg['allow']) {
430d3bae478SChristopher Smith        case MSG_USERS_ONLY:
431d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
432d3bae478SChristopher Smith
433d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
434d3bae478SChristopher Smith            return $INFO['ismanager'];
435d3bae478SChristopher Smith
436d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
437d3bae478SChristopher Smith            return $INFO['isadmin'];
438d3bae478SChristopher Smith
439d3bae478SChristopher Smith        default:
440dccd6b2bSAndreas Gohr            trigger_error(
441dccd6b2bSAndreas Gohr                'invalid msg allow restriction.  msg="' . $msg['msg'] . '" allow=' . $msg['allow'] . '"',
442dccd6b2bSAndreas Gohr                E_USER_WARNING
443dccd6b2bSAndreas Gohr            );
444d3bae478SChristopher Smith            return $INFO['isadmin'];
445d3bae478SChristopher Smith    }
446d3bae478SChristopher Smith}
447d3bae478SChristopher Smith
448c29dc6e4SAndreas Gohr/**
449c29dc6e4SAndreas Gohr * print debug messages
450c29dc6e4SAndreas Gohr *
451c29dc6e4SAndreas Gohr * little function to print the content of a var
452c29dc6e4SAndreas Gohr *
453f50a239bSTakamura * @param string $msg
454f50a239bSTakamura * @param bool $hidden
455*f5be2fc0SGerrit Uitslag *
456*f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
457c29dc6e4SAndreas Gohr */
458d868eb89SAndreas Gohrfunction dbg($msg, $hidden = false)
459d868eb89SAndreas Gohr{
46013493794SAndreas Gohr    if ($hidden) {
46113493794SAndreas Gohr        echo "<!--\n";
462c29dc6e4SAndreas Gohr        print_r($msg);
46313493794SAndreas Gohr        echo "\n-->";
46413493794SAndreas Gohr    } else {
46513493794SAndreas Gohr        echo '<pre class="dbg">';
46613493794SAndreas Gohr        echo hsc(print_r($msg, true));
46713493794SAndreas Gohr        echo '</pre>';
46813493794SAndreas Gohr    }
469c29dc6e4SAndreas Gohr}
470c29dc6e4SAndreas Gohr
471c29dc6e4SAndreas Gohr/**
472cad4fbf6SAndreas Gohr * Print info to debug log file
473c29dc6e4SAndreas Gohr *
474f50a239bSTakamura * @param string $msg
475f50a239bSTakamura * @param string $header
476*f5be2fc0SGerrit Uitslag *
477*f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
478*f5be2fc0SGerrit Uitslag * @deprecated 2020-08-13
479c29dc6e4SAndreas Gohr */
480d868eb89SAndreas Gohrfunction dbglog($msg, $header = '')
481d868eb89SAndreas Gohr{
4820ecde6ceSAndreas Gohr    dbg_deprecated('\\dokuwiki\\Logger');
483585bf44eSChristopher Smith
4840ecde6ceSAndreas Gohr    // was the msg as single line string? use it as header
485cad4fbf6SAndreas Gohr    if ($header === '' && is_string($msg) && strpos($msg, "\n") === false) {
4860ecde6ceSAndreas Gohr        $header = $msg;
4870ecde6ceSAndreas Gohr        $msg = '';
488c7408a63SAndreas Gohr    }
489c7408a63SAndreas Gohr
49031667ec6SAndreas Gohr    Logger::getInstance(Logger::LOG_DEBUG)->log(
491dccd6b2bSAndreas Gohr        $header,
492dccd6b2bSAndreas Gohr        $msg
4930ecde6ceSAndreas Gohr    );
494c29dc6e4SAndreas Gohr}
495c29dc6e4SAndreas Gohr
496db09e31eSAndreas Gohr/**
4971419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4981419a485SAndreas Gohr *
4991419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
50085331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
5011419a485SAndreas Gohr */
502d868eb89SAndreas Gohrfunction dbg_deprecated($alternative = '')
503d868eb89SAndreas Gohr{
50424870174SAndreas Gohr    DebugHelper::dbgDeprecatedFunction($alternative, 2);
50544455016SAndreas Gohr}
5061419a485SAndreas Gohr
5071419a485SAndreas Gohr/**
508db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
509db09e31eSAndreas Gohr *
510db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
511db09e31eSAndreas Gohr */
512d868eb89SAndreas Gohrfunction dbg_backtrace()
513d868eb89SAndreas Gohr{
514db09e31eSAndreas Gohr    // Get backtrace
515db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
516db09e31eSAndreas Gohr
517db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
518db09e31eSAndreas Gohr    array_shift($backtrace);
519db09e31eSAndreas Gohr
520db09e31eSAndreas Gohr    // Iterate backtrace
52124870174SAndreas Gohr    $calls = [];
522db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
523db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
524db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
525db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
526db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
527db09e31eSAndreas Gohr
52824870174SAndreas Gohr        $params = [];
529db09e31eSAndreas Gohr        if (isset($call['args'])) {
5308259f1aaSAndreas Gohr            foreach ($call['args'] as $arg) {
5318259f1aaSAndreas Gohr                if (is_object($arg)) {
5328259f1aaSAndreas Gohr                    $params[] = '[Object ' . get_class($arg) . ']';
5338259f1aaSAndreas Gohr                } elseif (is_array($arg)) {
5348259f1aaSAndreas Gohr                    $params[] = '[Array]';
5358259f1aaSAndreas Gohr                } elseif (is_null($arg)) {
53659bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
5378259f1aaSAndreas Gohr                } else {
53824870174SAndreas Gohr                    $params[] = '"' . $arg . '"';
539db09e31eSAndreas Gohr                }
5408259f1aaSAndreas Gohr            }
5418259f1aaSAndreas Gohr        }
5428259f1aaSAndreas Gohr        $params = implode(', ', $params);
543db09e31eSAndreas Gohr
544dccd6b2bSAndreas Gohr        $calls[$depth - $i] = sprintf(
545dccd6b2bSAndreas Gohr            '%s(%s) called at %s',
546db09e31eSAndreas Gohr            $function,
547db09e31eSAndreas Gohr            str_replace("\n", '\n', $params),
548dccd6b2bSAndreas Gohr            $location
549dccd6b2bSAndreas Gohr        );
550db09e31eSAndreas Gohr    }
551db09e31eSAndreas Gohr    ksort($calls);
552db09e31eSAndreas Gohr
553db09e31eSAndreas Gohr    return implode("\n", $calls);
554db09e31eSAndreas Gohr}
555db09e31eSAndreas Gohr
55624297a69SAndreas Gohr/**
55724297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
55824297a69SAndreas Gohr *
55924297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
56024297a69SAndreas Gohr * debug output
56124297a69SAndreas Gohr *
562f50a239bSTakamura * @param array $data
563*f5be2fc0SGerrit Uitslag *
564*f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
56524297a69SAndreas Gohr */
566d868eb89SAndreas Gohrfunction debug_guard(&$data)
567d868eb89SAndreas Gohr{
56824297a69SAndreas Gohr    foreach ($data as $key => $value) {
56924297a69SAndreas Gohr        if (preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i', $key)) {
57024297a69SAndreas Gohr            $data[$key] = '***';
57124297a69SAndreas Gohr            continue;
57224297a69SAndreas Gohr        }
57324297a69SAndreas Gohr        if (is_array($value)) debug_guard($data[$key]);
57424297a69SAndreas Gohr    }
57524297a69SAndreas Gohr}
576