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