xref: /dokuwiki/inc/infoutils.php (revision be5c1ea2da9ce21461b05de9ad624446de1786be)
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;
10*be5c1ea2SSatoshi Saharause dokuwiki\Search\MetadataIndex;
11*be5c1ea2SSatoshi Saharause dokuwiki\Search\PagewordIndex;
12*be5c1ea2SSatoshi Saharause dokuwiki\Utf8;
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);
416c5e3c5eSPhy        dbglog("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 {
5086c04d87SAngus Gratton            dbglog("checkUpdateMessages(): unexpected HTTP response received");
518f1efc43SAndreas Gohr        }
52c29dc6e4SAndreas Gohr    }else{
5337b21a1bSAndreas Gohr        dbglog("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(){
7125c07f93SAnika Henke    $version = array();
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
815cf31920SAndreas Gohr        $inventory = DOKU_INC.'.git/logs/HEAD';
825cf31920SAndreas Gohr        if(is_file($inventory)){
83e570ed43SAndreas Gohr            $sz   = filesize($inventory);
845cf31920SAndreas Gohr            $seek = max(0,$sz-2000); // read from back of the file
85e570ed43SAndreas Gohr            $fh   = fopen($inventory,'rb');
86c29dc6e4SAndreas Gohr            fseek($fh,$seek);
87c29dc6e4SAndreas Gohr            $chunk = fread($fh,2000);
88c29dc6e4SAndreas Gohr            fclose($fh);
895cf31920SAndreas Gohr            $chunk = trim($chunk);
90b838050eSPiyush Mishra            $chunk = @array_pop(explode("\n",$chunk));   //last log line
91b838050eSPiyush Mishra            $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg
925cf31920SAndreas Gohr            $chunk = explode(" ",$chunk);
935cf31920SAndreas Gohr            array_pop($chunk); //strip timezone
945cf31920SAndreas Gohr            $date = date('Y-m-d',array_pop($chunk));
955cf31920SAndreas Gohr            if($date) $version['date'] = $date;
965cf31920SAndreas Gohr        }
97c29dc6e4SAndreas Gohr    }else{
986a34de2dSAnika Henke        global $updateVersion;
996a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
10025c07f93SAnika Henke        $version['type'] = 'snapshot?';
101c29dc6e4SAndreas Gohr    }
1025cf31920SAndreas Gohr    return $version;
103c29dc6e4SAndreas Gohr}
104c29dc6e4SAndreas Gohr
105c29dc6e4SAndreas Gohr/**
10625c07f93SAnika Henke * Return DokuWiki's version (as a string)
10725c07f93SAnika Henke *
10825c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
10925c07f93SAnika Henke */
11025c07f93SAnika Henkefunction getVersion(){
11125c07f93SAnika Henke    $version = getVersionData();
11225c07f93SAnika Henke    return $version['type'].' '.$version['date'];
11325c07f93SAnika Henke}
11425c07f93SAnika Henke
11525c07f93SAnika Henke/**
116c29dc6e4SAndreas Gohr * Run a few sanity checks
117c29dc6e4SAndreas Gohr *
118c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
119c29dc6e4SAndreas Gohr */
120c29dc6e4SAndreas Gohrfunction check(){
121c29dc6e4SAndreas Gohr    global $conf;
122c29dc6e4SAndreas Gohr    global $INFO;
123585bf44eSChristopher Smith    /* @var Input $INPUT */
124585bf44eSChristopher Smith    global $INPUT;
125c29dc6e4SAndreas Gohr
1263f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
127c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
128c29dc6e4SAndreas Gohr
1293476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
1303476bb81SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 5.6.0+ needed)',-1);
131c29dc6e4SAndreas Gohr        }else{
132c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
133c29dc6e4SAndreas Gohr        }
13408d1a8dfSAndreas Gohr    } else {
1353476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
13608d1a8dfSAndreas Gohr            msg('Your PHP version is too old',-1);
13708d1a8dfSAndreas Gohr        }
13808d1a8dfSAndreas Gohr    }
139c6e971ddSAndreas Gohr
140c6e971ddSAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
14173038c47SAndreas Gohr    if($mem){
142eb2e46caSAndreas Gohr        if ($mem === -1) {
1438f8499faSpeterfromearth            msg('PHP memory is unlimited', 1);
1448f8499faSpeterfromearth        } else if ($mem < 16777216) {
1452b9c4a05SAndreas Gohr            msg('PHP is limited to less than 16MB RAM (' . filesize_h($mem) . ').
1462b9c4a05SAndreas Gohr            Increase memory_limit in php.ini', -1);
14773038c47SAndreas Gohr        } else if ($mem < 20971520) {
1482b9c4a05SAndreas Gohr            msg('PHP is limited to less than 20MB RAM (' . filesize_h($mem) . '),
14964159a61SAndreas Gohr                you might encounter problems with bigger pages. Increase memory_limit in php.ini', -1);
15073038c47SAndreas Gohr        } else if ($mem < 33554432) {
1512b9c4a05SAndreas Gohr            msg('PHP is limited to less than 32MB RAM (' . filesize_h($mem) . '),
15264159a61SAndreas Gohr                but that should be enough in most cases. If not, increase memory_limit in php.ini', 0);
15373038c47SAndreas Gohr        } else {
154c6e971ddSAndreas Gohr            msg('More than 32MB RAM (' . filesize_h($mem) . ') available.', 1);
15573038c47SAndreas Gohr        }
15673038c47SAndreas Gohr    }
15773038c47SAndreas Gohr
158c29dc6e4SAndreas Gohr    if(is_writable($conf['changelog'])){
159c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
160c29dc6e4SAndreas Gohr    }else{
16179e79377SAndreas Gohr        if (file_exists($conf['changelog'])) {
162c29dc6e4SAndreas Gohr            msg('Changelog is not writable',-1);
163c29dc6e4SAndreas Gohr        }
164c29dc6e4SAndreas Gohr    }
165c29dc6e4SAndreas Gohr
16679e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1672cdbda06SAnika Henke        msg('Old changelog exists', 0);
168c29dc6e4SAndreas Gohr    }
169c29dc6e4SAndreas Gohr
17079e79377SAndreas Gohr    if (file_exists($conf['changelog'].'_failed')) {
1712cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
17279e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_importing')) {
173c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
17479e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_import_ok')) {
1752cdbda06SAnika Henke        msg('Old changelog imported', 1);
176c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
1772cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
178c29dc6e4SAndreas Gohr        }
179c29dc6e4SAndreas Gohr    }
180c29dc6e4SAndreas Gohr
1814c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
1824c7ecf15SGuy Brand        msg('conf directory is writable',1);
1834c7ecf15SGuy Brand    }else{
1844c7ecf15SGuy Brand        msg('conf directory is not writable',-1);
1854c7ecf15SGuy Brand    }
1864c7ecf15SGuy Brand
1870d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
188defb7d57SAnika Henke        global $config_cascade;
189defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
190c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
191c29dc6e4SAndreas Gohr        }else{
192c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
193c29dc6e4SAndreas Gohr        }
1940d487d8fSAndreas Gohr    }
195c29dc6e4SAndreas Gohr
196c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
197c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
198c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
199c29dc6e4SAndreas Gohr        }else{
200c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
2014222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
2024222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
2034222b898SAndreas Gohr            }
204c29dc6e4SAndreas Gohr        }
205c29dc6e4SAndreas Gohr    }else{
206c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
207c29dc6e4SAndreas Gohr    }
208c29dc6e4SAndreas Gohr
2093161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
210a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
211a731ed1dSAndreas Gohr    }
2123161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
213a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
214a731ed1dSAndreas Gohr    }
215a731ed1dSAndreas Gohr
216e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
217e5ab313fSAndreas Gohr    if(!$loc){
218e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this',-1);
219e5ab313fSAndreas Gohr    }elseif(stripos($loc,'utf') === false){
22064159a61SAndreas Gohr        msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale,
22164159a61SAndreas Gohr             you should fix this if you encounter problems.',0);
222e5ab313fSAndreas Gohr    }else{
223e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
224e5ab313fSAndreas Gohr    }
225e5ab313fSAndreas Gohr
226c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
227c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
228c29dc6e4SAndreas Gohr    }else{
229c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
230c29dc6e4SAndreas Gohr    }
231c29dc6e4SAndreas Gohr
2323d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
233585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
234c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2353d3c095dSMike Frysinger    }else{
2363d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2373d3c095dSMike Frysinger    }
2383d3c095dSMike Frysinger
239c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
240c29dc6e4SAndreas Gohr
241c29dc6e4SAndreas Gohr    if(is_writable($INFO['filepath'])){
242c29dc6e4SAndreas Gohr        msg('The current page is writable by the webserver',0);
243c29dc6e4SAndreas Gohr    }else{
244c29dc6e4SAndreas Gohr        msg('The current page is not writable by the webserver',0);
245c29dc6e4SAndreas Gohr    }
246c29dc6e4SAndreas Gohr
247c29dc6e4SAndreas Gohr    if($INFO['writable']){
248c29dc6e4SAndreas Gohr        msg('The current page is writable by you',0);
249c29dc6e4SAndreas Gohr    }else{
2502cdbda06SAnika Henke        msg('The current page is not writable by you',0);
251c29dc6e4SAndreas Gohr    }
2523b1dfc83SAndreas Gohr
2533f4a342bSSatoshi Sahara    // Check for corrupted fulltext search index
254*be5c1ea2SSatoshi Sahara    $PagewordIndex = PagewordIndex::getInstance();
255*be5c1ea2SSatoshi Sahara    $lengths = $PagewordIndex->listIndexLengths();
25626f7dbf5SMichael Hamann    $index_corrupted = false;
25726f7dbf5SMichael Hamann    foreach ($lengths as $length) {
258*be5c1ea2SSatoshi Sahara        if (count($PagewordIndex->getIndex('w', $length)) != count($PagewordIndex->getIndex('i', $length))) {
25926f7dbf5SMichael Hamann            $index_corrupted = true;
26026f7dbf5SMichael Hamann            break;
26126f7dbf5SMichael Hamann        }
26226f7dbf5SMichael Hamann    }
26326f7dbf5SMichael Hamann
2643f4a342bSSatoshi Sahara    // Check for corrupted metadata index
265*be5c1ea2SSatoshi Sahara    $MetadataIndex = MetadataIndex::getInstance();
266*be5c1ea2SSatoshi Sahara    foreach ($MetadataIndex->getIndex('metadata', '') as $name) {
267*be5c1ea2SSatoshi Sahara        if (count($MetadataIndex->getIndex($name.'_w', '')) != count($MetadataIndex->getIndex($name.'_i', ''))) {
26826f7dbf5SMichael Hamann            $index_corrupted = true;
26926f7dbf5SMichael Hamann            break;
27026f7dbf5SMichael Hamann        }
27126f7dbf5SMichael Hamann    }
27226f7dbf5SMichael Hamann
273d6c7b502SAndreas Gohr    if($index_corrupted) {
274d6c7b502SAndreas Gohr        msg(
275d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
2763d94d9edSMichael Hamann                probably needs to be rebuilt. See
27726f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
278d6c7b502SAndreas Gohr                for ways to rebuild the search index.', -1
279d6c7b502SAndreas Gohr        );
280d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
2813d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
282d6c7b502SAndreas Gohr    } else {
283d6c7b502SAndreas Gohr        msg(
284d6c7b502SAndreas Gohr            'The search index is empty. See
28526f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2863d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
287d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
288d6c7b502SAndreas Gohr        );
289d6c7b502SAndreas Gohr    }
290d6c7b502SAndreas Gohr
291d6c7b502SAndreas Gohr    // rough time check
292d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
293d6c7b502SAndreas Gohr    $http->max_redirect = 0;
294d6c7b502SAndreas Gohr    $http->timeout = 3;
295d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
296d6c7b502SAndreas Gohr    $now = time();
297d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
298d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
299d6c7b502SAndreas Gohr        $diff = $time - $now;
300d6c7b502SAndreas Gohr
301d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
302d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
303d6c7b502SAndreas Gohr        } else {
30464159a61SAndreas Gohr            msg("Your server's clock seems to be out of sync!
30564159a61SAndreas Gohr                 Consider configuring a sync with a NTP server.  Diff: {$diff}s");
306d6c7b502SAndreas Gohr        }
307d6c7b502SAndreas Gohr    }
308d6c7b502SAndreas Gohr
309c29dc6e4SAndreas Gohr}
310c29dc6e4SAndreas Gohr
311c29dc6e4SAndreas Gohr/**
312c29dc6e4SAndreas Gohr * print a message
313c29dc6e4SAndreas Gohr *
314c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
315c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
316c29dc6e4SAndreas Gohr * using html_msgarea()
317c29dc6e4SAndreas Gohr *
318c29dc6e4SAndreas Gohr *
319c29dc6e4SAndreas Gohr * Levels can be:
320c29dc6e4SAndreas Gohr *
321c29dc6e4SAndreas Gohr * -1 error
322c29dc6e4SAndreas Gohr *  0 info
323c29dc6e4SAndreas Gohr *  1 success
324c29dc6e4SAndreas Gohr *
325c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
326c29dc6e4SAndreas Gohr * @see    html_msgarea
327c29dc6e4SAndreas Gohr */
328d3bae478SChristopher Smith
329d3bae478SChristopher Smithdefine('MSG_PUBLIC', 0);
330d3bae478SChristopher Smithdefine('MSG_USERS_ONLY', 1);
331d3bae478SChristopher Smithdefine('MSG_MANAGERS_ONLY',2);
332d3bae478SChristopher Smithdefine('MSG_ADMINS_ONLY',4);
333d3bae478SChristopher Smith
3346164d900SAndreas Gohr/**
3356164d900SAndreas Gohr * Display a message to the user
3366164d900SAndreas Gohr *
3376164d900SAndreas Gohr * @param string $message
3386164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3396164d900SAndreas Gohr * @param string $line  line number
3406164d900SAndreas Gohr * @param string $file  file number
3416164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3426164d900SAndreas Gohr */
343f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
344cc58224cSMichael Hamann    global $MSG, $MSG_shown;
34559bc3b48SGerrit Uitslag    $errors = array();
346c29dc6e4SAndreas Gohr    $errors[-1] = 'error';
347c29dc6e4SAndreas Gohr    $errors[0]  = 'info';
348c29dc6e4SAndreas Gohr    $errors[1]  = 'success';
349c29dc6e4SAndreas Gohr    $errors[2]  = 'notify';
350c29dc6e4SAndreas Gohr
351*be5c1ea2SSatoshi Sahara    if($line || $file) $message.=' ['.Utf8\PhpString::basename($file).':'.$line.']';
352c29dc6e4SAndreas Gohr
353c29dc6e4SAndreas Gohr    if(!isset($MSG)) $MSG = array();
354f755f9abSChristopher Smith    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message, 'allow' => $allow);
355cc58224cSMichael Hamann    if(isset($MSG_shown) || headers_sent()){
356c29dc6e4SAndreas Gohr        if(function_exists('html_msgarea')){
357c29dc6e4SAndreas Gohr            html_msgarea();
358c29dc6e4SAndreas Gohr        }else{
359c29dc6e4SAndreas Gohr            print "ERROR($lvl) $message";
360c29dc6e4SAndreas Gohr        }
36169266de5SDominik Eckelmann        unset($GLOBALS['MSG']);
362c29dc6e4SAndreas Gohr    }
363c29dc6e4SAndreas Gohr}
364f755f9abSChristopher Smith/**
365f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
366f755f9abSChristopher Smith * in the $msg data structure
367f755f9abSChristopher Smith *
368f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
369f755f9abSChristopher Smith *                         msg   => string, the message
370f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
371f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
372f755f9abSChristopher Smith *                                       see MSG_* constants
3736164d900SAndreas Gohr * @return bool
374f755f9abSChristopher Smith */
375f755f9abSChristopher Smithfunction info_msg_allowed($msg){
376d3bae478SChristopher Smith    global $INFO, $auth;
377d3bae478SChristopher Smith
378d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
379f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
380d3bae478SChristopher Smith
381d3bae478SChristopher Smith    // restricted msg, but no authentication
382d3bae478SChristopher Smith    if (empty($auth)) return false;
383d3bae478SChristopher Smith
384f755f9abSChristopher Smith    switch ($msg['allow']){
385d3bae478SChristopher Smith        case MSG_USERS_ONLY:
386d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
387d3bae478SChristopher Smith
388d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
389d3bae478SChristopher Smith            return $INFO['ismanager'];
390d3bae478SChristopher Smith
391d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
392d3bae478SChristopher Smith            return $INFO['isadmin'];
393d3bae478SChristopher Smith
394d3bae478SChristopher Smith        default:
39564159a61SAndreas Gohr            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"',
39664159a61SAndreas Gohr                          E_USER_WARNING);
397d3bae478SChristopher Smith            return $INFO['isadmin'];
398d3bae478SChristopher Smith    }
399d3bae478SChristopher Smith
400d3bae478SChristopher Smith    return false;
401d3bae478SChristopher Smith}
402d3bae478SChristopher Smith
403c29dc6e4SAndreas Gohr/**
404c29dc6e4SAndreas Gohr * print debug messages
405c29dc6e4SAndreas Gohr *
406c29dc6e4SAndreas Gohr * little function to print the content of a var
407c29dc6e4SAndreas Gohr *
408c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
409f50a239bSTakamura *
410f50a239bSTakamura * @param string $msg
411f50a239bSTakamura * @param bool $hidden
412c29dc6e4SAndreas Gohr */
413c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
41413493794SAndreas Gohr    if($hidden){
41513493794SAndreas Gohr        echo "<!--\n";
416c29dc6e4SAndreas Gohr        print_r($msg);
41713493794SAndreas Gohr        echo "\n-->";
41813493794SAndreas Gohr    }else{
41913493794SAndreas Gohr        echo '<pre class="dbg">';
42013493794SAndreas Gohr        echo hsc(print_r($msg,true));
42113493794SAndreas Gohr        echo '</pre>';
42213493794SAndreas Gohr    }
423c29dc6e4SAndreas Gohr}
424c29dc6e4SAndreas Gohr
425c29dc6e4SAndreas Gohr/**
426c29dc6e4SAndreas Gohr * Print info to a log file
427c29dc6e4SAndreas Gohr *
428c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
429f50a239bSTakamura *
430f50a239bSTakamura * @param string $msg
431f50a239bSTakamura * @param string $header
432c29dc6e4SAndreas Gohr */
4330699d739SAndreas Gohrfunction dbglog($msg,$header=''){
434c29dc6e4SAndreas Gohr    global $conf;
435585bf44eSChristopher Smith    /* @var Input $INPUT */
436585bf44eSChristopher Smith    global $INPUT;
437585bf44eSChristopher Smith
4385bd930ffSMichael Hamann    // The debug log isn't automatically cleaned thus only write it when
4395bd930ffSMichael Hamann    // debugging has been enabled by the user.
4405bd930ffSMichael Hamann    if($conf['allowdebug'] !== 1) return;
441c7408a63SAndreas Gohr    if(is_object($msg) || is_array($msg)){
442c7408a63SAndreas Gohr        $msg = print_r($msg,true);
443c7408a63SAndreas Gohr    }
444c7408a63SAndreas Gohr
4450699d739SAndreas Gohr    if($header) $msg = "$header\n$msg";
4460699d739SAndreas Gohr
447c29dc6e4SAndreas Gohr    $file = $conf['cachedir'].'/debug.log';
448c29dc6e4SAndreas Gohr    $fh = fopen($file,'a');
449c29dc6e4SAndreas Gohr    if($fh){
450585bf44eSChristopher Smith        fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n");
451c29dc6e4SAndreas Gohr        fclose($fh);
452c29dc6e4SAndreas Gohr    }
453c29dc6e4SAndreas Gohr}
454c29dc6e4SAndreas Gohr
455db09e31eSAndreas Gohr/**
4561419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4571419a485SAndreas Gohr *
4581419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
45985331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
4601419a485SAndreas Gohr */
4611419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') {
4620c5eb5e2SMichael Große    \dokuwiki\Debug\DebugHelper::dbgDeprecatedFunction($alternative, 2);
46344455016SAndreas Gohr}
4641419a485SAndreas Gohr
4651419a485SAndreas Gohr/**
466db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
467db09e31eSAndreas Gohr *
468db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
469db09e31eSAndreas Gohr */
470db09e31eSAndreas Gohrfunction dbg_backtrace(){
471db09e31eSAndreas Gohr    // Get backtrace
472db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
473db09e31eSAndreas Gohr
474db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
475db09e31eSAndreas Gohr    array_shift($backtrace);
476db09e31eSAndreas Gohr
477db09e31eSAndreas Gohr    // Iterate backtrace
478db09e31eSAndreas Gohr    $calls = array();
479db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
480db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
481db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
482db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
483db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
484db09e31eSAndreas Gohr
4858259f1aaSAndreas Gohr        $params = array();
486db09e31eSAndreas Gohr        if (isset($call['args'])){
4878259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
4888259f1aaSAndreas Gohr                if(is_object($arg)){
4898259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
4908259f1aaSAndreas Gohr                }elseif(is_array($arg)){
4918259f1aaSAndreas Gohr                    $params[] = '[Array]';
4928259f1aaSAndreas Gohr                }elseif(is_null($arg)){
49359bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
4948259f1aaSAndreas Gohr                }else{
4958259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
496db09e31eSAndreas Gohr                }
4978259f1aaSAndreas Gohr            }
4988259f1aaSAndreas Gohr        }
4998259f1aaSAndreas Gohr        $params = implode(', ',$params);
500db09e31eSAndreas Gohr
5018259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
502db09e31eSAndreas Gohr                $function,
503db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
504db09e31eSAndreas Gohr                $location);
505db09e31eSAndreas Gohr    }
506db09e31eSAndreas Gohr    ksort($calls);
507db09e31eSAndreas Gohr
508db09e31eSAndreas Gohr    return implode("\n", $calls);
509db09e31eSAndreas Gohr}
510db09e31eSAndreas Gohr
51124297a69SAndreas Gohr/**
51224297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
51324297a69SAndreas Gohr *
51424297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
51524297a69SAndreas Gohr * debug output
51624297a69SAndreas Gohr *
51724297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
518f50a239bSTakamura *
519f50a239bSTakamura * @param array $data
52024297a69SAndreas Gohr */
52124297a69SAndreas Gohrfunction debug_guard(&$data){
52224297a69SAndreas Gohr    foreach($data as $key => $value){
52324297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
52424297a69SAndreas Gohr            $data[$key] = '***';
52524297a69SAndreas Gohr            continue;
52624297a69SAndreas Gohr        }
52724297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
52824297a69SAndreas Gohr    }
52924297a69SAndreas Gohr}
530