xref: /dokuwiki/inc/actions.php (revision 6b13307fb447795714d01cdc029d6ed7ac087cf3)
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 * Call the needed action handlers
14 *
15 * @author Andreas Gohr <andi@splitbrain.org>
16 */
17function act_dispatch(){
18  global $INFO;
19  global $ACT;
20  global $ID;
21  global $QUERY;
22  global $lang;
23  global $conf;
24
25  //check permissions
26  $ACT = act_permcheck($ACT);
27
28  //login stuff
29  if(in_array($ACT,array('login','logout','register')))
30    $ACT = act_login($ACT);
31
32  //save
33  if($ACT == 'save')
34    $ACT = act_save($ACT);
35
36  //edit
37  if(($ACT == 'edit' || $ACT == $lang['btn_preview']) && $INFO['editable']){
38    $ACT = act_save($ACT);
39  }else{
40    unlock($ID); //try to unlock
41  }
42
43  //handle export
44  if(substr($ACT,0,6) == 'export')
45    $ACT = act_export($ACT);
46
47  //display some infos
48  if($ACT == 'check'){
49    check();
50    $ACT = 'show';
51  }
52
53  //check if searchword was given - else just show
54  if($ACT == 'search' && empty($QUERY)){
55    $ACT = 'show';
56  }
57
58  //fixme sanitize $ACT
59
60  //call template FIXME: all needed vars available?
61  header('Content-Type: text/html; charset=utf-8');
62  include(DOKU_INC.'tpl/'.$conf['template'].'/main.php');
63}
64
65/**
66 * Run permissionchecks
67 *
68 * @author Andreas Gohr <andi@splitbrain.org>
69 */
70function act_permcheck($act){
71  if(in_array($act,array('save','preview','edit'))){
72    if($INFO['exists']){
73      $permneed = AUTH_EDIT;
74    }else{
75      $permneed = AUTH_CREATE;
76    }
77  }elseif(in_array($act,array('login','register','search','recent'))){
78    $permneed = AUTH_NONE;
79  }else{
80    $permneed = AUTH_READ;
81  }
82  if(! auth_quickaclcheck($ID) >= $permneed){
83    return 'denied';
84  }
85
86  return $act;
87}
88
89/**
90 * Handle 'save'
91 *
92 * Checks for spam and conflicts and saves the page.
93 * Does a redirect to show the page afterwards or
94 * returns a new action.
95 *
96 * @author Andreas Gohr <andi@splitbrain.org>
97 */
98function act_save($act){
99  global $ID;
100  global $DATE;
101  global $PRE;
102  global $TEXT;
103  global $SUF;
104  global $SUM;
105
106  //spam check
107  if(checkwordblock())
108    return 'wordblock';
109  //conflict check //FIXME use INFO
110  if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE )
111    return 'conflict';
112
113  //save it
114  saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM); //use pretty mode for con
115  //unlock it
116  unlock($ID);
117
118  //show it
119  session_write_close();
120  header("Location: ".wl($ID,'',true));
121  exit();
122}
123
124/**
125 * Handle 'login', 'logout', 'register'
126 *
127 * @author Andreas Gohr <andi@splitbrain.org>
128 */
129function act_auth($act){
130  //already logged in?
131  if($_SERVER['REMOTE_USER'] && $act=='login')
132    return 'show';
133
134  //handle logout
135  if($act=='logout'){
136    auth_logoff();
137    return 'login';
138  }
139
140  //handle register
141  if($act=='register' && register()){
142    $act='login';
143  }
144
145  return $act;
146}
147
148/**
149 * Handle 'edit', 'preview'
150 *
151 * @author Andreas Gohr <andi@splitbrain.org>
152 */
153function act_edit($act){
154  //check if locked by anyone - if not lock for my self
155  $lockedby = checklock($ID);
156  if($lockedby) return 'locked';
157
158  lock($ID);
159  return $act;
160}
161
162/**
163 * Handle 'edit', 'preview'
164 *
165 * @author Andreas Gohr <andi@splitbrain.org>
166 */
167function act_export($act){
168  global $ID;
169  global $REV;
170
171  if($act == 'export_html'){
172    header('Content-Type: text/html; charset=utf-8');
173    ptln('<html>');
174    ptln('<head>');
175    tpl_metaheaders();
176    ptln('</head>');
177    ptln('<body>');
178    print parsedWiki($ID,$REV,false);
179    ptln('</body>');
180    ptln('</html>');
181    exit;
182  }
183
184  if($act == 'export_raw'){
185    header('Content-Type: text/plain; charset=utf-8');
186    print rawWiki($ID,$REV);
187    exit;
188  }
189
190  return 'show';
191}
192?>
193