forked from cosmocode/do
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
204 lines (174 loc) · 6.57 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
<?php
/**
* DokuWiki Plugin do (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
* @author Adrian Lang <[email protected]>
* @author Dominik Eckelmann <[email protected]>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) {
die();
}
require_once(DOKU_INC . 'inc/JSON.php');
class action_plugin_do extends DokuWiki_Action_Plugin
{
/**
* Register handlers for some event hooks
*
* @param Doku_Event_Handler $controller
*/
public function register(Doku_Event_Handler $controller)
{
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_act_preprocess');
$controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'handle_delete');
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_adduser');
}
/**
* @param Doku_Event $event event object by reference
* @param null $param the parameters passed to register_hook when this handler was registered
*/
public function _adduser(&$event, $param)
{
if (!isset($_SERVER['REMOTE_USER'])) {
return;
}
global $JSINFO;
/** @var helper_plugin_do $hlp */
$hlp = plugin_load('helper', 'do');
$JSINFO['plugin_do_user'] = $_SERVER['REMOTE_USER'];
$JSINFO['plugin_do_user_name'] = $hlp->getPrettyUser($_SERVER['REMOTE_USER']);
$JSINFO['plugin_do_user_clean'] = html_entity_decode(strip_tags($JSINFO['plugin_do_user_name']));
}
/**
* @param Doku_Event $event event object by reference
* @param null $param the parameters passed to register_hook when this handler was registered
*
* @return bool
*/
public function handle_ajax_call(&$event, $param)
{
if ($event->data == 'plugin_do') { // FIXME: refactor this into early return and switch
// toggle status of a single task
global $INPUT;
$event->preventDefault();
$event->stopPropagation();
$id = cleanID($_REQUEST['do_page']);
if (auth_quickaclcheck($id) < AUTH_EDIT) {
if ($INPUT->server->has('REMOTE_USER')) {
echo -2; //not allowed
} else {
echo -1; //not logged in
}
return;
}
/** @var helper_plugin_do $hlp */
$hlp = plugin_load('helper', 'do');
$status = $hlp->toggleTaskStatus($id, $_REQUEST['do_md5'], $_REQUEST['do_commit']);
// rerender the page
p_get_metadata($id, '', true);
header('Content-Type: application/json; charset=utf-8');
$JSON = new JSON();
echo $JSON->encode($status);
} elseif ($event->data == 'plugin_do_status') {
// read status for a bunch of tasks
$event->preventDefault();
$event->stopPropagation();
$page = cleanID($_REQUEST['do_page']);
if (auth_quickaclcheck($page) < AUTH_READ) {
$status = array();
} else {
/** @var helper_plugin_do $hlp */
$hlp = plugin_load('helper', 'do');
$status = $hlp->getAllPageStatuses($page);
}
header('Content-Type: application/json; charset=utf-8');
$JSON = new JSON();
echo $JSON->encode($status);
} elseif ($event->data === 'plugin_do_userTasksOverlay') {
$event->preventDefault();
$event->stopPropagation();
global $INPUT;
if (!$INPUT->server->has('REMOTE_USER')) {
http_status(401, 'login required');
return false;
}
$user = $INPUT->server->str('REMOTE_USER');
/** @var helper_plugin_do $hlp */
$hlp = plugin_load('helper', 'do');
$tasks = $hlp->loadTasks(array('status' => array('undone'), 'user' => $user));
/** @var syntax_plugin_do_dolist $syntax */
$syntax = plugin_load('syntax', 'do_dolist');
$html = $syntax->buildTasklistHTML($tasks, true, false);
header('Content-Type: text/html; charset=utf-8');
echo $html;
}
}
/**
* @param Doku_Event $event event object by reference
* @param null $param the parameters passed to register_hook when this handler was registered
*
* @return bool
*/
public function handle_act_preprocess(&$event, $param)
{
if ($event->data != 'plugin_do') {
return true;
}
global $INPUT;
$pageid = cleanID($_REQUEST['do_page']);
$status = '';
if (auth_quickaclcheck($pageid) < AUTH_EDIT) {
$lvl = -1;
$key = 'notloggedin';
if ($INPUT->server->has('REMOTE_USER')) {
$key = 'notallowed';
}
} else {
/** @var helper_plugin_do $hlp */
$hlp = plugin_load('helper', 'do');
$status = $hlp->toggleTaskStatus($pageid, $_REQUEST['do_md5']);
if ($status == -2) {
$lvl = -1;
$key = 'notallowed';
} else {
$lvl = 1;
if ($status) {
$key = 'done';
} else {
$key = 'open';
}
}
}
$jslang = $this->getLang('js');
msg(sprintf($jslang[$key], $status), $lvl);
global $ACT;
$ACT = 'show';
return true;
}
/**
* Delete all tasks associated with a page from database, if all have been removed from the page
*
* @param Doku_Event $event event object by reference
* @param null $param the parameters passed to register_hook when this handler was registered
*/
public function handle_delete(&$event, $param)
{
if (preg_match('/<do[^>]*>.*<\/do>/i', $event->data[0][1])) {
// Only run if all tasks where removed from the page, partial removes are handled in \syntax_plugin_do_do::_save
return;
}
$namespace = $event->data[1] ? $event->data[1] . ':' : '';
$id = $namespace . $event->data[2];
if (isset($this->run[$id])) {
// Only execute on the first run
return;
}
/** @var helper_plugin_do $hlp */
$hlp = plugin_load('helper', 'do');
$hlp->cleanPageTasks($id);
$this->run[$id] = true;
}
}