form.php 28 KB

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