xref: /dokuwiki/inc/infoutils.php (revision f50a239b3b819527445d240746b09a05fb76d103)
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.');
93d7760aaSAndreas Gohrif(!defined('DOKU_MESSAGEURL')) define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
10c29dc6e4SAndreas Gohr
11c29dc6e4SAndreas Gohr/**
12c29dc6e4SAndreas Gohr * Check for new messages from upstream
13c29dc6e4SAndreas Gohr *
14c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
15c29dc6e4SAndreas Gohr */
16c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){
17c29dc6e4SAndreas Gohr    global $conf;
18c29dc6e4SAndreas Gohr    global $INFO;
19ef362bb8SAnika Henke    global $updateVersion;
20c29dc6e4SAndreas Gohr    if(!$conf['updatecheck']) return;
21f8cc712eSAndreas Gohr    if($conf['useacl'] && !$INFO['ismanager']) return;
22c29dc6e4SAndreas Gohr
2337b21a1bSAndreas Gohr    $cf = getCacheName($updateVersion, '.updmsg');
24c29dc6e4SAndreas Gohr    $lm = @filemtime($cf);
25c29dc6e4SAndreas Gohr
26c29dc6e4SAndreas Gohr    // check if new messages needs to be fetched
278d3d7569SAnika Henke    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
2863d9b820SAndreas Gohr        @touch($cf);
2937b21a1bSAndreas Gohr        dbglog("checkUpdateMessages(): downloading messages to ".$cf);
30c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
3163d9b820SAndreas Gohr        $http->timeout = 12;
3286c04d87SAngus Gratton        $resp = $http->get(DOKU_MESSAGEURL.$updateVersion);
3386c04d87SAngus Gratton        if(is_string($resp) && ($resp == "" || substr(trim($resp), -1) == '%')) {
3486c04d87SAngus Gratton            // basic sanity check that this is either an empty string response (ie "no messages")
3586c04d87SAngus Gratton            // or it looks like one of our messages, not WiFi login or other interposed response
3686c04d87SAngus Gratton            io_saveFile($cf,$resp);
378f1efc43SAndreas Gohr        } else {
3886c04d87SAngus Gratton            dbglog("checkUpdateMessages(): unexpected HTTP response received");
398f1efc43SAndreas Gohr        }
40c29dc6e4SAndreas Gohr    }else{
4137b21a1bSAndreas Gohr        dbglog("checkUpdateMessages(): messages up to date");
42c29dc6e4SAndreas Gohr    }
43c29dc6e4SAndreas Gohr
4486c04d87SAngus Gratton    $data = io_readFile($cf);
45c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
46c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n",$data);
47c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
48c29dc6e4SAndreas Gohr        if($msg) msg($msg,2);
49c29dc6e4SAndreas Gohr    }
50c29dc6e4SAndreas Gohr}
51c29dc6e4SAndreas Gohr
52c29dc6e4SAndreas Gohr
53c29dc6e4SAndreas Gohr/**
5425c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
55c29dc6e4SAndreas Gohr *
56c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
57c29dc6e4SAndreas Gohr */
5825c07f93SAnika Henkefunction getVersionData(){
5925c07f93SAnika Henke    $version = array();
60c29dc6e4SAndreas Gohr    //import version string
6179e79377SAndreas Gohr    if(file_exists(DOKU_INC.'VERSION')){
62c29dc6e4SAndreas Gohr        //official release
63d6c7b502SAndreas Gohr        $version['date'] = trim(io_readFile(DOKU_INC.'VERSION'));
6425c07f93SAnika Henke        $version['type'] = 'Release';
655cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
665cf31920SAndreas Gohr        $version['type'] = 'Git';
6725c07f93SAnika Henke        $version['date'] = 'unknown';
68e570ed43SAndreas Gohr
695cf31920SAndreas Gohr        $inventory = DOKU_INC.'.git/logs/HEAD';
705cf31920SAndreas Gohr        if(is_file($inventory)){
71e570ed43SAndreas Gohr            $sz   = filesize($inventory);
725cf31920SAndreas Gohr            $seek = max(0,$sz-2000); // read from back of the file
73e570ed43SAndreas Gohr            $fh   = fopen($inventory,'rb');
74c29dc6e4SAndreas Gohr            fseek($fh,$seek);
75c29dc6e4SAndreas Gohr            $chunk = fread($fh,2000);
76c29dc6e4SAndreas Gohr            fclose($fh);
775cf31920SAndreas Gohr            $chunk = trim($chunk);
78b838050eSPiyush Mishra            $chunk = @array_pop(explode("\n",$chunk));   //last log line
79b838050eSPiyush Mishra            $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg
805cf31920SAndreas Gohr            $chunk = explode(" ",$chunk);
815cf31920SAndreas Gohr            array_pop($chunk); //strip timezone
825cf31920SAndreas Gohr            $date = date('Y-m-d',array_pop($chunk));
835cf31920SAndreas Gohr            if($date) $version['date'] = $date;
845cf31920SAndreas Gohr        }
85c29dc6e4SAndreas Gohr    }else{
866a34de2dSAnika Henke        global $updateVersion;
876a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
8825c07f93SAnika Henke        $version['type'] = 'snapshot?';
89c29dc6e4SAndreas Gohr    }
905cf31920SAndreas Gohr    return $version;
91c29dc6e4SAndreas Gohr}
92c29dc6e4SAndreas Gohr
93c29dc6e4SAndreas Gohr/**
9425c07f93SAnika Henke * Return DokuWiki's version (as a string)
9525c07f93SAnika Henke *
9625c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
9725c07f93SAnika Henke */
9825c07f93SAnika Henkefunction getVersion(){
9925c07f93SAnika Henke    $version = getVersionData();
10025c07f93SAnika Henke    return $version['type'].' '.$version['date'];
10125c07f93SAnika Henke}
10225c07f93SAnika Henke
10325c07f93SAnika Henke/**
104c29dc6e4SAndreas Gohr * Run a few sanity checks
105c29dc6e4SAndreas Gohr *
106c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
107c29dc6e4SAndreas Gohr */
108c29dc6e4SAndreas Gohrfunction check(){
109c29dc6e4SAndreas Gohr    global $conf;
110c29dc6e4SAndreas Gohr    global $INFO;
111585bf44eSChristopher Smith    /* @var Input $INPUT */
112585bf44eSChristopher Smith    global $INPUT;
113c29dc6e4SAndreas Gohr
1143f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
115c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
116c29dc6e4SAndreas Gohr
1173476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
1183476bb81SAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 5.6.0+ needed)',-1);
119c29dc6e4SAndreas Gohr        }else{
120c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
121c29dc6e4SAndreas Gohr        }
12208d1a8dfSAndreas Gohr    } else {
1233476bb81SAndreas Gohr        if(version_compare(phpversion(),'5.6.0','<')){
12408d1a8dfSAndreas Gohr            msg('Your PHP version is too old',-1);
12508d1a8dfSAndreas Gohr        }
12608d1a8dfSAndreas Gohr    }
127c29dc6e4SAndreas Gohr
12873038c47SAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
12973038c47SAndreas Gohr    if($mem){
13073038c47SAndreas Gohr        if($mem < 16777216){
13173038c47SAndreas Gohr            msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
13273038c47SAndreas Gohr        }elseif($mem < 20971520){
13373038c47SAndreas 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);
13473038c47SAndreas Gohr        }elseif($mem < 33554432){
13573038c47SAndreas 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);
13673038c47SAndreas Gohr        }else{
13773038c47SAndreas Gohr            msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
13873038c47SAndreas Gohr        }
13973038c47SAndreas Gohr    }
14073038c47SAndreas Gohr
141c29dc6e4SAndreas Gohr    if(is_writable($conf['changelog'])){
142c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
143c29dc6e4SAndreas Gohr    }else{
14479e79377SAndreas Gohr        if (file_exists($conf['changelog'])) {
145c29dc6e4SAndreas Gohr            msg('Changelog is not writable',-1);
146c29dc6e4SAndreas Gohr        }
147c29dc6e4SAndreas Gohr    }
148c29dc6e4SAndreas Gohr
14979e79377SAndreas Gohr    if (isset($conf['changelog_old']) && file_exists($conf['changelog_old'])) {
1502cdbda06SAnika Henke        msg('Old changelog exists', 0);
151c29dc6e4SAndreas Gohr    }
152c29dc6e4SAndreas Gohr
15379e79377SAndreas Gohr    if (file_exists($conf['changelog'].'_failed')) {
1542cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
15579e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_importing')) {
156c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
15779e79377SAndreas Gohr    } else if (file_exists($conf['changelog'].'_import_ok')) {
1582cdbda06SAnika Henke        msg('Old changelog imported', 1);
159c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
1602cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
161c29dc6e4SAndreas Gohr        }
162c29dc6e4SAndreas Gohr    }
163c29dc6e4SAndreas Gohr
1644c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
1654c7ecf15SGuy Brand        msg('conf directory is writable',1);
1664c7ecf15SGuy Brand    }else{
1674c7ecf15SGuy Brand        msg('conf directory is not writable',-1);
1684c7ecf15SGuy Brand    }
1694c7ecf15SGuy Brand
1700d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
171defb7d57SAnika Henke        global $config_cascade;
172defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
173c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
174c29dc6e4SAndreas Gohr        }else{
175c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
176c29dc6e4SAndreas Gohr        }
1770d487d8fSAndreas Gohr    }
178c29dc6e4SAndreas Gohr
179c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
180c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
181c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
182c29dc6e4SAndreas Gohr        }else{
183c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
1844222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
1854222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
1864222b898SAndreas Gohr            }
187c29dc6e4SAndreas Gohr        }
188c29dc6e4SAndreas Gohr    }else{
189c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
190c29dc6e4SAndreas Gohr    }
191c29dc6e4SAndreas Gohr
1923161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
193a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
194a731ed1dSAndreas Gohr    }
1953161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
196a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
197a731ed1dSAndreas Gohr    }
198a731ed1dSAndreas Gohr
199e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
200e5ab313fSAndreas Gohr    if(!$loc){
201e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this',-1);
202e5ab313fSAndreas Gohr    }elseif(stripos($loc,'utf') === false){
203e5ab313fSAndreas 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);
204e5ab313fSAndreas Gohr    }else{
205e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
206e5ab313fSAndreas Gohr    }
207e5ab313fSAndreas Gohr
208c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
209c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
210c29dc6e4SAndreas Gohr    }else{
211c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
212c29dc6e4SAndreas Gohr    }
213c29dc6e4SAndreas Gohr
2143d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
215585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
216c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2173d3c095dSMike Frysinger    }else{
2183d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2193d3c095dSMike Frysinger    }
2203d3c095dSMike Frysinger
221c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
222c29dc6e4SAndreas Gohr
223c29dc6e4SAndreas Gohr    if(is_writable($INFO['filepath'])){
224c29dc6e4SAndreas Gohr        msg('The current page is writable by the webserver',0);
225c29dc6e4SAndreas Gohr    }else{
226c29dc6e4SAndreas Gohr        msg('The current page is not writable by the webserver',0);
227c29dc6e4SAndreas Gohr    }
228c29dc6e4SAndreas Gohr
229c29dc6e4SAndreas Gohr    if($INFO['writable']){
230c29dc6e4SAndreas Gohr        msg('The current page is writable by you',0);
231c29dc6e4SAndreas Gohr    }else{
2322cdbda06SAnika Henke        msg('The current page is not writable by you',0);
233c29dc6e4SAndreas Gohr    }
2343b1dfc83SAndreas Gohr
23526f7dbf5SMichael Hamann    // Check for corrupted search index
23626f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
23726f7dbf5SMichael Hamann    $index_corrupted = false;
23826f7dbf5SMichael Hamann    foreach ($lengths as $length) {
23926f7dbf5SMichael Hamann        if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
24026f7dbf5SMichael Hamann            $index_corrupted = true;
24126f7dbf5SMichael Hamann            break;
24226f7dbf5SMichael Hamann        }
24326f7dbf5SMichael Hamann    }
24426f7dbf5SMichael Hamann
24526f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
24626f7dbf5SMichael Hamann        if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
24726f7dbf5SMichael Hamann            $index_corrupted = true;
24826f7dbf5SMichael Hamann            break;
24926f7dbf5SMichael Hamann        }
25026f7dbf5SMichael Hamann    }
25126f7dbf5SMichael Hamann
252d6c7b502SAndreas Gohr    if($index_corrupted) {
253d6c7b502SAndreas Gohr        msg(
254d6c7b502SAndreas Gohr            'The search index is corrupted. It might produce wrong results and most
2553d94d9edSMichael Hamann                probably needs to be rebuilt. See
25626f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
257d6c7b502SAndreas Gohr                for ways to rebuild the search index.', -1
258d6c7b502SAndreas Gohr        );
259d6c7b502SAndreas Gohr    } elseif(!empty($lengths)) {
2603d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
261d6c7b502SAndreas Gohr    } else {
262d6c7b502SAndreas Gohr        msg(
263d6c7b502SAndreas Gohr            'The search index is empty. See
26426f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2653d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
266d6c7b502SAndreas Gohr                isn\'t used or the wiki is actually empty this is normal.'
267d6c7b502SAndreas Gohr        );
268d6c7b502SAndreas Gohr    }
269d6c7b502SAndreas Gohr
270d6c7b502SAndreas Gohr    // rough time check
271d6c7b502SAndreas Gohr    $http = new DokuHTTPClient();
272d6c7b502SAndreas Gohr    $http->max_redirect = 0;
273d6c7b502SAndreas Gohr    $http->timeout = 3;
274d6c7b502SAndreas Gohr    $http->sendRequest('http://www.dokuwiki.org', '', 'HEAD');
275d6c7b502SAndreas Gohr    $now = time();
276d6c7b502SAndreas Gohr    if(isset($http->resp_headers['date'])) {
277d6c7b502SAndreas Gohr        $time = strtotime($http->resp_headers['date']);
278d6c7b502SAndreas Gohr        $diff = $time - $now;
279d6c7b502SAndreas Gohr
280d6c7b502SAndreas Gohr        if(abs($diff) < 4) {
281d6c7b502SAndreas Gohr            msg("Server time seems to be okay. Diff: {$diff}s", 1);
282d6c7b502SAndreas Gohr        } else {
283d6c7b502SAndreas Gohr            msg("Your server's clock seems to be out of sync! Consider configuring a sync with a NTP server.  Diff: {$diff}s");
284d6c7b502SAndreas Gohr        }
285d6c7b502SAndreas Gohr    }
286d6c7b502SAndreas Gohr
287c29dc6e4SAndreas Gohr}
288c29dc6e4SAndreas Gohr
289c29dc6e4SAndreas Gohr/**
290c29dc6e4SAndreas Gohr * print a message
291c29dc6e4SAndreas Gohr *
292c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
293c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
294c29dc6e4SAndreas Gohr * using html_msgarea()
295c29dc6e4SAndreas Gohr *
296c29dc6e4SAndreas Gohr *
297c29dc6e4SAndreas Gohr * Levels can be:
298c29dc6e4SAndreas Gohr *
299c29dc6e4SAndreas Gohr * -1 error
300c29dc6e4SAndreas Gohr *  0 info
301c29dc6e4SAndreas Gohr *  1 success
302c29dc6e4SAndreas Gohr *
303c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
304c29dc6e4SAndreas Gohr * @see    html_msgarea
305c29dc6e4SAndreas Gohr */
306d3bae478SChristopher Smith
307d3bae478SChristopher Smithdefine('MSG_PUBLIC', 0);
308d3bae478SChristopher Smithdefine('MSG_USERS_ONLY', 1);
309d3bae478SChristopher Smithdefine('MSG_MANAGERS_ONLY',2);
310d3bae478SChristopher Smithdefine('MSG_ADMINS_ONLY',4);
311d3bae478SChristopher Smith
3126164d900SAndreas Gohr/**
3136164d900SAndreas Gohr * Display a message to the user
3146164d900SAndreas Gohr *
3156164d900SAndreas Gohr * @param string $message
3166164d900SAndreas Gohr * @param int    $lvl   -1 = error, 0 = info, 1 = success, 2 = notify
3176164d900SAndreas Gohr * @param string $line  line number
3186164d900SAndreas Gohr * @param string $file  file number
3196164d900SAndreas Gohr * @param int    $allow who's allowed to see the message, see MSG_* constants
3206164d900SAndreas Gohr */
321f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
322cc58224cSMichael Hamann    global $MSG, $MSG_shown;
32359bc3b48SGerrit Uitslag    $errors = array();
324c29dc6e4SAndreas Gohr    $errors[-1] = 'error';
325c29dc6e4SAndreas Gohr    $errors[0]  = 'info';
326c29dc6e4SAndreas Gohr    $errors[1]  = 'success';
327c29dc6e4SAndreas Gohr    $errors[2]  = 'notify';
328c29dc6e4SAndreas Gohr
3293009a773SAndreas Gohr    if($line || $file) $message.=' ['.utf8_basename($file).':'.$line.']';
330c29dc6e4SAndreas Gohr
331c29dc6e4SAndreas Gohr    if(!isset($MSG)) $MSG = array();
332f755f9abSChristopher Smith    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message, 'allow' => $allow);
333cc58224cSMichael Hamann    if(isset($MSG_shown) || headers_sent()){
334c29dc6e4SAndreas Gohr        if(function_exists('html_msgarea')){
335c29dc6e4SAndreas Gohr            html_msgarea();
336c29dc6e4SAndreas Gohr        }else{
337c29dc6e4SAndreas Gohr            print "ERROR($lvl) $message";
338c29dc6e4SAndreas Gohr        }
33969266de5SDominik Eckelmann        unset($GLOBALS['MSG']);
340c29dc6e4SAndreas Gohr    }
341c29dc6e4SAndreas Gohr}
342f755f9abSChristopher Smith/**
343f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
344f755f9abSChristopher Smith * in the $msg data structure
345f755f9abSChristopher Smith *
346f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
347f755f9abSChristopher Smith *                         msg   => string, the message
348f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
349f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
350f755f9abSChristopher Smith *                                       see MSG_* constants
3516164d900SAndreas Gohr * @return bool
352f755f9abSChristopher Smith */
353f755f9abSChristopher Smithfunction info_msg_allowed($msg){
354d3bae478SChristopher Smith    global $INFO, $auth;
355d3bae478SChristopher Smith
356d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
357f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
358d3bae478SChristopher Smith
359d3bae478SChristopher Smith    // restricted msg, but no authentication
360d3bae478SChristopher Smith    if (empty($auth)) return false;
361d3bae478SChristopher Smith
362f755f9abSChristopher Smith    switch ($msg['allow']){
363d3bae478SChristopher Smith        case MSG_USERS_ONLY:
364d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
365d3bae478SChristopher Smith
366d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
367d3bae478SChristopher Smith            return $INFO['ismanager'];
368d3bae478SChristopher Smith
369d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
370d3bae478SChristopher Smith            return $INFO['isadmin'];
371d3bae478SChristopher Smith
372d3bae478SChristopher Smith        default:
373f755f9abSChristopher Smith            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"', E_USER_WARNING);
374d3bae478SChristopher Smith            return $INFO['isadmin'];
375d3bae478SChristopher Smith    }
376d3bae478SChristopher Smith
377d3bae478SChristopher Smith    return false;
378d3bae478SChristopher Smith}
379d3bae478SChristopher Smith
380c29dc6e4SAndreas Gohr/**
381c29dc6e4SAndreas Gohr * print debug messages
382c29dc6e4SAndreas Gohr *
383c29dc6e4SAndreas Gohr * little function to print the content of a var
384c29dc6e4SAndreas Gohr *
385c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
386*f50a239bSTakamura *
387*f50a239bSTakamura * @param string $msg
388*f50a239bSTakamura * @param bool $hidden
389c29dc6e4SAndreas Gohr */
390c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
39113493794SAndreas Gohr    if($hidden){
39213493794SAndreas Gohr        echo "<!--\n";
393c29dc6e4SAndreas Gohr        print_r($msg);
39413493794SAndreas Gohr        echo "\n-->";
39513493794SAndreas Gohr    }else{
39613493794SAndreas Gohr        echo '<pre class="dbg">';
39713493794SAndreas Gohr        echo hsc(print_r($msg,true));
39813493794SAndreas Gohr        echo '</pre>';
39913493794SAndreas Gohr    }
400c29dc6e4SAndreas Gohr}
401c29dc6e4SAndreas Gohr
402c29dc6e4SAndreas Gohr/**
403c29dc6e4SAndreas Gohr * Print info to a log file
404c29dc6e4SAndreas Gohr *
405c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
406*f50a239bSTakamura *
407*f50a239bSTakamura * @param string $msg
408*f50a239bSTakamura * @param string $header
409c29dc6e4SAndreas Gohr */
4100699d739SAndreas Gohrfunction dbglog($msg,$header=''){
411c29dc6e4SAndreas Gohr    global $conf;
412585bf44eSChristopher Smith    /* @var Input $INPUT */
413585bf44eSChristopher Smith    global $INPUT;
414585bf44eSChristopher Smith
4155bd930ffSMichael Hamann    // The debug log isn't automatically cleaned thus only write it when
4165bd930ffSMichael Hamann    // debugging has been enabled by the user.
4175bd930ffSMichael Hamann    if($conf['allowdebug'] !== 1) return;
418c7408a63SAndreas Gohr    if(is_object($msg) || is_array($msg)){
419c7408a63SAndreas Gohr        $msg = print_r($msg,true);
420c7408a63SAndreas Gohr    }
421c7408a63SAndreas Gohr
4220699d739SAndreas Gohr    if($header) $msg = "$header\n$msg";
4230699d739SAndreas Gohr
424c29dc6e4SAndreas Gohr    $file = $conf['cachedir'].'/debug.log';
425c29dc6e4SAndreas Gohr    $fh = fopen($file,'a');
426c29dc6e4SAndreas Gohr    if($fh){
427585bf44eSChristopher Smith        fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n");
428c29dc6e4SAndreas Gohr        fclose($fh);
429c29dc6e4SAndreas Gohr    }
430c29dc6e4SAndreas Gohr}
431c29dc6e4SAndreas Gohr
432db09e31eSAndreas Gohr/**
4331419a485SAndreas Gohr * Log accesses to deprecated fucntions to the debug log
4341419a485SAndreas Gohr *
4351419a485SAndreas Gohr * @param string $alternative The function or method that should be used instead
4361419a485SAndreas Gohr */
4371419a485SAndreas Gohrfunction dbg_deprecated($alternative = '') {
4381419a485SAndreas Gohr    global $conf;
4391419a485SAndreas Gohr    if(!$conf['allowdebug']) return;
4401419a485SAndreas Gohr
4411419a485SAndreas Gohr    $backtrace = debug_backtrace();
4421419a485SAndreas Gohr    array_shift($backtrace);
4431419a485SAndreas Gohr    $self = array_shift($backtrace);
4441419a485SAndreas Gohr    $call = array_shift($backtrace);
4451419a485SAndreas Gohr
4461419a485SAndreas Gohr    $called = trim($self['class'].'::'.$self['function'].'()', ':');
4471419a485SAndreas Gohr    $caller = trim($call['class'].'::'.$call['function'].'()', ':');
4481419a485SAndreas Gohr
4491419a485SAndreas Gohr    $msg = $called.' is deprecated. It was called from ';
4501419a485SAndreas Gohr    $msg .= $caller.' in '.$call['file'].':'.$call['line'];
4511419a485SAndreas Gohr    if($alternative) {
4521419a485SAndreas Gohr        $msg .= ' '.$alternative.' should be used instead!';
4531419a485SAndreas Gohr    }
4541419a485SAndreas Gohr
4551419a485SAndreas Gohr    dbglog($msg);
4561419a485SAndreas Gohr}
4571419a485SAndreas Gohr
4581419a485SAndreas Gohr/**
459db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
460db09e31eSAndreas Gohr *
461db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
462db09e31eSAndreas Gohr */
463db09e31eSAndreas Gohrfunction dbg_backtrace(){
464db09e31eSAndreas Gohr    // Get backtrace
465db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
466db09e31eSAndreas Gohr
467db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
468db09e31eSAndreas Gohr    array_shift($backtrace);
469db09e31eSAndreas Gohr
470db09e31eSAndreas Gohr    // Iterate backtrace
471db09e31eSAndreas Gohr    $calls = array();
472db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
473db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
474db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
475db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
476db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
477db09e31eSAndreas Gohr
4788259f1aaSAndreas Gohr        $params = array();
479db09e31eSAndreas Gohr        if (isset($call['args'])){
4808259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
4818259f1aaSAndreas Gohr                if(is_object($arg)){
4828259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
4838259f1aaSAndreas Gohr                }elseif(is_array($arg)){
4848259f1aaSAndreas Gohr                    $params[] = '[Array]';
4858259f1aaSAndreas Gohr                }elseif(is_null($arg)){
48659bc3b48SGerrit Uitslag                    $params[] = '[NULL]';
4878259f1aaSAndreas Gohr                }else{
4888259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
489db09e31eSAndreas Gohr                }
4908259f1aaSAndreas Gohr            }
4918259f1aaSAndreas Gohr        }
4928259f1aaSAndreas Gohr        $params = implode(', ',$params);
493db09e31eSAndreas Gohr
4948259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
495db09e31eSAndreas Gohr                $function,
496db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
497db09e31eSAndreas Gohr                $location);
498db09e31eSAndreas Gohr    }
499db09e31eSAndreas Gohr    ksort($calls);
500db09e31eSAndreas Gohr
501db09e31eSAndreas Gohr    return implode("\n", $calls);
502db09e31eSAndreas Gohr}
503db09e31eSAndreas Gohr
50424297a69SAndreas Gohr/**
50524297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
50624297a69SAndreas Gohr *
50724297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
50824297a69SAndreas Gohr * debug output
50924297a69SAndreas Gohr *
51024297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
511*f50a239bSTakamura *
512*f50a239bSTakamura * @param array $data
51324297a69SAndreas Gohr */
51424297a69SAndreas Gohrfunction debug_guard(&$data){
51524297a69SAndreas Gohr    foreach($data as $key => $value){
51624297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
51724297a69SAndreas Gohr            $data[$key] = '***';
51824297a69SAndreas Gohr            continue;
51924297a69SAndreas Gohr        }
52024297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
52124297a69SAndreas Gohr    }
52224297a69SAndreas Gohr}
523