xref: /dokuwiki/inc/infoutils.php (revision 067b4fb970c56586acf5f06c80b8fc5049552dbd)
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
77*067b4fb9SMichael Große        if ($date = shell_exec("git log -1 --pretty=format:'%cd' --date=short")) {
78*067b4fb9SMichael Große            $version['date'] = hsc($date);
795cf31920SAndreas Gohr        }
80c29dc6e4SAndreas Gohr    }else{
816a34de2dSAnika Henke        global $updateVersion;
826a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
8325c07f93SAnika Henke        $version['type'] = 'snapshot?';
84c29dc6e4SAndreas Gohr    }
855cf31920SAndreas Gohr    return $version;
86c29dc6e4SAndreas Gohr}
87c29dc6e4SAndreas Gohr
88c29dc6e4SAndreas Gohr/**
8925c07f93SAnika Henke * Return DokuWiki's version (as a string)
9025c07f93SAnika Henke *
9125c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
9225c07f93SAnika Henke */
9325c07f93SAnika Henkefunction getVersion(){
9425c07f93SAnika Henke    $version = getVersionData();
9525c07f93SAnika Henke    return $version['type'].' '.$version['date'];
9625c07f93SAnika Henke}
9725c07f93SAnika Henke
9825c07f93SAnika Henke/**
99c29dc6e4SAndreas Gohr * Run a few sanity checks
100c29dc6e4SAndreas Gohr *
101c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
102c29dc6e4SAndreas Gohr */
103c29dc6e4SAndreas Gohrfunction check(){
104c29dc6e4SAndreas Gohr    global $conf;
105c29dc6e4SAndreas Gohr    global $INFO;
106585bf44eSChristopher Smith    /* @var Input $INPUT */
107585bf44eSChristopher Smith    global $INPUT;
108c29dc6e4SAndreas Gohr
1093f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
110c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
111c29dc6e4SAndreas Gohr
1123476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
1133476bb81SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 5.6.0+ needed)',-1);
114c29dc6e4SAndreas Gohr        }else{
115c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
116c29dc6e4SAndreas Gohr        }
11708d1a8dfSAndreas Gohr    } else {
1183476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
11908d1a8dfSAndreas Gohr            msg('Your PHP version is too old',-1);
12008d1a8dfSAndreas Gohr        }
12108d1a8dfSAndreas Gohr    }
122c29dc6e4SAndreas Gohr
12373038c47SAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
12473038c47SAndreas Gohr    if($mem){
12573038c47SAndreas Gohr        if($mem < 16777216){
12673038c47SAndreas Gohr            msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
12773038c47SAndreas Gohr        }elseif($mem < 20971520){
12873038c47SAndreas 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);
12973038c47SAndreas Gohr        }elseif($mem < 33554432){
13073038c47SAndreas 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);
13173038c47SAndreas Gohr        }else{
13273038c47SAndreas Gohr            msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
13373038c47SAndreas Gohr        }
13473038c47SAndreas Gohr    }
13573038c47SAndreas Gohr
136c29dc6e4SAndreas Gohr    if(is_writable($conf['changelog'])){
137c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
138c29dc6e4SAndreas Gohr    }else{
13979e79377SAndreas Gohr        if (file_exists($conf['changelog'])) {
140c29dc6e4SAndreas Gohr            msg('Changelog is not writable',-1);
141c29dc6e4SAndreas Gohr        }
142c29dc6e4SAndreas Gohr    }
143c29dc6e4SAndreas Gohr
14479e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1452cdbda06SAnika Henke        msg('Old changelog exists', 0);
146c29dc6e4SAndreas Gohr    }
147c29dc6e4SAndreas Gohr
14879e79377SAndreas Gohr    if (file_exists($conf['changelog'].'_failed')) {
1492cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
15079e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_importing')) {
151c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
15279e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_import_ok')) {
1532cdbda06SAnika Henke        msg('Old changelog imported', 1);
154c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
1552cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
156c29dc6e4SAndreas Gohr        }
157c29dc6e4SAndreas Gohr    }
158c29dc6e4SAndreas Gohr
1594c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
1604c7ecf15SGuy Brand        msg('conf directory is writable',1);
1614c7ecf15SGuy Brand    }else{
1624c7ecf15SGuy Brand        msg('conf directory is not writable',-1);
1634c7ecf15SGuy Brand    }
1644c7ecf15SGuy Brand
1650d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
166defb7d57SAnika Henke        global $config_cascade;
167defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
168c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
169c29dc6e4SAndreas Gohr        }else{
170c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
171c29dc6e4SAndreas Gohr        }
1720d487d8fSAndreas Gohr    }
173c29dc6e4SAndreas Gohr
174c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
175c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
176c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
177c29dc6e4SAndreas Gohr        }else{
178c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
1794222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
1804222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
1814222b898SAndreas Gohr            }
182c29dc6e4SAndreas Gohr        }
183c29dc6e4SAndreas Gohr    }else{
184c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
185c29dc6e4SAndreas Gohr    }
186c29dc6e4SAndreas Gohr
1873161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
188a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
189a731ed1dSAndreas Gohr    }
1903161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
191a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
192a731ed1dSAndreas Gohr    }
193a731ed1dSAndreas Gohr
194e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
195e5ab313fSAndreas Gohr    if(!$loc){
196e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this',-1);
197e5ab313fSAndreas Gohr    }elseif(stripos($loc,'utf') === false){
198e5ab313fSAndreas 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);
199e5ab313fSAndreas Gohr    }else{
200e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
201e5ab313fSAndreas Gohr    }
202e5ab313fSAndreas Gohr
203c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
204c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
205c29dc6e4SAndreas Gohr    }else{
206c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
207c29dc6e4SAndreas Gohr    }
208c29dc6e4SAndreas Gohr
2093d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
210585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
211c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2123d3c095dSMike Frysinger    }else{
2133d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2143d3c095dSMike Frysinger    }
2153d3c095dSMike Frysinger
216c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
217c29dc6e4SAndreas Gohr
218c29dc6e4SAndreas Gohr    if(is_writable($INFO['filepath'])){
219c29dc6e4SAndreas Gohr        msg('The current page is writable by the webserver',0);
220c29dc6e4SAndreas Gohr    }else{
221c29dc6e4SAndreas Gohr        msg('The current page is not writable by the webserver',0);
222c29dc6e4SAndreas Gohr    }
223c29dc6e4SAndreas Gohr
224c29dc6e4SAndreas Gohr    if($INFO['writable']){
225c29dc6e4SAndreas Gohr        msg('The current page is writable by you',0);
226c29dc6e4SAndreas Gohr    }else{
2272cdbda06SAnika Henke        msg('The current page is not writable by you',0);
228c29dc6e4SAndreas Gohr    }
2293b1dfc83SAndreas Gohr
23026f7dbf5SMichael Hamann    // Check for corrupted search index
23126f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
23226f7dbf5SMichael Hamann    $index_corrupted = false;
23326f7dbf5SMichael Hamann    foreach ($lengths as $length) {
23426f7dbf5SMichael Hamann        if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
23526f7dbf5SMichael Hamann            $index_corrupted = true;
23626f7dbf5SMichael Hamann            break;
23726f7dbf5SMichael Hamann        }
23826f7dbf5SMichael Hamann    }
23926f7dbf5SMichael Hamann
24026f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
24126f7dbf5SMichael Hamann        if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
24226f7dbf5SMichael Hamann            $index_corrupted = true;
24326f7dbf5SMichael Hamann            break;
24426f7dbf5SMichael Hamann        }
24526f7dbf5SMichael Hamann    }
24626f7dbf5SMichael Hamann
247d6c7b502SAndreas Gohr    if($index_corrupted) {
248d6c7b502SAndreas Gohr        msg(
249d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
2503d94d9edSMichael Hamann                probably needs to be rebuilt. See
25126f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
252d6c7b502SAndreas Gohr                for ways to rebuild the search index.', -1
253d6c7b502SAndreas Gohr        );
254d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
2553d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
256d6c7b502SAndreas Gohr    } else {
257d6c7b502SAndreas Gohr        msg(
258d6c7b502SAndreas Gohr            'The search index is empty. See
25926f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2603d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
261d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
262d6c7b502SAndreas Gohr        );
263d6c7b502SAndreas Gohr    }
264d6c7b502SAndreas Gohr
265d6c7b502SAndreas Gohr    // rough time check
266d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
267d6c7b502SAndreas Gohr    $http->max_redirect = 0;
268d6c7b502SAndreas Gohr    $http->timeout = 3;
269d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
270d6c7b502SAndreas Gohr    $now = time();
271d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
272d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
273d6c7b502SAndreas Gohr        $diff = $time - $now;
274d6c7b502SAndreas Gohr
275d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
276d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
277d6c7b502SAndreas Gohr        } else {
278d6c7b502SAndreas Gohr            msg("Your server's clock seems to be out of sync! Consider configuring a sync with a NTP server.  Diff: {$diff}s");
279d6c7b502SAndreas Gohr        }
280d6c7b502SAndreas Gohr    }
281d6c7b502SAndreas Gohr
282c29dc6e4SAndreas Gohr}
283c29dc6e4SAndreas Gohr
284c29dc6e4SAndreas Gohr/**
285c29dc6e4SAndreas Gohr * print a message
286c29dc6e4SAndreas Gohr *
287c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
288c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
289c29dc6e4SAndreas Gohr * using html_msgarea()
290c29dc6e4SAndreas Gohr *
291c29dc6e4SAndreas Gohr *
292c29dc6e4SAndreas Gohr * Levels can be:
293c29dc6e4SAndreas Gohr *
294c29dc6e4SAndreas Gohr * -1 error
295c29dc6e4SAndreas Gohr *  0 info
296c29dc6e4SAndreas Gohr *  1 success
297c29dc6e4SAndreas Gohr *
298c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
299c29dc6e4SAndreas Gohr * @see    html_msgarea
300c29dc6e4SAndreas Gohr */
301d3bae478SChristopher Smith
302d3bae478SChristopher Smithdefine('MSG_PUBLIC', 0);
303d3bae478SChristopher Smithdefine('MSG_USERS_ONLY', 1);
304d3bae478SChristopher Smithdefine('MSG_MANAGERS_ONLY',2);
305d3bae478SChristopher Smithdefine('MSG_ADMINS_ONLY',4);
306d3bae478SChristopher Smith
3076164d900SAndreas Gohr/**
3086164d900SAndreas Gohr * Display a message to the user
3096164d900SAndreas Gohr *
3106164d900SAndreas Gohr * @param string $message
3116164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3126164d900SAndreas Gohr * @param string $line  line number
3136164d900SAndreas Gohr * @param string $file  file number
3146164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3156164d900SAndreas Gohr */
316f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
317cc58224cSMichael Hamann    global $MSG, $MSG_shown;
31859bc3b48SGerrit Uitslag    $errors = array();
319c29dc6e4SAndreas Gohr    $errors[-1] = 'error';
320c29dc6e4SAndreas Gohr    $errors[0]  = 'info';
321c29dc6e4SAndreas Gohr    $errors[1]  = 'success';
322c29dc6e4SAndreas Gohr    $errors[2]  = 'notify';
323c29dc6e4SAndreas Gohr
3243009a773SAndreas Gohr    if($line || $file) $message.=' ['.utf8_basename($file).':'.$line.']';
325c29dc6e4SAndreas Gohr
326c29dc6e4SAndreas Gohr    if(!isset($MSG)) $MSG = array();
327f755f9abSChristopher Smith    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message, 'allow' => $allow);
328cc58224cSMichael Hamann    if(isset($MSG_shown) || headers_sent()){
329c29dc6e4SAndreas Gohr        if(function_exists('html_msgarea')){
330c29dc6e4SAndreas Gohr            html_msgarea();
331c29dc6e4SAndreas Gohr        }else{
332c29dc6e4SAndreas Gohr            print "ERROR($lvl) $message";
333c29dc6e4SAndreas Gohr        }
33469266de5SDominik Eckelmann        unset($GLOBALS['MSG']);
335c29dc6e4SAndreas Gohr    }
336c29dc6e4SAndreas Gohr}
337f755f9abSChristopher Smith/**
338f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
339f755f9abSChristopher Smith * in the $msg data structure
340f755f9abSChristopher Smith *
341f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
342f755f9abSChristopher Smith *                         msg   => string, the message
343f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
344f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
345f755f9abSChristopher Smith *                                       see MSG_* constants
3466164d900SAndreas Gohr * @return bool
347f755f9abSChristopher Smith */
348f755f9abSChristopher Smithfunction info_msg_allowed($msg){
349d3bae478SChristopher Smith    global $INFO, $auth;
350d3bae478SChristopher Smith
351d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
352f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
353d3bae478SChristopher Smith
354d3bae478SChristopher Smith    // restricted msg, but no authentication
355d3bae478SChristopher Smith    if (empty($auth)) return false;
356d3bae478SChristopher Smith
357f755f9abSChristopher Smith    switch ($msg['allow']){
358d3bae478SChristopher Smith        case MSG_USERS_ONLY:
359d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
360d3bae478SChristopher Smith
361d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
362d3bae478SChristopher Smith            return $INFO['ismanager'];
363d3bae478SChristopher Smith
364d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
365d3bae478SChristopher Smith            return $INFO['isadmin'];
366d3bae478SChristopher Smith
367d3bae478SChristopher Smith        default:
368f755f9abSChristopher Smith            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"', E_USER_WARNING);
369d3bae478SChristopher Smith            return $INFO['isadmin'];
370d3bae478SChristopher Smith    }
371d3bae478SChristopher Smith
372d3bae478SChristopher Smith    return false;
373d3bae478SChristopher Smith}
374d3bae478SChristopher Smith
375c29dc6e4SAndreas Gohr/**
376c29dc6e4SAndreas Gohr * print debug messages
377c29dc6e4SAndreas Gohr *
378c29dc6e4SAndreas Gohr * little function to print the content of a var
379c29dc6e4SAndreas Gohr *
380c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
381f50a239bSTakamura *
382f50a239bSTakamura * @param string $msg
383f50a239bSTakamura * @param bool $hidden
384c29dc6e4SAndreas Gohr */
385c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
38613493794SAndreas Gohr    if($hidden){
38713493794SAndreas Gohr        echo "<!--\n";
388c29dc6e4SAndreas Gohr        print_r($msg);
38913493794SAndreas Gohr        echo "\n-->";
39013493794SAndreas Gohr    }else{
39113493794SAndreas Gohr        echo '<pre class="dbg">';
39213493794SAndreas Gohr        echo hsc(print_r($msg,true));
39313493794SAndreas Gohr        echo '</pre>';
39413493794SAndreas Gohr    }
395c29dc6e4SAndreas Gohr}
396c29dc6e4SAndreas Gohr
397c29dc6e4SAndreas Gohr/**
398c29dc6e4SAndreas Gohr * Print info to a log file
399c29dc6e4SAndreas Gohr *
400c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
401f50a239bSTakamura *
402f50a239bSTakamura * @param string $msg
403f50a239bSTakamura * @param string $header
404c29dc6e4SAndreas Gohr */
4050699d739SAndreas Gohrfunction dbglog($msg,$header=''){
406c29dc6e4SAndreas Gohr    global $conf;
407585bf44eSChristopher Smith    /* @var Input $INPUT */
408585bf44eSChristopher Smith    global $INPUT;
409585bf44eSChristopher Smith
4105bd930ffSMichael Hamann    // The debug log isn't automatically cleaned thus only write it when
4115bd930ffSMichael Hamann    // debugging has been enabled by the user.
4125bd930ffSMichael Hamann    if($conf['allowdebug'] !== 1) return;
413c7408a63SAndreas Gohr    if(is_object($msg) || is_array($msg)){
414c7408a63SAndreas Gohr        $msg = print_r($msg,true);
415c7408a63SAndreas Gohr    }
416c7408a63SAndreas Gohr
4170699d739SAndreas Gohr    if($header) $msg = "$header\n$msg";
4180699d739SAndreas Gohr
419c29dc6e4SAndreas Gohr    $file = $conf['cachedir'].'/debug.log';
420c29dc6e4SAndreas Gohr    $fh = fopen($file,'a');
421c29dc6e4SAndreas Gohr    if($fh){
422585bf44eSChristopher Smith        fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n");
423c29dc6e4SAndreas Gohr        fclose($fh);
424c29dc6e4SAndreas Gohr    }
425c29dc6e4SAndreas Gohr}
426c29dc6e4SAndreas Gohr
427db09e31eSAndreas Gohr/**
4281419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4291419a485SAndreas Gohr *
4301419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
43185331086SAndreas Gohr * @triggers INFO_DEPRECATION_LOG
4321419a485SAndreas Gohr */
4331419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') {
43485331086SAndreas Gohr    global $conf;
43585331086SAndreas Gohr    global $EVENT_HANDLER;
43685331086SAndreas Gohr    if(!$conf['allowdebug'] && !$EVENT_HANDLER->hasHandlerForEvent('INFO_DEPRECATION_LOG')) {
43785331086SAndreas Gohr        // avoid any work if no one cares
43885331086SAndreas Gohr        return;
43985331086SAndreas Gohr    }
44085331086SAndreas Gohr
4411419a485SAndreas Gohr    $backtrace = debug_backtrace();
4421419a485SAndreas Gohr    array_shift($backtrace);
44344455016SAndreas Gohr    $self = $backtrace[0];
44444455016SAndreas Gohr    $call = $backtrace[1];
4451419a485SAndreas Gohr
44644455016SAndreas Gohr    $data = [
44744455016SAndreas Gohr        'trace' => $backtrace,
44844455016SAndreas Gohr        'alternative' => $alternative,
44944455016SAndreas Gohr        'called' => trim($self['class'] . '::' . $self['function'] . '()', ':'),
45044455016SAndreas Gohr        'caller' => trim($call['class'] . '::' . $call['function'] . '()', ':'),
45144455016SAndreas Gohr        'file' => $call['file'],
45244455016SAndreas Gohr        'line' => $call['line'],
45344455016SAndreas Gohr    ];
4541419a485SAndreas Gohr
45544455016SAndreas Gohr    $event = new Doku_Event('INFO_DEPRECATION_LOG', $data);
45644455016SAndreas Gohr    if($event->advise_before()) {
45744455016SAndreas Gohr        $msg = $event->data['called'] . ' is deprecated. It was called from ';
45844455016SAndreas Gohr        $msg .= $event->data['caller'] . ' in ' . $event->data['file'] . ':' . $event->data['line'];
45944455016SAndreas Gohr        if($event->data['alternative']) {
46044455016SAndreas Gohr            $msg .= ' ' . $event->data['alternative'] . ' should be used instead!';
4611419a485SAndreas Gohr        }
4621419a485SAndreas Gohr        dbglog($msg);
4631419a485SAndreas Gohr    }
46444455016SAndreas Gohr    $event->advise_after();
46544455016SAndreas Gohr}
4661419a485SAndreas Gohr
4671419a485SAndreas Gohr/**
468db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
469db09e31eSAndreas Gohr *
470db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
471db09e31eSAndreas Gohr */
472db09e31eSAndreas Gohrfunction dbg_backtrace(){
473db09e31eSAndreas Gohr    // Get backtrace
474db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
475db09e31eSAndreas Gohr
476db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
477db09e31eSAndreas Gohr    array_shift($backtrace);
478db09e31eSAndreas Gohr
479db09e31eSAndreas Gohr    // Iterate backtrace
480db09e31eSAndreas Gohr    $calls = array();
481db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
482db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
483db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
484db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
485db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
486db09e31eSAndreas Gohr
4878259f1aaSAndreas Gohr        $params = array();
488db09e31eSAndreas Gohr        if (isset($call['args'])){
4898259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
4908259f1aaSAndreas Gohr                if(is_object($arg)){
4918259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
4928259f1aaSAndreas Gohr                }elseif(is_array($arg)){
4938259f1aaSAndreas Gohr                    $params[] = '[Array]';
4948259f1aaSAndreas Gohr                }elseif(is_null($arg)){
49559bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
4968259f1aaSAndreas Gohr                }else{
4978259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
498db09e31eSAndreas Gohr                }
4998259f1aaSAndreas Gohr            }
5008259f1aaSAndreas Gohr        }
5018259f1aaSAndreas Gohr        $params = implode(', ',$params);
502db09e31eSAndreas Gohr
5038259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
504db09e31eSAndreas Gohr                $function,
505db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
506db09e31eSAndreas Gohr                $location);
507db09e31eSAndreas Gohr    }
508db09e31eSAndreas Gohr    ksort($calls);
509db09e31eSAndreas Gohr
510db09e31eSAndreas Gohr    return implode("\n", $calls);
511db09e31eSAndreas Gohr}
512db09e31eSAndreas Gohr
51324297a69SAndreas Gohr/**
51424297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
51524297a69SAndreas Gohr *
51624297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
51724297a69SAndreas Gohr * debug output
51824297a69SAndreas Gohr *
51924297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
520f50a239bSTakamura *
521f50a239bSTakamura * @param array $data
52224297a69SAndreas Gohr */
52324297a69SAndreas Gohrfunction debug_guard(&$data){
52424297a69SAndreas Gohr    foreach($data as $key => $value){
52524297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
52624297a69SAndreas Gohr            $data[$key] = '***';
52724297a69SAndreas Gohr            continue;
52824297a69SAndreas Gohr        }
52924297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
53024297a69SAndreas Gohr    }
53124297a69SAndreas Gohr}
532