xref: /plugin/combo/action/login.php (revision a6bf47aa01c4ea7d24944b0e48eb1943151e3c25)
1<?php
2/**
3 * Action Component
4 * Add a button in the edit toolbar
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Nicolas GERARD
8 */
9
10use ComboStrap\Identity;
11use ComboStrap\LogUtility;
12use ComboStrap\PluginUtility;
13use ComboStrap\Snippet;
14use dokuwiki\Menu\Item\Login;
15
16if (!defined('DOKU_INC')) die();
17require_once(__DIR__ . '/../class/PluginUtility.php');
18
19/**
20 * Class action_plugin_combo_login
21 *
22 * $conf['rememberme']
23 */
24class action_plugin_combo_login extends DokuWiki_Action_Plugin
25{
26
27
28    const CANONICAL = Identity::CANONICAL;
29    const TAG = "login";
30    const FORM_LOGIN_CLASS = "form-" . self::TAG;
31
32    const CONF_ENABLE_LOGIN_FORM = "enableLoginForm";
33
34
35    function register(Doku_Event_Handler $controller)
36    {
37        /**
38         * To modify the form and add class
39         *
40         * Deprecated object passed by the event but still in use
41         * https://www.dokuwiki.org/devel:event:html_loginform_output
42         */
43        if (PluginUtility::getConfValue(self::CONF_ENABLE_LOGIN_FORM, 1)) {
44            $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_login_html', array());
45        }
46
47        /**
48         * Event using the new object but only in use in
49         * the {@link https://codesearch.dokuwiki.org/xref/dokuwiki/lib/plugins/authad/action.php authad plugin}
50         * (ie login against active directory)
51         *
52         * https://www.dokuwiki.org/devel:event:form_login_output
53         */
54        // $controller->register_hook('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handle_login_html', array());
55
56
57    }
58
59    function handle_login_html(&$event, $param)
60    {
61
62        /**
63         * The Login page is created via buffer
64         * We print before the forms
65         * to avoid a FOUC
66         */
67        $loginCss = Snippet::createCssSnippet(self::TAG);
68        $content = $loginCss->getContent();
69        $class = $loginCss->getClass();
70        $cssHtml = <<<EOF
71<style class="$class">
72$content
73</style>
74EOF;
75        print $cssHtml;
76
77
78        /**
79         * @var Doku_Form $form
80         */
81        $form = &$event->data;
82        $form->params["class"] = self::FORM_LOGIN_CLASS;
83
84
85        /**
86         * Heading
87         */
88        $newFormContent[] = Identity::getHeaderHTML($form, self::FORM_LOGIN_CLASS);
89
90        /**
91         * Field
92         */
93        foreach ($form->_content as $field) {
94            if (!is_array($field)) {
95                continue;
96            }
97            $fieldName = $field["name"];
98            if ($fieldName == null) {
99                // this is not an input field
100                if ($field["type"] == "submit") {
101                    /**
102                     * This is important to keep the submit element intact
103                     * for forms integration such as captcha
104                     * They search the submit button to insert before it
105                     */
106                    $classes = "btn btn-primary btn-block";
107                    if (isset($field["class"])) {
108                        $field["class"] = $field["class"] . " " . $classes;
109                    } else {
110                        $field["class"] = $classes;
111                    }
112                    $newFormContent[] = $field;
113                }
114                continue;
115            }
116            switch ($fieldName) {
117                case "u":
118                    $loginText = $field["_text"];
119                    $loginValue = $field["value"];
120                    $loginHTMLField = <<<EOF
121<div class="form-floating">
122    <input type="text" id="inputUserName" class="form-control" placeholder="$loginText" required="required" autofocus="" name="u" value="$loginValue">
123    <label for="inputUserName">$loginText</label>
124</div>
125EOF;
126                    $newFormContent[] = $loginHTMLField;
127                    break;
128                case "p":
129                    $passwordText = $field["_text"];
130                    $passwordFieldHTML = <<<EOF
131<div class="form-floating">
132    <input type="password" id="inputPassword" class="form-control" placeholder="$passwordText" required="required" name="p">
133    <label for="inputPassword">$passwordText</label>
134</div>
135EOF;
136                    $newFormContent[] = $passwordFieldHTML;
137                    break;
138                case "r":
139                    $rememberText = $field["_text"];
140                    $rememberValue = $field["value"];
141                    $rememberMeHtml = <<<EOF
142<div class="checkbox rememberMe">
143    <label><input type="checkbox" id="remember__me" name="r" value="$rememberValue"> $rememberText</label>
144</div>
145EOF;
146                    $newFormContent[] = $rememberMeHtml;
147                    break;
148                default:
149                    $tag = self::TAG;
150                    LogUtility::msg("The $tag field name ($fieldName) is unknown", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
151
152
153            }
154        }
155
156
157        $registerHtml = action_plugin_combo_registration::getRegisterLinkAndParagraph();
158        if(!empty($registerHtml)){
159            $newFormContent[] = $registerHtml;
160        }
161        $resendPwdHtml = action_plugin_combo_resend::getResendPasswordParagraphWithLinkToFormPage();
162        if(!empty($resendPwdHtml)){
163            $newFormContent[] = $resendPwdHtml;
164        }
165
166        /**
167         * Set the new in place of the old one
168         */
169        $form->_content = $newFormContent;
170
171        return true;
172
173
174    }
175
176
177    /**
178     * Login
179     * @return string
180     */
181    public static function getLoginParagraphWithLinkToFormPage()
182    {
183
184        $loginPwLink = (new Login())->asHtmlLink('', false);
185        global $lang;
186        $loginText = $lang['btn_login'];
187        return <<<EOF
188<p class="login">$loginText ? : $loginPwLink</p>
189EOF;
190
191    }
192}
193
194