xref: /dokuwiki/inc/infoutils.php (revision 26f7dbf522bd7473a47791384d73d5721bca8984)
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
27ef362bb8SAnika Henke    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.'doku.php')){
28c29dc6e4SAndreas Gohr        $http = new DokuHTTPClient();
29c29dc6e4SAndreas Gohr        $http->timeout = 8;
30ef362bb8SAnika Henke        $data = $http->get(DOKU_MESSAGEURL.$updateVersion);
31c29dc6e4SAndreas Gohr        io_saveFile($cf,$data);
32c29dc6e4SAndreas Gohr    }else{
33c29dc6e4SAndreas Gohr        $data = io_readFile($cf);
34c29dc6e4SAndreas Gohr    }
35c29dc6e4SAndreas Gohr
36c29dc6e4SAndreas Gohr    // show messages through the usual message mechanism
37c29dc6e4SAndreas Gohr    $msgs = explode("\n%\n",$data);
38c29dc6e4SAndreas Gohr    foreach($msgs as $msg){
39c29dc6e4SAndreas Gohr        if($msg) msg($msg,2);
40c29dc6e4SAndreas Gohr    }
41c29dc6e4SAndreas Gohr}
42c29dc6e4SAndreas Gohr
43c29dc6e4SAndreas Gohr
44c29dc6e4SAndreas Gohr/**
4525c07f93SAnika Henke * Return DokuWiki's version (split up in date and type)
46c29dc6e4SAndreas Gohr *
47c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
48c29dc6e4SAndreas Gohr */
4925c07f93SAnika Henkefunction getVersionData(){
5025c07f93SAnika Henke    $version = array();
51c29dc6e4SAndreas Gohr    //import version string
52e9208eb1SAndreas Gohr    if(@file_exists(DOKU_INC.'VERSION')){
53c29dc6e4SAndreas Gohr        //official release
5425c07f93SAnika Henke        $version['date'] = trim(io_readfile(DOKU_INC.'VERSION'));
5525c07f93SAnika Henke        $version['type'] = 'Release';
565cf31920SAndreas Gohr    }elseif(is_dir(DOKU_INC.'.git')){
575cf31920SAndreas Gohr        $version['type'] = 'Git';
5825c07f93SAnika Henke        $version['date'] = 'unknown';
59e570ed43SAndreas Gohr
605cf31920SAndreas Gohr        $inventory = DOKU_INC.'.git/logs/HEAD';
615cf31920SAndreas Gohr        if(is_file($inventory)){
62e570ed43SAndreas Gohr            $sz   = filesize($inventory);
635cf31920SAndreas Gohr            $seek = max(0,$sz-2000); // read from back of the file
64e570ed43SAndreas Gohr            $fh   = fopen($inventory,'rb');
65c29dc6e4SAndreas Gohr            fseek($fh,$seek);
66c29dc6e4SAndreas Gohr            $chunk = fread($fh,2000);
67c29dc6e4SAndreas Gohr            fclose($fh);
685cf31920SAndreas Gohr            $chunk = trim($chunk);
695cf31920SAndreas Gohr            $chunk = array_pop(explode("\n",$chunk));   //last log line
705cf31920SAndreas Gohr            $chunk = array_shift(explode("\t",$chunk)); //strip commit msg
715cf31920SAndreas Gohr            $chunk = explode(" ",$chunk);
725cf31920SAndreas Gohr            array_pop($chunk); //strip timezone
735cf31920SAndreas Gohr            $date = date('Y-m-d',array_pop($chunk));
745cf31920SAndreas Gohr            if($date) $version['date'] = $date;
755cf31920SAndreas Gohr        }
76c29dc6e4SAndreas Gohr    }else{
7725c07f93SAnika Henke        $version['date'] = 'unknown';
7825c07f93SAnika Henke        $version['type'] = 'snapshot?';
79c29dc6e4SAndreas Gohr    }
805cf31920SAndreas Gohr    return $version;
81c29dc6e4SAndreas Gohr}
82c29dc6e4SAndreas Gohr
83c29dc6e4SAndreas Gohr/**
8425c07f93SAnika Henke * Return DokuWiki's version (as a string)
8525c07f93SAnika Henke *
8625c07f93SAnika Henke * @author Anika Henke <anika@selfthinker.org>
8725c07f93SAnika Henke */
8825c07f93SAnika Henkefunction getVersion(){
8925c07f93SAnika Henke    $version = getVersionData();
9025c07f93SAnika Henke    return $version['type'].' '.$version['date'];
9125c07f93SAnika Henke}
9225c07f93SAnika Henke
9325c07f93SAnika Henke/**
94c29dc6e4SAndreas Gohr * Run a few sanity checks
95c29dc6e4SAndreas Gohr *
96c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
97c29dc6e4SAndreas Gohr */
98c29dc6e4SAndreas Gohrfunction check(){
99c29dc6e4SAndreas Gohr    global $conf;
100c29dc6e4SAndreas Gohr    global $INFO;
101c29dc6e4SAndreas Gohr
1023f803e5eSGina Haeussge    if ($INFO['isadmin'] || $INFO['ismanager']){
103c29dc6e4SAndreas Gohr        msg('DokuWiki version: '.getVersion(),1);
1043f803e5eSGina Haeussge    }
105c29dc6e4SAndreas Gohr
106ab91da89SAndreas Gohr    if(version_compare(phpversion(),'5.1.2','<')){
107ab91da89SAndreas Gohr        msg('Your PHP version is too old ('.phpversion().' vs. 5.1.2+ needed)',-1);
108c29dc6e4SAndreas Gohr    }else{
109c29dc6e4SAndreas Gohr        msg('PHP version '.phpversion(),1);
110c29dc6e4SAndreas Gohr    }
111c29dc6e4SAndreas Gohr
11273038c47SAndreas Gohr    $mem = (int) php_to_byte(ini_get('memory_limit'));
11373038c47SAndreas Gohr    if($mem){
11473038c47SAndreas Gohr        if($mem < 16777216){
11573038c47SAndreas Gohr            msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
11673038c47SAndreas Gohr        }elseif($mem < 20971520){
11773038c47SAndreas 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);
11873038c47SAndreas Gohr        }elseif($mem < 33554432){
11973038c47SAndreas 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);
12073038c47SAndreas Gohr        }else{
12173038c47SAndreas Gohr            msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
12273038c47SAndreas Gohr        }
12373038c47SAndreas Gohr    }
12473038c47SAndreas Gohr
125c29dc6e4SAndreas Gohr    if(is_writable($conf['changelog'])){
126c29dc6e4SAndreas Gohr        msg('Changelog is writable',1);
127c29dc6e4SAndreas Gohr    }else{
128c29dc6e4SAndreas Gohr        if (@file_exists($conf['changelog'])) {
129c29dc6e4SAndreas Gohr            msg('Changelog is not writable',-1);
130c29dc6e4SAndreas Gohr        }
131c29dc6e4SAndreas Gohr    }
132c29dc6e4SAndreas Gohr
133c29dc6e4SAndreas Gohr    if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
1342cdbda06SAnika Henke        msg('Old changelog exists', 0);
135c29dc6e4SAndreas Gohr    }
136c29dc6e4SAndreas Gohr
137c29dc6e4SAndreas Gohr    if (@file_exists($conf['changelog'].'_failed')) {
1382cdbda06SAnika Henke        msg('Importing old changelog failed', -1);
139c29dc6e4SAndreas Gohr    } else if (@file_exists($conf['changelog'].'_importing')) {
140c29dc6e4SAndreas Gohr        msg('Importing old changelog now.', 0);
141c29dc6e4SAndreas Gohr    } else if (@file_exists($conf['changelog'].'_import_ok')) {
1422cdbda06SAnika Henke        msg('Old changelog imported', 1);
143c29dc6e4SAndreas Gohr        if (!plugin_isdisabled('importoldchangelog')) {
1442cdbda06SAnika Henke            msg('Importoldchangelog plugin not disabled after import', -1);
145c29dc6e4SAndreas Gohr        }
146c29dc6e4SAndreas Gohr    }
147c29dc6e4SAndreas Gohr
148c29dc6e4SAndreas Gohr    if(is_writable($conf['datadir'])){
149c29dc6e4SAndreas Gohr        msg('Datadir is writable',1);
150c29dc6e4SAndreas Gohr    }else{
151c29dc6e4SAndreas Gohr        msg('Datadir is not writable',-1);
152c29dc6e4SAndreas Gohr    }
153c29dc6e4SAndreas Gohr
154c29dc6e4SAndreas Gohr    if(is_writable($conf['olddir'])){
155c29dc6e4SAndreas Gohr        msg('Attic is writable',1);
156c29dc6e4SAndreas Gohr    }else{
157c29dc6e4SAndreas Gohr        msg('Attic is not writable',-1);
158c29dc6e4SAndreas Gohr    }
159c29dc6e4SAndreas Gohr
160c29dc6e4SAndreas Gohr    if(is_writable($conf['mediadir'])){
161c29dc6e4SAndreas Gohr        msg('Mediadir is writable',1);
162c29dc6e4SAndreas Gohr    }else{
163c29dc6e4SAndreas Gohr        msg('Mediadir is not writable',-1);
164c29dc6e4SAndreas Gohr    }
165c29dc6e4SAndreas Gohr
166c29dc6e4SAndreas Gohr    if(is_writable($conf['cachedir'])){
167c29dc6e4SAndreas Gohr        msg('Cachedir is writable',1);
168c29dc6e4SAndreas Gohr    }else{
169c29dc6e4SAndreas Gohr        msg('Cachedir is not writable',-1);
170c29dc6e4SAndreas Gohr    }
171c29dc6e4SAndreas Gohr
172c29dc6e4SAndreas Gohr    if(is_writable($conf['lockdir'])){
173c29dc6e4SAndreas Gohr        msg('Lockdir is writable',1);
174c29dc6e4SAndreas Gohr    }else{
175c29dc6e4SAndreas Gohr        msg('Lockdir is not writable',-1);
176c29dc6e4SAndreas Gohr    }
177c29dc6e4SAndreas Gohr
1780d487d8fSAndreas Gohr    if($conf['authtype'] == 'plain'){
179c29dc6e4SAndreas Gohr        if(is_writable(DOKU_CONF.'users.auth.php')){
180c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is writable',1);
181c29dc6e4SAndreas Gohr        }else{
182c29dc6e4SAndreas Gohr            msg('conf/users.auth.php is not writable',0);
183c29dc6e4SAndreas Gohr        }
1840d487d8fSAndreas Gohr    }
185c29dc6e4SAndreas Gohr
186c29dc6e4SAndreas Gohr    if(function_exists('mb_strpos')){
187c29dc6e4SAndreas Gohr        if(defined('UTF8_NOMBSTRING')){
188c29dc6e4SAndreas Gohr            msg('mb_string extension is available but will not be used',0);
189c29dc6e4SAndreas Gohr        }else{
190c29dc6e4SAndreas Gohr            msg('mb_string extension is available and will be used',1);
1914222b898SAndreas Gohr            if(ini_get('mbstring.func_overload') != 0){
1924222b898SAndreas Gohr                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
1934222b898SAndreas Gohr            }
194c29dc6e4SAndreas Gohr        }
195c29dc6e4SAndreas Gohr    }else{
196c29dc6e4SAndreas Gohr        msg('mb_string extension not available - PHP only replacements will be used',0);
197c29dc6e4SAndreas Gohr    }
198c29dc6e4SAndreas Gohr
199c29dc6e4SAndreas Gohr    if($conf['allowdebug']){
200c29dc6e4SAndreas Gohr        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
201c29dc6e4SAndreas Gohr    }else{
202c29dc6e4SAndreas Gohr        msg('Debugging support is disabled',1);
203c29dc6e4SAndreas Gohr    }
204c29dc6e4SAndreas Gohr
2053d3c095dSMike Frysinger    if($INFO['userinfo']['name']){
206c1791678SAndreas Gohr        msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0);
207c1791678SAndreas Gohr        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
2083d3c095dSMike Frysinger    }else{
2093d3c095dSMike Frysinger        msg('You are currently not logged in',0);
2103d3c095dSMike Frysinger    }
2113d3c095dSMike Frysinger
212c29dc6e4SAndreas Gohr    msg('Your current permission for this page is '.$INFO['perm'],0);
213c29dc6e4SAndreas Gohr
214c29dc6e4SAndreas Gohr    if(is_writable($INFO['filepath'])){
215c29dc6e4SAndreas Gohr        msg('The current page is writable by the webserver',0);
216c29dc6e4SAndreas Gohr    }else{
217c29dc6e4SAndreas Gohr        msg('The current page is not writable by the webserver',0);
218c29dc6e4SAndreas Gohr    }
219c29dc6e4SAndreas Gohr
220c29dc6e4SAndreas Gohr    if($INFO['writable']){
221c29dc6e4SAndreas Gohr        msg('The current page is writable by you',0);
222c29dc6e4SAndreas Gohr    }else{
2232cdbda06SAnika Henke        msg('The current page is not writable by you',0);
224c29dc6e4SAndreas Gohr    }
2253b1dfc83SAndreas Gohr
2263b1dfc83SAndreas Gohr    $check = wl('','',true).'data/_dummy';
2273b1dfc83SAndreas Gohr    $http = new DokuHTTPClient();
2283b1dfc83SAndreas Gohr    $http->timeout = 6;
2293b1dfc83SAndreas Gohr    $res = $http->get($check);
2303b1dfc83SAndreas Gohr    if(strpos($res,'data directory') !== false){
2313b1dfc83SAndreas Gohr        msg('It seems like the data directory is accessible from the web.
2323b1dfc83SAndreas Gohr                Make sure this directory is properly protected
2333b1dfc83SAndreas Gohr                (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
2343b1dfc83SAndreas Gohr    }elseif($http->status == 404 || $http->status == 403){
2353b1dfc83SAndreas Gohr        msg('The data directory seems to be properly protected',1);
2363b1dfc83SAndreas Gohr    }else{
2373b1dfc83SAndreas Gohr        msg('Failed to check if the data directory is accessible from the web.
2383b1dfc83SAndreas Gohr                Make sure this directory is properly protected
2393b1dfc83SAndreas Gohr                (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
2403b1dfc83SAndreas Gohr    }
241*26f7dbf5SMichael Hamann
242*26f7dbf5SMichael Hamann    // Check for corrupted search index
243*26f7dbf5SMichael Hamann    $lengths = idx_listIndexLengths();
244*26f7dbf5SMichael Hamann    $index_corrupted = false;
245*26f7dbf5SMichael Hamann    foreach ($lengths as $length) {
246*26f7dbf5SMichael Hamann        if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
247*26f7dbf5SMichael Hamann            $index_corrupted = true;
248*26f7dbf5SMichael Hamann            break;
249*26f7dbf5SMichael Hamann        }
250*26f7dbf5SMichael Hamann    }
251*26f7dbf5SMichael Hamann
252*26f7dbf5SMichael Hamann    foreach (idx_getIndex('metadata', '') as $index) {
253*26f7dbf5SMichael Hamann        if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
254*26f7dbf5SMichael Hamann            $index_corrupted = true;
255*26f7dbf5SMichael Hamann            break;
256*26f7dbf5SMichael Hamann        }
257*26f7dbf5SMichael Hamann    }
258*26f7dbf5SMichael Hamann
259*26f7dbf5SMichael Hamann    if ($index_corrupted)
260*26f7dbf5SMichael Hamann        msg('Your search index is corrupted. It might produce wrong results
261*26f7dbf5SMichael Hamann                unless you fix it. See
262*26f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
263*26f7dbf5SMichael Hamann                for ways to rebuild your search index.', -1);
264*26f7dbf5SMichael Hamann    elseif (!empty($lengths))
265*26f7dbf5SMichael Hamann        msg('Your search index seems to be okay.', 1);
266*26f7dbf5SMichael Hamann    else
267*26f7dbf5SMichael Hamann        msg('Your search index is empty. See
268*26f7dbf5SMichael Hamann                <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
269*26f7dbf5SMichael Hamann                for help on how to fix your search index unless you are using an
270*26f7dbf5SMichael Hamann                external indexer or your wiki is actually empty.');
271*26f7dbf5SMichael Hamann
272c29dc6e4SAndreas Gohr}
273c29dc6e4SAndreas Gohr
274c29dc6e4SAndreas Gohr/**
275c29dc6e4SAndreas Gohr * print a message
276c29dc6e4SAndreas Gohr *
277c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added
278c29dc6e4SAndreas Gohr * to the global message array else it's printed directly
279c29dc6e4SAndreas Gohr * using html_msgarea()
280c29dc6e4SAndreas Gohr *
281c29dc6e4SAndreas Gohr *
282c29dc6e4SAndreas Gohr * Levels can be:
283c29dc6e4SAndreas Gohr *
284c29dc6e4SAndreas Gohr * -1 error
285c29dc6e4SAndreas Gohr *  0 info
286c29dc6e4SAndreas Gohr *  1 success
287c29dc6e4SAndreas Gohr *
288c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
289c29dc6e4SAndreas Gohr * @see    html_msgarea
290c29dc6e4SAndreas Gohr */
291c29dc6e4SAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){
292cc58224cSMichael Hamann    global $MSG, $MSG_shown;
293c29dc6e4SAndreas Gohr    $errors[-1] = 'error';
294c29dc6e4SAndreas Gohr    $errors[0]  = 'info';
295c29dc6e4SAndreas Gohr    $errors[1]  = 'success';
296c29dc6e4SAndreas Gohr    $errors[2]  = 'notify';
297c29dc6e4SAndreas Gohr
298c29dc6e4SAndreas Gohr    if($line || $file) $message.=' ['.basename($file).':'.$line.']';
299c29dc6e4SAndreas Gohr
300c29dc6e4SAndreas Gohr    if(!isset($MSG)) $MSG = array();
301c29dc6e4SAndreas Gohr    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
302cc58224cSMichael Hamann    if(isset($MSG_shown) || headers_sent()){
303c29dc6e4SAndreas Gohr        if(function_exists('html_msgarea')){
304c29dc6e4SAndreas Gohr            html_msgarea();
305c29dc6e4SAndreas Gohr        }else{
306c29dc6e4SAndreas Gohr            print "ERROR($lvl) $message";
307c29dc6e4SAndreas Gohr        }
30869266de5SDominik Eckelmann        unset($GLOBALS['MSG']);
309c29dc6e4SAndreas Gohr    }
310c29dc6e4SAndreas Gohr}
311c29dc6e4SAndreas Gohr
312c29dc6e4SAndreas Gohr/**
313c29dc6e4SAndreas Gohr * print debug messages
314c29dc6e4SAndreas Gohr *
315c29dc6e4SAndreas Gohr * little function to print the content of a var
316c29dc6e4SAndreas Gohr *
317c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
318c29dc6e4SAndreas Gohr */
319c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){
32013493794SAndreas Gohr    if($hidden){
32113493794SAndreas Gohr        echo "<!--\n";
322c29dc6e4SAndreas Gohr        print_r($msg);
32313493794SAndreas Gohr        echo "\n-->";
32413493794SAndreas Gohr    }else{
32513493794SAndreas Gohr        echo '<pre class="dbg">';
32613493794SAndreas Gohr        echo hsc(print_r($msg,true));
32713493794SAndreas Gohr        echo '</pre>';
32813493794SAndreas Gohr    }
329c29dc6e4SAndreas Gohr}
330c29dc6e4SAndreas Gohr
331c29dc6e4SAndreas Gohr/**
332c29dc6e4SAndreas Gohr * Print info to a log file
333c29dc6e4SAndreas Gohr *
334c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
335c29dc6e4SAndreas Gohr */
3360699d739SAndreas Gohrfunction dbglog($msg,$header=''){
337c29dc6e4SAndreas Gohr    global $conf;
3385bd930ffSMichael Hamann    // The debug log isn't automatically cleaned thus only write it when
3395bd930ffSMichael Hamann    // debugging has been enabled by the user.
3405bd930ffSMichael Hamann    if($conf['allowdebug'] !== 1) return;
341c7408a63SAndreas Gohr    if(is_object($msg) || is_array($msg)){
342c7408a63SAndreas Gohr        $msg = print_r($msg,true);
343c7408a63SAndreas Gohr    }
344c7408a63SAndreas Gohr
3450699d739SAndreas Gohr    if($header) $msg = "$header\n$msg";
3460699d739SAndreas Gohr
347c29dc6e4SAndreas Gohr    $file = $conf['cachedir'].'/debug.log';
348c29dc6e4SAndreas Gohr    $fh = fopen($file,'a');
349c29dc6e4SAndreas Gohr    if($fh){
350c29dc6e4SAndreas Gohr        fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
351c29dc6e4SAndreas Gohr        fclose($fh);
352c29dc6e4SAndreas Gohr    }
353c29dc6e4SAndreas Gohr}
354c29dc6e4SAndreas Gohr
355db09e31eSAndreas Gohr/**
356db09e31eSAndreas Gohr * Print a reversed, prettyprinted backtrace
357db09e31eSAndreas Gohr *
358db09e31eSAndreas Gohr * @author Gary Owen <gary_owen@bigfoot.com>
359db09e31eSAndreas Gohr */
360db09e31eSAndreas Gohrfunction dbg_backtrace(){
361db09e31eSAndreas Gohr    // Get backtrace
362db09e31eSAndreas Gohr    $backtrace = debug_backtrace();
363db09e31eSAndreas Gohr
364db09e31eSAndreas Gohr    // Unset call to debug_print_backtrace
365db09e31eSAndreas Gohr    array_shift($backtrace);
366db09e31eSAndreas Gohr
367db09e31eSAndreas Gohr    // Iterate backtrace
368db09e31eSAndreas Gohr    $calls = array();
369db09e31eSAndreas Gohr    $depth = count($backtrace) - 1;
370db09e31eSAndreas Gohr    foreach ($backtrace as $i => $call) {
371db09e31eSAndreas Gohr        $location = $call['file'] . ':' . $call['line'];
372db09e31eSAndreas Gohr        $function = (isset($call['class'])) ?
373db09e31eSAndreas Gohr            $call['class'] . $call['type'] . $call['function'] : $call['function'];
374db09e31eSAndreas Gohr
3758259f1aaSAndreas Gohr        $params = array();
376db09e31eSAndreas Gohr        if (isset($call['args'])){
3778259f1aaSAndreas Gohr            foreach($call['args'] as $arg){
3788259f1aaSAndreas Gohr                if(is_object($arg)){
3798259f1aaSAndreas Gohr                    $params[] = '[Object '.get_class($arg).']';
3808259f1aaSAndreas Gohr                }elseif(is_array($arg)){
3818259f1aaSAndreas Gohr                    $params[] = '[Array]';
3828259f1aaSAndreas Gohr                }elseif(is_null($arg)){
3838259f1aaSAndreas Gohr                    $param[] = '[NULL]';
3848259f1aaSAndreas Gohr                }else{
3858259f1aaSAndreas Gohr                    $params[] = (string) '"'.$arg.'"';
386db09e31eSAndreas Gohr                }
3878259f1aaSAndreas Gohr            }
3888259f1aaSAndreas Gohr        }
3898259f1aaSAndreas Gohr        $params = implode(', ',$params);
390db09e31eSAndreas Gohr
3918259f1aaSAndreas Gohr        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
392db09e31eSAndreas Gohr                $function,
393db09e31eSAndreas Gohr                str_replace("\n", '\n', $params),
394db09e31eSAndreas Gohr                $location);
395db09e31eSAndreas Gohr    }
396db09e31eSAndreas Gohr    ksort($calls);
397db09e31eSAndreas Gohr
398db09e31eSAndreas Gohr    return implode("\n", $calls);
399db09e31eSAndreas Gohr}
400db09e31eSAndreas Gohr
40124297a69SAndreas Gohr/**
40224297a69SAndreas Gohr * Remove all data from an array where the key seems to point to sensitive data
40324297a69SAndreas Gohr *
40424297a69SAndreas Gohr * This is used to remove passwords, mail addresses and similar data from the
40524297a69SAndreas Gohr * debug output
40624297a69SAndreas Gohr *
40724297a69SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
40824297a69SAndreas Gohr */
40924297a69SAndreas Gohrfunction debug_guard(&$data){
41024297a69SAndreas Gohr    foreach($data as $key => $value){
41124297a69SAndreas Gohr        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
41224297a69SAndreas Gohr            $data[$key] = '***';
41324297a69SAndreas Gohr            continue;
41424297a69SAndreas Gohr        }
41524297a69SAndreas Gohr        if(is_array($value)) debug_guard($data[$key]);
41624297a69SAndreas Gohr    }
41724297a69SAndreas Gohr}
418