8 * @file classes/user/form/RegistrationForm.inc.php
10 * Copyright (c) 2003-2008 John Willinsky
11 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
13 * @class RegistrationForm
16 * @brief Form for user registration.
19 // $Id: RegistrationForm.inc.php,v 1.7 2009/09/22 19:22:09 asmecher Exp $
24 class RegistrationForm
extends Form
{
26 /** @var boolean user is already registered with another press */
29 /** @var AuthPlugin default authentication source, if specified */
32 /** @var boolean whether or not captcha is enabled for this form */
35 /** @var boolean whether or not implicit authentication is used */
41 function RegistrationForm() {
42 parent
::Form('user/register.tpl');
43 $this->implicitAuth
= Config
::getVar('security', 'implicit_auth');
45 if ($this->implicitAuth
) {
46 // If implicit auth - it is always an existing user
47 $this->existingUser
= 1;
49 $this->existingUser
= Request
::getUserVar('existingUser') ?
1 : 0;
51 import('captcha.CaptchaManager');
52 $captchaManager = new CaptchaManager();
53 $this->captchaEnabled
= ($captchaManager->isEnabled() && Config
::getVar('captcha', 'captcha_on_register'))?
true:false;
55 // Validation checks for this form
56 $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
57 $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
59 if ($this->existingUser
) {
60 // Existing user -- check login
61 $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.login.loginError', create_function('$username,$form', 'return Validation::checkCredentials($form->getData(\'username\'), $form->getData(\'password\'));'), array(&$this)));
63 // New user -- check required profile fields
64 $site =& Request
::getSite();
66 $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', array(DAORegistry
::getDAO('UserDAO'), 'userExistsByUsername'), array(), true));
67 $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
68 $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
69 $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
70 $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
71 $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
72 $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
73 $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
74 $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', array(DAORegistry
::getDAO('UserDAO'), 'userExistsByEmail'), array(), true));
75 if ($this->captchaEnabled
) {
76 $this->addCheck(new FormValidatorCaptcha($this, 'captcha', 'captchaId', 'common.captchaField.badCaptcha'));
79 $authDao =& DAORegistry
::getDAO('AuthSourceDAO');
80 $this->defaultAuth
=& $authDao->getDefaultPlugin();
81 if (isset($this->defaultAuth
)) {
82 $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', create_function('$username,$form,$auth', 'return (!$auth->userExists($username) || $auth->authenticate($username, $form->getData(\'password\')));'), array(&$this, $this->defaultAuth
)));
87 $this->addCheck(new FormValidatorPost($this));
94 $templateMgr =& TemplateManager
::getManager();
95 $site =& Request
::getSite();
96 $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
97 $press =& Request
::getPress();
99 if ($this->captchaEnabled
) {
100 import('captcha.CaptchaManager');
101 $captchaManager = new CaptchaManager();
102 $captcha =& $captchaManager->createCaptcha();
104 $templateMgr->assign('captchaEnabled', $this->captchaEnabled
);
105 $this->setData('captchaId', $captcha->getId());
109 $countryDao =& DAORegistry
::getDAO('CountryDAO');
110 $countries =& $countryDao->getCountries();
111 $templateMgr->assign_by_ref('countries', $countries);
113 $userDao =& DAORegistry
::getDAO('UserDAO');
114 $templateMgr->assign('genderOptions', $userDao->getGenderOptions());
116 $templateMgr->assign('privacyStatement', $press->getLocalizedSetting('privacyStatement'));
117 $templateMgr->assign('allowRegReader', $press->getSetting('allowRegReader')==1?
1:0);
118 $templateMgr->assign('allowRegAuthor', $press->getSetting('allowRegAuthor')==1?
1:0);
119 $templateMgr->assign('allowRegReviewer', $press->getSetting('allowRegReviewer')==1?
1:0);
120 $templateMgr->assign('source', Request
::getUserVar('source'));
122 $site =& Request
::getSite();
123 $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
125 $templateMgr->assign('helpTopicId', 'user.registerAndProfile');
129 function getLocaleFieldNames() {
130 $userDao =& DAORegistry
::getDAO('UserDAO');
131 return $userDao->getLocaleFieldNames();
135 * Initialize default data.
137 function initData() {
138 $this->setData('registerAsReader', 1);
139 $this->setData('existingUser', $this->existingUser
);
140 $this->setData('userLocales', array());
141 $this->setData('sendPassword', 1);
145 * Assign form data to user-submitted data.
147 function readInputData() {
149 'username', 'password', 'password2',
150 'salutation', 'firstName', 'middleName', 'lastName',
151 'gender', 'initials', 'country',
152 'affiliation', 'email', 'userUrl', 'phone', 'fax', 'signature',
153 'mailingAddress', 'biography', 'interests', 'userLocales',
154 'registerAsReader', 'registerAsAuthor',
155 'registerAsReviewer', 'existingUser', 'sendPassword'
157 if ($this->captchaEnabled
) {
158 $userVars[] = 'captchaId';
159 $userVars[] = 'captcha';
162 $this->readUserVars($userVars);
164 if ($this->getData('userLocales') == null ||
!is_array($this->getData('userLocales'))) {
165 $this->setData('userLocales', array());
168 if ($this->getData('username') != null) {
169 // Usernames must be lowercase
170 $this->setData('username', strtolower($this->getData('username')));
175 * Register a new user.
178 $requireValidation = Config
::getVar('email', 'require_validation');
180 if ($this->existingUser
) { // If using implicit auth - we hardwire that we are working on an existing user
181 // Existing user in the system
182 $userDao =& DAORegistry
::getDAO('UserDAO');
184 if ($this->implicitAuth
) { // If we are using implicit auth - then use the session username variable - rather than data from the form
185 $sessionManager =& SessionManager
::getManager();
186 $session =& $sessionManager->getUserSession();
188 $user =& $userDao->getUserByUsername($session->getSessionVar('username'));
190 $user =& $userDao->getUserByUsername($this->getData('username'));
197 $userId = $user->getId();
203 $user->setUsername($this->getData('username'));
204 $user->setSalutation($this->getData('salutation'));
205 $user->setFirstName($this->getData('firstName'));
206 $user->setMiddleName($this->getData('middleName'));
207 $user->setInitials($this->getData('initials'));
208 $user->setLastName($this->getData('lastName'));
209 $user->setGender($this->getData('gender'));
210 $user->setAffiliation($this->getData('affiliation'));
211 $user->setSignature($this->getData('signature'), null); // Localized
212 $user->setEmail($this->getData('email'));
213 $user->setUrl($this->getData('userUrl'));
214 $user->setPhone($this->getData('phone'));
215 $user->setFax($this->getData('fax'));
216 $user->setMailingAddress($this->getData('mailingAddress'));
217 $user->setBiography($this->getData('biography'), null); // Localized
218 $user->setInterests($this->getData('interests'), null); // Localized
219 $user->setDateRegistered(Core
::getCurrentDate());
220 $user->setCountry($this->getData('country'));
222 $site =& Request
::getSite();
223 $availableLocales = $site->getSupportedLocales();
226 foreach ($this->getData('userLocales') as $locale) {
227 if (Locale
::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
228 array_push($locales, $locale);
231 $user->setLocales($locales);
233 if (isset($this->defaultAuth
)) {
234 $user->setPassword($this->getData('password'));
235 // FIXME Check result and handle failures
236 $this->defaultAuth
->doCreateUser($user);
237 $user->setAuthId($this->defaultAuth
->authId
);
239 $user->setPassword(Validation
::encryptCredentials($this->getData('username'), $this->getData('password')));
241 if ($requireValidation) {
242 // The account should be created in a disabled
244 $user->setDisabled(true);
245 $user->setDisabledReason(Locale
::translate('user.login.accountNotValidated'));
248 $userDao =& DAORegistry
::getDAO('UserDAO');
249 $userDao->insertUser($user);
250 $userId = $user->getId();
255 $sessionManager =& SessionManager
::getManager();
256 $session =& $sessionManager->getUserSession();
257 $session->setSessionVar('username', $user->getUsername());
261 $press =& Request
::getPress();
262 $roleDao =& DAORegistry
::getDAO('RoleDAO');
264 // Roles users are allowed to register themselves in
265 $allowedRoles = array('reader' => 'registerAsReader', 'author' => 'registerAsAuthor', 'reviewer' => 'registerAsReviewer');
267 $pressSettingsDao =& DAORegistry
::getDAO('PressSettingsDAO');
268 if (!$pressSettingsDao->getSetting($press->getId(), 'allowRegReader')) {
269 unset($allowedRoles['reader']);
271 if (!$pressSettingsDao->getSetting($press->getId(), 'allowRegAuthor')) {
272 unset($allowedRoles['author']);
274 if (!$pressSettingsDao->getSetting($press->getId(), 'allowRegReviewer')) {
275 unset($allowedRoles['reviewer']);
278 foreach ($allowedRoles as $k => $v) {
279 $roleId = $roleDao->getRoleIdFromPath($k);
280 if ($this->getData($v) && !$roleDao->roleExists($press->getId(), $userId, $roleId)) {
282 $role->setPressId($press->getId());
283 $role->setUserId($userId);
284 $role->setRoleId($roleId);
285 $roleDao->insertRole($role);
290 if (!$this->existingUser
) {
291 import('mail.MailTemplate');
292 if ($requireValidation) {
293 // Create an access key
294 import('security.AccessKeyManager');
295 $accessKeyManager = new AccessKeyManager();
296 $accessKey = $accessKeyManager->createKey('RegisterContext', $user->getId(), null, Config
::getVar('email', 'validation_timeout'));
298 // Send email validation request to user
299 $mail = new MailTemplate('USER_VALIDATE');
300 $mail->setFrom($press->getSetting('contactEmail'), $press->getSetting('contactName'));
301 $mail->assignParams(array(
302 'userFullName' => $user->getFullName(),
303 'activateUrl' => Request
::url($press->getPath(), 'user', 'activateUser', array($this->getData('username'), $accessKey))
305 $mail->addRecipient($user->getEmail(), $user->getFullName());
309 if ($this->getData('sendPassword')) {
310 // Send welcome email to user
311 $mail = new MailTemplate('USER_REGISTER');
312 $mail->setFrom($press->getSetting('contactEmail'), $press->getSetting('contactName'));
313 $mail->assignParams(array(
314 'username' => $this->getData('username'),
315 'password' => String::substr($this->getData('password'), 0, 30), // Prevent mailer abuse via long passwords
316 'userFullName' => $user->getFullName()
318 $mail->addRecipient($user->getEmail(), $user->getFullName());
324 // By default, self-registering readers will receive
325 // press updates. (The double set is here to prevent a
326 // duplicate insert error msg if there was a notification entry
327 // left over from a previous role.)
328 if (isset($allowedRoles['reader']) && $this->getData($allowedRoles['reader'])) {
329 $notificationStatusDao =& DAORegistry
::getDAO('NotificationStatusDAO');
330 $notificationStatusDao->setPressNotifications($press->getId(), $userId, false);
331 $notificationStatusDao->setPressNotifications($press->getId(), $userId, true);