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