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. function user_create($name, $password, $class="spectator", $full_name=NULL, $email=NULL, $url=NULL, $bio=NULL) { $id = db_new_id("lusers", "id"); $password = password_hash($password, PASSWORD_BCRYPT, array('cost' => 11)); return db_insert_row("lusers", array("id", "username", "hash", "class", "full_name", "email", "website", "biography"), array($id, $name, $password, $class, $full_name, $email, $url, $bio)); } // NUMBER --> BOOLEAN // Delete a user by their ID. function user_delete($id) { return db_cmd("delete from lusers where id = " . $id); } // ------------------------------------- // 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) { return db_get_cell("lusers", "username", string_wrap($username), "id"); } // ------------------------------------- // NUMBER --> STRING // Return a username from a user-ID function user_name($id) { return user_id_to_name($id); } // NUMBER --> STRING // Return a user's login-class from ID function user_class($id) { return user_get($id, "class"); } // 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"); } // ------------------------------------- // NUMBER --> ARRAY // Fetch an array of a user's IDs function user_ids() { return db_get_columns("lusers", "id"); } // ------------------------------------- // NUMBER --> ARRAY // Fetch an array of a user's posts (by ID) function user_posts($id) { return db_get_cells("posts", "user", $id, array('id')); } // ------------------------------------- // 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)); } // ------------------------------------- // 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"; } } // ------------------------------------- // 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")); } ?>