-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
400 lines (335 loc) · 10.7 KB
/
admin.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
<?php
/**
* DokuWiki Plugin dokutranslate (Admin Component)
* Based on built-in ACL plugin
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Martin Doucha <[email protected]>
* @author Andreas Gohr <[email protected]>
* @author Anika Henke <[email protected]> (concepts)
* @author Frank Schubert <[email protected]> (old version)
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'admin.php';
require_once 'utils.php';
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_dokutranslate extends DokuWiki_Admin_Plugin {
var $acl = null;
var $ns = null;
/**
* return prompt for admin menu
*/
function getMenuText($language) {
return $this->getLang('admin_dokutranslate');
}
/**
* return sort order for position in admin menu
*/
function getMenuSort() {
return 1;
}
/**
* handle user request
*
* Initializes internal vars and handles modifications
*
* @author Andreas Gohr <[email protected]>
*/
function handle() {
global $AUTH_ACL;
global $ID;
global $auth;
// fresh 1:1 copy without replacements
$AUTH_ACL = loadModlist();
// namespace given?
if(empty($_REQUEST['ns']) || $_REQUEST['ns'] == '*'){
$this->ns = '*';
} else {
$this->ns = cleanID($_REQUEST['ns']);
}
// handle modifications
if (isset($_REQUEST['cmd']) && checkSecurityToken()) {
// scope for modifications
if($this->ns == '*'){
$scope = '*';
}else{
$scope = $this->ns.':*';
}
if (isset($_REQUEST['cmd']['save']) && $scope && isset($_REQUEST['modgroup'])) {
// handle additions or single modifications
$this->_acl_del($scope);
$this->_acl_add($scope, trim($_REQUEST['modgroup']));
} elseif (isset($_REQUEST['cmd']['del']) && $scope) {
// handle single deletions
$this->_acl_del($scope);
} elseif (isset($_REQUEST['cmd']['update'])) {
// handle update of the whole file
foreach((array) $_REQUEST['del'] as $where){
// remove all rules marked for deletion
unset($_REQUEST['acl'][$where]);
}
// prepare lines
$lines = array();
// keep header
foreach($AUTH_ACL as $line){
if($line{0} == '#'){
$lines[] = $line;
}else{
break;
}
}
foreach((array) $_REQUEST['acl'] as $where => $who){
$who = $auth->cleanGroup($who);
$who = auth_nameencode($who,true);
$lines[] = "$where\t$who\n";
}
// save it
io_saveFile(DOKUTRANSLATE_MODLIST, join('',$lines));
}
// reload ACL config
$AUTH_ACL = loadModlist();
}
// initialize ACL array
$this->_init_acl_config();
}
/**
* ACL Output function
*
* print a table with all significant permissions for the
* current id
*
* @author Frank Schubert <[email protected]>
* @author Andreas Gohr <[email protected]>
*/
function html() {
global $ID;
echo '<div id="dokutranslate_manager">'.NL;
echo '<h1>'.$this->getLang('admin_dokutranslate').'</h1>'.NL;
echo '<div class="level1">'.NL;
echo '<div id="dokutranslate__tree">'.NL;
$this->_html_explorer();
echo '</div>'.NL;
echo '<div id="dokutranslate__detail">'.NL;
$this->_html_detail();
echo '</div>'.NL;
echo '</div>'.NL;
echo '<div class="clearer"></div>';
echo '<h2>'.$this->getLang('current').'</h2>'.NL;
echo '<div class="level2">'.NL;
$this->_html_table();
echo '</div>'.NL;
echo '</div>'.NL;
}
/**
* returns array with set options for building links
*
* @author Andreas Gohr <[email protected]>
*/
function _get_opts($addopts=null){
global $ID;
$opts = array(
'do'=>'admin',
'page'=>'dokutranslate',
);
if($this->ns) $opts['ns'] = $this->ns;
if(is_null($addopts)) return $opts;
return array_merge($opts, $addopts);
}
/**
* Display a tree menu to select a page or namespace
*
* @author Andreas Gohr <[email protected]>
*/
function _html_explorer(){
global $conf;
global $ID;
global $lang;
$dir = $conf['datadir'];
$ns = $this->ns;
if(empty($ns)){
$ns = dirname(str_replace(':','/',$ID));
if($ns == '.') $ns ='';
}elseif($ns == '*'){
$ns ='';
}
$ns = utf8_encodeFN(str_replace(':','/',$ns));
$data = $this->_get_tree($ns);
// wrap a list with the root level around the other namespaces
array_unshift($data, array( 'level' => 0, 'id' => '*', 'type' => 'd',
'open' =>'true', 'label' => '['.$lang['mediaroot'].']'));
echo html_buildlist($data,'dokutranslate',
array($this,'_html_list_acl'),
array($this,'_html_li_acl'));
}
/**
* get a combined list of media and page files
*
* @param string $folder an already converted filesystem folder of the current namespace
* @param string $limit limit the search to this folder
*/
function _get_tree($folder,$limit=''){
global $conf;
// read tree structure from pages and media
$data = array();
search($data,$conf['datadir'],'search_index',array('ns' => $folder),$limit);
# Filter out pages, leave only namespaces
$count = count($data);
for ($i = 0; $i < $count; $i++) {
if ($data[$i]['type'] != 'd') {
unset($data[$i]);
}
}
return $data;
}
/**
* Display the current ACL for selected where/who combination with
* selectors and modification form
*
* @author Andreas Gohr <[email protected]>
*/
function _html_detail(){
echo '<form action="'.wl().'" method="post" accept-charset="utf-8"><div class="no">'.NL;
echo '<div id="dokutranslate__user">';
$this->_html_modform();
echo '</div>'.NL;
echo '<input type="hidden" name="ns" value="'.hsc($this->ns).'" />'.NL;
echo '<input type="hidden" name="do" value="admin" />'.NL;
echo '<input type="hidden" name="page" value="dokutranslate" />'.NL;
echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'.NL;
echo '</div></form>'.NL;
}
function _html_modform() {
global $lang;
$ns = $this->ns . ($this->ns == '*' ? '' : ':*');
echo $this->getLang('modgroup').' ';
echo '<input type="text" name="modgroup" class="edit" value="'.hsc(isset($this->acl[$ns]) ? $this->acl[$ns] : '').'" />'.NL;
if(!isset($this->acl[$ns])){
echo '<input type="submit" name="cmd[save]" class="button" value="'.$lang['btn_save'].'" />'.NL;
}else{
echo '<input type="submit" name="cmd[save]" class="button" value="'.$lang['btn_update'].'" />'.NL;
echo '<input type="submit" name="cmd[del]" class="button" value="'.$lang['btn_delete'].'" />'.NL;
}
}
/**
* Item formatter for the tree view
*
* User function for html_buildlist()
*
* @author Andreas Gohr <[email protected]>
*/
function _html_list_acl($item){
global $ID;
$ret = '';
// what to display
if($item['label']){
$base = $item['label'];
}else{
$base = ':'.$item['id'];
$base = substr($base,strrpos($base,':')+1);
}
// highlight?
if ($item['id'] == $this->ns) {
$cl = ' cur';
}
// namespace or page?
if($item['open']){
$img = DOKU_BASE.'lib/images/minus.gif';
$alt = '−';
}else{
$img = DOKU_BASE.'lib/images/plus.gif';
$alt = '+';
}
$ret .= '<img src="'.$img.'" alt="'.$alt.'" />';
$ret .= '<a href="'.wl('',$this->_get_opts(array('ns'=>$item['id'],'sectok'=>getSecurityToken()))).'" class="idx_dir'.$cl.'">';
$ret .= $base;
$ret .= '</a>';
return $ret;
}
function _html_li_acl($item){
return '<li class="level' . $item['level'] . ' ' .
($item['open'] ? 'open' : 'closed') . '">';
}
/**
* Get current ACL settings as multidim array
*
* @author Andreas Gohr <[email protected]>
*/
function _init_acl_config(){
global $AUTH_ACL;
$this->acl = parseModlist($AUTH_ACL);
}
/**
* Display all currently set permissions in a table
*
* @author Andreas Gohr <[email protected]>
*/
function _html_table(){
global $lang;
global $ID;
echo '<form action="'.wl().'" method="post" accept-charset="utf-8"><div class="no">'.NL;
if($this->ns){
echo '<input type="hidden" name="ns" value="'.hsc($this->ns).'" />'.NL;
}
echo '<input type="hidden" name="do" value="admin" />'.NL;
echo '<input type="hidden" name="page" value="dokutranslate" />'.NL;
echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'.NL;
echo '<table class="inline">';
echo '<tr>';
echo '<th>'.$this->getLang('where').'</th>';
echo '<th>'.$this->getLang('who').'</th>';
echo '<th>'.$lang['btn_delete'].'</th>';
echo '</tr>';
foreach($this->acl as $where => $who){
echo '<tr>';
echo '<td>';
echo '<span class="dokutranslatens">'.hsc($where).'</span>';
echo '</td>';
echo '<td>';
echo '<span class="dokutranslategroup">'.hsc($who).'</span>';
echo '</td>';
echo '<td align="center">';
echo '<input type="hidden" name="acl['.hsc($where).']" value="'.hsc($who).'" />';
echo '<input type="checkbox" name="del[]" value="'.hsc($where).'" />';
echo '</td>';
echo '</tr>';
}
echo '<tr>';
echo '<th align="right" colspan="3">';
echo '<input type="submit" value="'.$this->getLang('delsel').'" name="cmd[update]" class="button" />';
echo '</th>';
echo '</tr>';
echo '</table>';
echo '</div></form>'.NL;
}
/**
* adds new acl-entry to conf/acl.auth.php
*
* @author Frank Schubert <[email protected]>
*/
function _acl_add($acl_scope, $acl_user){
$acl_config = loadModlist();
$acl_user = auth_nameencode($acl_user,true);
$new_acl = "$acl_scope\t$acl_user\n";
$acl_config[] = $new_acl;
return io_saveFile(DOKUTRANSLATE_MODLIST, join('',$acl_config));
}
/**
* remove acl-entry from conf/acl.auth.php
*
* @author Frank Schubert <[email protected]>
*/
function _acl_del($acl_scope){
$acl_config = loadModlist();
$acl_pattern = '^'.preg_quote($acl_scope,'/').'\s+.*$';
// save all non!-matching
$new_config = preg_grep("/$acl_pattern/", $acl_config, PREG_GREP_INVERT);
return io_saveFile(DOKUTRANSLATE_MODLIST, join('',$new_config));
}
}