| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 | 
							- <?php
 
- namespace Grav\Plugin;
 
- use Grav\Common\Data\ValidationException;
 
- use Grav\Common\Filesystem\Folder;
 
- use Grav\Common\Page\Page;
 
- use Grav\Common\Page\Pages;
 
- use Grav\Common\Plugin;
 
- use Grav\Common\Twig\Twig;
 
- use Grav\Common\Utils;
 
- use Grav\Common\Uri;
 
- use Symfony\Component\Yaml\Yaml;
 
- use RocketTheme\Toolbox\File\File;
 
- use RocketTheme\Toolbox\Event\Event;
 
- /**
 
-  * Class FormPlugin
 
-  * @package Grav\Plugin
 
-  */
 
- class FormPlugin extends Plugin
 
- {
 
-     public $features = [
 
-         'blueprints' => 1000
 
-     ];
 
-     /**
 
-      * @var Form
 
-      */
 
-     protected $form;
 
-     protected $forms = [];
 
-     protected $flat_forms = [];
 
-     protected $json_response = [];
 
-     protected $recache_forms = false;
 
-     /**
 
-      * @return array
 
-      */
 
-     public static function getSubscribedEvents()
 
-     {
 
-         return [
 
-             'onPluginsInitialized' => ['onPluginsInitialized', 0],
 
-             'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0]
 
-         ];
 
-     }
 
-     /**
 
-      * Initialize forms from cache if possible
 
-      */
 
-     public function onPluginsInitialized()
 
-     {
 
-         require_once(__DIR__ . '/classes/form.php');
 
-         if ($this->isAdmin()) {
 
-             $this->enable([
 
-                 'onPagesInitialized' => ['onPagesInitialized', 0]
 
-             ]);
 
-             return;
 
-         }
 
-         $this->enable([
 
-             'onPageProcessed' => ['onPageProcessed', 0],
 
-             'onPagesInitialized' => ['onPagesInitialized', 0],
 
-             'onTwigInitialized' => ['onTwigInitialized', 0],
 
-             'onTwigPageVariables' => ['onTwigVariables', 0],
 
-             'onTwigSiteVariables' => ['onTwigVariables', 0],
 
-             'onFormValidationProcessed' => ['onFormValidationProcessed', 0],
 
-         ]);
 
-     }
 
-     /**
 
-      * Process forms after page header processing, but before caching
 
-      *
 
-      * @param Event $e
 
-      */
 
-     public function onPageProcessed(Event $e)
 
-     {
 
-         /** @var Page $page */
 
-         $page = $e['page'];
 
-         $page_route = $page->route();
 
-         if ($page->home()) {
 
-             $page_route = '/';
 
-         }
 
-         $header = $page->header();
 
-         //call event to allow filling the page header form dynamically (e.g. use case: Comments plugin)
 
-         $this->grav->fireEvent('onFormPageHeaderProcessed', new Event(['header' => $header]));
 
-         if ((isset($header->forms) && is_array($header->forms)) ||
 
-             (isset($header->form) && is_array($header->form))) {
 
-             $page_forms = [];
 
-             // Force never_cache_twig if modular form
 
-             if ($page->modular()) {
 
-                 $header->never_cache_twig = true;
 
-             }
 
-             // Get the forms from the page headers
 
-             if (isset($header->forms)) {
 
-                 $page_forms = $header->forms;
 
-             } elseif (isset($header->form)) {
 
-                 $page_forms[] = $header->form;
 
-             }
 
-             // Store the page forms in the forms instance
 
-             foreach ($page_forms as $name => $page_form) {
 
-                 $form = new Form($page, $name, $page_form);
 
-                 $this->addForm($page_route, $form);
 
-             }
 
-         }
 
-     }
 
-     /**
 
-      * Add a form to the forms plugin
 
-      *
 
-      * @param $page_route
 
-      * @param $form
 
-      */
 
-     public function addForm($page_route, $form)
 
-     {
 
-         $form_array = [$form['name'] => $form];
 
-         if (array_key_exists($page_route, $this->forms)) {
 
-             if (!isset($this->form[$page_route][$form['name']])) {
 
-                 $this->forms[$page_route] = array_merge($this->forms[$page_route], $form_array);
 
-             }
 
-         } else {
 
-             $this->forms[$page_route] = $form_array;
 
-         }
 
-         $this->flattenForms();
 
-         $this->recache_forms = true;
 
-     }
 
-     /**
 
-      * Initialize form if the page has one. Also catches form processing if user posts the form.
 
-      */
 
-     public function onPagesInitialized()
 
-     {
 
-         $submitted = false;
 
-         $this->json_response = [];
 
-         $cache_id = $this->grav['pages']->getPagesCacheId() . '-form-plugin';
 
-         // Get and set the cache of forms if it exists
 
-         list($forms, $flat_forms) = $this->grav['cache']->fetch($cache_id);
 
-         // Only store the forms if they are an array
 
-         if (is_array($forms)) {
 
-             $this->forms = array_merge($this->forms, $forms);
 
-         }
 
-         // Only store the flat_forms if they are an array
 
-         if (is_array($flat_forms)) {
 
-             $this->flat_forms = array_merge($this->flat_forms, $flat_forms);
 
-         }
 
-         // Save the current state of the forms to cache
 
-         if ($this->recache_forms) {
 
-             $this->grav['cache']->save($cache_id, [$this->forms, $this->flat_forms]);
 
-         }
 
-         // Enable form events if there's a POST
 
-         if ($this->shouldProcessForm()) {
 
-             $this->enable([
 
-                 'onFormProcessed' => ['onFormProcessed', 0],
 
-                 'onFormValidationError' => ['onFormValidationError', 0],
 
-                 'onFormFieldTypes'       => ['onFormFieldTypes', 0],
 
-             ]);
 
-             // Post the form
 
-             if ($this->form()) {
 
-                 if ($this->grav['uri']->extension() === 'json' && isset($_POST['__form-file-uploader__'])) {
 
-                     $this->json_response = $this->form->uploadFiles();
 
-                 } else {
 
-                     $this->form->post();
 
-                     $submitted = true;
 
-                 }
 
-             }
 
-             // Clear flash objects for previously uploaded files
 
-             // whenever the user switches page / reloads
 
-             // ignoring any JSON / extension call
 
-             if (null === $this->grav['uri']->extension() && !$submitted) {
 
-                 // Discard any previously uploaded files session.
 
-                 // and if there were any uploaded file, remove them from the filesystem
 
-                 if ($flash = $this->grav['session']->getFlashObject('files-upload')) {
 
-                     $flash = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($flash));
 
-                     foreach ($flash as $key => $value) {
 
-                         if ($key !== 'tmp_name') {
 
-                             continue;
 
-                         }
 
-                         @unlink($value);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-     }
 
-     /**
 
-      * Add simple `forms()` Twig function
 
-      */
 
-     public function onTwigInitialized()
 
-     {
 
-         $this->grav['twig']->twig()->addFunction(
 
-             new \Twig_SimpleFunction('forms', [$this, 'getForm'])
 
-         );
 
-         $this->grav['twig']->twig()->getExtension('Twig_Extension_Core')->setEscaper('yaml', function($twig, $string, $charset) {
 
-             return Yaml::dump($string);
 
-             }
 
-         );
 
-     }
 
-     /**
 
-      * Add current directory to twig lookup paths.
 
-      */
 
-     public function onTwigTemplatePaths()
 
-     {
 
-         $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
 
-     }
 
-     /**
 
-      * Make form accessible from twig.
 
-      *
 
-      * @param Event $event
 
-      */
 
-     public function onTwigVariables(Event $event = null)
 
-     {
 
-         if ($event !== null && isset($event['page'])) {
 
-             $page = $event['page'];
 
-         } else {
 
-             $page = $this->grav['page'];
 
-         }
 
-         $twig = $this->grav['twig'];
 
-         if (!isset($twig->twig_vars['form'])) {
 
-             $twig->twig_vars['form'] = $this->form($page);
 
-         }
 
-         if ($this->config->get('plugins.form.built_in_css')) {
 
-             $this->grav['assets']->addCss('plugin://form/assets/form-styles.css');
 
-         }
 
-         $twig->twig_vars['form_max_filesize'] = Form::getMaxFilesize();
 
-         $twig->twig_vars['form_json_response'] = $this->json_response;
 
-     }
 
-     /**
 
-      * Handle form processing instructions.
 
-      *
 
-      * @param Event $event
 
-      * @throws \Exception
 
-      */
 
-     public function onFormProcessed(Event $event)
 
-     {
 
-         $form = $event['form'];
 
-         $action = $event['action'];
 
-         $params = $event['params'];
 
-         $this->process($form);
 
-         switch ($action) {
 
-             case 'captcha':
 
-                 if (isset($params['recaptcha_secret'])) {
 
-                     $recaptchaSecret = $params['recaptcha_secret'];
 
-                 } elseif (isset($params['recatpcha_secret'])) {
 
-                     // Included for backwards compatibility with typo (issue #51)
 
-                     $recaptchaSecret = $params['recatpcha_secret'];
 
-                 } else {
 
-                     $recaptchaSecret = $this->config->get('plugins.form.recaptcha.secret_key');
 
-                 }
 
-                 // Validate the captcha
 
-                 $query = http_build_query([
 
-                     'secret'   => $recaptchaSecret,
 
-                     'response' => $form->value('g-recaptcha-response', true)
 
-                 ]);
 
-                 $url = 'https://www.google.com/recaptcha/api/siteverify?' . $query;
 
-                 $response = json_decode(file_get_contents($url), true);
 
-                 if (!isset($response['success']) || $response['success'] !== true) {
 
-                     $this->grav->fireEvent('onFormValidationError', new Event([
 
-                         'form'    => $form,
 
-                         'message' => $this->grav['language']->translate('PLUGIN_FORM.ERROR_VALIDATING_CAPTCHA')
 
-                     ]));
 
-                     $event->stopPropagation();
 
-                     return;
 
-                 }
 
-                 break;
 
-             case 'ip':
 
-                 $label = isset($params['label']) ? $params['label'] : 'User IP';
 
-                 $blueprint = $form->value()->blueprints();
 
-                 $blueprint->set('form/fields/ip', ['name'=>'ip', 'label'=> $label]);
 
-                 $form->setFields($blueprint->fields());
 
-                 $form->setData('ip', Uri::ip());
 
-                 break;
 
-             case 'message':
 
-                 $translated_string = $this->grav['language']->translate($params);
 
-                 $vars = array(
 
-                     'form' => $form
 
-                 );
 
-                 /** @var Twig $twig */
 
-                 $twig = $this->grav['twig'];
 
-                 $processed_string = $twig->processString($translated_string, $vars);
 
-                 $form->message = $processed_string;
 
-                 break;
 
-             case 'redirect':
 
-                 $this->grav['session']->setFlashObject('form', $form);
 
-                 $url = ((string)$params);
 
-                 $vars = array(
 
-                     'form' => $form
 
-                 );
 
-                 /** @var Twig $twig */
 
-                 $twig = $this->grav['twig'];
 
-                 $url = $twig->processString($url, $vars);
 
-                 $this->grav->redirect($url);
 
-                 break;
 
-             case 'reset':
 
-                 if (Utils::isPositive($params)) {
 
-                     $form->reset();
 
-                 }
 
-                 break;
 
-             case 'display':
 
-                 $route = (string)$params;
 
-                 if (!$route || $route[0] !== '/') {
 
-                     /** @var Uri $uri */
 
-                     $uri = $this->grav['uri'];
 
-                     $route = rtrim($uri->route(), '/'). '/' . ($route ?: '');
 
-                 }
 
-                 /** @var Twig $twig */
 
-                 $twig = $this->grav['twig'];
 
-                 $twig->twig_vars['form'] = $form;
 
-                 /** @var Pages $pages */
 
-                 $pages = $this->grav['pages'];
 
-                 $page = $pages->dispatch($route, true);
 
-                 if (!$page) {
 
-                     throw new \RuntimeException('Display page not found. Please check the page exists.', 400);
 
-                 }
 
-                 unset($this->grav['page']);
 
-                 $this->grav['page'] = $page;
 
-                 break;
 
-             case 'remember':
 
-                 foreach ($params as $remember_field) {
 
-                     $field_cookie = 'forms-'.$form['name'].'-'.$remember_field;
 
-                     setcookie($field_cookie, $form->value($remember_field), time()+60*60*24*60);
 
-                 }
 
-                 break;
 
-             case 'save':
 
-                 $prefix = !empty($params['fileprefix']) ? $params['fileprefix'] : '';
 
-                 $format = !empty($params['dateformat']) ? $params['dateformat'] : 'Ymd-His-u';
 
-                 $ext = !empty($params['extension']) ? '.' . trim($params['extension'], '.') : '.txt';
 
-                 $filename = !empty($params['filename']) ? $params['filename'] : '';
 
-                 $operation = !empty($params['operation']) ? $params['operation'] : 'create';
 
-                 if (!$filename) {
 
-                     $filename = $prefix . $this->udate($format) . $ext;
 
-                 }
 
-                 /** @var Twig $twig */
 
-                 $twig = $this->grav['twig'];
 
-                 $vars = [
 
-                     'form' => $form
 
-                 ];
 
-                 // Process with Twig
 
-                 $filename = $twig->processString($filename, $vars);
 
-                 $locator = $this->grav['locator'];
 
-                 $path = $locator->findResource('user://data', true);
 
-                 $dir = $path . DS . $form->name();
 
-                 $fullFileName = $dir. DS . $filename;
 
-                 $file = File::instance($fullFileName);
 
-                 if ($operation === 'create') {
 
-                     $body = $twig->processString(!empty($params['body']) ? $params['body'] : '{% include "forms/data.txt.twig" %}',
 
-                         $vars);
 
-                     $file->save($body);
 
-                 } elseif ($operation === 'add') {
 
-                     if (!empty($params['body'])) {
 
-                         // use body similar to 'create' action and append to file as a log
 
-                         $body = $twig->processString($params['body'], $vars);
 
-                         // create folder if it doesn't exist
 
-                         if (!file_exists($dir)) {
 
-                             Folder::create($dir);
 
-                         }
 
-                         // append data to existing file
 
-                         file_put_contents($fullFileName, $body, FILE_APPEND | LOCK_EX);
 
-                     } else {
 
-                         // serialize YAML out to file for easier parsing as data sets
 
-                         $vars = $vars['form']->value()->toArray();
 
-                         foreach ($form->fields as $field) {
 
-                             if (!empty($field['process']['ignore'])) {
 
-                                 unset($vars[$field['name']]);
 
-                             }
 
-                         }
 
-                         if (file_exists($fullFileName)) {
 
-                             $data = Yaml::parse($file->content());
 
-                             if (count($data) > 0) {
 
-                                 array_unshift($data, $vars);
 
-                             } else {
 
-                                 $data[] = $vars;
 
-                             }
 
-                         } else {
 
-                             $data[] = $vars;
 
-                         }
 
-                         $file->save(Yaml::dump($data));
 
-                     }
 
-                 }
 
-                 break;
 
-         }
 
-     }
 
-     /**
 
-      * Custom field logic can go in here
 
-      *
 
-      * @param Event $event
 
-      */
 
-     public function onFormValidationProcessed(Event $event)
 
-     {
 
-         // special check for honeypot field
 
-         foreach ($event['form']->fields() as $field) {
 
-             if ($field['type'] === 'honeypot' && !empty($event['form']->value($field['name']))) {
 
-                 throw new ValidationException('Are you a bot?');
 
-             }
 
-         }
 
-     }
 
-     /**
 
-      * Handle form validation error
 
-      *
 
-      * @param  Event $event An event object
 
-      * @throws \Exception
 
-      */
 
-     public function onFormValidationError(Event $event)
 
-     {
 
-         $form = $event['form'];
 
-         if (isset($event['message'])) {
 
-             $form->status = 'error';
 
-             $form->message = $event['message'];
 
-             $form->messages = $event['messages'];
 
-         }
 
-         $uri = $this->grav['uri'];
 
-         $route = $uri->route();
 
-         /** @var Twig $twig */
 
-         $twig = $this->grav['twig'];
 
-         $twig->twig_vars['form'] = $form;
 
-         /** @var Pages $pages */
 
-         $pages = $this->grav['pages'];
 
-         $page = $pages->dispatch($route, true);
 
-         if ($page) {
 
-             unset($this->grav['page']);
 
-             $this->grav['page'] = $page;
 
-         }
 
-         $event->stopPropagation();
 
-     }
 
-     /**
 
-      * Get list of form field types specified in this plugin. Only special types needs to be listed.
 
-      *
 
-      * @return array
 
-      */
 
-     public function getFormFieldTypes()
 
-     {
 
-         return [
 
-             'column' => [
 
-                 'input@' => false
 
-             ],
 
-             'columns' => [
 
-                 'input@' => false
 
-             ],
 
-             'fieldset' => [
 
-                 'input@' => false
 
-             ],
 
-             'conditional' => [
 
-                 'input@' => false
 
-             ],
 
-             'display' => [
 
-                 'input@' => false
 
-             ],
 
-             'spacer' => [
 
-                 'input@' => false
 
-             ],
 
-             'captcha' => [
 
-                 'input@' => false
 
-             ]
 
-         ];
 
-     }
 
-     /**
 
-      * Process a form
 
-      *
 
-      * Currently available processing tasks:
 
-      *
 
-      * - fillWithCurrentDateTime
 
-      *
 
-      * @param Form $form
 
-      */
 
-     protected function process($form)
 
-     {
 
-         foreach ($form->fields as $field) {
 
-             if (!empty($field['process']['fillWithCurrentDateTime'])) {
 
-                 $form->setData($field['name'], gmdate('D, d M Y H:i:s', time()));
 
-             }
 
-         }
 
-     }
 
-     /**
 
-      * Create unix timestamp for storing the data into the filesystem.
 
-      *
 
-      * @param string $format
 
-      * @param int    $utimestamp
 
-      *
 
-      * @return string
 
-      */
 
-     private function udate($format = 'u', $utimestamp = null)
 
-     {
 
-         if (null === $utimestamp) {
 
-             $utimestamp = microtime(true);
 
-         }
 
-         $timestamp = floor($utimestamp);
 
-         $milliseconds = round(($utimestamp - $timestamp) * 1000000);
 
-         return date(preg_replace('`(?<!\\\\)u`', \sprintf('%06d', $milliseconds), $format), $timestamp);
 
-     }
 
-     /**
 
-      * @param Page $page
 
-      * @return mixed
 
-      */
 
-     private function getFormName(Page $page)
 
-     {
 
-         $name = filter_input(INPUT_POST, '__form-name__');
 
-         if (!$name) {
 
-             $name = $page->slug();
 
-         }
 
-         return $name;
 
-     }
 
-     /**
 
-      * function to get a specific form
 
-      *
 
-      * @param null|array|string $data optional form `name`
 
-      *
 
-      * @return null|Form
 
-      */
 
-     public function getForm($data = null)
 
-     {
 
-         $page_route = null;
 
-         $form_name = null;
 
-         if (is_array($data)) {
 
-             if (isset($data['name'])) {
 
-                 $form_name = $data['name'];
 
-             }
 
-             if (isset($data['route'])) {
 
-                 $page_route = $data['route'];
 
-             }
 
-         } elseif (is_string($data)) {
 
-             $form_name = $data;
 
-         }
 
-         // if no form name, use the first form found in the page
 
-         if (!$form_name) {
 
-             // If page route not provided, use the current page
 
-             if (!$page_route) {
 
-                 // Get page route
 
-                 $page_route = $this->grav['page']->route();
 
-                 // fallback using current URI if page not initialized yet
 
-                 if (!$page_route) {
 
-                     $page_route = $this->getCurrentPageRoute();
 
-                 }
 
-             }
 
-             if (isset($this->forms[$page_route])) {
 
-                 $forms = $this->forms[$page_route];
 
-                 $first_form = array_shift($forms);
 
-                 $form_name = $first_form['name'];
 
-             } else {
 
-                 //No form on this route. Try looking up in the current page first
 
-                 return new Form($this->grav['page']);
 
-             }
 
-         }
 
-         // return the form you are looking for if available
 
-         return $this->getFormByName($form_name);
 
-     }
 
-     /**
 
-      * Get current page's route
 
-      *
 
-      * @return mixed
 
-      */
 
-     protected function getCurrentPageRoute()
 
-     {
 
-         $path = $this->grav['uri']->route();
 
-         $path = $path ?: '/';
 
-         return $path;
 
-     }
 
-     /**
 
-      * Retrieve a form based on the form name
 
-      *
 
-      * @param $form_name
 
-      * @return mixed
 
-      */
 
-     protected function getFormByName($form_name)
 
-     {
 
-         if (array_key_exists($form_name, $this->flat_forms)) {
 
-             return $this->flat_forms[$form_name];
 
-         }
 
-         return null;
 
-     }
 
-     /**
 
-      * Determine if the page has a form submission that should be processed
 
-      *
 
-      * @return bool
 
-      */
 
-     protected function shouldProcessForm()
 
-     {
 
-         $status = isset($_POST) && isset($_POST['form-nonce']);
 
-         $refresh_prevention = null;
 
-         if ($status && $this->form()) {
 
-             // Set page template if passed by form
 
-             if (isset($this->form->template)) {
 
-                 $this->grav['page']->template($this->form->template);
 
-             }
 
-             if (!is_null($this->form->refresh_prevention)) {
 
-                 $refresh_prevention = (bool) $this->form->refresh_prevention;
 
-             } else {
 
-                 $refresh_prevention = $this->config->get('plugins.form.refresh_prevention', false);
 
-             }
 
-             $unique_form_id = filter_input(INPUT_POST, '__unique_form_id__', FILTER_SANITIZE_STRING);
 
-             if ($refresh_prevention && $unique_form_id) {
 
-                 if (($this->grav['session']->unique_form_id != $unique_form_id)) {
 
-                     $this->grav['session']->unique_form_id = $unique_form_id;
 
-                 } else {
 
-                     $status = false;
 
-                     $this->form->message = $this->grav['language']->translate('PLUGIN_FORM.FORM_ALREADY_SUBMITTED');
 
-                     $this->form->status = 'error';
 
-                 }
 
-             }
 
-         }
 
-         return $status;
 
-     }
 
-     /**
 
-      * Flatten the forms array into something that can be more easily searched
 
-      */
 
-     protected function flattenForms()
 
-     {
 
-         $this->flat_forms = Utils::arrayFlatten($this->forms);
 
-     }
 
-     /**
 
-      * Get the current form, should already be processed but can get it directly from the page if necessary
 
-      *
 
-      * @param null $page
 
-      * @return Form|mixed
 
-      */
 
-     protected function form($page = null)
 
-     {
 
-         // Regenerate list of flat_forms if not already populated
 
-         if (empty($this->flat_forms)) {
 
-             $this->flattenForms();
 
-         }
 
-         if (null === $this->form) {
 
-             $current_form_name = $this->getFormName($this->grav['page']);
 
-             $this->form = $this->getFormByName($current_form_name);
 
-         }
 
-         // last attempt using current page's form
 
-         if (null == $this->form) {
 
-             // try to get the page if possible
 
-             if ($page == null) {
 
-                 $page = $this->grav['page'];
 
-             }
 
-             if ($page) {
 
-                 $header = $page->header();
 
-                 if (isset($header->form)) {
 
-                     $this->form = new Form($page);
 
-                 }
 
-             }
 
-         }
 
-         return $this->form;
 
-     }
 
- }
 
 
  |