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\Bootstrap; 11use ComboStrap\Identity; 12use ComboStrap\LogUtility; 13use ComboStrap\Snippet; 14use dokuwiki\Menu\Item\Register; 15 16if (!defined('DOKU_INC')) die(); 17require_once(__DIR__ . '/../ComboStrap/PluginUtility.php'); 18 19/** 20 * 21 * Register forms depend on the following configuration 22 * https://www.dokuwiki.org/config:autopasswd 23 * If true, there is no password field 24 */ 25class action_plugin_combo_registration extends DokuWiki_Action_Plugin 26{ 27 28 const CANONICAL = Identity::CANONICAL; 29 const TAG = "register"; 30 const FORM_REGISTER_CLASS = "form-" . self::TAG; 31 const CONF_ENABLE_REGISTER_FORM = "enableRegistrationForm"; 32 33 34 /** 35 * Return the register text and link paragraph 36 * @return string 37 */ 38 public static function getRegisterLinkAndParagraph() 39 { 40 41 42 $registerHtml = ""; 43 if (actionOK('register')) { 44 45 /** 46 * The register class does not allow 47 * registration if your are logged in (What ?) 48 * and send an exception 49 */ 50 if (!Identity::isLoggedIn()) { 51 $registerLink = (new Register())->asHtmlLink('', false); 52 global $lang; 53 $tag = self::TAG; 54 $registerText = $lang['reghere']; 55 $registerHtml = <<<EOF 56<p class="$tag">$registerText : $registerLink</p> 57EOF; 58 } 59 } 60 return $registerHtml; 61 } 62 63 64 function register(Doku_Event_Handler $controller) 65 { 66 /** 67 * To modify the register form and add class 68 * 69 * Deprecated object passed by the event but still in use 70 * https://www.dokuwiki.org/devel:event:html_registerform_output 71 */ 72 $controller->register_hook('HTML_REGISTERFORM_OUTPUT', 'BEFORE', $this, 'handle_register_page', array()); 73 74 /** 75 * Event using the new object but not yet used 76 * https://www.dokuwiki.org/devel:event:form_register_output 77 */ 78 // $controller->register_hook('FORM_REGISTER_OUTPUT', 'BEFORE', $this, 'handle_register', array()); 79 80 81 } 82 83 function handle_register_page(&$event, $param) 84 { 85 86 /** 87 * The register page is created via buffer 88 * We print before the forms 89 * to avoid a FOUC 90 */ 91 print Identity::getHtmlStyleTag(self::TAG); 92 93 94 /** 95 * @var Doku_Form $form 96 */ 97 $form = &$event->data; 98 $class = &$form->params["class"]; 99 Identity::addIdentityClass($class, self::FORM_REGISTER_CLASS); 100 $newFormContent = []; 101 102 /** 103 * Header (Logo / Title) 104 */ 105 $newFormContent[] = Identity::getHeaderHTML($form, self::FORM_REGISTER_CLASS); 106 107 108 /** 109 * Form Attributes 110 * https://getbootstrap.com/docs/5.0/forms/layout/#horizontal-form 111 */ 112 $rowClass = "row"; 113 if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFourMajorVersion) { 114 $rowClass .= " form-group"; 115 } 116 $firstColWeight = 5; 117 $secondColWeight = 12 - $firstColWeight; 118 119 120 /** 121 * Replace the field 122 * 123 * The password text localized by lang is shared 124 * between the password and the password check field 125 */ 126 $passwordText = "Password"; 127 foreach ($form->_content as $pos => $field) { 128 if (!is_array($field)) { 129 continue; 130 } 131 $fieldName = $field["name"]; 132 if ($fieldName == null) { 133 // this is not an input field 134 if ($field["type"] == "submit") { 135 /** 136 * This is important to keep the submit element intact 137 * for forms integration such as captcha 138 * The search the submit button to insert before it 139 */ 140 $classes = "btn btn-primary"; 141 if (isset($field["class"])) { 142 $field["class"] = $field["class"] . " " . $classes; 143 } else { 144 $field["class"] = $classes; 145 } 146 $field["tabindex"] = "6"; 147 $newFormContent[] = $field; 148 } 149 continue; 150 } 151 switch ($fieldName) { 152 case "login": 153 $loginText = $field["_text"]; 154 $loginValue = $field["value"]; 155 $loginHTML = <<<EOF 156<div class="$rowClass"> 157 <label for="inputUserName" class="col-sm-$firstColWeight col-form-label">$loginText</label> 158 <div class="col-sm-$secondColWeight"> 159 <input type="text" class="form-control" id="inputUserName" placeholder="Username" tabindex="1" name="login" value="$loginValue" required="required"> 160 </div> 161</div> 162EOF; 163 $newFormContent[] = $loginHTML; 164 break; 165 case "pass": 166 $passwordText = $field["_text"]; 167 $passwordHtml = <<<EOF 168<div class="$rowClass"> 169 <label for="inputPassword" class="col-sm-$firstColWeight col-form-label">$passwordText</label> 170 <div class="col-sm-$secondColWeight"> 171 <input type="password" class="form-control" id="inputPassword" placeholder="$passwordText" tabindex="2" name="pass" required="required"> 172 </div> 173</div> 174EOF; 175 $newFormContent[] = $passwordHtml; 176 break; 177 case "passchk": 178 $passwordCheckText = $field["_text"]; 179 $passwordCheckHtml = <<<EOF 180<div class="$rowClass"> 181 <label for="inputPasswordCheck" class="col-sm-$firstColWeight col-form-label">$passwordCheckText</label> 182 <div class="col-sm-$secondColWeight"> 183 <input type="password" class="form-control" id="inputPasswordCheck" placeholder="$passwordText" tabindex="3" name="passchk" required="required"> 184 </div> 185</div> 186EOF; 187 $newFormContent[] = $passwordCheckHtml; 188 break; 189 case "fullname": 190 $fullNameText = $field["_text"]; 191 $fullNameValue = $field["value"]; 192 $fullNameHtml = <<<EOF 193<div class="$rowClass"> 194 <label for="inputRealName" class="col-sm-$firstColWeight col-form-label">$fullNameText</label> 195 <div class="col-sm-$secondColWeight"> 196 <input type="text" class="form-control" id="inputRealName" placeholder="$fullNameText" tabindex="4" name="fullname" value="$fullNameValue" required="required"> 197 </div> 198</div> 199EOF; 200 $newFormContent[] = $fullNameHtml; 201 break; 202 case "email": 203 $emailText = $field["_text"]; 204 $emailValue = $field["value"]; 205 $emailHTML = <<<EOF 206<div class="$rowClass"> 207 <label for="inputEmail" class="col-sm-$firstColWeight col-form-label">$emailText</label> 208 <div class="col-sm-$secondColWeight"> 209 <input type="email" class="form-control" id="inputEmail" placeholder="name@example.com" tabindex="5" name="email" value="$emailValue" required="required"> 210 </div> 211</div> 212EOF; 213 $newFormContent[] = $emailHTML; 214 break; 215 default: 216 $tag = self::TAG; 217 LogUtility::msg("The $tag field name ($fieldName) is unknown", LogUtility::LVL_MSG_ERROR, self::CANONICAL); 218 219 } 220 } 221 222 223 /** 224 * Link in the form footer to login and resend 225 */ 226 $loginLinkToHtmlForm = action_plugin_combo_login::getLoginParagraphWithLinkToFormPage(); 227 if (!empty($loginHTML)) { 228 $newFormContent[] = $loginLinkToHtmlForm; 229 } 230 $resendHtml = action_plugin_combo_resend::getResendPasswordParagraphWithLinkToFormPage(); 231 if (!empty($resendHtml)) { 232 $newFormContent[] = $resendHtml; 233 } 234 235 /** 236 * Update 237 */ 238 $form->_content = $newFormContent; 239 return true; 240 241 242 } 243 244 245} 246 247