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/'); 10 11/** 12 * Check for new messages from upstream 13 * 14 * @author Andreas Gohr <andi@splitbrain.org> 15 */ 16function checkUpdateMessages(){ 17 global $conf; 18 global $INFO; 19 global $updateVersion; 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_INC.'doku.php')){ 28 $http = new DokuHTTPClient(); 29 $http->timeout = 8; 30 $data = $http->get(DOKU_MESSAGEURL.$updateVersion); 31 io_saveFile($cf,$data); 32 }else{ 33 $data = io_readFile($cf); 34 } 35 36 // show messages through the usual message mechanism 37 $msgs = explode("\n%\n",$data); 38 foreach($msgs as $msg){ 39 if($msg) msg($msg,2); 40 } 41} 42 43 44/** 45 * Return DokuWiki's version (split up in date and type) 46 * 47 * @author Andreas Gohr <andi@splitbrain.org> 48 */ 49function getVersionData(){ 50 $version = array(); 51 //import version string 52 if(@file_exists(DOKU_INC.'VERSION')){ 53 //official release 54 $version['date'] = trim(io_readfile(DOKU_INC.'VERSION')); 55 $version['type'] = 'Release'; 56 }elseif(is_dir(DOKU_INC.'.git')){ 57 $version['type'] = 'Git'; 58 $version['date'] = 'unknown'; 59 60 $inventory = DOKU_INC.'.git/logs/HEAD'; 61 if(is_file($inventory)){ 62 $sz = filesize($inventory); 63 $seek = max(0,$sz-2000); // read from back of the file 64 $fh = fopen($inventory,'rb'); 65 fseek($fh,$seek); 66 $chunk = fread($fh,2000); 67 fclose($fh); 68 $chunk = trim($chunk); 69 $chunk = array_pop(explode("\n",$chunk)); //last log line 70 $chunk = array_shift(explode("\t",$chunk)); //strip commit msg 71 $chunk = explode(" ",$chunk); 72 array_pop($chunk); //strip timezone 73 $date = date('Y-m-d',array_pop($chunk)); 74 if($date) $version['date'] = $date; 75 } 76 }else{ 77 $version['date'] = 'unknown'; 78 $version['type'] = 'snapshot?'; 79 } 80 return $version; 81} 82 83/** 84 * Return DokuWiki's version (as a string) 85 * 86 * @author Anika Henke <anika@selfthinker.org> 87 */ 88function getVersion(){ 89 $version = getVersionData(); 90 return $version['type'].' '.$version['date']; 91} 92 93/** 94 * Run a few sanity checks 95 * 96 * @author Andreas Gohr <andi@splitbrain.org> 97 */ 98function check(){ 99 global $conf; 100 global $INFO; 101 102 if ($INFO['isadmin'] || $INFO['ismanager']){ 103 msg('DokuWiki version: '.getVersion(),1); 104 } 105 106 if(version_compare(phpversion(),'5.1.2','<')){ 107 msg('Your PHP version is too old ('.phpversion().' vs. 5.1.2+ needed)',-1); 108 }else{ 109 msg('PHP version '.phpversion(),1); 110 } 111 112 $mem = (int) php_to_byte(ini_get('memory_limit')); 113 if($mem){ 114 if($mem < 16777216){ 115 msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1); 116 }elseif($mem < 20971520){ 117 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); 118 }elseif($mem < 33554432){ 119 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); 120 }else{ 121 msg('More than 32MB RAM ('.$mem.' bytes) available.',1); 122 } 123 } 124 125 if(is_writable($conf['changelog'])){ 126 msg('Changelog is writable',1); 127 }else{ 128 if (@file_exists($conf['changelog'])) { 129 msg('Changelog is not writable',-1); 130 } 131 } 132 133 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 134 msg('Old changelog exists', 0); 135 } 136 137 if (@file_exists($conf['changelog'].'_failed')) { 138 msg('Importing old changelog failed', -1); 139 } else if (@file_exists($conf['changelog'].'_importing')) { 140 msg('Importing old changelog now.', 0); 141 } else if (@file_exists($conf['changelog'].'_import_ok')) { 142 msg('Old changelog imported', 1); 143 if (!plugin_isdisabled('importoldchangelog')) { 144 msg('Importoldchangelog plugin not disabled after import', -1); 145 } 146 } 147 148 if(is_writable($conf['datadir'])){ 149 msg('Datadir is writable',1); 150 }else{ 151 msg('Datadir is not writable',-1); 152 } 153 154 if(is_writable($conf['olddir'])){ 155 msg('Attic is writable',1); 156 }else{ 157 msg('Attic is not writable',-1); 158 } 159 160 if(is_writable($conf['mediadir'])){ 161 msg('Mediadir is writable',1); 162 }else{ 163 msg('Mediadir is not writable',-1); 164 } 165 166 if(is_writable($conf['cachedir'])){ 167 msg('Cachedir is writable',1); 168 }else{ 169 msg('Cachedir is not writable',-1); 170 } 171 172 if(is_writable($conf['lockdir'])){ 173 msg('Lockdir is writable',1); 174 }else{ 175 msg('Lockdir is not writable',-1); 176 } 177 178 if($conf['authtype'] == 'plain'){ 179 if(is_writable(DOKU_CONF.'users.auth.php')){ 180 msg('conf/users.auth.php is writable',1); 181 }else{ 182 msg('conf/users.auth.php is not writable',0); 183 } 184 } 185 186 if(function_exists('mb_strpos')){ 187 if(defined('UTF8_NOMBSTRING')){ 188 msg('mb_string extension is available but will not be used',0); 189 }else{ 190 msg('mb_string extension is available and will be used',1); 191 if(ini_get('mbstring.func_overload') != 0){ 192 msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1); 193 } 194 } 195 }else{ 196 msg('mb_string extension not available - PHP only replacements will be used',0); 197 } 198 199 if($conf['allowdebug']){ 200 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 201 }else{ 202 msg('Debugging support is disabled',1); 203 } 204 205 if($INFO['userinfo']['name']){ 206 msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0); 207 msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0); 208 }else{ 209 msg('You are currently not logged in',0); 210 } 211 212 msg('Your current permission for this page is '.$INFO['perm'],0); 213 214 if(is_writable($INFO['filepath'])){ 215 msg('The current page is writable by the webserver',0); 216 }else{ 217 msg('The current page is not writable by the webserver',0); 218 } 219 220 if($INFO['writable']){ 221 msg('The current page is writable by you',0); 222 }else{ 223 msg('The current page is not writable by you',0); 224 } 225 226 $check = wl('','',true).'data/_dummy'; 227 $http = new DokuHTTPClient(); 228 $http->timeout = 6; 229 $res = $http->get($check); 230 if(strpos($res,'data directory') !== false){ 231 msg('It seems like the data directory is accessible from the web. 232 Make sure this directory is properly protected 233 (See <a href="http://www.dokuwiki.org/security">security</a>)',-1); 234 }elseif($http->status == 404 || $http->status == 403){ 235 msg('The data directory seems to be properly protected',1); 236 }else{ 237 msg('Failed to check if 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 } 241 242 // Check for corrupted search index 243 $lengths = idx_listIndexLengths(); 244 $index_corrupted = false; 245 foreach ($lengths as $length) { 246 if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) { 247 $index_corrupted = true; 248 break; 249 } 250 } 251 252 foreach (idx_getIndex('metadata', '') as $index) { 253 if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) { 254 $index_corrupted = true; 255 break; 256 } 257 } 258 259 if ($index_corrupted) 260 msg('The search index is corrupted. It might produce wrong results and most 261 probably needs to be rebuilt. See 262 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 263 for ways to rebuild the search index.', -1); 264 elseif (!empty($lengths)) 265 msg('The search index seems to be working', 1); 266 else 267 msg('The search index is empty. See 268 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a> 269 for help on how to fix the search index. If the default indexer 270 isn\'t used or the wiki is actually empty this is normal.'); 271} 272 273/** 274 * print a message 275 * 276 * If HTTP headers were not sent yet the message is added 277 * to the global message array else it's printed directly 278 * using html_msgarea() 279 * 280 * 281 * Levels can be: 282 * 283 * -1 error 284 * 0 info 285 * 1 success 286 * 287 * @author Andreas Gohr <andi@splitbrain.org> 288 * @see html_msgarea 289 */ 290function msg($message,$lvl=0,$line='',$file=''){ 291 global $MSG, $MSG_shown; 292 $errors[-1] = 'error'; 293 $errors[0] = 'info'; 294 $errors[1] = 'success'; 295 $errors[2] = 'notify'; 296 297 if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 298 299 if(!isset($MSG)) $MSG = array(); 300 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 301 if(isset($MSG_shown) || headers_sent()){ 302 if(function_exists('html_msgarea')){ 303 html_msgarea(); 304 }else{ 305 print "ERROR($lvl) $message"; 306 } 307 unset($GLOBALS['MSG']); 308 } 309} 310 311/** 312 * print debug messages 313 * 314 * little function to print the content of a var 315 * 316 * @author Andreas Gohr <andi@splitbrain.org> 317 */ 318function dbg($msg,$hidden=false){ 319 if($hidden){ 320 echo "<!--\n"; 321 print_r($msg); 322 echo "\n-->"; 323 }else{ 324 echo '<pre class="dbg">'; 325 echo hsc(print_r($msg,true)); 326 echo '</pre>'; 327 } 328} 329 330/** 331 * Print info to a log file 332 * 333 * @author Andreas Gohr <andi@splitbrain.org> 334 */ 335function dbglog($msg,$header=''){ 336 global $conf; 337 // The debug log isn't automatically cleaned thus only write it when 338 // debugging has been enabled by the user. 339 if($conf['allowdebug'] !== 1) return; 340 if(is_object($msg) || is_array($msg)){ 341 $msg = print_r($msg,true); 342 } 343 344 if($header) $msg = "$header\n$msg"; 345 346 $file = $conf['cachedir'].'/debug.log'; 347 $fh = fopen($file,'a'); 348 if($fh){ 349 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 350 fclose($fh); 351 } 352} 353 354/** 355 * Print a reversed, prettyprinted backtrace 356 * 357 * @author Gary Owen <gary_owen@bigfoot.com> 358 */ 359function dbg_backtrace(){ 360 // Get backtrace 361 $backtrace = debug_backtrace(); 362 363 // Unset call to debug_print_backtrace 364 array_shift($backtrace); 365 366 // Iterate backtrace 367 $calls = array(); 368 $depth = count($backtrace) - 1; 369 foreach ($backtrace as $i => $call) { 370 $location = $call['file'] . ':' . $call['line']; 371 $function = (isset($call['class'])) ? 372 $call['class'] . $call['type'] . $call['function'] : $call['function']; 373 374 $params = array(); 375 if (isset($call['args'])){ 376 foreach($call['args'] as $arg){ 377 if(is_object($arg)){ 378 $params[] = '[Object '.get_class($arg).']'; 379 }elseif(is_array($arg)){ 380 $params[] = '[Array]'; 381 }elseif(is_null($arg)){ 382 $param[] = '[NULL]'; 383 }else{ 384 $params[] = (string) '"'.$arg.'"'; 385 } 386 } 387 } 388 $params = implode(', ',$params); 389 390 $calls[$depth - $i] = sprintf('%s(%s) called at %s', 391 $function, 392 str_replace("\n", '\n', $params), 393 $location); 394 } 395 ksort($calls); 396 397 return implode("\n", $calls); 398} 399 400/** 401 * Remove all data from an array where the key seems to point to sensitive data 402 * 403 * This is used to remove passwords, mail addresses and similar data from the 404 * debug output 405 * 406 * @author Andreas Gohr <andi@splitbrain.org> 407 */ 408function debug_guard(&$data){ 409 foreach($data as $key => $value){ 410 if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){ 411 $data[$key] = '***'; 412 continue; 413 } 414 if(is_array($value)) debug_guard($data[$key]); 415 } 416} 417