public static function isUnusedCode($code)
 {
     global $ilDB;
     include_once './Services/Registration/classes/class.ilRegistrationCode.php';
     return ilRegistrationCode::isUnusedCode($code);
     $set = $ilDB->query("SELECT used FROM " . self::DB_TABLE . " WHERE code = " . $ilDB->quote($code, "text"));
     $set = $ilDB->fetchAssoc($set);
     if ($set && !$set["used"]) {
         return true;
     }
     return false;
 }
 public function saveForm()
 {
     global $lng, $ilSetting;
     $this->__initForm();
     $form_valid = $this->form->checkInput();
     require_once 'Services/User/classes/class.ilObjUser.php';
     // custom validation
     // validate email against restricted domains
     $email = $this->form->getInput("usr_email");
     if ($email) {
         // #10366
         $domains = array();
         foreach ($this->registration_settings->getAllowedDomains() as $item) {
             if (trim($item)) {
                 $domains[] = $item;
             }
         }
         if (sizeof($domains)) {
             $mail_valid = false;
             foreach ($domains as $domain) {
                 $domain = str_replace("*", "~~~", $domain);
                 $domain = preg_quote($domain);
                 $domain = str_replace("~~~", ".+", $domain);
                 if (preg_match("/^" . $domain . "\$/", $email, $hit)) {
                     $mail_valid = true;
                     break;
                 }
             }
             if (!$mail_valid) {
                 $mail_obj = $this->form->getItemByPostVar('usr_email');
                 $mail_obj->setAlert(sprintf($lng->txt("reg_email_domains"), implode(", ", $domains)));
                 $form_valid = false;
             }
         }
     }
     if (!$this->form->getInput("usr_agreement")) {
         $agr_obj = $this->form->getItemByPostVar('usr_agreement');
         $agr_obj->setAlert($lng->txt("force_accept_usr_agreement"));
         $form_valid = false;
     }
     $valid_role = false;
     // code
     if ($this->code_enabled) {
         $code = $this->form->getInput('usr_registration_code');
         // could be optional
         if ($code) {
             // code validation
             include_once './Services/Registration/classes/class.ilRegistrationCode.php';
             if (!ilRegistrationCode::isUnusedCode($code)) {
                 $code_obj = $this->form->getItemByPostVar('usr_registration_code');
                 $code_obj->setAlert($lng->txt('registration_code_not_valid'));
                 $form_valid = false;
             } else {
                 // get role from valid code
                 $valid_role = (int) ilRegistrationCode::getCodeRole($code);
             }
         }
     }
     // no need if role is attached to code
     if (!$valid_role) {
         // manual selection
         if ($this->registration_settings->roleSelectionEnabled()) {
             include_once "./Services/AccessControl/classes/class.ilObjRole.php";
             $selected_role = $this->form->getInput("usr_roles");
             if ($selected_role && ilObjRole::_lookupAllowRegister($selected_role)) {
                 $valid_role = (int) $selected_role;
             }
         } else {
             include_once 'Services/Registration/classes/class.ilRegistrationEmailRoleAssignments.php';
             $registration_role_assignments = new ilRegistrationRoleAssignments();
             $valid_role = (int) $registration_role_assignments->getRoleByEmail($this->form->getInput("usr_email"));
         }
     }
     // no valid role could be determined
     if (!$valid_role) {
         ilUtil::sendInfo($lng->txt("registration_no_valid_role"));
         $form_valid = false;
     }
     // validate username
     $login_obj = $this->form->getItemByPostVar('username');
     $login = $this->form->getInput("username");
     if (!ilUtil::isLogin($login)) {
         $login_obj->setAlert($lng->txt("login_invalid"));
         $form_valid = false;
     } else {
         if (ilObjUser::_loginExists($login)) {
             $login_obj->setAlert($lng->txt("login_exists"));
             $form_valid = false;
         } else {
             if ((int) $ilSetting->get('allow_change_loginname') && (int) $ilSetting->get('reuse_of_loginnames') == 0 && ilObjUser::_doesLoginnameExistInHistory($login)) {
                 $login_obj->setAlert($lng->txt('login_exists'));
                 $form_valid = false;
             }
         }
     }
     if (!$form_valid) {
         ilUtil::sendFailure($lng->txt('form_input_not_valid'));
     } else {
         $password = $this->__createUser($valid_role);
         $this->__distributeMails($password, $this->form->getInput("usr_language"));
         $this->login($password);
         return true;
     }
     $this->form->setValuesByPost();
     $this->displayForm();
     return false;
 }