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