2019-02-13 20:43:32 -06:00
|
|
|
<?php
|
|
|
|
/* This file is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of version 3 of the GNU Affero General Public
|
|
|
|
License as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This file is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details. */
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
// NUMBER STRING --> VARYING
|
|
|
|
// Return the value of a given user's row
|
|
|
|
function user_get($id, $variable) {
|
|
|
|
return db_get_cell("lusers", "id", $id, $variable);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NUMBER STRING VARYING --> NIL
|
|
|
|
// Set the value of a given user's cell
|
|
|
|
function user_set($id, $variable, $new_value) {
|
|
|
|
return db_set_cell("lusers", "id", $id, $variable, $new_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// NUMBER STRING STRING [STRING STRING STRING STRING STRING] --> BOOLEAN
|
|
|
|
// Create a user of the given specification.
|
2019-02-18 23:24:07 -06:00
|
|
|
function user_create($name, $password, $class="spectator",
|
2019-02-13 20:43:32 -06:00
|
|
|
$full_name=NULL, $email=NULL, $url=NULL, $bio=NULL) {
|
|
|
|
|
2019-02-18 23:24:07 -06:00
|
|
|
$id = db_new_id("lusers", "id");
|
|
|
|
$password = password_hash($password, PASSWORD_BCRYPT,
|
|
|
|
array('cost' => 11));
|
|
|
|
|
2019-02-13 20:43:32 -06:00
|
|
|
return db_insert_row("lusers",
|
2019-02-15 08:32:58 -06:00
|
|
|
array("id", "username", "hash", "class",
|
2019-02-13 20:43:32 -06:00
|
|
|
"full_name", "email", "website", "biography"),
|
2019-02-15 08:32:58 -06:00
|
|
|
array($id, $name, $password, $class,
|
2019-02-13 20:43:32 -06:00
|
|
|
$full_name, $email, $url, $bio));
|
|
|
|
}
|
|
|
|
|
2019-02-15 08:32:58 -06:00
|
|
|
// NUMBER --> BOOLEAN
|
|
|
|
// Delete a user by their ID.
|
|
|
|
function user_delete($id) {
|
|
|
|
return db_cmd("delete from lusers where id = " . $id);
|
|
|
|
}
|
|
|
|
|
2019-02-13 20:43:32 -06:00
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// NUMBER --> STRING
|
|
|
|
// Get a user's username from ID.
|
|
|
|
function user_id_to_name($id) {
|
|
|
|
return user_get($id, "username");
|
|
|
|
}
|
|
|
|
|
|
|
|
// STRING --> NUMBER
|
|
|
|
// Get a user's ID from username.
|
|
|
|
function user_name_to_id($username) {
|
2019-02-15 08:32:58 -06:00
|
|
|
return db_get_cell("lusers", "username", string_wrap($username), "id");
|
2019-02-13 20:43:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// NUMBER --> STRING
|
|
|
|
// Return a username from a user-ID
|
|
|
|
function user_name($id) {
|
2019-02-15 08:32:58 -06:00
|
|
|
return user_id_to_name($id);
|
2019-02-13 20:43:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NUMBER --> STRING
|
|
|
|
// Return a user's login-class from ID
|
2019-02-15 08:32:58 -06:00
|
|
|
function user_class($id) {
|
|
|
|
return user_get($id, "class");
|
2019-02-13 20:43:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NUMBER --> STRING
|
|
|
|
// Return a user's full name from ID
|
|
|
|
function user_full_name($id) {
|
|
|
|
return user_get($id, "full_name");
|
|
|
|
}
|
|
|
|
|
|
|
|
// NUMBER --> STRING
|
|
|
|
// Return a user's email from ID
|
|
|
|
function user_email($id) {
|
|
|
|
return user_get($id, "email");
|
|
|
|
}
|
|
|
|
|
|
|
|
// NUMBER --> STRING
|
|
|
|
// Return a user's website URL from ID
|
|
|
|
function user_website($id) {
|
|
|
|
return user_get($id, "website");
|
|
|
|
}
|
|
|
|
|
|
|
|
// NUMBER --> STRING
|
|
|
|
// Return a user's biography from ID
|
|
|
|
function user_biography($id) {
|
|
|
|
return user_get($id, "biography");
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2019-02-15 08:32:58 -06:00
|
|
|
|
|
|
|
// NUMBER --> ARRAY
|
|
|
|
// Fetch an array of a user's IDs
|
|
|
|
function user_ids() {
|
|
|
|
return db_get_columns("lusers", "id");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
2019-02-13 20:43:32 -06:00
|
|
|
// NUMBER --> ARRAY
|
|
|
|
// Fetch an array of a user's posts (by ID)
|
2019-02-15 08:32:58 -06:00
|
|
|
function user_posts($id) {
|
2019-02-18 23:24:07 -06:00
|
|
|
return db_get_cells("posts", "user", $id, array('id'));
|
2019-02-15 08:32:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// NUMBER --> ARRAY
|
|
|
|
// Return an array filled with all of a user's relevant data.
|
|
|
|
function user_data($id) {
|
|
|
|
return array('full_name' => user_full_name($id),
|
|
|
|
'name' => user_name($id),
|
|
|
|
'bio' => user_biography($id),
|
|
|
|
'email' => user_email($id),
|
|
|
|
'website' => user_website($id),
|
|
|
|
'posts' => user_posts($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
2019-02-20 08:04:55 -06:00
|
|
|
// NUMBER --> NUMBER
|
|
|
|
// Generate a new login-token and associate it with the user's account.
|
|
|
|
// Returns the token number.
|
|
|
|
function user_token_create($id) {
|
|
|
|
$token = rand(0, 5000000);
|
|
|
|
db_set_cell("lusers", "id", $id, "token", rand(0, 5000000));
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NUMBER NUMBER --> BOOLEAN
|
|
|
|
// Return whether or not a token is valid for a certain user account
|
|
|
|
function user_token_validate($id, $token) {
|
|
|
|
$valid_token = db_get_cell("lusers", "id", $id, "token");
|
|
|
|
|
|
|
|
if ($token == $valid_token) {
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// NUMBER --> NIL
|
|
|
|
// Log a user in-- create a token, then make a cookie with said token.
|
|
|
|
function user_log_in($id) {
|
|
|
|
$token = user_token_create($id);
|
|
|
|
setcookie("token", $token, 2628000);
|
|
|
|
setcookie("id", $id, 2628000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function logged() {
|
|
|
|
if (user_token_validate($id, $_COOKIE['token'])) {
|
|
|
|
return $id;
|
|
|
|
} else {
|
|
|
|
return "no";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
2019-02-15 08:32:58 -06:00
|
|
|
// NUMBER STRING --> BOOLEAN
|
|
|
|
// Return whether or not a given password is valid.
|
|
|
|
function user_valid_password($id, $password) {
|
|
|
|
return password_verify($password, user_get($id, "hash"));
|
2019-02-13 20:43:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|