forked from splitbrain/dokuwiki-plugin-bureaucracy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntax.php
387 lines (346 loc) · 12.2 KB
/
syntax.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
<?php
/**
* Bureaucracy Plugin: Allows flexible creation of forms
*
* This plugin allows definition of forms in wiki pages. The forms can be
* submitted via email or used to create new pages from templates.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <[email protected]>
* @author Adrian Lang <[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.'syntax.php');
/**
* All DokuWiki plugins to extend the parser/rendering mechanism
* need to inherit from this class
*/
class syntax_plugin_bureaucracy extends DokuWiki_Syntax_Plugin {
// allowed types and the number of arguments
private $form_id = 0;
/**
* What kind of syntax are we?
*/
public function getType(){
return 'substition';
}
/**
* What about paragraphs?
*/
public function getPType(){
return 'block';
}
/**
* Where to sort in?
*/
public function getSort(){
return 155;
}
/**
* Connect pattern to lexer
*/
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('<form>.*?</form>',$mode,'plugin_bureaucracy');
}
/**
* Handle the match
*/
public function handle($match, $state, $pos, Doku_Handler &$handler){
$match = substr($match,6,-7); // remove form wrap
$lines = explode("\n",$match);
$actions = array();
$thanks = '';
$labels = '';
// parse the lines into an command/argument array
$cmds = array();
while (count($lines) > 0) {
$line = trim(array_shift($lines));
if (!$line) continue;
$args = $this->_parse_line($line, $lines);
$args[0] = $this->_sanitizeClassName($args[0]);
if (in_array($args[0], array('action', 'thanks', 'labels'))) {
if (count($args) < 2) {
msg(sprintf($this->getLang('e_missingargs'),hsc($args[0]),hsc($args[1])),-1);
continue;
}
// is action element?
if ($args[0] == 'action') {
array_shift($args);
$actions[] = array('type' => array_shift($args), 'argv' => $args);
continue;
}
// is thank you text?
if ($args[0] == 'thanks') {
$thanks = $args[1];
continue;
}
// is labels?
if ($args[0] == 'labels') {
$labels = $args[1];
continue;
}
}
$class = 'syntax_plugin_bureaucracy_field_' . $args[0];
$cmds[] = new $class($args);
}
foreach($actions as $action) {
// check if action is available
$action['type'] = $this->_sanitizeClassName($action['type']);
if (!$action['type'] ||
!@file_exists(DOKU_PLUGIN.'bureaucracy/actions/' .
$action['type'] . '.php')) {
msg(sprintf($this->getLang('e_noaction'), $action), -1);
}
}
// set thank you message
if (!$thanks) {
$thanks = "";
foreach($actions as $action) {
$thanks .= $this->getLang($action['type'].'_thanks');
}
} else {
$thanks = hsc($thanks);
}
return array('data'=>$cmds,'actions'=>$actions,'thanks'=>$thanks,'labels'=>$labels);
}
/**
* Create output
*/
public function render($format, Doku_Renderer &$R, $data) {
if ($format != 'xhtml') return false;
$R->info['cache'] = false; // don't cache
/**
* replace some time and name placeholders in the default values
* @var $opt syntax_plugin_bureaucracy_field */
foreach ($data['data'] as &$opt) {
if(isset($opt->opt['value'])) {
$opt->opt['value'] = $this->replaceNSTemplatePlaceholders($opt->opt['value']);
}
}
if($data['labels']) $this->loadlabels($data);
$this->form_id++;
if (isset($_POST['bureaucracy']) && checkSecurityToken() &&
$_POST['bureaucracy']['$$id'] == $this->form_id) {
$success = $this->_handlepost($data);
if ($success !== false) {
$R->doc .= '<div class="bureaucracy__plugin" id="scroll__here">'
. $success . '</div>';
return true;
}
}
$R->doc .= $this->_htmlform($data['data']);
return true;
}
/**
* Initializes the labels, loaded from a defined labelpage
*
* @param array $data all data passed to render()
*/
protected function loadlabels(&$data){
global $INFO;
$labelpage = $data['labels'];
$exists = false;
resolve_pageid($INFO['namespace'], $labelpage, $exists);
if(!$exists){
msg(sprintf($this->getLang('e_labelpage'), html_wikilink($labelpage)),-1);
return;
}
// parse simple list (first level cdata only)
$labels = array();
$instructions = p_cached_instructions(wikiFN($labelpage));
$inli = 0;
$item = '';
foreach($instructions as $instruction){
if($instruction[0] == 'listitem_open'){
$inli++;
continue;
}
if($inli === 1 && $instruction[0] == 'cdata'){
$item .= $instruction[1][0];
}
if($instruction[0] == 'listitem_close'){
$inli--;
if($inli === 0 ){
list($k, $v) = explode('=', $item, 2);
$k = trim($k);
$v = trim($v);
if($k && $v) $labels[$k] = $v;
$item = '';
}
}
}
// apply labels to all fields
$len = count($data['data']);
for($i = 0; $i < $len; $i++) {
if(isset($data['data'][$i]->depends_on)) {
// translate dependency on fieldsets
$label = $data['data'][$i]->depends_on[0];
if(isset($labels[$label])) {
$data['data'][$i]->depends_on[0] = $labels[$label];
}
} else if(isset($data['data'][$i]->opt['label'])) {
// translate field labels
$label = $data['data'][$i]->opt['label'];
if(isset($labels[$label])) {
$data['data'][$i]->opt['display'] = $labels[$label];
}
}
}
if (isset($data['thanks'])) {
if (isset($labels[$data['thanks']])) {
$data['thanks'] = $labels[$data['thanks']];
}
}
}
/**
* Validate posted data, perform action(s)
*
* @param array $data all data passed to render()
* @return bool whether fields validated and performed the action(s) succesfully
*/
private function _handlepost($data) {
$success = true;
foreach ($data['data'] as $id => $opt) {
/** @var $opt syntax_plugin_bureaucracy_field */
$_ret = true;
if ($opt->getFieldType() === 'fieldset') {
$params = array($_POST['bureaucracy'][$id], $id, &$data['data']);
$_ret = $opt->handle_post($params);
} elseif(!$opt->hidden) {
$_ret = $opt->handle_post($_POST['bureaucracy'][$id]);
}
if (!$_ret) {
// Do not return instantly to allow validation of all fields.
$success = false;
}
}
if (!$success) {
return false;
}
foreach($data['actions'] as $actionData) {
/** @var syntax_plugin_bureaucracy_action $action */
$class = 'syntax_plugin_bureaucracy_action_' . $actionData['type'];
$action = new $class();
try {
$success = $action->run($data['data'], $data['thanks'],
$actionData['argv']);
} catch (Exception $e) {
msg($e->getMessage(), -1);
return false;
}
}
// Perform after_action hooks
foreach($data['data'] as $field) {
/** @var $field syntax_plugin_bureaucracy_field */
$field->after_action();
}
return $success;
}
/**
* Create the form
*
* @param array $data array with form fields
* @return string html of the form
*/
private function _htmlform($data){
global $ID;
$form = new Doku_Form(array('class' => 'bureaucracy__plugin',
'id' => 'bureaucracy__plugin' . $this->form_id));
$form->addHidden('id', $ID);
$form->addHidden('bureaucracy[$$id]', $this->form_id);
foreach ($data as $id => $opt) {
/** @var $opt syntax_plugin_bureaucracy_field */
$opt->renderfield(array('name' => 'bureaucracy['.$id.']'), $form);
}
return $form->getForm();
}
/**
* Parse a line into (quoted) arguments
* Splits line at spaces, except when quoted
*
* @author William Fletcher <[email protected]>
*
* @param string $line line to parse
* @param array $lines all remaining lines
* @return array with all the arguments
*/
private function _parse_line($line, &$lines) {
$args = array();
$inQuote = false;
$arg = '';
do {
$len = strlen($line);
for ( $i = 0 ; $i < $len; $i++ ) {
if ( $line{$i} == '"' ) {
if ($inQuote) {
array_push($args, $arg);
$inQuote = false;
$arg = '';
continue;
} else {
$inQuote = true;
continue;
}
} else if ( $line{$i} == ' ' ) {
if ($inQuote) {
$arg .= ' ';
continue;
} else {
if ( strlen($arg) < 1 ) continue;
array_push($args, $arg);
$arg = '';
continue;
}
}
$arg .= $line{$i};
}
if (!$inQuote || count($lines) === 0) break;
$line = array_shift($lines);
$arg .= "\n";
} while (true);
if ( strlen($arg) > 0 ) array_push($args, $arg);
return $args;
}
/**
* Clean class name
*
* @param string $classname
* @return string cleaned name
*/
private function _sanitizeClassName($classname) {
return preg_replace('/[^\w\x7f-\xff]/', '', strtolower($classname));
}
/**
* Replace some placeholders (default available for namespace templates) for userinfo and time
* - more replacements are done in syntax_plugin_bureaucracy_action_template
* @param $input
* @return mixed
*/
protected function replaceNSTemplatePlaceholders($input) {
global $USERINFO;
global $conf;
// replace placeholders
return str_replace(array(
'@USER@',
'@NAME@',
'@MAIL@',
'@DATE@',
'@YEAR@',
'@MONTH@',
'@DAY@',
'@TIME@'
),
array(
$_SERVER['REMOTE_USER'],
$USERINFO['name'],
$USERINFO['mail'],
strftime($conf['dformat']),
date('Y'),
date('m'),
date('d'),
date('H:i')
), $input);
}
}