summaryrefslogtreecommitdiffstats
path: root/modules/setup/application/forms/DbResourcePage.php
blob: b3f17848046963253a82441597279e3ecbcd2634 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Forms;

use Exception;
use Icinga\Web\Form;
use Icinga\Forms\Config\Resource\DbResourceForm;
use Icinga\Module\Setup\Utils\DbTool;

/**
 * Wizard page to define connection details for a database resource
 */
class DbResourcePage extends Form
{
    /**
     * Initialize this page
     */
    public function init()
    {
        $this->setTitle($this->translate('Database Resource', 'setup.page.title'));
        $this->setValidatePartial(true);
    }

    /**
     * @see Form::createElements()
     */
    public function createElements(array $formData)
    {
        $this->addElement(
            'hidden',
            'type',
            array(
                'required'  => true,
                'value'     => 'db'
            )
        );

        if (isset($formData['skip_validation']) && $formData['skip_validation']) {
            $this->addSkipValidationCheckbox();
        } else {
            $this->addElement(
                'hidden',
                'skip_validation',
                array(
                    'required'  => true,
                    'value'     => 0
                )
            );
        }

        $resourceForm = new DbResourceForm();
        $this->addElements($resourceForm->createElements($formData)->getElements());
        $this->getElement('name')->setValue('icingaweb_db');
    }

    /**
     * Validate the given form data and check whether it's possible to connect to the database server
     *
     * @param   array   $data   The data to validate
     *
     * @return  bool
     */
    public function isValid($data)
    {
        if (false === parent::isValid($data)) {
            return false;
        }

        if (false === isset($data['skip_validation']) || $data['skip_validation'] == 0) {
            if (! $this->validateConfiguration()) {
                $this->addSkipValidationCheckbox();
                return false;
            }
        }

        return true;
    }

    /**
     * Check whether it's possible to connect to the database server
     *
     * This will only run the check if the user pushed the 'backend_validation' button.
     *
     * @param   array   $formData
     *
     * @return  bool
     */
    public function isValidPartial(array $formData)
    {
        if (isset($formData['backend_validation']) && parent::isValid($formData)) {
            if (! $this->validateConfiguration()) {
                return false;
            }

            $this->info($this->translate('The configuration has been successfully validated.'));
        } elseif (! isset($formData['backend_validation'])) {
            // This is usually done by isValid(Partial), but as we're not calling any of these...
            $this->populate($formData);
        }

        return true;
    }

    /**
     * Return whether the configuration is valid
     *
     * @return  bool
     */
    protected function validateConfiguration()
    {
        try {
            $db = new DbTool($this->getValues());
            $db->checkConnectivity();
        } catch (Exception $e) {
            $this->error(sprintf(
                $this->translate('Failed to successfully validate the configuration: %s'),
                $e->getMessage()
            ));
            return false;
        }

        $state = true;
        $connectionError = null;

        try {
            $db->connectToDb();
        } catch (Exception $e) {
            $connectionError = $e;
        }

        if ($connectionError === null && array_search('icinga_instances', $db->listTables(), true) !== false) {
            $this->warning($this->translate(
                'The database you\'ve configured to use for Icinga Web 2 seems to be the one of Icinga. Please be aware'
                . ' that this database configuration is supposed to be used for Icinga Web 2\'s configuration and that'
                . ' it is highly recommended to not mix different schemas in the same database. If this is intentional,'
                . ' you can skip the validation and ignore this warning. If not, please provide a different database.'
            ));
            $state = false;
        }

        if ($this->getValue('db') === 'pgsql') {
            if ($connectionError !== null) {
                $this->warning(sprintf(
                    $this->translate('Unable to check the server\'s version. This is usually not a critical error'
                    . ' as there is probably only access to the database permitted which does not exist yet. If you are'
                    . ' absolutely sure you are running PostgreSQL in a version equal to or newer than 9.1,'
                    . ' you can skip the validation and safely proceed to the next step. The error was: %s'),
                    $connectionError->getMessage()
                ));
                $state = false;
            } else {
                $version = $db->getServerVersion();
                if (version_compare($version, '9.1', '<')) {
                    $this->error(sprintf(
                        $this->translate('The server\'s version %s is too old. The minimum required version is %s.'),
                        $version,
                        '9.1'
                    ));
                    $state = false;
                }
            }
        }

        return $state;
    }

    /**
     * Add a checkbox to the form by which the user can skip the configuration validation
     */
    protected function addSkipValidationCheckbox()
    {
        $this->addElement(
            'checkbox',
            'skip_validation',
            array(
                'required'      => true,
                'label'         => $this->translate('Skip Validation'),
                'description'   => $this->translate('Check this to not to validate the configuration')
            )
        );
    }
}