xref: /dokuwiki/inc/infoutils.php (revision 168f9feebfac547ac832cdc2561a85158f9bed2d)
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['perm'] < AUTH_ADMIN) 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('VERSION')){
54    //official release
55    return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION'));
56  }elseif(is_dir('_darcs')){
57    //darcs checkout - read last 2000 bytes of inventory
58    $sz   = filesize('_darcs/inventory');
59    $seek = max(0,$sz-2000);
60    $fh   = fopen('_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  msg('Your current permission for this page is '.$INFO['perm'],0);
168
169  if(is_writable($INFO['filepath'])){
170    msg('The current page is writable by the webserver',0);
171  }else{
172    msg('The current page is not writable by the webserver',0);
173  }
174
175  if($INFO['writable']){
176    msg('The current page is writable by you',0);
177  }else{
178    msg('The current page is not writable by you',0);
179  }
180}
181
182/**
183 * print a message
184 *
185 * If HTTP headers were not sent yet the message is added
186 * to the global message array else it's printed directly
187 * using html_msgarea()
188 *
189 *
190 * Levels can be:
191 *
192 * -1 error
193 *  0 info
194 *  1 success
195 *
196 * @author Andreas Gohr <andi@splitbrain.org>
197 * @see    html_msgarea
198 */
199function msg($message,$lvl=0,$line='',$file=''){
200  global $MSG;
201  $errors[-1] = 'error';
202  $errors[0]  = 'info';
203  $errors[1]  = 'success';
204  $errors[2]  = 'notify';
205
206  if($line || $file) $message.=' ['.basename($file).':'.$line.']';
207
208  if(!headers_sent()){
209    if(!isset($MSG)) $MSG = array();
210    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
211  }else{
212    $MSG = array();
213    $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
214    if(function_exists('html_msgarea')){
215      html_msgarea();
216    }else{
217      print "ERROR($lvl) $message";
218    }
219  }
220}
221
222/**
223 * print debug messages
224 *
225 * little function to print the content of a var
226 *
227 * @author Andreas Gohr <andi@splitbrain.org>
228 */
229function dbg($msg,$hidden=false){
230  (!$hidden) ? print '<pre class="dbg">' : print "<!--\n";
231  print_r($msg);
232  (!$hidden) ? print '</pre>' : print "\n-->";
233}
234
235/**
236 * Print info to a log file
237 *
238 * @author Andreas Gohr <andi@splitbrain.org>
239 */
240function dbglog($msg){
241  global $conf;
242  $file = $conf['cachedir'].'/debug.log';
243  $fh = fopen($file,'a');
244  if($fh){
245    fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
246    fclose($fh);
247  }
248}
249
250