/** * Registration logic * * @param TBGRequest $request */ public function runRegister(TBGRequest $request) { TBGContext::loadLibrary('common'); $i18n = TBGContext::getI18n(); try { $username = mb_strtolower(trim($request['fieldusername'])); $buddyname = $request['buddyname']; $email = mb_strtolower(trim($request['email_address'])); $confirmemail = mb_strtolower(trim($request['email_confirm'])); $security = $request['verification_no']; $realname = $request['realname']; $available = TBGUsersTable::getTable()->isUsernameAvailable($username); $fields = array(); if (!$available) { throw new Exception($i18n->__('This username is in use')); } if (!empty($buddyname) && !empty($email) && !empty($confirmemail) && !empty($security)) { if ($email != $confirmemail) { array_push($fields, 'email_address', 'email_confirm'); throw new Exception($i18n->__('The email address must be valid, and must be typed twice.')); } if ($security != $_SESSION['activation_number']) { array_push($fields, 'verification_no'); throw new Exception($i18n->__('To prevent automatic sign-ups, enter the verification number shown below.')); } $email_ok = false; $valid_domain = false; if (tbg_check_syntax($email, "EMAIL")) { $email_ok = true; } if ($email_ok && TBGSettings::get('limit_registration') != '') { $allowed_domains = preg_replace('/[[:space:]]*,[[:space:]]*/', '|', TBGSettings::get('limit_registration')); if (preg_match('/@(' . $allowed_domains . ')$/i', $email) == false) { array_push($fields, 'email_address', 'email_confirm'); throw new Exception($i18n->__('Email adresses from this domain can not be used.')); } /*if (count($allowed_domains) > 0) { foreach ($allowed_domains as $allowed_domain) { $allowed_domain = '@' . trim($allowed_domain); if (mb_strpos($email, $allowed_domain) !== false ) //mb_strpos checks if $to { $valid_domain = true; break; } } } else { $valid_domain = true; }*/ } /*if ($valid_domain == false) { array_push($fields, 'email_address', 'email_confirm'); throw new Exception($i18n->__('Email adresses from this domain can not be used.')); }*/ if ($email_ok == false) { array_push($fields, 'email_address', 'email_confirm'); throw new Exception($i18n->__('The email address must be valid, and must be typed twice.')); } if ($security != $_SESSION['activation_number']) { array_push($fields, 'verification_no'); throw new Exception($i18n->__('To prevent automatic sign-ups, enter the verification number shown below.')); } $password = TBGUser::createPassword(); $user = new TBGUser(); $user->setUsername($username); $user->setRealname($realname); $user->setBuddyname($buddyname); $user->setGroup(TBGSettings::getDefaultGroup()); $user->setEnabled(); $user->setPassword($password); $user->setEmail($email); $user->setJoined(); $user->save(); if ($user->isActivated()) { TBGContext::setMessage('auto_password', $password); return $this->renderJSON(array('loginmessage' => $i18n->__('After pressing %continue, you need to set your password.', array('%continue' => $i18n->__('Continue'))), 'one_time_password' => $password, 'activated' => true)); } return $this->renderJSON(array('loginmessage' => $i18n->__('The account has now been registered - check your email inbox for the activation email. Please be patient - this email can take up to two hours to arrive.'), 'activated' => false)); } else { array_push($fields, 'email_address', 'email_confirm', 'buddyname', 'verification_no'); throw new Exception($i18n->__('You need to fill out all fields correctly.')); } } catch (Exception $e) { $this->getResponse()->setHttpStatus(400); return $this->renderJSON(array('error' => $i18n->__($e->getMessage()), 'fields' => $fields)); } }