1*c29dc6e4SAndreas Gohr<?php 2*c29dc6e4SAndreas Gohr/** 3*c29dc6e4SAndreas Gohr * Information and debugging functions 4*c29dc6e4SAndreas Gohr * 5*c29dc6e4SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6*c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7*c29dc6e4SAndreas Gohr */ 8*c29dc6e4SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 9*c29dc6e4SAndreas Gohrif(!defined('DOKU_MESSAGEURL')) define('DOKU_MESSAGEURL','http://www.splitbrain.org/lib/exe/msg.php?msg='); 10*c29dc6e4SAndreas Gohrrequire_once(DOKU_INC.'inc/HTTPClient.php'); 11*c29dc6e4SAndreas Gohr 12*c29dc6e4SAndreas Gohr/** 13*c29dc6e4SAndreas Gohr * Check for new messages from upstream 14*c29dc6e4SAndreas Gohr * 15*c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 16*c29dc6e4SAndreas Gohr */ 17*c29dc6e4SAndreas Gohrfunction checkUpdateMessages(){ 18*c29dc6e4SAndreas Gohr global $conf; 19*c29dc6e4SAndreas Gohr global $INFO; 20*c29dc6e4SAndreas Gohr if(!$conf['updatecheck']) return; 21*c29dc6e4SAndreas Gohr if($INFO['perm'] < AUTH_ADMIN) return; 22*c29dc6e4SAndreas Gohr 23*c29dc6e4SAndreas Gohr $cf = $conf['cachedir'].'/messages.txt'; 24*c29dc6e4SAndreas Gohr $lm = @filemtime($cf); 25*c29dc6e4SAndreas Gohr 26*c29dc6e4SAndreas Gohr // check if new messages needs to be fetched 27*c29dc6e4SAndreas Gohr if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_CONF.'msg')){ 28*c29dc6e4SAndreas Gohr $num = file(DOKU_CONF.'msg'); 29*c29dc6e4SAndreas Gohr $num = (int) $num[0]; 30*c29dc6e4SAndreas Gohr $http = new DokuHTTPClient(); 31*c29dc6e4SAndreas Gohr $http->timeout = 8; 32*c29dc6e4SAndreas Gohr $data = $http->get(DOKU_MESSAGEURL.$num); 33*c29dc6e4SAndreas Gohr io_saveFile($cf,$data); 34*c29dc6e4SAndreas Gohr }else{ 35*c29dc6e4SAndreas Gohr $data = io_readFile($cf); 36*c29dc6e4SAndreas Gohr } 37*c29dc6e4SAndreas Gohr 38*c29dc6e4SAndreas Gohr // show messages through the usual message mechanism 39*c29dc6e4SAndreas Gohr $msgs = explode("\n%\n",$data); 40*c29dc6e4SAndreas Gohr foreach($msgs as $msg){ 41*c29dc6e4SAndreas Gohr if($msg) msg($msg,2); 42*c29dc6e4SAndreas Gohr } 43*c29dc6e4SAndreas Gohr} 44*c29dc6e4SAndreas Gohr 45*c29dc6e4SAndreas Gohr 46*c29dc6e4SAndreas Gohr/** 47*c29dc6e4SAndreas Gohr * Return DokuWikis version 48*c29dc6e4SAndreas Gohr * 49*c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 50*c29dc6e4SAndreas Gohr */ 51*c29dc6e4SAndreas Gohrfunction getVersion(){ 52*c29dc6e4SAndreas Gohr //import version string 53*c29dc6e4SAndreas Gohr if(@file_exists('VERSION')){ 54*c29dc6e4SAndreas Gohr //official release 55*c29dc6e4SAndreas Gohr return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION')); 56*c29dc6e4SAndreas Gohr }elseif(is_dir('_darcs')){ 57*c29dc6e4SAndreas Gohr //darcs checkout - read last 2000 bytes of inventory 58*c29dc6e4SAndreas Gohr $sz = filesize('_darcs/inventory'); 59*c29dc6e4SAndreas Gohr $seek = max(0,$sz-2000); 60*c29dc6e4SAndreas Gohr $fh = fopen('_darcs/inventory','rb'); 61*c29dc6e4SAndreas Gohr fseek($fh,$seek); 62*c29dc6e4SAndreas Gohr $chunk = fread($fh,2000); 63*c29dc6e4SAndreas Gohr fclose($fh); 64*c29dc6e4SAndreas Gohr $inv = preg_grep('#\*\*\d{14}[\]$]#',explode("\n",$chunk)); 65*c29dc6e4SAndreas Gohr $cur = array_pop($inv); 66*c29dc6e4SAndreas Gohr preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 67*c29dc6e4SAndreas Gohr return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 68*c29dc6e4SAndreas Gohr }else{ 69*c29dc6e4SAndreas Gohr return 'snapshot?'; 70*c29dc6e4SAndreas Gohr } 71*c29dc6e4SAndreas Gohr} 72*c29dc6e4SAndreas Gohr 73*c29dc6e4SAndreas Gohr/** 74*c29dc6e4SAndreas Gohr * Run a few sanity checks 75*c29dc6e4SAndreas Gohr * 76*c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 77*c29dc6e4SAndreas Gohr */ 78*c29dc6e4SAndreas Gohrfunction check(){ 79*c29dc6e4SAndreas Gohr global $conf; 80*c29dc6e4SAndreas Gohr global $INFO; 81*c29dc6e4SAndreas Gohr 82*c29dc6e4SAndreas Gohr msg('DokuWiki version: '.getVersion(),1); 83*c29dc6e4SAndreas Gohr 84*c29dc6e4SAndreas Gohr if(version_compare(phpversion(),'4.3.0','<')){ 85*c29dc6e4SAndreas Gohr msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 86*c29dc6e4SAndreas Gohr }elseif(version_compare(phpversion(),'4.3.10','<')){ 87*c29dc6e4SAndreas Gohr msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 88*c29dc6e4SAndreas Gohr }else{ 89*c29dc6e4SAndreas Gohr msg('PHP version '.phpversion(),1); 90*c29dc6e4SAndreas Gohr } 91*c29dc6e4SAndreas Gohr 92*c29dc6e4SAndreas Gohr if(is_writable($conf['changelog'])){ 93*c29dc6e4SAndreas Gohr msg('Changelog is writable',1); 94*c29dc6e4SAndreas Gohr }else{ 95*c29dc6e4SAndreas Gohr if (@file_exists($conf['changelog'])) { 96*c29dc6e4SAndreas Gohr msg('Changelog is not writable',-1); 97*c29dc6e4SAndreas Gohr } 98*c29dc6e4SAndreas Gohr } 99*c29dc6e4SAndreas Gohr 100*c29dc6e4SAndreas Gohr if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 101*c29dc6e4SAndreas Gohr msg('Old changelog exists.', 0); 102*c29dc6e4SAndreas Gohr } 103*c29dc6e4SAndreas Gohr 104*c29dc6e4SAndreas Gohr if (@file_exists($conf['changelog'].'_failed')) { 105*c29dc6e4SAndreas Gohr msg('Importing old changelog failed.', -1); 106*c29dc6e4SAndreas Gohr } else if (@file_exists($conf['changelog'].'_importing')) { 107*c29dc6e4SAndreas Gohr msg('Importing old changelog now.', 0); 108*c29dc6e4SAndreas Gohr } else if (@file_exists($conf['changelog'].'_import_ok')) { 109*c29dc6e4SAndreas Gohr msg('Old changelog imported.', 1); 110*c29dc6e4SAndreas Gohr if (!plugin_isdisabled('importoldchangelog')) { 111*c29dc6e4SAndreas Gohr msg('Importoldchangelog plugin not disabled after import.', -1); 112*c29dc6e4SAndreas Gohr } 113*c29dc6e4SAndreas Gohr } 114*c29dc6e4SAndreas Gohr 115*c29dc6e4SAndreas Gohr if(is_writable($conf['datadir'])){ 116*c29dc6e4SAndreas Gohr msg('Datadir is writable',1); 117*c29dc6e4SAndreas Gohr }else{ 118*c29dc6e4SAndreas Gohr msg('Datadir is not writable',-1); 119*c29dc6e4SAndreas Gohr } 120*c29dc6e4SAndreas Gohr 121*c29dc6e4SAndreas Gohr if(is_writable($conf['olddir'])){ 122*c29dc6e4SAndreas Gohr msg('Attic is writable',1); 123*c29dc6e4SAndreas Gohr }else{ 124*c29dc6e4SAndreas Gohr msg('Attic is not writable',-1); 125*c29dc6e4SAndreas Gohr } 126*c29dc6e4SAndreas Gohr 127*c29dc6e4SAndreas Gohr if(is_writable($conf['mediadir'])){ 128*c29dc6e4SAndreas Gohr msg('Mediadir is writable',1); 129*c29dc6e4SAndreas Gohr }else{ 130*c29dc6e4SAndreas Gohr msg('Mediadir is not writable',-1); 131*c29dc6e4SAndreas Gohr } 132*c29dc6e4SAndreas Gohr 133*c29dc6e4SAndreas Gohr if(is_writable($conf['cachedir'])){ 134*c29dc6e4SAndreas Gohr msg('Cachedir is writable',1); 135*c29dc6e4SAndreas Gohr }else{ 136*c29dc6e4SAndreas Gohr msg('Cachedir is not writable',-1); 137*c29dc6e4SAndreas Gohr } 138*c29dc6e4SAndreas Gohr 139*c29dc6e4SAndreas Gohr if(is_writable($conf['lockdir'])){ 140*c29dc6e4SAndreas Gohr msg('Lockdir is writable',1); 141*c29dc6e4SAndreas Gohr }else{ 142*c29dc6e4SAndreas Gohr msg('Lockdir is not writable',-1); 143*c29dc6e4SAndreas Gohr } 144*c29dc6e4SAndreas Gohr 145*c29dc6e4SAndreas Gohr if(is_writable(DOKU_CONF.'users.auth.php')){ 146*c29dc6e4SAndreas Gohr msg('conf/users.auth.php is writable',1); 147*c29dc6e4SAndreas Gohr }else{ 148*c29dc6e4SAndreas Gohr msg('conf/users.auth.php is not writable',0); 149*c29dc6e4SAndreas Gohr } 150*c29dc6e4SAndreas Gohr 151*c29dc6e4SAndreas Gohr if(function_exists('mb_strpos')){ 152*c29dc6e4SAndreas Gohr if(defined('UTF8_NOMBSTRING')){ 153*c29dc6e4SAndreas Gohr msg('mb_string extension is available but will not be used',0); 154*c29dc6e4SAndreas Gohr }else{ 155*c29dc6e4SAndreas Gohr msg('mb_string extension is available and will be used',1); 156*c29dc6e4SAndreas Gohr } 157*c29dc6e4SAndreas Gohr }else{ 158*c29dc6e4SAndreas Gohr msg('mb_string extension not available - PHP only replacements will be used',0); 159*c29dc6e4SAndreas Gohr } 160*c29dc6e4SAndreas Gohr 161*c29dc6e4SAndreas Gohr if($conf['allowdebug']){ 162*c29dc6e4SAndreas Gohr msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 163*c29dc6e4SAndreas Gohr }else{ 164*c29dc6e4SAndreas Gohr msg('Debugging support is disabled',1); 165*c29dc6e4SAndreas Gohr } 166*c29dc6e4SAndreas Gohr 167*c29dc6e4SAndreas Gohr msg('Your current permission for this page is '.$INFO['perm'],0); 168*c29dc6e4SAndreas Gohr 169*c29dc6e4SAndreas Gohr if(is_writable($INFO['filepath'])){ 170*c29dc6e4SAndreas Gohr msg('The current page is writable by the webserver',0); 171*c29dc6e4SAndreas Gohr }else{ 172*c29dc6e4SAndreas Gohr msg('The current page is not writable by the webserver',0); 173*c29dc6e4SAndreas Gohr } 174*c29dc6e4SAndreas Gohr 175*c29dc6e4SAndreas Gohr if($INFO['writable']){ 176*c29dc6e4SAndreas Gohr msg('The current page is writable by you',0); 177*c29dc6e4SAndreas Gohr }else{ 178*c29dc6e4SAndreas Gohr msg('The current page is not writable you',0); 179*c29dc6e4SAndreas Gohr } 180*c29dc6e4SAndreas Gohr} 181*c29dc6e4SAndreas Gohr 182*c29dc6e4SAndreas Gohr/** 183*c29dc6e4SAndreas Gohr * print a message 184*c29dc6e4SAndreas Gohr * 185*c29dc6e4SAndreas Gohr * If HTTP headers were not sent yet the message is added 186*c29dc6e4SAndreas Gohr * to the global message array else it's printed directly 187*c29dc6e4SAndreas Gohr * using html_msgarea() 188*c29dc6e4SAndreas Gohr * 189*c29dc6e4SAndreas Gohr * 190*c29dc6e4SAndreas Gohr * Levels can be: 191*c29dc6e4SAndreas Gohr * 192*c29dc6e4SAndreas Gohr * -1 error 193*c29dc6e4SAndreas Gohr * 0 info 194*c29dc6e4SAndreas Gohr * 1 success 195*c29dc6e4SAndreas Gohr * 196*c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 197*c29dc6e4SAndreas Gohr * @see html_msgarea 198*c29dc6e4SAndreas Gohr */ 199*c29dc6e4SAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){ 200*c29dc6e4SAndreas Gohr global $MSG; 201*c29dc6e4SAndreas Gohr $errors[-1] = 'error'; 202*c29dc6e4SAndreas Gohr $errors[0] = 'info'; 203*c29dc6e4SAndreas Gohr $errors[1] = 'success'; 204*c29dc6e4SAndreas Gohr $errors[2] = 'notify'; 205*c29dc6e4SAndreas Gohr 206*c29dc6e4SAndreas Gohr if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 207*c29dc6e4SAndreas Gohr 208*c29dc6e4SAndreas Gohr if(!headers_sent()){ 209*c29dc6e4SAndreas Gohr if(!isset($MSG)) $MSG = array(); 210*c29dc6e4SAndreas Gohr $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 211*c29dc6e4SAndreas Gohr }else{ 212*c29dc6e4SAndreas Gohr $MSG = array(); 213*c29dc6e4SAndreas Gohr $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 214*c29dc6e4SAndreas Gohr if(function_exists('html_msgarea')){ 215*c29dc6e4SAndreas Gohr html_msgarea(); 216*c29dc6e4SAndreas Gohr }else{ 217*c29dc6e4SAndreas Gohr print "ERROR($lvl) $message"; 218*c29dc6e4SAndreas Gohr } 219*c29dc6e4SAndreas Gohr } 220*c29dc6e4SAndreas Gohr} 221*c29dc6e4SAndreas Gohr 222*c29dc6e4SAndreas Gohr/** 223*c29dc6e4SAndreas Gohr * print debug messages 224*c29dc6e4SAndreas Gohr * 225*c29dc6e4SAndreas Gohr * little function to print the content of a var 226*c29dc6e4SAndreas Gohr * 227*c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 228*c29dc6e4SAndreas Gohr */ 229*c29dc6e4SAndreas Gohrfunction dbg($msg,$hidden=false){ 230*c29dc6e4SAndreas Gohr (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 231*c29dc6e4SAndreas Gohr print_r($msg); 232*c29dc6e4SAndreas Gohr (!$hidden) ? print '</pre>' : print "\n-->"; 233*c29dc6e4SAndreas Gohr} 234*c29dc6e4SAndreas Gohr 235*c29dc6e4SAndreas Gohr/** 236*c29dc6e4SAndreas Gohr * Print info to a log file 237*c29dc6e4SAndreas Gohr * 238*c29dc6e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 239*c29dc6e4SAndreas Gohr */ 240*c29dc6e4SAndreas Gohrfunction dbglog($msg){ 241*c29dc6e4SAndreas Gohr global $conf; 242*c29dc6e4SAndreas Gohr $file = $conf['cachedir'].'/debug.log'; 243*c29dc6e4SAndreas Gohr $fh = fopen($file,'a'); 244*c29dc6e4SAndreas Gohr if($fh){ 245*c29dc6e4SAndreas Gohr fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 246*c29dc6e4SAndreas Gohr fclose($fh); 247*c29dc6e4SAndreas Gohr } 248*c29dc6e4SAndreas Gohr} 249*c29dc6e4SAndreas Gohr 250*c29dc6e4SAndreas Gohr 251