1<?php
2
3use ComboStrap\Bootstrap;
4use ComboStrap\Identity;
5use ComboStrap\LogUtility;
6use ComboStrap\PluginUtility;
7use ComboStrap\Snippet;
8
9if (!defined('DOKU_INC')) die();
10require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
11
12/**
13 *
14 */
15class action_plugin_combo_profile extends DokuWiki_Action_Plugin
16{
17
18    const CANONICAL = Identity::CANONICAL;
19    const TAG_UPDATE = "profile-update";
20    const TAG_DELETE = "profile-delete";
21    const FORM_PROFILE_UPDATE_CLASS = "form-" . self::TAG_UPDATE;
22    const FORM_PROFILE_DELETE_CLASS = "form-" . self::TAG_DELETE;
23    const CONF_ENABLE_PROFILE_UPDATE_FORM = "enableProfileUpdateForm";
24    const CONF_ENABLE_PROFILE_DELETE_FORM = "enableProfileDeleteForm";
25
26
27
28    function register(Doku_Event_Handler $controller)
29    {
30        /**
31         * To modify the profile update form and add class
32         *
33         * Deprecated object passed by the event but still in use
34         * https://www.dokuwiki.org/devel:event:html_updateprofileform_output
35         *
36         * Event using the new object but not found anywhere
37         * https://www.dokuwiki.org/devel:event:form_updateprofile_output
38         */
39        if (PluginUtility::getConfValue(self::CONF_ENABLE_PROFILE_UPDATE_FORM,1)) {
40            $controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handle_profile_update', array());
41        }
42
43        /**
44         * To modify the register form and add class
45         *
46         * Deprecated object passed by the event but still in use
47         * https://www.dokuwiki.org/devel:event:html_profiledeleteform_output
48         *
49         * Event using the new object but not found anywhere
50         * https://www.dokuwiki.org/devel:event:form_profiledelete_output
51         */
52        if (PluginUtility::getConfValue(self::CONF_ENABLE_PROFILE_DELETE_FORM,1)) {
53            $controller->register_hook('HTML_PROFILEDELETEFORM_OUTPUT', 'BEFORE', $this, 'handle_profile_delete', array());
54        }
55
56
57
58
59    }
60
61    function handle_profile_update(&$event, $param)
62    {
63
64        /**
65         * The profile page is created via buffer
66         * We print before the forms to avoid a FOUC
67         */
68        print Snippet::createCssSnippet(self::TAG_UPDATE)
69            ->getHtmlStyleTag();
70
71        /**
72         * @var Doku_Form $form
73         */
74        $form = &$event->data;
75        $class = &$form->params["class"];
76        if (isset($class)) {
77            $class = $class . " " . self::FORM_PROFILE_UPDATE_CLASS;
78        } else {
79            $class = self::FORM_PROFILE_UPDATE_CLASS;
80        }
81        $newFormContent = [];
82
83        /**
84         * Header (Logo / Title)
85         */
86        $newFormContent[] = Identity::getHeaderHTML($form, self::FORM_PROFILE_UPDATE_CLASS);
87
88
89        /**
90         * Form Attributes
91         * https://getbootstrap.com/docs/5.0/forms/layout/#horizontal-form
92         */
93        $rowClass = "row";
94        if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFourMajorVersion) {
95            $rowClass .= " form-group";
96        }
97        $firstColWeight = 5;
98        $secondColWeight = 12 - $firstColWeight;
99
100
101        /**
102         * Replace the field
103         *
104         * The password text localized by lang is shared
105         * between the password and the password check field
106         */
107        $passwordText = "Password";
108        foreach ($form->_content as $field) {
109            if (!is_array($field)) {
110                continue;
111            }
112            $fieldName = $field["name"];
113            if ($fieldName == null) {
114                // this is not an input field
115                switch ($field["type"]) {
116                    case "submit":
117                        /**
118                         * This is important to keep the submit element intact
119                         * for forms integration such as captcha
120                         * The search the submit button to insert before it
121                         */
122                        $classes = "btn btn-primary";
123                        if (isset($field["class"])) {
124                            $field["class"] = $field["class"] . " " . $classes;
125                        } else {
126                            $field["class"] = $classes;
127                        }
128                        $field["tabindex"] = "6";
129                        $newFormContent[] = $field;
130                        break;
131                    case "reset":
132                        $classes = "btn btn-secondary";
133                        if (isset($field["class"])) {
134                            $field["class"] = $field["class"] . " " . $classes;
135                        } else {
136                            $field["class"] = $classes;
137                        }
138                        $field["tabindex"] = "7";
139                        $newFormContent[] = $field;
140                        break;
141                }
142                continue;
143            }
144            switch ($fieldName) {
145                case "login":
146                    $loginText = $field["_text"];
147                    $loginValue = $field["value"];
148                    $loginHTML = <<<EOF
149<div class="$rowClass">
150    <label for="inputUserName" class="col-sm-$firstColWeight col-form-label">$loginText</label>
151    <div class="col-sm-$secondColWeight">
152      <input type="text" class="form-control" id="inputUserName" placeholder="Username" name="$fieldName" value="$loginValue" disabled>
153    </div>
154</div>
155EOF;
156                    $newFormContent[] = $loginHTML;
157                    break;
158                case "fullname":
159                    $fullNameText = $field["_text"];
160                    $fullNameValue = $field["value"];
161                    $fullNameHtml = <<<EOF
162<div class="$rowClass">
163    <label for="inputRealName" class="col-sm-$firstColWeight col-form-label">$fullNameText</label>
164    <div class="col-sm-$secondColWeight">
165      <input type="text" class="form-control" id="inputRealName" placeholder="$fullNameText" tabindex="1" name="$fieldName" value="$fullNameValue" required="required">
166    </div>
167</div>
168EOF;
169                    $newFormContent[] = $fullNameHtml;
170                    break;
171                case "email":
172                    $emailText = $field["_text"];
173                    $emailValue = $field["value"];
174                    $emailHTML = <<<EOF
175<div class="$rowClass">
176    <label for="inputEmail" class="col-sm-$firstColWeight col-form-label">$emailText</label>
177    <div class="col-sm-$secondColWeight">
178      <input type="email" class="form-control" id="inputEmail" placeholder="name@example.com" tabindex="2" name="$fieldName" value="$emailValue" required="required">
179    </div>
180</div>
181EOF;
182                    $newFormContent[] = $emailHTML;
183                    break;
184                case "newpass":
185                    $passwordText = $field["_text"];
186                    $passwordHtml = <<<EOF
187<div class="$rowClass">
188    <label for="inputPassword" class="col-sm-$firstColWeight col-form-label">$passwordText</label>
189    <div class="col-sm-$secondColWeight">
190      <input type="password" class="form-control" id="inputPassword" placeholder="$passwordText" tabindex="3" name="$fieldName">
191    </div>
192</div>
193EOF;
194                    $newFormContent[] = $passwordHtml;
195                    break;
196                case "passchk":
197                    $passwordCheckText = $field["_text"];
198                    $passwordCheckHtml = <<<EOF
199<div class="$rowClass">
200    <label for="inputPasswordCheck" class="col-sm-$firstColWeight col-form-label">$passwordCheckText</label>
201    <div class="col-sm-$secondColWeight">
202      <input type="password" class="form-control" id="inputPasswordCheck" placeholder="$passwordText" tabindex="4" name="$fieldName">
203    </div>
204</div>
205EOF;
206                    $newFormContent[] = $passwordCheckHtml;
207                    break;
208                case "oldpass":
209                    $passwordCheckText = $field["_text"];
210                    $passwordCheckHtml = <<<EOF
211<div class="$rowClass">
212    <label for="inputPasswordCheck" class="col-sm-$firstColWeight col-form-label">$passwordCheckText</label>
213    <div class="col-sm-$secondColWeight">
214      <input type="password" class="form-control" id="inputPasswordCheck" placeholder="$passwordCheckText" tabindex="5" name="$fieldName" required="required">
215    </div>
216</div>
217EOF;
218                    $newFormContent[] = $passwordCheckHtml;
219                    break;
220
221
222                default:
223                    $tag = self::TAG_UPDATE;
224                    LogUtility::msg("The $tag field name ($fieldName) is unknown", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
225
226            }
227        }
228
229
230        /**
231         * Update
232         */
233        $form->_content = $newFormContent;
234        return true;
235
236
237    }
238
239    public function handle_profile_delete($event,$param){
240
241        /**
242         * The profile page is created via buffer
243         * We print before the forms to avoid a FOUC
244         */
245        print Snippet::createCssSnippet(self::TAG_DELETE)
246            ->getHtmlStyleTag();
247
248        /**
249         * @var Doku_Form $form
250         */
251        $form = &$event->data;
252        $class = &$form->params["class"];
253        if (isset($class)) {
254            $class = $class . " " . self::FORM_PROFILE_DELETE_CLASS;
255        } else {
256            $class = self::FORM_PROFILE_DELETE_CLASS;
257        }
258        $newFormContent = [];
259
260        /**
261         * Header (Logo / Title)
262         */
263        $newFormContent[] = Identity::getHeaderHTML($form, self::FORM_PROFILE_DELETE_CLASS,false);
264
265        /**
266         * Field
267         */
268        foreach ($form->_content as $field) {
269            if (!is_array($field)) {
270                continue;
271            }
272            $fieldName = $field["name"];
273            if ($fieldName == null) {
274                // this is not an input field
275                if ($field["type"] == "submit") {
276                    /**
277                     * This is important to keep the submit element intact
278                     * for forms integration such as captcha
279                     * They search the submit button to insert before it
280                     */
281                    $classes = "btn btn-primary btn-block";
282                    if (isset($field["class"])) {
283                        $field["class"] = $field["class"] . " " . $classes;
284                    } else {
285                        $field["class"] = $classes;
286                    }
287                    $newFormContent[] = $field;
288                }
289                continue;
290            }
291            switch ($fieldName) {
292                case "oldpass":
293                    $passwordText = $field["_text"];
294                    $passwordFieldHTML = <<<EOF
295<div>
296    <input type="password" class="form-control" placeholder="$passwordText" required="required" name="$fieldName">
297</div>
298EOF;
299                    $newFormContent[] = $passwordFieldHTML;
300                    break;
301                case "confirm_delete":
302                    $confirmText = $field["_text"];
303                    $ConfirmValue = $field["value"];
304                    $rememberMeHtml = <<<EOF
305<div class="checkbox rememberMe">
306    <label><input type="checkbox" name="$fieldName" value="$ConfirmValue" required="required"> $confirmText</label>
307</div>
308EOF;
309                    $newFormContent[] = $rememberMeHtml;
310                    break;
311                default:
312                    $tag = self::TAG_DELETE;
313                    LogUtility::msg("The $tag field name ($fieldName) is unknown", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
314
315
316            }
317        }
318        $form->_content = $newFormContent;
319        return true;
320    }
321
322
323}
324
325