xref: /dokuwiki/inc/actions.php (revision 0c1887a0b9adc8aac879974db9ee17eb11ceb8d3)
1<?php
2/**
3 * DokuWiki Actions
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10  require_once(DOKU_INC.'inc/template.php');
11
12
13/**
14 * Call the needed action handlers
15 *
16 * @author Andreas Gohr <andi@splitbrain.org>
17 */
18function act_dispatch(){
19  global $INFO;
20  global $ACT;
21  global $ID;
22  global $QUERY;
23  global $lang;
24  global $conf;
25
26  //sanitize $ACT
27  $ACT = act_clean($ACT);
28
29  //check if searchword was given - else just show
30  $s = cleanID($QUERY);
31  if($ACT == 'search' && empty($s)){
32    $ACT = 'show';
33  }
34
35  //login stuff
36  if(in_array($ACT,array('login','logout')))
37    $ACT = act_auth($ACT);
38
39  //check if user is asking to (un)subscribe a page
40  if($ACT == 'subscribe' || $ACT == 'unsubscribe')
41    $ACT = act_subscription($ACT);
42
43  //check permissions
44  $ACT = act_permcheck($ACT);
45
46  //register
47  if($ACT == 'register' && register()){
48    $ACT = 'login';
49  }
50
51  if ($ACT == 'resendpwd' && act_resendpwd()) {
52    $ACT = 'login';
53  }
54
55  //update user profile
56  if (($ACT == 'profile') && updateprofile()) {
57    msg($lang['profchanged'],1);
58    $ACT = 'show';
59  }
60
61  //save
62  if($ACT == 'save')
63    $ACT = act_save($ACT);
64
65  //edit
66  if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){
67    $ACT = act_edit($ACT);
68  }else{
69    unlock($ID); //try to unlock
70  }
71
72  //handle export
73  if(substr($ACT,0,7) == 'export_')
74    $ACT = act_export($ACT);
75
76  //display some infos
77  if($ACT == 'check'){
78    check();
79    $ACT = 'show';
80  }
81
82  //handle admin tasks
83  if($ACT == 'admin'){
84    // retrieve admin plugin name from $_REQUEST['page']
85    if ($_REQUEST['page']) {
86        $pluginlist = plugin_list('admin');
87        if (in_array($_REQUEST['page'], $pluginlist)) {
88          // attempt to load the plugin
89          if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== NULL)
90              $plugin->handle();
91        }
92    }
93/*
94        if($_REQUEST['page'] == 'acl'){
95            require_once(DOKU_INC.'inc/admin_acl.php');
96            admin_acl_handler();
97    }
98*/
99  }
100
101  //call template FIXME: all needed vars available?
102  header('Content-Type: text/html; charset=utf-8');
103  include(template('main.php'));
104  // output for the commands is now handled in inc/templates.php
105  // in function tpl_content()
106}
107
108/**
109 * Sanitize the action command
110 *
111 * Add all allowed commands here.
112 *
113 * @author Andreas Gohr <andi@splitbrain.org>
114 */
115function act_clean($act){
116  global $lang;
117
118  //handle localized buttons
119  if($act == $lang['btn_save']) $act = 'save';
120  if($act == $lang['btn_preview']) $act = 'preview';
121  if($act == $lang['btn_cancel']) $act = 'show';
122
123  //remove all bad chars
124  $act = strtolower($act);
125  $act = preg_replace('/[^a-z_]+/','',$act);
126
127  if($act == 'export_html') $act = 'export_xhtml';
128  if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
129
130  if(array_search($act,array('login','logout','register','save','edit',
131                             'preview','search','show','check','index','revisions',
132                             'diff','recent','backlink','admin','subscribe',
133                             'unsubscribe','profile','resendpwd',)) === false
134     && substr($act,0,7) != 'export_' ) {
135    msg('Unknown command: '.htmlspecialchars($act),-1);
136    return 'show';
137  }
138  return $act;
139}
140
141/**
142 * Run permissionchecks
143 *
144 * @author Andreas Gohr <andi@splitbrain.org>
145 */
146function act_permcheck($act){
147  global $INFO;
148  global $conf;
149
150  if(in_array($act,array('save','preview','edit'))){
151    if($INFO['exists']){
152      if($act == 'edit'){
153        //the edit function will check again and do a source show
154        //when no AUTH_EDIT available
155        $permneed = AUTH_READ;
156      }else{
157        $permneed = AUTH_EDIT;
158      }
159    }else{
160      $permneed = AUTH_CREATE;
161    }
162  }elseif(in_array($act,array('login','search','recent','profile'))){
163    $permneed = AUTH_NONE;
164  }elseif($act == 'register'){
165    if ($conf['openregister']){
166      $permneed = AUTH_NONE;
167    }else{
168      $permneed = AUTH_ADMIN;
169    }
170  }elseif($act == 'admin'){
171    $permneed = AUTH_ADMIN;
172  }else{
173    $permneed = AUTH_READ;
174  }
175  if($INFO['perm'] >= $permneed) return $act;
176
177  return 'denied';
178}
179
180/**
181 * Handle 'save'
182 *
183 * Checks for spam and conflicts and saves the page.
184 * Does a redirect to show the page afterwards or
185 * returns a new action.
186 *
187 * @author Andreas Gohr <andi@splitbrain.org>
188 */
189function act_save($act){
190  global $ID;
191  global $DATE;
192  global $PRE;
193  global $TEXT;
194  global $SUF;
195  global $SUM;
196
197  //spam check
198  if(checkwordblock())
199    return 'wordblock';
200  //conflict check //FIXME use INFO
201  if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE )
202    return 'conflict';
203
204  //save it
205  saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con
206  //unlock it
207  unlock($ID);
208
209  //show it
210  session_write_close();
211  header("Location: ".wl($ID,'',true));
212  exit();
213}
214
215/**
216 * Handle 'login', 'logout'
217 *
218 * @author Andreas Gohr <andi@splitbrain.org>
219 */
220function act_auth($act){
221  global $ID;
222  global $INFO;
223
224  //already logged in?
225  if($_SERVER['REMOTE_USER'] && $act=='login')
226    return 'show';
227
228  //handle logout
229  if($act=='logout'){
230    $lockedby = checklock($ID); //page still locked?
231    if($lockedby == $_SERVER['REMOTE_USER'])
232      unlock($ID); //try to unlock
233
234    // do the logout stuff
235    auth_logoff();
236
237    // rebuild info array
238    $INFO = pageinfo();
239
240    return 'login';
241  }
242
243  return $act;
244}
245
246/**
247 * Handle 'edit', 'preview'
248 *
249 * @author Andreas Gohr <andi@splitbrain.org>
250 */
251function act_edit($act){
252  global $ID;
253
254  //check if locked by anyone - if not lock for my self
255  $lockedby = checklock($ID);
256  if($lockedby) return 'locked';
257
258  lock($ID);
259  return $act;
260}
261
262/**
263 * Handle 'edit', 'preview'
264 *
265 * @author Andreas Gohr <andi@splitbrain.org>
266 */
267function act_export($act){
268  global $ID;
269  global $REV;
270
271  // no renderer for this
272  if($act == 'export_raw'){
273    header('Content-Type: text/plain; charset=utf-8');
274    print rawWiki($ID,$REV);
275    exit;
276  }
277
278  // html export #FIXME what about the template's style?
279  if($act == 'export_xhtml'){
280    global $conf;
281    global $lang;
282    header('Content-Type: text/html; charset=utf-8');
283    ptln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
284    ptln(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
285    ptln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"');
286    ptln(' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">');
287    ptln('<head>');
288    ptln('  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
289    ptln('  <title>'.$ID.'</title>');
290    tpl_metaheaders();
291    ptln('</head>');
292    ptln('<body>');
293    ptln('<div class="dokuwiki export">');
294    print p_wiki_xhtml($ID,$REV,false);
295    ptln('</div>');
296    ptln('</body>');
297    ptln('</html>');
298    exit;
299  }
300
301  // html body only
302  if($act == 'export_xhtmlbody'){
303    print p_wiki_xhtml($ID,$REV,false);
304    exit;
305  }
306
307  // try to run renderer #FIXME use cached instructions
308  $mode = substr($act,7);
309  $text = p_render($mode,p_get_instructions(rawWiki($ID,$REV)),$info);
310  if(!is_null($text)){
311    print $text;
312    exit;
313  }
314
315
316
317  return 'show';
318}
319
320/**
321 * Handle 'subscribe', 'unsubscribe'
322 *
323 * @author Steven Danz <steven-danz@kc.rr.com>
324 * @todo   localize
325 */
326function act_subscription($act){
327  global $ID;
328  global $INFO;
329  global $lang;
330
331  $file=metaFN($ID,'.mlist');
332  if ($act=='subscribe' && !$INFO['subscribed']){
333    if ($INFO['userinfo']['mail']){
334      if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) {
335        $INFO['subscribed'] = true;
336        msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1);
337      } else {
338        msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1);
339      }
340    } else {
341      msg($lang['subscribe_noaddress']);
342    }
343  } elseif ($act=='unsubscribe' && $INFO['subscribed']){
344    if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) {
345      $INFO['subscribed'] = false;
346      msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1);
347    } else {
348      msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1);
349    }
350  }
351
352  return 'show';
353}
354
355//Setup VIM: ex: et ts=2 enc=utf-8 :
356