xref: /dokuwiki/inc/infoutils.php (revision 54e76260c0b2582a4922e778f87e8202e3f14ec9)
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
1079f150bdSAndreas Gohruse dokuwiki\Debug\DebugHelper;
116547cfc7SGerrit Uitslaguse dokuwiki\Extension\AuthPlugin;
1224870174SAndreas Gohruse dokuwiki\Extension\Event;
135a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
1431667ec6SAndreas Gohruse dokuwiki\Logger;
1521fbd01bSAndreas Gohruse dokuwiki\Search\Exception\IndexIntegrityException;
1621fbd01bSAndreas Gohruse dokuwiki\Search\Indexer;
17be5c1ea2SSatoshi Saharause dokuwiki\Utf8;
1879f150bdSAndreas Gohruse dokuwiki\Utf8\PhpString;
19198564abSMichael Große
206c5e3c5eSPhyif (!defined('DOKU_MESSAGEURL')) {
216c5e3c5eSPhy    if (in_array('ssl', stream_get_transports())) {
226c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'https://update.dokuwiki.org/check/');
236c5e3c5eSPhy    } else {
246c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'http://update.dokuwiki.org/check/');
256c5e3c5eSPhy    }
266c5e3c5eSPhy}
27c29dc6e4SAndreas Gohr
28c29dc6e4SAndreas Gohr/**
29c29dc6e4SAndreas Gohr * Check for new messages from upstream
30c29dc6e4SAndreas Gohr *
31c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
32c29dc6e4SAndreas Gohr */
33d868eb89SAndreas Gohrfunction checkUpdateMessages()
34d868eb89SAndreas Gohr{
35c29dc6e4SAndreas Gohr    global $conf;
36c29dc6e4SAndreas Gohr    global $INFO;
37ef362bb8SAnika Henke    global $updateVersion;
38c29dc6e4SAndreas Gohr    if (!$conf['updatecheck']) return;
39f8cc712eSAndreas Gohr    if ($conf['useacl'] && !$INFO['ismanager']) return;
40c29dc6e4SAndreas Gohr
4137b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
42c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
436c16a3a9Sfiwswe    $is_http = !str_starts_with(DOKU_MESSAGEURL, 'https');
44c29dc6e4SAndreas Gohr
45c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
468d3d7569SAnika Henke    if ($lm < time() - (60 * 60 * 24) || $lm < @filemtime(DOKU_INC . DOKU_SCRIPT)) {
4763d9b820SAndreas Gohr        @touch($cf);
4890fb952cSAndreas Gohr        Logger::debug(
4990fb952cSAndreas Gohr            sprintf(
5090fb952cSAndreas Gohr                'checkUpdateMessages(): downloading messages to %s%s',
5190fb952cSAndreas Gohr                $cf,
5290fb952cSAndreas Gohr                $is_http ? ' (without SSL)' : ' (with SSL)'
5390fb952cSAndreas Gohr            )
5490fb952cSAndreas Gohr        );
55c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
5663d9b820SAndreas Gohr        $http->timeout = 12;
5786c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL . $updateVersion);
586c16a3a9Sfiwswe        if (is_string($resp) && ($resp == '' || str_ends_with(trim($resp), '%'))) {
5986c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
6086c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
6186c04d87SAngus Gratton            io_saveFile($cf, $resp);
628f1efc43SAndreas Gohr        } else {
6331667ec6SAndreas Gohr            Logger::debug("checkUpdateMessages(): unexpected HTTP response received", $http->error);
648f1efc43SAndreas Gohr        }
65c29dc6e4SAndreas Gohr    } else {
6631667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): messages up to date");
67c29dc6e4SAndreas Gohr    }
68c29dc6e4SAndreas Gohr
6986c04d87SAngus Gratton    $data = io_readFile($cf);
70c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
71c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n", $data);
72c29dc6e4SAndreas Gohr    foreach ($msgs as $msg) {
73c29dc6e4SAndreas Gohr        if ($msg) msg($msg, 2);
74c29dc6e4SAndreas Gohr    }
75c29dc6e4SAndreas Gohr}
76c29dc6e4SAndreas Gohr
77c29dc6e4SAndreas Gohr
78c29dc6e4SAndreas Gohr/**
7925c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
80c29dc6e4SAndreas Gohr *
81c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
82c29dc6e4SAndreas Gohr */
83d868eb89SAndreas Gohrfunction getVersionData()
84d868eb89SAndreas Gohr{
8524870174SAndreas Gohr    $version = [];
86c29dc6e4SAndreas Gohr    //import version string
8779e79377SAndreas Gohr    if (file_exists(DOKU_INC . 'VERSION')) {
88c29dc6e4SAndreas Gohr        //official release
89d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC . 'VERSION'));
9025c07f93SAnika Henke        $version['type'] = 'Release';
915cf31920SAndreas Gohr    } elseif (is_dir(DOKU_INC . '.git')) {
925cf31920SAndreas Gohr        $version['type'] = 'Git';
9325c07f93SAnika Henke        $version['date'] = 'unknown';
94e570ed43SAndreas Gohr
95b9f5205aSDamien Regad        // First try to get date and commit hash by calling Git
96b6f8a5c6SDamien Regad        if (function_exists('shell_exec')) {
97b9e35b2fSAndreas Gohr            $args = ['git', 'log', '-1', '--pretty=format:%h %cd', '--date=short'];
9827bd7771Ssplitbrain            $commitInfo = shell_exec(implode(' ', array_map(escapeshellarg(...), $args)));
99b9f5205aSDamien Regad            if ($commitInfo) {
10024870174SAndreas Gohr                [$version['sha'], $date] = explode(' ', $commitInfo);
101067b4fb9SMichael Große                $version['date'] = hsc($date);
102b9f5205aSDamien Regad                return $version;
103b9f5205aSDamien Regad            }
104b9f5205aSDamien Regad        }
105b9f5205aSDamien Regad
106f519f9dbSMichael Große        // we cannot use git on the shell -- let's do it manually!
107b9f5205aSDamien Regad        if (file_exists(DOKU_INC . '.git/HEAD')) {
108f519f9dbSMichael Große            $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD'));
109093fe67eSAndreas Gohr            if (str_starts_with($headCommit, 'ref: ')) {
110f519f9dbSMichael Große                // it is something like `ref: refs/heads/master`
11109bf5d22SDamien Regad                $headCommit = substr($headCommit, 5);
11209bf5d22SDamien Regad                $pathToHead = DOKU_INC . '.git/' . $headCommit;
11309bf5d22SDamien Regad                if (file_exists($pathToHead)) {
11409bf5d22SDamien Regad                    $headCommit = trim(file_get_contents($pathToHead));
11509bf5d22SDamien Regad                } else {
11609bf5d22SDamien Regad                    $packedRefs = file_get_contents(DOKU_INC . '.git/packed-refs');
11709bf5d22SDamien Regad                    if (!preg_match("~([[:xdigit:]]+) $headCommit~", $packedRefs, $matches)) {
11809bf5d22SDamien Regad                        # ref not found in pack file
11909bf5d22SDamien Regad                        return $version;
120f519f9dbSMichael Große                    }
12109bf5d22SDamien Regad                    $headCommit = $matches[1];
12209bf5d22SDamien Regad                }
12309bf5d22SDamien Regad            }
12409bf5d22SDamien Regad            // At this point $headCommit is a SHA
125b6f8a5c6SDamien Regad            $version['sha'] = $headCommit;
126b6f8a5c6SDamien Regad
12709bf5d22SDamien Regad            // Get commit date from Git object
128f519f9dbSMichael Große            $subDir = substr($headCommit, 0, 2);
129f519f9dbSMichael Große            $fileName = substr($headCommit, 2);
130be2e462dSMichael Große            $gitCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName";
131a8ac20eaSGerrit Uitslag            if (file_exists($gitCommitObject) && function_exists('zlib_decode')) {
132be2e462dSMichael Große                $commit = zlib_decode(file_get_contents($gitCommitObject));
133f519f9dbSMichael Große                $committerLine = explode("\n", $commit)[3];
134f519f9dbSMichael Große                $committerData = explode(' ', $committerLine);
135f519f9dbSMichael Große                end($committerData);
136f519f9dbSMichael Große                $ts = prev($committerData);
137f519f9dbSMichael Große                if ($ts && $date = date('Y-m-d', $ts)) {
138f519f9dbSMichael Große                    $version['date'] = $date;
139f519f9dbSMichael Große                }
1405cf31920SAndreas Gohr            }
14125c07f93SAnika Henke        }
142c29dc6e4SAndreas Gohr    } else {
1436a34de2dSAnika Henke        global $updateVersion;
1446a34de2dSAnika Henke        $version['date'] = 'update version ' . $updateVersion;
14525c07f93SAnika Henke        $version['type'] = 'snapshot?';
146c29dc6e4SAndreas Gohr    }
1475cf31920SAndreas Gohr    return $version;
148c29dc6e4SAndreas Gohr}
149c29dc6e4SAndreas Gohr
150c29dc6e4SAndreas Gohr/**
151fe9f11e2SAndreas Gohr * Return DokuWiki's version
152fe9f11e2SAndreas Gohr *
153fe9f11e2SAndreas Gohr * This returns the version in the form "Type Date (SHA)". Where type is either
154fe9f11e2SAndreas Gohr * "Release" or "Git" and date is the date of the release or the date of the
155fe9f11e2SAndreas Gohr * last commit. SHA is the short SHA of the last commit - this is only added on
156fe9f11e2SAndreas Gohr * git checkouts.
157fe9f11e2SAndreas Gohr *
158fe9f11e2SAndreas Gohr * If no version can be determined "snapshot? update version XX" is returned.
159fe9f11e2SAndreas Gohr * Where XX represents the update version number set in doku.php.
160fe9f11e2SAndreas Gohr *
161fe9f11e2SAndreas Gohr * @return string The version string e.g. "Release 2023-04-04a"
16279f150bdSAndreas Gohr * @author Anika Henke <anika@selfthinker.org>
16325c07f93SAnika Henke */
164d868eb89SAndreas Gohrfunction getVersion()
165d868eb89SAndreas Gohr{
16625c07f93SAnika Henke    $version = getVersionData();
16724870174SAndreas Gohr    $sha = empty($version['sha']) ? '' : ' (' . $version['sha'] . ')';
168b6f8a5c6SDamien Regad    return $version['type'] . ' ' . $version['date'] . $sha;
16925c07f93SAnika Henke}
17025c07f93SAnika Henke
17125c07f93SAnika Henke/**
17279f150bdSAndreas Gohr * Get some data about the environment this wiki is running in
17379f150bdSAndreas Gohr *
17479f150bdSAndreas Gohr * @return array
17579f150bdSAndreas Gohr */
17679f150bdSAndreas Gohrfunction getRuntimeVersions()
17779f150bdSAndreas Gohr{
17879f150bdSAndreas Gohr    $data = [];
17979f150bdSAndreas Gohr    $data['php'] = 'PHP ' . PHP_VERSION;
18079f150bdSAndreas Gohr
18179f150bdSAndreas Gohr    $osRelease = getOsRelease();
18279f150bdSAndreas Gohr    if (isset($osRelease['PRETTY_NAME'])) {
18379f150bdSAndreas Gohr        $data['dist'] = $osRelease['PRETTY_NAME'];
18479f150bdSAndreas Gohr    }
18579f150bdSAndreas Gohr
18679f150bdSAndreas Gohr    $data['os'] = php_uname('s') . ' ' . php_uname('r');
18779f150bdSAndreas Gohr    $data['sapi'] = PHP_SAPI;
18879f150bdSAndreas Gohr
18979f150bdSAndreas Gohr    if (getenv('KUBERNETES_SERVICE_HOST')) {
19079f150bdSAndreas Gohr        $data['container'] = 'Kubernetes';
1910a374113SAndreas Gohr    } elseif (@file_exists('/.dockerenv')) {
19279f150bdSAndreas Gohr        $data['container'] = 'Docker';
19379f150bdSAndreas Gohr    }
19479f150bdSAndreas Gohr
19579f150bdSAndreas Gohr    return $data;
19679f150bdSAndreas Gohr}
19779f150bdSAndreas Gohr
19879f150bdSAndreas Gohr/**
19979f150bdSAndreas Gohr * Get informational data about the linux distribution this wiki is running on
20079f150bdSAndreas Gohr *
20179f150bdSAndreas Gohr * @see https://gist.github.com/natefoo/814c5bf936922dad97ff
20279f150bdSAndreas Gohr * @return array an os-release array, might be empty
20379f150bdSAndreas Gohr */
20479f150bdSAndreas Gohrfunction getOsRelease()
20579f150bdSAndreas Gohr{
206649695abSAndreas Gohr    $reader = fn($file) => @parse_ini_string(preg_replace('/^\s*#.*$/m', '', file_get_contents($file))) ?: [];
2070a374113SAndreas Gohr
20879f150bdSAndreas Gohr    $osRelease = [];
2090a374113SAndreas Gohr    if (@file_exists('/etc/os-release')) {
21079f150bdSAndreas Gohr        // pretty much any common Linux distribution has this
2110a374113SAndreas Gohr        $osRelease = $reader('/etc/os-release');
2120a374113SAndreas Gohr    } elseif (@file_exists('/etc/synoinfo.conf') && @file_exists('/etc/VERSION')) {
21379f150bdSAndreas Gohr        // Synology DSM has its own way
2140a374113SAndreas Gohr        $synoInfo = $reader('/etc/synoinfo.conf');
2150a374113SAndreas Gohr        $synoVersion = $reader('/etc/VERSION');
21679f150bdSAndreas Gohr        $osRelease['NAME'] = 'Synology DSM';
21779f150bdSAndreas Gohr        $osRelease['ID'] = 'synology';
21879f150bdSAndreas Gohr        $osRelease['ID_LIKE'] = 'linux';
219649695abSAndreas Gohr        $osRelease['VERSION_ID'] = $synoVersion['productversion'] ?? '';
220649695abSAndreas Gohr        $osRelease['VERSION'] = $synoVersion['productversion'] ?? '';
221649695abSAndreas Gohr        $osRelease['SYNO_MODEL'] = $synoInfo['upnpmodelname'] ?? '';
222*54e76260SAndreas Gohr        $osRelease['PRETTY_NAME'] = trim(implode(' ', [
223*54e76260SAndreas Gohr            $osRelease['NAME'],
224*54e76260SAndreas Gohr            $osRelease['VERSION'],
225*54e76260SAndreas Gohr            $osRelease['SYNO_MODEL']
226*54e76260SAndreas Gohr        ]));
22779f150bdSAndreas Gohr    }
22879f150bdSAndreas Gohr    return $osRelease;
22979f150bdSAndreas Gohr}
23079f150bdSAndreas Gohr
2310a374113SAndreas Gohr
23279f150bdSAndreas Gohr/**
233c29dc6e4SAndreas Gohr * Run a few sanity checks
234c29dc6e4SAndreas Gohr *
235c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
236c29dc6e4SAndreas Gohr */
237d868eb89SAndreas Gohrfunction check()
238d868eb89SAndreas Gohr{
239c29dc6e4SAndreas Gohr    global $conf;
240c29dc6e4SAndreas Gohr    global $INFO;
241585bf44eSChristopher Smith    /* @var Input $INPUT */
242585bf44eSChristopher Smith    global $INPUT;
243c29dc6e4SAndreas Gohr
2443f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']) {
245c29dc6e4SAndreas Gohr        msg('DokuWiki version: ' . getVersion(), 1);
2468e88a29bSAndreas Gohr        if (version_compare(phpversion(), '8.2.0', '<')) {
2478e88a29bSAndreas Gohr            msg('Your PHP version is too old (' . phpversion() . ' vs. 8.2+ needed)', -1);
248c29dc6e4SAndreas Gohr        } else {
249c29dc6e4SAndreas Gohr            msg('PHP version ' . phpversion(), 1);
250c29dc6e4SAndreas Gohr        }
2518e88a29bSAndreas Gohr    } elseif (version_compare(phpversion(), '8.2.0', '<')) {
25208d1a8dfSAndreas Gohr        msg('Your PHP version is too old', -1);
25308d1a8dfSAndreas Gohr    }
254c6e971ddSAndreas Gohr
25524870174SAndreas Gohr    $mem = php_to_byte(ini_get('memory_limit'));
25673038c47SAndreas Gohr    if ($mem) {
257eb2e46caSAndreas Gohr        if ($mem === -1) {
2588f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
25924870174SAndreas Gohr        } elseif ($mem < 16_777_216) {
2602b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
2612b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
26224870174SAndreas Gohr        } elseif ($mem < 20_971_520) {
2632b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
26464159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
26524870174SAndreas Gohr        } elseif ($mem < 33_554_432) {
2662b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
26764159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
26873038c47SAndreas Gohr        } else {
269c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
27073038c47SAndreas Gohr        }
27173038c47SAndreas Gohr    }
27273038c47SAndreas Gohr
273c29dc6e4SAndreas Gohr    if (is_writable($conf['changelog'])) {
274c29dc6e4SAndreas Gohr        msg('Changelog is writable', 1);
27524870174SAndreas Gohr    } elseif (file_exists($conf['changelog'])) {
276c29dc6e4SAndreas Gohr        msg('Changelog is not writable', -1);
277c29dc6e4SAndreas Gohr    }
278c29dc6e4SAndreas Gohr
27979e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
2802cdbda06SAnika Henke        msg('Old changelog exists', 0);
281c29dc6e4SAndreas Gohr    }
282c29dc6e4SAndreas Gohr
28379e79377SAndreas Gohr    if (file_exists($conf['changelog'] . '_failed')) {
2842cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
28579e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'] . '_importing')) {
286c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
28779e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'] . '_import_ok')) {
2882cdbda06SAnika Henke        msg('Old changelog imported', 1);
289c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
2902cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
291c29dc6e4SAndreas Gohr        }
292c29dc6e4SAndreas Gohr    }
293c29dc6e4SAndreas Gohr
2944c7ecf15SGuy Brand    if (is_writable(DOKU_CONF)) {
2954c7ecf15SGuy Brand        msg('conf directory is writable', 1);
2964c7ecf15SGuy Brand    } else {
2974c7ecf15SGuy Brand        msg('conf directory is not writable', -1);
2984c7ecf15SGuy Brand    }
2994c7ecf15SGuy Brand
3000d487d8fSAndreas Gohr    if ($conf['authtype'] == 'plain') {
301defb7d57SAnika Henke        global $config_cascade;
302defb7d57SAnika Henke        if (is_writable($config_cascade['plainauth.users']['default'])) {
303c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable', 1);
304c29dc6e4SAndreas Gohr        } else {
305c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable', 0);
306c29dc6e4SAndreas Gohr        }
3070d487d8fSAndreas Gohr    }
308c29dc6e4SAndreas Gohr
309c29dc6e4SAndreas Gohr    if (function_exists('mb_strpos')) {
310c29dc6e4SAndreas Gohr        if (defined('UTF8_NOMBSTRING')) {
311c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used', 0);
312c29dc6e4SAndreas Gohr        } else {
313c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used', 1);
314c29dc6e4SAndreas Gohr        }
315c29dc6e4SAndreas Gohr    } else {
316c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used', 0);
317c29dc6e4SAndreas Gohr    }
318c29dc6e4SAndreas Gohr
3193161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
320a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
321a731ed1dSAndreas Gohr    }
3223161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
323a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
324a731ed1dSAndreas Gohr    }
325a731ed1dSAndreas Gohr
326e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
327e5ab313fSAndreas Gohr    if (!$loc) {
328e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this', -1);
329e5ab313fSAndreas Gohr    } elseif (stripos($loc, 'utf') === false) {
33064159a61SAndreas Gohr        msg('Your locale <code>' . hsc($loc) . '</code> seems not to be a UTF-8 locale,
33164159a61SAndreas Gohr             you should fix this if you encounter problems.', 0);
332e5ab313fSAndreas Gohr    } else {
333e5ab313fSAndreas Gohr        msg('Valid locale ' . hsc($loc) . ' found.', 1);
334e5ab313fSAndreas Gohr    }
335e5ab313fSAndreas Gohr
336c29dc6e4SAndreas Gohr    if ($conf['allowdebug']) {
337c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0', -1);
338c29dc6e4SAndreas Gohr    } else {
339c29dc6e4SAndreas Gohr        msg('Debugging support is disabled', 1);
340c29dc6e4SAndreas Gohr    }
341c29dc6e4SAndreas Gohr
342a4231b8cSfiwswe    if (!empty($INFO['userinfo']['name'])) {
34390fb952cSAndreas Gohr        msg(sprintf(
34490fb952cSAndreas Gohr            "You are currently logged in as %s (%s)",
34590fb952cSAndreas Gohr            $INPUT->server->str('REMOTE_USER'),
34690fb952cSAndreas Gohr            $INFO['userinfo']['name']
34790fb952cSAndreas Gohr        ), 0);
3488368419bSSyntaxseed        msg('You are part of the groups ' . implode(', ', $INFO['userinfo']['grps']), 0);
3493d3c095dSMike Frysinger    } else {
3503d3c095dSMike Frysinger        msg('You are currently not logged in', 0);
3513d3c095dSMike Frysinger    }
3523d3c095dSMike Frysinger
353c29dc6e4SAndreas Gohr    msg('Your current permission for this page is ' . $INFO['perm'], 0);
354c29dc6e4SAndreas Gohr
355322ad074SAndreas Gohr    if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) {
356322ad074SAndreas Gohr        msg('The current page is writable by the webserver', 1);
357322ad074SAndreas Gohr    } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) {
358322ad074SAndreas Gohr        msg('The current page can be created by the webserver', 1);
359c29dc6e4SAndreas Gohr    } else {
360322ad074SAndreas Gohr        msg('The current page is not writable by the webserver', -1);
361c29dc6e4SAndreas Gohr    }
362c29dc6e4SAndreas Gohr
363c29dc6e4SAndreas Gohr    if ($INFO['writable']) {
364322ad074SAndreas Gohr        msg('The current page is writable by you', 1);
365c29dc6e4SAndreas Gohr    } else {
366322ad074SAndreas Gohr        msg('The current page is not writable by you', -1);
367c29dc6e4SAndreas Gohr    }
3683b1dfc83SAndreas Gohr
36926f7dbf5SMichael Hamann    // Check for corrupted search index
37021fbd01bSAndreas Gohr    $indexer = new Indexer();
37121fbd01bSAndreas Gohr    try {
37221fbd01bSAndreas Gohr        $indexer->checkIntegrity();
37321fbd01bSAndreas Gohr        if (!$indexer->isIndexEmpty()) {
3743d94d9edSMichael Hamann            msg('The search index seems to be working', 1);
375d6c7b502SAndreas Gohr        } else {
376d6c7b502SAndreas Gohr            msg(
377d6c7b502SAndreas Gohr                'The search index is empty. See
378f5be2fc0SGerrit Uitslag                <a href="https://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
3793d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
380d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
381d6c7b502SAndreas Gohr            );
382d6c7b502SAndreas Gohr        }
3838788dbbdSsplitbrain    } catch (IndexIntegrityException) {
38421fbd01bSAndreas Gohr        msg(
38521fbd01bSAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
38621fbd01bSAndreas Gohr                probably needs to be rebuilt. See
38721fbd01bSAndreas Gohr                <a href="https://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
38821fbd01bSAndreas Gohr                for ways to rebuild the search index.',
38921fbd01bSAndreas Gohr            -1
39021fbd01bSAndreas Gohr        );
39121fbd01bSAndreas Gohr    }
392d6c7b502SAndreas Gohr
393d6c7b502SAndreas Gohr    // rough time check
394d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
395d6c7b502SAndreas Gohr    $http->max_redirect = 0;
396d6c7b502SAndreas Gohr    $http->timeout = 3;
397f5be2fc0SGerrit Uitslag    $http->sendRequest('https://www.dokuwiki.org', '', 'HEAD');
398093fe67eSAndreas Gohr
399d6c7b502SAndreas Gohr    $now = time();
400d6c7b502SAndreas Gohr    if (isset($http->resp_headers['date'])) {
401d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
402d6c7b502SAndreas Gohr        $diff = $time - $now;
403d6c7b502SAndreas Gohr
404d6c7b502SAndreas Gohr        if (abs($diff) < 4) {
405d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
406d6c7b502SAndreas Gohr        } else {
40764159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
40864159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
409d6c7b502SAndreas Gohr        }
410d6c7b502SAndreas Gohr    }
411c29dc6e4SAndreas Gohr}
412c29dc6e4SAndreas Gohr
413c29dc6e4SAndreas Gohr/**
41446028c4cSAndreas Gohr * Display a message to the user
415c29dc6e4SAndreas Gohr *
416c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
417c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
418c29dc6e4SAndreas Gohr * using html_msgarea()
419c29dc6e4SAndreas Gohr *
4200e20480fSPhy * Triggers INFOUTIL_MSG_SHOW
4210e20480fSPhy *
4226164d900SAndreas Gohr * @param string $message
4236164d900SAndreas Gohr * @param int $lvl -1 = error, 0 = info, 1 = success, 2 = notify
4246164d900SAndreas Gohr * @param string $line line number
4256164d900SAndreas Gohr * @param string $file file number
4266164d900SAndreas Gohr * @param int $allow who's allowed to see the message, see MSG_* constants
427f5be2fc0SGerrit Uitslag * @see html_msgarea()
4286164d900SAndreas Gohr */
429d868eb89SAndreas Gohrfunction msg($message, $lvl = 0, $line = '', $file = '', $allow = MSG_PUBLIC)
430d868eb89SAndreas Gohr{
431cc58224cSMichael Hamann    global $MSG, $MSG_shown;
432556e996eSAndreas Gohr    static $errors = [
433556e996eSAndreas Gohr        -1 => 'error',
434556e996eSAndreas Gohr        0 => 'info',
435556e996eSAndreas Gohr        1 => 'success',
436556e996eSAndreas Gohr        2 => 'notify',
437556e996eSAndreas Gohr    ];
438c29dc6e4SAndreas Gohr
439556e996eSAndreas Gohr    $msgdata = [
4400e20480fSPhy        'msg' => $message,
441556e996eSAndreas Gohr        'lvl' => $errors[$lvl],
4420e20480fSPhy        'allow' => $allow,
4430e20480fSPhy        'line' => $line,
4440e20480fSPhy        'file' => $file,
445556e996eSAndreas Gohr    ];
4460e20480fSPhy
44724870174SAndreas Gohr    $evt = new Event('INFOUTIL_MSG_SHOW', $msgdata);
4480e20480fSPhy    if ($evt->advise_before()) {
4490e20480fSPhy        /* Show msg normally - event could suppress message show */
4500e20480fSPhy        if ($msgdata['line'] || $msgdata['file']) {
45124870174SAndreas Gohr            $basename = PhpString::basename($msgdata['file']);
4520e20480fSPhy            $msgdata['msg'] .= ' [' . $basename . ':' . $msgdata['line'] . ']';
4530e20480fSPhy        }
454c29dc6e4SAndreas Gohr
45524870174SAndreas Gohr        if (!isset($MSG)) $MSG = [];
4560e20480fSPhy        $MSG[] = $msgdata;
457cc58224cSMichael Hamann        if (isset($MSG_shown) || headers_sent()) {
458c29dc6e4SAndreas Gohr            if (function_exists('html_msgarea')) {
459c29dc6e4SAndreas Gohr                html_msgarea();
460c29dc6e4SAndreas Gohr            } else {
46126dfc232SAndreas Gohr                echo "ERROR(" . $msgdata['lvl'] . ") " . $msgdata['msg'] . "\n";
462c29dc6e4SAndreas Gohr            }
46369266de5SDominik Eckelmann            unset($GLOBALS['MSG']);
464c29dc6e4SAndreas Gohr        }
465c29dc6e4SAndreas Gohr    }
4660e20480fSPhy    $evt->advise_after();
4670e20480fSPhy    unset($evt);
4680e20480fSPhy}
469f5be2fc0SGerrit Uitslag
470f755f9abSChristopher Smith/**
471f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
472f755f9abSChristopher Smith * in the $msg data structure
473f755f9abSChristopher Smith *
474f5be2fc0SGerrit Uitslag * @param array $msg dokuwiki msg structure:
475f5be2fc0SGerrit Uitslag *              msg   => string, the message;
476f5be2fc0SGerrit Uitslag *              lvl   => int, level of the message (see msg() function);
477f5be2fc0SGerrit Uitslag *              allow => int, flag used to determine who is allowed to see the message, see MSG_* constants
4786164d900SAndreas Gohr * @return bool
479f755f9abSChristopher Smith */
480d868eb89SAndreas Gohrfunction info_msg_allowed($msg)
481d868eb89SAndreas Gohr{
482d3bae478SChristopher Smith    global $INFO, $auth;
483d3bae478SChristopher Smith
484d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
485f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
486d3bae478SChristopher Smith
487d3bae478SChristopher Smith    // restricted msg, but no authentication
4886547cfc7SGerrit Uitslag    if (!$auth instanceof AuthPlugin) return false;
489d3bae478SChristopher Smith
490f755f9abSChristopher Smith    switch ($msg['allow']) {
491d3bae478SChristopher Smith        case MSG_USERS_ONLY:
492d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
493d3bae478SChristopher Smith
494d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
495d3bae478SChristopher Smith            return $INFO['ismanager'];
496d3bae478SChristopher Smith
497d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
498d3bae478SChristopher Smith            return $INFO['isadmin'];
499d3bae478SChristopher Smith
500d3bae478SChristopher Smith        default:
501dccd6b2bSAndreas Gohr            trigger_error(
502dccd6b2bSAndreas Gohr                'invalid msg allow restriction.  msg="' . $msg['msg'] . '" allow=' . $msg['allow'] . '"',
503dccd6b2bSAndreas Gohr                E_USER_WARNING
504dccd6b2bSAndreas Gohr            );
505d3bae478SChristopher Smith            return $INFO['isadmin'];
506d3bae478SChristopher Smith    }
507d3bae478SChristopher Smith}
508d3bae478SChristopher Smith
509c29dc6e4SAndreas Gohr/**
510c29dc6e4SAndreas Gohr * print debug messages
511c29dc6e4SAndreas Gohr *
512c29dc6e4SAndreas Gohr * little function to print the content of a var
513c29dc6e4SAndreas Gohr *
514f50a239bSTakamura * @param string $msg
515f50a239bSTakamura * @param bool $hidden
516f5be2fc0SGerrit Uitslag *
517f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
518c29dc6e4SAndreas Gohr */
519d868eb89SAndreas Gohrfunction dbg($msg, $hidden = false)
520d868eb89SAndreas Gohr{
52113493794SAndreas Gohr    if ($hidden) {
52213493794SAndreas Gohr        echo "<!--\n";
523c29dc6e4SAndreas Gohr        print_r($msg);
52413493794SAndreas Gohr        echo "\n-->";
52513493794SAndreas Gohr    } else {
52613493794SAndreas Gohr        echo '<pre class="dbg">';
52713493794SAndreas Gohr        echo hsc(print_r($msg, true));
52813493794SAndreas Gohr        echo '</pre>';
52913493794SAndreas Gohr    }
530c29dc6e4SAndreas Gohr}
531c29dc6e4SAndreas Gohr
532c29dc6e4SAndreas Gohr/**
533cad4fbf6SAndreas Gohr * Print info to debug log file
534c29dc6e4SAndreas Gohr *
535f50a239bSTakamura * @param string $msg
536f50a239bSTakamura * @param string $header
537f5be2fc0SGerrit Uitslag *
538f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
539f5be2fc0SGerrit Uitslag * @deprecated 2020-08-13
540c29dc6e4SAndreas Gohr */
541d868eb89SAndreas Gohrfunction dbglog($msg, $header = '')
542d868eb89SAndreas Gohr{
5430ecde6ceSAndreas Gohr    dbg_deprecated('\\dokuwiki\\Logger');
544585bf44eSChristopher Smith
5450ecde6ceSAndreas Gohr    // was the msg as single line string? use it as header
546093fe67eSAndreas Gohr    if ($header === '' && is_string($msg) && !str_contains($msg, "\n")) {
5470ecde6ceSAndreas Gohr        $header = $msg;
5480ecde6ceSAndreas Gohr        $msg = '';
549c7408a63SAndreas Gohr    }
550c7408a63SAndreas Gohr
55131667ec6SAndreas Gohr    Logger::getInstance(Logger::LOG_DEBUG)->log(
552dccd6b2bSAndreas Gohr        $header,
553dccd6b2bSAndreas Gohr        $msg
5540ecde6ceSAndreas Gohr    );
555c29dc6e4SAndreas Gohr}
556c29dc6e4SAndreas Gohr
557db09e31eSAndreas Gohr/**
5581419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
5591419a485SAndreas Gohr *
5601419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
56185331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
5621419a485SAndreas Gohr */
563d868eb89SAndreas Gohrfunction dbg_deprecated($alternative = '')
564d868eb89SAndreas Gohr{
56524870174SAndreas Gohr    DebugHelper::dbgDeprecatedFunction($alternative, 2);
56644455016SAndreas Gohr}
5671419a485SAndreas Gohr
5681419a485SAndreas Gohr/**
569db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
570db09e31eSAndreas Gohr *
571db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
572db09e31eSAndreas Gohr */
573d868eb89SAndreas Gohrfunction dbg_backtrace()
574d868eb89SAndreas Gohr{
575db09e31eSAndreas Gohr    // Get backtrace
576db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
577db09e31eSAndreas Gohr
578db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
579db09e31eSAndreas Gohr    array_shift($backtrace);
580db09e31eSAndreas Gohr
581db09e31eSAndreas Gohr    // Iterate backtrace
58224870174SAndreas Gohr    $calls = [];
583db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
584db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
585c0f08ea9SAndreas Gohr        if (isset($call['file'])) {
586c0f08ea9SAndreas Gohr            $location = $call['file'] . ':' . ($call['line'] ?? '0');
587c0f08ea9SAndreas Gohr        } else {
588c0f08ea9SAndreas Gohr            $location = '[anonymous]';
589c0f08ea9SAndreas Gohr        }
590c0f08ea9SAndreas Gohr        if (isset($call['class'])) {
591c0f08ea9SAndreas Gohr            $function = $call['class'] . $call['type'] . $call['function'];
592c0f08ea9SAndreas Gohr        } else {
593c0f08ea9SAndreas Gohr            $function = $call['function'];
594c0f08ea9SAndreas Gohr        }
595db09e31eSAndreas Gohr
59624870174SAndreas Gohr        $params = [];
597db09e31eSAndreas Gohr        if (isset($call['args'])) {
5988259f1aaSAndreas Gohr            foreach ($call['args'] as $arg) {
5998259f1aaSAndreas Gohr                if (is_object($arg)) {
600093fe67eSAndreas Gohr                    $params[] = '[Object ' . $arg::class . ']';
6018259f1aaSAndreas Gohr                } elseif (is_array($arg)) {
6028259f1aaSAndreas Gohr                    $params[] = '[Array]';
6038259f1aaSAndreas Gohr                } elseif (is_null($arg)) {
60459bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
6058259f1aaSAndreas Gohr                } else {
60624870174SAndreas Gohr                    $params[] = '"' . $arg . '"';
607db09e31eSAndreas Gohr                }
6088259f1aaSAndreas Gohr            }
6098259f1aaSAndreas Gohr        }
6108259f1aaSAndreas Gohr        $params = implode(', ', $params);
611db09e31eSAndreas Gohr
612dccd6b2bSAndreas Gohr        $calls[$depth - $i] = sprintf(
613dccd6b2bSAndreas Gohr            '%s(%s) called at %s',
614db09e31eSAndreas Gohr            $function,
615db09e31eSAndreas Gohr            str_replace("\n", '\n', $params),
616dccd6b2bSAndreas Gohr            $location
617dccd6b2bSAndreas Gohr        );
618db09e31eSAndreas Gohr    }
619db09e31eSAndreas Gohr    ksort($calls);
620db09e31eSAndreas Gohr
621db09e31eSAndreas Gohr    return implode("\n", $calls);
622db09e31eSAndreas Gohr}
623db09e31eSAndreas Gohr
62424297a69SAndreas Gohr/**
62524297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
62624297a69SAndreas Gohr *
62724297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
62824297a69SAndreas Gohr * debug output
62924297a69SAndreas Gohr *
630f50a239bSTakamura * @param array $data
631f5be2fc0SGerrit Uitslag *
632f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
63324297a69SAndreas Gohr */
634d868eb89SAndreas Gohrfunction debug_guard(&$data)
635d868eb89SAndreas Gohr{
63624297a69SAndreas Gohr    foreach ($data as $key => $value) {
63724297a69SAndreas Gohr        if (preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i', $key)) {
63824297a69SAndreas Gohr            $data[$key] = '***';
63924297a69SAndreas Gohr            continue;
64024297a69SAndreas Gohr        }
64124297a69SAndreas Gohr        if (is_array($value)) debug_guard($data[$key]);
64224297a69SAndreas Gohr    }
64324297a69SAndreas Gohr}
644