~skye/poker/act/auth.php

view raw


<?php

require '../com.php';
require '../src/db.php';

try {
	form_has_post([
		"name" => [1, Cfg::USERNAME_MAX_LEN],
		"pass" => [6, 32],
		"action" => [0, 10]
	]);
} catch (Exception $e) {
	exit(page_err("malformed form values: " . $e->getMessage()));
}


$n = $_POST["name"];
$p = $_POST["pass"];
$a = $_POST["action"];

try {
	$db = new Database('../run/poker.db');
} catch (Exception $e) {
	exit(page_err("failed to open db: " . $e->getMessage()));
}

switch ($a) {
	case "register": {
		try {
			$db->new_user($n, $p);
		} catch (Exception $e) {
			exit(page_err(
				"failed to create user: " . $e->getMessage()
			));
		}

		header("Location: /");
		break;
	}

	case "login": {
		try {
			$db->login($n, $p);
		} catch (Exception $e) {
			exit(page_err(
				"failed to login: " . $e->getMessage()
			));
		}

		header("Location: /");
		break;
	}

	default: {
		exit(page_err("unknown form action: $a"));
	}
}
?>