xref: /dokuwiki/inc/infoutils.php (revision 556e996e8e0ce3bb7821043aaf192099183d1159)
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 */
8198564abSMichael Große
95a8d6e48SMichael Großeuse dokuwiki\HTTP\DokuHTTPClient;
10198564abSMichael Große
116c5e3c5eSPhyif(!defined('DOKU_MESSAGEURL')){
126c5e3c5eSPhy    if(in_array('ssl', stream_get_transports())) {
136c5e3c5eSPhy        define('DOKU_MESSAGEURL','https://update.dokuwiki.org/check/');
146c5e3c5eSPhy    }else{
156c5e3c5eSPhy        define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
166c5e3c5eSPhy    }
176c5e3c5eSPhy}
18c29dc6e4SAndreas Gohr
19c29dc6e4SAndreas Gohr/**
20c29dc6e4SAndreas Gohr * Check for new messages from upstream
21c29dc6e4SAndreas Gohr *
22c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
23c29dc6e4SAndreas Gohr */
24c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){
25c29dc6e4SAndreas Gohr    global $conf;
26c29dc6e4SAndreas Gohr    global $INFO;
27ef362bb8SAnika Henke    global $updateVersion;
28c29dc6e4SAndreas Gohr    if(!$conf['updatecheck']) return;
29f8cc712eSAndreas Gohr    if($conf['useacl'] && !$INFO['ismanager']) return;
30c29dc6e4SAndreas Gohr
3137b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
32c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
336c5e3c5eSPhy    $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https';
34c29dc6e4SAndreas Gohr
35c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
368d3d7569SAnika Henke    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
3763d9b820SAndreas Gohr        @touch($cf);
386c5e3c5eSPhy        dbglog("checkUpdateMessages(): downloading messages to ".$cf.($is_http?' (without SSL)':' (with SSL)'));
39c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
4063d9b820SAndreas Gohr        $http->timeout = 12;
4186c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL.$updateVersion);
4286c04d87SAngus Gratton        if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
4386c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
4486c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
4586c04d87SAngus Gratton            io_saveFile($cf,$resp);
468f1efc43SAndreas Gohr        } else {
4786c04d87SAngus Gratton            dbglog("checkUpdateMessages(): unexpected HTTP response received");
488f1efc43SAndreas Gohr        }
49c29dc6e4SAndreas Gohr    }else{
5037b21a1bSAndreas Gohr        dbglog("checkUpdateMessages(): messages up to date");
51c29dc6e4SAndreas Gohr    }
52c29dc6e4SAndreas Gohr
5386c04d87SAngus Gratton    $data = io_readFile($cf);
54c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
55c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n",$data);
56c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
57c29dc6e4SAndreas Gohr        if($msg) msg($msg,2);
58c29dc6e4SAndreas Gohr    }
59c29dc6e4SAndreas Gohr}
60c29dc6e4SAndreas Gohr
61c29dc6e4SAndreas Gohr
62c29dc6e4SAndreas Gohr/**
6325c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
64c29dc6e4SAndreas Gohr *
65c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
66c29dc6e4SAndreas Gohr */
6725c07f93SAnika Henkefunction getVersionData(){
6825c07f93SAnika Henke    $version = array();
69c29dc6e4SAndreas Gohr    //import version string
7079e79377SAndreas Gohr    if(file_exists(DOKU_INC.'VERSION')){
71c29dc6e4SAndreas Gohr        //official release
72d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
7325c07f93SAnika Henke        $version['type'] = 'Release';
745cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
755cf31920SAndreas Gohr        $version['type'] = 'Git';
7625c07f93SAnika Henke        $version['date'] = 'unknown';
77e570ed43SAndreas Gohr
785cf31920SAndreas Gohr        $inventory = DOKU_INC.'.git/logs/HEAD';
795cf31920SAndreas Gohr        if(is_file($inventory)){
80e570ed43SAndreas Gohr            $sz   = filesize($inventory);
815cf31920SAndreas Gohr            $seek = max(0,$sz-2000); // read from back of the file
82e570ed43SAndreas Gohr            $fh   = fopen($inventory,'rb');
83c29dc6e4SAndreas Gohr            fseek($fh,$seek);
84c29dc6e4SAndreas Gohr            $chunk = fread($fh,2000);
85c29dc6e4SAndreas Gohr            fclose($fh);
865cf31920SAndreas Gohr            $chunk = trim($chunk);
87b838050eSPiyush Mishra            $chunk = @array_pop(explode("\n",$chunk));   //last log line
88b838050eSPiyush Mishra            $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg
895cf31920SAndreas Gohr            $chunk = explode(" ",$chunk);
905cf31920SAndreas Gohr            array_pop($chunk); //strip timezone
915cf31920SAndreas Gohr            $date = date('Y-m-d',array_pop($chunk));
925cf31920SAndreas Gohr            if($date) $version['date'] = $date;
935cf31920SAndreas Gohr        }
94c29dc6e4SAndreas Gohr    }else{
956a34de2dSAnika Henke        global $updateVersion;
966a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
9725c07f93SAnika Henke        $version['type'] = 'snapshot?';
98c29dc6e4SAndreas Gohr    }
995cf31920SAndreas Gohr    return $version;
100c29dc6e4SAndreas Gohr}
101c29dc6e4SAndreas Gohr
102c29dc6e4SAndreas Gohr/**
10325c07f93SAnika Henke * Return DokuWiki's version (as a string)
10425c07f93SAnika Henke *
10525c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
10625c07f93SAnika Henke */
10725c07f93SAnika Henkefunction getVersion(){
10825c07f93SAnika Henke    $version = getVersionData();
10925c07f93SAnika Henke    return $version['type'].' '.$version['date'];
11025c07f93SAnika Henke}
11125c07f93SAnika Henke
11225c07f93SAnika Henke/**
113c29dc6e4SAndreas Gohr * Run a few sanity checks
114c29dc6e4SAndreas Gohr *
115c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
116c29dc6e4SAndreas Gohr */
117c29dc6e4SAndreas Gohrfunction check(){
118c29dc6e4SAndreas Gohr    global $conf;
119c29dc6e4SAndreas Gohr    global $INFO;
120585bf44eSChristopher Smith    /* @var Input $INPUT */
121585bf44eSChristopher Smith    global $INPUT;
122c29dc6e4SAndreas Gohr
1233f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
124c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
125c29dc6e4SAndreas Gohr
1263476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
1273476bb81SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 5.6.0+ needed)',-1);
128c29dc6e4SAndreas Gohr        }else{
129c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
130c29dc6e4SAndreas Gohr        }
13108d1a8dfSAndreas Gohr    } else {
1323476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
13308d1a8dfSAndreas Gohr            msg('Your PHP version is too old',-1);
13408d1a8dfSAndreas Gohr        }
13508d1a8dfSAndreas Gohr    }
136c6e971ddSAndreas Gohr
137c6e971ddSAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
13873038c47SAndreas Gohr    if($mem){
139eb2e46caSAndreas Gohr        if ($mem === -1) {
1408f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
1418f8499faSpeterfromearth        } else if ($mem < 16777216) {
1422b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
1432b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
14473038c47SAndreas Gohr        } else if ($mem < 20971520) {
1452b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
14664159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
14773038c47SAndreas Gohr        } else if ($mem < 33554432) {
1482b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
14964159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
15073038c47SAndreas Gohr        } else {
151c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
15273038c47SAndreas Gohr        }
15373038c47SAndreas Gohr    }
15473038c47SAndreas Gohr
155c29dc6e4SAndreas Gohr    if(is_writable($conf['changelog'])){
156c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
157c29dc6e4SAndreas Gohr    }else{
15879e79377SAndreas Gohr        if (file_exists($conf['changelog'])) {
159c29dc6e4SAndreas Gohr            msg('Changelog is not writable',-1);
160c29dc6e4SAndreas Gohr        }
161c29dc6e4SAndreas Gohr    }
162c29dc6e4SAndreas Gohr
16379e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1642cdbda06SAnika Henke        msg('Old changelog exists', 0);
165c29dc6e4SAndreas Gohr    }
166c29dc6e4SAndreas Gohr
16779e79377SAndreas Gohr    if (file_exists($conf['changelog'].'_failed')) {
1682cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
16979e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_importing')) {
170c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
17179e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_import_ok')) {
1722cdbda06SAnika Henke        msg('Old changelog imported', 1);
173c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
1742cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
175c29dc6e4SAndreas Gohr        }
176c29dc6e4SAndreas Gohr    }
177c29dc6e4SAndreas Gohr
1784c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
1794c7ecf15SGuy Brand        msg('conf directory is writable',1);
1804c7ecf15SGuy Brand    }else{
1814c7ecf15SGuy Brand        msg('conf directory is not writable',-1);
1824c7ecf15SGuy Brand    }
1834c7ecf15SGuy Brand
1840d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
185defb7d57SAnika Henke        global $config_cascade;
186defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
187c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
188c29dc6e4SAndreas Gohr        }else{
189c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
190c29dc6e4SAndreas Gohr        }
1910d487d8fSAndreas Gohr    }
192c29dc6e4SAndreas Gohr
193c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
194c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
195c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
196c29dc6e4SAndreas Gohr        }else{
197c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
1984222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
1994222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
2004222b898SAndreas Gohr            }
201c29dc6e4SAndreas Gohr        }
202c29dc6e4SAndreas Gohr    }else{
203c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
204c29dc6e4SAndreas Gohr    }
205c29dc6e4SAndreas Gohr
2063161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
207a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
208a731ed1dSAndreas Gohr    }
2093161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
210a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
211a731ed1dSAndreas Gohr    }
212a731ed1dSAndreas Gohr
213e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
214e5ab313fSAndreas Gohr    if(!$loc){
215e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this',-1);
216e5ab313fSAndreas Gohr    }elseif(stripos($loc,'utf') === false){
21764159a61SAndreas Gohr        msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale,
21864159a61SAndreas Gohr             you should fix this if you encounter problems.',0);
219e5ab313fSAndreas Gohr    }else{
220e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
221e5ab313fSAndreas Gohr    }
222e5ab313fSAndreas Gohr
223c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
224c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
225c29dc6e4SAndreas Gohr    }else{
226c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
227c29dc6e4SAndreas Gohr    }
228c29dc6e4SAndreas Gohr
2293d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
230585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
231c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2323d3c095dSMike Frysinger    }else{
2333d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2343d3c095dSMike Frysinger    }
2353d3c095dSMike Frysinger
236c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
237c29dc6e4SAndreas Gohr
238c29dc6e4SAndreas Gohr    if(is_writable($INFO['filepath'])){
239c29dc6e4SAndreas Gohr        msg('The current page is writable by the webserver',0);
240c29dc6e4SAndreas Gohr    }else{
241c29dc6e4SAndreas Gohr        msg('The current page is not writable by the webserver',0);
242c29dc6e4SAndreas Gohr    }
243c29dc6e4SAndreas Gohr
244c29dc6e4SAndreas Gohr    if($INFO['writable']){
245c29dc6e4SAndreas Gohr        msg('The current page is writable by you',0);
246c29dc6e4SAndreas Gohr    }else{
2472cdbda06SAnika Henke        msg('The current page is not writable by you',0);
248c29dc6e4SAndreas Gohr    }
2493b1dfc83SAndreas Gohr
25026f7dbf5SMichael Hamann    // Check for corrupted search index
25126f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
25226f7dbf5SMichael Hamann    $index_corrupted = false;
25326f7dbf5SMichael Hamann    foreach ($lengths as $length) {
25426f7dbf5SMichael Hamann        if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
25526f7dbf5SMichael Hamann            $index_corrupted = true;
25626f7dbf5SMichael Hamann            break;
25726f7dbf5SMichael Hamann        }
25826f7dbf5SMichael Hamann    }
25926f7dbf5SMichael Hamann
26026f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
26126f7dbf5SMichael Hamann        if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
26226f7dbf5SMichael Hamann            $index_corrupted = true;
26326f7dbf5SMichael Hamann            break;
26426f7dbf5SMichael Hamann        }
26526f7dbf5SMichael Hamann    }
26626f7dbf5SMichael Hamann
267d6c7b502SAndreas Gohr    if($index_corrupted) {
268d6c7b502SAndreas Gohr        msg(
269d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
2703d94d9edSMichael Hamann                probably needs to be rebuilt. See
27126f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
272d6c7b502SAndreas Gohr                for ways to rebuild the search index.', -1
273d6c7b502SAndreas Gohr        );
274d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
2753d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
276d6c7b502SAndreas Gohr    } else {
277d6c7b502SAndreas Gohr        msg(
278d6c7b502SAndreas Gohr            'The search index is empty. See
27926f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2803d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
281d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
282d6c7b502SAndreas Gohr        );
283d6c7b502SAndreas Gohr    }
284d6c7b502SAndreas Gohr
285d6c7b502SAndreas Gohr    // rough time check
286d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
287d6c7b502SAndreas Gohr    $http->max_redirect = 0;
288d6c7b502SAndreas Gohr    $http->timeout = 3;
289d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
290d6c7b502SAndreas Gohr    $now = time();
291d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
292d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
293d6c7b502SAndreas Gohr        $diff = $time - $now;
294d6c7b502SAndreas Gohr
295d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
296d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
297d6c7b502SAndreas Gohr        } else {
29864159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
29964159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
300d6c7b502SAndreas Gohr        }
301d6c7b502SAndreas Gohr    }
302d6c7b502SAndreas Gohr
303c29dc6e4SAndreas Gohr}
304c29dc6e4SAndreas Gohr
305c29dc6e4SAndreas Gohr/**
306c29dc6e4SAndreas Gohr * print a message
307c29dc6e4SAndreas Gohr *
308c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
309c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
310c29dc6e4SAndreas Gohr * using html_msgarea()
311c29dc6e4SAndreas Gohr *
312c29dc6e4SAndreas Gohr *
313c29dc6e4SAndreas Gohr * Levels can be:
314c29dc6e4SAndreas Gohr *
315c29dc6e4SAndreas Gohr * -1 error
316c29dc6e4SAndreas Gohr *  0 info
317c29dc6e4SAndreas Gohr *  1 success
318c29dc6e4SAndreas Gohr *
319c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
320c29dc6e4SAndreas Gohr * @see    html_msgarea
321c29dc6e4SAndreas Gohr */
322d3bae478SChristopher Smith
323d3bae478SChristopher Smithdefine('MSG_PUBLIC', 0);
324d3bae478SChristopher Smithdefine('MSG_USERS_ONLY', 1);
325d3bae478SChristopher Smithdefine('MSG_MANAGERS_ONLY',2);
326d3bae478SChristopher Smithdefine('MSG_ADMINS_ONLY',4);
327d3bae478SChristopher Smith
3286164d900SAndreas Gohr/**
3296164d900SAndreas Gohr * Display a message to the user
3306164d900SAndreas Gohr *
3310e20480fSPhy * Triggers INFOUTIL_MSG_SHOW
3320e20480fSPhy *
3336164d900SAndreas Gohr * @param string $message
3346164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3356164d900SAndreas Gohr * @param string $line  line number
3366164d900SAndreas Gohr * @param string $file  file number
3376164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3386164d900SAndreas Gohr */
339f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
340cc58224cSMichael Hamann    global $MSG, $MSG_shown;
341*556e996eSAndreas Gohr    static $errors = [
342*556e996eSAndreas Gohr        -1 => 'error',
343*556e996eSAndreas Gohr        0 => 'info',
344*556e996eSAndreas Gohr        1 => 'success',
345*556e996eSAndreas Gohr        2 => 'notify',
346*556e996eSAndreas Gohr    ];
347c29dc6e4SAndreas Gohr
348*556e996eSAndreas Gohr    $msgdata = [
3490e20480fSPhy        'msg' => $message,
350*556e996eSAndreas Gohr        'lvl' => $errors[$lvl],
3510e20480fSPhy        'allow' => $allow,
3520e20480fSPhy        'line' => $line,
3530e20480fSPhy        'file' => $file,
354*556e996eSAndreas Gohr    ];
3550e20480fSPhy
3560e20480fSPhy    $evt = new \dokuwiki\Extension\Event('INFOUTIL_MSG_SHOW', $msgdata);
3570e20480fSPhy    if ($evt->advise_before()) {
3580e20480fSPhy        /* Show msg normally - event could suppress message show */
3590e20480fSPhy        if($msgdata['line'] || $msgdata['file']) {
3600e20480fSPhy            $basename = \dokuwiki\Utf8\PhpString::basename($msgdata['file']);
3610e20480fSPhy            $msgdata['msg'] .=' ['.$basename.':'.$msgdata['line'].']';
3620e20480fSPhy        }
363c29dc6e4SAndreas Gohr
364c29dc6e4SAndreas Gohr        if(!isset($MSG)) $MSG = array();
3650e20480fSPhy        $MSG[] = $msgdata;
366cc58224cSMichael Hamann        if(isset($MSG_shown) || headers_sent()){
367c29dc6e4SAndreas Gohr            if(function_exists('html_msgarea')){
368c29dc6e4SAndreas Gohr                html_msgarea();
369c29dc6e4SAndreas Gohr            }else{
3700e20480fSPhy                print "ERROR(".$msgdata['lvl'].") ".$msgdata['msg']."\n";
371c29dc6e4SAndreas Gohr            }
37269266de5SDominik Eckelmann            unset($GLOBALS['MSG']);
373c29dc6e4SAndreas Gohr        }
374c29dc6e4SAndreas Gohr    }
3750e20480fSPhy    $evt->advise_after();
3760e20480fSPhy    unset($evt);
3770e20480fSPhy}
378f755f9abSChristopher Smith/**
379f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
380f755f9abSChristopher Smith * in the $msg data structure
381f755f9abSChristopher Smith *
382f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
383f755f9abSChristopher Smith *                         msg   => string, the message
384f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
385f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
386f755f9abSChristopher Smith *                                       see MSG_* constants
3876164d900SAndreas Gohr * @return bool
388f755f9abSChristopher Smith */
389f755f9abSChristopher Smithfunction info_msg_allowed($msg){
390d3bae478SChristopher Smith    global $INFO, $auth;
391d3bae478SChristopher Smith
392d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
393f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
394d3bae478SChristopher Smith
395d3bae478SChristopher Smith    // restricted msg, but no authentication
396d3bae478SChristopher Smith    if (empty($auth)) return false;
397d3bae478SChristopher Smith
398f755f9abSChristopher Smith    switch ($msg['allow']){
399d3bae478SChristopher Smith        case MSG_USERS_ONLY:
400d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
401d3bae478SChristopher Smith
402d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
403d3bae478SChristopher Smith            return $INFO['ismanager'];
404d3bae478SChristopher Smith
405d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
406d3bae478SChristopher Smith            return $INFO['isadmin'];
407d3bae478SChristopher Smith
408d3bae478SChristopher Smith        default:
40964159a61SAndreas Gohr            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"',
41064159a61SAndreas Gohr                          E_USER_WARNING);
411d3bae478SChristopher Smith            return $INFO['isadmin'];
412d3bae478SChristopher Smith    }
413d3bae478SChristopher Smith
414d3bae478SChristopher Smith    return false;
415d3bae478SChristopher Smith}
416d3bae478SChristopher Smith
417c29dc6e4SAndreas Gohr/**
418c29dc6e4SAndreas Gohr * print debug messages
419c29dc6e4SAndreas Gohr *
420c29dc6e4SAndreas Gohr * little function to print the content of a var
421c29dc6e4SAndreas Gohr *
422c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
423f50a239bSTakamura *
424f50a239bSTakamura * @param string $msg
425f50a239bSTakamura * @param bool $hidden
426c29dc6e4SAndreas Gohr */
427c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
42813493794SAndreas Gohr    if($hidden){
42913493794SAndreas Gohr        echo "<!--\n";
430c29dc6e4SAndreas Gohr        print_r($msg);
43113493794SAndreas Gohr        echo "\n-->";
43213493794SAndreas Gohr    }else{
43313493794SAndreas Gohr        echo '<pre class="dbg">';
43413493794SAndreas Gohr        echo hsc(print_r($msg,true));
43513493794SAndreas Gohr        echo '</pre>';
43613493794SAndreas Gohr    }
437c29dc6e4SAndreas Gohr}
438c29dc6e4SAndreas Gohr
439c29dc6e4SAndreas Gohr/**
440c29dc6e4SAndreas Gohr * Print info to a log file
441c29dc6e4SAndreas Gohr *
442c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
443f50a239bSTakamura *
444f50a239bSTakamura * @param string $msg
445f50a239bSTakamura * @param string $header
446c29dc6e4SAndreas Gohr */
4470699d739SAndreas Gohrfunction dbglog($msg,$header=''){
448c29dc6e4SAndreas Gohr    global $conf;
449585bf44eSChristopher Smith    /* @var Input $INPUT */
450585bf44eSChristopher Smith    global $INPUT;
451585bf44eSChristopher Smith
4525bd930ffSMichael Hamann    // The debug log isn't automatically cleaned thus only write it when
4535bd930ffSMichael Hamann    // debugging has been enabled by the user.
4545bd930ffSMichael Hamann    if($conf['allowdebug'] !== 1) return;
455c7408a63SAndreas Gohr    if(is_object($msg) || is_array($msg)){
456c7408a63SAndreas Gohr        $msg = print_r($msg,true);
457c7408a63SAndreas Gohr    }
458c7408a63SAndreas Gohr
4590699d739SAndreas Gohr    if($header) $msg = "$header\n$msg";
4600699d739SAndreas Gohr
461c29dc6e4SAndreas Gohr    $file = $conf['cachedir'].'/debug.log';
462c29dc6e4SAndreas Gohr    $fh = fopen($file,'a');
463c29dc6e4SAndreas Gohr    if($fh){
464585bf44eSChristopher Smith        fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n");
465c29dc6e4SAndreas Gohr        fclose($fh);
466c29dc6e4SAndreas Gohr    }
467c29dc6e4SAndreas Gohr}
468c29dc6e4SAndreas Gohr
469db09e31eSAndreas Gohr/**
4701419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4711419a485SAndreas Gohr *
4721419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
47385331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
4741419a485SAndreas Gohr */
4751419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') {
4760c5eb5e2SMichael Große    \dokuwiki\Debug\DebugHelper::dbgDeprecatedFunction($alternative, 2);
47744455016SAndreas Gohr}
4781419a485SAndreas Gohr
4791419a485SAndreas Gohr/**
480db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
481db09e31eSAndreas Gohr *
482db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
483db09e31eSAndreas Gohr */
484db09e31eSAndreas Gohrfunction dbg_backtrace(){
485db09e31eSAndreas Gohr    // Get backtrace
486db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
487db09e31eSAndreas Gohr
488db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
489db09e31eSAndreas Gohr    array_shift($backtrace);
490db09e31eSAndreas Gohr
491db09e31eSAndreas Gohr    // Iterate backtrace
492db09e31eSAndreas Gohr    $calls = array();
493db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
494db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
495db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
496db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
497db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
498db09e31eSAndreas Gohr
4998259f1aaSAndreas Gohr        $params = array();
500db09e31eSAndreas Gohr        if (isset($call['args'])){
5018259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
5028259f1aaSAndreas Gohr                if(is_object($arg)){
5038259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
5048259f1aaSAndreas Gohr                }elseif(is_array($arg)){
5058259f1aaSAndreas Gohr                    $params[] = '[Array]';
5068259f1aaSAndreas Gohr                }elseif(is_null($arg)){
50759bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
5088259f1aaSAndreas Gohr                }else{
5098259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
510db09e31eSAndreas Gohr                }
5118259f1aaSAndreas Gohr            }
5128259f1aaSAndreas Gohr        }
5138259f1aaSAndreas Gohr        $params = implode(', ',$params);
514db09e31eSAndreas Gohr
5158259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
516db09e31eSAndreas Gohr                $function,
517db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
518db09e31eSAndreas Gohr                $location);
519db09e31eSAndreas Gohr    }
520db09e31eSAndreas Gohr    ksort($calls);
521db09e31eSAndreas Gohr
522db09e31eSAndreas Gohr    return implode("\n", $calls);
523db09e31eSAndreas Gohr}
524db09e31eSAndreas Gohr
52524297a69SAndreas Gohr/**
52624297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
52724297a69SAndreas Gohr *
52824297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
52924297a69SAndreas Gohr * debug output
53024297a69SAndreas Gohr *
53124297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
532f50a239bSTakamura *
533f50a239bSTakamura * @param array $data
53424297a69SAndreas Gohr */
53524297a69SAndreas Gohrfunction debug_guard(&$data){
53624297a69SAndreas Gohr    foreach($data as $key => $value){
53724297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
53824297a69SAndreas Gohr            $data[$key] = '***';
53924297a69SAndreas Gohr            continue;
54024297a69SAndreas Gohr        }
54124297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
54224297a69SAndreas Gohr    }
54324297a69SAndreas Gohr}
544