form.php 28 KB

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