forked from getgrav/grav-plugin-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.php
354 lines (305 loc) · 11.1 KB
/
form.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Utils;
use Grav\Common\Uri;
use Symfony\Component\Yaml\Yaml;
use RocketTheme\Toolbox\File\File;
use RocketTheme\Toolbox\Event\Event;
/**
* Class FormPlugin
* @package Grav\Plugin
*/
class FormPlugin extends Plugin
{
public $features = [
'blueprints' => 1000
];
/**
* @var bool
*/
protected $active = false;
/**
* @var Form
*/
protected $form;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onFormFieldTypes' => ['onFormFieldTypes', 0]
];
}
public function onPluginsInitialized()
{
require_once(__DIR__ . '/classes/form.php');
require_once(__DIR__ . '/classes/form_serializable.php');
}
/**
* Initialize form if the page has one. Also catches form processing if user posts the form.
*/
public function onPageInitialized()
{
/** @var Page $page */
$page = $this->grav['page'];
if (!$page) {
return;
}
$header = $page->header();
if (isset($header->form) && is_array($header->form)) {
$this->active = true;
// Create form
$this->form = new Form($page);
$this->enable([
'onFormProcessed' => ['onFormProcessed', 0],
'onFormValidationError' => ['onFormValidationError', 0]
]);
// Handle posting if needed.
if (!empty($_POST)) {
$this->form->post();
}
}
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Make form accessible from twig.
*/
public function onTwigSiteVariables()
{
if (!$this->active) {
return;
}
$this->grav['twig']->twig_vars['form'] = $this->form;
}
/**
* Handle form processing instructions.
*
* @param Event $event
*/
public function onFormProcessed(Event $event)
{
$form = $event['form'];
$action = $event['action'];
$params = $event['params'];
$this->process($form);
switch ($action) {
case 'captcha':
// Validate the captcha
$query = http_build_query([
'secret' => isset($params['recaptcha_secret']) ? $params['recaptcha_secret'] : isset($params['recatpcha_secret']) ? $params['recatpcha_secret'] : $this->config->get('plugins.form.recaptcha.secret_key'),
'response' => $this->form->value('g-recaptcha-response', true)
]);
$url = 'https://www.google.com/recaptcha/api/siteverify?' . $query;
$response = json_decode(file_get_contents($url), true);
if (!isset($response['success']) || $response['success'] !== true) {
$this->grav->fireEvent('onFormValidationError', new Event([
'form' => $form,
'message' => $this->grav['language']->translate('PLUGIN_FORM.ERROR_VALIDATING_CAPTCHA')
]));
$event->stopPropagation();
return;
}
break;
case 'ip':
$label = isset($params['label']) ? $params['label'] : 'User IP';
$blueprint = $this->form->value()->blueprints();
$blueprint->set('form/fields/ip', ['name'=>'ip', 'label'=> $label]);
$this->form->setFields($blueprint->fields());
$this->form->setData('ip', Uri::ip());
break;
case 'message':
$translated_string = $this->grav['language']->translate($params);
$vars = array(
'form' => $form
);
/** @var Twig $twig */
$twig = $this->grav['twig'];
$processed_string = $twig->processString($translated_string, $vars);
$this->form->message = $processed_string;
break;
case 'redirect':
$form = new FormSerializable();
$form->message = $this->form->message;
$form->message_color = $this->form->message_color;
$form->fields = $this->form->fields;
$form->data = $this->form->value();
$this->grav['session']->setFlashObject('form', $form);
$this->grav->redirect((string)$params);
break;
case 'reset':
if (Utils::isPositive($params)) {
$this->form->reset();
}
break;
case 'display':
$route = (string)$params;
if (!$route || $route[0] != '/') {
/** @var Uri $uri */
$uri = $this->grav['uri'];
$route = $uri->route() . ($route ? '/' . $route : '');
}
/** @var Twig $twig */
$twig = $this->grav['twig'];
$twig->twig_vars['form'] = $form;
/** @var Pages $pages */
$pages = $this->grav['pages'];
$page = $pages->dispatch($route, true);
if (!$page) {
throw new \RuntimeException('Display page not found. Please check the page exists.', 400);
}
unset($this->grav['page']);
$this->grav['page'] = $page;
break;
case 'save':
$prefix = !empty($params['fileprefix']) ? $params['fileprefix'] : '';
$format = !empty($params['dateformat']) ? $params['dateformat'] : 'Ymd-His-u';
$ext = !empty($params['extension']) ? '.' . trim($params['extension'], '.') : '.txt';
$filename = !empty($params['filename']) ? $params['filename'] : '';
$operation = !empty($params['operation']) ? $params['operation'] : 'create';
if (!$filename) {
$filename = $prefix . $this->udate($format) . $ext;
}
/** @var Twig $twig */
$twig = $this->grav['twig'];
$vars = [
'form' => $this->form
];
$locator = $this->grav['locator'];
$path = $locator->findResource('user://data', true);
$dir = $path . DS . $this->form->name;
$fullFileName = $dir. DS . $filename;
$file = File::instance($fullFileName);
if ($operation == 'create') {
$body = $twig->processString(!empty($params['body']) ? $params['body'] : '{% include "forms/data.txt.twig" %}',
$vars);
$file->save($body);
} elseif ($operation == 'add') {
if (!empty($params['body'])) {
// use body similar to 'create' action and append to file as a log
$body = $twig->processString($params['body'], $vars);
// create folder if it doesn't exist
if (!file_exists($dir)) {
mkdir($dir);
}
// append data to existing file
file_put_contents($fullFileName, $body, FILE_APPEND | LOCK_EX);
} else {
// serialize YAML out to file for easier parsing as data sets
$vars = $vars['form']->value()->toArray();
foreach ($form->fields as $field) {
if (isset($field['process']) && isset($field['process']['ignore']) && $field['process']['ignore']) {
unset($vars[$field['name']]);
}
}
if (file_exists($fullFileName)) {
$data = Yaml::parse($file->content());
if (count($data) > 0) {
array_unshift($data, $vars);
} else {
$data[] = $vars;
}
} else {
$data[] = $vars;
}
$file->save(Yaml::dump($data));
}
}
break;
}
}
/**
* Handle form validation error
*
* @param Event $event An event object
*/
public function onFormValidationError(Event $event)
{
$form = $event['form'];
if (isset($event['message'])) {
$form->message_color = 'red';
$form->message = $event['message'];
}
$uri = $this->grav['uri'];
$route = $uri->route();
/** @var Twig $twig */
$twig = $this->grav['twig'];
$twig->twig_vars['form'] = $form;
/** @var Pages $pages */
$pages = $this->grav['pages'];
$page = $pages->dispatch($route, true);
if ($page) {
unset($this->grav['page']);
$this->grav['page'] = $page;
}
$event->stopPropagation();
}
/**
* Get list of form field types specified in this plugin. Only special types needs to be listed.
*
* @return array
*/
public function getFormFieldTypes()
{
return [
'display' => [
'input@' => false
],
'spacer' => [
'input@' => false
],
'captcha' => [
'input@' => false
]
];
}
/**
* Process a form
*
* Currently available processing tasks:
*
* - fillWithCurrentDateTime
*
* @param Form $form
*
* @return bool
*/
protected function process($form)
{
foreach ($form->fields as $field) {
if (isset($field['process'])) {
if (isset($field['process']['fillWithCurrentDateTime']) && $field['process']['fillWithCurrentDateTime']) {
$form->setData($field['name'], gmdate('D, d M Y H:i:s', time()));
}
}
}
}
/**
* Create unix timestamp for storing the data into the filesystem.
*
* @param string $format
* @param int $utimestamp
*
* @return string
*/
private function udate($format = 'u', $utimestamp = null)
{
if (is_null($utimestamp)) {
$utimestamp = microtime(true);
}
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', \sprintf('%06d', $milliseconds), $format), $timestamp);
}
}