3 * Information and debugging functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
8 if(!defined('DOKU_INC')) die('meh.');
9 if(!defined('DOKU_MESSAGEURL')) define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
10 require_once(DOKU_INC
.'inc/HTTPClient.php');
13 * Check for new messages from upstream
15 * @author Andreas Gohr <andi@splitbrain.org>
17 function checkUpdateMessages(){
20 if(!$conf['updatecheck']) return;
21 if($conf['useacl'] && !$INFO['ismanager']) return;
23 $cf = $conf['cachedir'].'/messages.txt';
24 $lm = @filemtime
($cf);
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();
32 $data = $http->get(DOKU_MESSAGEURL
.$num);
33 io_saveFile($cf,$data);
35 $data = io_readFile($cf);
38 // show messages through the usual message mechanism
39 $msgs = explode("\n%\n",$data);
40 foreach($msgs as $msg){
47 * Return DokuWiki's version (split up in date and type)
49 * @author Andreas Gohr <andi@splitbrain.org>
51 function getVersionData(){
53 //import version string
54 if(@file_exists
(DOKU_INC
.'VERSION')){
56 $version['date'] = trim(io_readfile(DOKU_INC
.'VERSION'));
57 $version['type'] = 'Release';
59 }elseif(is_dir(DOKU_INC
.'_darcs')){
60 if(is_file(DOKU_INC
.'_darcs/inventory')){
61 $inventory = DOKU_INC
.'_darcs/inventory';
62 }elseif(is_file(DOKU_INC
.'_darcs/hashed_inventory')){
63 $inventory = DOKU_INC
.'_darcs/hashed_inventory';
65 $version['date'] = 'unknown';
66 $version['type'] = 'Darcs';
70 //darcs checkout - read last 2000 bytes of inventory
71 $sz = filesize($inventory);
72 $seek = max(0,$sz-2000);
73 $fh = fopen($inventory,'rb');
75 $chunk = fread($fh,2000);
78 preg_match_all('#\*\*(\d{4})(\d{2})(\d{2})\d{6}(?:\]|$)#m', $chunk, $matches,
80 $version['date'] = implode('-', array_slice(array_pop($matches), 1));
81 $version['type'] = 'Darcs';
84 $version['date'] = 'unknown';
85 $version['type'] = 'snapshot?';
91 * Return DokuWiki's version (as a string)
93 * @author Anika Henke <anika@selfthinker.org>
95 function getVersion(){
96 $version = getVersionData();
97 return $version['type'].' '.$version['date'];
101 * Run a few sanity checks
103 * @author Andreas Gohr <andi@splitbrain.org>
109 msg('DokuWiki version: '.getVersion(),1);
111 if(version_compare(phpversion(),'5.1.2','<')){
112 msg('Your PHP version is too old ('.phpversion().' vs. 5.1.2+ needed)',-1);
114 msg('PHP version '.phpversion(),1);
117 $mem = (int) php_to_byte(ini_get('memory_limit'));
120 msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
121 }elseif($mem < 20971520){
122 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);
123 }elseif($mem < 33554432){
124 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);
126 msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
130 if(is_writable($conf['changelog'])){
131 msg('Changelog is writable',1);
133 if (@file_exists
($conf['changelog'])) {
134 msg('Changelog is not writable',-1);
138 if (isset($conf['changelog_old']) && @file_exists
($conf['changelog_old'])) {
139 msg('Old changelog exists', 0);
142 if (@file_exists
($conf['changelog'].'_failed')) {
143 msg('Importing old changelog failed', -1);
144 } else if (@file_exists
($conf['changelog'].'_importing')) {
145 msg('Importing old changelog now.', 0);
146 } else if (@file_exists
($conf['changelog'].'_import_ok')) {
147 msg('Old changelog imported', 1);
148 if (!plugin_isdisabled('importoldchangelog')) {
149 msg('Importoldchangelog plugin not disabled after import', -1);
153 if(is_writable($conf['datadir'])){
154 msg('Datadir is writable',1);
156 msg('Datadir is not writable',-1);
159 if(is_writable($conf['olddir'])){
160 msg('Attic is writable',1);
162 msg('Attic is not writable',-1);
165 if(is_writable($conf['mediadir'])){
166 msg('Mediadir is writable',1);
168 msg('Mediadir is not writable',-1);
171 if(is_writable($conf['cachedir'])){
172 msg('Cachedir is writable',1);
174 msg('Cachedir is not writable',-1);
177 if(is_writable($conf['lockdir'])){
178 msg('Lockdir is writable',1);
180 msg('Lockdir is not writable',-1);
183 if($conf['authtype'] == 'plain'){
184 if(is_writable(DOKU_CONF
.'users.auth.php')){
185 msg('conf/users.auth.php is writable',1);
187 msg('conf/users.auth.php is not writable',0);
191 if(function_exists('mb_strpos')){
192 if(defined('UTF8_NOMBSTRING')){
193 msg('mb_string extension is available but will not be used',0);
195 msg('mb_string extension is available and will be used',1);
196 if(ini_get('mbstring.func_overload') != 0){
197 msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
201 msg('mb_string extension not available - PHP only replacements will be used',0);
204 if($conf['allowdebug']){
205 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
207 msg('Debugging support is disabled',1);
210 if($INFO['userinfo']['name']){
211 msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0);
212 msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
214 msg('You are currently not logged in',0);
217 msg('Your current permission for this page is '.$INFO['perm'],0);
219 if(is_writable($INFO['filepath'])){
220 msg('The current page is writable by the webserver',0);
222 msg('The current page is not writable by the webserver',0);
225 if($INFO['writable']){
226 msg('The current page is writable by you',0);
228 msg('The current page is not writable by you',0);
231 require_once(DOKU_INC
.'inc/HTTPClient.php');
232 $check = wl('','',true).'data/_dummy';
233 $http = new DokuHTTPClient();
235 $res = $http->get($check);
236 if(strpos($res,'data directory') !== false){
237 msg('It seems like the data directory is accessible from the web.
238 Make sure this directory is properly protected
239 (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
240 }elseif($http->status
== 404 ||
$http->status
== 403){
241 msg('The data directory seems to be properly protected',1);
243 msg('Failed to check if the data directory is accessible from the web.
244 Make sure this directory is properly protected
245 (See <a href="http://www.dokuwiki.org/security">security</a>)',-1);
252 * If HTTP headers were not sent yet the message is added
253 * to the global message array else it's printed directly
254 * using html_msgarea()
263 * @author Andreas Gohr <andi@splitbrain.org>
266 function msg($message,$lvl=0,$line='',$file=''){
268 $errors[-1] = 'error';
270 $errors[1] = 'success';
271 $errors[2] = 'notify';
273 if($line ||
$file) $message.=' ['.basename($file).':'.$line.']';
276 if(!isset($MSG)) $MSG = array();
277 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
280 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
281 if(function_exists('html_msgarea')){
284 print "ERROR($lvl) $message";
290 * print debug messages
292 * little function to print the content of a var
294 * @author Andreas Gohr <andi@splitbrain.org>
296 function dbg($msg,$hidden=false){
297 (!$hidden) ?
print '<pre class="dbg">' : print "<!--\n";
299 (!$hidden) ?
print '</pre>' : print "\n-->";
303 * Print info to a log file
305 * @author Andreas Gohr <andi@splitbrain.org>
307 function dbglog($msg,$header=''){
309 if(is_object($msg) ||
is_array($msg)){
310 $msg = print_r($msg,true);
313 if($header) $msg = "$header\n$msg";
315 $file = $conf['cachedir'].'/debug.log';
316 $fh = fopen($file,'a');
318 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
324 * Print a reversed, prettyprinted backtrace
326 * @author Gary Owen <gary_owen@bigfoot.com>
328 function dbg_backtrace(){
330 $backtrace = debug_backtrace();
332 // Unset call to debug_print_backtrace
333 array_shift($backtrace);
337 $depth = count($backtrace) - 1;
338 foreach ($backtrace as $i => $call) {
339 $location = $call['file'] . ':' . $call['line'];
340 $function = (isset($call['class'])) ?
341 $call['class'] . $call['type'] . $call['function'] : $call['function'];
344 if (isset($call['args'])){
345 foreach($call['args'] as $arg){
347 $params[] = '[Object '.get_class($arg).']';
348 }elseif(is_array($arg)){
349 $params[] = '[Array]';
350 }elseif(is_null($arg)){
353 $params[] = (string) '"'.$arg.'"';
357 $params = implode(', ',$params);
359 $calls[$depth - $i] = sprintf('%s(%s) called at %s',
361 str_replace("\n", '\n', $params),
366 return implode("\n", $calls);
370 * Remove all data from an array where the key seems to point to sensitive data
372 * This is used to remove passwords, mail addresses and similar data from the
375 * @author Andreas Gohr <andi@splitbrain.org>
377 function debug_guard(&$data){
378 foreach($data as $key => $value){
379 if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
383 if(is_array($value)) debug_guard($data[$key]);