form.php 23 KB

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