xref: /dokuwiki/inc/infoutils.php (revision 24870174d2ee45460ba6bcfe5f5a0ae94715efd7)
1c29dc6e4SAndreas Gohr<?php
2c29dc6e4SAndreas Gohr/**
3c29dc6e4SAndreas Gohr * Information and debugging functions
4c29dc6e4SAndreas Gohr *
5c29dc6e4SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6c29dc6e4SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
7c29dc6e4SAndreas Gohr */
8*24870174SAndreas Gohruse dokuwiki\Extension\Event;
9*24870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
10*24870174SAndreas Gohruse dokuwiki\Debug\DebugHelper;
115a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
1231667ec6SAndreas Gohruse dokuwiki\Logger;
13198564abSMichael Große
146c5e3c5eSPhyif(!defined('DOKU_MESSAGEURL')){
156c5e3c5eSPhy    if(in_array('ssl', stream_get_transports())) {
166c5e3c5eSPhy        define('DOKU_MESSAGEURL','https://update.dokuwiki.org/check/');
176c5e3c5eSPhy    }else{
186c5e3c5eSPhy        define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
196c5e3c5eSPhy    }
206c5e3c5eSPhy}
21c29dc6e4SAndreas Gohr
22c29dc6e4SAndreas Gohr/**
23c29dc6e4SAndreas Gohr * Check for new messages from upstream
24c29dc6e4SAndreas Gohr *
25c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
26c29dc6e4SAndreas Gohr */
27c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){
28c29dc6e4SAndreas Gohr    global $conf;
29c29dc6e4SAndreas Gohr    global $INFO;
30ef362bb8SAnika Henke    global $updateVersion;
31c29dc6e4SAndreas Gohr    if(!$conf['updatecheck']) return;
32f8cc712eSAndreas Gohr    if($conf['useacl'] && !$INFO['ismanager']) return;
33c29dc6e4SAndreas Gohr
3437b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
35c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
366c5e3c5eSPhy    $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https';
37c29dc6e4SAndreas Gohr
38c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
398d3d7569SAnika Henke    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
4063d9b820SAndreas Gohr        @touch($cf);
4131667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): downloading messages to ".$cf.($is_http?' (without SSL)':' (with SSL)'));
42c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
4363d9b820SAndreas Gohr        $http->timeout = 12;
4486c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL.$updateVersion);
4586c04d87SAngus Gratton        if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
4686c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
4786c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
4886c04d87SAngus Gratton            io_saveFile($cf,$resp);
498f1efc43SAndreas Gohr        } else {
5031667ec6SAndreas Gohr            Logger::debug("checkUpdateMessages(): unexpected HTTP response received", $http->error);
518f1efc43SAndreas Gohr        }
52c29dc6e4SAndreas Gohr    }else{
5331667ec6SAndreas Gohr        Logger::debug("checkUpdateMessages(): messages up to date");
54c29dc6e4SAndreas Gohr    }
55c29dc6e4SAndreas Gohr
5686c04d87SAngus Gratton    $data = io_readFile($cf);
57c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
58c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n",$data);
59c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
60c29dc6e4SAndreas Gohr        if($msg) msg($msg,2);
61c29dc6e4SAndreas Gohr    }
62c29dc6e4SAndreas Gohr}
63c29dc6e4SAndreas Gohr
64c29dc6e4SAndreas Gohr
65c29dc6e4SAndreas Gohr/**
6625c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
67c29dc6e4SAndreas Gohr *
68c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
69c29dc6e4SAndreas Gohr */
7025c07f93SAnika Henkefunction getVersionData(){
71*24870174SAndreas Gohr    $version = [];
72c29dc6e4SAndreas Gohr    //import version string
7379e79377SAndreas Gohr    if(file_exists(DOKU_INC.'VERSION')){
74c29dc6e4SAndreas Gohr        //official release
75d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
7625c07f93SAnika Henke        $version['type'] = 'Release';
775cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
785cf31920SAndreas Gohr        $version['type'] = 'Git';
7925c07f93SAnika Henke        $version['date'] = 'unknown';
80e570ed43SAndreas Gohr
81b9f5205aSDamien Regad        // First try to get date and commit hash by calling Git
82b6f8a5c6SDamien Regad        if (function_exists('shell_exec')) {
83b6f8a5c6SDamien Regad            $commitInfo = shell_exec("git log -1 --pretty=format:'%h %cd' --date=short");
84b9f5205aSDamien Regad            if ($commitInfo) {
85*24870174SAndreas Gohr                [$version['sha'], $date] = explode(' ', $commitInfo);
86067b4fb9SMichael Große                $version['date'] = hsc($date);
87b9f5205aSDamien Regad                return $version;
88b9f5205aSDamien Regad            }
89b9f5205aSDamien Regad        }
90b9f5205aSDamien Regad
91f519f9dbSMichael Große        // we cannot use git on the shell -- let's do it manually!
92b9f5205aSDamien Regad        if (file_exists(DOKU_INC . '.git/HEAD')) {
93f519f9dbSMichael Große            $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD'));
94f519f9dbSMichael Große            if (strpos($headCommit, 'ref: ') === 0) {
95f519f9dbSMichael Große                // it is something like `ref: refs/heads/master`
9609bf5d22SDamien Regad                $headCommit = substr($headCommit, 5);
9709bf5d22SDamien Regad                $pathToHead = DOKU_INC . '.git/' . $headCommit;
9809bf5d22SDamien Regad                if (file_exists($pathToHead)) {
9909bf5d22SDamien Regad                    $headCommit = trim(file_get_contents($pathToHead));
10009bf5d22SDamien Regad                } else {
10109bf5d22SDamien Regad                    $packedRefs = file_get_contents(DOKU_INC . '.git/packed-refs');
10209bf5d22SDamien Regad                    if (!preg_match("~([[:xdigit:]]+) $headCommit~", $packedRefs, $matches)) {
10309bf5d22SDamien Regad                        # ref not found in pack file
10409bf5d22SDamien Regad                        return $version;
105f519f9dbSMichael Große                    }
10609bf5d22SDamien Regad                    $headCommit = $matches[1];
10709bf5d22SDamien Regad                }
10809bf5d22SDamien Regad            }
10909bf5d22SDamien Regad            // At this point $headCommit is a SHA
110b6f8a5c6SDamien Regad            $version['sha'] = $headCommit;
111b6f8a5c6SDamien Regad
11209bf5d22SDamien Regad            // Get commit date from Git object
113f519f9dbSMichael Große            $subDir = substr($headCommit, 0, 2);
114f519f9dbSMichael Große            $fileName = substr($headCommit, 2);
115be2e462dSMichael Große            $gitCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName";
116a8ac20eaSGerrit Uitslag            if (file_exists($gitCommitObject) && function_exists('zlib_decode')) {
117be2e462dSMichael Große                $commit = zlib_decode(file_get_contents($gitCommitObject));
118f519f9dbSMichael Große                $committerLine = explode("\n", $commit)[3];
119f519f9dbSMichael Große                $committerData = explode(' ', $committerLine);
120f519f9dbSMichael Große                end($committerData);
121f519f9dbSMichael Große                $ts = prev($committerData);
122f519f9dbSMichael Große                if ($ts && $date = date('Y-m-d', $ts)) {
123f519f9dbSMichael Große                    $version['date'] = $date;
124f519f9dbSMichael Große                }
1255cf31920SAndreas Gohr            }
12625c07f93SAnika Henke        }
127c29dc6e4SAndreas Gohr    }else{
1286a34de2dSAnika Henke        global $updateVersion;
1296a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
13025c07f93SAnika Henke        $version['type'] = 'snapshot?';
131c29dc6e4SAndreas Gohr    }
1325cf31920SAndreas Gohr    return $version;
133c29dc6e4SAndreas Gohr}
134c29dc6e4SAndreas Gohr
135c29dc6e4SAndreas Gohr/**
13625c07f93SAnika Henke * Return DokuWiki's version (as a string)
13725c07f93SAnika Henke *
13825c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
13925c07f93SAnika Henke */
14025c07f93SAnika Henkefunction getVersion(){
14125c07f93SAnika Henke    $version = getVersionData();
142*24870174SAndreas Gohr    $sha = empty($version['sha']) ? '' : ' (' . $version['sha'] . ')';
143b6f8a5c6SDamien Regad    return $version['type'] . ' ' . $version['date'] . $sha;
14425c07f93SAnika Henke}
14525c07f93SAnika Henke
14625c07f93SAnika Henke/**
147c29dc6e4SAndreas Gohr * Run a few sanity checks
148c29dc6e4SAndreas Gohr *
149c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
150c29dc6e4SAndreas Gohr */
151c29dc6e4SAndreas Gohrfunction check(){
152c29dc6e4SAndreas Gohr    global $conf;
153c29dc6e4SAndreas Gohr    global $INFO;
154585bf44eSChristopher Smith    /* @var Input $INPUT */
155585bf44eSChristopher Smith    global $INPUT;
156c29dc6e4SAndreas Gohr
1573f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']) {
158c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
159c49393f5SAndreas Gohr        if(version_compare(phpversion(),'7.4.0','<')){
160c49393f5SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 7.4+ needed)',-1);
161c29dc6e4SAndreas Gohr        }else{
162c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
163c29dc6e4SAndreas Gohr        }
164*24870174SAndreas Gohr    } elseif (version_compare(phpversion(),'7.4.0','<')) {
16508d1a8dfSAndreas Gohr        msg('Your PHP version is too old',-1);
16608d1a8dfSAndreas Gohr    }
167c6e971ddSAndreas Gohr
168*24870174SAndreas Gohr    $mem = php_to_byte(ini_get('memory_limit'));
16973038c47SAndreas Gohr    if($mem){
170eb2e46caSAndreas Gohr        if ($mem === -1) {
1718f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
172*24870174SAndreas Gohr        } elseif ($mem < 16_777_216) {
1732b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
1742b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
175*24870174SAndreas Gohr        } elseif ($mem < 20_971_520) {
1762b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
17764159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
178*24870174SAndreas Gohr        } elseif ($mem < 33_554_432) {
1792b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
18064159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
18173038c47SAndreas Gohr        } else {
182c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
18373038c47SAndreas Gohr        }
18473038c47SAndreas Gohr    }
18573038c47SAndreas Gohr
186c29dc6e4SAndreas Gohr    if (is_writable($conf['changelog'])) {
187c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
188*24870174SAndreas Gohr    } elseif (file_exists($conf['changelog'])) {
189c29dc6e4SAndreas Gohr        msg('Changelog is not writable',-1);
190c29dc6e4SAndreas Gohr    }
191c29dc6e4SAndreas Gohr
19279e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1932cdbda06SAnika Henke        msg('Old changelog exists', 0);
194c29dc6e4SAndreas Gohr    }
195c29dc6e4SAndreas Gohr
19679e79377SAndreas Gohr    if (file_exists($conf['changelog'].'_failed')) {
1972cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
19879e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'].'_importing')) {
199c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
20079e79377SAndreas Gohr    } elseif (file_exists($conf['changelog'].'_import_ok')) {
2012cdbda06SAnika Henke        msg('Old changelog imported', 1);
202c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
2032cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
204c29dc6e4SAndreas Gohr        }
205c29dc6e4SAndreas Gohr    }
206c29dc6e4SAndreas Gohr
2074c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
2084c7ecf15SGuy Brand        msg('conf directory is writable',1);
2094c7ecf15SGuy Brand    }else{
2104c7ecf15SGuy Brand        msg('conf directory is not writable',-1);
2114c7ecf15SGuy Brand    }
2124c7ecf15SGuy Brand
2130d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
214defb7d57SAnika Henke        global $config_cascade;
215defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
216c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
217c29dc6e4SAndreas Gohr        }else{
218c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
219c29dc6e4SAndreas Gohr        }
2200d487d8fSAndreas Gohr    }
221c29dc6e4SAndreas Gohr
222c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
223c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
224c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
225c29dc6e4SAndreas Gohr        }else{
226c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
2274222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
2284222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
2294222b898SAndreas Gohr            }
230c29dc6e4SAndreas Gohr        }
231c29dc6e4SAndreas Gohr    }else{
232c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
233c29dc6e4SAndreas Gohr    }
234c29dc6e4SAndreas Gohr
2353161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
236a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
237a731ed1dSAndreas Gohr    }
2383161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
239a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
240a731ed1dSAndreas Gohr    }
241a731ed1dSAndreas Gohr
242e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
243e5ab313fSAndreas Gohr    if(!$loc){
244e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this',-1);
245e5ab313fSAndreas Gohr    }elseif(stripos($loc,'utf') === false){
24664159a61SAndreas Gohr        msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale,
24764159a61SAndreas Gohr             you should fix this if you encounter problems.',0);
248e5ab313fSAndreas Gohr    }else{
249e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
250e5ab313fSAndreas Gohr    }
251e5ab313fSAndreas Gohr
252c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
253c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
254c29dc6e4SAndreas Gohr    }else{
255c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
256c29dc6e4SAndreas Gohr    }
257c29dc6e4SAndreas Gohr
258a4231b8cSfiwswe    if(!empty($INFO['userinfo']['name'])){
259585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
2608368419bSSyntaxseed        msg('You are part of the groups '.implode(', ', $INFO['userinfo']['grps']),0);
2613d3c095dSMike Frysinger    }else{
2623d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2633d3c095dSMike Frysinger    }
2643d3c095dSMike Frysinger
265c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
266c29dc6e4SAndreas Gohr
267322ad074SAndreas Gohr    if (file_exists($INFO['filepath']) && is_writable($INFO['filepath'])) {
268322ad074SAndreas Gohr        msg('The current page is writable by the webserver', 1);
269322ad074SAndreas Gohr    } elseif (!file_exists($INFO['filepath']) && is_writable(dirname($INFO['filepath']))) {
270322ad074SAndreas Gohr        msg('The current page can be created by the webserver', 1);
271c29dc6e4SAndreas Gohr    } else {
272322ad074SAndreas Gohr        msg('The current page is not writable by the webserver', -1);
273c29dc6e4SAndreas Gohr    }
274c29dc6e4SAndreas Gohr
275c29dc6e4SAndreas Gohr    if ($INFO['writable']) {
276322ad074SAndreas Gohr        msg('The current page is writable by you', 1);
277c29dc6e4SAndreas Gohr    } else {
278322ad074SAndreas Gohr        msg('The current page is not writable by you', -1);
279c29dc6e4SAndreas Gohr    }
2803b1dfc83SAndreas Gohr
28126f7dbf5SMichael Hamann    // Check for corrupted search index
28226f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
28326f7dbf5SMichael Hamann    $index_corrupted = false;
28426f7dbf5SMichael Hamann    foreach ($lengths as $length) {
285*24870174SAndreas Gohr        if (count(idx_getIndex('w', $length)) !== count(idx_getIndex('i', $length))) {
28626f7dbf5SMichael Hamann            $index_corrupted = true;
28726f7dbf5SMichael Hamann            break;
28826f7dbf5SMichael Hamann        }
28926f7dbf5SMichael Hamann    }
29026f7dbf5SMichael Hamann
29126f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
292*24870174SAndreas Gohr        if (count(idx_getIndex($index.'_w', '')) !== count(idx_getIndex($index.'_i', ''))) {
29326f7dbf5SMichael Hamann            $index_corrupted = true;
29426f7dbf5SMichael Hamann            break;
29526f7dbf5SMichael Hamann        }
29626f7dbf5SMichael Hamann    }
29726f7dbf5SMichael Hamann
298d6c7b502SAndreas Gohr    if($index_corrupted) {
299d6c7b502SAndreas Gohr        msg(
300d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
3013d94d9edSMichael Hamann                probably needs to be rebuilt. See
30226f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
303d6c7b502SAndreas Gohr                for ways to rebuild the search index.', -1
304d6c7b502SAndreas Gohr        );
305d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
3063d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
307d6c7b502SAndreas Gohr    } else {
308d6c7b502SAndreas Gohr        msg(
309d6c7b502SAndreas Gohr            'The search index is empty. See
31026f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
3113d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
312d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
313d6c7b502SAndreas Gohr        );
314d6c7b502SAndreas Gohr    }
315d6c7b502SAndreas Gohr
316d6c7b502SAndreas Gohr    // rough time check
317d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
318d6c7b502SAndreas Gohr    $http->max_redirect = 0;
319d6c7b502SAndreas Gohr    $http->timeout = 3;
320d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
321d6c7b502SAndreas Gohr    $now = time();
322d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
323d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
324d6c7b502SAndreas Gohr        $diff = $time - $now;
325d6c7b502SAndreas Gohr
326d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
327d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
328d6c7b502SAndreas Gohr        } else {
32964159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
33064159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
331d6c7b502SAndreas Gohr        }
332d6c7b502SAndreas Gohr    }
333d6c7b502SAndreas Gohr
334c29dc6e4SAndreas Gohr}
335c29dc6e4SAndreas Gohr
336c29dc6e4SAndreas Gohr/**
33746028c4cSAndreas Gohr * Display a message to the user
338c29dc6e4SAndreas Gohr *
339c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
340c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
341c29dc6e4SAndreas Gohr * using html_msgarea()
342c29dc6e4SAndreas Gohr *
3430e20480fSPhy * Triggers INFOUTIL_MSG_SHOW
3440e20480fSPhy *
34546028c4cSAndreas Gohr * @see    html_msgarea()
3466164d900SAndreas Gohr * @param string $message
3476164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3486164d900SAndreas Gohr * @param string $line  line number
3496164d900SAndreas Gohr * @param string $file  file number
3506164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3516164d900SAndreas Gohr */
352f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
353cc58224cSMichael Hamann    global $MSG, $MSG_shown;
354556e996eSAndreas Gohr    static $errors = [
355556e996eSAndreas Gohr        -1 => 'error',
356556e996eSAndreas Gohr        0 => 'info',
357556e996eSAndreas Gohr        1 => 'success',
358556e996eSAndreas Gohr        2 => 'notify',
359556e996eSAndreas Gohr    ];
360c29dc6e4SAndreas Gohr
361556e996eSAndreas Gohr    $msgdata = [
3620e20480fSPhy        'msg' => $message,
363556e996eSAndreas Gohr        'lvl' => $errors[$lvl],
3640e20480fSPhy        'allow' => $allow,
3650e20480fSPhy        'line' => $line,
3660e20480fSPhy        'file' => $file,
367556e996eSAndreas Gohr    ];
3680e20480fSPhy
369*24870174SAndreas Gohr    $evt = new Event('INFOUTIL_MSG_SHOW', $msgdata);
3700e20480fSPhy    if ($evt->advise_before()) {
3710e20480fSPhy        /* Show msg normally - event could suppress message show */
3720e20480fSPhy        if($msgdata['line'] || $msgdata['file']) {
373*24870174SAndreas Gohr            $basename = PhpString::basename($msgdata['file']);
3740e20480fSPhy            $msgdata['msg'] .=' ['.$basename.':'.$msgdata['line'].']';
3750e20480fSPhy        }
376c29dc6e4SAndreas Gohr
377*24870174SAndreas Gohr        if(!isset($MSG)) $MSG = [];
3780e20480fSPhy        $MSG[] = $msgdata;
379cc58224cSMichael Hamann        if(isset($MSG_shown) || headers_sent()){
380c29dc6e4SAndreas Gohr            if(function_exists('html_msgarea')){
381c29dc6e4SAndreas Gohr                html_msgarea();
382c29dc6e4SAndreas Gohr            }else{
3830e20480fSPhy                print "ERROR(".$msgdata['lvl'].") ".$msgdata['msg']."\n";
384c29dc6e4SAndreas Gohr            }
38569266de5SDominik Eckelmann            unset($GLOBALS['MSG']);
386c29dc6e4SAndreas Gohr        }
387c29dc6e4SAndreas Gohr    }
3880e20480fSPhy    $evt->advise_after();
3890e20480fSPhy    unset($evt);
3900e20480fSPhy}
391f755f9abSChristopher Smith/**
392f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
393f755f9abSChristopher Smith * in the $msg data structure
394f755f9abSChristopher Smith *
395f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
396f755f9abSChristopher Smith *                         msg   => string, the message
397f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
398f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
399f755f9abSChristopher Smith *                                       see MSG_* constants
4006164d900SAndreas Gohr * @return bool
401f755f9abSChristopher Smith */
402f755f9abSChristopher Smithfunction info_msg_allowed($msg){
403d3bae478SChristopher Smith    global $INFO, $auth;
404d3bae478SChristopher Smith
405d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
406f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
407d3bae478SChristopher Smith
408d3bae478SChristopher Smith    // restricted msg, but no authentication
409d3bae478SChristopher Smith    if (empty($auth)) return false;
410d3bae478SChristopher Smith
411f755f9abSChristopher Smith    switch ($msg['allow']){
412d3bae478SChristopher Smith        case MSG_USERS_ONLY:
413d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
414d3bae478SChristopher Smith
415d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
416d3bae478SChristopher Smith            return $INFO['ismanager'];
417d3bae478SChristopher Smith
418d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
419d3bae478SChristopher Smith            return $INFO['isadmin'];
420d3bae478SChristopher Smith
421d3bae478SChristopher Smith        default:
42264159a61SAndreas Gohr            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"',
42364159a61SAndreas Gohr                          E_USER_WARNING);
424d3bae478SChristopher Smith            return $INFO['isadmin'];
425d3bae478SChristopher Smith    }
426d3bae478SChristopher Smith}
427d3bae478SChristopher Smith
428c29dc6e4SAndreas Gohr/**
429c29dc6e4SAndreas Gohr * print debug messages
430c29dc6e4SAndreas Gohr *
431c29dc6e4SAndreas Gohr * little function to print the content of a var
432c29dc6e4SAndreas Gohr *
433c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
434f50a239bSTakamura *
435f50a239bSTakamura * @param string $msg
436f50a239bSTakamura * @param bool $hidden
437c29dc6e4SAndreas Gohr */
438c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
43913493794SAndreas Gohr    if($hidden){
44013493794SAndreas Gohr        echo "<!--\n";
441c29dc6e4SAndreas Gohr        print_r($msg);
44213493794SAndreas Gohr        echo "\n-->";
44313493794SAndreas Gohr    }else{
44413493794SAndreas Gohr        echo '<pre class="dbg">';
44513493794SAndreas Gohr        echo hsc(print_r($msg,true));
44613493794SAndreas Gohr        echo '</pre>';
44713493794SAndreas Gohr    }
448c29dc6e4SAndreas Gohr}
449c29dc6e4SAndreas Gohr
450c29dc6e4SAndreas Gohr/**
451cad4fbf6SAndreas Gohr * Print info to debug log file
452c29dc6e4SAndreas Gohr *
453c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4540ecde6ceSAndreas Gohr * @deprecated 2020-08-13
455f50a239bSTakamura * @param string $msg
456f50a239bSTakamura * @param string $header
457c29dc6e4SAndreas Gohr */
4580699d739SAndreas Gohrfunction dbglog($msg,$header=''){
4590ecde6ceSAndreas Gohr    dbg_deprecated('\\dokuwiki\\Logger');
460585bf44eSChristopher Smith
4610ecde6ceSAndreas Gohr    // was the msg as single line string? use it as header
462cad4fbf6SAndreas Gohr    if($header === '' && is_string($msg) && strpos($msg, "\n") === false) {
4630ecde6ceSAndreas Gohr        $header = $msg;
4640ecde6ceSAndreas Gohr        $msg = '';
465c7408a63SAndreas Gohr    }
466c7408a63SAndreas Gohr
46731667ec6SAndreas Gohr    Logger::getInstance(Logger::LOG_DEBUG)->log(
4680ecde6ceSAndreas Gohr        $header, $msg
4690ecde6ceSAndreas Gohr    );
470c29dc6e4SAndreas Gohr}
471c29dc6e4SAndreas Gohr
472db09e31eSAndreas Gohr/**
4731419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4741419a485SAndreas Gohr *
4751419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
47685331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
4771419a485SAndreas Gohr */
4781419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') {
479*24870174SAndreas Gohr    DebugHelper::dbgDeprecatedFunction($alternative, 2);
48044455016SAndreas Gohr}
4811419a485SAndreas Gohr
4821419a485SAndreas Gohr/**
483db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
484db09e31eSAndreas Gohr *
485db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
486db09e31eSAndreas Gohr */
487db09e31eSAndreas Gohrfunction dbg_backtrace(){
488db09e31eSAndreas Gohr    // Get backtrace
489db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
490db09e31eSAndreas Gohr
491db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
492db09e31eSAndreas Gohr    array_shift($backtrace);
493db09e31eSAndreas Gohr
494db09e31eSAndreas Gohr    // Iterate backtrace
495*24870174SAndreas Gohr    $calls = [];
496db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
497db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
498db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
499db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
500db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
501db09e31eSAndreas Gohr
502*24870174SAndreas Gohr        $params = [];
503db09e31eSAndreas Gohr        if (isset($call['args'])){
5048259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
5058259f1aaSAndreas Gohr                if(is_object($arg)){
5068259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
5078259f1aaSAndreas Gohr                }elseif(is_array($arg)){
5088259f1aaSAndreas Gohr                    $params[] = '[Array]';
5098259f1aaSAndreas Gohr                }elseif(is_null($arg)){
51059bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
5118259f1aaSAndreas Gohr                }else{
512*24870174SAndreas Gohr                    $params[] = '"'.$arg.'"';
513db09e31eSAndreas Gohr                }
5148259f1aaSAndreas Gohr            }
5158259f1aaSAndreas Gohr        }
5168259f1aaSAndreas Gohr        $params = implode(', ',$params);
517db09e31eSAndreas Gohr
5188259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
519db09e31eSAndreas Gohr                $function,
520db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
521db09e31eSAndreas Gohr                $location);
522db09e31eSAndreas Gohr    }
523db09e31eSAndreas Gohr    ksort($calls);
524db09e31eSAndreas Gohr
525db09e31eSAndreas Gohr    return implode("\n", $calls);
526db09e31eSAndreas Gohr}
527db09e31eSAndreas Gohr
52824297a69SAndreas Gohr/**
52924297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
53024297a69SAndreas Gohr *
53124297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
53224297a69SAndreas Gohr * debug output
53324297a69SAndreas Gohr *
53424297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
535f50a239bSTakamura *
536f50a239bSTakamura * @param array $data
53724297a69SAndreas Gohr */
53824297a69SAndreas Gohrfunction debug_guard(&$data){
53924297a69SAndreas Gohr    foreach($data as $key => $value){
54024297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
54124297a69SAndreas Gohr            $data[$key] = '***';
54224297a69SAndreas Gohr            continue;
54324297a69SAndreas Gohr        }
54424297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
54524297a69SAndreas Gohr    }
54624297a69SAndreas Gohr}
547