xref: /dokuwiki/inc/actions.php (revision c19fe9c0f68f58ff9c18f0e185a5bc6b591bf798)
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 permissions
30  $ACT = act_permcheck($ACT);
31
32  //login stuff
33  if(in_array($ACT,array('login','logout','register')))
34    $ACT = act_login($ACT);
35
36  //save
37  if($ACT == 'save')
38    $ACT = act_save($ACT);
39
40  //edit
41  if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){
42    $ACT = act_edit($ACT);
43  }else{
44    unlock($ID); //try to unlock
45  }
46
47  //handle export
48  if(substr($ACT,0,6) == 'export')
49    $ACT = act_export($ACT);
50
51  //display some infos
52  if($ACT == 'check'){
53    check();
54    $ACT = 'show';
55  }
56
57  //check if searchword was given - else just show
58  if($ACT == 'search' && empty($QUERY)){
59    $ACT = 'show';
60  }
61
62  //handle admin tasks
63  if($ACT == 'admin'){
64		if($_REQUEST['page'] == 'acl'){
65			require_once(DOKU_INC.'inc/admin_acl.php');
66			admin_acl_handler();
67		}
68  }
69
70  //call template FIXME: all needed vars available?
71  header('Content-Type: text/html; charset=utf-8');
72  include(DOKU_INC.'tpl/'.$conf['template'].'/main.php');
73  // output for the commands is now handled in inc/templates.php
74  // in function tpl_content()
75}
76
77/**
78 * Sanitize the action command
79 *
80 * Add all allowed commands here.
81 *
82 * @author Andreas Gohr <andi@splitbrain.org>
83 */
84function act_clean($act){
85  global $lang;
86  global $conf;
87
88  if($act == 'register' && !$conf['openregister'])
89    return 'show';
90
91  if($act == $lang['btn_save']) $act = 'save';
92  if($act == $lang['btn_preview']) $act = 'preview';
93  $act = strtolower($act);
94
95  if(!array_search($act,array('login','logout','register','save','edit',
96                              'preview','export_raw','export_html',
97                              'search','show','check','index','revisions',
98                              'diff','recent','backlink','admin',))){
99    msg('Unknown command: '.htmlspecialchars($act),-1);
100    return 'show';
101  }
102  return $act;
103}
104
105/**
106 * Run permissionchecks
107 *
108 * @author Andreas Gohr <andi@splitbrain.org>
109 */
110function act_permcheck($act){
111  global $INFO;
112
113  if(in_array($act,array('save','preview','edit'))){
114    if($INFO['exists']){
115      $permneed = AUTH_EDIT;
116    }else{
117      $permneed = AUTH_CREATE;
118    }
119  }elseif(in_array($act,array('login','register','search','recent'))){
120    $permneed = AUTH_NONE;
121  }elseif($act == 'admin'){
122    $permneed = AUTH_ADMIN;
123  }else{
124    $permneed = AUTH_READ;
125  }
126  if($INFO['perm'] >= $permneed) return $act;
127
128  return 'denied';
129}
130
131/**
132 * Handle 'save'
133 *
134 * Checks for spam and conflicts and saves the page.
135 * Does a redirect to show the page afterwards or
136 * returns a new action.
137 *
138 * @author Andreas Gohr <andi@splitbrain.org>
139 */
140function act_save($act){
141  global $ID;
142  global $DATE;
143  global $PRE;
144  global $TEXT;
145  global $SUF;
146  global $SUM;
147
148  //spam check
149  if(checkwordblock())
150    return 'wordblock';
151  //conflict check //FIXME use INFO
152  if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE )
153    return 'conflict';
154
155  //save it
156  saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM); //use pretty mode for con
157  //unlock it
158  unlock($ID);
159
160  //show it
161  session_write_close();
162  header("Location: ".wl($ID,'',true));
163  exit();
164}
165
166/**
167 * Handle 'login', 'logout', 'register'
168 *
169 * @author Andreas Gohr <andi@splitbrain.org>
170 */
171function act_auth($act){
172  //already logged in?
173  if($_SERVER['REMOTE_USER'] && $act=='login')
174    return 'show';
175
176  //handle logout
177  if($act=='logout'){
178    auth_logoff();
179    return 'login';
180  }
181
182  //handle register
183  if($act=='register' && register()){
184    $act='login';
185  }
186
187  return $act;
188}
189
190/**
191 * Handle 'edit', 'preview'
192 *
193 * @author Andreas Gohr <andi@splitbrain.org>
194 */
195function act_edit($act){
196  //check if locked by anyone - if not lock for my self
197  $lockedby = checklock($ID);
198  if($lockedby) return 'locked';
199
200  lock($ID);
201  return $act;
202}
203
204/**
205 * Handle 'edit', 'preview'
206 *
207 * @author Andreas Gohr <andi@splitbrain.org>
208 */
209function act_export($act){
210  global $ID;
211  global $REV;
212
213  if($act == 'export_html'){
214    header('Content-Type: text/html; charset=utf-8');
215    ptln('<html>');
216    ptln('<head>');
217    tpl_metaheaders();
218    ptln('</head>');
219    ptln('<body>');
220    print parsedWiki($ID,$REV,false);
221    ptln('</body>');
222    ptln('</html>');
223    exit;
224  }
225
226  if($act == 'export_raw'){
227    header('Content-Type: text/plain; charset=utf-8');
228    print rawWiki($ID,$REV);
229    exit;
230  }
231
232  return 'show';
233}
234?>
235