This repository was archived by the owner on Oct 29, 2020. It is now read-only.
forked from Klap-in/dokuwiki-plugin-bureaucracy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.php
283 lines (251 loc) · 8.56 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
<?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
var $form_id = 0;
/**
* What kind of syntax are we?
*/
function getType(){
return 'substition';
}
/**
* What about paragraphs?
*/
function getPType(){
return 'block';
}
/**
* Where to sort in?
*/
function getSort(){
return 155;
}
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern('<form>.*?</form>',$mode,'plugin_bureaucracy');
}
/**
* Handle the match
*/
function handle($match, $state, $pos, &$handler){
$match = substr($match,6,-7); // remove form wrap
$lines = explode("\n",$match);
$action = array('type' => '',
'argv' => array());
$thanks = '';
// 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'))) {
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);
$action['type'] = array_shift($args);
$action['argv'] = $args;
continue;
}
// is thank you text?
if ($args[0] == 'thanks') {
$thanks = $args[1];
continue;
}
}
$class = 'syntax_plugin_bureaucracy_field_' . $args[0];
$cmds[] = new $class($args);
}
// 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 = $this->getLang($action['type'].'_thanks');
} else {
$thanks = hsc($thanks);
}
return array('data'=>$cmds,'action'=>$action,'thanks'=>$thanks);
}
/**
* Create output
*/
function render($format, &$R, $data) {
global $ID;
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 $id => &$opt) {
if(isset($opt->opt['value'])) {
$opt->opt['value'] = $this->replace($opt->opt['value']);
}
}
$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;
}
/**
* Validate data, perform action
*/
function _handlepost($data) {
$success = true;
foreach ($data['data'] as $id => $opt) {
if ($opt->getFieldType() === 'fieldset') {
$_ret = $opt->handle_post($_POST['bureaucracy'][$id], $id, $data['data']);
} 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;
}
$class = 'syntax_plugin_bureaucracy_action_' . $data['action']['type'];
$action = new $class();
try {
$success = $action->run($data['data'], $data['thanks'],
$data['action']['argv']);
} catch (Exception $e) {
msg($e->getMessage());
return false;
}
// Perform after_action hooks
foreach($data['data'] as $id => $field) {
$field->after_action();
}
return $success;
}
/**
* Create the form
*/
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) {
$opt->render(array('name' => 'bureaucracy['.$id.']'), $form);
}
return $form->getForm();
}
/**
* Parse a line into (quoted) arguments
*
* @author William Fletcher <[email protected]>
*/
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;
}
function _sanitizeClassName($classname) {
return preg_replace('/[^\w\x7f-\xff]/', '', strtolower($classname));
}
/**
* Replace some placeholders for userinfo and time
* - more replacements are done in syntax_plugin_bureaucracy_action_template
* @param $input
* @return mixed
*/
function replace($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);
}
}