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