Пример #1
0
function registration_on_create(&$api)
{
    include dirname(__FILE__) . '/registration_controller.class.php';
    $registration = new RegistrationController($api);
    $user = init_user_from_post_data();
    if ($_POST['cancel']) {
        $api->refer_to(cfg('site_url'));
    }
    // Check the data for completeness.
    $err = $user->check_complete();
    if ($err) {
        $registration->add_hint(new \hint\Error($err));
    }
    if ($_POST['password'] !== $_POST['password2']) {
        $registration->add_hint(new \hint\Error(_('Error: Passwords do not match.')));
    }
    if ($registration->has_errors()) {
        return $registration->show($user);
    }
    // Make sure that the name is available.
    if (!$api->userdb()->username_is_available($user->get_name())) {
        $err = _('The entered username is not available.');
        $registration->add_hint(new \hint\Error($err));
    }
    // Make sure that the email address is available.
    if ($api->userdb()->get_user_from_mail($user->get_mail())) {
        $err = _('The given email address already exists in our database.');
        $registration->add_hint(new \hint\Error($err));
    }
    if ($registration->has_errors()) {
        return $registration->show($user);
    }
    // Create the user.
    $user->set_group_id(cfg('default_group_id'));
    if (!$api->userdb()->save_user($user)) {
        $registration->add_hint(new \hint\Error(_('Failed to save the user.')));
        return $registration->show($user);
    }
    // Done.
    registration_mail_send($api, $user);
}