xref: /dokuwiki/inc/actions.php (revision ac83b9d8ee0da2839787d268e28b1bfe3066c1b7)
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_auth($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,7) == '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  //remove all bad chars
89  $act = strtolower($act);
90  $act = preg_replace('/[^a-z_]+/','',$act);
91
92  if($act == 'register' && !$conf['openregister'])
93    return 'show';
94
95  if($act == $lang['btn_save']) $act = 'save';
96  if($act == $lang['btn_preview']) $act = 'preview';
97  if($act == $lang['btn_cancel']) $act = 'show';
98  if($act == 'export_html') $act = 'export_xhtml';
99
100  if(array_search($act,array('login','logout','register','save','edit',
101                             'preview','search','show','check','index','revisions',
102                             'diff','recent','backlink','admin',)) === false
103     && substr($act,0,7) != 'export_' ) {
104    msg('Unknown command: '.htmlspecialchars($act),-1);
105    return 'show';
106  }
107  return $act;
108}
109
110/**
111 * Run permissionchecks
112 *
113 * @author Andreas Gohr <andi@splitbrain.org>
114 */
115function act_permcheck($act){
116  global $INFO;
117
118  if(in_array($act,array('save','preview','edit'))){
119    if($INFO['exists']){
120      $permneed = AUTH_EDIT;
121    }else{
122      $permneed = AUTH_CREATE;
123    }
124  }elseif(in_array($act,array('login','register','search','recent'))){
125    $permneed = AUTH_NONE;
126  }elseif($act == 'admin'){
127    $permneed = AUTH_ADMIN;
128  }else{
129    $permneed = AUTH_READ;
130  }
131  if($INFO['perm'] >= $permneed) return $act;
132
133  return 'denied';
134}
135
136/**
137 * Handle 'save'
138 *
139 * Checks for spam and conflicts and saves the page.
140 * Does a redirect to show the page afterwards or
141 * returns a new action.
142 *
143 * @author Andreas Gohr <andi@splitbrain.org>
144 */
145function act_save($act){
146  global $ID;
147  global $DATE;
148  global $PRE;
149  global $TEXT;
150  global $SUF;
151  global $SUM;
152
153  //spam check
154  if(checkwordblock())
155    return 'wordblock';
156  //conflict check //FIXME use INFO
157  if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE )
158    return 'conflict';
159
160  //save it
161  saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM); //use pretty mode for con
162  //unlock it
163  unlock($ID);
164
165  //show it
166  session_write_close();
167  header("Location: ".wl($ID,'',true));
168  exit();
169}
170
171/**
172 * Handle 'login', 'logout', 'register'
173 *
174 * @author Andreas Gohr <andi@splitbrain.org>
175 */
176function act_auth($act){
177  //already logged in?
178  if($_SERVER['REMOTE_USER'] && $act=='login')
179    return 'show';
180
181  //handle logout
182  if($act=='logout'){
183    auth_logoff();
184    return 'login';
185  }
186
187  //handle register
188  if($act=='register' && register()){
189    return 'login';
190  }
191
192  return $act;
193}
194
195/**
196 * Handle 'edit', 'preview'
197 *
198 * @author Andreas Gohr <andi@splitbrain.org>
199 */
200function act_edit($act){
201  global $ID;
202
203  //check if locked by anyone - if not lock for my self
204  $lockedby = checklock($ID);
205  if($lockedby) return 'locked';
206
207  lock($ID);
208  return $act;
209}
210
211/**
212 * Handle 'edit', 'preview'
213 *
214 * @author Andreas Gohr <andi@splitbrain.org>
215 */
216function act_export($act){
217  global $ID;
218  global $REV;
219
220  // no renderer for this
221  if($act == 'export_raw'){
222    header('Content-Type: text/plain; charset=utf-8');
223    print rawWiki($ID,$REV);
224    exit;
225  }
226
227  // html export #FIXME what about the template's style?
228  if($act == 'export_xhtml'){
229    header('Content-Type: text/html; charset=utf-8');
230    ptln('<html>');
231    ptln('<head>');
232    tpl_metaheaders();
233    ptln('</head>');
234    ptln('<body>');
235    print p_wiki_xhtml($ID,$REV,false);
236    ptln('</body>');
237    ptln('</html>');
238    exit;
239  }
240
241  // try to run renderer #FIXME use cached instructions
242  $mode = substr($act,7);
243  $text = p_render($mode,p_get_instructions(rawWiki($ID,$REV)));
244  if(!is_null($text)){
245    print $text;
246    exit;
247  }
248
249
250
251  return 'show';
252}
253
254
255//Setup VIM: ex: et ts=2 enc=utf-8 :
256