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')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 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(),'4.3.3','<')){ 85 msg('Your PHP version is too old ('.phpversion().' vs. 4.3.3+ recommended)',-1); 86 }elseif(version_compare(phpversion(),'4.3.10','<')){ 87 msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 88 }else{ 89 msg('PHP version '.phpversion(),1); 90 } 91 92 $mem = (int) php_to_byte(ini_get('memory_limit')); 93 if($mem){ 94 if($mem < 16777216){ 95 msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1); 96 }elseif($mem < 20971520){ 97 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); 98 }elseif($mem < 33554432){ 99 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); 100 }else{ 101 msg('More than 32MB RAM ('.$mem.' bytes) available.',1); 102 } 103 } 104 105 106 if(is_writable($conf['changelog'])){ 107 msg('Changelog is writable',1); 108 }else{ 109 if (@file_exists($conf['changelog'])) { 110 msg('Changelog is not writable',-1); 111 } 112 } 113 114 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 115 msg('Old changelog exists', 0); 116 } 117 118 if (@file_exists($conf['changelog'].'_failed')) { 119 msg('Importing old changelog failed', -1); 120 } else if (@file_exists($conf['changelog'].'_importing')) { 121 msg('Importing old changelog now.', 0); 122 } else if (@file_exists($conf['changelog'].'_import_ok')) { 123 msg('Old changelog imported', 1); 124 if (!plugin_isdisabled('importoldchangelog')) { 125 msg('Importoldchangelog plugin not disabled after import', -1); 126 } 127 } 128 129 if(is_writable($conf['datadir'])){ 130 msg('Datadir is writable',1); 131 }else{ 132 msg('Datadir is not writable',-1); 133 } 134 135 if(is_writable($conf['olddir'])){ 136 msg('Attic is writable',1); 137 }else{ 138 msg('Attic is not writable',-1); 139 } 140 141 if(is_writable($conf['mediadir'])){ 142 msg('Mediadir is writable',1); 143 }else{ 144 msg('Mediadir is not writable',-1); 145 } 146 147 if(is_writable($conf['cachedir'])){ 148 msg('Cachedir is writable',1); 149 }else{ 150 msg('Cachedir is not writable',-1); 151 } 152 153 if(is_writable($conf['lockdir'])){ 154 msg('Lockdir is writable',1); 155 }else{ 156 msg('Lockdir is not writable',-1); 157 } 158 159 if($conf['authtype'] == 'plain'){ 160 if(is_writable(DOKU_CONF.'users.auth.php')){ 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 } 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 205/** 206 * print a message 207 * 208 * If HTTP headers were not sent yet the message is added 209 * to the global message array else it's printed directly 210 * using html_msgarea() 211 * 212 * 213 * Levels can be: 214 * 215 * -1 error 216 * 0 info 217 * 1 success 218 * 219 * @author Andreas Gohr <andi@splitbrain.org> 220 * @see html_msgarea 221 */ 222function msg($message,$lvl=0,$line='',$file=''){ 223 global $MSG; 224 $errors[-1] = 'error'; 225 $errors[0] = 'info'; 226 $errors[1] = 'success'; 227 $errors[2] = 'notify'; 228 229 if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 230 231 if(!headers_sent()){ 232 if(!isset($MSG)) $MSG = array(); 233 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 234 }else{ 235 $MSG = array(); 236 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 237 if(function_exists('html_msgarea')){ 238 html_msgarea(); 239 }else{ 240 print "ERROR($lvl) $message"; 241 } 242 } 243} 244 245/** 246 * print debug messages 247 * 248 * little function to print the content of a var 249 * 250 * @author Andreas Gohr <andi@splitbrain.org> 251 */ 252function dbg($msg,$hidden=false){ 253 (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 254 print_r($msg); 255 (!$hidden) ? print '</pre>' : print "\n-->"; 256} 257 258/** 259 * Print info to a log file 260 * 261 * @author Andreas Gohr <andi@splitbrain.org> 262 */ 263function dbglog($msg){ 264 global $conf; 265 $file = $conf['cachedir'].'/debug.log'; 266 $fh = fopen($file,'a'); 267 if($fh){ 268 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 269 fclose($fh); 270 } 271} 272 273/** 274 * Print a reversed, prettyprinted backtrace 275 * 276 * @author Gary Owen <gary_owen@bigfoot.com> 277 */ 278function dbg_backtrace(){ 279 // Get backtrace 280 $backtrace = debug_backtrace(); 281 282 // Unset call to debug_print_backtrace 283 array_shift($backtrace); 284 285 // Iterate backtrace 286 $calls = array(); 287 $depth = count($backtrace) - 1; 288 foreach ($backtrace as $i => $call) { 289 $location = $call['file'] . ':' . $call['line']; 290 $function = (isset($call['class'])) ? 291 $call['class'] . $call['type'] . $call['function'] : $call['function']; 292 293 $params = array(); 294 if (isset($call['args'])){ 295 foreach($call['args'] as $arg){ 296 if(is_object($arg)){ 297 $params[] = '[Object '.get_class($arg).']'; 298 }elseif(is_array($arg)){ 299 $params[] = '[Array]'; 300 }elseif(is_null($arg)){ 301 $param[] = '[NULL]'; 302 }else{ 303 $params[] = (string) '"'.$arg.'"'; 304 } 305 } 306 } 307 $params = implode(', ',$params); 308 309 $calls[$depth - $i] = sprintf('%s(%s) called at %s', 310 $function, 311 str_replace("\n", '\n', $params), 312 $location); 313 } 314 ksort($calls); 315 316 return implode("\n", $calls); 317} 318 319/** 320 * Remove all data from an array where the key seems to point to sensitive data 321 * 322 * This is used to remove passwords, mail addresses and similar data from the 323 * debug output 324 * 325 * @author Andreas Gohr <andi@splitbrain.org> 326 */ 327function debug_guard(&$data){ 328 foreach($data as $key => $value){ 329 if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){ 330 $data[$key] = '***'; 331 continue; 332 } 333 if(is_array($value)) debug_guard($data[$key]); 334 } 335} 336