xref: /dokuwiki/inc/infoutils.php (revision e53f9e72a48912e662dde20d7dceca74a022dcdf)
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    msg('DokuWiki version: '.getVersion(),1);
104
105    if(version_compare(phpversion(),'5.1.2','<')){
106        msg('Your PHP version is too old ('.phpversion().' vs. 5.1.2+ needed)',-1);
107    }else{
108        msg('PHP version '.phpversion(),1);
109    }
110
111    $mem = (int) php_to_byte(ini_get('memory_limit'));
112    if($mem){
113        if($mem < 16777216){
114            msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
115        }elseif($mem < 20971520){
116            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);
117        }elseif($mem < 33554432){
118            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);
119        }else{
120            msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
121        }
122    }
123
124    if(is_writable($conf['changelog'])){
125        msg('Changelog is writable',1);
126    }else{
127        if (@file_exists($conf['changelog'])) {
128            msg('Changelog is not writable',-1);
129        }
130    }
131
132    if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
133        msg('Old changelog exists', 0);
134    }
135
136    if (@file_exists($conf['changelog'].'_failed')) {
137        msg('Importing old changelog failed', -1);
138    } else if (@file_exists($conf['changelog'].'_importing')) {
139        msg('Importing old changelog now.', 0);
140    } else if (@file_exists($conf['changelog'].'_import_ok')) {
141        msg('Old changelog imported', 1);
142        if (!plugin_isdisabled('importoldchangelog')) {
143            msg('Importoldchangelog plugin not disabled after import', -1);
144        }
145    }
146
147    if(is_writable($conf['datadir'])){
148        msg('Datadir is writable',1);
149    }else{
150        msg('Datadir is not writable',-1);
151    }
152
153    if(is_writable($conf['olddir'])){
154        msg('Attic is writable',1);
155    }else{
156        msg('Attic is not writable',-1);
157    }
158
159    if(is_writable($conf['mediadir'])){
160        msg('Mediadir is writable',1);
161    }else{
162        msg('Mediadir is not writable',-1);
163    }
164
165    if(is_writable($conf['cachedir'])){
166        msg('Cachedir is writable',1);
167    }else{
168        msg('Cachedir is not writable',-1);
169    }
170
171    if(is_writable($conf['lockdir'])){
172        msg('Lockdir is writable',1);
173    }else{
174        msg('Lockdir is not writable',-1);
175    }
176
177    if($conf['authtype'] == 'plain'){
178        if(is_writable(DOKU_CONF.'users.auth.php')){
179            msg('conf/users.auth.php is writable',1);
180        }else{
181            msg('conf/users.auth.php is not writable',0);
182        }
183    }
184
185    if(function_exists('mb_strpos')){
186        if(defined('UTF8_NOMBSTRING')){
187            msg('mb_string extension is available but will not be used',0);
188        }else{
189            msg('mb_string extension is available and will be used',1);
190            if(ini_get('mbstring.func_overload') != 0){
191                msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
192            }
193        }
194    }else{
195        msg('mb_string extension not available - PHP only replacements will be used',0);
196    }
197
198    if($conf['allowdebug']){
199        msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
200    }else{
201        msg('Debugging support is disabled',1);
202    }
203
204    if($INFO['userinfo']['name']){
205        msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0);
206        msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
207    }else{
208        msg('You are currently not logged in',0);
209    }
210
211    msg('Your current permission for this page is '.$INFO['perm'],0);
212
213    if(is_writable($INFO['filepath'])){
214        msg('The current page is writable by the webserver',0);
215    }else{
216        msg('The current page is not writable by the webserver',0);
217    }
218
219    if($INFO['writable']){
220        msg('The current page is writable by you',0);
221    }else{
222        msg('The current page is not writable by you',0);
223    }
224
225    $check = wl('','',true).'data/_dummy';
226    $http = new DokuHTTPClient();
227    $http->timeout = 6;
228    $res = $http->get($check);
229    if(strpos($res,'data directory') !== false){
230        msg('It seems like the data directory is accessible from the web.
231                Make sure this directory is properly protected
232                (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
233    }elseif($http->status == 404 || $http->status == 403){
234        msg('The data directory seems to be properly protected',1);
235    }else{
236        msg('Failed to check if the data directory is accessible from the web.
237                Make sure this directory is properly protected
238                (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
239    }
240}
241
242/**
243 * print a message
244 *
245 * If HTTP headers were not sent yet the message is added
246 * to the global message array else it's printed directly
247 * using html_msgarea()
248 *
249 *
250 * Levels can be:
251 *
252 * -1 error
253 *  0 info
254 *  1 success
255 *
256 * @author Andreas Gohr <andi@splitbrain.org>
257 * @see    html_msgarea
258 */
259function msg($message,$lvl=0,$line='',$file=''){
260    global $MSG;
261    $errors[-1] = 'error';
262    $errors[0]  = 'info';
263    $errors[1]  = 'success';
264    $errors[2]  = 'notify';
265
266    if($line || $file) $message.=' ['.basename($file).':'.$line.']';
267
268    if(!headers_sent()){
269        if(!isset($MSG)) $MSG = array();
270        $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
271    }else{
272        $MSG = array();
273        $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
274        if(function_exists('html_msgarea')){
275            html_msgarea();
276        }else{
277            print "ERROR($lvl) $message";
278        }
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