3 * Information and debugging functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
8 if(!defined('DOKU_INC')) die('meh.');
9 if(!defined('DOKU_MESSAGEURL')) define('DOKU_MESSAGEURL','http://update.dokuwiki.org/check/');
12 * Check for new messages from upstream
14 * @author Andreas Gohr <andi@splitbrain.org>
16 function checkUpdateMessages(){
19 global $updateVersion;
20 if(!$conf['updatecheck']) return;
21 if($conf['useacl'] && !$INFO['ismanager']) return;
23 $cf = $conf['cachedir'].'/messages.txt';
24 $lm = @filemtime($cf);
26 // check if new messages needs to be fetched
27 if($lm < time()-(60*60*24) || $lm < @filemtime(DOKU_INC.DOKU_SCRIPT)){
28 dbglog("checkUpdatesMessages(): downloading messages.txt");
29 $http = new DokuHTTPClient();
31 $data = $http->get(DOKU_MESSAGEURL.$updateVersion);
32 io_saveFile($cf,$data);
35 dbglog("checkUpdatesMessages(): messages.txt up to date");
36 $data = io_readFile($cf);
39 // show messages through the usual message mechanism
40 $msgs = explode("\n%\n",$data);
41 foreach($msgs as $msg){
48 * Return DokuWiki's version (split up in date and type)
50 * @author Andreas Gohr <andi@splitbrain.org>
52 function getVersionData(){
54 //import version string
55 if(@file_exists(DOKU_INC.'VERSION')){
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';
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');
69 $chunk = fread($fh,2000);
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;
80 $version['date'] = 'unknown';
81 $version['type'] = 'snapshot?';
87 * Return DokuWiki's version (as a string)
89 * @author Anika Henke <anika@selfthinker.org>
91 function getVersion(){
92 $version = getVersionData();
93 return $version['type'].' '.$version['date'];
97 * Run a few sanity checks
99 * @author Andreas Gohr <andi@splitbrain.org>
105 if ($INFO['isadmin'] || $INFO['ismanager']){
106 msg('DokuWiki version: '.getVersion(),1);
109 if(version_compare(phpversion(),'5.1.2','<')){
110 msg('Your PHP version is too old ('.phpversion().' vs. 5.1.2+ needed)',-1);
112 msg('PHP version '.phpversion(),1);
115 $mem = (int) php_to_byte(ini_get('memory_limit'));
118 msg('PHP is limited to less than 16MB RAM ('.$mem.' bytes). Increase memory_limit in php.ini',-1);
119 }elseif($mem < 20971520){
120 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);
121 }elseif($mem < 33554432){
122 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 msg('More than 32MB RAM ('.$mem.' bytes) available.',1);
128 if(is_writable($conf['changelog'])){
129 msg('Changelog is writable',1);
131 if (@file_exists($conf['changelog'])) {
132 msg('Changelog is not writable',-1);
136 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) {
137 msg('Old changelog exists', 0);
140 if (@file_exists($conf['changelog'].'_failed')) {
141 msg('Importing old changelog failed', -1);
142 } else if (@file_exists($conf['changelog'].'_importing')) {
143 msg('Importing old changelog now.', 0);
144 } else if (@file_exists($conf['changelog'].'_import_ok')) {
145 msg('Old changelog imported', 1);
146 if (!plugin_isdisabled('importoldchangelog')) {
147 msg('Importoldchangelog plugin not disabled after import', -1);
151 if(is_writable(DOKU_CONF)){
152 msg('conf directory is writable',1);
154 msg('conf directory is not writable',-1);
157 if($conf['authtype'] == 'plain'){
158 global $config_cascade;
159 if(is_writable($config_cascade['plainauth.users']['default'])){
160 msg('conf/users.auth.php is writable',1);
162 msg('conf/users.auth.php is not writable',0);
166 if(function_exists('mb_strpos')){
167 if(defined('UTF8_NOMBSTRING')){
168 msg('mb_string extension is available but will not be used',0);
170 msg('mb_string extension is available and will be used',1);
171 if(ini_get('mbstring.func_overload') != 0){
172 msg('mb_string function overloading is enabled, this will cause problems and should be disabled',-1);
176 msg('mb_string extension not available - PHP only replacements will be used',0);
179 $loc = setlocale(LC_ALL, 0);
181 msg('No valid locale is set for your PHP setup. You should fix this',-1);
182 }elseif(stripos($loc,'utf') === false){
183 msg('Your locale <code>'.hsc($loc).'</code> seems not to be a UTF-8 locale, you should fix this if you encounter problems.',0);
185 msg('Valid locale '.hsc($loc).' found.', 1);
189 if($conf['allowdebug']){
190 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1);
192 msg('Debugging support is disabled',1);
195 if($INFO['userinfo']['name']){
196 msg('You are currently logged in as '.$_SERVER['REMOTE_USER'].' ('.$INFO['userinfo']['name'].')',0);
197 msg('You are part of the groups '.join($INFO['userinfo']['grps'],', '),0);
199 msg('You are currently not logged in',0);
202 msg('Your current permission for this page is '.$INFO['perm'],0);
204 if(is_writable($INFO['filepath'])){
205 msg('The current page is writable by the webserver',0);
207 msg('The current page is not writable by the webserver',0);
210 if($INFO['writable']){
211 msg('The current page is writable by you',0);
213 msg('The current page is not writable by you',0);
216 // Check for corrupted search index
217 $lengths = idx_listIndexLengths();
218 $index_corrupted = false;
219 foreach ($lengths as $length) {
220 if (count(idx_getIndex('w', $length)) != count(idx_getIndex('i', $length))) {
221 $index_corrupted = true;
226 foreach (idx_getIndex('metadata', '') as $index) {
227 if (count(idx_getIndex($index.'_w', '')) != count(idx_getIndex($index.'_i', ''))) {
228 $index_corrupted = true;
233 if ($index_corrupted)
234 msg('The search index is corrupted. It might produce wrong results and most
235 probably needs to be rebuilt. See
236 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
237 for ways to rebuild the search index.', -1);
238 elseif (!empty($lengths))
239 msg('The search index seems to be working', 1);
241 msg('The search index is empty. See
242 <a href="http://www.dokuwiki.org/faq:searchindex">faq:searchindex</a>
243 for help on how to fix the search index. If the default indexer
244 isn\'t used or the wiki is actually empty this is normal.');
250 * If HTTP headers were not sent yet the message is added
251 * to the global message array else it's printed directly
252 * using html_msgarea()
261 * @author Andreas Gohr <andi@splitbrain.org>
264 function msg($message,$lvl=0,$line='',$file=''){
265 global $MSG, $MSG_shown;
266 $errors[-1] = 'error';
268 $errors[1] = 'success';
269 $errors[2] = 'notify';
271 if($line || $file) $message.=' ['.utf8_basename($file).':'.$line.']';
273 if(!isset($MSG)) $MSG = array();
274 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message);
275 if(isset($MSG_shown) || headers_sent()){
276 if(function_exists('html_msgarea')){
279 print "ERROR($lvl) $message";
281 unset($GLOBALS['MSG']);
286 * print debug messages
288 * little function to print the content of a var
290 * @author Andreas Gohr <andi@splitbrain.org>
292 function dbg($msg,$hidden=false){
298 echo '<pre class="dbg">';
299 echo hsc(print_r($msg,true));
305 * Print info to a log file
307 * @author Andreas Gohr <andi@splitbrain.org>
309 function dbglog($msg,$header=''){
311 // The debug log isn't automatically cleaned thus only write it when
312 // debugging has been enabled by the user.
313 if($conf['allowdebug'] !== 1) return;
314 if(is_object($msg) || is_array($msg)){
315 $msg = print_r($msg,true);
318 if($header) $msg = "$header\n$msg";
320 $file = $conf['cachedir'].'/debug.log';
321 $fh = fopen($file,'a');
323 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n");
329 * Print a reversed, prettyprinted backtrace
331 * @author Gary Owen <gary_owen@bigfoot.com>
333 function dbg_backtrace(){
335 $backtrace = debug_backtrace();
337 // Unset call to debug_print_backtrace
338 array_shift($backtrace);
342 $depth = count($backtrace) - 1;
343 foreach ($backtrace as $i => $call) {
344 $location = $call['file'] . ':' . $call['line'];
345 $function = (isset($call['class'])) ?
346 $call['class'] . $call['type'] . $call['function'] : $call['function'];
349 if (isset($call['args'])){
350 foreach($call['args'] as $arg){
352 $params[] = '[Object '.get_class($arg).']';
353 }elseif(is_array($arg)){
354 $params[] = '[Array]';
355 }elseif(is_null($arg)){
358 $params[] = (string) '"'.$arg.'"';
362 $params = implode(', ',$params);
364 $calls[$depth - $i] = sprintf('%s(%s) called at %s',
366 str_replace("\n", '\n', $params),
371 return implode("\n", $calls);
375 * Remove all data from an array where the key seems to point to sensitive data
377 * This is used to remove passwords, mail addresses and similar data from the
380 * @author Andreas Gohr <andi@splitbrain.org>
382 function debug_guard(&$data){
383 foreach($data as $key => $value){
384 if(preg_match('/(notify|pass|auth|secret|ftp|userinfo|token|buid|mail|proxy)/i',$key)){
388 if(is_array($value)) debug_guard($data[$key]);