Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $app->group('/get', function() use ($app)
- {
- if(!isset($_POST['userid'])) {
- sendFail("Please submit a POST variable called \"userid\" which should be equal to the Discord ID or Minecraft UUID of the user requesting information from the API.");
- } else {
- $requester = fetchUserId($_POST['userid']);
- if(!$requester) {
- sendFail("Invalid userid variable. Perhaps it's not associated with their account yet?");
- }
- }
- $app->post('/notes/:username', function($username)
- {
- $player = fetchPlayerInfo($username);
- $amount = 5;
- if(isset($_POST['amount']) && inRange($_POST['amount'], 1, 30)) {
- $amount = $_POST['amount'];
- }
- fetchPlayerNotes($player, $amount);
- });
- });
- //Called Functions:
- function fetchPlayerInfo($username, $customTooManyResults = false)
- {
- global $db;
- $profiles = $db->all("SELECT * FROM profiles WHERE `username` LIKE ? OR `aliases` LIKE ?", ["{$username}%", "%{$username}%"]);
- $nprofiles = count($profiles);
- if($nprofiles == 0) {
- sendFail("No Profile found for **{$username}**.");
- } else if($nprofiles == 1) {
- $data = $profiles[0];
- $info = [
- "uid" => $data['uid'],
- "uuid" => $data['uuid'],
- "username" => $data['username'],
- "aliases" => $data['aliases']
- ];
- return $info;
- } else if(!$customTooManyResults) {
- sendFail("Too many results found for **{$username}**. Please try one of the following:\n" . implode(", ", array_column($profiles, 'username')));
- } else {
- return $profiles;
- }
- }
- function inRange($number, $min, $max)
- {
- if($number < $min || $number > $max)
- return false;
- return true;
- }
- function fetchPlayerNotes($player, $amount)
- {
- global $db;
- $amount = (int) $amount; //neccessary to prevent sql injection, PDO doesn't allow it to be inserted dynamically.
- $results = $db->all("SELECT * FROM notes WHERE `profile`=? ORDER BY timestamp DESC LIMIT {$amount}", [$player['uid']]);
- if(empty($results))
- sendSuccess("No notes found! `Good Egg?`");
- //$output = [];
- foreach($results as $key => $note) {
- $noter = fetchUserDisplayname($note['noter']);
- $output[] = ($key + 1) . ": {$note['note']} - {$noter}";
- }
- sendSuccess("Latest notes for **{$player['username']}**:\n```css\n" . implode("\n", $output) . "```");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement