xref: /dokuwiki/inc/infoutils.php (revision 27bd777138af595d8b6398b9891b82765360a860)
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;
1579f150bdSAndreas Gohruse dokuwiki\Utf8\PhpString;
16198564abSMichael Große
176c5e3c5eSPhyif (!defined('DOKU_MESSAGEURL')) {
186c5e3c5eSPhy    if (in_array('ssl', stream_get_transports())) {
196c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'https://update.dokuwiki.org/check/');
206c5e3c5eSPhy    } else {
216c5e3c5eSPhy        define('DOKU_MESSAGEURL', 'http://update.dokuwiki.org/check/');
226c5e3c5eSPhy    }
236c5e3c5eSPhy}
24c29dc6e4SAndreas Gohr
25c29dc6e4SAndreas Gohr/**
26c29dc6e4SAndreas Gohr * Check for new messages from upstream
27c29dc6e4SAndreas Gohr *
28c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
29c29dc6e4SAndreas Gohr */
30d868eb89SAndreas Gohrfunction checkUpdateMessages()
31d868eb89SAndreas Gohr{
32c29dc6e4SAndreas Gohr    global $conf;
33c29dc6e4SAndreas Gohr    global $INFO;
34ef362bb8SAnika Henke    global $updateVersion;
35c29dc6e4SAndreas Gohr    if (!$conf['updatecheck']) return;
36f8cc712eSAndreas Gohr    if ($conf['useacl'] && !$INFO['ismanager']) return;
37c29dc6e4SAndreas Gohr
3837b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
39c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
406c16a3a9Sfiwswe    $is_http = !str_starts_with(DOKU_MESSAGEURL, 'https');
41c29dc6e4SAndreas Gohr
42c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
438d3d7569SAnika Henke    if ($lm < time() - (60 * 60 * 24) || $lm < @filemtime(DOKU_INC . DOKU_SCRIPT)) {
4463d9b820SAndreas Gohr        @touch($cf);
4590fb952cSAndreas Gohr        Logger::debug(
4690fb952cSAndreas Gohr            sprintf(
4790fb952cSAndreas Gohr                'checkUpdateMessages(): downloading messages to %s%s',
4890fb952cSAndreas Gohr                $cf,
4990fb952cSAndreas Gohr                $is_http ? ' (without SSL)' : ' (with SSL)'
5090fb952cSAndreas Gohr            )
5190fb952cSAndreas Gohr        );
52c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
5363d9b820SAndreas Gohr        $http->timeout = 12;
5486c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL . $updateVersion);
556c16a3a9Sfiwswe        if (is_string($resp) && ($resp == '' || str_ends_with(trim($resp), '%'))) {
5686c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
5786c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
5886c04d87SAngus Gratton            io_saveFile($cf, $resp);
598f1efc43SAndreas Gohr        } else {
6031667ec6SAndreas Gohr            Logger::debug("checkUpdateMessages(): unexpected HTTP response received", $http->error);
618f1efc43SAndreas Gohr        }
62c29dc6e4SAndreas Gohr    } else {
6331667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): messages up to date");
64c29dc6e4SAndreas Gohr    }
65c29dc6e4SAndreas Gohr
6686c04d87SAngus Gratton    $data = io_readFile($cf);
67c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
68c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n", $data);
69c29dc6e4SAndreas Gohr    foreach ($msgs as $msg) {
70c29dc6e4SAndreas Gohr        if ($msg) msg($msg, 2);
71c29dc6e4SAndreas Gohr    }
72c29dc6e4SAndreas Gohr}
73c29dc6e4SAndreas Gohr
74c29dc6e4SAndreas Gohr
75c29dc6e4SAndreas Gohr/**
7625c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
77c29dc6e4SAndreas Gohr *
78c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
79c29dc6e4SAndreas Gohr */
80d868eb89SAndreas Gohrfunction getVersionData()
81d868eb89SAndreas Gohr{
8224870174SAndreas Gohr    $version = [];
83c29dc6e4SAndreas Gohr    //import version string
8479e79377SAndreas Gohr    if (file_exists(DOKU_INC . 'VERSION')) {
85c29dc6e4SAndreas Gohr        //official release
86d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC . 'VERSION'));
8725c07f93SAnika Henke        $version['type'] = 'Release';
885cf31920SAndreas Gohr    } elseif (is_dir(DOKU_INC . '.git')) {
895cf31920SAndreas Gohr        $version['type'] = 'Git';
9025c07f93SAnika Henke        $version['date'] = 'unknown';
91e570ed43SAndreas Gohr
92b9f5205aSDamien Regad        // First try to get date and commit hash by calling Git
93b6f8a5c6SDamien Regad        if (function_exists('shell_exec')) {
94b9e35b2fSAndreas Gohr            $args = ['git', 'log', '-1', '--pretty=format:%h %cd', '--date=short'];
95*27bd7771Ssplitbrain            $commitInfo = shell_exec(implode(' ', array_map(escapeshellarg(...), $args)));
96b9f5205aSDamien Regad            if ($commitInfo) {
9724870174SAndreas Gohr                [$version['sha'], $date] = explode(' ', $commitInfo);
98067b4fb9SMichael Große                $version['date'] = hsc($date);
99b9f5205aSDamien Regad                return $version;
100b9f5205aSDamien Regad            }
101b9f5205aSDamien Regad        }
102b9f5205aSDamien Regad
103f519f9dbSMichael Große        // we cannot use git on the shell -- let's do it manually!
104b9f5205aSDamien Regad        if (file_exists(DOKU_INC . '.git/HEAD')) {
105f519f9dbSMichael Große            $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD'));
106093fe67eSAndreas Gohr            if (str_starts_with($headCommit, 'ref: ')) {
107f519f9dbSMichael Große                // it is something like `ref: refs/heads/master`
10809bf5d22SDamien Regad                $headCommit = substr($headCommit, 5);
10909bf5d22SDamien Regad                $pathToHead = DOKU_INC . '.git/' . $headCommit;
11009bf5d22SDamien Regad                if (file_exists($pathToHead)) {
11109bf5d22SDamien Regad                    $headCommit = trim(file_get_contents($pathToHead));
11209bf5d22SDamien Regad                } else {
11309bf5d22SDamien Regad                    $packedRefs = file_get_contents(DOKU_INC . '.git/packed-refs');
11409bf5d22SDamien Regad                    if (!preg_match("~([[:xdigit:]]+) $headCommit~", $packedRefs, $matches)) {
11509bf5d22SDamien Regad                        # ref not found in pack file
11609bf5d22SDamien Regad                        return $version;
117f519f9dbSMichael Große                    }
11809bf5d22SDamien Regad                    $headCommit = $matches[1];
11909bf5d22SDamien Regad                }
12009bf5d22SDamien Regad            }
12109bf5d22SDamien Regad            // At this point $headCommit is a SHA
122b6f8a5c6SDamien Regad            $version['sha'] = $headCommit;
123b6f8a5c6SDamien Regad
12409bf5d22SDamien Regad            // Get commit date from Git object
125f519f9dbSMichael Große            $subDir = substr($headCommit, 0, 2);
126f519f9dbSMichael Große            $fileName = substr($headCommit, 2);
127be2e462dSMichael Große            $gitCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName";
128a8ac20eaSGerrit Uitslag            if (file_exists($gitCommitObject) && function_exists('zlib_decode')) {
129be2e462dSMichael Große                $commit = zlib_decode(file_get_contents($gitCommitObject));
130f519f9dbSMichael Große                $committerLine = explode("\n", $commit)[3];
131f519f9dbSMichael Große                $committerData = explode(' ', $committerLine);
132f519f9dbSMichael Große                end($committerData);
133f519f9dbSMichael Große                $ts = prev($committerData);
134f519f9dbSMichael Große                if ($ts && $date = date('Y-m-d', $ts)) {
135f519f9dbSMichael Große                    $version['date'] = $date;
136f519f9dbSMichael Große                }
1375cf31920SAndreas Gohr            }
13825c07f93SAnika Henke        }
139c29dc6e4SAndreas Gohr    } else {
1406a34de2dSAnika Henke        global $updateVersion;
1416a34de2dSAnika Henke        $version['date'] = 'update version ' . $updateVersion;
14225c07f93SAnika Henke        $version['type'] = 'snapshot?';
143c29dc6e4SAndreas Gohr    }
1445cf31920SAndreas Gohr    return $version;
145c29dc6e4SAndreas Gohr}
146c29dc6e4SAndreas Gohr
147c29dc6e4SAndreas Gohr/**
148fe9f11e2SAndreas Gohr * Return DokuWiki's version
149fe9f11e2SAndreas Gohr *
150fe9f11e2SAndreas Gohr * This returns the version in the form "Type Date (SHA)". Where type is either
151fe9f11e2SAndreas Gohr * "Release" or "Git" and date is the date of the release or the date of the
152fe9f11e2SAndreas Gohr * last commit. SHA is the short SHA of the last commit - this is only added on
153fe9f11e2SAndreas Gohr * git checkouts.
154fe9f11e2SAndreas Gohr *
155fe9f11e2SAndreas Gohr * If no version can be determined "snapshot? update version XX" is returned.
156fe9f11e2SAndreas Gohr * Where XX represents the update version number set in doku.php.
157fe9f11e2SAndreas Gohr *
158fe9f11e2SAndreas Gohr * @return string The version string e.g. "Release 2023-04-04a"
15979f150bdSAndreas Gohr * @author Anika Henke <anika@selfthinker.org>
16025c07f93SAnika Henke */
161d868eb89SAndreas Gohrfunction getVersion()
162d868eb89SAndreas Gohr{
16325c07f93SAnika Henke    $version = getVersionData();
16424870174SAndreas Gohr    $sha = empty($version['sha']) ? '' : ' (' . $version['sha'] . ')';
165b6f8a5c6SDamien Regad    return $version['type'] . ' ' . $version['date'] . $sha;
16625c07f93SAnika Henke}
16725c07f93SAnika Henke
16825c07f93SAnika Henke/**
16979f150bdSAndreas Gohr * Get some data about the environment this wiki is running in
17079f150bdSAndreas Gohr *
17179f150bdSAndreas Gohr * @return array
17279f150bdSAndreas Gohr */
17379f150bdSAndreas Gohrfunction getRuntimeVersions()
17479f150bdSAndreas Gohr{
17579f150bdSAndreas Gohr    $data = [];
17679f150bdSAndreas Gohr    $data['php'] = 'PHP ' . PHP_VERSION;
17779f150bdSAndreas Gohr
17879f150bdSAndreas Gohr    $osRelease = getOsRelease();
17979f150bdSAndreas Gohr    if (isset($osRelease['PRETTY_NAME'])) {
18079f150bdSAndreas Gohr        $data['dist'] = $osRelease['PRETTY_NAME'];
18179f150bdSAndreas Gohr    }
18279f150bdSAndreas Gohr
18379f150bdSAndreas Gohr    $data['os'] = php_uname('s') . ' ' . php_uname('r');
18479f150bdSAndreas Gohr    $data['sapi'] = PHP_SAPI;
18579f150bdSAndreas Gohr
18679f150bdSAndreas Gohr    if (getenv('KUBERNETES_SERVICE_HOST')) {
18779f150bdSAndreas Gohr        $data['container'] = 'Kubernetes';
1880a374113SAndreas Gohr    } elseif (@file_exists('/.dockerenv')) {
18979f150bdSAndreas Gohr        $data['container'] = 'Docker';
19079f150bdSAndreas Gohr    }
19179f150bdSAndreas Gohr
19279f150bdSAndreas Gohr    return $data;
19379f150bdSAndreas Gohr}
19479f150bdSAndreas Gohr
19579f150bdSAndreas Gohr/**
19679f150bdSAndreas Gohr * Get informational data about the linux distribution this wiki is running on
19779f150bdSAndreas Gohr *
19879f150bdSAndreas Gohr * @see https://gist.github.com/natefoo/814c5bf936922dad97ff
19979f150bdSAndreas Gohr * @return array an os-release array, might be empty
20079f150bdSAndreas Gohr */
20179f150bdSAndreas Gohrfunction getOsRelease()
20279f150bdSAndreas Gohr{
2030a374113SAndreas Gohr    $reader = fn($file) => @parse_ini_string(preg_replace('/#.*$/m', '', file_get_contents($file)));
2040a374113SAndreas Gohr
20579f150bdSAndreas Gohr    $osRelease = [];
2060a374113SAndreas Gohr    if (@file_exists('/etc/os-release')) {
20779f150bdSAndreas Gohr        // pretty much any common Linux distribution has this
2080a374113SAndreas Gohr        $osRelease = $reader('/etc/os-release');
2090a374113SAndreas Gohr    } elseif (@file_exists('/etc/synoinfo.conf') && @file_exists('/etc/VERSION')) {
21079f150bdSAndreas Gohr        // Synology DSM has its own way
2110a374113SAndreas Gohr        $synoInfo = $reader('/etc/synoinfo.conf');
2120a374113SAndreas Gohr        $synoVersion = $reader('/etc/VERSION');
21379f150bdSAndreas Gohr        $osRelease['NAME'] = 'Synology DSM';
21479f150bdSAndreas Gohr        $osRelease['ID'] = 'synology';
21579f150bdSAndreas Gohr        $osRelease['ID_LIKE'] = 'linux';
21679f150bdSAndreas Gohr        $osRelease['VERSION_ID'] = $synoVersion['productversion'];
21779f150bdSAndreas Gohr        $osRelease['VERSION'] = $synoVersion['productversion'];
21879f150bdSAndreas Gohr        $osRelease['SYNO_MODEL'] = $synoInfo['upnpmodelname'];
219dafc9892Ssplitbrain        $osRelease['PRETTY_NAME'] = implode(' ', [$osRelease['NAME'], $osRelease['VERSION'], $osRelease['SYNO_MODEL']]);
22079f150bdSAndreas Gohr    }
22179f150bdSAndreas Gohr    return $osRelease;
22279f150bdSAndreas Gohr}
22379f150bdSAndreas Gohr
2240a374113SAndreas Gohr
22579f150bdSAndreas Gohr/**
226c29dc6e4SAndreas Gohr * Run a few sanity checks
227c29dc6e4SAndreas Gohr *
228c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
229c29dc6e4SAndreas Gohr */
230d868eb89SAndreas Gohrfunction check()
231d868eb89SAndreas Gohr{
232c29dc6e4SAndreas Gohr    global $conf;
233c29dc6e4SAndreas Gohr    global $INFO;
234585bf44eSChristopher Smith    /* @var Input $INPUT */
235585bf44eSChristopher Smith    global $INPUT;
236c29dc6e4SAndreas Gohr
2373f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']) {
238c29dc6e4SAndreas Gohr        msg('DokuWiki version: ' . getVersion(), 1);
2398e88a29bSAndreas Gohr        if (version_compare(phpversion(), '8.2.0', '<')) {
2408e88a29bSAndreas Gohr            msg('Your PHP version is too old (' . phpversion() . ' vs. 8.2+ needed)', -1);
241c29dc6e4SAndreas Gohr        } else {
242c29dc6e4SAndreas Gohr            msg('PHP version ' . phpversion(), 1);
243c29dc6e4SAndreas Gohr        }
2448e88a29bSAndreas Gohr    } elseif (version_compare(phpversion(), '8.2.0', '<')) {
24508d1a8dfSAndreas Gohr        msg('Your PHP version is too old', -1);
24608d1a8dfSAndreas Gohr    }
247c6e971ddSAndreas Gohr
24824870174SAndreas Gohr    $mem = php_to_byte(ini_get('memory_limit'));
24973038c47SAndreas Gohr    if ($mem) {
250eb2e46caSAndreas Gohr        if ($mem === -1) {
2518f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
25224870174SAndreas Gohr        } elseif ($mem < 16_777_216) {
2532b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
2542b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
25524870174SAndreas Gohr        } elseif ($mem < 20_971_520) {
2562b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
25764159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
25824870174SAndreas Gohr        } elseif ($mem < 33_554_432) {
2592b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
26064159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
26173038c47SAndreas Gohr        } else {
262c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
26373038c47SAndreas Gohr        }
26473038c47SAndreas Gohr    }
26573038c47SAndreas Gohr
266c29dc6e4SAndreas Gohr    if (is_writable($conf['changelog'])) {
267c29dc6e4SAndreas Gohr        msg('Changelog is writable', 1);
26824870174SAndreas Gohr    } elseif (file_exists($conf['changelog'])) {
269c29dc6e4SAndreas Gohr        msg('Changelog is not writable', -1);
270c29dc6e4SAndreas Gohr    }
271c29dc6e4SAndreas Gohr
27279e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
2732cdbda06SAnika Henke        msg('Old changelog exists', 0);
274c29dc6e4SAndreas Gohr    }
275c29dc6e4SAndreas Gohr
27679e79377SAndreas Gohr    if (file_exists($conf['changelog'] . '_failed')) {
2772cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
27879e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'] . '_importing')) {
279c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
28079e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'] . '_import_ok')) {
2812cdbda06SAnika Henke        msg('Old changelog imported', 1);
282c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
2832cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
284c29dc6e4SAndreas Gohr        }
285c29dc6e4SAndreas Gohr    }
286c29dc6e4SAndreas Gohr
2874c7ecf15SGuy Brand    if (is_writable(DOKU_CONF)) {
2884c7ecf15SGuy Brand        msg('conf directory is writable', 1);
2894c7ecf15SGuy Brand    } else {
2904c7ecf15SGuy Brand        msg('conf directory is not writable', -1);
2914c7ecf15SGuy Brand    }
2924c7ecf15SGuy Brand
2930d487d8fSAndreas Gohr    if ($conf['authtype'] == 'plain') {
294defb7d57SAnika Henke        global $config_cascade;
295defb7d57SAnika Henke        if (is_writable($config_cascade['plainauth.users']['default'])) {
296c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable', 1);
297c29dc6e4SAndreas Gohr        } else {
298c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable', 0);
299c29dc6e4SAndreas Gohr        }
3000d487d8fSAndreas Gohr    }
301c29dc6e4SAndreas Gohr
302c29dc6e4SAndreas Gohr    if (function_exists('mb_strpos')) {
303c29dc6e4SAndreas Gohr        if (defined('UTF8_NOMBSTRING')) {
304c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used', 0);
305c29dc6e4SAndreas Gohr        } else {
306c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used', 1);
307c29dc6e4SAndreas Gohr        }
308c29dc6e4SAndreas Gohr    } else {
309c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used', 0);
310c29dc6e4SAndreas Gohr    }
311c29dc6e4SAndreas Gohr
3123161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
313a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
314a731ed1dSAndreas Gohr    }
3153161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
316a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
317a731ed1dSAndreas Gohr    }
318a731ed1dSAndreas Gohr
319e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
320e5ab313fSAndreas Gohr    if (!$loc) {
321e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this', -1);
322e5ab313fSAndreas Gohr    } elseif (stripos($loc, 'utf') === false) {
32364159a61SAndreas Gohr        msg('Your locale <code>' . hsc($loc) . '</code> seems not to be a UTF-8 locale,
32464159a61SAndreas Gohr             you should fix this if you encounter problems.', 0);
325e5ab313fSAndreas Gohr    } else {
326e5ab313fSAndreas Gohr        msg('Valid locale ' . hsc($loc) . ' found.', 1);
327e5ab313fSAndreas Gohr    }
328e5ab313fSAndreas Gohr
329c29dc6e4SAndreas Gohr    if ($conf['allowdebug']) {
330c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0', -1);
331c29dc6e4SAndreas Gohr    } else {
332c29dc6e4SAndreas Gohr        msg('Debugging support is disabled', 1);
333c29dc6e4SAndreas Gohr    }
334c29dc6e4SAndreas Gohr
335a4231b8cSfiwswe    if (!empty($INFO['userinfo']['name'])) {
33690fb952cSAndreas Gohr        msg(sprintf(
33790fb952cSAndreas Gohr            "You are currently logged in as %s (%s)",
33890fb952cSAndreas Gohr            $INPUT->server->str('REMOTE_USER'),
33990fb952cSAndreas Gohr            $INFO['userinfo']['name']
34090fb952cSAndreas Gohr        ), 0);
3418368419bSSyntaxseed        msg('You are part of the groups ' . implode(', ', $INFO['userinfo']['grps']), 0);
3423d3c095dSMike Frysinger    } else {
3433d3c095dSMike Frysinger        msg('You are currently not logged in', 0);
3443d3c095dSMike Frysinger    }
3453d3c095dSMike Frysinger
346c29dc6e4SAndreas Gohr    msg('Your current permission for this page is ' . $INFO['perm'], 0);
347c29dc6e4SAndreas Gohr
348322ad074SAndreas Gohr    if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) {
349322ad074SAndreas Gohr        msg('The current page is writable by the webserver', 1);
350322ad074SAndreas Gohr    } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) {
351322ad074SAndreas Gohr        msg('The current page can be created by the webserver', 1);
352c29dc6e4SAndreas Gohr    } else {
353322ad074SAndreas Gohr        msg('The current page is not writable by the webserver', -1);
354c29dc6e4SAndreas Gohr    }
355c29dc6e4SAndreas Gohr
356c29dc6e4SAndreas Gohr    if ($INFO['writable']) {
357322ad074SAndreas Gohr        msg('The current page is writable by you', 1);
358c29dc6e4SAndreas Gohr    } else {
359322ad074SAndreas Gohr        msg('The current page is not writable by you', -1);
360c29dc6e4SAndreas Gohr    }
3613b1dfc83SAndreas Gohr
36226f7dbf5SMichael Hamann    // Check for corrupted search index
36326f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
36426f7dbf5SMichael Hamann    $index_corrupted = false;
36526f7dbf5SMichael Hamann    foreach ($lengths as $length) {
36624870174SAndreas Gohr        if (count(idx_getIndex('w', $length)) !== count(idx_getIndex('i', $length))) {
36726f7dbf5SMichael Hamann            $index_corrupted = true;
36826f7dbf5SMichael Hamann            break;
36926f7dbf5SMichael Hamann        }
37026f7dbf5SMichael Hamann    }
37126f7dbf5SMichael Hamann
37226f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
37324870174SAndreas Gohr        if (count(idx_getIndex($index . '_w', '')) !== count(idx_getIndex($index . '_i', ''))) {
37426f7dbf5SMichael Hamann            $index_corrupted = true;
37526f7dbf5SMichael Hamann            break;
37626f7dbf5SMichael Hamann        }
37726f7dbf5SMichael Hamann    }
37826f7dbf5SMichael Hamann
379d6c7b502SAndreas Gohr    if ($index_corrupted) {
380d6c7b502SAndreas Gohr        msg(
381d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
3823d94d9edSMichael Hamann                probably needs to be rebuilt. See
383f5be2fc0SGerrit Uitslag                <a href="https://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
384dccd6b2bSAndreas Gohr                for ways to rebuild the search index.',
385dccd6b2bSAndreas Gohr            -1
386d6c7b502SAndreas Gohr        );
387d6c7b502SAndreas Gohr    } elseif (!empty($lengths)) {
3883d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
389d6c7b502SAndreas Gohr    } else {
390d6c7b502SAndreas Gohr        msg(
391d6c7b502SAndreas Gohr            'The search index is empty. See
392f5be2fc0SGerrit Uitslag                <a href="https://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
3933d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
394d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
395d6c7b502SAndreas Gohr        );
396d6c7b502SAndreas Gohr    }
397d6c7b502SAndreas Gohr
398d6c7b502SAndreas Gohr    // rough time check
399d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
400d6c7b502SAndreas Gohr    $http->max_redirect = 0;
401d6c7b502SAndreas Gohr    $http->timeout = 3;
402f5be2fc0SGerrit Uitslag    $http->sendRequest('https://www.dokuwiki.org', '', 'HEAD');
403093fe67eSAndreas Gohr
404d6c7b502SAndreas Gohr    $now = time();
405d6c7b502SAndreas Gohr    if (isset($http->resp_headers['date'])) {
406d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
407d6c7b502SAndreas Gohr        $diff = $time - $now;
408d6c7b502SAndreas Gohr
409d6c7b502SAndreas Gohr        if (abs($diff) < 4) {
410d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
411d6c7b502SAndreas Gohr        } else {
41264159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
41364159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
414d6c7b502SAndreas Gohr        }
415d6c7b502SAndreas Gohr    }
416c29dc6e4SAndreas Gohr}
417c29dc6e4SAndreas Gohr
418c29dc6e4SAndreas Gohr/**
41946028c4cSAndreas Gohr * Display a message to the user
420c29dc6e4SAndreas Gohr *
421c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
422c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
423c29dc6e4SAndreas Gohr * using html_msgarea()
424c29dc6e4SAndreas Gohr *
4250e20480fSPhy * Triggers INFOUTIL_MSG_SHOW
4260e20480fSPhy *
4276164d900SAndreas Gohr * @param string $message
4286164d900SAndreas Gohr * @param int $lvl -1 = error, 0 = info, 1 = success, 2 = notify
4296164d900SAndreas Gohr * @param string $line line number
4306164d900SAndreas Gohr * @param string $file file number
4316164d900SAndreas Gohr * @param int $allow who's allowed to see the message, see MSG_* constants
432f5be2fc0SGerrit Uitslag * @see html_msgarea()
4336164d900SAndreas Gohr */
434d868eb89SAndreas Gohrfunction msg($message, $lvl = 0, $line = '', $file = '', $allow = MSG_PUBLIC)
435d868eb89SAndreas Gohr{
436cc58224cSMichael Hamann    global $MSG, $MSG_shown;
437556e996eSAndreas Gohr    static $errors = [
438556e996eSAndreas Gohr        -1 => 'error',
439556e996eSAndreas Gohr        0 => 'info',
440556e996eSAndreas Gohr        1 => 'success',
441556e996eSAndreas Gohr        2 => 'notify',
442556e996eSAndreas Gohr    ];
443c29dc6e4SAndreas Gohr
444556e996eSAndreas Gohr    $msgdata = [
4450e20480fSPhy        'msg' => $message,
446556e996eSAndreas Gohr        'lvl' => $errors[$lvl],
4470e20480fSPhy        'allow' => $allow,
4480e20480fSPhy        'line' => $line,
4490e20480fSPhy        'file' => $file,
450556e996eSAndreas Gohr    ];
4510e20480fSPhy
45224870174SAndreas Gohr    $evt = new Event('INFOUTIL_MSG_SHOW', $msgdata);
4530e20480fSPhy    if ($evt->advise_before()) {
4540e20480fSPhy        /* Show msg normally - event could suppress message show */
4550e20480fSPhy        if ($msgdata['line'] || $msgdata['file']) {
45624870174SAndreas Gohr            $basename = PhpString::basename($msgdata['file']);
4570e20480fSPhy            $msgdata['msg'] .= ' [' . $basename . ':' . $msgdata['line'] . ']';
4580e20480fSPhy        }
459c29dc6e4SAndreas Gohr
46024870174SAndreas Gohr        if (!isset($MSG)) $MSG = [];
4610e20480fSPhy        $MSG[] = $msgdata;
462cc58224cSMichael Hamann        if (isset($MSG_shown) || headers_sent()) {
463c29dc6e4SAndreas Gohr            if (function_exists('html_msgarea')) {
464c29dc6e4SAndreas Gohr                html_msgarea();
465c29dc6e4SAndreas Gohr            } else {
46626dfc232SAndreas Gohr                echo "ERROR(" . $msgdata['lvl'] . ") " . $msgdata['msg'] . "\n";
467c29dc6e4SAndreas Gohr            }
46869266de5SDominik Eckelmann            unset($GLOBALS['MSG']);
469c29dc6e4SAndreas Gohr        }
470c29dc6e4SAndreas Gohr    }
4710e20480fSPhy    $evt->advise_after();
4720e20480fSPhy    unset($evt);
4730e20480fSPhy}
474f5be2fc0SGerrit Uitslag
475f755f9abSChristopher Smith/**
476f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
477f755f9abSChristopher Smith * in the $msg data structure
478f755f9abSChristopher Smith *
479f5be2fc0SGerrit Uitslag * @param array $msg dokuwiki msg structure:
480f5be2fc0SGerrit Uitslag *              msg   => string, the message;
481f5be2fc0SGerrit Uitslag *              lvl   => int, level of the message (see msg() function);
482f5be2fc0SGerrit Uitslag *              allow => int, flag used to determine who is allowed to see the message, see MSG_* constants
4836164d900SAndreas Gohr * @return bool
484f755f9abSChristopher Smith */
485d868eb89SAndreas Gohrfunction info_msg_allowed($msg)
486d868eb89SAndreas Gohr{
487d3bae478SChristopher Smith    global $INFO, $auth;
488d3bae478SChristopher Smith
489d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
490f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
491d3bae478SChristopher Smith
492d3bae478SChristopher Smith    // restricted msg, but no authentication
4936547cfc7SGerrit Uitslag    if (!$auth instanceof AuthPlugin) return false;
494d3bae478SChristopher Smith
495f755f9abSChristopher Smith    switch ($msg['allow']) {
496d3bae478SChristopher Smith        case MSG_USERS_ONLY:
497d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
498d3bae478SChristopher Smith
499d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
500d3bae478SChristopher Smith            return $INFO['ismanager'];
501d3bae478SChristopher Smith
502d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
503d3bae478SChristopher Smith            return $INFO['isadmin'];
504d3bae478SChristopher Smith
505d3bae478SChristopher Smith        default:
506dccd6b2bSAndreas Gohr            trigger_error(
507dccd6b2bSAndreas Gohr                'invalid msg allow restriction.  msg="' . $msg['msg'] . '" allow=' . $msg['allow'] . '"',
508dccd6b2bSAndreas Gohr                E_USER_WARNING
509dccd6b2bSAndreas Gohr            );
510d3bae478SChristopher Smith            return $INFO['isadmin'];
511d3bae478SChristopher Smith    }
512d3bae478SChristopher Smith}
513d3bae478SChristopher Smith
514c29dc6e4SAndreas Gohr/**
515c29dc6e4SAndreas Gohr * print debug messages
516c29dc6e4SAndreas Gohr *
517c29dc6e4SAndreas Gohr * little function to print the content of a var
518c29dc6e4SAndreas Gohr *
519f50a239bSTakamura * @param string $msg
520f50a239bSTakamura * @param bool $hidden
521f5be2fc0SGerrit Uitslag *
522f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
523c29dc6e4SAndreas Gohr */
524d868eb89SAndreas Gohrfunction dbg($msg, $hidden = false)
525d868eb89SAndreas Gohr{
52613493794SAndreas Gohr    if ($hidden) {
52713493794SAndreas Gohr        echo "<!--\n";
528c29dc6e4SAndreas Gohr        print_r($msg);
52913493794SAndreas Gohr        echo "\n-->";
53013493794SAndreas Gohr    } else {
53113493794SAndreas Gohr        echo '<pre class="dbg">';
53213493794SAndreas Gohr        echo hsc(print_r($msg, true));
53313493794SAndreas Gohr        echo '</pre>';
53413493794SAndreas Gohr    }
535c29dc6e4SAndreas Gohr}
536c29dc6e4SAndreas Gohr
537c29dc6e4SAndreas Gohr/**
538cad4fbf6SAndreas Gohr * Print info to debug log file
539c29dc6e4SAndreas Gohr *
540f50a239bSTakamura * @param string $msg
541f50a239bSTakamura * @param string $header
542f5be2fc0SGerrit Uitslag *
543f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
544f5be2fc0SGerrit Uitslag * @deprecated 2020-08-13
545c29dc6e4SAndreas Gohr */
546d868eb89SAndreas Gohrfunction dbglog($msg, $header = '')
547d868eb89SAndreas Gohr{
5480ecde6ceSAndreas Gohr    dbg_deprecated('\\dokuwiki\\Logger');
549585bf44eSChristopher Smith
5500ecde6ceSAndreas Gohr    // was the msg as single line string? use it as header
551093fe67eSAndreas Gohr    if ($header === '' && is_string($msg) && !str_contains($msg, "\n")) {
5520ecde6ceSAndreas Gohr        $header = $msg;
5530ecde6ceSAndreas Gohr        $msg = '';
554c7408a63SAndreas Gohr    }
555c7408a63SAndreas Gohr
55631667ec6SAndreas Gohr    Logger::getInstance(Logger::LOG_DEBUG)->log(
557dccd6b2bSAndreas Gohr        $header,
558dccd6b2bSAndreas Gohr        $msg
5590ecde6ceSAndreas Gohr    );
560c29dc6e4SAndreas Gohr}
561c29dc6e4SAndreas Gohr
562db09e31eSAndreas Gohr/**
5631419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
5641419a485SAndreas Gohr *
5651419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
56685331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
5671419a485SAndreas Gohr */
568d868eb89SAndreas Gohrfunction dbg_deprecated($alternative = '')
569d868eb89SAndreas Gohr{
57024870174SAndreas Gohr    DebugHelper::dbgDeprecatedFunction($alternative, 2);
57144455016SAndreas Gohr}
5721419a485SAndreas Gohr
5731419a485SAndreas Gohr/**
574db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
575db09e31eSAndreas Gohr *
576db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
577db09e31eSAndreas Gohr */
578d868eb89SAndreas Gohrfunction dbg_backtrace()
579d868eb89SAndreas Gohr{
580db09e31eSAndreas Gohr    // Get backtrace
581db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
582db09e31eSAndreas Gohr
583db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
584db09e31eSAndreas Gohr    array_shift($backtrace);
585db09e31eSAndreas Gohr
586db09e31eSAndreas Gohr    // Iterate backtrace
58724870174SAndreas Gohr    $calls = [];
588db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
589db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
590c0f08ea9SAndreas Gohr        if (isset($call['file'])) {
591c0f08ea9SAndreas Gohr            $location = $call['file'] . ':' . ($call['line'] ?? '0');
592c0f08ea9SAndreas Gohr        } else {
593c0f08ea9SAndreas Gohr            $location = '[anonymous]';
594c0f08ea9SAndreas Gohr        }
595c0f08ea9SAndreas Gohr        if (isset($call['class'])) {
596c0f08ea9SAndreas Gohr            $function = $call['class'] . $call['type'] . $call['function'];
597c0f08ea9SAndreas Gohr        } else {
598c0f08ea9SAndreas Gohr            $function = $call['function'];
599c0f08ea9SAndreas Gohr        }
600db09e31eSAndreas Gohr
60124870174SAndreas Gohr        $params = [];
602db09e31eSAndreas Gohr        if (isset($call['args'])) {
6038259f1aaSAndreas Gohr            foreach ($call['args'] as $arg) {
6048259f1aaSAndreas Gohr                if (is_object($arg)) {
605093fe67eSAndreas Gohr                    $params[] = '[Object ' . $arg::class . ']';
6068259f1aaSAndreas Gohr                } elseif (is_array($arg)) {
6078259f1aaSAndreas Gohr                    $params[] = '[Array]';
6088259f1aaSAndreas Gohr                } elseif (is_null($arg)) {
60959bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
6108259f1aaSAndreas Gohr                } else {
61124870174SAndreas Gohr                    $params[] = '"' . $arg . '"';
612db09e31eSAndreas Gohr                }
6138259f1aaSAndreas Gohr            }
6148259f1aaSAndreas Gohr        }
6158259f1aaSAndreas Gohr        $params = implode(', ', $params);
616db09e31eSAndreas Gohr
617dccd6b2bSAndreas Gohr        $calls[$depth - $i] = sprintf(
618dccd6b2bSAndreas Gohr            '%s(%s) called at %s',
619db09e31eSAndreas Gohr            $function,
620db09e31eSAndreas Gohr            str_replace("\n", '\n', $params),
621dccd6b2bSAndreas Gohr            $location
622dccd6b2bSAndreas Gohr        );
623db09e31eSAndreas Gohr    }
624db09e31eSAndreas Gohr    ksort($calls);
625db09e31eSAndreas Gohr
626db09e31eSAndreas Gohr    return implode("\n", $calls);
627db09e31eSAndreas Gohr}
628db09e31eSAndreas Gohr
62924297a69SAndreas Gohr/**
63024297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
63124297a69SAndreas Gohr *
63224297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
63324297a69SAndreas Gohr * debug output
63424297a69SAndreas Gohr *
635f50a239bSTakamura * @param array $data
636f5be2fc0SGerrit Uitslag *
637f5be2fc0SGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org>
63824297a69SAndreas Gohr */
639d868eb89SAndreas Gohrfunction debug_guard(&$data)
640d868eb89SAndreas Gohr{
64124297a69SAndreas Gohr    foreach ($data as $key => $value) {
64224297a69SAndreas Gohr        if (preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i', $key)) {
64324297a69SAndreas Gohr            $data[$key] = '***';
64424297a69SAndreas Gohr            continue;
64524297a69SAndreas Gohr        }
64624297a69SAndreas Gohr        if (is_array($value)) debug_guard($data[$key]);
64724297a69SAndreas Gohr    }
64824297a69SAndreas Gohr}
649