-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.php
452 lines (363 loc) · 12.5 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
//error_reporting(E_ERROR | E_WARNING | E_PARSE);
/**
* Proof of concept plugin for simple per page permissions
* in dokuwiki
*
* @license WTFPL 2 (http://sam.zoy.org/wtfpl/)
* @author Robert Mcleod <[email protected]>
* @modified Bhavic Patel <[email protected]>
*
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC'))
die();
/**
* All DokuWiki plugins to interfere with the event system
* need to inherit from this class
*/
class action_plugin_simpleperms extends DokuWiki_Action_Plugin
{
const META_NAME = "simpleperms";
const LEVEL_PUBLIC_RW = 1;
const LEVEL_PUBLIC_R = 0;
const LEVEL_PRIVATE = -1;
/**
* Registers a callback function for a given event
*/
function register(&$controller)
{
// Add hooks
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess', array());
$controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_tpl_content_display', array());
$controller->register_hook('HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'handle_html_editform_output', array());
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown', array());
}
function handle_action_act_preprocess(&$event, $param) {
global $ACT;
switch ( $ACT )
{
case "edit":
$this->_set_ownership();
break;
default:
return;
}
}
function handle_tpl_content_display(&$event, $param) {
global $ACT;
global $ID;
switch ( $ACT )
{
case "show":
if ( !$this->_page_exists() )
return;
if ( !$this->_user_can_read() )
$this->hide_page($event);
if ( !$this->_user_can_edit() )
$this->hide_edit_button();
// This line is handy for troubleshooting permissions errors
// msg( print_r( p_get_metadata( $ID, self::META_NAME ), 1 ) );
break;
case "edit":
if ( $this->_page_exists() && !$this->_user_can_edit() )
$this->hide_page($event);
default:
return;
}
}
function handle_html_editform_output( &$event, $param ) {
// No globals in dokuwiki-ajax-land
global $ID;
$JSINFO["id"] = $ID;
$this->insert_dropdown( $event ); // this is always edit
// This line is handy for troubleshooting permissions errors
// msg( print_r( $this->_get_simpleperm_metadata(), 1 ) );
}
function handle_ajax_call_unknown( &$event, $param ) {
switch ( $event->data )
{
case "update.level":
$this->apply_permissions();
$event->preventDefault();
$event->stopPropagation();
break;
default:
return;
}
}
/**
* Insert the select element into the page
* Added checks
*/
function insert_dropdown(&$event)
{
// don't add perms select if not owner
if ( !$this->_user_is_owner() )
return;
$pos = $event->data->findElementByAttribute('class', 'summary');
$dropdown = $this->_generate_permissions_dropdown();
$event->data->insertElement($pos++, $dropdown);
}
function _set_ownership() {
if ( $this->_page_exists() )
return;
// make the user the owner before they even save the page!
$this->_add_simpleperm_metadata();
}
/**
* Adds the simpleperm metadata to the page
* Ensures only the author can do this
*/
function apply_permissions()
{
$id = cleanID($_POST["id"]);
$json = new StdClass;
$json->error = 0;
try {
// TODO: Make this check the owner
// - currently the dropdown will be hidden from non-owners but
// that won't stop people posting directly to this script to
// unlock a page
// - the ajax call apparently doesn't have access to $_SESSION
// from which to determine the user... no access to $INFO
// global either... wtf...?
// Don't let anyone else set permissions
// if ( !$this->_user_is_owner() )
// throw new Exception("You don't own this page");
// Check if the simpleperm value was given in the request
if ( !isset( $_POST['level'] ) )
throw new Exception("Permission data not set");
$json->id = $id;
// get the metadata and make it output with the json
$meta = p_get_metadata( $id, self::META_NAME);
$json->old_level = $meta["level"];
$json->old_owner = $meta["owner"];
// Set the meta key
$meta["level"] = $_POST["level"];
// Save the metadata
if ( !p_set_metadata( $id, array(self::META_NAME => $meta) ) )
throw new Exception("Saving metadata failed");
// switch ( $_POST['level'] )
// {
// case self::LEVEL_PRIVATE:
// $this->_make_page_private();
// break;
// case self::LEVEL_PUBLIC_R:
// $this->_make_page_public_r();
// break;
// case self::LEVEL_PUBLIC_RW:
// $this->_make_page_public_rw();
// break;
// default:
// throw new Exception("Could not determine the permission required");
// break;
// }
// Get the metadata to check it saved
$new_meta = p_get_metadata( $id, self::META_NAME );
$json->level = $new_meta["level"];
$json->owner = $new_meta["owner"];
// did it save?
if ( $_POST["level"] != $new_meta["level"] )
throw new Exception("The level was not set for $id... {$_POST["level"]} was given but the level is at {$data["level"]}");
} catch ( Exception $e ) {
// ...no :(
$json->error = 1;
$json->message = "There was a problem setting the permissions: ".$e->getMessage();
}
echo json_encode($json);
}
/**
* Doesn't allow the page to be viewed if its private
*/
function hide_page(&$event)
{
$event->preventDefault();
echo <<<EOF
<h1 class="sectionedit1"><a name="this_topic_does_not_exist_yet" id="this_topic_does_not_exist_yet">This topic does not exist yet</a></h1>
<div class="level1">
<p>
You've followed a link to a topic that doesn't exist yet. If permissions allow, you may create it by using the <code>Create this page</code> button.
</p>
</div>
EOF;
}
/**
* Hides the edit button if the user only has read perms
*/
function hide_edit_button()
{
$out = <<<EOF
<script>
jQuery(document).ready(function(){
jQuery('.edit').parent("li").remove();
});
</script>
EOF;
echo $out;
}
/**
* @return the name of the user
*/
function _get_user()
{
$session = reset( $_SESSION ); // gives the first element of the array
$user = $session["auth"]["user"];
return ( is_null( $user ) ) ? "unknown" : $user;
}
/**
* @return true if the user has read access
*/
function _user_can_read()
{
if ( $this->_user_is_admin() && !$this->_page_has_owner() )
return true;
if ( $this->_user_is_owner() )
return true;
if ($this->_public_can_edit())
return true;
if ( $this->_public_can_read() )
return true;
return false;
}
/**
* @return true if user can edit
*/
function _user_can_edit()
{
if ( $this->_user_is_admin() && !$this->_page_has_owner() )
return true;
if ($this->_user_is_owner())
return true;
if ($this->_public_can_edit())
return true;
return false;
}
/**
* @return true if public can edit
*/
function _public_can_edit()
{
$data = $this->_get_simpleperm_metadata();
return $data["level"] == self::LEVEL_PUBLIC_RW;
}
/**
* @return true if public can read
*/
function _public_can_read()
{
$data = $this->_get_simpleperm_metadata();
return $data["level"] == self::LEVEL_PUBLIC_R;
}
/**
* @return true if private
*/
function _private()
{
$data = $this->_get_simpleperm_metadata();
return $data["level"] == self::LEVEL_PRIVATE;
}
/**
* @return true if the current user is creator
*/
function _user_is_owner()
{
$data = $this->_get_simpleperm_metadata();
return $data["owner"] == $this->_get_user();
}
function _make_user_owner()
{
if ( $this->_set_simpleperm_metadata( array( "owner" => $this->_get_user() ) ) !== true )
throw new Exception("Failed to make ".$this->_get_user()." owner of the page");
}
/**
* @return true if user is admin
*/
function _user_is_admin()
{
return $this->_get_user() == "admin";
}
/**
* @return true if page exists
*/
function _page_exists()
{
global $INFO;
return $INFO['exists'];
}
function _page_has_owner()
{
$data = $this->_get_simpleperm_metadata();
return !is_null( $data["owner"] );
}
/**
* @return the html for the dropdown permissions selection
*/
function _generate_permissions_dropdown()
{
$data = $this->_get_simpleperm_metadata();
$level = $data["level"];
// make private selected by default
list( $p, $pr, $prw ) = array( " selected", "", "" );
// Only check the permissions if the page exists
if ( $this->_page_exists() ) {
$p = ( $level == self::LEVEL_PRIVATE ) ? " selected" : "";
$pr = ( $level == self::LEVEL_PUBLIC_R ) ? " selected" : "";
$prw = ( $level == self::LEVEL_PUBLIC_RW ) ? " selected" : "";
}
// Make the dropdown
$out = <<<EOF
<div class="summary" style="margin-right: 10px;">
<span>Permissions: <select name="level" id="permission">
<option value="-1"$p>Private</option>
<option value="0"$pr>Publicly Readable</option>
<option value="1"$prw>Publicly Editable</option>
</select></span>
</div>
EOF;
return $out;
}
function _make_page_public_r()
{
if ( $this->_set_simpleperm_metadata( array("level" => self::LEVEL_PUBLIC_R ) ) !== true )
throw new Exception( "Failed to make this page publicly readable" );
}
function _make_page_public_rw()
{
if ( $this->_set_simpleperm_metadata( array("level" => self::LEVEL_PUBLIC_RW ) ) !== true )
throw new Exception( "Failed to make this page publicly writeable" );
}
function _make_page_private()
{
if ( $this->_set_simpleperm_metadata( array("level" => self::LEVEL_PRIVATE ) ) !== true )
throw new Exception( "Failed to make this page private" );
}
function _get_simpleperm_metadata()
{
// global $INFO;
global $ID;
//if ( !isset( $INFO["meta"][ self::META_NAME ] ) )
//$this->_add_simpleperm_metadata(); // add the data if it is not existing
$meta = p_get_metadata( $ID, self::META_NAME );
// if ( !empty( $INFO ) )
// $INFO["meta"][self::META_NAME] = $meta;
return $meta;
}
function _set_simpleperm_metadata( $data )
{
global $ID;
return p_set_metadata( $ID, array(self::META_NAME => $data) );
}
function _add_simpleperm_metadata()
{
$data = array(
"level" => self::LEVEL_PRIVATE,
"owner" => $this->_get_user()
);
if ( $this->_set_simpleperm_metadata( $data ) !== true )
throw new Exception( "Couldn't add permissions metadata to this page.");
return true;
}
function _set_sp_data( $owner=null, $level=null ) {
}
}