Kirby Uniform

repository·master·Indexed 42 days ago

https://github.com/mzur/kirby-uniform

A versatile plugin for the Kirby CMS designed to handle web form actions, validation, and submission processing. It provides built-in actions such as emailAction, emailSelectAction, and logAction, and allows developers to create custom actions by extending the Uniform\Actions\Action base class.

Tokens
27.9K
Snippets
91
Records
116
Agent score
53%

What's inside Kirby Uniform

  1. What is the Honeytime Guard and how does it work?

    master
    The Honeytime Guard is a bot protection mechanism that uses a hidden form field containing an encrypted timestamp. When a form is submitted, the guard compares this timestamp against the current time. If the submission occurs faster than the configured threshold (defaulting to 10 seconds), the submission is rejected. This prevents bots from forging the field because the timestamp is encrypted using a secret key.
  2. How Guards work in Uniform

    master

    Guards are the spam protection mechanism in Uniform. By default, if no guard is explicitly specified, the HoneypotGuard is used. You can chain multiple guards together to provide layered protection. If you need to bypass all spam protection for a specific form instance, you can use the withoutGuards() method before calling any other actions.

    use Uniformorm;
    
    $form = new Form;
    if (kirby()->request()->is('POST')) {
        $form->honeypotGuard()
            ->calcGuard(['option' => 'value'])
            // more guards or call actions
    }
  3. How the Honeypot Guard works

    master

    The Honeypot Guard detects bot submissions by using a camouflaged form field. This field is visually hidden from human users but remains visible to bots. If the field contains any content upon submission, the guard rejects the request.

    By default, the field is named website and uses the CSS class uniform__potty to hide itself. You must ensure the uniform__potty class is defined in your CSS to prevent human users from seeing the field.

  4. How guards and actions work together

    master

    Uniform uses a layered approach to process form submissions:

    1. CSRF Validation: Automatically checked before any other step.
    2. Validation: Form fields are checked against the rules defined in the constructor.
    3. Guards: Mechanisms for spam protection (e.g., honeypotGuard, calcGuard). If no guard is specified, honeypotGuard is used by default.
    4. Actions: The final processing step (e.g., emailAction, logAction).

    Chaining Guards and Actions

    You can chain multiple guards or actions. Note that if any step fails, the process stops and redirects back to the form.

    if ($kirby->request()->is('POST')) {
        $form->honeypotGuard()
            ->calcGuard(['field' => 'result'])
            ->emailAction(['to' => 'user@example.com'])
            ->logAction(['file' => 'path/to/log.txt']);
    }

    To skip spam protection entirely, use withoutGuards():

    $form->withoutGuards()->emailAction([...]);
  5. How actions work in Uniform

    master

    Actions are used to process submitted web form data. They are only executed if the form data passes all configured guards. Multiple actions can be chained together, but if any action fails, the subsequent actions in the chain will not be executed.

    If no guard is explicitly called, the first action in the chain will automatically execute the default HoneypotGuard.

    To chain actions, call them sequentially on the Uniform\ Form instance:

    use Uniform\Form;
    
    $form = new Form;
    if (kirby()->request()->is('POST')) {
        $form->emailAction([/* options */])
            ->logAction([/* options */])
            ->webhookAction([/* options */]);
    }
  6. Render form fields with error handling and old values

    master

    When rendering a form in a template, use the following methods from the $form instance to ensure a good user experience:

    • $form->old('field_name'): Retrieves the value previously submitted by the user so the field can be pre-populated after a validation error.
    • $form->error('field_name'): Returns true if the specific field has validation errors, allowing you to apply CSS classes like error.
    • $form->error('field_name') (returning array): Returns an array of error messages for that field.

    For global action errors (like an emailAction failing), you can check for errors using the class name: $form->error(\Uniform\Actions\EmailAction::class).

    <input
        <?php if ($form->error('name')): ?> class="error"<?php endif; ?>
        name="name"
        type="text"
        value="<?php echo $form->old('name') ?>"
    >
    
    <!-- Displaying error messages -->
    <?php if ($form->error('name')): ?>
        <p class="error-text"><?php echo implode('<br>', $form->error('name')) ?></p>
    <?php endif; ?>
  7. Configure Email transport in Uniform v4

    master

    Uniform v4 uses Kirby 3's native email handling via PHPMailer. You can no longer define custom email services within Uniform actions.

    To configure transport settings (like SMTP), use the standard Kirby configuration. You must remove the service and service-options keys from the EmailAction options array.

  8. Update Email subject templates for Uniform v4

    master

    String templating in Uniform v4 (Kirby 3) uses double curly braces {{ }} for variables instead of single braces { }.

    Note on missing variables: In Uniform v4, if a variable in a template has no replacement, it will be removed from the string entirely rather than being left as literal text.

    $form->emailAction([
        // ...
        'subject' => 'New request from {{firstname}} {{lastname}}',
    ]);
  9. Configure Kirby cache and sessions for Uniform

    master

    When using Uniform, keep the following requirements in mind:

    • Disable Kirby cache: For pages where you use Uniform, you must disable the Kirby cache to ensure forms are generated dynamically.
    • Session Cookies: Uniform utilizes Kirby sessions, which requires a session cookie to be correctly set in the user's browser.
  10. Use the Upload Action to handle file uploads

    master

    The uploadAction handles one or more uploaded files and moves them to a specified directory.

    Important Security Note: The uploadAction does not perform any validation (such as file size or MIME type) by itself. You must use Uniform validation rules (like file, mime, and filesize) to ensure uploaded files are safe and meet your requirements. Always use a whitelist of allowed MIME types and a maximum file size.

    $form->uploadAction(['fields' => [
        'filefield' => [
            'target' => $target_directory,
            'prefix' => $prefix_string,
        ],
    ]]);
  11. Configure Kirby Uniform setup

    master

    After installation, perform the following two steps to ensure correct functionality:

    1. CSS Setup: Add the following rule to your CSS to hide the honeypot field used for spam protection:
      .uniform__potty {
          position: absolute;
          left: -9999px;
      }
    2. Disable Cache: Disable the Kirby cache for any pages where you are using Uniform. This ensures that form instances are generated dynamically and correctly handle state.
    .uniform__potty {
        position: absolute;
        left: -9999px;
    }