00001 <?php
00002
00019
00020
00021
00022 import('form.Form');
00023
00024 class RegistrationForm extends Form {
00025
00027 var $existingUser;
00028
00030 var $defaultAuth;
00031
00033 var $captchaEnabled;
00034
00036 var $implicitAuth;
00037
00041 function RegistrationForm() {
00042 parent::Form('user/register.tpl');
00043 $this->implicitAuth = Config::getVar('security', 'implicit_auth');
00044
00045 if ($this->implicitAuth) {
00046
00047 $this->existingUser = 1;
00048 } else {
00049 $this->existingUser = Request::getUserVar('existingUser') ? 1 : 0;
00050
00051 import('captcha.CaptchaManager');
00052 $captchaManager =& new CaptchaManager();
00053 $this->captchaEnabled = ($captchaManager->isEnabled() && Config::getVar('captcha', 'captcha_on_register'))?true:false;
00054
00055
00056 $this->addCheck(new FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
00057 $this->addCheck(new FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
00058
00059 if ($this->existingUser) {
00060
00061 $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)));
00062 } else {
00063
00064 $site = &Request::getSite();
00065
00066 $this->addCheck(new FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByUsername'), array(), true));
00067 $this->addCheck(new FormValidatorAlphaNum($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
00068 $this->addCheck(new FormValidatorLength($this, 'password', 'required', 'user.register.form.passwordLengthTooShort', '>=', $site->getMinPasswordLength()));
00069 $this->addCheck(new FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'password2\');'), array(&$this)));
00070 $this->addCheck(new FormValidator($this, 'firstName', 'required', 'user.profile.form.firstNameRequired'));
00071 $this->addCheck(new FormValidator($this, 'lastName', 'required', 'user.profile.form.lastNameRequired'));
00072 $this->addCheck(new FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
00073 $this->addCheck(new FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
00074 $this->addCheck(new FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', array(DAORegistry::getDAO('UserDAO'), 'userExistsByEmail'), array(), true));
00075 if ($this->captchaEnabled) {
00076 $this->addCheck(new FormValidatorCaptcha($this, 'captcha', 'captchaId', 'common.captchaField.badCaptcha'));
00077 }
00078
00079 $authDao = &DAORegistry::getDAO('AuthSourceDAO');
00080 $this->defaultAuth = &$authDao->getDefaultPlugin();
00081 if (isset($this->defaultAuth)) {
00082 $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)));
00083 }
00084 }
00085 }
00086
00087 $this->addCheck(new FormValidatorPost($this));
00088 }
00089
00093 function display() {
00094 $templateMgr = &TemplateManager::getManager();
00095 $site = &Request::getSite();
00096 $templateMgr->assign('minPasswordLength', $site->getMinPasswordLength());
00097 $journal = &Request::getJournal();
00098
00099 if ($this->captchaEnabled) {
00100 import('captcha.CaptchaManager');
00101 $captchaManager =& new CaptchaManager();
00102 $captcha =& $captchaManager->createCaptcha();
00103 if ($captcha) {
00104 $templateMgr->assign('captchaEnabled', $this->captchaEnabled);
00105 $this->setData('captchaId', $captcha->getCaptchaId());
00106 }
00107 }
00108
00109 $countryDao =& DAORegistry::getDAO('CountryDAO');
00110 $countries =& $countryDao->getCountries();
00111 $templateMgr->assign_by_ref('countries', $countries);
00112
00113 $templateMgr->assign('privacyStatement', $journal->getLocalizedSetting('privacyStatement'));
00114 $templateMgr->assign('allowRegReader', $journal->getSetting('allowRegReader')==1?1:0);
00115 $templateMgr->assign('enableSubscriptions', $journal->getSetting('enableSubscriptions')==1?1:0);
00116 $templateMgr->assign('enableOpenAccessNotification', $journal->getSetting('enableOpenAccessNotification')==1?1:0);
00117 $templateMgr->assign('allowRegAuthor', $journal->getSetting('allowRegAuthor')==1?1:0);
00118 $templateMgr->assign('allowRegReviewer', $journal->getSetting('allowRegReviewer')==1?1:0);
00119 $templateMgr->assign('source', Request::getUserVar('source'));
00120
00121 $site = &Request::getSite();
00122 $templateMgr->assign('availableLocales', $site->getSupportedLocaleNames());
00123
00124 $templateMgr->assign('helpTopicId', 'user.registerAndProfile');
00125 parent::display();
00126 }
00127
00128 function getLocaleFieldNames() {
00129 $userDao =& DAORegistry::getDAO('UserDAO');
00130 return $userDao->getLocaleFieldNames();
00131 }
00132
00136 function initData() {
00137 $this->setData('registerAsReader', 1);
00138 $this->setData('existingUser', $this->existingUser);
00139 $this->setData('userLocales', array());
00140 $this->setData('sendPassword', 1);
00141 }
00142
00146 function readInputData() {
00147 $userVars = array(
00148 'username', 'password', 'password2',
00149 'salutation', 'firstName', 'middleName', 'lastName',
00150 'gender', 'initials', 'country',
00151 'affiliation', 'email', 'userUrl', 'phone', 'fax', 'signature',
00152 'mailingAddress', 'biography', 'interests', 'userLocales',
00153 'registerAsReader', 'openAccessNotification', 'registerAsAuthor',
00154 'registerAsReviewer', 'existingUser', 'sendPassword'
00155 );
00156 if ($this->captchaEnabled) {
00157 $userVars[] = 'captchaId';
00158 $userVars[] = 'captcha';
00159 }
00160
00161 $this->readUserVars($userVars);
00162
00163 if ($this->getData('userLocales') == null || !is_array($this->getData('userLocales'))) {
00164 $this->setData('userLocales', array());
00165 }
00166
00167 if ($this->getData('username') != null) {
00168
00169 $this->setData('username', strtolower($this->getData('username')));
00170 }
00171 }
00172
00176 function execute() {
00177 $requireValidation = Config::getVar('email', 'require_validation');
00178
00179 if ($this->existingUser) {
00180
00181 $userDao = &DAORegistry::getDAO('UserDAO');
00182
00183 if ($this->implicitAuth) {
00184 $sessionManager =& SessionManager::getManager();
00185 $session =& $sessionManager->getUserSession();
00186
00187 $user =& $userDao->getUserByUsername($session->getSessionVar('username'));
00188 } else {
00189 $user = &$userDao->getUserByUsername($this->getData('username'));
00190 }
00191
00192 if ($user == null) {
00193 return false;
00194 }
00195
00196 $userId = $user->getUserId();
00197
00198 } else {
00199
00200 $user = &new User();
00201
00202 $user->setUsername($this->getData('username'));
00203 $user->setSalutation($this->getData('salutation'));
00204 $user->setFirstName($this->getData('firstName'));
00205 $user->setMiddleName($this->getData('middleName'));
00206 $user->setInitials($this->getData('initials'));
00207 $user->setLastName($this->getData('lastName'));
00208 $user->setGender($this->getData('gender'));
00209 $user->setAffiliation($this->getData('affiliation'));
00210 $user->setSignature($this->getData('signature'), null);
00211 $user->setEmail($this->getData('email'));
00212 $user->setUrl($this->getData('userUrl'));
00213 $user->setPhone($this->getData('phone'));
00214 $user->setFax($this->getData('fax'));
00215 $user->setMailingAddress($this->getData('mailingAddress'));
00216 $user->setBiography($this->getData('biography'), null);
00217 $user->setInterests($this->getData('interests'), null);
00218 $user->setDateRegistered(Core::getCurrentDate());
00219 $user->setCountry($this->getData('country'));
00220
00221 $site = &Request::getSite();
00222 $availableLocales = $site->getSupportedLocales();
00223
00224 $locales = array();
00225 foreach ($this->getData('userLocales') as $locale) {
00226 if (Locale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
00227 array_push($locales, $locale);
00228 }
00229 }
00230 $user->setLocales($locales);
00231
00232 if (isset($this->defaultAuth)) {
00233 $user->setPassword($this->getData('password'));
00234
00235 $this->defaultAuth->doCreateUser($user);
00236 $user->setAuthId($this->defaultAuth->authId);
00237 }
00238 $user->setPassword(Validation::encryptCredentials($this->getData('username'), $this->getData('password')));
00239
00240 if ($requireValidation) {
00241
00242
00243 $user->setDisabled(true);
00244 $user->setDisabledReason(Locale::translate('user.login.accountNotValidated'));
00245 }
00246
00247 $userDao = &DAORegistry::getDAO('UserDAO');
00248 $userDao->insertUser($user);
00249 $userId = $user->getUserId();
00250 if (!$userId) {
00251 return false;
00252 }
00253
00254 $sessionManager = &SessionManager::getManager();
00255 $session = &$sessionManager->getUserSession();
00256 $session->setSessionVar('username', $user->getUsername());
00257
00258 }
00259
00260 $journal = &Request::getJournal();
00261 $roleDao = &DAORegistry::getDAO('RoleDAO');
00262
00263
00264 $allowedRoles = array('reader' => 'registerAsReader', 'author' => 'registerAsAuthor', 'reviewer' => 'registerAsReviewer');
00265
00266 $journalSettingsDao = &DAORegistry::getDAO('JournalSettingsDAO');
00267 if (!$journalSettingsDao->getSetting($journal->getJournalId(), 'allowRegReader')) {
00268 unset($allowedRoles['reader']);
00269 }
00270 if (!$journalSettingsDao->getSetting($journal->getJournalId(), 'allowRegAuthor')) {
00271 unset($allowedRoles['author']);
00272 }
00273 if (!$journalSettingsDao->getSetting($journal->getJournalId(), 'allowRegReviewer')) {
00274 unset($allowedRoles['reviewer']);
00275 }
00276
00277 foreach ($allowedRoles as $k => $v) {
00278 $roleId = $roleDao->getRoleIdFromPath($k);
00279 if ($this->getData($v) && !$roleDao->roleExists($journal->getJournalId(), $userId, $roleId)) {
00280 $role = &new Role();
00281 $role->setJournalId($journal->getJournalId());
00282 $role->setUserId($userId);
00283 $role->setRoleId($roleId);
00284 $roleDao->insertRole($role);
00285
00286 }
00287 }
00288
00289 if (!$this->existingUser) {
00290 import('mail.MailTemplate');
00291 if ($requireValidation) {
00292
00293 import('security.AccessKeyManager');
00294 $accessKeyManager =& new AccessKeyManager();
00295 $accessKey = $accessKeyManager->createKey('RegisterContext', $user->getUserId(), null, Config::getVar('email', 'validation_timeout'));
00296
00297
00298 $mail =& new MailTemplate('USER_VALIDATE');
00299 $mail->setFrom($journal->getSetting('contactEmail'), $journal->getSetting('contactName'));
00300 $mail->assignParams(array(
00301 'userFullName' => $user->getFullName(),
00302 'activateUrl' => Request::url($journal->getPath(), 'user', 'activateUser', array($this->getData('username'), $accessKey))
00303 ));
00304 $mail->addRecipient($user->getEmail(), $user->getFullName());
00305 $mail->send();
00306 unset($mail);
00307 }
00308 if ($this->getData('sendPassword')) {
00309
00310 $mail = &new MailTemplate('USER_REGISTER');
00311 $mail->setFrom($journal->getSetting('contactEmail'), $journal->getSetting('contactName'));
00312 $mail->assignParams(array(
00313 'username' => $this->getData('username'),
00314 'password' => String::substr($this->getData('password'), 0, 30),
00315 'userFullName' => $user->getFullName()
00316 ));
00317 $mail->addRecipient($user->getEmail(), $user->getFullName());
00318 $mail->send();
00319 unset($mail);
00320 }
00321 }
00322
00323
00324
00325
00326
00327 if (isset($allowedRoles['reader']) && $this->getData($allowedRoles['reader'])) {
00328 $notificationStatusDao = &DAORegistry::getDAO('NotificationStatusDAO');
00329 $notificationStatusDao->setJournalNotifications($journal->getJournalId(), $userId, false);
00330 $notificationStatusDao->setJournalNotifications($journal->getJournalId(), $userId, true);
00331 }
00332
00333 if (isset($allowedRoles['reader']) && $this->getData('openAccessNotification')) {
00334 $userSettingsDao = &DAORegistry::getDAO('UserSettingsDAO');
00335 $userSettingsDao->updateSetting($userId, 'openAccessNotification', true, 'bool', $journal->getJournalId());
00336 }
00337 }
00338
00339 }
00340
00341 ?>