summaryrefslogtreecommitdiff
path: root/inc/actions.php
blob: ce201e60e41018344c3e4c14079de2574f602108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
 * DokuWiki Actions
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 */

  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
  require_once(DOKU_INC.'inc/template.php');


/**
 * Call the needed action handlers
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function act_dispatch(){
  global $INFO;
  global $ACT;
  global $ID;
  global $QUERY;
  global $lang;
  global $conf;

  //sanitize $ACT
  $ACT = act_clean($ACT);

  //check if searchword was given - else just show
  $s = cleanID($QUERY);
  if($ACT == 'search' && empty($s)){
    $ACT = 'show';
  }

  //login stuff
  if(in_array($ACT,array('login','logout')))
    $ACT = act_auth($ACT);

  //check if user is asking to (un)subscribe a page
  if($ACT == 'subscribe' || $ACT == 'unsubscribe')
    $ACT = act_subscription($ACT);

  //check permissions
  $ACT = act_permcheck($ACT);

  //register
  if($ACT == 'register' && register()){
    $ACT = 'login';
  }

  if ($ACT == 'resendpwd' && act_resendpwd()) {
    $ACT = 'login';
  }

  //update user profile
  if (($ACT == 'profile') && updateprofile()) {
    msg($lang['profchanged'],1);
    $ACT = 'show';
  }

  //save
  if($ACT == 'save')
    $ACT = act_save($ACT);

  //edit
  if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){
    $ACT = act_edit($ACT);
  }else{
    unlock($ID); //try to unlock
  }

  //handle export
  if(substr($ACT,0,7) == 'export_')
    $ACT = act_export($ACT);

  //display some infos
  if($ACT == 'check'){
    check();
    $ACT = 'show';
  }

  //handle admin tasks
  if($ACT == 'admin'){
    // retrieve admin plugin name from $_REQUEST['page']
    if ($_REQUEST['page']) {
        $pluginlist = plugin_list('admin');
        if (in_array($_REQUEST['page'], $pluginlist)) {
          // attempt to load the plugin
          if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== NULL)
              $plugin->handle();
        }
    }
/*
        if($_REQUEST['page'] == 'acl'){
            require_once(DOKU_INC.'inc/admin_acl.php');
            admin_acl_handler();
    }
*/
  }

  //call template FIXME: all needed vars available?
  header('Content-Type: text/html; charset=utf-8');
  include(template('main.php'));
  // output for the commands is now handled in inc/templates.php
  // in function tpl_content()
}

/**
 * Sanitize the action command
 *
 * Add all allowed commands here.
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function act_clean($act){
  global $lang;

  //handle localized buttons
  if($act == $lang['btn_save']) $act = 'save';
  if($act == $lang['btn_preview']) $act = 'preview';
  if($act == $lang['btn_cancel']) $act = 'show';

  //remove all bad chars
  $act = strtolower($act);
  $act = preg_replace('/[^a-z_]+/','',$act);

  if($act == 'export_html') $act = 'export_xhtml';
  if($act == 'export_htmlbody') $act = 'export_xhtmlbody';

  if(array_search($act,array('login','logout','register','save','edit',
                             'preview','search','show','check','index','revisions',
                             'diff','recent','backlink','admin','subscribe',
                             'unsubscribe','profile','resendpwd',)) === false
     && substr($act,0,7) != 'export_' ) {
    msg('Unknown command: '.htmlspecialchars($act),-1);
    return 'show';
  }
  return $act;
}

/**
 * Run permissionchecks
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function act_permcheck($act){
  global $INFO;
  global $conf;

  if(in_array($act,array('save','preview','edit'))){
    if($INFO['exists']){
      if($act == 'edit'){
        //the edit function will check again and do a source show
        //when no AUTH_EDIT available
        $permneed = AUTH_READ;
      }else{
        $permneed = AUTH_EDIT;
      }
    }else{
      $permneed = AUTH_CREATE;
    }
  }elseif(in_array($act,array('login','search','recent','profile'))){
    $permneed = AUTH_NONE;
  }elseif($act == 'register'){
    if ($conf['openregister']){
      $permneed = AUTH_NONE;
    }else{
      $permneed = AUTH_ADMIN;
    }
  }elseif($act == 'admin'){
    $permneed = AUTH_ADMIN;
  }else{
    $permneed = AUTH_READ;
  }
  if($INFO['perm'] >= $permneed) return $act;

  return 'denied';
}

/**
 * Handle 'save'
 *
 * Checks for spam and conflicts and saves the page.
 * Does a redirect to show the page afterwards or
 * returns a new action.
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function act_save($act){
  global $ID;
  global $DATE;
  global $PRE;
  global $TEXT;
  global $SUF;
  global $SUM;

  //spam check
  if(checkwordblock())
    return 'wordblock';
  //conflict check //FIXME use INFO
  if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE )
    return 'conflict';

  //save it
  saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con
  //unlock it
  unlock($ID);

  //show it
  session_write_close();
  header("Location: ".wl($ID,'',true));
  exit();
}

/**
 * Handle 'login', 'logout'
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function act_auth($act){
  global $ID;
  global $INFO;

  //already logged in?
  if($_SERVER['REMOTE_USER'] && $act=='login')
    return 'show';

  //handle logout
  if($act=='logout'){
    $lockedby = checklock($ID); //page still locked?
    if($lockedby == $_SERVER['REMOTE_USER'])
      unlock($ID); //try to unlock

    // do the logout stuff
    auth_logoff();

    // rebuild info array
    $INFO = pageinfo();

    return 'login';
  }

  return $act;
}

/**
 * Handle 'edit', 'preview'
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function act_edit($act){
  global $ID;

  //check if locked by anyone - if not lock for my self
  $lockedby = checklock($ID);
  if($lockedby) return 'locked';

  lock($ID);
  return $act;
}

/**
 * Handle 'edit', 'preview'
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function act_export($act){
  global $ID;
  global $REV;

  // no renderer for this
  if($act == 'export_raw'){
    header('Content-Type: text/plain; charset=utf-8');
    print rawWiki($ID,$REV);
    exit;
  }

  // html export #FIXME what about the template's style?
  if($act == 'export_xhtml'){
    header('Content-Type: text/html; charset=utf-8');
    ptln('<html>');
    ptln('<head>');
    tpl_metaheaders();
    ptln('</head>');
    ptln('<body>');
    ptln('<div class="dokuwiki">');
    print p_wiki_xhtml($ID,$REV,false);
    ptln('</div>');
    ptln('</body>');
    ptln('</html>');
    exit;
  }

  // html body only
  if($act == 'export_xhtmlbody'){
    print p_wiki_xhtml($ID,$REV,false);
    exit;
  }

  // try to run renderer #FIXME use cached instructions
  $mode = substr($act,7);
  $text = p_render($mode,p_get_instructions(rawWiki($ID,$REV)),$info);
  if(!is_null($text)){
    print $text;
    exit;
  }



  return 'show';
}

/**
 * Handle 'subscribe', 'unsubscribe'
 *
 * @author Steven Danz <steven-danz@kc.rr.com>
 * @todo   localize
 */
function act_subscription($act){
  global $ID;
  global $INFO;
  global $lang;

  $file=metaFN($ID,'.mlist');
  if ($act=='subscribe' && !$INFO['subscribed']){
    if ($INFO['userinfo']['mail']){
      if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) {
        $INFO['subscribed'] = true;
        msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1);
      } else {
        msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1);
      }
    } else {
      msg($lang['subscribe_noaddress']);
    }
  } elseif ($act=='unsubscribe' && $INFO['subscribed']){
    if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) {
      $INFO['subscribed'] = false;
      msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1);
    } else {
      msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1);
    }
  }

  return 'show';
}

//Setup VIM: ex: et ts=2 enc=utf-8 :