Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Plugin loader
-> Frontend changes
  • Loading branch information
NaysKutzu committed Dec 6, 2024
1 parent 62f02b6 commit 6a1fa31
Show file tree
Hide file tree
Showing 26 changed files with 480 additions and 697 deletions.
3 changes: 1 addition & 2 deletions backend/app/Api/User/Auth/ForgotPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@

use MythicalClient\App;
use MythicalClient\Chat\User;
use MythicalClient\CloudFlare\CloudFlareRealIP;
use MythicalSystems\CloudFlare\Turnstile;
use MythicalClient\Config\ConfigInterface;
use MythicalSystems\CloudFlare\CloudFlare;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\CloudFlare\CloudFlareRealIP;

$router->add('/api/user/auth/forgot', function (): void {
$appInstance = App::getInstance(true);
Expand Down
3 changes: 1 addition & 2 deletions backend/app/Api/User/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@

use MythicalClient\App;
use MythicalClient\Chat\User;
use MythicalClient\CloudFlare\CloudFlareRealIP;
use MythicalClient\Mail\Mail;
use MythicalSystems\CloudFlare\Turnstile;
use MythicalClient\Config\ConfigInterface;
use MythicalSystems\CloudFlare\CloudFlare;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\CloudFlare\CloudFlareRealIP;

$router->add('/api/user/auth/login', function (): void {
$appInstance = App::getInstance(true);
Expand Down
3 changes: 1 addition & 2 deletions backend/app/Api/User/Auth/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@

use MythicalClient\App;
use MythicalClient\Chat\User;
use MythicalClient\CloudFlare\CloudFlareRealIP;
use MythicalSystems\CloudFlare\Turnstile;
use MythicalClient\Config\ConfigInterface;
use MythicalSystems\CloudFlare\CloudFlare;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\CloudFlare\CloudFlareRealIP;

$router->add('/api/user/auth/register', function (): void {
App::init();
Expand Down
3 changes: 1 addition & 2 deletions backend/app/Api/User/Auth/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
use MythicalClient\App;
use MythicalClient\Chat\User;
use MythicalClient\Chat\Verification;
use MythicalClient\CloudFlare\CloudFlareRealIP;
use MythicalSystems\CloudFlare\Turnstile;
use MythicalClient\Config\ConfigInterface;
use MythicalSystems\CloudFlare\CloudFlare;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\CloudFlare\CloudFlareRealIP;
use MythicalClient\Chat\columns\EmailVerificationColumns;

$router->get('/api/user/auth/reset', function (): void {
Expand Down
8 changes: 4 additions & 4 deletions backend/app/Api/User/Auth/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
setcookie('user_token', $token, time() + 3600, '/');
User::updateInfo(User::getTokenFromUUID(Verification::getUserUUID($code)), UserColumns::VERIFIED, 'true', false);
Verification::delete($code);
die(header('location: /'));
} else {
$appInstance->BadRequest('Bad Request', ['error_code' => 'INVALID_USER','email_code' => $code]);
exit(header('location: /'));
}
$appInstance->BadRequest('Bad Request', ['error_code' => 'INVALID_USER', 'email_code' => $code]);

} else {
$appInstance->BadRequest('Bad Request', ['error_code' => 'INVALID_USER','email_code' => $code]);
$appInstance->BadRequest('Bad Request', ['error_code' => 'INVALID_USER', 'email_code' => $code]);
}
} else {
$appInstance->BadRequest('Bad Request', ['error_code' => 'INVALID_CODE', 'email_code' => $code]);
Expand Down
17 changes: 11 additions & 6 deletions backend/app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use MythicalSystems\Utils\XChaCha20;
use MythicalClient\Config\ConfigFactory;
use MythicalClient\Logger\LoggerFactory;
use MythicalClient\Plugins\PluginCompiler;

class App extends \MythicalSystems\Api\Api
{
Expand Down Expand Up @@ -93,8 +92,12 @@ public function __construct(bool $softBoot)
self::InternalServerError('Failed to connect to Redis', null);
}

new PluginCompiler();

/**
* Initialize the plugin manager.
*/
Plugins\PluginManager::loadKernel();
define('LOGGER', $this->getLogger());

$router = new rt();
$this->registerApiRoutes($router);

Expand Down Expand Up @@ -257,11 +260,13 @@ public function decrypt(string $data): string
return XChaCha20::decrypt($data, $_ENV['DATABASE_ENCRYPTION_KEY'], true);
}

public function generateCode() : string {
public function generateCode(): string
{
$code = base64_encode(random_bytes(64));
$code = str_replace('=', '', $code);
$code = str_replace('+','', $code);
$code = str_replace('/','', $code);
$code = str_replace('+', '', $code);
$code = str_replace('/', '', $code);

return $code;
}
}
9 changes: 4 additions & 5 deletions backend/app/Chat/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
use MythicalClient\App;
use MythicalClient\Chat\columns\UserColumns;
use MythicalClient\CloudFlare\CloudFlareRealIP;
use MythicalSystems\CloudFlare\CloudFlare;

class Session extends Database
{
Expand All @@ -46,9 +45,9 @@ public function __construct(App $app)
if (isset($_COOKIE['user_token']) && !$_COOKIE['user_token'] == '') {
if (User::exists(UserColumns::ACCOUNT_TOKEN, $_COOKIE['user_token'])) {
try {
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
$this->app = $app;
$this->SESSION_KEY = $_COOKIE['user_token'];
$this->updateLastSeen();
Expand Down Expand Up @@ -92,7 +91,7 @@ public function updateLastSeen(): void
$ip = CloudFlareRealIP::getRealIP();
$this->app->getLogger()->info('Updating last seen for ' . $this->SESSION_KEY . ' with IP: ' . $ip);
$con->exec('UPDATE ' . User::TABLE_NAME . ' SET last_seen = NOW() WHERE token = "' . $this->SESSION_KEY . '";');
$con->exec('UPDATE ' . User::TABLE_NAME . ' SET last_ip = "'.$ip.'" WHERE token = "' . $this->SESSION_KEY . '";');
$con->exec('UPDATE ' . User::TABLE_NAME . ' SET last_ip = "' . $ip . '" WHERE token = "' . $this->SESSION_KEY . '";');
} catch (\Exception $e) {
$this->app->getLogger()->error('Failed to update last seen: ' . $e->getMessage());
}
Expand Down
38 changes: 37 additions & 1 deletion backend/app/Cli/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private function handleCustomCommands(string $cmdName, array $subCmd): void

exit;
} elseif ($cmdName == 'frontend:watch') {
$process = popen('cd frontend && yarn watch 2>&1', 'r');
$process = popen('cd frontend && yarn dev 2>&1', 'r');
if (is_resource($process)) {
while (!feof($process)) {
$output = fgets($process);
Expand Down Expand Up @@ -156,6 +156,42 @@ private function handleCustomCommands(string $cmdName, array $subCmd): void
$this->sendOutput($this->prefix . 'Failed to start lint process.');
}
exit;
} else if ($cmdName == "backend:watch") {
$process = popen("tail -f backend/storage/logs/mythicalclient.log backend/storage/logs/framework.log", "r");
$this->sendOutput("Please wait while we attach to the process...");
$this->sendOutput(message: "\n");
sleep(5);
if (is_resource($process)) {
$this->sendOutput("Attached to the process.");
$this->sendOutput(message: "\n");
while (!feof($process)) {
$output = fgets($process);
if (strpos($output, "[DEBUG]") !== false) {
$this->sendOutput($this->prefix . "\e[34m" . $output . "\e[0m"); // Blue for DEBUG
} elseif (strpos($output, "[INFO]") !== false) {
$this->sendOutput($this->prefix . "\e[32m" . $output . "\e[0m"); // Green for INFO
} elseif (strpos($output, "[WARNING]") !== false) {
$this->sendOutput($this->prefix . "\e[33m" . $output . "\e[0m"); // Yellow for WARNING
} elseif (strpos($output, "[ERROR]") !== false) {
$this->sendOutput($this->prefix . "\e[31m" . $output . "\e[0m"); // Red for ERROR
} elseif (strpos($output, "[CRITICAL]") !== false) {
$this->sendOutput($this->prefix . "\e[35m" . $output . "\e[0m"); // Magenta for CRITICAL
} else {
$this->sendOutput($this->prefix . $output);
}
}
$returnVar = pclose($process);
if ($returnVar !== 0) {
$this->sendOutput('Failed to watch backend.');
$this->sendOutput(message: "\n");
} else {
$this->sendOutput('Backend is now being watched.');
$this->sendOutput("\n");
}
} else {
$this->sendOutput($this->prefix . 'Failed to start watch process.');
}
exit;
}
}
}
42 changes: 37 additions & 5 deletions backend/app/CloudFlare/CloudFlareRealIP.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
<?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\CloudFlare;

class CloudFlareRealIP {
public static function getRealIP() {
class CloudFlareRealIP
{
public static function getRealIP()
{
if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
return $_SERVER['HTTP_CF_CONNECTING_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
return $_SERVER['REMOTE_ADDR'];
}

return $_SERVER['REMOTE_ADDR'];

}
}
}
17 changes: 12 additions & 5 deletions backend/app/Logger/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,34 @@ public function __construct(string $logFile)

public function info(string $message): void
{
$this->appendLog('[INFO] ' . $message);
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? 'unknown';
$this->appendLog('[INFO] ['. $caller .'] '. $message);
}

public function warning(string $message): void
{
$this->appendLog('[WARNING] ' . $message);
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? 'unknown';
$this->appendLog('[WARNING] ['. $caller .'] '. $message);
}

public function error(string $message): void
{
$this->appendLog('[ERROR] ' . $message);
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? 'unknown';
$this->appendLog('[ERROR] ['. $caller .'] '. $message);
}

public function critical(string $message): void
{
$this->appendLog('[CRITICAL] ' . $message);
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? 'unknown';
$this->appendLog('[CRITICAL] ['. $caller .'] '. $message);
}

public function debug(string $message): void
{
$this->appendLog('[DEBUG] ' . $message);
if (APP_DEBUG == true) {
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? 'unknown';
$this->appendLog('[DEBUG] [' . $caller . '] ' . $message);
}
}

private function getFormattedDate(): string
Expand Down
Loading

0 comments on commit 6a1fa31

Please sign in to comment.