Example #1
0
}
function attemptDataRetrieval($key)
{
    if (array_key_exists($key, $_POST)) {
        return $_POST[$key];
    }
    response(["success" => false, "message" => "<strong>Uh oh!</strong> Please fill out the form completely."]);
}
$recaptcha = new \ReCaptcha\ReCaptcha("6LdcjCUTAAAAAF5fvOKpF-dAPBjACkIzFNLxxgis");
$resp = $recaptcha->verify(attemptDataRetrieval("captcha"), $_SERVER["REMOTE_ADDR"]);
if (!$resp->isSuccess()) {
    response(["success" => false, "message" => "<strong>Uh oh!</strong> Invalid captcha."]);
}
$username = attemptDataRetrieval("username");
$password = attemptDataRetrieval("password");
$color = attemptDataRetrieval("color");
$colors = range(1, 17);
if (strlen($username) < 4 || strlen($username) > 12) {
    $lengthWord = strlen($username) < 3 ? "short" : "long";
    response(["success" => false, "message" => "<strong>Uh oh!</strong> Username is too {$lengthWord}."]);
} elseif (strlen($password) < 4) {
    response(["success" => false, "message" => "<strong>Uh oh!</strong> Password is too short."]);
} elseif (!is_numeric($color) || !in_array($color, $colors)) {
    response(["success" => false, "message" => "<strong>Uh oh!</strong> Invalid color specified."]);
}
$db = new Database();
if ($db->usernameTaken($username)) {
    response(["success" => false, "message" => "<strong>Uh oh!</strong> The username you've specified is already in use."]);
}
$playerId = $db->addUser($username, $password, $color);
response(["success" => true, "message" => "<strong>Hooray!</strong> You have successfully registered your account. Your player id is <strong>{$playerId}</strong>."]);