xref: /dokuwiki/inc/infoutils.php (revision 37daf7b06c75c38fc76ee792fddc7e75242e0ed4)
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',realpath(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(is_writable(DOKU_CONF.'users.auth.php')){
146    msg('conf/users.auth.php is writable',1);
147  }else{
148    msg('conf/users.auth.php is not writable',0);
149  }
150
151  if(function_exists('mb_strpos')){
152    if(defined('UTF8_NOMBSTRING')){
153      msg('mb_string extension is available but will not be used',0);
154    }else{
155      msg('mb_string extension is available and will be used',1);
156    }
157  }else{
158    msg('mb_string extension not available - PHP only replacements will be used',0);
159  }
160
161  if($conf['allowdebug']){
162    msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
163  }else{
164    msg('Debugging support is disabled',1);
165  }
166
167  if($INFO['userinfo']['name']){
168    global $auth;
169    msg('You are currently logged in as '.$_SESSION[DOKU_COOKIE]['auth']['user'].' ('.$INFO['userinfo']['name'].')',0);
170
171    $info = $auth->getUserData($_SESSION[DOKU_COOKIE]['auth']['user']);
172    msg('You are part of the groups '.implode($info['grps'],', '),0);
173  }else{
174    msg('You are currently not logged in',0);
175  }
176
177  msg('Your current permission for this page is '.$INFO['perm'],0);
178
179  if(is_writable($INFO['filepath'])){
180    msg('The current page is writable by the webserver',0);
181  }else{
182    msg('The current page is not writable by the webserver',0);
183  }
184
185  if($INFO['writable']){
186    msg('The current page is writable by you',0);
187  }else{
188    msg('The current page is not writable by you',0);
189  }
190}
191
192/**
193 * print a message
194 *
195 * If HTTP headers were not sent yet the message is added
196 * to the global message array else it's printed directly
197 * using html_msgarea()
198 *
199 *
200 * Levels can be:
201 *
202 * -1 error
203 *  0 info
204 *  1 success
205 *
206 * @author Andreas Gohr <andi@splitbrain.org>
207 * @see    html_msgarea
208 */
209function msg($message,$lvl=0,$line='',$file=''){
210  global $MSG;
211  $errors[-1] = 'error';
212  $errors[0]  = 'info';
213  $errors[1]  = 'success';
214  $errors[2]  = 'notify';
215
216  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
217
218  if(!headers_sent()){
219    if(!isset($MSG)) $MSG = array();
220    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
221  }else{
222    $MSG = array();
223    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
224    if(function_exists('html_msgarea')){
225      html_msgarea();
226    }else{
227      print "ERROR($lvl) $message";
228    }
229  }
230}
231
232/**
233 * print debug messages
234 *
235 * little function to print the content of a var
236 *
237 * @author Andreas Gohr <andi@splitbrain.org>
238 */
239function dbg($msg,$hidden=false){
240  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
241  print_r($msg);
242  (!$hidden) ? print '</pre>' : print "\n-->";
243}
244
245/**
246 * Print info to a log file
247 *
248 * @author Andreas Gohr <andi@splitbrain.org>
249 */
250function dbglog($msg){
251  global $conf;
252  $file = $conf['cachedir'].'/debug.log';
253  $fh = fopen($file,'a');
254  if($fh){
255    fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
256    fclose($fh);
257  }
258}
259
260