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