form.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. <?php
  2. namespace Grav\Plugin;
  3. use Composer\Autoload\ClassLoader;
  4. use Grav\Common\Data\ValidationException;
  5. use Grav\Common\Filesystem\Folder;
  6. use Grav\Common\Page\Page;
  7. use Grav\Common\Page\Pages;
  8. use Grav\Common\Page\Types;
  9. use Grav\Common\Plugin;
  10. use Grav\Common\Twig\Twig;
  11. use Grav\Common\Utils;
  12. use Grav\Common\Uri;
  13. use Grav\Common\Yaml;
  14. use Grav\Plugin\Form\Form;
  15. use RocketTheme\Toolbox\File\JsonFile;
  16. use RocketTheme\Toolbox\File\YamlFile;
  17. use RocketTheme\Toolbox\File\File;
  18. use RocketTheme\Toolbox\Event\Event;
  19. /**
  20. * Class FormPlugin
  21. * @package Grav\Plugin
  22. */
  23. class FormPlugin extends Plugin
  24. {
  25. /** @var array */
  26. public $features = [
  27. 'blueprints' => 1000
  28. ];
  29. /** @var Form */
  30. protected $form;
  31. /** @var array */
  32. protected $forms = [];
  33. /** @var array */
  34. protected $flat_forms = [];
  35. /** @var array */
  36. protected $json_response = [];
  37. /** @var bool */
  38. protected $recache_forms = false;
  39. /**
  40. * @return array
  41. */
  42. public static function getSubscribedEvents()
  43. {
  44. return [
  45. 'onPluginsInitialized' => [
  46. ['autoload', 100000],
  47. ['onPluginsInitialized', 0]
  48. ],
  49. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
  50. ];
  51. }
  52. /**
  53. * [onPluginsInitialized:100000] Composer autoload.
  54. *
  55. * @return ClassLoader
  56. */
  57. public function autoload()
  58. {
  59. return require __DIR__ . '/vendor/autoload.php';
  60. }
  61. /**
  62. * Initialize forms from cache if possible
  63. */
  64. public function onPluginsInitialized()
  65. {
  66. // Backwards compatibility for plugins that use forms.
  67. class_alias(Form::class, 'Grav\Plugin\Form');
  68. if ($this->isAdmin()) {
  69. $this->enable([
  70. 'onPageInitialized' => ['onPageInitialized', 0],
  71. 'onGetPageTemplates' => ['onGetPageTemplates', 0],
  72. ]);
  73. return;
  74. }
  75. $this->enable([
  76. 'onPageProcessed' => ['onPageProcessed', 0],
  77. 'onPagesInitialized' => ['onPagesInitialized', 0],
  78. 'onPageInitialized' => ['onPageInitialized', 0],
  79. 'onTwigInitialized' => ['onTwigInitialized', 0],
  80. 'onTwigPageVariables' => ['onTwigVariables', 0],
  81. 'onTwigSiteVariables' => ['onTwigVariables', 0],
  82. 'onFormValidationProcessed' => ['onFormValidationProcessed', 0],
  83. ]);
  84. // Mini Keep-Alive Logic
  85. $task = $this->grav['uri']->param('task');
  86. if ($task && $task === 'keep-alive') {
  87. exit;
  88. }
  89. }
  90. public function onGetPageTemplates(Event $event)
  91. {
  92. /** @var Types $types */
  93. $types = $event->types;
  94. $types->register('form');
  95. }
  96. /**
  97. * Process forms after page header processing, but before caching
  98. *
  99. * @param Event $e
  100. */
  101. public function onPageProcessed(Event $e)
  102. {
  103. /** @var Page $page */
  104. $page = $e['page'];
  105. $page_route = $page->route();
  106. if ($page->home()) {
  107. $page_route = '/';
  108. }
  109. $header = $page->header();
  110. // Call event to allow filling the page header form dynamically (e.g. use case: Comments plugin)
  111. $this->grav->fireEvent('onFormPageHeaderProcessed', new Event(['page' => $page, 'header' => $header]));
  112. if ((isset($header->forms) && is_array($header->forms)) ||
  113. (isset($header->form) && is_array($header->form))) {
  114. $page_forms = [];
  115. // Force never_cache_twig if modular form
  116. if ($page->modular()) {
  117. $header->never_cache_twig = true;
  118. }
  119. // Get the forms from the page headers
  120. if (isset($header->forms)) {
  121. $page_forms = $header->forms;
  122. } elseif (isset($header->form)) {
  123. $page_forms[] = $header->form;
  124. }
  125. // Store the page forms in the forms instance
  126. foreach ($page_forms as $name => $page_form) {
  127. $form = new Form($page, $name, $page_form);
  128. $this->addForm($page_route, $form);
  129. }
  130. }
  131. }
  132. /**
  133. * Initialize all the forms
  134. */
  135. public function onPagesInitialized()
  136. {
  137. $this->loadCachedForms();
  138. }
  139. /**
  140. * Catches form processing if user posts the form.
  141. */
  142. public function onPageInitialized()
  143. {
  144. $submitted = false;
  145. $this->json_response = [];
  146. // Save cached forms.
  147. if ($this->recache_forms) {
  148. $this->saveCachedForms();
  149. }
  150. // Force rebuild form when form has not been built and form cache expired.
  151. // This happens when form cache expires before the page cache
  152. // and then does not trigger 'onPageProcessed' event.
  153. if (!$this->forms) {
  154. $this->onPageProcessed(new Event(['page' => $this->grav['page']]));
  155. }
  156. // Enable form events if there's a POST
  157. if ($this->shouldProcessForm()) {
  158. $this->enable([
  159. 'onFormProcessed' => ['onFormProcessed', 0],
  160. 'onFormValidationError' => ['onFormValidationError', 0],
  161. 'onFormFieldTypes' => ['onFormFieldTypes', 0],
  162. ]);
  163. $uri = $this->grav['uri'];
  164. // Post the form
  165. if ($this->form) {
  166. $isJson = $uri->extension() === 'json';
  167. if ($isJson && $uri->post('__form-file-uploader__')) {
  168. $this->json_response = $this->form->uploadFiles();
  169. } elseif ($isJson && $uri->post('__form-file-remover__')) {
  170. $this->json_response = $this->form->filesSessionRemove();
  171. } else {
  172. $this->form->post();
  173. $submitted = true;
  174. }
  175. }
  176. // Clear flash objects for previously uploaded files
  177. // whenever the user switches page / reloads
  178. // ignoring any JSON / extension call
  179. if (!$submitted && null === $uri->extension()) {
  180. // Discard any previously uploaded files session.
  181. // and if there were any uploaded file, remove them from the filesystem
  182. if ($flash = $this->grav['session']->getFlashObject('files-upload')) {
  183. $flash = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($flash));
  184. foreach ($flash as $key => $value) {
  185. if ($key !== 'tmp_name') {
  186. continue;
  187. }
  188. @unlink($value);
  189. }
  190. }
  191. }
  192. }
  193. }
  194. /**
  195. * Add simple `forms()` Twig function
  196. */
  197. public function onTwigInitialized()
  198. {
  199. $this->grav['twig']->twig()->addFunction(
  200. new \Twig_SimpleFunction('forms', [$this, 'getForm'])
  201. );
  202. $this->grav['twig']->twig()->getExtension('Twig_Extension_Core')->setEscaper('yaml', function($twig, $string, $charset) {
  203. return Yaml::dump($string);
  204. }
  205. );
  206. }
  207. /**
  208. * Add current directory to twig lookup paths.
  209. */
  210. public function onTwigTemplatePaths()
  211. {
  212. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  213. }
  214. /**
  215. * Make form accessible from twig.
  216. *
  217. * @param Event $event
  218. */
  219. public function onTwigVariables(Event $event = null)
  220. {
  221. if ($event !== null && isset($event['page'])) {
  222. $page = $event['page'];
  223. } else {
  224. $page = $this->grav['page'];
  225. }
  226. $twig = $this->grav['twig'];
  227. if (!isset($twig->twig_vars['form'])) {
  228. $twig->twig_vars['form'] = $this->form($page);
  229. }
  230. if ($this->config->get('plugins.form.built_in_css')) {
  231. $this->grav['assets']->addCss('plugin://form/assets/form-styles.css');
  232. }
  233. $twig->twig_vars['form_max_filesize'] = Form::getMaxFilesize();
  234. $twig->twig_vars['form_json_response'] = $this->json_response;
  235. }
  236. /**
  237. * Handle form processing instructions.
  238. *
  239. * @param Event $event
  240. * @throws \Exception
  241. */
  242. public function onFormProcessed(Event $event)
  243. {
  244. /** @var Form $form */
  245. $form = $event['form'];
  246. $action = $event['action'];
  247. $params = $event['params'];
  248. $this->process($form);
  249. switch ($action) {
  250. case 'captcha':
  251. if (isset($params['recaptcha_secret'])) {
  252. $recaptchaSecret = $params['recaptcha_secret'];
  253. } elseif (isset($params['recatpcha_secret'])) {
  254. // Included for backwards compatibility with typo (issue #51)
  255. $recaptchaSecret = $params['recatpcha_secret'];
  256. } else {
  257. $recaptchaSecret = $this->config->get('plugins.form.recaptcha.secret_key');
  258. }
  259. // Validate the captcha
  260. $query = http_build_query([
  261. 'secret' => $recaptchaSecret,
  262. 'response' => $form->value('g-recaptcha-response', true)
  263. ]);
  264. $url = 'https://www.google.com/recaptcha/api/siteverify';
  265. if (ini_get('allow_url_fopen')) {
  266. $response = json_decode(file_get_contents($url . '?' . $query), true);
  267. } else {
  268. $ch = curl_init();
  269. curl_setopt($ch, CURLOPT_URL, $url);
  270. curl_setopt($ch, CURLOPT_POST, true);
  271. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  272. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  273. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  274. $response = json_decode(curl_exec($ch), true);
  275. }
  276. if (!isset($response['success']) || $response['success'] !== true) {
  277. $this->grav->fireEvent('onFormValidationError', new Event([
  278. 'form' => $form,
  279. 'message' => $this->grav['language']->translate('PLUGIN_FORM.ERROR_VALIDATING_CAPTCHA')
  280. ]));
  281. $event->stopPropagation();
  282. return;
  283. }
  284. break;
  285. case 'timestamp':
  286. $label = isset($params['label']) ? $params['label'] : 'Timestamp';
  287. $format = isset($params['format']) ? $params['format'] : 'Y-m-d H:i:s';
  288. $blueprint = $form->value()->blueprints();
  289. $blueprint->set('form/fields/timestamp', ['name'=>'timestamp', 'label'=> $label]);
  290. $now = new \DateTime('now');
  291. $date_string = $now->format($format);
  292. $form->setFields($blueprint->fields());
  293. $form->setData('timestamp',$date_string);
  294. break;
  295. case 'ip':
  296. $label = isset($params['label']) ? $params['label'] : 'User IP';
  297. $blueprint = $form->value()->blueprints();
  298. $blueprint->set('form/fields/ip', ['name'=>'ip', 'label'=> $label]);
  299. $form->setFields($blueprint->fields());
  300. $form->setData('ip', Uri::ip());
  301. break;
  302. case 'message':
  303. $translated_string = $this->grav['language']->translate($params);
  304. $vars = array(
  305. 'form' => $form
  306. );
  307. /** @var Twig $twig */
  308. $twig = $this->grav['twig'];
  309. $processed_string = $twig->processString($translated_string, $vars);
  310. $form->message = $processed_string;
  311. break;
  312. case 'redirect':
  313. $this->grav['session']->setFlashObject('form', $form);
  314. $url = ((string)$params);
  315. $vars = array(
  316. 'form' => $form
  317. );
  318. /** @var Twig $twig */
  319. $twig = $this->grav['twig'];
  320. $url = $twig->processString($url, $vars);
  321. $this->grav->redirect($url);
  322. break;
  323. case 'reset':
  324. if (Utils::isPositive($params)) {
  325. $form->reset();
  326. }
  327. break;
  328. case 'display':
  329. $route = (string)$params;
  330. if (!$route || $route[0] !== '/') {
  331. /** @var Uri $uri */
  332. $uri = $this->grav['uri'];
  333. $route = rtrim($uri->route(), '/'). '/' . ($route ?: '');
  334. }
  335. /** @var Twig $twig */
  336. $twig = $this->grav['twig'];
  337. $twig->twig_vars['form'] = $form;
  338. /** @var Pages $pages */
  339. $pages = $this->grav['pages'];
  340. $page = $pages->dispatch($route, true);
  341. if (!$page) {
  342. throw new \RuntimeException('Display page not found. Please check the page exists.', 400);
  343. }
  344. unset($this->grav['page']);
  345. $this->grav['page'] = $page;
  346. break;
  347. case 'remember':
  348. foreach ($params as $remember_field) {
  349. $field_cookie = 'forms-'.$form['name'].'-'.$remember_field;
  350. setcookie($field_cookie, $form->value($remember_field), time()+60*60*24*60);
  351. }
  352. break;
  353. case 'save':
  354. $prefix = !empty($params['fileprefix']) ? $params['fileprefix'] : '';
  355. $format = !empty($params['dateformat']) ? $params['dateformat'] : 'Ymd-His-u';
  356. $raw_format = !empty($params['dateraw']) ? (bool) $params['dateraw'] : false;
  357. $postfix = !empty($params['filepostfix']) ? $params['filepostfix'] : '';
  358. $ext = !empty($params['extension']) ? '.' . trim($params['extension'], '.') : '.txt';
  359. $filename = !empty($params['filename']) ? $params['filename'] : '';
  360. $operation = !empty($params['operation']) ? $params['operation'] : 'create';
  361. if (!$filename) {
  362. $filename = $prefix . $this->udate($format, $raw_format) . $postfix. $ext;
  363. }
  364. /** @var Twig $twig */
  365. $twig = $this->grav['twig'];
  366. $vars = [
  367. 'form' => $form
  368. ];
  369. // Process with Twig
  370. $filename = $twig->processString($filename, $vars);
  371. $locator = $this->grav['locator'];
  372. $path = $locator->findResource('user://data', true);
  373. $dir = $path . DS . $form->name();
  374. $fullFileName = $dir. DS . $filename;
  375. if (!empty($params['raw']) || !empty($params['template'])) {
  376. // Save data as it comes from the form.
  377. if ($operation === 'add') {
  378. throw new \RuntimeException('Form save: \'operation: add\' is not supported for raw files');
  379. }
  380. switch ($ext) {
  381. case '.yaml':
  382. $file = YamlFile::instance($fullFileName);
  383. break;
  384. case '.json':
  385. $file = JsonFile::instance($fullFileName);
  386. break;
  387. default:
  388. throw new \RuntimeException('Form save: Unsupported RAW file format, please use either yaml or json');
  389. }
  390. $data = [
  391. '_data_type' => 'form',
  392. 'template' => !empty($params['template']) ? $params['template'] : null,
  393. 'name' => $form->name(),
  394. 'timestamp' => date('Y-m-d H:i:s'),
  395. 'content' => $form->getData()->toArray()
  396. ];
  397. $file->save(array_filter($data));
  398. break;
  399. }
  400. $file = File::instance($fullFileName);
  401. if ($operation === 'create') {
  402. $body = $twig->processString(!empty($params['body']) ? $params['body'] : '{% include "forms/data.txt.twig" %}',
  403. $vars);
  404. $file->save($body);
  405. } elseif ($operation === 'add') {
  406. if (!empty($params['body'])) {
  407. // use body similar to 'create' action and append to file as a log
  408. $body = $twig->processString($params['body'], $vars);
  409. // create folder if it doesn't exist
  410. if (!file_exists($dir)) {
  411. Folder::create($dir);
  412. }
  413. // append data to existing file
  414. file_put_contents($fullFileName, $body, FILE_APPEND | LOCK_EX);
  415. } else {
  416. // serialize YAML out to file for easier parsing as data sets
  417. $vars = $vars['form']->value()->toArray();
  418. foreach ($form->fields as $field) {
  419. if (!empty($field['process']['ignore'])) {
  420. unset($vars[$field['name']]);
  421. }
  422. }
  423. if (file_exists($fullFileName)) {
  424. $data = Yaml::parse($file->content());
  425. if (count($data) > 0) {
  426. array_unshift($data, $vars);
  427. } else {
  428. $data[] = $vars;
  429. }
  430. } else {
  431. $data[] = $vars;
  432. }
  433. $file->save(Yaml::dump($data));
  434. }
  435. }
  436. break;
  437. case 'call':
  438. $callable = $params;
  439. if (is_array($callable) && !method_exists($callable[0], $callable[1])) {
  440. throw new \RuntimeException('Form cannot be processed (method does not exist)');
  441. }
  442. if (is_string($callable) && !function_exists($callable)) {
  443. throw new \RuntimeException('Form cannot be processed (function does not exist)');
  444. }
  445. call_user_func($callable, $form);
  446. break;
  447. }
  448. }
  449. /**
  450. * Custom field logic can go in here
  451. *
  452. * @param Event $event
  453. */
  454. public function onFormValidationProcessed(Event $event)
  455. {
  456. // special check for honeypot field
  457. foreach ($event['form']->fields() as $field) {
  458. if ($field['type'] === 'honeypot' && !empty($event['form']->value($field['name']))) {
  459. throw new ValidationException('Are you a bot?');
  460. }
  461. }
  462. }
  463. /**
  464. * Handle form validation error
  465. *
  466. * @param Event $event An event object
  467. * @throws \Exception
  468. */
  469. public function onFormValidationError(Event $event)
  470. {
  471. $form = $event['form'];
  472. if (isset($event['message'])) {
  473. $form->status = 'error';
  474. $form->message = $event['message'];
  475. $form->messages = $event['messages'];
  476. }
  477. $uri = $this->grav['uri'];
  478. $route = $uri->route();
  479. /** @var Twig $twig */
  480. $twig = $this->grav['twig'];
  481. $twig->twig_vars['form'] = $form;
  482. /** @var Pages $pages */
  483. $pages = $this->grav['pages'];
  484. $page = $pages->dispatch($route, true);
  485. if ($page) {
  486. unset($this->grav['page']);
  487. $this->grav['page'] = $page;
  488. }
  489. $event->stopPropagation();
  490. }
  491. /**
  492. * Add a form to the forms plugin
  493. *
  494. * @param $page_route
  495. * @param $form
  496. */
  497. public function addForm($page_route, $form)
  498. {
  499. $form_array = [$form['name'] => $form];
  500. if (array_key_exists($page_route, $this->forms)) {
  501. if (!isset($this->form[$page_route][$form['name']])) {
  502. $this->forms[$page_route] = array_merge($this->forms[$page_route], $form_array);
  503. }
  504. } else {
  505. $this->forms[$page_route] = $form_array;
  506. }
  507. $this->flattenForms();
  508. $this->recache_forms = true;
  509. }
  510. /**
  511. * function to get a specific form
  512. *
  513. * @param null|array|string $data optional form `name`
  514. *
  515. * @return null|Form
  516. */
  517. public function getForm($data = null)
  518. {
  519. $page_route = null;
  520. $form_name = null;
  521. if (is_array($data)) {
  522. if (isset($data['name'])) {
  523. $form_name = $data['name'];
  524. }
  525. if (isset($data['route'])) {
  526. $page_route = $data['route'];
  527. }
  528. } elseif (is_string($data)) {
  529. $form_name = $data;
  530. }
  531. // if no form name, use the first form found in the page
  532. if (!$form_name) {
  533. // If page route not provided, use the current page
  534. if (!$page_route) {
  535. // Get page route
  536. $page_route = $this->grav['page']->route();
  537. // fallback using current URI if page not initialized yet
  538. if (!$page_route) {
  539. $page_route = $this->getCurrentPageRoute();
  540. }
  541. }
  542. if (isset($this->forms[$page_route])) {
  543. $forms = $this->forms[$page_route];
  544. $first_form = array_shift($forms);
  545. $form_name = $first_form['name'];
  546. } else {
  547. //No form on this route. Try looking up in the current page first
  548. return new Form($this->grav['page']);
  549. }
  550. }
  551. // return the form you are looking for if available
  552. return $this->getFormByName($form_name);
  553. }
  554. /**
  555. * Get list of form field types specified in this plugin. Only special types needs to be listed.
  556. *
  557. * @return array
  558. */
  559. public function getFormFieldTypes()
  560. {
  561. return [
  562. 'column' => [
  563. 'input@' => false
  564. ],
  565. 'columns' => [
  566. 'input@' => false
  567. ],
  568. 'fieldset' => [
  569. 'input@' => false
  570. ],
  571. 'conditional' => [
  572. 'input@' => false
  573. ],
  574. 'display' => [
  575. 'input@' => false
  576. ],
  577. 'spacer' => [
  578. 'input@' => false
  579. ],
  580. 'captcha' => [
  581. 'input@' => false
  582. ]
  583. ];
  584. }
  585. /**
  586. * Process a form
  587. *
  588. * Currently available processing tasks:
  589. *
  590. * - fillWithCurrentDateTime
  591. *
  592. * @param Form $form
  593. */
  594. protected function process($form)
  595. {
  596. foreach ($form->fields as $field) {
  597. if (!empty($field['process']['fillWithCurrentDateTime'])) {
  598. $form->setData($field['name'], gmdate('D, d M Y H:i:s', time()));
  599. }
  600. }
  601. }
  602. /**
  603. * Get current page's route
  604. *
  605. * @return mixed
  606. */
  607. protected function getCurrentPageRoute()
  608. {
  609. $path = $this->grav['uri']->route();
  610. $path = $path ?: '/';
  611. return $path;
  612. }
  613. /**
  614. * Retrieve a form based on the form name
  615. *
  616. * @param $form_name
  617. * @return mixed
  618. */
  619. protected function getFormByName($form_name)
  620. {
  621. if (array_key_exists($form_name, $this->flat_forms)) {
  622. return $this->flat_forms[$form_name];
  623. }
  624. return null;
  625. }
  626. /**
  627. * Determine if the page has a form submission that should be processed
  628. *
  629. * @return bool
  630. */
  631. protected function shouldProcessForm()
  632. {
  633. $uri = $this->grav['uri'];
  634. $nonce = $uri->post('form-nonce');
  635. $status = $nonce ? true : false; // php72 quirk?
  636. $refresh_prevention = null;
  637. if ($status && $this->form()) {
  638. // Set page template if passed by form
  639. if (isset($this->form->template)) {
  640. $this->grav['page']->template($this->form->template);
  641. }
  642. if (isset($this->form->refresh_prevention)) {
  643. $refresh_prevention = (bool) $this->form->refresh_prevention;
  644. } else {
  645. $refresh_prevention = $this->config->get('plugins.form.refresh_prevention', false);
  646. }
  647. $unique_form_id = $uri->post('__unique_form_id__', FILTER_SANITIZE_STRING);
  648. if ($refresh_prevention && $unique_form_id) {
  649. if ($this->grav['session']->unique_form_id !== $unique_form_id) {
  650. $this->grav['session']->unique_form_id = $unique_form_id;
  651. } else {
  652. $status = false;
  653. $this->form->message = $this->grav['language']->translate('PLUGIN_FORM.FORM_ALREADY_SUBMITTED');
  654. $this->form->status = 'error';
  655. }
  656. }
  657. }
  658. return $status;
  659. }
  660. /**
  661. * Flatten the forms array into something that can be more easily searched
  662. */
  663. protected function flattenForms()
  664. {
  665. $this->flat_forms = Utils::arrayFlatten($this->forms);
  666. }
  667. /**
  668. * Get the current form, should already be processed but can get it directly from the page if necessary
  669. *
  670. * @param Page|null $page
  671. * @return Form|mixed
  672. */
  673. protected function form($page = null)
  674. {
  675. // Regenerate list of flat_forms if not already populated
  676. if (empty($this->flat_forms)) {
  677. $this->flattenForms();
  678. }
  679. if (null === $this->form) {
  680. // try to get the page if possible
  681. if (null === $page) {
  682. $page = $this->grav['page'];
  683. }
  684. $form_name = $this->grav['uri']->post('__form-name__', FILTER_SANITIZE_STRING);
  685. if (!$form_name) {
  686. $form_name = $page ? $page->slug() : null;
  687. }
  688. $this->form = $this->getFormByName($form_name);
  689. // last attempt using current page's form
  690. if (null === $this->form && $page) {
  691. $header = $page->header();
  692. if (isset($header->form)) {
  693. $this->form = new Form($page);
  694. }
  695. }
  696. }
  697. return $this->form;
  698. }
  699. /**
  700. * Load cached forms and merge with any currently found forms
  701. */
  702. protected function loadCachedForms()
  703. {
  704. // Get and set the cache of forms if it exists
  705. list($forms, $flat_forms) = $this->grav['cache']->fetch($this->getFormCacheId());
  706. // Only store the forms if they are an array
  707. if (is_array($forms)) {
  708. $this->forms = array_merge($this->forms, $forms);
  709. }
  710. // Only store the flat_forms if they are an array
  711. if (is_array($flat_forms)) {
  712. $this->flat_forms = array_merge($this->flat_forms, $flat_forms);
  713. }
  714. }
  715. /**
  716. * Save the current state of the forms
  717. */
  718. protected function saveCachedForms()
  719. {
  720. // Save the current state of the forms to cache
  721. if ($this->recache_forms) {
  722. $this->recache_forms = false;
  723. $this->grav['cache']->save($this->getFormCacheId(), [$this->forms, $this->flat_forms]);
  724. }
  725. }
  726. /**
  727. * Get the current page cache based id for the forms cache
  728. *
  729. * @return string
  730. */
  731. protected function getFormCacheId()
  732. {
  733. return $this->grav['pages']->getPagesCacheId() . '-form-plugin';
  734. }
  735. /**
  736. * Create unix timestamp for storing the data into the filesystem.
  737. *
  738. * @param string $format
  739. * @param bool $raw
  740. *
  741. * @return string
  742. */
  743. protected function udate($format = 'u', $raw = false)
  744. {
  745. $utimestamp = microtime(true);
  746. if ($raw) {
  747. return date($format);
  748. }
  749. $timestamp = floor($utimestamp);
  750. $milliseconds = round(($utimestamp - $timestamp) * 1000000);
  751. return date(preg_replace('`(?<!\\\\)u`', \sprintf('%06d', $milliseconds), $format), $timestamp);
  752. }
  753. }