xref: /dokuwiki/inc/infoutils.php (revision f519f9dbd38c1c52b28244ed9adfab8b6ff9561b)
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 */
8fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
96c5e3c5eSPhy
106c5e3c5eSPhyif(!defined('DOKU_MESSAGEURL')){
116c5e3c5eSPhy    if(in_array('ssl', stream_get_transports())) {
126c5e3c5eSPhy        define('DOKU_MESSAGEURL','https://update.dokuwiki.org/check/');
136c5e3c5eSPhy    }else{
146c5e3c5eSPhy        define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
156c5e3c5eSPhy    }
166c5e3c5eSPhy}
17c29dc6e4SAndreas Gohr
18c29dc6e4SAndreas Gohr/**
19c29dc6e4SAndreas Gohr * Check for new messages from upstream
20c29dc6e4SAndreas Gohr *
21c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
22c29dc6e4SAndreas Gohr */
23c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){
24c29dc6e4SAndreas Gohr    global $conf;
25c29dc6e4SAndreas Gohr    global $INFO;
26ef362bb8SAnika Henke    global $updateVersion;
27c29dc6e4SAndreas Gohr    if(!$conf['updatecheck']) return;
28f8cc712eSAndreas Gohr    if($conf['useacl'] && !$INFO['ismanager']) return;
29c29dc6e4SAndreas Gohr
3037b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
31c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
326c5e3c5eSPhy    $is_http = substr(DOKU_MESSAGEURL, 0, 5) != 'https';
33c29dc6e4SAndreas Gohr
34c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
358d3d7569SAnika Henke    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
3663d9b820SAndreas Gohr        @touch($cf);
376c5e3c5eSPhy        dbglog("checkUpdateMessages(): downloading messages to ".$cf.($is_http?' (without SSL)':' (with SSL)'));
38c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
3963d9b820SAndreas Gohr        $http->timeout = 12;
4086c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL.$updateVersion);
4186c04d87SAngus Gratton        if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
4286c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
4386c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
4486c04d87SAngus Gratton            io_saveFile($cf,$resp);
458f1efc43SAndreas Gohr        } else {
4686c04d87SAngus Gratton            dbglog("checkUpdateMessages(): unexpected HTTP response received");
478f1efc43SAndreas Gohr        }
48c29dc6e4SAndreas Gohr    }else{
4937b21a1bSAndreas Gohr        dbglog("checkUpdateMessages(): messages up to date");
50c29dc6e4SAndreas Gohr    }
51c29dc6e4SAndreas Gohr
5286c04d87SAngus Gratton    $data = io_readFile($cf);
53c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
54c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n",$data);
55c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
56c29dc6e4SAndreas Gohr        if($msg) msg($msg,2);
57c29dc6e4SAndreas Gohr    }
58c29dc6e4SAndreas Gohr}
59c29dc6e4SAndreas Gohr
60c29dc6e4SAndreas Gohr
61c29dc6e4SAndreas Gohr/**
6225c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
63c29dc6e4SAndreas Gohr *
64c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
65c29dc6e4SAndreas Gohr */
6625c07f93SAnika Henkefunction getVersionData(){
6725c07f93SAnika Henke    $version = array();
68c29dc6e4SAndreas Gohr    //import version string
6979e79377SAndreas Gohr    if(file_exists(DOKU_INC.'VERSION')){
70c29dc6e4SAndreas Gohr        //official release
71d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
7225c07f93SAnika Henke        $version['type'] = 'Release';
735cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
745cf31920SAndreas Gohr        $version['type'] = 'Git';
7525c07f93SAnika Henke        $version['date'] = 'unknown';
76e570ed43SAndreas Gohr
77067b4fb9SMichael Große        if ($date = shell_exec("git log -1 --pretty=format:'%cd' --date=short")) {
78067b4fb9SMichael Große            $version['date'] = hsc($date);
79*f519f9dbSMichael Große        } else if (file_exists(DOKU_INC . '.git/HEAD')) {
80*f519f9dbSMichael Große            // we cannot use git on the shell -- let's do it manually!
81*f519f9dbSMichael Große            $headCommit = trim(file_get_contents(DOKU_INC . '.git/HEAD'));
82*f519f9dbSMichael Große            if (strpos($headCommit, 'ref: ') === 0) {
83*f519f9dbSMichael Große                // it is something like `ref: refs/heads/master`
84*f519f9dbSMichael Große                $pathToHead = substr($headCommit, 5);
85*f519f9dbSMichael Große                $headCommit = trim(file_get_contents(DOKU_INC . '.git/' . $pathToHead));
86*f519f9dbSMichael Große            }
87*f519f9dbSMichael Große            $subDir = substr($headCommit, 0, 2);
88*f519f9dbSMichael Große            $fileName = substr($headCommit, 2);
89*f519f9dbSMichael Große            $getCommitObject = DOKU_INC . ".git/objects/$subDir/$fileName";
90*f519f9dbSMichael Große            $commit = zlib_decode(file_get_contents($getCommitObject));
91*f519f9dbSMichael Große            $committerLine = explode("\n", $commit)[3];
92*f519f9dbSMichael Große            $committerData = explode(' ', $committerLine);
93*f519f9dbSMichael Große            end($committerData);
94*f519f9dbSMichael Große            $ts = prev($committerData);
95*f519f9dbSMichael Große            if ($ts && $date = date('Y-m-d', $ts)) {
96*f519f9dbSMichael Große                $version['date'] = $date;
97*f519f9dbSMichael Große            }
985cf31920SAndreas Gohr        }
99c29dc6e4SAndreas Gohr    }else{
1006a34de2dSAnika Henke        global $updateVersion;
1016a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
10225c07f93SAnika Henke        $version['type'] = 'snapshot?';
103c29dc6e4SAndreas Gohr    }
1045cf31920SAndreas Gohr    return $version;
105c29dc6e4SAndreas Gohr}
106c29dc6e4SAndreas Gohr
107c29dc6e4SAndreas Gohr/**
10825c07f93SAnika Henke * Return DokuWiki's version (as a string)
10925c07f93SAnika Henke *
11025c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
11125c07f93SAnika Henke */
11225c07f93SAnika Henkefunction getVersion(){
11325c07f93SAnika Henke    $version = getVersionData();
11425c07f93SAnika Henke    return $version['type'].' '.$version['date'];
11525c07f93SAnika Henke}
11625c07f93SAnika Henke
11725c07f93SAnika Henke/**
118c29dc6e4SAndreas Gohr * Run a few sanity checks
119c29dc6e4SAndreas Gohr *
120c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
121c29dc6e4SAndreas Gohr */
122c29dc6e4SAndreas Gohrfunction check(){
123c29dc6e4SAndreas Gohr    global $conf;
124c29dc6e4SAndreas Gohr    global $INFO;
125585bf44eSChristopher Smith    /* @var Input $INPUT */
126585bf44eSChristopher Smith    global $INPUT;
127c29dc6e4SAndreas Gohr
1283f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
129c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
130c29dc6e4SAndreas Gohr
1313476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
1323476bb81SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 5.6.0+ needed)',-1);
133c29dc6e4SAndreas Gohr        }else{
134c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
135c29dc6e4SAndreas Gohr        }
13608d1a8dfSAndreas Gohr    } else {
1373476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
13808d1a8dfSAndreas Gohr            msg('Your PHP version is too old',-1);
13908d1a8dfSAndreas Gohr        }
14008d1a8dfSAndreas Gohr    }
141c29dc6e4SAndreas Gohr
14273038c47SAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
14373038c47SAndreas Gohr    if($mem){
14473038c47SAndreas Gohr        if($mem < 16777216){
14573038c47SAndreas Gohr            msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
14673038c47SAndreas Gohr        }elseif($mem < 20971520){
14773038c47SAndreas Gohr            msg('PHP is limited to less than 20MB RAM ('.$mem.' bytes), you might encounter problems with bigger pages. Increase memory_limit in php.ini',-1);
14873038c47SAndreas Gohr        }elseif($mem < 33554432){
14973038c47SAndreas Gohr            msg('PHP is limited to less than 32MB RAM ('.$mem.' bytes), but that should be enough in most cases. If not, increase memory_limit in php.ini',0);
15073038c47SAndreas Gohr        }else{
15173038c47SAndreas Gohr            msg('More than 32MB RAM ('.$mem.' bytes) 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){
217e5ab313fSAndreas Gohr        msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale, you should fix this if you encounter problems.',0);
218e5ab313fSAndreas Gohr    }else{
219e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
220e5ab313fSAndreas Gohr    }
221e5ab313fSAndreas Gohr
222c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
223c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
224c29dc6e4SAndreas Gohr    }else{
225c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
226c29dc6e4SAndreas Gohr    }
227c29dc6e4SAndreas Gohr
2283d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
229585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
230c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2313d3c095dSMike Frysinger    }else{
2323d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2333d3c095dSMike Frysinger    }
2343d3c095dSMike Frysinger
235c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
236c29dc6e4SAndreas Gohr
237c29dc6e4SAndreas Gohr    if(is_writable($INFO['filepath'])){
238c29dc6e4SAndreas Gohr        msg('The current page is writable by the webserver',0);
239c29dc6e4SAndreas Gohr    }else{
240c29dc6e4SAndreas Gohr        msg('The current page is not writable by the webserver',0);
241c29dc6e4SAndreas Gohr    }
242c29dc6e4SAndreas Gohr
243c29dc6e4SAndreas Gohr    if($INFO['writable']){
244c29dc6e4SAndreas Gohr        msg('The current page is writable by you',0);
245c29dc6e4SAndreas Gohr    }else{
2462cdbda06SAnika Henke        msg('The current page is not writable by you',0);
247c29dc6e4SAndreas Gohr    }
2483b1dfc83SAndreas Gohr
24926f7dbf5SMichael Hamann    // Check for corrupted search index
25026f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
25126f7dbf5SMichael Hamann    $index_corrupted = false;
25226f7dbf5SMichael Hamann    foreach ($lengths as $length) {
25326f7dbf5SMichael Hamann        if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
25426f7dbf5SMichael Hamann            $index_corrupted = true;
25526f7dbf5SMichael Hamann            break;
25626f7dbf5SMichael Hamann        }
25726f7dbf5SMichael Hamann    }
25826f7dbf5SMichael Hamann
25926f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
26026f7dbf5SMichael Hamann        if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
26126f7dbf5SMichael Hamann            $index_corrupted = true;
26226f7dbf5SMichael Hamann            break;
26326f7dbf5SMichael Hamann        }
26426f7dbf5SMichael Hamann    }
26526f7dbf5SMichael Hamann
266d6c7b502SAndreas Gohr    if($index_corrupted) {
267d6c7b502SAndreas Gohr        msg(
268d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
2693d94d9edSMichael Hamann                probably needs to be rebuilt. See
27026f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
271d6c7b502SAndreas Gohr                for ways to rebuild the search index.', -1
272d6c7b502SAndreas Gohr        );
273d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
2743d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
275d6c7b502SAndreas Gohr    } else {
276d6c7b502SAndreas Gohr        msg(
277d6c7b502SAndreas Gohr            'The search index is empty. See
27826f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2793d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
280d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
281d6c7b502SAndreas Gohr        );
282d6c7b502SAndreas Gohr    }
283d6c7b502SAndreas Gohr
284d6c7b502SAndreas Gohr    // rough time check
285d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
286d6c7b502SAndreas Gohr    $http->max_redirect = 0;
287d6c7b502SAndreas Gohr    $http->timeout = 3;
288d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
289d6c7b502SAndreas Gohr    $now = time();
290d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
291d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
292d6c7b502SAndreas Gohr        $diff = $time - $now;
293d6c7b502SAndreas Gohr
294d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
295d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
296d6c7b502SAndreas Gohr        } else {
297d6c7b502SAndreas Gohr            msg("Your server's clock seems to be out of sync! Consider configuring a sync with a NTP server.  Diff: {$diff}s");
298d6c7b502SAndreas Gohr        }
299d6c7b502SAndreas Gohr    }
300d6c7b502SAndreas Gohr
301c29dc6e4SAndreas Gohr}
302c29dc6e4SAndreas Gohr
303c29dc6e4SAndreas Gohr/**
304c29dc6e4SAndreas Gohr * print a message
305c29dc6e4SAndreas Gohr *
306c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
307c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
308c29dc6e4SAndreas Gohr * using html_msgarea()
309c29dc6e4SAndreas Gohr *
310c29dc6e4SAndreas Gohr *
311c29dc6e4SAndreas Gohr * Levels can be:
312c29dc6e4SAndreas Gohr *
313c29dc6e4SAndreas Gohr * -1 error
314c29dc6e4SAndreas Gohr *  0 info
315c29dc6e4SAndreas Gohr *  1 success
316c29dc6e4SAndreas Gohr *
317c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
318c29dc6e4SAndreas Gohr * @see    html_msgarea
319c29dc6e4SAndreas Gohr */
320d3bae478SChristopher Smith
321d3bae478SChristopher Smithdefine('MSG_PUBLIC', 0);
322d3bae478SChristopher Smithdefine('MSG_USERS_ONLY', 1);
323d3bae478SChristopher Smithdefine('MSG_MANAGERS_ONLY',2);
324d3bae478SChristopher Smithdefine('MSG_ADMINS_ONLY',4);
325d3bae478SChristopher Smith
3266164d900SAndreas Gohr/**
3276164d900SAndreas Gohr * Display a message to the user
3286164d900SAndreas Gohr *
3296164d900SAndreas Gohr * @param string $message
3306164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3316164d900SAndreas Gohr * @param string $line  line number
3326164d900SAndreas Gohr * @param string $file  file number
3336164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3346164d900SAndreas Gohr */
335f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
336cc58224cSMichael Hamann    global $MSG, $MSG_shown;
33759bc3b48SGerrit Uitslag    $errors = array();
338c29dc6e4SAndreas Gohr    $errors[-1] = 'error';
339c29dc6e4SAndreas Gohr    $errors[0]  = 'info';
340c29dc6e4SAndreas Gohr    $errors[1]  = 'success';
341c29dc6e4SAndreas Gohr    $errors[2]  = 'notify';
342c29dc6e4SAndreas Gohr
3433009a773SAndreas Gohr    if($line || $file) $message.=' ['.utf8_basename($file).':'.$line.']';
344c29dc6e4SAndreas Gohr
345c29dc6e4SAndreas Gohr    if(!isset($MSG)) $MSG = array();
346f755f9abSChristopher Smith    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message, 'allow' => $allow);
347cc58224cSMichael Hamann    if(isset($MSG_shown) || headers_sent()){
348c29dc6e4SAndreas Gohr        if(function_exists('html_msgarea')){
349c29dc6e4SAndreas Gohr            html_msgarea();
350c29dc6e4SAndreas Gohr        }else{
351c29dc6e4SAndreas Gohr            print "ERROR($lvl) $message";
352c29dc6e4SAndreas Gohr        }
35369266de5SDominik Eckelmann        unset($GLOBALS['MSG']);
354c29dc6e4SAndreas Gohr    }
355c29dc6e4SAndreas Gohr}
356f755f9abSChristopher Smith/**
357f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
358f755f9abSChristopher Smith * in the $msg data structure
359f755f9abSChristopher Smith *
360f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
361f755f9abSChristopher Smith *                         msg   => string, the message
362f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
363f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
364f755f9abSChristopher Smith *                                       see MSG_* constants
3656164d900SAndreas Gohr * @return bool
366f755f9abSChristopher Smith */
367f755f9abSChristopher Smithfunction info_msg_allowed($msg){
368d3bae478SChristopher Smith    global $INFO, $auth;
369d3bae478SChristopher Smith
370d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
371f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
372d3bae478SChristopher Smith
373d3bae478SChristopher Smith    // restricted msg, but no authentication
374d3bae478SChristopher Smith    if (empty($auth)) return false;
375d3bae478SChristopher Smith
376f755f9abSChristopher Smith    switch ($msg['allow']){
377d3bae478SChristopher Smith        case MSG_USERS_ONLY:
378d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
379d3bae478SChristopher Smith
380d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
381d3bae478SChristopher Smith            return $INFO['ismanager'];
382d3bae478SChristopher Smith
383d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
384d3bae478SChristopher Smith            return $INFO['isadmin'];
385d3bae478SChristopher Smith
386d3bae478SChristopher Smith        default:
387f755f9abSChristopher Smith            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"', E_USER_WARNING);
388d3bae478SChristopher Smith            return $INFO['isadmin'];
389d3bae478SChristopher Smith    }
390d3bae478SChristopher Smith
391d3bae478SChristopher Smith    return false;
392d3bae478SChristopher Smith}
393d3bae478SChristopher Smith
394c29dc6e4SAndreas Gohr/**
395c29dc6e4SAndreas Gohr * print debug messages
396c29dc6e4SAndreas Gohr *
397c29dc6e4SAndreas Gohr * little function to print the content of a var
398c29dc6e4SAndreas Gohr *
399c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
400f50a239bSTakamura *
401f50a239bSTakamura * @param string $msg
402f50a239bSTakamura * @param bool $hidden
403c29dc6e4SAndreas Gohr */
404c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
40513493794SAndreas Gohr    if($hidden){
40613493794SAndreas Gohr        echo "<!--\n";
407c29dc6e4SAndreas Gohr        print_r($msg);
40813493794SAndreas Gohr        echo "\n-->";
40913493794SAndreas Gohr    }else{
41013493794SAndreas Gohr        echo '<pre class="dbg">';
41113493794SAndreas Gohr        echo hsc(print_r($msg,true));
41213493794SAndreas Gohr        echo '</pre>';
41313493794SAndreas Gohr    }
414c29dc6e4SAndreas Gohr}
415c29dc6e4SAndreas Gohr
416c29dc6e4SAndreas Gohr/**
417c29dc6e4SAndreas Gohr * Print info to a log file
418c29dc6e4SAndreas Gohr *
419c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
420f50a239bSTakamura *
421f50a239bSTakamura * @param string $msg
422f50a239bSTakamura * @param string $header
423c29dc6e4SAndreas Gohr */
4240699d739SAndreas Gohrfunction dbglog($msg,$header=''){
425c29dc6e4SAndreas Gohr    global $conf;
426585bf44eSChristopher Smith    /* @var Input $INPUT */
427585bf44eSChristopher Smith    global $INPUT;
428585bf44eSChristopher Smith
4295bd930ffSMichael Hamann    // The debug log isn't automatically cleaned thus only write it when
4305bd930ffSMichael Hamann    // debugging has been enabled by the user.
4315bd930ffSMichael Hamann    if($conf['allowdebug'] !== 1) return;
432c7408a63SAndreas Gohr    if(is_object($msg) || is_array($msg)){
433c7408a63SAndreas Gohr        $msg = print_r($msg,true);
434c7408a63SAndreas Gohr    }
435c7408a63SAndreas Gohr
4360699d739SAndreas Gohr    if($header) $msg = "$header\n$msg";
4370699d739SAndreas Gohr
438c29dc6e4SAndreas Gohr    $file = $conf['cachedir'].'/debug.log';
439c29dc6e4SAndreas Gohr    $fh = fopen($file,'a');
440c29dc6e4SAndreas Gohr    if($fh){
441585bf44eSChristopher Smith        fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n");
442c29dc6e4SAndreas Gohr        fclose($fh);
443c29dc6e4SAndreas Gohr    }
444c29dc6e4SAndreas Gohr}
445c29dc6e4SAndreas Gohr
446db09e31eSAndreas Gohr/**
4471419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4481419a485SAndreas Gohr *
4491419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
45085331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
4511419a485SAndreas Gohr */
4521419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') {
45385331086SAndreas Gohr    global $conf;
45485331086SAndreas Gohr    global $EVENT_HANDLER;
45585331086SAndreas Gohr    if(!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG')) {
45685331086SAndreas Gohr        // avoid any work if no one cares
45785331086SAndreas Gohr        return;
45885331086SAndreas Gohr    }
45985331086SAndreas Gohr
4601419a485SAndreas Gohr    $backtrace = debug_backtrace();
4611419a485SAndreas Gohr    array_shift($backtrace);
46244455016SAndreas Gohr    $self = $backtrace[0];
46344455016SAndreas Gohr    $call = $backtrace[1];
4641419a485SAndreas Gohr
46544455016SAndreas Gohr    $data = [
46644455016SAndreas Gohr        'trace' => $backtrace,
46744455016SAndreas Gohr        'alternative' => $alternative,
46844455016SAndreas Gohr        'called' => trim($self['class'] . '::' . $self['function'] . '()', ':'),
46944455016SAndreas Gohr        'caller' => trim($call['class'] . '::' . $call['function'] . '()', ':'),
47044455016SAndreas Gohr        'file' => $call['file'],
47144455016SAndreas Gohr        'line' => $call['line'],
47244455016SAndreas Gohr    ];
4731419a485SAndreas Gohr
47444455016SAndreas Gohr    $event = new Doku_Event('INFO_DEPRECATION_LOG', $data);
47544455016SAndreas Gohr    if($event->advise_before()) {
47644455016SAndreas Gohr        $msg = $event->data['called'] . ' is deprecated. It was called from ';
47744455016SAndreas Gohr        $msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line'];
47844455016SAndreas Gohr        if($event->data['alternative']) {
47944455016SAndreas Gohr            $msg .= ' ' . $event->data['alternative'] . ' should be used instead!';
4801419a485SAndreas Gohr        }
4811419a485SAndreas Gohr        dbglog($msg);
4821419a485SAndreas Gohr    }
48344455016SAndreas Gohr    $event->advise_after();
48444455016SAndreas Gohr}
4851419a485SAndreas Gohr
4861419a485SAndreas Gohr/**
487db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
488db09e31eSAndreas Gohr *
489db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
490db09e31eSAndreas Gohr */
491db09e31eSAndreas Gohrfunction dbg_backtrace(){
492db09e31eSAndreas Gohr    // Get backtrace
493db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
494db09e31eSAndreas Gohr
495db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
496db09e31eSAndreas Gohr    array_shift($backtrace);
497db09e31eSAndreas Gohr
498db09e31eSAndreas Gohr    // Iterate backtrace
499db09e31eSAndreas Gohr    $calls = array();
500db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
501db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
502db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
503db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
504db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
505db09e31eSAndreas Gohr
5068259f1aaSAndreas Gohr        $params = array();
507db09e31eSAndreas Gohr        if (isset($call['args'])){
5088259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
5098259f1aaSAndreas Gohr                if(is_object($arg)){
5108259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
5118259f1aaSAndreas Gohr                }elseif(is_array($arg)){
5128259f1aaSAndreas Gohr                    $params[] = '[Array]';
5138259f1aaSAndreas Gohr                }elseif(is_null($arg)){
51459bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
5158259f1aaSAndreas Gohr                }else{
5168259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
517db09e31eSAndreas Gohr                }
5188259f1aaSAndreas Gohr            }
5198259f1aaSAndreas Gohr        }
5208259f1aaSAndreas Gohr        $params = implode(', ',$params);
521db09e31eSAndreas Gohr
5228259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
523db09e31eSAndreas Gohr                $function,
524db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
525db09e31eSAndreas Gohr                $location);
526db09e31eSAndreas Gohr    }
527db09e31eSAndreas Gohr    ksort($calls);
528db09e31eSAndreas Gohr
529db09e31eSAndreas Gohr    return implode("\n", $calls);
530db09e31eSAndreas Gohr}
531db09e31eSAndreas Gohr
53224297a69SAndreas Gohr/**
53324297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
53424297a69SAndreas Gohr *
53524297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
53624297a69SAndreas Gohr * debug output
53724297a69SAndreas Gohr *
53824297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
539f50a239bSTakamura *
540f50a239bSTakamura * @param array $data
54124297a69SAndreas Gohr */
54224297a69SAndreas Gohrfunction debug_guard(&$data){
54324297a69SAndreas Gohr    foreach($data as $key => $value){
54424297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
54524297a69SAndreas Gohr            $data[$key] = '***';
54624297a69SAndreas Gohr            continue;
54724297a69SAndreas Gohr        }
54824297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
54924297a69SAndreas Gohr    }
55024297a69SAndreas Gohr}
551