summaryrefslogtreecommitdiffstats
path: root/modules/setup/library/Setup/Steps/GeneralConfigStep.php
blob: 5deb18d3164dbdfb9dce900f55c899568af01c16 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Steps;

use Exception;
use Icinga\Application\Logger;
use Icinga\Application\Config;
use Icinga\Exception\IcingaException;
use Icinga\Module\Setup\Step;

class GeneralConfigStep extends Step
{
    protected $data;

    protected $error;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function apply()
    {
        $config = array();
        foreach ($this->data['generalConfig'] as $sectionAndPropertyName => $value) {
            list($section, $property) = explode('_', $sectionAndPropertyName, 2);
            $config[$section][$property] = $value;
        }

        $config['global']['config_resource'] = $this->data['resourceName'];

        try {
            Config::fromArray($config)
                ->setConfigFile(Config::resolvePath('config.ini'))
                ->saveIni();
        } catch (Exception $e) {
            $this->error = $e;
            return false;
        }

        $this->error = false;
        return true;
    }

    public function getSummary()
    {
        $pageTitle = '<h2>' . mt('setup', 'Application Configuration', 'setup.page.title') . '</h2>';
        $generalTitle = '<h3>' . t('General', 'app.config') . '</h3>';
        $loggingTitle = '<h3>' . t('Logging', 'app.config') . '</h3>';

        $generalHtml = ''
            . '<ul>'
            . '<li>' . ($this->data['generalConfig']['global_show_stacktraces']
                ? t('An exception\'s stacktrace is shown to every user by default.')
                : t('An exception\'s stacktrace is hidden from every user by default.')
            ) . '</li>'
            . '<li>' . t('Preferences will be stored using a database.') . '</li>'
            . '</ul>';

        $type = $this->data['generalConfig']['logging_log'];
        if ($type === 'none') {
            $loggingHtml = '<p>' . mt('setup', 'Logging will be disabled.') . '</p>';
        } else {
            $level = $this->data['generalConfig']['logging_level'];

            $typeDescription = null;
            $typeSpecificHtml = null;
            switch ($type) {
                case 'php':
                    $typeDescription = t('Webserver Log', 'app.config.logging.type');
                    $typeSpecificHtml = '';
                    break;

                case 'syslog':
                    $typeDescription = 'Syslog';
                    $typeSpecificHtml = '<td><strong>' . t('Application Prefix') . '</strong></td>'
                        . '<td>' . $this->data['generalConfig']['logging_application'] . '</td>';
                    break;

                case 'file':
                    $typeDescription = t('File', 'app.config.logging.type');
                    $typeSpecificHtml = '<td><strong>' . t('Filepath') . '</strong></td>'
                        . '<td>' . $this->data['generalConfig']['logging_file'] . '</td>';
                    break;
            }

            $loggingHtml = ''
                . '<table>'
                . '<tbody>'
                . '<tr>'
                . '<td><strong>' . t('Type', 'app.config.logging') . '</strong></td>'
                . '<td>' . $typeDescription . '</td>'
                . '</tr>'
                . '<tr>'
                . '<td><strong>' . t('Level', 'app.config.logging') . '</strong></td>'
                . '<td>' . ($level === Logger::$levels[Logger::ERROR] ? t('Error', 'app.config.logging.level') : (
                    $level === Logger::$levels[Logger::WARNING] ? t('Warning', 'app.config.logging.level') : (
                        $level === Logger::$levels[Logger::INFO] ? t('Information', 'app.config.logging.level') : (
                            t('Debug', 'app.config.logging.level')
                        )
                    )
                )) . '</td>'
                . '</tr>'
                . '<tr>'
                . $typeSpecificHtml
                . '</tr>'
                . '</tbody>'
                . '</table>';
        }

        return $pageTitle . '<div class="topic">' . $generalTitle . $generalHtml . '</div>'
            . '<div class="topic">' . $loggingTitle . $loggingHtml . '</div>';
    }

    public function getReport()
    {
        if ($this->error === false) {
            return array(sprintf(
                mt('setup', 'General configuration has been successfully written to: %s'),
                Config::resolvePath('config.ini')
            ));
        } elseif ($this->error !== null) {
            return array(
                sprintf(
                    mt('setup', 'General configuration could not be written to: %s. An error occured:'),
                    Config::resolvePath('config.ini')
                ),
                sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->error))
            );
        }
    }
}