aboutsummaryrefslogtreecommitdiff
path: root/srcs/phpmyadmin/libraries/classes/UserPreferencesHeader.php
blob: cecbb05cdc7dca5ad594b2b8af6ce4e7a0ce9461 (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
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Holds the PhpMyAdmin\UserPreferencesHeader class
 *
 * @package PhpMyAdmin
 */
declare(strict_types=1);

namespace PhpMyAdmin;

use PhpMyAdmin\Config\Forms\User\UserFormList;
use Throwable;
use Twig_Error_Loader;
use Twig_Error_Runtime;
use Twig_Error_Syntax;

/**
 * Functions for displaying user preferences header
 *
 * @package PhpMyAdmin
 */
class UserPreferencesHeader
{
    /**
     * Get HTML content
     *
     * @param Template $template Template object used to render data
     * @param Relation $relation Relation object
     *
     * @return string
     * @throws Throwable
     * @throws Twig_Error_Loader
     * @throws Twig_Error_Runtime
     * @throws Twig_Error_Syntax
     */
    public static function getContent(Template $template, Relation $relation): string
    {
        return self::displayTabs($template)
            . self::displayConfigurationSavedMessage()
            . self::sessionStorageWarning($relation);
    }

    /**
     * @param Template $template Template object used to render data
     *
     * @return string
     * @throws Throwable
     * @throws Twig_Error_Loader
     * @throws Twig_Error_Runtime
     * @throws Twig_Error_Syntax
     */
    protected static function displayTabs(Template $template): string
    {
        // build user preferences menu
        $content = Util::getHtmlTab(
            [
                'link' => 'prefs_manage.php',
                'text' => __('Manage your settings'),
            ]
        ) . "\n";
        /* Second authentication factor */
        $content .= Util::getHtmlTab(
            [
                'link' => 'prefs_twofactor.php',
                'text' => __('Two-factor authentication'),
            ]
        ) . "\n";

        $content .= self::displayTabsWithIcon();

        return $template->render(
            'list/unordered',
            [
                'id' => 'topmenu2',
                'class' => 'user_prefs_tabs',
                'content' => $content,
            ]
        ) . '<div class="clearfloat"></div>';
    }

    /**
     * @return string
     */
    protected static function displayTabsWithIcon(): string
    {
        $form_param = $_GET['form'] ?? null;
        $tabs_icons = [
            'Features' => 'b_tblops',
            'Sql' => 'b_sql',
            'Navi' => 'b_select',
            'Main' => 'b_props',
            'Import' => 'b_import',
            'Export' => 'b_export',
        ];
        $script_name = basename($GLOBALS['PMA_PHP_SELF']);
        $content = null;
        foreach (UserFormList::getAll() as $formset) {
            $formset_class = UserFormList::get($formset);
            $tab = [
                'link' => 'prefs_forms.php',
                'text' => $formset_class::getName(),
                'icon' => $tabs_icons[$formset],
                'active' => 'prefs_forms.php' === $script_name && $formset === $form_param,
            ];
            $content .= Util::getHtmlTab($tab, ['form' => $formset]) . "\n";
        }
        return $content;
    }

    /**
     * @return string|null
     */
    protected static function displayConfigurationSavedMessage(): ?string
    {
        // show "configuration saved" message and reload navigation panel if needed
        if (! empty($_GET['saved'])) {
            return Message::rawSuccess(__('Configuration has been saved.'))
                ->getDisplay();
        }

        return null;
    }

    /**
     * @param Relation $relation Relation instance
     *
     * @return string|null
     */
    protected static function sessionStorageWarning(Relation $relation): ?string
    {
        // warn about using session storage for settings
        $cfgRelation = $relation->getRelationsParam();
        if (! $cfgRelation['userconfigwork']) {
            $msg = __(
                'Your preferences will be saved for current session only. Storing them '
                . 'permanently requires %sphpMyAdmin configuration storage%s.'
            );
            $msg = Sanitize::sanitizeMessage(
                sprintf($msg, '[doc@linked-tables]', '[/doc]')
            );
            return Message::notice($msg)
                ->getDisplay();
        }

        return null;
    }
}