Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Bump packages
-> Starting to create a firewall
-> Finish registration
-> Added turnstile
-> Added a nice template for the framework
-> Wrote the basics of the api
-> Extended the api to be better
-> Added an option to show or hide errors via debugger
-> Rewrote the template :))
-> Added a easy page.twig that everyone can base on components to build new pages :)
  • Loading branch information
NaysKutzu committed Aug 9, 2024
1 parent c662ac0 commit 88592e1
Show file tree
Hide file tree
Showing 170 changed files with 9,786 additions and 337 deletions.
4 changes: 2 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MythicalFramework

One of the best frameworks to start learning php <3
One of the best frameworks to start learning php <3 and building production ready applications with over `11.40K` lines of code!

## Support
Donations:
Expand All @@ -13,4 +13,4 @@ Donations:

![Alt](https://repobeats.axiom.co/api/embed/c403f00d546911a7f80a4e9ace2628c2ab760b04.svg "Repobeats analytics image")

*Copyright MythicalSystems LTD® 2020-2024*
*Copyright MythicalSystems LTD® 2020-2024*
5 changes: 2 additions & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
"chuaple.php-formatter",
"esbenp.prettier-vscode",
"lkrms.pretty-php",
"m-thorsen.smarty-double-delimiters",
"aswinkumar863.smarty-template-support",
"bradlc.vscode-tailwindcss",
"zarifprogrammer.tailwind-snippets"
"zarifprogrammer.tailwind-snippets",
"fincity.rest-api-tester"
]
}
23 changes: 23 additions & 0 deletions api/User/infoalreadyexists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use MythicalSystems\Api\Api as api;
use MythicalSystemsFramework\User\UserHelper;

api::init();
api::allowOnlyGET();
if (isset($_GET['info']) && !$_GET['info'] == '') {
if (isset($_GET['value']) && !$_GET['value'] == '') {
$info = $_GET['info'];
$value = $_GET['value'];
$user = UserHelper::doesInfoAboutExist($info, $value);
if ($user == 'INFO_EXISTS') {
api::OK('The info exists!', ['RESULT' => $user]);
} elseif ($user == 'INFO_NOT_FOUND') {
api::BadRequest('The info does not exist!', ['RESULT' => $user]);
}
} else {
api::BadRequest('You are missing the post field for value!', []);
}
} else {
api::BadRequest('You are missing the post field for info!', []);
}
3 changes: 0 additions & 3 deletions api/User/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@

api::init();
api::allowOnlyPOST();

if (isset($_POST['email']) && isset($_POST['password'])) {
}
65 changes: 65 additions & 0 deletions api/User/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

use MythicalSystemsFramework\Api\Api as api;
use MythicalSystemsFramework\Kernel\Debugger;
use MythicalSystemsFramework\Mail\MailService;
use MythicalSystemsFramework\User\UserHelper as user;
use MythicalSystemsFramework\Handlers\ActivityHandler;

api::init();
api::allowOnlyPOST();

// Hide all errors
Debugger::HideAllErrors();
try {
if (isset($_POST['username'])) {
api::makeSureValueIsNotNull($_POST['username'], ['message' => 'You are missing the post field for username!']);
} else {
api::BadRequest('You are missing the post field for username!', []);
}

if (isset($_POST['password'])) {
api::makeSureValueIsNotNull($_POST['password'], ['message' => 'You are missing the post field for password!']);
} else {
api::BadRequest('You are missing the post field for password!', []);
}

if (isset($_POST['email'])) {
api::makeSureValueIsNotNull($_POST['email'], ['message' => 'You are missing the post field for email!']);
} else {
api::BadRequest('You are missing the post field for email!', []);
}

if (isset($_POST['first_name'])) {
api::makeSureValueIsNotNull($_POST['first_name'], ['message' => 'You are missing the post field for first_name!']);
} else {
api::BadRequest('You are missing the post field for first_name!', []);
}

if (isset($_POST['last_name'])) {
api::makeSureValueIsNotNull($_POST['last_name'], ['message' => 'You are missing the post field for last_name!']);
} else {
api::BadRequest('You are missing the post field for last_name!', []);
}

$user = user::create($_POST['username'], $_POST['password'], $_POST['email'], $_POST['first_name'], $_POST['last_name'], MythicalSystems\CloudFlare\CloudFlare::getRealUserIP());

if ($user == 'ERROR_USERNAME_EXISTS') {
api::BadRequest('The username exists!', ['RESULT' => $user]);
} elseif ($user == 'ERROR_EMAIL_EXISTS') {
api::BadRequest('The email exists!', ['RESULT' => $user]);
} elseif ($user == 'ERROR_DATABASE_INSERT_FAILED') {
api::BadRequest('Failed to insert the user into the database!', ['RESULT' => $user]);
} else {
$user_id = user::getSpecificUserData($user, 'uuid', false);
if (MailService::isEnabled() == true) {
// TODO: Add a verify system
} else {
user::updateSpecificUserData($user, 'verified', 'true');
ActivityHandler::addActivity($user_id, user::getSpecificUserData($user, 'username', false), 'User created an account!', MythicalSystems\CloudFlare\CloudFlare::getRealUserIP(), 'USER_CREATED');
ActivityHandler::addActivity($user_id, user::getSpecificUserData($user, 'username', false), 'User verified his account!', MythicalSystems\CloudFlare\CloudFlare::getRealUserIP(), 'USER_VERIFIED');
}
api::OK('The user has been created!', ['TOKEN' => $user]);
}
} catch (Exception $e) {
}
15 changes: 15 additions & 0 deletions app/Api/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MythicalSystemsFramework\Api;

class Api extends \MythicalSystems\Api\Api
{
public static function makeSureValueIsNotNull(string $info, ?array $array): void
{
if (!$info == '') {
return;
} else {
self::BadRequest("You are missing the field for $info!", $array);
}
}
}
18 changes: 18 additions & 0 deletions app/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace MythicalSystemsFramework;

class App extends \MythicalSystems\Main
{
/**
* Convert a string to a bool.
*/
public static function convertStringToBool(string $value): bool
{
if ($value == 'true') {
return true;
} else {
return false;
}
}
}
30 changes: 30 additions & 0 deletions app/CloudFlare/CloudFlare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace MythicalSystemsFramework\CloudFlare;

class CloudFlare extends \MythicalSystems\CloudFlare\CloudFlare
{
/**
* Get the ip of a user.
*
* @return string|null The ipv4 or ipv6 or null incase if the ip is not valid or was tampered with!
*/
public static function getUserIP(): ?string
{
$ip = \MythicalSystems\CloudFlare\CloudFlare::getRealUserIP();
// Check if the ip is valid
if (filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
} else {
return null;
}
}

/**
* DEPRECATED: Use getUserIP() instead.
*/
public static function getRealUserIP(): string
{
return self::getUserIP();
}
}
17 changes: 17 additions & 0 deletions app/CloudFlare/TurnStile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace MythicalSystemsFramework\CloudFlare;

use MythicalSystemsFramework\App;
use MythicalSystemsFramework\Managers\Settings as setting;

class TurnStile extends \MythicalSystems\CloudFlare\Turnstile
{
/**
* Is cloudflare turnstile enabled?
*/
public static function isEnabled(): bool
{
return App::convertStringToBool(setting::getSetting('cloudflare_turnstile', 'enabled'));
}
}
10 changes: 10 additions & 0 deletions app/Firewall/Events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MythicalSystemsFramework\Firewall;

interface Events
{
public const DROP = 'drop';
public const ALLOW = 'allow';
public const NONE = 'none';
}
97 changes: 97 additions & 0 deletions app/Firewall/Firewall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace MythicalSystemsFramework\Firewall;

use MythicalSystemsFramework\Kernel\Logger;
use MythicalSystemsFramework\Database\MySQL;
use MythicalSystemsFramework\Kernel\LoggerTypes;
use MythicalSystemsFramework\Kernel\LoggerLevels;
use MythicalSystemsFramework\CloudFlare\CloudFlare;

class Firewall extends CloudFlare implements Types
{
/**
* Check if a ip is allowed to pass!
*
* @param string|null $ip The ip of the user <3
*
* @return string (DROP|NONE|ALLOW|DATABASE_ERROR)
*/
public static function check(?string $ip): string
{
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();

$stmt = $conn->prepare('SELECT * FROM framework_firewall WHERE ip = ?');
$stmt->bind_param('s', $ip);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$action = $row['action'];

if ($action === 'DROP' || $action === 'NONE' || $action === 'ALLOW') {
return $action;
} else {
return 'DATABASE_ERROR';
}
} else {
self::addIP($ip);

return 'NONE';
}
}

/**
* Add an ip to the database.
*
* @param string $ip The ip of the user <3
*
* @return string The status of the operation. (IP_ADDED/DATABASE_ERROR)
*/
public static function addIP(string $ip): string
{
try {
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();

$stmt = $conn->prepare('INSERT INTO framework_firewall (ip, action) VALUES (?, ?)');
$type = Types::NONE;
$stmt->bind_param('ss', $ip, $type);
$stmt->execute();
$stmt->close();

return 'IP_ADDED';
} catch (\Exception $e) {
Logger::log(LoggerTypes::DATABASE, LoggerLevels::ERROR, '(App/Firewall/Firewall.php) Failed to insert ip into database: ' . $e->__toString());

return 'DATABASE_ERROR';
}
}

/**
* Assign a owner for a ip address!
*/
public static function assignOwnership(string $ip, string $uuid): string
{
try {
if (self::check($ip) === 'NONE') {
}
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();

$stmt = $conn->prepare('UPDATE framework_firewall SET owner = ? WHERE ip = ?');
$stmt->bind_param('ss', $uuid, $ip);
$stmt->execute();
$stmt->close();

return 'OWNER_ASSIGNED';
} catch (\Exception $e) {
Logger::log(LoggerTypes::DATABASE, LoggerLevels::ERROR, '(App/Firewall/Firewall.php) Failed to assign ownership to ip: ' . $e->__toString());

return 'DATABASE_ERROR';
}
}
}
9 changes: 9 additions & 0 deletions app/Firewall/Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace MythicalSystemsFramework\Firewall;

interface Types extends Events
{
public const IP = 'ip';
public const USER_AGENT = 'user_agent';
}
20 changes: 20 additions & 0 deletions app/Kernel/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,24 @@ public static function throw_error($renderer, $error_text, $error_full_message,
exit($e->getMessage());
}
}

/**
* Show all errors to a page!
*/
public static function ShowAllErrors(): void
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}

/**
* Hide all errors from a page!
*/
public static function HideAllErrors(): void
{
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);
}
}
20 changes: 20 additions & 0 deletions app/Mail/MailService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MythicalSystemsFramework\Mail;

use MythicalSystemsFramework\Managers\Settings as setting;

class MailService
{
/**
* Is the mail server enabled?
*/
public static function isEnabled(): bool
{
if (setting::getSetting('mail', 'enabled') == 'true') {
return true;
} else {
return false;
}
}
}
15 changes: 15 additions & 0 deletions app/Managers/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ public static function updateSetting(string $category, string $name, string $val
}
}

public static function setSetting(string $category, string $name, string $value, bool $updateCache = true): void
{
DBSettingsManager::set($category, $name, $value);
if ($updateCache) {
MySQLCache::saveCache('framework_settings');
}
}

/**
* Migrate the settings table :).
*
* @param bool $isTerminal Is this a terminal?
*
* @throws NoMigrationsFound
*/
public static function migrate(bool $isTerminal = false): void
{
try {
Expand Down
Loading

0 comments on commit 88592e1

Please sign in to comment.