-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
210 lines (184 loc) · 5.02 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
<?php
/**
* DokuWiki Plugin metaeditor (Action Component)
*
* Simple Meta Data Editor, heavily AJAX/jQuery based.
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Böhler <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class action_plugin_metaeditor extends DokuWiki_Action_Plugin {
// Load the helper plugin
public function action_plugin_metaeditor() {
}
// Register our hooks
function register(Doku_Event_Handler $controller) {
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
}
function handle_ajax_call_unknown($event, $param) {
if($event->data != 'plugin_metaeditor') return;
$event->preventDefault();
$event->stopPropagation();
global $INPUT;
$action = trim($INPUT->post->str('q'));
$pageid = trim($INPUT->post->str('r'));
$opts = $INPUT->post->arr('opts');
$data = array();
$useJson = true;
if(!checkSecurityToken())
{
echo "CSRF Attack.";
return;
}
$perm = auth_quickaclcheck($pageid);
if($perm == AUTH_ADMIN)
{
switch($action)
{
case 'getMeta':
$data = $this->getMetaForPage($pageid);
break;
case 'getMetaValue':
$useJson = false;
$data = $this->getMetaValueForPage($pageid, $opts['key']);
break;
case 'setMetaValue':
$data = $this->setMetaValueForPage($pageid, $opts['key'], $opts['oldval'], $opts['newval']);
break;
case 'deleteMetaValue':
$data = $this->deleteMetaValueForPage($pageid, $opts['key']);
break;
case 'createMetaArray':
$data = $this->createMetaArrayForPage($pageid, $opts['key'], $opts['newval']);
break;
case 'createMetaValue':
$data = $this->createMetaValueForPage($pageid, $opts['key'], $opts['newkey'], $opts['newval']);
break;
}
}
else
{
$data = array(false, "You are not an admin");
}
if($useJson)
{
//json library of DokuWiki
require_once DOKU_INC . 'inc/JSON.php';
$json = new JSON();
//set content type
header('Content-Type: application/json');
echo $json->encode($data);
}
else
{
echo $data;
}
}
function createMetaArrayForPage($pageid, $key, $newval)
{
$cache = false;
$meta = p_read_metadata($pageid, $cache);
$m = &$meta;
foreach($key as $k)
{
$m = &$m[$k];
}
$m[$newval] = array();
if(p_save_metadata($pageid, $meta))
return array(true, "Successfully saved: $newval");
else
return array(false, "Error saving value: $newval");
}
function createMetaValueForPage($pageid, $key, $newkey, $newval)
{
$cache = false;
$meta = p_read_metadata($pageid, $cache);
$m = &$meta;
foreach($key as $k)
{
$m = &$m[$k];
}
$m[$newkey] = $newval;
if(p_save_metadata($pageid, $meta))
return array(true, "Successfully saved: $newval");
else
return array(false, "Error saving value: $newval");
}
function setMetaValueForPage($pageid, $key, $oldval, $newval)
{
$cache = false;
$meta = p_read_metadata($pageid, $cache);
$m = &$meta;
foreach($key as $k)
$m = &$m[$k];
if($m == $oldval)
{
$m = $newval;
if(p_save_metadata($pageid, $meta))
return array(true, "Successfully saved: $newval");
else
return array(false, "Error saving value: $newval");
}
else
{
return array(false, "Key has changed in the meantime, expected $oldval but got $m. Nothing was saved!");
}
}
function deleteMetaValueForPage($pageid, $key)
{
$cache = false;
$meta = p_read_metadata($pageid, $cache);
$m = &$meta;
for($i=0;$i<count($key);$i++)
{
if($i == count($key)-1)
unset($m[$key[$i]]);
else
$m = &$m[$key[$i]];
}
if(p_save_metadata($pageid, $meta))
return array(true, "Successfully deleted key: " . join(':', $key));
else
return array(false, "Error deleting key: " . join(':', $key));
}
function parseMetaTree($meta)
{
$out = array();
foreach($meta as $k => $v)
{
$a = array();
$a['text'] = $k;
if(is_array($v))
{
$a['children'] = $this->parseMetaTree($v);
$a['li_attr'] = array('data-type' => 'folder');
}
else
{
$a['li_attr'] = array('data-type' => 'file');
$a['icon'] = DOKU_URL."/lib/images/page.png";
}
$out[] = $a;
}
return $out;
}
function getMetaForPage($pageid)
{
$cache = false;
$meta = p_read_metadata($pageid, $cache);
$out = $this->parseMetaTree($meta);
return $out;
}
function getMetaValueForPage($pageid, $key)
{
$cache = false;
$meta = p_read_metadata($pageid, $cache);
foreach($key as $k)
{
$meta = $meta[$k];
}
return $meta;
}
}