Skip to content

Commit

Permalink
✨ Commented some code
Browse files Browse the repository at this point in the history
  • Loading branch information
anditv21 committed Dec 8, 2023
1 parent 253b17e commit e83d468
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 109 deletions.
71 changes: 52 additions & 19 deletions src/app/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,14 @@ public function isDiscordLinked()

public function discord_link($code)
{
// Get the user ID from the session
$uid = Session::Get("uid");

// Check if the Discord authorization code is provided
if (!empty($code)) {
$discord_code = $code;


// Set up the payload for the token request to Discord
$payload = [
'code' => $discord_code,
'client_id' => Util::securevar(client_id),
Expand All @@ -581,88 +585,117 @@ public function discord_link($code)
'redirect_uri' => Util::securevar(SITE_URL . SUB_DIR . '/user/profile.php'),
'scope' => 'identify',
];


// Convert the payload to a URL-encoded string
$payload_string = http_build_query($payload);


$discord_token_url = "https://discordapp.com/api/v9/oauth2/token";



$ch = curl_init();

// Set cURL options for token request
curl_setopt($ch, CURLOPT_URL, $discord_token_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);



$result = curl_exec($ch);



if ($result === false) {
Util::display("Error: " . Util::securevar(curl_error($ch)));
curl_close($ch);
exit();
}



$result = json_decode($result, true);



if (!isset($result["access_token"])) {
Util::display("Error: Failed to get access token from Discord.");
exit();
}



if (!isset($result["refresh_token"])) {
Util::display("Error: Failed to get refresh token from Discord.");
exit();
}


// Securely store access and refresh tokens
$access_token = Util::securevar($result["access_token"]);
$refresh_token = Util::securevar($result["refresh_token"]);


// Discord user info URL
$discord_users_url = "https://discordapp.com/api/users/@me";

// Set cURL options for user info request
$header = [
"Authorization: Bearer $access_token",
"Content-Type: application/x-www-form-urlencoded",
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $discord_users_url);
curl_setopt($ch, CURLOPT_POST, false);



$result = curl_exec($ch);



if ($result === false) {
Util::display("Error: " . Util::securevar(curl_error($ch)));
curl_close($ch);
exit();
}



$result = json_decode($result, true);



if (!isset($result["id"])) {
Util::display("Error: Failed to get user ID from Discord.");
exit();
}


// Securely store user ID and avatar
$id = Util::securevar($result["id"]);
$avatar = Util::securevar($result["avatar"]);


// Set the path for saving the user's avatar
$path = Util::securevar(IMG_DIR . $uid);


// Delete existing avatar files
if (@getimagesize($path . ".png")) {
unlink($path . ".png");
} elseif (@getimagesize($path . ".jpg")) {
unlink($path . ".jpg");
} elseif (@getimagesize($path . ".gif")) {
unlink($path . ".gif");
}


// Download and save the user's avatar from Discord
$url = "https://cdn.discordapp.com/avatars/$id/$avatar.png";
$img = $path . ".png";
file_put_contents($img, file_get_contents($url));

// Set appropriate permissions on directories and files
chmod(IMG_DIR, 0775);
chmod($img, 0775);

// Set the access token, refresh token, and Discord ID in the session
$this->set_access_token($access_token);
$this->set_refresh_token($refresh_token);
$this->set_dcid($id, $uid);

// Redirect to the user's profile page
header("location: profile.php");
}
}



public function downloadAvatarWithAccessToken($userId, $uid)
Expand Down
64 changes: 46 additions & 18 deletions src/app/controllers/UtilController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ public static function display($string)
echo $string;
}


/**
* Sanitizes and secures a variable or an array of variables.
*
* This function applies HTML escaping, removes extra whitespace, and
* protects against potential cross-site scripting (XSS) attacks.
*
* @param mixed $var The variable or array to be secured.
* @return mixed The secured variable or array.
*/

public static function securevar($var)
{
if (empty($var)) {
Expand Down Expand Up @@ -174,25 +185,60 @@ public static function muteCheck()
return $res;
}

/**
* Calculate the number of days since the user's join date,
* based on the stored 'createdAt' value in the session.
*
* @return int The number of days since the user joined.
*/
public static function getjoin()
{
$joindate = Session::get("createdAt");
$now = new DateTime();
$date = new DateTime($joindate);
$interval = $now->diff($date);

// Return the number of days as an integer
return (int) $interval->format("%a");
}


/**
* Calculate the number of days since a specified join date.
*
* @param string $joindate The join date in "Y-m-d H:i:s" format.
*
* @return int The number of days since the specified join date.
*/
public static function getjoinprofile($joindate)
{
$now = new DateTime();
$date = DateTime::createFromFormat("Y-m-d H:i:s", $joindate);
$interval = $now->diff($date);

// Return the number of days as an integer
return (int) $interval->format("%a");
}


public static function daysago($dateString)
{
if (!$dateString) {
return 'Not available';
}
$date = strtotime($dateString);
$now = time();
$diff = $now - $date;
$days = floor($diff / (60 * 60 * 24));
if ($days == 0) {
return 'Today';
} elseif ($days == 1) {
return 'Yesterday';
} else {
return $days . ' days ago';
}
}

public static function getavatar($uid)
{
$path = IMG_DIR . $uid;
Expand Down Expand Up @@ -234,22 +280,4 @@ public static function getextention($uid)
return false;
}
}

public static function daysago($dateString)
{
if (!$dateString) {
return 'Not available';
}
$date = strtotime($dateString);
$now = time();
$diff = $now - $date;
$days = floor($diff / (60 * 60 * 24));
if ($days == 0) {
return 'Today';
} elseif ($days == 1) {
return 'Yesterday';
} else {
return $days . ' days ago';
}
}
}
Loading

0 comments on commit e83d468

Please sign in to comment.