Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Finish #126
-> Added translations for account area
-> Finish #127
  • Loading branch information
NaysKutzu committed Dec 22, 2024
1 parent 5cb6060 commit b1062fe
Show file tree
Hide file tree
Showing 18 changed files with 539 additions and 210 deletions.
10 changes: 6 additions & 4 deletions backend/app/Api/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
*/

use MythicalClient\App;
use MythicalClient\Chat\Mails;
use MythicalClient\Chat\User;
use MythicalClient\Chat\Roles;
use MythicalClient\Chat\Billing;
use MythicalClient\Chat\Session;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\Chat\UserActivities;
use MythicalClient\Chat\columns\UserColumns;

$router->post('/api/user/session/info/update', function (): void {
App::init();
Expand Down Expand Up @@ -183,7 +184,6 @@

});


$router->get('/api/user/session/activities', function (): void {
App::init();
$appInstance = App::getInstance(true);
Expand All @@ -196,6 +196,8 @@
$accountToken = $session->SESSION_KEY;

$appInstance->OK('User activities', [
'activities' => UserActivities::get(User::getInfo($accountToken, UserColumns::UUID, false))
'activities' => UserActivities::get(User::getInfo($accountToken, UserColumns::UUID, false)),
]);
});
});


89 changes: 89 additions & 0 deletions backend/app/Api/User/Session/Emails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
use MythicalClient\App;
use MythicalClient\Chat\Mails;
use MythicalClient\Chat\User;
use MythicalClient\Chat\Roles;
use MythicalClient\Chat\Billing;
use MythicalClient\Chat\Session;
use MythicalClient\Chat\UserActivities;
use MythicalClient\Chat\columns\UserColumns;


$router->get('/api/user/session/emails', function (): void {
App::init();
$appInstance = App::getInstance(true);
$config = $appInstance->getConfig();

$appInstance->allowOnlyGET();

$session = new Session($appInstance);

$accountToken = $session->SESSION_KEY;

$appInstance->OK('User emails', [
'emails' => Mails::getAll(User::getInfo($accountToken, UserColumns::UUID, false))
]);
});


$router->get('/api/user/session/emails/(.*)/raw', function (string $id): void {
$appInstance = App::getInstance(true);
$config = $appInstance->getConfig();
if ($id == '') {
die(header('location: /account'));
}

if (!is_numeric($id)) {
die(header('location: /account'));
}
$id = (int) $id;

$appInstance->allowOnlyGET();

$session = new Session($appInstance);

$accountToken = $session->SESSION_KEY;

if (Mails::exists($id)) {
if (Mails::doesUserOwnEmail(User::getInfo($accountToken, UserColumns::UUID, false), $id)) {
$mail = Mails::get($id);
header('Content-Type: text/html; charset=utf-8');
echo $mail['body'];
exit;
} else {
die(header('location: /account'));
}
} else {
die(header('location: /account'));
}
});

$router->delete('/api/user/session/emails/(.*)/delete', function (string $id): void {
$appInstance = App::getInstance(true);
$config = $appInstance->getConfig();
if ($id == '') {
$appInstance->BadRequest('Email not found!', ['error_code' => 'EMAIL_NOT_FOUND']);
}

if (!is_numeric($id)) {
$appInstance->BadRequest('Email not found!', ['error_code' => 'EMAIL_NOT_FOUND']);
}
$id = (int) $id;

$appInstance->allowOnlyDELETE();

$session = new Session($appInstance);

$accountToken = $session->SESSION_KEY;

if (Mails::exists($id)) {
if (Mails::doesUserOwnEmail(User::getInfo($accountToken, UserColumns::UUID, false), $id)) {
Mails::delete($id, User::getInfo($accountToken, UserColumns::UUID, false));
$appInstance->OK('Email deleted successfully!', []);
} else {
$appInstance->Unauthorized('Unauthorized', ['error_code' => 'UNAUTHORIZED']);
}
} else {
$appInstance->BadRequest('Email not found!', ['error_code' => 'EMAIL_NOT_FOUND']);
}
});
169 changes: 169 additions & 0 deletions backend/app/Chat/Mails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

/*
* This file is part of MythicalClient.
* Please view the LICENSE file that was distributed with this source code.
*
* MIT License
*
* (c) MythicalSystems <mythicalsystems.xyz> - All rights reserved
* (c) NaysKutzu <nayskutzu.xyz> - All rights reserved
* (c) Cassian Gherman <nayskutzu.xyz> - All rights reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace MythicalClient\Chat;

class Mails
{
/**
* Add a mail.
*
* @param string $subject Mail subject
* @param string $body Mail body
* @param string $uuid User UUID
*
* @return bool
*/
public static function add(string $subject, string $body, string $uuid): bool
{
try {
$dbConn = Database::getPdoConnection();
$from = \MythicalClient\App::getInstance(true)->getConfig()->getSetting(\MythicalClient\Config\ConfigInterface::SMTP_FROM, '[email protected]');
$stmt = $dbConn->prepare('INSERT INTO ' . self::getTableName() . ' (subject, body, `from`, `user`) VALUES (:subject, :body, :from, :user)');
$stmt->bindParam(':subject', $subject);
$stmt->bindParam(':body', $body);
$stmt->bindParam(':from', $from);
$stmt->bindParam(':user', $uuid);

return $stmt->execute();
} catch (\Exception $e) {
return false;
}

}
/**
* Delete a mail.
*
* @param string $id Mail ID
* @param string $uuid User UUID
*
* @return bool
*/
public static function delete(string $id, string $uuid): bool
{
try {
$dbConn = Database::getPdoConnection();
$stmt = $dbConn->prepare('DELETE FROM ' . self::getTableName() . ' WHERE id = :id AND `user` = :user');
$stmt->bindParam(':id', $id);
$stmt->bindParam(':user', $uuid);

return $stmt->execute();
} catch (\Exception $e) {
return false;
}
}
/**
* Get all mails for a user.
*
* @param string $uuid User UUID
*
* @return array
*/
public static function getAll(string $uuid): array
{
try {
$dbConn = Database::getPdoConnection();
$stmt = $dbConn->prepare('SELECT * FROM ' . self::getTableName() . ' WHERE `user` = :user ORDER BY id DESC LIMIT 50');
$stmt->bindParam(':user', $uuid);
$stmt->execute();

return $stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $e) {
return [];
}
}
/**
* Get a mail.
*
* @param string $id Mail ID
*
* @return array Mail data
*/
public static function get(string $id): array
{
try {
$dbConn = Database::getPdoConnection();
$stmt = $dbConn->prepare('SELECT * FROM ' . self::getTableName() . ' WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();

return $stmt->fetch(\PDO::FETCH_ASSOC);
} catch (\Exception $e) {
return [];
}
}
/**
* Check if a mail exists.
*
* @param string $id Mail ID
*
* @return bool Does mail exist
*/
public static function exists(string $id): bool
{
try {
$dbConn = Database::getPdoConnection();
$stmt = $dbConn->prepare('SELECT * FROM ' . self::getTableName() . ' WHERE id = :id');
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt->rowCount() > 0;
} catch (\Exception $e) {
return false;
}
}
/**
* Get all mails for a user.
*
* @param string $uuid User UUID
* @param string $id Mail ID
*
* @return bool Does user own email
*/
public static function doesUserOwnEmail(string $uuid, string $id): bool
{
try {
$dbConn = Database::getPdoConnection();
$stmt = $dbConn->prepare('SELECT * FROM ' . self::getTableName() . ' WHERE id = :id AND `user` = :user');
$stmt->bindParam(':id', $id);
$stmt->bindParam(':user', $uuid);
$stmt->execute();

return $stmt->rowCount() > 0;
} catch (\Exception $e) {
return false;
}
}

public static function getTableName(): string
{
return 'mythicalclient_users_mails';
}
}
5 changes: 3 additions & 2 deletions backend/app/Chat/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@

use Gravatar\Gravatar;
use MythicalClient\App;
use MythicalClient\Chat\interface\UserActivitiesTypes;
use MythicalClient\Mail\Mail;
use MythicalClient\Mail\templates\Verify;
use MythicalSystems\CloudFlare\CloudFlare;
use MythicalClient\Mail\templates\NewLogin;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\Mail\templates\ResetPassword;
use MythicalClient\Chat\interface\UserActivitiesTypes;
use MythicalClient\Chat\columns\EmailVerificationColumns;
use MythicalSystems\CloudFlare\CloudFlare;

class User extends Database
{
Expand Down Expand Up @@ -225,6 +225,7 @@ public static function login(string $login, string $password): string
}
}
UserActivities::add($user['uuid'], UserActivitiesTypes::$login, CloudFlare::getRealUserIP());

return $user['token'];
}

Expand Down
Loading

0 comments on commit b1062fe

Please sign in to comment.