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