xref: /dokuwiki/inc/infoutils.php (revision 9a3b7d9f166ec2227b91614228c308f50d3e259f)
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')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
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 DokuWikis version
48 *
49 * @author Andreas Gohr <andi@splitbrain.org>
50 */
51function getVersion(){
52  //import version string
53  if(@file_exists(DOKU_INC.'VERSION')){
54    //official release
55    return 'Release '.trim(io_readfile(DOKU_INC.'VERSION'));
56  }elseif(is_dir(DOKU_INC.'_darcs')){
57    //darcs checkout - read last 2000 bytes of inventory
58    $sz   = filesize(DOKU_INC.'_darcs/inventory');
59    $seek = max(0,$sz-2000);
60    $fh   = fopen(DOKU_INC.'_darcs/inventory','rb');
61    fseek($fh,$seek);
62    $chunk = fread($fh,2000);
63    fclose($fh);
64    $inv = preg_grep('#\*\*\d{14}[\]$]#',explode("\n",$chunk));
65    $cur = array_pop($inv);
66    preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches);
67    return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3];
68  }else{
69    return 'snapshot?';
70  }
71}
72
73/**
74 * Run a few sanity checks
75 *
76 * @author Andreas Gohr <andi@splitbrain.org>
77 */
78function check(){
79  global $conf;
80  global $INFO;
81
82  msg('DokuWiki version: '.getVersion(),1);
83
84  if(version_compare(phpversion(),'4.3.3','<')){
85    msg('Your PHP version is too old ('.phpversion().' vs. 4.3.3+ recommended)',-1);
86  }elseif(version_compare(phpversion(),'4.3.10','<')){
87    msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0);
88  }else{
89    msg('PHP version '.phpversion(),1);
90  }
91
92  if(is_writable($conf['changelog'])){
93    msg('Changelog is writable',1);
94  }else{
95    if (@file_exists($conf['changelog'])) {
96      msg('Changelog is not writable',-1);
97    }
98  }
99
100  if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
101    msg('Old changelog exists', 0);
102  }
103
104  if (@file_exists($conf['changelog'].'_failed')) {
105    msg('Importing old changelog failed', -1);
106  } else if (@file_exists($conf['changelog'].'_importing')) {
107    msg('Importing old changelog now.', 0);
108  } else if (@file_exists($conf['changelog'].'_import_ok')) {
109    msg('Old changelog imported', 1);
110    if (!plugin_isdisabled('importoldchangelog')) {
111      msg('Importoldchangelog plugin not disabled after import', -1);
112    }
113  }
114
115  if(is_writable($conf['datadir'])){
116    msg('Datadir is writable',1);
117  }else{
118    msg('Datadir is not writable',-1);
119  }
120
121  if(is_writable($conf['olddir'])){
122    msg('Attic is writable',1);
123  }else{
124    msg('Attic is not writable',-1);
125  }
126
127  if(is_writable($conf['mediadir'])){
128    msg('Mediadir is writable',1);
129  }else{
130    msg('Mediadir is not writable',-1);
131  }
132
133  if(is_writable($conf['cachedir'])){
134    msg('Cachedir is writable',1);
135  }else{
136    msg('Cachedir is not writable',-1);
137  }
138
139  if(is_writable($conf['lockdir'])){
140    msg('Lockdir is writable',1);
141  }else{
142    msg('Lockdir is not writable',-1);
143  }
144
145  if($conf['authtype'] == 'plain'){
146    if(is_writable(DOKU_CONF.'users.auth.php')){
147      msg('conf/users.auth.php is writable',1);
148    }else{
149      msg('conf/users.auth.php is not writable',0);
150    }
151  }
152
153  if(function_exists('mb_strpos')){
154    if(defined('UTF8_NOMBSTRING')){
155      msg('mb_string extension is available but will not be used',0);
156    }else{
157      msg('mb_string extension is available and will be used',1);
158    }
159  }else{
160    msg('mb_string extension not available - PHP only replacements will be used',0);
161  }
162
163  if($conf['allowdebug']){
164    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
165  }else{
166    msg('Debugging support is disabled',1);
167  }
168
169  if($INFO['userinfo']['name']){
170    msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0);
171    msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
172  }else{
173    msg('You are currently not logged in',0);
174  }
175
176  msg('Your current permission for this page is '.$INFO['perm'],0);
177
178  if(is_writable($INFO['filepath'])){
179    msg('The current page is writable by the webserver',0);
180  }else{
181    msg('The current page is not writable by the webserver',0);
182  }
183
184  if($INFO['writable']){
185    msg('The current page is writable by you',0);
186  }else{
187    msg('The current page is not writable by you',0);
188  }
189}
190
191/**
192 * print a message
193 *
194 * If HTTP headers were not sent yet the message is added
195 * to the global message array else it's printed directly
196 * using html_msgarea()
197 *
198 *
199 * Levels can be:
200 *
201 * -1 error
202 *  0 info
203 *  1 success
204 *
205 * @author Andreas Gohr <andi@splitbrain.org>
206 * @see    html_msgarea
207 */
208function msg($message,$lvl=0,$line='',$file=''){
209  global $MSG;
210  $errors[-1] = 'error';
211  $errors[0]  = 'info';
212  $errors[1]  = 'success';
213  $errors[2]  = 'notify';
214
215  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
216
217  if(!headers_sent()){
218    if(!isset($MSG)) $MSG = array();
219    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
220  }else{
221    $MSG = array();
222    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
223    if(function_exists('html_msgarea')){
224      html_msgarea();
225    }else{
226      print "ERROR($lvl) $message";
227    }
228  }
229}
230
231/**
232 * print debug messages
233 *
234 * little function to print the content of a var
235 *
236 * @author Andreas Gohr <andi@splitbrain.org>
237 */
238function dbg($msg,$hidden=false){
239  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
240  print_r($msg);
241  (!$hidden) ? print '</pre>' : print "\n-->";
242}
243
244/**
245 * Print info to a log file
246 *
247 * @author Andreas Gohr <andi@splitbrain.org>
248 */
249function dbglog($msg){
250  global $conf;
251  $file = $conf['cachedir'].'/debug.log';
252  $fh = fopen($file,'a');
253  if($fh){
254    fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
255    fclose($fh);
256  }
257}
258
259/**
260 * Print a reversed, prettyprinted backtrace
261 *
262 * @author Gary Owen <gary_owen@bigfoot.com>
263 */
264function dbg_backtrace(){
265  // Get backtrace
266  $backtrace = debug_backtrace();
267
268  // Unset call to debug_print_backtrace
269  array_shift($backtrace);
270
271  // Iterate backtrace
272  $calls = array();
273  $depth = count($backtrace) - 1;
274  foreach ($backtrace as $i => $call) {
275    $location = $call['file'] . ':' . $call['line'];
276    $function = (isset($call['class'])) ?
277    $call['class'] . $call['type'] . $call['function'] : $call['function'];
278
279    $params = array();
280    if (isset($call['args'])){
281        foreach($call['args'] as $arg){
282            if(is_object($arg)){
283                $params[] = '[Object '.get_class($arg).']';
284            }elseif(is_array($arg)){
285                $params[] = '[Array]';
286            }elseif(is_null($arg)){
287                $param[] = '[NULL]';
288            }else{
289                $params[] = (string) '"'.$arg.'"';
290            }
291        }
292    }
293    $params = implode(', ',$params);
294
295    $calls[$depth - $i] = sprintf('%s(%s) called at %s',
296                          $function,
297                          str_replace("\n", '\n', $params),
298                          $location);
299  }
300  ksort($calls);
301
302  return implode("\n", $calls);
303}
304
305