1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\Extension\AuthPlugin;
6use dokuwiki\Form\Form;
7use dokuwiki\JWT;
8
9/**
10 * DokuWiki User Profile Interface
11 *
12 * @package dokuwiki\Ui
13 */
14class UserProfile extends Ui
15{
16    /**
17     * Display the User Profile Form Panel
18     *
19     * @return void
20     * @author   Andreas Gohr <andi@splitbrain.org>
21     *
22     */
23    public function show()
24    {
25        /** @var AuthPlugin $auth */
26        global $auth;
27        global $INFO;
28        global $INPUT;
29
30        $userinfo = [
31            'user' => $_SERVER['REMOTE_USER'],
32            'name' => $INPUT->post->str('fullname', $INFO['userinfo']['name'], true),
33            'mail' => $INPUT->post->str('email', $INFO['userinfo']['mail'], true),
34
35        ];
36
37        echo p_locale_xhtml('updateprofile');
38        echo '<div class="centeralign">';
39
40        echo $this->updateProfileForm($userinfo)->toHTML('UpdateProfile');
41        echo $this->tokenForm($userinfo['user'])->toHTML();
42        if ($auth->canDo('delUser') && actionOK('profile_delete')) {
43            echo $this->deleteProfileForm()->toHTML('ProfileDelete');
44        }
45
46        echo '</div>';
47    }
48
49    /**
50     * Add the password confirmation field to the form if configured
51     *
52     * @param Form $form
53     * @return void
54     */
55    protected function addPasswordConfirmation(Form $form)
56    {
57        global $lang;
58        global $conf;
59
60        if (!$conf['profileconfirm']) return;
61        $form->addHTML("<br>\n");
62        $attr = ['size' => '50', 'required' => 'required'];
63        $input = $form->addPasswordInput('oldpass', $lang['oldpass'])->attrs($attr)
64            ->addClass('edit');
65        $input->getLabel()->attr('class', 'block');
66        $form->addHTML("<br>\n");
67    }
68
69    /**
70     * Create the profile form
71     *
72     * @return Form
73     */
74    protected function updateProfileForm($userinfo)
75    {
76        global $lang;
77        /** @var AuthPlugin $auth */
78        global $auth;
79
80        $form = new Form(['id' => 'dw__register']);
81        $form->addTagOpen('div')->addClass('no');
82        $form->addFieldsetOpen($lang['profile']);
83        $form->setHiddenField('do', 'profile');
84        $form->setHiddenField('save', '1');
85
86        $attr = ['size' => '50', 'disabled' => 'disabled'];
87        $input = $form->addTextInput('login', $lang['user'])
88            ->attrs($attr)
89            ->addClass('edit')
90            ->val($userinfo['user']);
91        $input->getLabel()->attr('class', 'block');
92        $form->addHTML("<br>\n");
93
94        $attr = ['size' => '50'];
95        if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
96        $input = $form->addTextInput('fullname', $lang['fullname'])
97            ->attrs($attr)
98            ->addClass('edit')
99            ->val($userinfo['name']);
100        $input->getLabel()->attr('class', 'block');
101        $form->addHTML("<br>\n");
102
103        $attr = ['type' => 'email', 'size' => '50'];
104        if (!$auth->canDo('modMail')) $attr['disabled'] = 'disabled';
105        $input = $form->addTextInput('email', $lang['email'])
106            ->attrs($attr)
107            ->addClass('edit')
108            ->val($userinfo['mail']);
109        $input->getLabel()->attr('class', 'block');
110        $form->addHTML("<br>\n");
111
112        if ($auth->canDo('modPass')) {
113            $attr = ['size' => '50'];
114            $input = $form->addPasswordInput('newpass', $lang['newpass'])->attrs($attr)->addClass('edit');
115            $input->getLabel()->attr('class', 'block');
116            $form->addHTML("<br>\n");
117
118            $input = $form->addPasswordInput('passchk', $lang['passchk'])->attrs($attr)->addClass('edit');
119            $input->getLabel()->attr('class', 'block');
120            $form->addHTML("<br>\n");
121        }
122
123        $this->addPasswordConfirmation($form);
124
125        $form->addButton('', $lang['btn_save'])->attr('type', 'submit');
126        $form->addButton('', $lang['btn_reset'])->attr('type', 'reset');
127
128        $form->addFieldsetClose();
129        $form->addTagClose('div');
130
131        return $form;
132    }
133
134    /**
135     * Create the profile delete form
136     *
137     * @return Form
138     */
139    protected function deleteProfileForm()
140    {
141        global $lang;
142
143        $form = new Form(['id' => 'dw__profiledelete']);
144        $form->addTagOpen('div')->addClass('no');
145        $form->addFieldsetOpen($lang['profdeleteuser']);
146        $form->setHiddenField('do', 'profile_delete');
147        $form->setHiddenField('delete', '1');
148
149        $form->addCheckbox('confirm_delete', $lang['profconfdelete'])
150            ->attrs(['required' => 'required'])
151            ->id('dw__confirmdelete')
152            ->val('1');
153
154        $this->addPasswordConfirmation($form);
155
156        $form->addButton('', $lang['btn_deleteuser'])->attr('type', 'submit');
157        $form->addFieldsetClose();
158        $form->addTagClose('div');
159        return $form;
160    }
161
162    /**
163     * Get the authentication token form
164     *
165     * @param string $user
166     * @return Form
167     */
168    protected function tokenForm($user)
169    {
170        global $lang;
171
172        $token = JWT::fromUser($user);
173
174        $form = new Form(['id' => 'dw__profiletoken', 'action' => wl(), 'method' => 'POST']);
175        $form->setHiddenField('do', 'authtoken');
176        $form->setHiddenField('id', 'ID');
177        $form->addFieldsetOpen($lang['proftokenlegend']);
178        $form->addHTML('<p>' . $lang['proftokeninfo'] . '</p>');
179        $form->addHTML('<p><code style="display: block; word-break: break-word">' . $token->getToken() . '</code></p>');
180        $form->addButton('regen', $lang['proftokengenerate']);
181        $form->addFieldsetClose();
182
183        return $form;
184    }
185}
186