This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaction.php
414 lines (387 loc) · 16.4 KB
/
action.php
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/**
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Danny Lin <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once (DOKU_PLUGIN . 'action.php');
require_once(DOKU_INC.'inc/fulltext.php');
class action_plugin_editx extends DokuWiki_Action_Plugin {
/**
* register the eventhandlers
*/
function register(Doku_Event_Handler $contr) {
$contr->register_hook('TPL_ACT_RENDER', 'AFTER', $this, '_append_to_edit', array());
$contr->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
$contr->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, '_handle_tpl_act', array());
}
/**
* main hooks
*/
function _append_to_edit(&$event, $param) {
global $ID;
if ($event->data != 'edit') return;
if (!$this->_auth_check_all($ID)) return;
$link = sprintf('<a href="%s" class="action editx" rel="nofollow">%s</a>', wl($ID,'do=editx'), $this->getLang('pagemanagement'));
$intro = $this->locale_xhtml('intro');
$intro = str_replace( '@LINK@', $link, $intro );
print $intro;
}
function _handle_act(&$event, $param) {
if($event->data != 'editx') return;
$event->preventDefault();
}
function _handle_tpl_act(&$event, $param) {
if($event->data != 'editx') return;
$event->preventDefault();
switch ($_REQUEST['work']) {
case 'rename':
$opts['oldpage'] = cleanID($_REQUEST['oldpage']);
$opts['newpage'] = cleanID($_REQUEST['newpage']);
$opts['summary'] = $_REQUEST['summary'];
$opts['rp_nr'] = $_REQUEST['rp_nr'];
$opts['confirm'] = $_REQUEST['rp_confirm'];
$this->_rename_page($opts);
break;
case 'delete':
$opts['oldpage'] = cleanID($_REQUEST['oldpage']);
$opts['summary'] = $_REQUEST['summary'];
$opts['purge'] = $_REQUEST['dp_purge'];
$opts['confirm'] = $_REQUEST['dp_confirm'];
$this->_delete_page($opts);
break;
default:
$this->_print_form();
break;
}
}
/**
* helper functions
*/
function _auth_check_list($list) {
global $conf;
global $USERINFO;
if(!$conf['useacl']) return true; //no ACL - then no checks
$allowed = explode(',',$list);
$allowed = array_map('trim', $allowed);
$allowed = array_unique($allowed);
$allowed = array_filter($allowed);
if(!count($allowed)) return true; //no restrictions
$user = $_SERVER['REMOTE_USER'];
$groups = (array) $USERINFO['grps'];
if(in_array($user,$allowed)) return true; //user explicitly mentioned
//check group memberships
foreach($groups as $group){
if(in_array('@'.$group,$allowed)) return true;
}
//still here? no access!
return false;
}
function _auth_check_all($id) {
return $this->_auth_can_rename($id) ||
$this->_auth_can_delete($id);
}
function _auth_can_rename($id) {
static $cache = null;
if (!$cache[$id]) {
$cache[$id] = auth_quickaclcheck($id)>=AUTH_EDIT &&
$this->_auth_check_list($this->getConf('user_rename'));
}
return $cache[$id];
}
function _auth_can_rename_nr($id) {
static $cache = null;
if (!$cache[$id]) {
$cache[$id] = auth_quickaclcheck($id)>=AUTH_DELETE &&
$this->_auth_check_list($this->getConf('user_rename_nr'));
}
return $cache[$id];
}
function _auth_can_delete($id) {
static $cache = null;
if (!$cache[$id]) {
$cache[$id] = auth_quickaclcheck($id)>=AUTH_DELETE &&
$this->_auth_check_list($this->getConf('user_delete'));
}
return $cache[$id];
}
function _locate_filepairs(&$opts, $dir, $regex ){
global $conf;
$oldpath = $conf[$dir].'/'.str_replace(':','/',$opts['oldns']);
$newpath = $conf[$dir].'/'.str_replace(':','/',$opts['newns']);
if (!$opts['oldfiles']) $opts['oldfiles'] = array();
if (!$opts['newfiles']) $opts['newfiles'] = array();
$dh = @opendir($oldpath);
if($dh) {
while(($file = readdir($dh)) !== false){
if ($file{0}=='.') continue;
$oldfile = $oldpath.$file;
if (is_file($oldfile) && preg_match($regex,$file)){
$opts['oldfiles'][] = $oldfile;
if ($opts['move']) {
$newfilebase = str_replace($opts['oldname'], $opts['newname'], $file);
$newfile = $newpath.$newfilebase;
if (@file_exists($newfile)) {
$this->errors[] = sprintf( $this->getLang('rp_msg_file_conflict'), '<a href="'. wl($opts['newpage']) . '">'.$opts['newpage'].'</a>', $newfilebase );
return false;
}
$opts['newfiles'][] = $newfile;
}
}
}
closedir($dh);
return true;
}
return false;
}
function _apply_moves(&$opts) {
foreach ($opts['oldfiles'] as $i => $oldfile) {
$newfile = $opts['newfiles'][$i];
$newdir = dirname($newfile);
if (!io_mkdir_p($newdir)) continue;
io_rename($oldfile, $newfile);
}
}
function _apply_deletes(&$opts) {
foreach ($opts['oldfiles'] as $oldfile) {
unlink($oldfile);
}
}
function _FN($id) {
$id = str_replace(':','/',$id);
$id = utf8_encodeFN($id);
return $id;
}
function _custom_delete_page($id, $summary) {
global $ID, $INFO, $conf;
// mark as nonexist to prevent indexerWebBug
if ($id==$ID) $INFO['exists'] = 0;
// delete page, meta and attic
$file = wikiFN($id);
$old = @filemtime($file); // from page
if (file_exists($file)) unlink($file);
$opts['oldname'] = $this->_FN(noNS($id));
$opts['oldns'] = $this->_FN(getNS($id));
if ($opts['oldns']) $opts['oldns'] .= '/';
$this->_locate_filepairs( $opts, 'metadir', '/^'.$opts['oldname'].'\.(?!mlist)\w*?$/' );
$this->_locate_filepairs( $opts, 'olddir', '/^'.$opts['oldname'].'\.\d{10}\.txt(\.gz|\.bz2)?$/' );
$this->_apply_deletes($opts);
io_sweepNS($id, 'datadir');
io_sweepNS($id, 'metadir');
io_sweepNS($id, 'olddir');
// send notify mails
notify($id,'admin',$old,$summary);
notify($id,'subscribers',$old,$summary);
// update the purgefile (timestamp of the last time anything within the wiki was changed)
io_saveFile($conf['cachedir'].'/purgefile',time());
// if useheading is enabled, purge the cache of all linking pages
if(useHeading('content')){
$pages = ft_backlinks($id);
foreach ($pages as $page) {
$cache = new cache_renderer($page, wikiFN($page), 'xhtml');
$cache->removeCache();
}
}
}
/**
* main functions
*/
function _rename_page(&$opts) {
// check confirmation
if (!$opts['confirm']) {
$this->errors[] = $this->getLang('rp_msg_unconfirmed');
}
// check old page
if (!$opts['oldpage']) {
$this->errors[] = $this->getLang('rp_msg_old_empty');
} else if (!page_exists($opts['oldpage'])) {
$this->errors[] = sprintf( $this->getLang('rp_msg_old_noexist'), $opts['oldpage'] );
} else if (!$this->_auth_can_rename($opts['oldpage'])) {
$this->errors[] = sprintf( $this->getLang('rp_msg_auth'), $opts['oldpage'] );
} else if (checklock($opts['oldpage'])) {
$this->errors[] = sprintf( $this->getLang('rp_msg_locked'), $opts['oldpage'] );
}
// check noredirect
if ($opts['rp_nr'] && !$this->_auth_can_rename_nr($opts['oldpage']))
$this->errors[] = $this->getLang('rp_msg_auth_nr');
// check new page
if (!$opts['newpage']) {
$this->errors[] = $this->getLang('rp_msg_new_empty');
} else if (page_exists($opts['newpage'])) {
$this->errors[] = sprintf( $this->getLang('rp_msg_new_exist'), '<a href="'. wl($opts['newpage']) . '">'.$opts['newpage'].'</a>' );
} else if (!$this->_auth_can_rename($opts['newpage'])) {
$this->errors[] = sprintf( $this->getLang('rp_msg_auth'), $opts['newpage'] );
} else if (checklock($opts['newpage'])) {
$this->errors[] = sprintf( $this->getLang('rp_msg_locked'), $opts['newpage'] );
}
// try to locate moves
if (!$this->errors) {
$opts['move'] = true;
$opts['oldname'] = $this->_FN(noNS($opts['oldpage']));
$opts['newname'] = $this->_FN(noNS($opts['newpage']));
$opts['oldns'] = $this->_FN(getNS($opts['oldpage']));
$opts['newns'] = $this->_FN(getNS($opts['newpage']));
if ($opts['oldns']) $opts['oldns'] .= '/';
if ($opts['newns']) $opts['newns'] .= '/';
$this->_locate_filepairs( $opts, 'metadir', '/^'.$opts['oldname'].'\.(?!mlist|meta|indexed)\w*?$/' );
$this->_locate_filepairs( $opts, 'olddir', '/^'.$opts['oldname'].'\.\d{10}\.txt(\.gz|\.bz2)?$/' );
}
// if no error do rename
if (!$this->errors) {
// move meta and attic
$this->_apply_moves($opts);
// save to newpage
$text = rawWiki($opts['oldpage']);
if ($opts['summary'])
$summary = sprintf( $this->getLang('rp_newsummaryx'), $opts['oldpage'], $opts['newpage'], $opts['summary'] );
else
$summary = sprintf( $this->getLang('rp_newsummary'), $opts['oldpage'], $opts['newpage'] );
saveWikiText($opts['newpage'],$text,$summary);
// purge or recreate old page
$summary = $opts['summary'] ?
sprintf( $this->getLang('rp_oldsummaryx'), $opts['oldpage'], $opts['newpage'], $opts['summary'] ) :
sprintf( $this->getLang('rp_oldsummary'), $opts['oldpage'], $opts['newpage'] );
if ($opts['rp_nr']) {
$this->_custom_delete_page( $opts['oldpage'], $summary );
// write change log afterwards, or it would be deleted
addLogEntry( null, $opts['oldpage'], DOKU_CHANGE_TYPE_DELETE, $summary ); // also writes to global changes
unlink(metaFN($opts['oldpage'],'.changes')); // purge page changes
}
else {
$text = $this->getConf('redirecttext');
if (!$text) $text = $this->getLang('redirecttext');
$text = str_replace( '@ID@', $opts['newpage'], $text );
@unlink(wikiFN($opts['oldpage'])); // remove old page file so no additional history
saveWikiText($opts['oldpage'],$text,$summary);
}
}
// show messages
if ($this->errors) {
foreach ($this->errors as $error) msg( $error, -1 );
}
else {
$msg = sprintf( $this->getLang('rp_msg_success'), $opts['oldpage'], '<a href="'. wl($opts['newpage']) . '">'.$opts['newpage'].'</a>' );
msg( $msg, 1 );
}
// display form and table
$data = array( rp_newpage => $opts['newpage'], rp_summary => $opts['summary'], rp_nr => $opts['rp_nr'] );
$this->_print_form($data);
}
function _delete_page(&$opts) {
// check confirm
if (!$opts['confirm']) {
$this->errors[] = $this->getLang('dp_msg_unconfirmed');
}
// check old page
if (!$opts['oldpage']) {
$this->errors[] = $this->getLang('dp_msg_old_empty');
} else if (!$this->_auth_can_delete($opts['oldpage'])) {
$this->errors[] = sprintf( $this->getLang('dp_msg_auth'), $opts['oldpage'] );
}
// if no error do delete
if (!$this->errors) {
$summary = $opts['summary'] ?
sprintf( $this->getLang('dp_oldsummaryx'), $opts['summary'] ) :
$this->getLang('dp_oldsummary');
$this->_custom_delete_page( $opts['oldpage'], $summary );
// write change log afterwards, or it would be deleted
addLogEntry( null, $opts['oldpage'], DOKU_CHANGE_TYPE_DELETE, $summary ); // also writes to global changes
if ($opts['purge']) unlink(metaFN($opts['oldpage'],'.changes')); // purge page changes
}
// show messages
if ($this->errors) {
foreach ($this->errors as $error) msg( $error, -1 );
}
else {
$msg = sprintf( $this->getLang('dp_msg_success'), $opts['oldpage'] );
msg( $msg, 1 );
}
// display form and table
$data = array( dp_purge => $opts['purge'], dp_summary => $opts['summary'] );
$this->_print_form($data);
}
function _print_form($data=null) {
global $ID, $lang;
$chk = ' checked="checked"';
?>
<h1><?php echo sprintf( $this->getLang('title'), $ID); ?></h1>
<div id="config__manager">
<?php
if ($this->_auth_can_rename($ID)) {
?>
<form action="<?php echo wl($ID); ?>" method="post">
<fieldset>
<legend><?php echo $this->getLang('rp_title'); ?></legend>
<input type="hidden" name="do" value="editx" />
<input type="hidden" name="work" value="rename" />
<input type="hidden" name="oldpage" value="<?php echo $ID; ?>" />
<table class="inline">
<tr>
<td class="label"><?php echo $this->getLang('rp_newpage'); ?></td>
<td class="value"><input class="edit" type="input" name="newpage" value="<?php echo $data['rp_newpage']; ?>" /></td>
</tr>
<tr>
<td class="label"><?php echo $this->getLang('rp_summary'); ?></td>
<td class="value"><input class="edit" type="input" name="summary" value="<?php echo $data['rp_summary']; ?>" /></td>
</tr>
<?php
if ($this->_auth_can_rename_nr($ID)) {
?>
<tr>
<td class="label"><?php echo $this->getLang('rp_nr'); ?></td>
<td class="value"><input type="checkbox" name="rp_nr" value="1"<?php if ($data['rp_nr']) echo $chk; ?> /></td>
</tr>
<?php
}
?>
<tr>
<td class="label"><?php echo $this->getLang('rp_confirm'); ?></td>
<td class="value"><input type="checkbox" name="rp_confirm" value="1" /></td>
</tr>
</table>
<p>
<input type="submit" class="button" value="<?php echo $lang['btn_save']; ?>" />
<input type="reset" class="button" value="<?php echo $lang['btn_reset']; ?>" />
</p>
</fieldset>
</form>
<?php
}
if ($this->_auth_can_delete($ID)) {
?>
<form action="<?php echo wl($ID); ?>" method="post">
<fieldset>
<legend><?php echo $this->getLang('dp_title'); ?></legend>
<input type="hidden" name="do" value="editx" />
<input type="hidden" name="work" value="delete" />
<input type="hidden" name="oldpage" value="<?php echo $ID; ?>" />
<table class="inline">
<tr>
<td class="label"><?php echo $this->getLang('dp_summary'); ?></td>
<td class="value"><input class="edit" type="input" name="summary" value="<?php echo $data['dp_summary']; ?>" /></td>
</tr>
<tr>
<td class="label"><?php echo $this->getLang('dp_purge'); ?></td>
<td class="value"><input type="checkbox" name="dp_purge" value="1"<?php if ($data['dp_purge']) echo $chk; ?> /></td>
</tr>
<tr>
<td class="label"><?php echo $this->getLang('dp_confirm'); ?></td>
<td class="value"><input type="checkbox" name="dp_confirm" value="1" /></td>
</tr>
</table>
<p>
<input type="submit" class="button" value="<?php echo $lang['btn_save']; ?>" />
<input type="reset" class="button" value="<?php echo $lang['btn_reset']; ?>" />
</p>
</fieldset>
</form>
<?php
}
?>
</div>
<?php
}
}
// vim:ts=4:sw=4:et:enc=utf-8: