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 if(is_writable($conf['changelog'])){ 93 msg('Changelog is writable',1); 94 }else{ 95 if (@file_exists($conf['changelog'])) { 96 msg('Changelog is not writable',-1); 97 } 98 } 99 100 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 101 msg('Old changelog exists', 0); 102 } 103 104 if (@file_exists($conf['changelog'].'_failed')) { 105 msg('Importing old changelog failed', -1); 106 } else if (@file_exists($conf['changelog'].'_importing')) { 107 msg('Importing old changelog now.', 0); 108 } else if (@file_exists($conf['changelog'].'_import_ok')) { 109 msg('Old changelog imported', 1); 110 if (!plugin_isdisabled('importoldchangelog')) { 111 msg('Importoldchangelog plugin not disabled after import', -1); 112 } 113 } 114 115 if(is_writable($conf['datadir'])){ 116 msg('Datadir is writable',1); 117 }else{ 118 msg('Datadir is not writable',-1); 119 } 120 121 if(is_writable($conf['olddir'])){ 122 msg('Attic is writable',1); 123 }else{ 124 msg('Attic is not writable',-1); 125 } 126 127 if(is_writable($conf['mediadir'])){ 128 msg('Mediadir is writable',1); 129 }else{ 130 msg('Mediadir is not writable',-1); 131 } 132 133 if(is_writable($conf['cachedir'])){ 134 msg('Cachedir is writable',1); 135 }else{ 136 msg('Cachedir is not writable',-1); 137 } 138 139 if(is_writable($conf['lockdir'])){ 140 msg('Lockdir is writable',1); 141 }else{ 142 msg('Lockdir is not writable',-1); 143 } 144 145 if(is_writable(DOKU_CONF.'users.auth.php')){ 146 msg('conf/users.auth.php is writable',1); 147 }else{ 148 msg('conf/users.auth.php is not writable',0); 149 } 150 151 if(function_exists('mb_strpos')){ 152 if(defined('UTF8_NOMBSTRING')){ 153 msg('mb_string extension is available but will not be used',0); 154 }else{ 155 msg('mb_string extension is available and will be used',1); 156 } 157 }else{ 158 msg('mb_string extension not available - PHP only replacements will be used',0); 159 } 160 161 if($conf['allowdebug']){ 162 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 163 }else{ 164 msg('Debugging support is disabled',1); 165 } 166 167 if($INFO['userinfo']['name']){ 168 msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0); 169 msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0); 170 }else{ 171 msg('You are currently not logged in',0); 172 } 173 174 msg('Your current permission for this page is '.$INFO['perm'],0); 175 176 if(is_writable($INFO['filepath'])){ 177 msg('The current page is writable by the webserver',0); 178 }else{ 179 msg('The current page is not writable by the webserver',0); 180 } 181 182 if($INFO['writable']){ 183 msg('The current page is writable by you',0); 184 }else{ 185 msg('The current page is not writable by you',0); 186 } 187} 188 189/** 190 * print a message 191 * 192 * If HTTP headers were not sent yet the message is added 193 * to the global message array else it's printed directly 194 * using html_msgarea() 195 * 196 * 197 * Levels can be: 198 * 199 * -1 error 200 * 0 info 201 * 1 success 202 * 203 * @author Andreas Gohr <andi@splitbrain.org> 204 * @see html_msgarea 205 */ 206function msg($message,$lvl=0,$line='',$file=''){ 207 global $MSG; 208 $errors[-1] = 'error'; 209 $errors[0] = 'info'; 210 $errors[1] = 'success'; 211 $errors[2] = 'notify'; 212 213 if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 214 215 if(!headers_sent()){ 216 if(!isset($MSG)) $MSG = array(); 217 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 218 }else{ 219 $MSG = array(); 220 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 221 if(function_exists('html_msgarea')){ 222 html_msgarea(); 223 }else{ 224 print "ERROR($lvl) $message"; 225 } 226 } 227} 228 229/** 230 * print debug messages 231 * 232 * little function to print the content of a var 233 * 234 * @author Andreas Gohr <andi@splitbrain.org> 235 */ 236function dbg($msg,$hidden=false){ 237 (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 238 print_r($msg); 239 (!$hidden) ? print '</pre>' : print "\n-->"; 240} 241 242/** 243 * Print info to a log file 244 * 245 * @author Andreas Gohr <andi@splitbrain.org> 246 */ 247function dbglog($msg){ 248 global $conf; 249 $file = $conf['cachedir'].'/debug.log'; 250 $fh = fopen($file,'a'); 251 if($fh){ 252 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 253 fclose($fh); 254 } 255} 256 257/** 258 * Print a reversed, prettyprinted backtrace 259 * 260 * @author Gary Owen <gary_owen@bigfoot.com> 261 */ 262function dbg_backtrace(){ 263 // Get backtrace 264 $backtrace = debug_backtrace(); 265 266 // Unset call to debug_print_backtrace 267 array_shift($backtrace); 268 269 // Iterate backtrace 270 $calls = array(); 271 $depth = count($backtrace) - 1; 272 foreach ($backtrace as $i => $call) { 273 $location = $call['file'] . ':' . $call['line']; 274 $function = (isset($call['class'])) ? 275 $call['class'] . $call['type'] . $call['function'] : $call['function']; 276 277 $params = array(); 278 if (isset($call['args'])){ 279 foreach($call['args'] as $arg){ 280 if(is_object($arg)){ 281 $params[] = '[Object '.get_class($arg).']'; 282 }elseif(is_array($arg)){ 283 $params[] = '[Array]'; 284 }elseif(is_null($arg)){ 285 $param[] = '[NULL]'; 286 }else{ 287 $params[] = (string) '"'.$arg.'"'; 288 } 289 } 290 } 291 $params = implode(', ',$params); 292 293 $calls[$depth - $i] = sprintf('%s(%s) called at %s', 294 $function, 295 str_replace("\n", '\n', $params), 296 $location); 297 } 298 ksort($calls); 299 300 return implode("\n", $calls); 301} 302 303