initial commit

This commit is contained in:
Matthieu Bessat 2021-08-08 19:23:49 +02:00
commit b65642cd0a
18 changed files with 1543 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
tmp

25
config.example.php Normal file
View file

@ -0,0 +1,25 @@
<?php
return [
'domain' => 'lefuturiste.fr',
'modules' => [
'random' => [],
'date' => [
'alias' => ['printdate']
],
'httpbin' => [],
'printip' => [
'alias' => ['ip', 'getip']
],
'quicknote' => [
'auth' => true
],
'quickupload' => [
'auth' => true
],
'logator' => []
],
'auth' => [
'root' => 'password'
]
];

27
config.php Normal file
View file

@ -0,0 +1,27 @@
<?php
return [
'domain' => 'lefuturiste.fr',
'modules' => [
'random' => [],
'date' => [
'alias' => ['printdate']
],
'httpbin' => [],
'printip' => [
'alias' => ['ip', 'getip']
],
'quicknote' => [
'auth' => true
],
'quickupload' => [
'auth' => true
],
'logator' => [
'alias' => ['logme']
]
],
'auth' => [
'root' => 'password'
]
];

34
configure_nginx.php Normal file
View file

@ -0,0 +1,34 @@
<?php
echo "> This script will generate a folder in the current directory where you can find every config file to use to make this thing works with nginx \n";
echo "\n";
$appBasePath = __DIR__;
$path = $appBasePath . '/tmp/generated_nginx_conf';
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
require('./load_config.php');
$template = file_get_contents('./nginx.conf.template');
$phpSocket = '/var/run/php/php8.0-fpm.sock';
foreach ($config['modules'] as $moduleName => $moduleConfig) {
$domains = array_merge([$moduleName], $moduleConfig['alias'] ?? []);
$domains = array_map(fn ($d) => $d . '.' . $config['domain'], $domains);
$serverName = implode(' ', $domains);
$rootPath = $appBasePath . '/' . $domains[0];
$nginxConfig = $template;
$nginxConfig = str_replace('{{ROOT_PATH}}', $rootPath, $nginxConfig);
$nginxConfig = str_replace('{{SERVER_NAME}}', $serverName, $nginxConfig);
$nginxConfig = str_replace('{{PHP_SOCKET}}', $phpSocket, $nginxConfig);
file_put_contents($path . '/' . $domains[0], $nginxConfig);
echo "> Wrote " . strlen($nginxConfig) . " bytes in " . $path . '/' . $domains[0] . "\n";
}
echo "\n";
echo "> Done.\n";

71
index.php Normal file
View file

@ -0,0 +1,71 @@
<?php
require('utils.php');
require('load_config.php');
function generateHelp($selectedModule) {
if (strpos($_SERVER['REQUEST_URI'], '/help') === false) {
return;
}
$additionalHelpPath = './modules/' . $selectedModule . '/' . $selectedModule . '_help.php';
if (!file_exists($additionalHelpPath)) {
return;
}
$helpConfig = require($additionalHelpPath);
echo "<h1>Help for module \"$selectedModule\"</h1>";
echo " <ul>";
foreach ($helpConfig['actions'] as $action) {
?>
<li><?= $action['name'] ?> <a href="<?= $action['path'] ?>"><?= $action['path'] ?></a></li>
<?php
}
echo "\n </ul> \n <br>";
echo $helpConfig['append']() ?? '';
exit();
}
$modules = $config['modules'];
$selectedModule = null;
foreach ($modules as $key => $module) {
if (strpos($_SERVER['HTTP_HOST'], $key . '.') === 0) {
$selectedModule = $module;
$selectedModule['key'] = $key;
break;
}
}
if ($selectedModule === null) {
http_response_code(404);
echo 'unknown module';
}
generateHelp($selectedModule['key']);
if (isset($selectedModule['auth']) && $selectedModule['auth']) {
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
{
header("WWW-Authenticate: Basic realm=\"You need to provide user and passwd\"");
http_response_code(401);
echo "Authentification Required";
exit();
}
$providedUsername = $_SERVER['PHP_AUTH_USER'];
$providedPassword = $_SERVER['PHP_AUTH_PW'];
$found = false;
foreach ($config['auth'] as $username => $password) {
if ($username === $providedUsername && $password == $providedPassword) {
$found = true;
}
}
if (!$found) {
header("WWW-Authenticate: Basic realm=\"Invalid auth\"");
http_response_code(401);
echo "Invalid authentification, try again";
exit();
}
}
require('./modules/' . $selectedModule['key'] . '/' . $selectedModule['key'] . '.php');

14
load_config.php Normal file
View file

@ -0,0 +1,14 @@
<?php
$config = require('./config.php');
// a basic validation of config keys
if (!(isset($config['domain']) && strlen($config['domain']) > 0)) {
die('Domain key not present in config');
}
if (!(isset($config['modules']) && count($config['modules']) > 0)) {
die('Modules key not present in config or no modules configured');
}
if (!(isset($config['auth']) && count($config['auth']) > 0)) {
die('Auth key not present in config or no auth configured');
}

10
modules/date/date.php Normal file
View file

@ -0,0 +1,10 @@
<?php
$format = $_GET['format'] ?? 'full';
date_default_timezone_set($_GET['timezone'] ?? 'UTC');
echo date(match ($format) {
'full' => 'Y-m-d H:i:s',
'unix' => 'U'
});

1073
modules/date/date_help.php Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,9 @@
<?php
$data = resumeRequest();
header("Content-Type: application/json");
$str = json_encode($data);
echo $str;

112
modules/logator/logator.php Normal file
View file

@ -0,0 +1,112 @@
<?php
function getFilesAsArray($path) {
$dirs = array_filter(glob($path . '/*'), 'is_file');
$dirs = array_map(function ($dirName) {
$compo = explode('/', $dirName);
return $compo[count($compo)-1];
}, $dirs);
return array_values($dirs);
}
function getDirsAsArray($path) {
$dirs = array_filter(glob($path . '/*'), 'is_dir');
$dirs = array_map(function ($dirName) {
$compo = explode('/', $dirName);
return $compo[count($compo)-1];
}, $dirs);
return array_values($dirs);
}
header('Content-Type: application/json');
$logAt = __DIR__ . '/../../tmp/requests';
if (!file_exists($logAt)) {
mkdir($logAt, 0770, true);
}
$logAt = realpath($logAt);
if ($_SERVER['REQUEST_URI'] === '/') {
echo json_encode([
'success' => true,
'message' => 'Welcome to the logator app!'
]);
exit();
}
if ($_SERVER['REQUEST_URI'] === '/loggers') {
echo json_encode([
'success' => true,
'data' => getDirsAsArray($logAt)
]);
exit();
}
if (strpos($_SERVER['REQUEST_URI'], '/logger-details') === 0) {
if (!isset($_GET['id']) || $_GET['id'] === '') {
echo json_encode([
'success' => false,
'message' => 'Bro, I need an ID bro. Why are you doing that to me?'
]);
}
$id = preg_replace('/[^0-9a-zA-Z]/', '', $_GET['id']);
$path = $logAt . '/' . $id;
if (!file_exists($path)) {
http_response_code(404);
echo json_encode([
'success' => false,
'message' => 'Bruh id did not find'
]);
exit();
}
$data = [];
foreach (getFilesAsArray($path) as $file) {
$data[] = json_decode(file_get_contents($path . '/' . $file), true);
}
echo json_encode([
'success' => true,
'data' => $data
]);
exit();
}
if ($_SERVER['REQUEST_URI'] === '/create') {
$id = generateRandomString(6, ['upper', 'digit']);
mkdir($logAt . '/' . $id, 0770);
echo json_encode([
'success' => true,
'message' => 'Created a logger with id: ' . $id,
'id' => $id
]);
exit();
}
$id = parse_url($_SERVER['REQUEST_URI'])['path'];
$id = preg_replace('/[^0-9a-zA-Z]/', '', $id);
if (!file_exists($logAt . '/' . $id)) {
http_response_code(404);
echo json_encode([
'success' => false,
'message' => "This logger doesn't exist, this incident will be reported (of course not, your dumbass who will think that something so uninteresting as that is worth to log in a file that no one will read anyways. You fools.)"
]);
exit();
}
$resume = resumeRequest();
$requestId = substr(hash('sha256', json_encode($resume)), 0, 16);
$resume['id'] = $requestId;
$path = $logAt . '/' . $id . '/' . $requestId . '.json';
file_put_contents($path, json_encode($resume));
echo json_encode(['success' => true, 'requestId' => $requestId]);

View file

@ -0,0 +1,18 @@
<?php
return [
'actions' => [
[
'name' => 'List all loggers',
'path' => '/loggers'
],
[
'name' => 'Get details about a logger',
'path' => '/logger-details?id=YOURID'
],
[
'name' => 'Execute a logger',
'path' => '/YOURID'
]
]
];

View file

@ -0,0 +1,3 @@
<?php
echo $_SERVER['REMOTE_ADDRESS'];

View file

@ -0,0 +1,31 @@
<?php
$path = __DIR__ . '/../../tmp/quicknotes';
if (!file_exists($path)) {
mkdir($path);
}
$path = realpath($path);
$message = '';
if (isset($_GET['m'])) {
$message = $_GET['m'];
}
if (isset($_GET['message'])) {
$message = $_GET['message'];
}
if (strlen($message) === 0) {
exit();
}
$data = '[' . date('Y-m-d H:i:s') . ']';
$data .= ' - ' . $message;
$data .= "\n";
file_put_contents($path . '/logs.txt', $data, FILE_APPEND);
echo 'OK';

View file

@ -0,0 +1,29 @@
<?php
$path = __DIR__ . '/../../tmp/quickupload';
if (!file_exists($path)) {
mkdir($path);
}
$path = realpath($path);
function slugify($string){
return strtolower(trim(preg_replace('/[^.A-Za-z0-9-]+/', '-', $string), '-'));
}
if (isset($_FILES['file'])) {
$f = $_FILES['file'];
copy($f['tmp_name'], $path . '/' . uniqid() . '_' . slugify($f['name']));
?>
<div style="padding: 20px; border: 1px solid green; background: green; color: white">
File uploaded!
</div>
<?php
}
?>
<h3>QuickUpload</h3>
<form enctype="multipart/form-data" method="POST">
<input type="hidden" name="name" value="wow" />
<input type="file" name="file" />
<input type="submit" value="Submit">
</form>

View file

@ -0,0 +1,9 @@
<?php
$len = $_GET['length'] ?? 32;
$contains = isset($_GET['contains']) ? explode(',', $_GET['contains']) : ['digit', 'upper', 'lower', 'special'];
header('Content-Type: text/plain');
echo generateRandomString($len, $contains);

View file

@ -0,0 +1,23 @@
<?php
return [
'actions' => [
[
'name' => 'Generate alphanumerics',
'path' => '/?contains=digit,lower,upper'
],
[
'name' => 'More complex',
'path' => '/?contains=digit,lower,upper,special'
],
[
'name' => 'Change length',
'path' => '/?length=20'
]
],
'append' => function () {
?>
Of course, you can combine many parameters into one query.
<?php
}
];

15
nginx.conf.template Normal file
View file

@ -0,0 +1,15 @@
server {
index index.html index.php;
root {{ROOT_PATH}};
server_name {{SERVER_NAME}};
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:{{PHP_SOCKET}};
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}

38
utils.php Normal file
View file

@ -0,0 +1,38 @@
<?php
function generateRandomString($length = 10, $contains = ['upper', 'lower', 'digit']) {
$charset = [
'digit' => '0123456789',
'lower' => 'abcdefghijklmnopqrstuvwxyz',
'upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'special' => '!#$%&()*+,-.:;<=>?@[]'
];
$characters = '';
foreach ($contains as $c) {
$characters .= $charset[$c] ?? '';
}
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function resumeRequest() {
return [
'protocol'=> $_SERVER['SERVER_PROTOCOL'],
'method' => $_SERVER['REQUEST_METHOD'],
'query' => $_GET,
'post' => $_POST,
'body' => file_get_contents('php://input'),
'headers' => getallheaders(),
'query_string' => $_SERVER['QUERY_STRING'] ?? '',
'request_uri' => $_SERVER['REQUEST_URI'],
'remote_address' => $_SERVER['REMOTE_ADDR'],
'remote_port' => (int) $_SERVER['REMOTE_PORT'],
'at' => date('Y-m-d H:i:s'),
'at_unix' => (int) date('U'),
'at_unix_micro' => microtime()
];
}