Example #1
0
function registerExe()
{
    if (!validateEmail($_REQUEST['email'])) {
        addError("Email is wrong");
    }
    $d = new Delegate();
    $existingUser = $d->userGetByEmail(trim($_REQUEST['email']));
    if (is_object($existingUser)) {
        addError("An user with same email already present.");
    }
    if (!validateString($_REQUEST['password'])) {
        addError("Password is not ok");
    }
    if ($_REQUEST['password'] != $_REQUEST['password2']) {
        addError("Passwords do not match");
    }
    if (!isset($_REQUEST['invitationToken'])) {
        if ($_REQUEST['captcha'] != $_SESSION['captcha']) {
            addError("Code was incorrect");
        }
    }
    if (errors()) {
        redirect('../../register.php');
        exit(0);
    }
    $user = new User();
    $user->email = trim($_REQUEST['email']);
    $user->password = md5($_REQUEST['password']);
    $user->createdDate = now();
    $user->lastLoginDate = now();
    $user->lastLoginIp = $_SERVER['REMOTE_ADDR'];
    $user->lastBrowserType = $_SERVER['HTTP_USER_AGENT'];
    $userId = $d->userCreate($user);
    if (is_numeric($userId)) {
        addMessage("You were registered");
        $_SESSION['userId'] = $userId;
        $_SESSION['captcha'] = null;
        unset($_SESSION['captcha']);
        //TODO: if we have a temp diagram we will redirect to save page
        if (isset($_SESSION['tempDiagram'])) {
            redirect('../saveDiagram.php');
        } else {
            if (isset($_REQUEST['invitationToken'])) {
                $invitation = $d->invitationGetByToken($_REQUEST['invitationToken']);
                if (is_object($invitation)) {
                    //find the diagram
                    $diagram = $d->diagramGetById($invitation->diagramId);
                    //create userdiagram
                    $userdiagram = new Userdiagram();
                    $userdiagram->diagramId = $diagram->id;
                    $userdiagram->invitedDate = $invitation->createdDate;
                    $userdiagram->level = Userdiagram::LEVEL_EDITOR;
                    $userdiagram->status = Userdiagram::STATUS_ACCEPTED;
                    $userdiagram->userId = $userId;
                    if (!$d->userdiagramCreate($userdiagram)) {
                        addError("Could not add you to the diagram");
                        redirect('../editor.php');
                        exit;
                    }
                    //delete invitation
                    $d->invitationDelete($invitation->id);
                    //all is fine, redirect to the diagram
                    redirect('../editor.php?diagramId=' . $diagram->id);
                } else {
                }
                redirect('../editor.php');
            } else {
                redirect('../editor.php');
            }
        }
        exit(0);
    } else {
        addError("User not added ");
        redirect('../../register.php');
        exit(0);
    }
}