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 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(),'5.1.2','<')){ 85 msg('Your PHP version is too old ('.phpversion().' vs. 5.1.2+ needed)',-1); 86 }else{ 87 msg('PHP version '.phpversion(),1); 88 } 89 90 $mem = (int) php_to_byte(ini_get('memory_limit')); 91 if($mem){ 92 if($mem < 16777216){ 93 msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1); 94 }elseif($mem < 20971520){ 95 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); 96 }elseif($mem < 33554432){ 97 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); 98 }else{ 99 msg('More than 32MB RAM ('.$mem.' bytes) available.',1); 100 } 101 } 102 103 if(is_writable($conf['changelog'])){ 104 msg('Changelog is writable',1); 105 }else{ 106 if (@file_exists($conf['changelog'])) { 107 msg('Changelog is not writable',-1); 108 } 109 } 110 111 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 112 msg('Old changelog exists', 0); 113 } 114 115 if (@file_exists($conf['changelog'].'_failed')) { 116 msg('Importing old changelog failed', -1); 117 } else if (@file_exists($conf['changelog'].'_importing')) { 118 msg('Importing old changelog now.', 0); 119 } else if (@file_exists($conf['changelog'].'_import_ok')) { 120 msg('Old changelog imported', 1); 121 if (!plugin_isdisabled('importoldchangelog')) { 122 msg('Importoldchangelog plugin not disabled after import', -1); 123 } 124 } 125 126 if(is_writable($conf['datadir'])){ 127 msg('Datadir is writable',1); 128 }else{ 129 msg('Datadir is not writable',-1); 130 } 131 132 if(is_writable($conf['olddir'])){ 133 msg('Attic is writable',1); 134 }else{ 135 msg('Attic is not writable',-1); 136 } 137 138 if(is_writable($conf['mediadir'])){ 139 msg('Mediadir is writable',1); 140 }else{ 141 msg('Mediadir is not writable',-1); 142 } 143 144 if(is_writable($conf['cachedir'])){ 145 msg('Cachedir is writable',1); 146 }else{ 147 msg('Cachedir is not writable',-1); 148 } 149 150 if(is_writable($conf['lockdir'])){ 151 msg('Lockdir is writable',1); 152 }else{ 153 msg('Lockdir is not writable',-1); 154 } 155 156 if($conf['authtype'] == 'plain'){ 157 if(is_writable(DOKU_CONF.'users.auth.php')){ 158 msg('conf/users.auth.php is writable',1); 159 }else{ 160 msg('conf/users.auth.php is not writable',0); 161 } 162 } 163 164 if(function_exists('mb_strpos')){ 165 if(defined('UTF8_NOMBSTRING')){ 166 msg('mb_string extension is available but will not be used',0); 167 }else{ 168 msg('mb_string extension is available and will be used',1); 169 if(ini_get('mbstring.func_overload') != 0){ 170 msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1); 171 } 172 } 173 }else{ 174 msg('mb_string extension not available - PHP only replacements will be used',0); 175 } 176 177 if($conf['allowdebug']){ 178 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 179 }else{ 180 msg('Debugging support is disabled',1); 181 } 182 183 if($INFO['userinfo']['name']){ 184 msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0); 185 msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0); 186 }else{ 187 msg('You are currently not logged in',0); 188 } 189 190 msg('Your current permission for this page is '.$INFO['perm'],0); 191 192 if(is_writable($INFO['filepath'])){ 193 msg('The current page is writable by the webserver',0); 194 }else{ 195 msg('The current page is not writable by the webserver',0); 196 } 197 198 if($INFO['writable']){ 199 msg('The current page is writable by you',0); 200 }else{ 201 msg('The current page is not writable by you',0); 202 } 203 204 require_once(DOKU_INC.'inc/HTTPClient.php'); 205 $check = wl('','',true).'data/_dummy'; 206 $http = new DokuHTTPClient(); 207 $http->timeout = 6; 208 $res = $http->get($check); 209 if(strpos($res,'data directory') !== false){ 210 msg('It seems like the data directory is accessible from the web. 211 Make sure this directory is properly protected 212 (See <a href="http://www.dokuwiki.org/security">security</a>)',-1); 213 }elseif($http->status == 404 || $http->status == 403){ 214 msg('The data directory seems to be properly protected',1); 215 }else{ 216 msg('Failed to check if the data directory is accessible from the web. 217 Make sure this directory is properly protected 218 (See <a href="http://www.dokuwiki.org/security">security</a>)',-1); 219 } 220} 221 222/** 223 * print a message 224 * 225 * If HTTP headers were not sent yet the message is added 226 * to the global message array else it's printed directly 227 * using html_msgarea() 228 * 229 * 230 * Levels can be: 231 * 232 * -1 error 233 * 0 info 234 * 1 success 235 * 236 * @author Andreas Gohr <andi@splitbrain.org> 237 * @see html_msgarea 238 */ 239function msg($message,$lvl=0,$line='',$file=''){ 240 global $MSG; 241 $errors[-1] = 'error'; 242 $errors[0] = 'info'; 243 $errors[1] = 'success'; 244 $errors[2] = 'notify'; 245 246 if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 247 248 if(!headers_sent()){ 249 if(!isset($MSG)) $MSG = array(); 250 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 251 }else{ 252 $MSG = array(); 253 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 254 if(function_exists('html_msgarea')){ 255 html_msgarea(); 256 }else{ 257 print "ERROR($lvl) $message"; 258 } 259 } 260} 261 262/** 263 * print debug messages 264 * 265 * little function to print the content of a var 266 * 267 * @author Andreas Gohr <andi@splitbrain.org> 268 */ 269function dbg($msg,$hidden=false){ 270 (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 271 print_r($msg); 272 (!$hidden) ? print '</pre>' : print "\n-->"; 273} 274 275/** 276 * Print info to a log file 277 * 278 * @author Andreas Gohr <andi@splitbrain.org> 279 */ 280function dbglog($msg,$header=''){ 281 global $conf; 282 if(is_object($msg) || is_array($msg)){ 283 $msg = print_r($msg,true); 284 } 285 286 if($header) $msg = "$header\n$msg"; 287 288 $file = $conf['cachedir'].'/debug.log'; 289 $fh = fopen($file,'a'); 290 if($fh){ 291 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 292 fclose($fh); 293 } 294} 295 296/** 297 * Print a reversed, prettyprinted backtrace 298 * 299 * @author Gary Owen <gary_owen@bigfoot.com> 300 */ 301function dbg_backtrace(){ 302 // Get backtrace 303 $backtrace = debug_backtrace(); 304 305 // Unset call to debug_print_backtrace 306 array_shift($backtrace); 307 308 // Iterate backtrace 309 $calls = array(); 310 $depth = count($backtrace) - 1; 311 foreach ($backtrace as $i => $call) { 312 $location = $call['file'] . ':' . $call['line']; 313 $function = (isset($call['class'])) ? 314 $call['class'] . $call['type'] . $call['function'] : $call['function']; 315 316 $params = array(); 317 if (isset($call['args'])){ 318 foreach($call['args'] as $arg){ 319 if(is_object($arg)){ 320 $params[] = '[Object '.get_class($arg).']'; 321 }elseif(is_array($arg)){ 322 $params[] = '[Array]'; 323 }elseif(is_null($arg)){ 324 $param[] = '[NULL]'; 325 }else{ 326 $params[] = (string) '"'.$arg.'"'; 327 } 328 } 329 } 330 $params = implode(', ',$params); 331 332 $calls[$depth - $i] = sprintf('%s(%s) called at %s', 333 $function, 334 str_replace("\n", '\n', $params), 335 $location); 336 } 337 ksort($calls); 338 339 return implode("\n", $calls); 340} 341 342/** 343 * Remove all data from an array where the key seems to point to sensitive data 344 * 345 * This is used to remove passwords, mail addresses and similar data from the 346 * debug output 347 * 348 * @author Andreas Gohr <andi@splitbrain.org> 349 */ 350function debug_guard(&$data){ 351 foreach($data as $key => $value){ 352 if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){ 353 $data[$key] = '***'; 354 continue; 355 } 356 if(is_array($value)) debug_guard($data[$key]); 357 } 358} 359