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