xref: /dokuwiki/inc/infoutils.php (revision 585bf44e2b756eac2e1cfce7035ef237bc02a788)
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
23c29dc6e4SAndreas Gohr    $cf = $conf['cachedir'].'/messages.txt';
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);
29d2d7bf0bSChristoph Schindler        dbglog("checkUpdateMessages(): downloading messages.txt");
30c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
3163d9b820SAndreas Gohr        $http->timeout = 12;
32ef362bb8SAnika Henke        $data = $http->get(DOKU_MESSAGEURL.$updateVersion);
33c29dc6e4SAndreas Gohr        io_saveFile($cf,$data);
34c29dc6e4SAndreas Gohr    }else{
35d2d7bf0bSChristoph Schindler        dbglog("checkUpdateMessages(): messages.txt up to date");
36c29dc6e4SAndreas Gohr        $data = io_readFile($cf);
37c29dc6e4SAndreas Gohr    }
38c29dc6e4SAndreas Gohr
39c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
40c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n",$data);
41c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
42c29dc6e4SAndreas Gohr        if($msg) msg($msg,2);
43c29dc6e4SAndreas Gohr    }
44c29dc6e4SAndreas Gohr}
45c29dc6e4SAndreas Gohr
46c29dc6e4SAndreas Gohr
47c29dc6e4SAndreas Gohr/**
4825c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
49c29dc6e4SAndreas Gohr *
50c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
51c29dc6e4SAndreas Gohr */
5225c07f93SAnika Henkefunction getVersionData(){
5325c07f93SAnika Henke    $version = array();
54c29dc6e4SAndreas Gohr    //import version string
55e9208eb1SAndreas Gohr    if(@file_exists(DOKU_INC.'VERSION')){
56c29dc6e4SAndreas Gohr        //official release
5725c07f93SAnika Henke        $version['date'] = trim(io_readfile(DOKU_INC.'VERSION'));
5825c07f93SAnika Henke        $version['type'] = 'Release';
595cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
605cf31920SAndreas Gohr        $version['type'] = 'Git';
6125c07f93SAnika Henke        $version['date'] = 'unknown';
62e570ed43SAndreas Gohr
635cf31920SAndreas Gohr        $inventory = DOKU_INC.'.git/logs/HEAD';
645cf31920SAndreas Gohr        if(is_file($inventory)){
65e570ed43SAndreas Gohr            $sz   = filesize($inventory);
665cf31920SAndreas Gohr            $seek = max(0,$sz-2000); // read from back of the file
67e570ed43SAndreas Gohr            $fh   = fopen($inventory,'rb');
68c29dc6e4SAndreas Gohr            fseek($fh,$seek);
69c29dc6e4SAndreas Gohr            $chunk = fread($fh,2000);
70c29dc6e4SAndreas Gohr            fclose($fh);
715cf31920SAndreas Gohr            $chunk = trim($chunk);
72b838050eSPiyush Mishra            $chunk = @array_pop(explode("\n",$chunk));   //last log line
73b838050eSPiyush Mishra            $chunk = @array_shift(explode("\t",$chunk)); //strip commit msg
745cf31920SAndreas Gohr            $chunk = explode(" ",$chunk);
755cf31920SAndreas Gohr            array_pop($chunk); //strip timezone
765cf31920SAndreas Gohr            $date = date('Y-m-d',array_pop($chunk));
775cf31920SAndreas Gohr            if($date) $version['date'] = $date;
785cf31920SAndreas Gohr        }
79c29dc6e4SAndreas Gohr    }else{
806a34de2dSAnika Henke        global $updateVersion;
816a34de2dSAnika Henke        $version['date'] = 'update version '.$updateVersion;
8225c07f93SAnika Henke        $version['type'] = 'snapshot?';
83c29dc6e4SAndreas Gohr    }
845cf31920SAndreas Gohr    return $version;
85c29dc6e4SAndreas Gohr}
86c29dc6e4SAndreas Gohr
87c29dc6e4SAndreas Gohr/**
8825c07f93SAnika Henke * Return DokuWiki's version (as a string)
8925c07f93SAnika Henke *
9025c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
9125c07f93SAnika Henke */
9225c07f93SAnika Henkefunction getVersion(){
9325c07f93SAnika Henke    $version = getVersionData();
9425c07f93SAnika Henke    return $version['type'].' '.$version['date'];
9525c07f93SAnika Henke}
9625c07f93SAnika Henke
9725c07f93SAnika Henke/**
98c29dc6e4SAndreas Gohr * Run a few sanity checks
99c29dc6e4SAndreas Gohr *
100c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
101c29dc6e4SAndreas Gohr */
102c29dc6e4SAndreas Gohrfunction check(){
103c29dc6e4SAndreas Gohr    global $conf;
104c29dc6e4SAndreas Gohr    global $INFO;
105*585bf44eSChristopher Smith    /* @var Input $INPUT */
106*585bf44eSChristopher Smith    global $INPUT;
107c29dc6e4SAndreas Gohr
1083f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
109c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
110c29dc6e4SAndreas Gohr
11121c9604eSAndreas Gohr        if(version_compare(phpversion(),'5.2.0','<')){
11221c9604eSAndreas Gohr            msg('Your PHP version is too old ('.phpversion().' vs. 5.2.0+ needed)',-1);
113c29dc6e4SAndreas Gohr        }else{
114c29dc6e4SAndreas Gohr            msg('PHP version '.phpversion(),1);
115c29dc6e4SAndreas Gohr        }
11608d1a8dfSAndreas Gohr    } else {
11708d1a8dfSAndreas Gohr        if(version_compare(phpversion(),'5.2.0','<')){
11808d1a8dfSAndreas Gohr            msg('Your PHP version is too old',-1);
11908d1a8dfSAndreas Gohr        }
12008d1a8dfSAndreas Gohr    }
121c29dc6e4SAndreas Gohr
12273038c47SAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
12373038c47SAndreas Gohr    if($mem){
12473038c47SAndreas Gohr        if($mem < 16777216){
12573038c47SAndreas Gohr            msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
12673038c47SAndreas Gohr        }elseif($mem < 20971520){
12773038c47SAndreas 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);
12873038c47SAndreas Gohr        }elseif($mem < 33554432){
12973038c47SAndreas 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);
13073038c47SAndreas Gohr        }else{
13173038c47SAndreas Gohr            msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
13273038c47SAndreas Gohr        }
13373038c47SAndreas Gohr    }
13473038c47SAndreas Gohr
135c29dc6e4SAndreas Gohr    if(is_writable($conf['changelog'])){
136c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
137c29dc6e4SAndreas Gohr    }else{
138c29dc6e4SAndreas Gohr        if (@file_exists($conf['changelog'])) {
139c29dc6e4SAndreas Gohr            msg('Changelog is not writable',-1);
140c29dc6e4SAndreas Gohr        }
141c29dc6e4SAndreas Gohr    }
142c29dc6e4SAndreas Gohr
143c29dc6e4SAndreas Gohr    if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
1442cdbda06SAnika Henke        msg('Old changelog exists', 0);
145c29dc6e4SAndreas Gohr    }
146c29dc6e4SAndreas Gohr
147c29dc6e4SAndreas Gohr    if (@file_exists($conf['changelog'].'_failed')) {
1482cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
149c29dc6e4SAndreas Gohr    } else if (@file_exists($conf['changelog'].'_importing')) {
150c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
151c29dc6e4SAndreas Gohr    } else if (@file_exists($conf['changelog'].'_import_ok')) {
1522cdbda06SAnika Henke        msg('Old changelog imported', 1);
153c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
1542cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
155c29dc6e4SAndreas Gohr        }
156c29dc6e4SAndreas Gohr    }
157c29dc6e4SAndreas Gohr
1584c7ecf15SGuy Brand    if(is_writable(DOKU_CONF)){
1594c7ecf15SGuy Brand        msg('conf directory is writable',1);
1604c7ecf15SGuy Brand    }else{
1614c7ecf15SGuy Brand        msg('conf directory is not writable',-1);
1624c7ecf15SGuy Brand    }
1634c7ecf15SGuy Brand
1640d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
165defb7d57SAnika Henke        global $config_cascade;
166defb7d57SAnika Henke        if(is_writable($config_cascade['plainauth.users']['default'])){
167c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
168c29dc6e4SAndreas Gohr        }else{
169c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
170c29dc6e4SAndreas Gohr        }
1710d487d8fSAndreas Gohr    }
172c29dc6e4SAndreas Gohr
173c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
174c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
175c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
176c29dc6e4SAndreas Gohr        }else{
177c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
1784222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
1794222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
1804222b898SAndreas Gohr            }
181c29dc6e4SAndreas Gohr        }
182c29dc6e4SAndreas Gohr    }else{
183c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
184c29dc6e4SAndreas Gohr    }
185c29dc6e4SAndreas Gohr
1863161005dSAndreas Gohr    if (!UTF8_PREGSUPPORT) {
187a731ed1dSAndreas Gohr        msg('PHP is missing UTF-8 support in Perl-Compatible Regular Expressions (PCRE)', -1);
188a731ed1dSAndreas Gohr    }
1893161005dSAndreas Gohr    if (!UTF8_PROPERTYSUPPORT) {
190a731ed1dSAndreas Gohr        msg('PHP is missing Unicode properties support in Perl-Compatible Regular Expressions (PCRE)', -1);
191a731ed1dSAndreas Gohr    }
192a731ed1dSAndreas Gohr
193e5ab313fSAndreas Gohr    $loc = setlocale(LC_ALL, 0);
194e5ab313fSAndreas Gohr    if(!$loc){
195e5ab313fSAndreas Gohr        msg('No valid locale is set for your PHP setup. You should fix this',-1);
196e5ab313fSAndreas Gohr    }elseif(stripos($loc,'utf') === false){
197e5ab313fSAndreas 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);
198e5ab313fSAndreas Gohr    }else{
199e5ab313fSAndreas Gohr        msg('Valid locale '.hsc($loc).' found.', 1);
200e5ab313fSAndreas Gohr    }
201e5ab313fSAndreas Gohr
202c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
203c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
204c29dc6e4SAndreas Gohr    }else{
205c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
206c29dc6e4SAndreas Gohr    }
207c29dc6e4SAndreas Gohr
2083d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
209*585bf44eSChristopher Smith        msg('You are currently logged in as '.$INPUT->server->str('REMOTE_USER').' ('.$INFO['userinfo']['name'].')',0);
210c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2113d3c095dSMike Frysinger    }else{
2123d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2133d3c095dSMike Frysinger    }
2143d3c095dSMike Frysinger
215c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
216c29dc6e4SAndreas Gohr
217c29dc6e4SAndreas Gohr    if(is_writable($INFO['filepath'])){
218c29dc6e4SAndreas Gohr        msg('The current page is writable by the webserver',0);
219c29dc6e4SAndreas Gohr    }else{
220c29dc6e4SAndreas Gohr        msg('The current page is not writable by the webserver',0);
221c29dc6e4SAndreas Gohr    }
222c29dc6e4SAndreas Gohr
223c29dc6e4SAndreas Gohr    if($INFO['writable']){
224c29dc6e4SAndreas Gohr        msg('The current page is writable by you',0);
225c29dc6e4SAndreas Gohr    }else{
2262cdbda06SAnika Henke        msg('The current page is not writable by you',0);
227c29dc6e4SAndreas Gohr    }
2283b1dfc83SAndreas Gohr
22926f7dbf5SMichael Hamann    // Check for corrupted search index
23026f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
23126f7dbf5SMichael Hamann    $index_corrupted = false;
23226f7dbf5SMichael Hamann    foreach ($lengths as $length) {
23326f7dbf5SMichael Hamann        if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
23426f7dbf5SMichael Hamann            $index_corrupted = true;
23526f7dbf5SMichael Hamann            break;
23626f7dbf5SMichael Hamann        }
23726f7dbf5SMichael Hamann    }
23826f7dbf5SMichael Hamann
23926f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
24026f7dbf5SMichael Hamann        if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
24126f7dbf5SMichael Hamann            $index_corrupted = true;
24226f7dbf5SMichael Hamann            break;
24326f7dbf5SMichael Hamann        }
24426f7dbf5SMichael Hamann    }
24526f7dbf5SMichael Hamann
24626f7dbf5SMichael Hamann    if ($index_corrupted)
2473d94d9edSMichael Hamann        msg('The search index is corrupted. It might produce wrong results and most
2483d94d9edSMichael Hamann                probably needs to be rebuilt. See
24926f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2503d94d9edSMichael Hamann                for ways to rebuild the search index.', -1);
25126f7dbf5SMichael Hamann    elseif (!empty($lengths))
2523d94d9edSMichael Hamann        msg('The search index seems to be working', 1);
25326f7dbf5SMichael Hamann    else
2543d94d9edSMichael Hamann        msg('The search index is empty. See
25526f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
2563d94d9edSMichael Hamann                for help on how to fix the search index. If the default indexer
2573d94d9edSMichael Hamann                isn\'t used or the wiki is actually empty this is normal.');
258c29dc6e4SAndreas Gohr}
259c29dc6e4SAndreas Gohr
260c29dc6e4SAndreas Gohr/**
261c29dc6e4SAndreas Gohr * print a message
262c29dc6e4SAndreas Gohr *
263c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
264c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
265c29dc6e4SAndreas Gohr * using html_msgarea()
266c29dc6e4SAndreas Gohr *
267c29dc6e4SAndreas Gohr *
268c29dc6e4SAndreas Gohr * Levels can be:
269c29dc6e4SAndreas Gohr *
270c29dc6e4SAndreas Gohr * -1 error
271c29dc6e4SAndreas Gohr *  0 info
272c29dc6e4SAndreas Gohr *  1 success
273c29dc6e4SAndreas Gohr *
274c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
275c29dc6e4SAndreas Gohr * @see    html_msgarea
276c29dc6e4SAndreas Gohr */
277d3bae478SChristopher Smith
278d3bae478SChristopher Smithdefine('MSG_PUBLIC', 0);
279d3bae478SChristopher Smithdefine('MSG_USERS_ONLY', 1);
280d3bae478SChristopher Smithdefine('MSG_MANAGERS_ONLY',2);
281d3bae478SChristopher Smithdefine('MSG_ADMINS_ONLY',4);
282d3bae478SChristopher Smith
283f755f9abSChristopher Smithfunction msg($message,$lvl=0,$line='',$file='',$allow=MSG_PUBLIC){
284cc58224cSMichael Hamann    global $MSG, $MSG_shown;
285c29dc6e4SAndreas Gohr    $errors[-1] = 'error';
286c29dc6e4SAndreas Gohr    $errors[0]  = 'info';
287c29dc6e4SAndreas Gohr    $errors[1]  = 'success';
288c29dc6e4SAndreas Gohr    $errors[2]  = 'notify';
289c29dc6e4SAndreas Gohr
2903009a773SAndreas Gohr    if($line || $file) $message.=' ['.utf8_basename($file).':'.$line.']';
291c29dc6e4SAndreas Gohr
292c29dc6e4SAndreas Gohr    if(!isset($MSG)) $MSG = array();
293f755f9abSChristopher Smith    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message, 'allow' => $allow);
294cc58224cSMichael Hamann    if(isset($MSG_shown) || headers_sent()){
295c29dc6e4SAndreas Gohr        if(function_exists('html_msgarea')){
296c29dc6e4SAndreas Gohr            html_msgarea();
297c29dc6e4SAndreas Gohr        }else{
298c29dc6e4SAndreas Gohr            print "ERROR($lvl) $message";
299c29dc6e4SAndreas Gohr        }
30069266de5SDominik Eckelmann        unset($GLOBALS['MSG']);
301c29dc6e4SAndreas Gohr    }
302c29dc6e4SAndreas Gohr}
303f755f9abSChristopher Smith/**
304f755f9abSChristopher Smith * Determine whether the current user is allowed to view the message
305f755f9abSChristopher Smith * in the $msg data structure
306f755f9abSChristopher Smith *
307f755f9abSChristopher Smith * @param  $msg   array    dokuwiki msg structure
308f755f9abSChristopher Smith *                         msg   => string, the message
309f755f9abSChristopher Smith *                         lvl   => int, level of the message (see msg() function)
310f755f9abSChristopher Smith *                         allow => int, flag used to determine who is allowed to see the message
311f755f9abSChristopher Smith *                                       see MSG_* constants
312f755f9abSChristopher Smith */
313f755f9abSChristopher Smithfunction info_msg_allowed($msg){
314d3bae478SChristopher Smith    global $INFO, $auth;
315d3bae478SChristopher Smith
316d3bae478SChristopher Smith    // is the message public? - everyone and anyone can see it
317f755f9abSChristopher Smith    if (empty($msg['allow']) || ($msg['allow'] == MSG_PUBLIC)) return true;
318d3bae478SChristopher Smith
319d3bae478SChristopher Smith    // restricted msg, but no authentication
320d3bae478SChristopher Smith    if (empty($auth)) return false;
321d3bae478SChristopher Smith
322f755f9abSChristopher Smith    switch ($msg['allow']){
323d3bae478SChristopher Smith        case MSG_USERS_ONLY:
324d3bae478SChristopher Smith            return !empty($INFO['userinfo']);
325d3bae478SChristopher Smith
326d3bae478SChristopher Smith        case MSG_MANAGERS_ONLY:
327d3bae478SChristopher Smith            return $INFO['ismanager'];
328d3bae478SChristopher Smith
329d3bae478SChristopher Smith        case MSG_ADMINS_ONLY:
330d3bae478SChristopher Smith            return $INFO['isadmin'];
331d3bae478SChristopher Smith
332d3bae478SChristopher Smith        default:
333f755f9abSChristopher Smith            trigger_error('invalid msg allow restriction.  msg="'.$msg['msg'].'" allow='.$msg['allow'].'"', E_USER_WARNING);
334d3bae478SChristopher Smith            return $INFO['isadmin'];
335d3bae478SChristopher Smith    }
336d3bae478SChristopher Smith
337d3bae478SChristopher Smith    return false;
338d3bae478SChristopher Smith}
339d3bae478SChristopher Smith
340c29dc6e4SAndreas Gohr/**
341c29dc6e4SAndreas Gohr * print debug messages
342c29dc6e4SAndreas Gohr *
343c29dc6e4SAndreas Gohr * little function to print the content of a var
344c29dc6e4SAndreas Gohr *
345c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
346c29dc6e4SAndreas Gohr */
347c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
34813493794SAndreas Gohr    if($hidden){
34913493794SAndreas Gohr        echo "<!--\n";
350c29dc6e4SAndreas Gohr        print_r($msg);
35113493794SAndreas Gohr        echo "\n-->";
35213493794SAndreas Gohr    }else{
35313493794SAndreas Gohr        echo '<pre class="dbg">';
35413493794SAndreas Gohr        echo hsc(print_r($msg,true));
35513493794SAndreas Gohr        echo '</pre>';
35613493794SAndreas Gohr    }
357c29dc6e4SAndreas Gohr}
358c29dc6e4SAndreas Gohr
359c29dc6e4SAndreas Gohr/**
360c29dc6e4SAndreas Gohr * Print info to a log file
361c29dc6e4SAndreas Gohr *
362c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
363c29dc6e4SAndreas Gohr */
3640699d739SAndreas Gohrfunction dbglog($msg,$header=''){
365c29dc6e4SAndreas Gohr    global $conf;
366*585bf44eSChristopher Smith    /* @var Input $INPUT */
367*585bf44eSChristopher Smith    global $INPUT;
368*585bf44eSChristopher Smith
3695bd930ffSMichael Hamann    // The debug log isn't automatically cleaned thus only write it when
3705bd930ffSMichael Hamann    // debugging has been enabled by the user.
3715bd930ffSMichael Hamann    if($conf['allowdebug'] !== 1) return;
372c7408a63SAndreas Gohr    if(is_object($msg) || is_array($msg)){
373c7408a63SAndreas Gohr        $msg = print_r($msg,true);
374c7408a63SAndreas Gohr    }
375c7408a63SAndreas Gohr
3760699d739SAndreas Gohr    if($header) $msg = "$header\n$msg";
3770699d739SAndreas Gohr
378c29dc6e4SAndreas Gohr    $file = $conf['cachedir'].'/debug.log';
379c29dc6e4SAndreas Gohr    $fh = fopen($file,'a');
380c29dc6e4SAndreas Gohr    if($fh){
381*585bf44eSChristopher Smith        fwrite($fh,date('H:i:s ').$INPUT->server->str('REMOTE_ADDR').': '.$msg."\n");
382c29dc6e4SAndreas Gohr        fclose($fh);
383c29dc6e4SAndreas Gohr    }
384c29dc6e4SAndreas Gohr}
385c29dc6e4SAndreas Gohr
386db09e31eSAndreas Gohr/**
387db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
388db09e31eSAndreas Gohr *
389db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
390db09e31eSAndreas Gohr */
391db09e31eSAndreas Gohrfunction dbg_backtrace(){
392db09e31eSAndreas Gohr    // Get backtrace
393db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
394db09e31eSAndreas Gohr
395db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
396db09e31eSAndreas Gohr    array_shift($backtrace);
397db09e31eSAndreas Gohr
398db09e31eSAndreas Gohr    // Iterate backtrace
399db09e31eSAndreas Gohr    $calls = array();
400db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
401db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
402db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
403db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
404db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
405db09e31eSAndreas Gohr
4068259f1aaSAndreas Gohr        $params = array();
407db09e31eSAndreas Gohr        if (isset($call['args'])){
4088259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
4098259f1aaSAndreas Gohr                if(is_object($arg)){
4108259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
4118259f1aaSAndreas Gohr                }elseif(is_array($arg)){
4128259f1aaSAndreas Gohr                    $params[] = '[Array]';
4138259f1aaSAndreas Gohr                }elseif(is_null($arg)){
4148259f1aaSAndreas Gohr                    $param[] = '[NULL]';
4158259f1aaSAndreas Gohr                }else{
4168259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
417db09e31eSAndreas Gohr                }
4188259f1aaSAndreas Gohr            }
4198259f1aaSAndreas Gohr        }
4208259f1aaSAndreas Gohr        $params = implode(', ',$params);
421db09e31eSAndreas Gohr
4228259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
423db09e31eSAndreas Gohr                $function,
424db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
425db09e31eSAndreas Gohr                $location);
426db09e31eSAndreas Gohr    }
427db09e31eSAndreas Gohr    ksort($calls);
428db09e31eSAndreas Gohr
429db09e31eSAndreas Gohr    return implode("\n", $calls);
430db09e31eSAndreas Gohr}
431db09e31eSAndreas Gohr
43224297a69SAndreas Gohr/**
43324297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
43424297a69SAndreas Gohr *
43524297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
43624297a69SAndreas Gohr * debug output
43724297a69SAndreas Gohr *
43824297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
43924297a69SAndreas Gohr */
44024297a69SAndreas Gohrfunction debug_guard(&$data){
44124297a69SAndreas Gohr    foreach($data as $key => $value){
44224297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
44324297a69SAndreas Gohr            $data[$key] = '***';
44424297a69SAndreas Gohr            continue;
44524297a69SAndreas Gohr        }
44624297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
44724297a69SAndreas Gohr    }
44824297a69SAndreas Gohr}
449