Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. $app->group('/get', function() use ($app)
  2. {
  3.    
  4.     if(!isset($_POST['userid'])) {
  5.         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.");
  6.     } else {
  7.         $requester = fetchUserId($_POST['userid']);
  8.         if(!$requester) {
  9.             sendFail("Invalid userid variable. Perhaps it's not associated with their account yet?");
  10.         }
  11.     }
  12.    
  13.     $app->post('/notes/:username', function($username)
  14.     {
  15.         $player = fetchPlayerInfo($username);
  16.         $amount = 5;
  17.         if(isset($_POST['amount']) && inRange($_POST['amount'], 1, 30)) {
  18.             $amount = $_POST['amount'];
  19.         }
  20.         fetchPlayerNotes($player, $amount);
  21.     });
  22. });
  23.  
  24. //Called Functions:
  25.  
  26. function fetchPlayerInfo($username, $customTooManyResults = false)
  27. {
  28.     global $db;
  29.    
  30.     $profiles = $db->all("SELECT * FROM profiles WHERE `username` LIKE ? OR `aliases` LIKE ?", ["{$username}%", "%{$username}%"]);
  31.     $nprofiles = count($profiles);
  32.    
  33.     if($nprofiles == 0) {
  34.         sendFail("No Profile found for **{$username}**.");
  35.     } else if($nprofiles == 1) {
  36.         $data = $profiles[0];
  37.         $info = [
  38.             "uid" => $data['uid'],
  39.             "uuid" => $data['uuid'],
  40.             "username" => $data['username'],
  41.             "aliases" => $data['aliases']
  42.         ];
  43.         return $info;
  44.     } else if(!$customTooManyResults) {
  45.         sendFail("Too many results found for **{$username}**. Please try one of the following:\n" . implode(", ", array_column($profiles, 'username')));
  46.     } else {
  47.         return $profiles;
  48.     }
  49. }
  50.  
  51. function inRange($number, $min, $max)
  52. {
  53.     if($number < $min || $number > $max)
  54.         return false;
  55.     return true;
  56. }
  57.  
  58. function fetchPlayerNotes($player, $amount)
  59. {
  60.     global $db;
  61.     $amount = (int) $amount; //neccessary to prevent sql injection, PDO doesn't allow it to be inserted dynamically.
  62.     $results = $db->all("SELECT * FROM notes WHERE `profile`=? ORDER BY timestamp DESC LIMIT {$amount}", [$player['uid']]);
  63.     if(empty($results))
  64.         sendSuccess("No notes found! `Good Egg?`");
  65.     //$output = [];
  66.     foreach($results as $key => $note) {
  67.         $noter    = fetchUserDisplayname($note['noter']);
  68.         $output[] = ($key + 1) . ": {$note['note']} - {$noter}";
  69.     }
  70.     sendSuccess("Latest notes for **{$player['username']}**:\n```css\n" . implode("\n", $output) . "```");
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement