xref: /dokuwiki/inc/infoutils.php (revision 7aeda574860428d96fa1660613646b5684c1a6ea)
1<?php
2/**
3 * Information and debugging functions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8if(!defined('DOKU_INC')) die('meh.');
9if(!defined('DOKU_MESSAGEURL')) define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
10
11/**
12 * Check for new messages from upstream
13 *
14 * @author Andreas Gohr <andi@splitbrain.org>
15 */
16function checkUpdateMessages(){
17    global $conf;
18    global $INFO;
19    if(!$conf['updatecheck']) return;
20    if($conf['useacl'] && !$INFO['ismanager']) return;
21
22    $cf = $conf['cachedir'].'/messages.txt';
23    $lm = @filemtime($cf);
24
25    // check if new messages needs to be fetched
26    if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_CONF.'msg')){
27        $num = @file(DOKU_CONF.'msg');
28        $num = is_array($num) ? (int) $num[0] : 0;
29        $http = new DokuHTTPClient();
30        $http->timeout = 8;
31        $data = $http->get(DOKU_MESSAGEURL.$num);
32        io_saveFile($cf,$data);
33    }else{
34        $data = io_readFile($cf);
35    }
36
37    // show messages through the usual message mechanism
38    $msgs = explode("\n%\n",$data);
39    foreach($msgs as $msg){
40        if($msg) msg($msg,2);
41    }
42}
43
44
45/**
46 * Return DokuWiki's version (split up in date and type)
47 *
48 * @author Andreas Gohr <andi@splitbrain.org>
49 */
50function getVersionData(){
51    $version = array();
52    //import version string
53    if(@file_exists(DOKU_INC.'VERSION')){
54        //official release
55        $version['date'] = trim(io_readfile(DOKU_INC.'VERSION'));
56        $version['type'] = 'Release';
57    }elseif(is_dir(DOKU_INC.'.git')){
58        $version['type'] = 'Git';
59        $version['date'] = 'unknown';
60
61        $inventory = DOKU_INC.'.git/logs/HEAD';
62        if(is_file($inventory)){
63            $sz   = filesize($inventory);
64            $seek = max(0,$sz-2000); // read from back of the file
65            $fh   = fopen($inventory,'rb');
66            fseek($fh,$seek);
67            $chunk = fread($fh,2000);
68            fclose($fh);
69            $chunk = trim($chunk);
70            $chunk = array_pop(explode("\n",$chunk));   //last log line
71            $chunk = array_shift(explode("\t",$chunk)); //strip commit msg
72            $chunk = explode(" ",$chunk);
73            array_pop($chunk); //strip timezone
74            $date = date('Y-m-d',array_pop($chunk));
75            if($date) $version['date'] = $date;
76        }
77    }else{
78        $version['date'] = 'unknown';
79        $version['type'] = 'snapshot?';
80    }
81    return $version;
82}
83
84/**
85 * Return DokuWiki's version (as a string)
86 *
87 * @author Anika Henke <anika@selfthinker.org>
88 */
89function getVersion(){
90    $version = getVersionData();
91    return $version['type'].' '.$version['date'];
92}
93
94/**
95 * Run a few sanity checks
96 *
97 * @author Andreas Gohr <andi@splitbrain.org>
98 */
99function check(){
100    global $conf;
101    global $INFO;
102
103    if ($INFO['isadmin'] || $INFO['ismanager']){
104        msg('DokuWiki version: '.getVersion(),1);
105    }
106
107    if(version_compare(phpversion(),'5.1.2','<')){
108        msg('Your PHP version is too old ('.phpversion().' vs. 5.1.2+ needed)',-1);
109    }else{
110        msg('PHP version '.phpversion(),1);
111    }
112
113    $mem = (int) php_to_byte(ini_get('memory_limit'));
114    if($mem){
115        if($mem < 16777216){
116            msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
117        }elseif($mem < 20971520){
118            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);
119        }elseif($mem < 33554432){
120            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);
121        }else{
122            msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
123        }
124    }
125
126    if(is_writable($conf['changelog'])){
127        msg('Changelog is writable',1);
128    }else{
129        if (@file_exists($conf['changelog'])) {
130            msg('Changelog is not writable',-1);
131        }
132    }
133
134    if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
135        msg('Old changelog exists', 0);
136    }
137
138    if (@file_exists($conf['changelog'].'_failed')) {
139        msg('Importing old changelog failed', -1);
140    } else if (@file_exists($conf['changelog'].'_importing')) {
141        msg('Importing old changelog now.', 0);
142    } else if (@file_exists($conf['changelog'].'_import_ok')) {
143        msg('Old changelog imported', 1);
144        if (!plugin_isdisabled('importoldchangelog')) {
145            msg('Importoldchangelog plugin not disabled after import', -1);
146        }
147    }
148
149    if(is_writable($conf['datadir'])){
150        msg('Datadir is writable',1);
151    }else{
152        msg('Datadir is not writable',-1);
153    }
154
155    if(is_writable($conf['olddir'])){
156        msg('Attic is writable',1);
157    }else{
158        msg('Attic is not writable',-1);
159    }
160
161    if(is_writable($conf['mediadir'])){
162        msg('Mediadir is writable',1);
163    }else{
164        msg('Mediadir is not writable',-1);
165    }
166
167    if(is_writable($conf['cachedir'])){
168        msg('Cachedir is writable',1);
169    }else{
170        msg('Cachedir is not writable',-1);
171    }
172
173    if(is_writable($conf['lockdir'])){
174        msg('Lockdir is writable',1);
175    }else{
176        msg('Lockdir is not writable',-1);
177    }
178
179    if($conf['authtype'] == 'plain'){
180        if(is_writable(DOKU_CONF.'users.auth.php')){
181            msg('conf/users.auth.php is writable',1);
182        }else{
183            msg('conf/users.auth.php is not writable',0);
184        }
185    }
186
187    if(function_exists('mb_strpos')){
188        if(defined('UTF8_NOMBSTRING')){
189            msg('mb_string extension is available but will not be used',0);
190        }else{
191            msg('mb_string extension is available and will be used',1);
192            if(ini_get('mbstring.func_overload') != 0){
193                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
194            }
195        }
196    }else{
197        msg('mb_string extension not available - PHP only replacements will be used',0);
198    }
199
200    if($conf['allowdebug']){
201        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
202    }else{
203        msg('Debugging support is disabled',1);
204    }
205
206    if($INFO['userinfo']['name']){
207        msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0);
208        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
209    }else{
210        msg('You are currently not logged in',0);
211    }
212
213    msg('Your current permission for this page is '.$INFO['perm'],0);
214
215    if(is_writable($INFO['filepath'])){
216        msg('The current page is writable by the webserver',0);
217    }else{
218        msg('The current page is not writable by the webserver',0);
219    }
220
221    if($INFO['writable']){
222        msg('The current page is writable by you',0);
223    }else{
224        msg('The current page is not writable by you',0);
225    }
226
227    $check = wl('','',true).'data/_dummy';
228    $http = new DokuHTTPClient();
229    $http->timeout = 6;
230    $res = $http->get($check);
231    if(strpos($res,'data directory') !== false){
232        msg('It seems like the data directory is accessible from the web.
233                Make sure this directory is properly protected
234                (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
235    }elseif($http->status == 404 || $http->status == 403){
236        msg('The data directory seems to be properly protected',1);
237    }else{
238        msg('Failed to check if the data directory is accessible from the web.
239                Make sure this directory is properly protected
240                (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
241    }
242}
243
244/**
245 * print a message
246 *
247 * If HTTP headers were not sent yet the message is added
248 * to the global message array else it's printed directly
249 * using html_msgarea()
250 *
251 *
252 * Levels can be:
253 *
254 * -1 error
255 *  0 info
256 *  1 success
257 *
258 * @author Andreas Gohr <andi@splitbrain.org>
259 * @see    html_msgarea
260 */
261function msg($message,$lvl=0,$line='',$file=''){
262    global $MSG;
263    $errors[-1] = 'error';
264    $errors[0]  = 'info';
265    $errors[1]  = 'success';
266    $errors[2]  = 'notify';
267
268    if($line || $file) $message.=' ['.basename($file).':'.$line.']';
269
270    if(!isset($MSG)) $MSG = array();
271    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
272    if(headers_sent()){
273        if(function_exists('html_msgarea')){
274            html_msgarea();
275        }else{
276            print "ERROR($lvl) $message";
277        }
278        unset($GLOBALS['MSG']);
279    }
280}
281
282/**
283 * print debug messages
284 *
285 * little function to print the content of a var
286 *
287 * @author Andreas Gohr <andi@splitbrain.org>
288 */
289function dbg($msg,$hidden=false){
290    if($hidden){
291        echo "<!--\n";
292        print_r($msg);
293        echo "\n-->";
294    }else{
295        echo '<pre class="dbg">';
296        echo hsc(print_r($msg,true));
297        echo '</pre>';
298    }
299}
300
301/**
302 * Print info to a log file
303 *
304 * @author Andreas Gohr <andi@splitbrain.org>
305 */
306function dbglog($msg,$header=''){
307    global $conf;
308    if(is_object($msg) || is_array($msg)){
309        $msg = print_r($msg,true);
310    }
311
312    if($header) $msg = "$header\n$msg";
313
314    $file = $conf['cachedir'].'/debug.log';
315    $fh = fopen($file,'a');
316    if($fh){
317        fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
318        fclose($fh);
319    }
320}
321
322/**
323 * Print a reversed, prettyprinted backtrace
324 *
325 * @author Gary Owen <gary_owen@bigfoot.com>
326 */
327function dbg_backtrace(){
328    // Get backtrace
329    $backtrace = debug_backtrace();
330
331    // Unset call to debug_print_backtrace
332    array_shift($backtrace);
333
334    // Iterate backtrace
335    $calls = array();
336    $depth = count($backtrace) - 1;
337    foreach ($backtrace as $i => $call) {
338        $location = $call['file'] . ':' . $call['line'];
339        $function = (isset($call['class'])) ?
340            $call['class'] . $call['type'] . $call['function'] : $call['function'];
341
342        $params = array();
343        if (isset($call['args'])){
344            foreach($call['args'] as $arg){
345                if(is_object($arg)){
346                    $params[] = '[Object '.get_class($arg).']';
347                }elseif(is_array($arg)){
348                    $params[] = '[Array]';
349                }elseif(is_null($arg)){
350                    $param[] = '[NULL]';
351                }else{
352                    $params[] = (string) '"'.$arg.'"';
353                }
354            }
355        }
356        $params = implode(', ',$params);
357
358        $calls[$depth - $i] = sprintf('%s(%s) called at %s',
359                $function,
360                str_replace("\n", '\n', $params),
361                $location);
362    }
363    ksort($calls);
364
365    return implode("\n", $calls);
366}
367
368/**
369 * Remove all data from an array where the key seems to point to sensitive data
370 *
371 * This is used to remove passwords, mail addresses and similar data from the
372 * debug output
373 *
374 * @author Andreas Gohr <andi@splitbrain.org>
375 */
376function debug_guard(&$data){
377    foreach($data as $key => $value){
378        if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
379            $data[$key] = '***';
380            continue;
381        }
382        if(is_array($value)) debug_guard($data[$key]);
383    }
384}
385