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