2014-09-29 12:27:43 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-09-29 12:27:43 +02:00
|
|
|
|
2014-11-14 11:01:16 +01:00
|
|
|
namespace Icinga\Module\Setup\Forms;
|
2014-09-29 12:27:43 +02:00
|
|
|
|
|
|
|
use PDOException;
|
|
|
|
use Icinga\Web\Form;
|
2014-11-14 10:57:14 +01:00
|
|
|
use Icinga\Forms\Config\Resource\DbResourceForm;
|
2014-11-10 16:31:40 +01:00
|
|
|
use Icinga\Module\Setup\Utils\DbTool;
|
2014-09-29 12:27:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wizard page to define connection details for a database resource
|
|
|
|
*/
|
|
|
|
class DbResourcePage extends Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Initialize this page
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
2015-03-02 18:36:04 +01:00
|
|
|
$this->setTitle($this->translate('Database Resource', 'setup.page.title'));
|
2015-07-29 10:52:32 +02:00
|
|
|
$this->setValidatePartial(true);
|
2014-09-29 12:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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());
|
2014-11-11 12:41:02 +01:00
|
|
|
$this->getElement('name')->setValue('icingaweb_db');
|
2014-09-29 12:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-08-18 16:20:25 +02:00
|
|
|
if (! $this->validateConfiguration()) {
|
2014-09-29 12:27:43 +02:00
|
|
|
$this->addSkipValidationCheckbox();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-29 10:52:32 +02:00
|
|
|
/**
|
|
|
|
* 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)) {
|
2015-08-18 16:20:25 +02:00
|
|
|
if (! $this->validateConfiguration()) {
|
2015-07-29 10:52:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info($this->translate('The configuration has been successfully validated.'));
|
2015-07-29 16:17:14 +02:00
|
|
|
} elseif (! isset($formData['backend_validation'])) {
|
|
|
|
// This is usually done by isValid(Partial), but as we're not calling any of these...
|
|
|
|
$this->populate($formData);
|
2015-07-29 10:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-18 16:20:25 +02:00
|
|
|
/**
|
|
|
|
* Return whether the configuration is valid
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function validateConfiguration()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$db = new DbTool($this->getValues());
|
|
|
|
$db->checkConnectivity();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->error(sprintf(
|
|
|
|
$this->translate('Failed to successfully validate the configuration: %s'),
|
|
|
|
$e->getMessage()
|
|
|
|
));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->getValue('db') === 'pgsql') {
|
|
|
|
if (! $db->isConnected()) {
|
|
|
|
try {
|
|
|
|
$db->connectToDb();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->warning($this->translate(sprintf(
|
|
|
|
'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',
|
|
|
|
$e->getMessage()
|
|
|
|
)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$version = $db->getServerVersion();
|
|
|
|
if (version_compare($version, '9.1', '<')) {
|
|
|
|
$this->error($this->translate(sprintf(
|
|
|
|
'The server\'s version %s is too old. The minimum required version is %s.',
|
|
|
|
$version,
|
|
|
|
'9.1'
|
|
|
|
)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-29 12:27:43 +02:00
|
|
|
/**
|
|
|
|
* Add a checkbox to the form by which the user can skip the connection validation
|
|
|
|
*/
|
|
|
|
protected function addSkipValidationCheckbox()
|
|
|
|
{
|
|
|
|
$this->addElement(
|
|
|
|
'checkbox',
|
|
|
|
'skip_validation',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
2015-01-19 11:07:39 +01:00
|
|
|
'label' => $this->translate('Skip Validation'),
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Check this to not to validate connectivity with the given database server'
|
|
|
|
)
|
2014-09-29 12:27:43 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|