Skip to content

Commit 8cd2d5d

Browse files
authored
upload
1 parent 2853b28 commit 8cd2d5d

File tree

15 files changed

+1237
-0
lines changed

15 files changed

+1237
-0
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
APP_NAME="API-Monster"

.htaccess

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
RewriteEngine On
2+
3+
# If the requested URL is a directory
4+
RewriteCond %{REQUEST_FILENAME} -d
5+
6+
# And if the directory does not contain an index file
7+
RewriteCond %{REQUEST_FILENAME}/index.php !-f
8+
RewriteCond %{REQUEST_FILENAME}/index.html !-f
9+
RewriteCond %{REQUEST_FILENAME}/index.htm !-f
10+
RewriteCond %{REQUEST_FILENAME}/index.shtml !-f
11+
RewriteCond %{REQUEST_FILENAME}/index.cgi !-f
12+
13+
# Then return a 403 Forbidden error
14+
RewriteRule ^ - [F]
15+
16+
# If the requested URL is not a file
17+
RewriteCond %{REQUEST_FILENAME} !-f
18+
19+
# Then rewrite the URL to index.php
20+
RewriteRule ^(.*)$ index.php [QSA,L]

App/Controllers/AppController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Monster\App\Controllers;
4+
5+
class AppController
6+
{
7+
public function index()
8+
{
9+
echo "Welcome to API-Monster";
10+
}
11+
}

App/Core.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace Monster\App;
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| API-Monster Framework
8+
|--------------------------------------------------------------------------
9+
|
10+
| API-Monster is a fast, safe, and easy-to-use PHP framework designed for
11+
| building API applications. It provides a wide range of features and
12+
| components to streamline the development process and enhance
13+
| productivity.
14+
|
15+
| Features:
16+
| - Fast: API-Monster is optimized for performance, allowing you to build
17+
| high-performance API applications.
18+
| - Safe: The framework prioritizes security and provides built-in mechanisms
19+
| for handling common security concerns.
20+
| - Easy: API-Monster follows a user-friendly approach, making it easy for
21+
| developers to understand and work with the framework.
22+
|
23+
| Key Components:
24+
| - Routing: API-Monster supports routing similar to Laravel, allowing you to
25+
| define routes and map them to corresponding controller actions.
26+
| - MySQL Class: The framework includes a MySQL class for easy interaction
27+
| with MySQL databases.
28+
| - HTTP Class: API-Monster provides an HTTP class for handling HTTP requests
29+
| and responses, simplifying the communication with external APIs.
30+
| - Cipher Class: The Cipher class offers encoding and decoding functionality,
31+
| allowing you to securely handle sensitive data.
32+
| - Controllers: API-Monster supports controllers, enabling you to organize
33+
| your application's logic into modular and reusable components.
34+
| - Object-Oriented Syntax: The framework utilizes object-oriented programming
35+
| (OOP) syntax, promoting clean and maintainable code.
36+
|
37+
| Getting Started:
38+
| To create a new API-Monster project, you can use Composer by running the
39+
| following command:
40+
| composer create-project darkphp/apimonster myapp
41+
|
42+
| GitHub Repository:
43+
| For more information and to explore the framework's source code, you can
44+
| visit the API-Monster GitHub repository at:
45+
| https://github.com/ReactMVC/API-Monster
46+
|
47+
| Developer Information:
48+
| API-Monster is developed by Hossein Pira. If you have any questions,
49+
| suggestions, or feedback, you can reach out to Hossein via email at:
50+
51+
52+
| Alternatively, you can contact Hossein on Telegram at @h3dev.
53+
|
54+
*/
55+
56+
class Core
57+
{
58+
// The HTTP method associated with this route item
59+
private $method;
60+
61+
// The path associated with this route item
62+
private $path;
63+
64+
// The controller method to execute when this route item is matched
65+
private $controller;
66+
67+
// The parameters extracted from the request URI when this route item is matched
68+
private $params;
69+
70+
// The regular expression used to match the request URI against the path of this route item
71+
private $pathRegex;
72+
73+
public function __construct($method, $path, $controller)
74+
{
75+
$this->method = $method;
76+
$this->path = $path;
77+
$this->controller = $controller;
78+
79+
// Cache the regular expression created by preg_replace
80+
if (!isset($this->pathRegex[$path])) {
81+
$path_regex = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '([a-zA-Z0-9_]+)', $path);
82+
$path_regex = str_replace('/', '\/', $path_regex);
83+
$path_regex .= '(\\?.*)?$'; // Include an optional query parameter at the end of the path
84+
$this->pathRegex[$path] = '/^' . $path_regex . '$/';
85+
}
86+
}
87+
88+
// Getters and setters for the private properties
89+
90+
public function getMethod()
91+
{
92+
return $this->method;
93+
}
94+
95+
public function setMethod($method)
96+
{
97+
$this->method = $method;
98+
}
99+
100+
public function getPath()
101+
{
102+
return $this->path;
103+
}
104+
105+
public function setPath($path)
106+
{
107+
$this->path = $path;
108+
}
109+
110+
public function getController()
111+
{
112+
return $this->controller;
113+
}
114+
115+
public function setController($controller)
116+
{
117+
$this->controller = $controller;
118+
}
119+
120+
public function getParams()
121+
{
122+
return $this->params;
123+
}
124+
125+
public function setParams($params)
126+
{
127+
$this->params = $params;
128+
}
129+
130+
// Match the route item against the request method and URI
131+
132+
public function match($request_method, $request_uri)
133+
{
134+
$request_uri = rtrim($request_uri, '/');
135+
if ($this->method === $request_method && preg_match($this->pathRegex[$this->path], $request_uri, $matches)) {
136+
$this->params = array_slice($matches, 1);
137+
return true;
138+
}
139+
return false;
140+
}
141+
142+
// Execute the controller method associated with this route item
143+
144+
public function execute()
145+
{
146+
list($controller, $method) = explode('@', $this->controller);
147+
$controllerClass = "Monster\\App\\Controllers\\" . $controller;
148+
149+
// Cache the value of class_exists and call_user_func_array
150+
static $classExists = [];
151+
static $callUserFuncArray = [];
152+
153+
if (!isset($classExists[$controllerClass])) {
154+
$classExists[$controllerClass] = class_exists($controllerClass);
155+
}
156+
157+
if ($classExists[$controllerClass]) {
158+
if (!isset($callUserFuncArray[$controllerClass])) {
159+
$callUserFuncArray[$controllerClass] = function ($controllerInstance, $method, $params) {
160+
if ($params) {
161+
$controllerInstance->$method(...$params);
162+
} else {
163+
$controllerInstance->$method();
164+
}
165+
};
166+
}
167+
$controllerInstance = new $controllerClass;
168+
$callUserFuncArray[$controllerClass]($controllerInstance, $method, $this->params);
169+
} else {
170+
http_response_code(500);
171+
echo "Internal Server Error: Controller class not found";
172+
}
173+
}
174+
}

App/Models/Cipher.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class Cipher {
2+
constructor(key) {
3+
this.key = key;
4+
}
5+
6+
encrypt(message) {
7+
// Custom encryption algorithm
8+
let encrypted = this.rot13(message); // Example: using ROT13 substitution
9+
10+
// Additional encryption steps using the key
11+
encrypted = this.xorEncrypt(encrypted);
12+
13+
return encrypted;
14+
}
15+
16+
decrypt(encryptedMessage) {
17+
// Reverse the additional encryption steps using the key
18+
let decrypted = this.xorDecrypt(encryptedMessage);
19+
20+
// Custom decryption algorithm
21+
decrypted = this.rot13(decrypted); // Example: reversing ROT13 substitution
22+
23+
return decrypted;
24+
}
25+
26+
xorEncrypt(message) {
27+
const key = this.key;
28+
const keyLength = key.length;
29+
const messageLength = message.length;
30+
let encrypted = '';
31+
32+
for (let i = 0; i < messageLength; i++) {
33+
encrypted += String.fromCharCode(message.charCodeAt(i) ^ key.charCodeAt(i % keyLength));
34+
}
35+
36+
return btoa(encrypted);
37+
}
38+
39+
xorDecrypt(encryptedMessage) {
40+
const key = this.key;
41+
const keyLength = key.length;
42+
const messageLength = atob(encryptedMessage).length;
43+
let decrypted = '';
44+
45+
for (let i = 0; i < messageLength; i++) {
46+
decrypted += String.fromCharCode(atob(encryptedMessage).charCodeAt(i) ^ key.charCodeAt(i % keyLength));
47+
}
48+
49+
return decrypted;
50+
}
51+
52+
rot13(message) {
53+
return message.replace(/[a-zA-Z]/g, function (c) {
54+
return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
55+
});
56+
}
57+
}
58+
59+
// Usage example:
60+
/*
61+
const key = "your_secret_key";
62+
const cipher = new Cipher(key);
63+
const message = "Hello, World!";
64+
const encryptedMessage = cipher.encrypt(message);
65+
const decryptedMessage = cipher.decrypt(encryptedMessage);
66+
67+
console.log("Original message: " + message);
68+
console.log("Encrypted message: " + encryptedMessage);
69+
console.log("Decrypted message: " + decryptedMessage);
70+
*/

App/Models/Cipher.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Monster\App\Models;
4+
5+
class Cipher
6+
{
7+
private $key;
8+
9+
public function __construct($key)
10+
{
11+
$this->key = $key;
12+
}
13+
14+
public function encrypt($message)
15+
{
16+
// Custom encryption algorithm
17+
$encrypted = str_rot13($message); // Example: using ROT13 substitution
18+
19+
// Additional encryption steps using the key
20+
$encrypted = $this->xorEncrypt($encrypted);
21+
22+
return $encrypted;
23+
}
24+
25+
public function decrypt($encryptedMessage)
26+
{
27+
// Reverse the additional encryption steps using the key
28+
$decrypted = $this->xorDecrypt($encryptedMessage);
29+
30+
// Custom decryption algorithm
31+
$decrypted = str_rot13($decrypted); // Example: reversing ROT13 substitution
32+
33+
return $decrypted;
34+
}
35+
36+
private function xorEncrypt($message)
37+
{
38+
$key = $this->key;
39+
$keyLength = strlen($key);
40+
$messageLength = strlen($message);
41+
$encrypted = '';
42+
43+
for ($i = 0; $i < $messageLength; $i++) {
44+
$encrypted .= $message[$i] ^ $key[$i % $keyLength];
45+
}
46+
47+
return base64_encode($encrypted);
48+
}
49+
50+
private function xorDecrypt($encryptedMessage)
51+
{
52+
$key = $this->key;
53+
$keyLength = strlen($key);
54+
$encryptedMessage = base64_decode($encryptedMessage);
55+
$messageLength = strlen($encryptedMessage);
56+
$decrypted = '';
57+
58+
for ($i = 0; $i < $messageLength; $i++) {
59+
$decrypted .= $encryptedMessage[$i] ^ $key[$i % $keyLength];
60+
}
61+
62+
return $decrypted;
63+
}
64+
}
65+
66+
67+
// Usage example:
68+
/*
69+
$key = "your_secret_key";
70+
$cipher = new Cipher($key);
71+
$message = "Hello, World!";
72+
$encryptedMessage = $cipher->encrypt($message);
73+
$decryptedMessage = $cipher->decrypt($encryptedMessage);
74+
75+
echo "Original message: " . $message . "\n";
76+
echo "Encrypted message: " . $encryptedMessage . "\n";
77+
echo "Decrypted message: " . $decryptedMessage . "\n";
78+
*/

0 commit comments

Comments
 (0)