summaryrefslogtreecommitdiffstats
path: root/application/forms/ChromeBinaryForm.php
blob: c72d933cba8efd415a4493d7332e06890002365c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php

/* Icinga PDF Export | (c) 2019 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Pdfexport\Forms;

use Exception;
use Icinga\Forms\ConfigForm;
use Icinga\Module\Pdfexport\HeadlessChrome;
use Zend_Validate_Callback;

class ChromeBinaryForm extends ConfigForm
{
    public function init()
    {
        $this->setName('pdfexport_binary');
        $this->setSubmitLabel($this->translate('Save Changes'));
    }

    public function createElements(array $formData)
    {
        $this->addElement('text', 'chrome_binary', [
            'label'       => $this->translate('Local Binary'),
            'placeholder' => '/usr/bin/google-chrome',
            'validators'  => [new Zend_Validate_Callback(function ($value) {
                $chrome = (new HeadlessChrome())
                    ->setBinary($value);

                try {
                    $version = $chrome->getVersion();
                } catch (Exception $e) {
                    $this->getElement('chrome_binary')->addError($e->getMessage());
                    return true;
                }

                if ($version < 59) {
                    $this->getElement('chrome_binary')->addError(sprintf(
                        $this->translate(
                            'Chrome/Chromium supporting headless mode required'
                            . ' which is provided since version 59. Version detected: %s'
                        ),
                        $version
                    ));
                }

                return true;
            })]
        ]);

        $this->addElement('checkbox', 'chrome_force_temp_storage', [
            'label'     => $this->translate('Force local temp storage')
        ]);

        $this->addElement('text', 'chrome_host', [
            'label'         => $this->translate('Remote Host'),
            'validators'    => [new Zend_Validate_Callback(function ($value) {
                if ($value === null) {
                    return true;
                }

                $port = $this->getValue('chrome_port') ?: 9222;

                $chrome = (new HeadlessChrome())
                    ->setRemote($value, $port);

                try {
                    $version = $chrome->getVersion();
                } catch (Exception $e) {
                    $this->getElement('chrome_host')->addError($e->getMessage());
                    return true;
                }

                if ($version < 59) {
                    $this->getElement('chrome_host')->addError(sprintf(
                        $this->translate(
                            'Chrome/Chromium supporting headless mode required'
                            . ' which is provided since version 59. Version detected: %s'
                        ),
                        $version
                    ));
                }

                return true;
            })]
        ]);

        $this->addElement('number', 'chrome_port', [
            'label'         => $this->translate('Remote Port'),
            'placeholder'   => 9222,
            'min'           => 1,
            'max'           => 65535
        ]);
    }
}