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__ . '/../class/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 $loginCss = Snippet::createCssSnippet("register"); 92 $content = $loginCss->getContent(); 93 $class = $loginCss->getClass(); 94 $cssHtml = <<<EOF 95<style class="$class"> 96$content 97</style> 98EOF; 99 print $cssHtml; 100 101 102 /** 103 * @var Doku_Form $form 104 */ 105 $form = &$event->data; 106 $class = &$form->params["class"]; 107 if (isset($class)) { 108 $class = $class . " " . self::FORM_REGISTER_CLASS; 109 } else { 110 $class = self::FORM_REGISTER_CLASS; 111 } 112 $newFormContent = []; 113 114 /** 115 * Header (Logo / Title) 116 */ 117 $newFormContent[] = Identity::getHeaderHTML($form, self::FORM_REGISTER_CLASS); 118 119 120 /** 121 * Form Attributes 122 * https://getbootstrap.com/docs/5.0/forms/layout/#horizontal-form 123 */ 124 $rowClass = "row"; 125 if (Bootstrap::getBootStrapMajorVersion() == Bootstrap::BootStrapFourMajorVersion) { 126 $rowClass .= " form-group"; 127 } 128 $firstColWeight = 5; 129 $secondColWeight = 12 - $firstColWeight; 130 131 132 /** 133 * Replace the field 134 * 135 * The password text localized by lang is shared 136 * between the password and the password check field 137 */ 138 $passwordText = "Password"; 139 foreach ($form->_content as $pos => $field) { 140 if (!is_array($field)) { 141 continue; 142 } 143 $fieldName = $field["name"]; 144 if ($fieldName == null) { 145 // this is not an input field 146 if ($field["type"] == "submit") { 147 /** 148 * This is important to keep the submit element intact 149 * for forms integration such as captcha 150 * The search the submit button to insert before it 151 */ 152 $classes = "btn btn-primary"; 153 if (isset($field["class"])) { 154 $field["class"] = $field["class"] . " " . $classes; 155 } else { 156 $field["class"] = $classes; 157 } 158 $field["tabindex"] = "6"; 159 $newFormContent[] = $field; 160 } 161 continue; 162 } 163 switch ($fieldName) { 164 case "login": 165 $loginText = $field["_text"]; 166 $loginValue = $field["value"]; 167 $loginHTML = <<<EOF 168<div class="$rowClass"> 169 <label for="inputUserName" class="col-sm-$firstColWeight col-form-label">$loginText</label> 170 <div class="col-sm-$secondColWeight"> 171 <input type="text" class="form-control" id="inputUserName" placeholder="Username" tabindex="1" name="login" value="$loginValue" required="required"> 172 </div> 173</div> 174EOF; 175 $newFormContent[] = $loginHTML; 176 break; 177 case "pass": 178 $passwordText = $field["_text"]; 179 $passwordHtml = <<<EOF 180<div class="$rowClass"> 181 <label for="inputPassword" class="col-sm-$firstColWeight col-form-label">$passwordText</label> 182 <div class="col-sm-$secondColWeight"> 183 <input type="password" class="form-control" id="inputPassword" placeholder="$passwordText" tabindex="2" name="pass" required="required"> 184 </div> 185</div> 186EOF; 187 $newFormContent[] = $passwordHtml; 188 break; 189 case "passchk": 190 $passwordCheckText = $field["_text"]; 191 $passwordCheckHtml = <<<EOF 192<div class="$rowClass"> 193 <label for="inputPasswordCheck" class="col-sm-$firstColWeight col-form-label">$passwordCheckText</label> 194 <div class="col-sm-$secondColWeight"> 195 <input type="password" class="form-control" id="inputPasswordCheck" placeholder="$passwordText" tabindex="3" name="passchk" required="required"> 196 </div> 197</div> 198EOF; 199 $newFormContent[] = $passwordCheckHtml; 200 break; 201 case "fullname": 202 $fullNameText = $field["_text"]; 203 $fullNameValue = $field["value"]; 204 $fullNameHtml = <<<EOF 205<div class="$rowClass"> 206 <label for="inputRealName" class="col-sm-$firstColWeight col-form-label">$fullNameText</label> 207 <div class="col-sm-$secondColWeight"> 208 <input type="text" class="form-control" id="inputRealName" placeholder="$fullNameText" tabindex="4" name="fullname" value="$fullNameValue" required="required"> 209 </div> 210</div> 211EOF; 212 $newFormContent[] = $fullNameHtml; 213 break; 214 case "email": 215 $emailText = $field["_text"]; 216 $emailValue = $field["value"]; 217 $emailHTML = <<<EOF 218<div class="$rowClass"> 219 <label for="inputEmail" class="col-sm-$firstColWeight col-form-label">$emailText</label> 220 <div class="col-sm-$secondColWeight"> 221 <input type="email" class="form-control" id="inputEmail" placeholder="name@example.com" tabindex="5" name="email" value="$emailValue" required="required"> 222 </div> 223</div> 224EOF; 225 $newFormContent[] = $emailHTML; 226 break; 227 default: 228 $tag = self::TAG; 229 LogUtility::msg("The $tag field name ($fieldName) is unknown", LogUtility::LVL_MSG_ERROR, self::CANONICAL); 230 231 } 232 } 233 234 235 /** 236 * Link in the form footer to login and resend 237 */ 238 $loginLinkToHtmlForm = action_plugin_combo_login::getLoginParagraphWithLinkToFormPage(); 239 if (!empty($loginHTML)) { 240 $newFormContent[] = $loginLinkToHtmlForm; 241 } 242 $resendHtml = action_plugin_combo_resend::getResendPasswordParagraphWithLinkToFormPage(); 243 if (!empty($resendHtml)) { 244 $newFormContent[] = $resendHtml; 245 } 246 247 /** 248 * Update 249 */ 250 $form->_content = $newFormContent; 251 return true; 252 253 254 } 255 256 257} 258 259