Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Dispatch path and cookie patch
  • Loading branch information
Wazabii committed Mar 9, 2024
1 parent 5657f4d commit e42ea3a
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 24 deletions.
18 changes: 12 additions & 6 deletions Auth/Middleware/SessionStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

class SessionStart implements MiddlewareInterface
{
//private $url;
const NAME = NULL; // Set a custom seesion name
const TIME = 360;
const SSL = true;
const SAMESITE = true;

private $container;
//private $json;

public function __construct(RequestInterface $request, ContainerInterface $container)
{
Expand All @@ -28,13 +31,16 @@ public function __construct(RequestInterface $request, ContainerInterface $conta
*/
public function before(ResponseInterface $response, RequestInterface $request)
{
$time = (getenv("SESSION_TIME") !== false) ? (int)getenv("SESSION_TIME") : static::TIME;
$SSL = (getenv("SESSION_SSL") !== false) ? ((int)getenv("SESSION_SSL") === 1) : static::SSL;

$session = new Session(
"maple",
(int)getenv("SESSION_TIME"),
static::NAME,
$time,
"/",
$request->getUri()->getHost(),
((int)getenv("SESSION_SSL") === 1),
true
$SSL,
static::SAMESITE
);
$this->container->set("session", $session);
$this->container->get("session")->start();
Expand Down
68 changes: 68 additions & 0 deletions Cli/Connectors/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace MaplePHP\Foundation\Cli\Connectors;

use MaplePHP\Foundation\Cli\Connectors\CliInterface;
use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\RequestInterface;
use MaplePHP\Container\Interfaces\ContainerInterface;
use MaplePHP\Http\Env;
use MaplePHP\Container\Reflection;
use MaplePHP\Foundation\Cli\StandardInput;
use MaplePHP\Foundation\Http\Dir;

class Server implements CliInterface
{
protected $container;
protected $args;
protected $dir;
protected $cli;
protected $port = 8000;

public function __construct(ContainerInterface $container, RequestInterface $request, Dir $dir, StandardInput $cli, Env $env)
{
$this->container = $container;
$this->args = $request->getCliArgs();
$this->dir = $dir;
$this->cli = $cli;
}


public function start()
{
die("COMING SOON");

//putenv("")
$localIPs = $this->cli->getLocalIPAddress();
$this->cli->write("MaplePHP Server has started, vist bellow to display you app.");
$this->cli->write("Vist: http://{$localIPs}:8080");
$cmd = (sprintf('php -S %s:%d -t %s', $localIPs, 8080, escapeshellarg($this->dir->getPublic())));
//$command = "nohup $cmd > /dev/null 2>&1 & echo $! > server.pid";
//echo $cmd;
//die;
$out = shell_exec('php -S 10.0.1.220:8080 -t /var/www/html/systems/_phpfuse/');

return $this->cli->getResponse();
}

public function stop()
{
/*
$pid = file_get_contents('server.pid');
shell_exec("kill $pid");
$this->cli->write("The server has been stopped!");
*/
return $this->cli->getResponse();
}

public function help()
{
$this->cli->write('$ config [type] [--values, --values, ...]');
$this->cli->write('Type: install, read, create, drop or help');
$this->cli->write('Values: --key=%s, --value=%s --strict');
$this->cli->write('--key: The env config key (type: create, drop)');
$this->cli->write('--value: The env config value (type: create)');
$this->cli->write('--strict: Will also show hidden configs, (type: read)');
return $this->cli->getResponse();
}
}
9 changes: 9 additions & 0 deletions Cli/Routers/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
$routes->cli("/drop", ['MaplePHP\Foundation\Cli\Connectors\Config', "drop"]);
});

$routes->group("server", function ($routes) {
// It is recommended to add this 2 handles at the begining of every grouped call
$routes->map("*", '[/{any:.*}]', ['MaplePHP\Foundation\Cli\Connectors\Cli', "handleMissingType"]);
$routes->cli("[/help]", ['MaplePHP\Foundation\Cli\Connectors\Server', "help"]);

$routes->cli("/start", ['MaplePHP\Foundation\Cli\Connectors\Server', "start"]);
});



$routes->group("image", function ($routes) {
// It is recommended to add this 2 handles at the begining of every grouped call
Expand Down
27 changes: 27 additions & 0 deletions Cli/StandardInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MaplePHP\Http\Client;
use MaplePHP\Http\Request;
use MaplePHP\Http\UploadedFile;
use MaplePHP\DTO\Format\Str;
use MaplePHP\Validate\Inp;
use Exception;

Expand Down Expand Up @@ -321,6 +322,32 @@ function lossyGetLocalIP(): string
return $serverIP;
}

function getLocalIPAddress(): string|false
{
$result = false;
$os = strtolower(PHP_OS);

if(strpos($os, 'win') !== false) {
$ipconfig = shell_exec('ipconfig');
preg_match('/IPv4 Address[. ]*: ([\d\.]+)/', $ipconfig, $matches);
$result = ($matches[1] ?? false);

} elseif(strpos($os, 'linux') !== false) {
$result = shell_exec('hostname -I | cut -d\' \' -f1');

} elseif(strpos($os, 'darwin') !== false) {
$result = shell_exec("ifconfig en0 | grep inet | grep -v inet6 | awk '{print $2}'");
}

if(is_string($result) && !empty($result)) {
$format = new Str($result);
return $format->clearBreaks()->get();
}

return false;
}


/**
* Get json data
*/
Expand Down
10 changes: 7 additions & 3 deletions Http/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class Cookie
{

const SAMESITE = "Strict";
const HTTPONLY = true;

private $url;
private $cookies;

Expand All @@ -19,10 +23,10 @@ class Cookie
public function __construct(UrlInterface $url)
{
$this->url = $url;
//path, domain, secure, httponly
$this->cookies = new Cookies("/", $this->url->getUri()->getHost(), true, true);
$scheme = $this->url->getUri()->getScheme();
$this->cookies = new Cookies("/", $this->url->getUri()->getHost(), ($scheme === "https"), static::HTTPONLY);
// Only read modify on same site
$this->cookies->setSameSite("Strict");
$this->cookies->setSameSite(static::SAMESITE);
}

public function inst()
Expand Down
5 changes: 3 additions & 2 deletions Kernel/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,12 @@ public function run(): void
$this->setupDispatch(function ($response) {
return $this->setupHeaders($response);
});

$response = $this->dispatcher->response();
$request = $this->dispatcher->request();

if ((int)getenv("APP_SSL") === 1 && !$request->isSSL()) {
// Will force https address.
if ((int)getenv("APP_FORCE_SSL") === 1 && !$request->isSSL()) {
$location = $request->getUri()->withScheme("https")->withQuery("")->withPort(null)->getUri();
$response->withStatus(301)->location($location);
}
Expand Down
30 changes: 17 additions & 13 deletions Kernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ class Kernel

public function __construct(string $dir)
{

$this->dir = $dir;

$this->stream = new Http\Stream(Http\Stream::TEMP);
$this->response = new Http\Response($this->stream);
$this->env = new Http\Environment();
$this->request = new Http\ServerRequest(new Http\Uri($this->env->getUriParts([
"dir" => $this->dir
])), $this->env);

$this->init();
}

public function getRequest() {
return $this->request;
}

private function init(): void
Expand All @@ -40,27 +47,24 @@ private function init(): void
$this->app = new App($this->emitter, $this->routes);
}

public function run(): void
public function run(?string $path = null): void
{
$this->request = new Http\ServerRequest(new Http\Uri($this->env->getUriParts([
"dir" => $this->dir
])), $this->env);


$this->init();

$this->app->enablePrettyErrorHandler();
$this->app->setContainer($this->container);
$this->app->enableTemplateEngine(true);
$this->app->excludeRouterFiles(["cli"]);

//$this->emitter->errorHandler(false, false, true, "{$dir}storage/logs/error.log");
// bool $displayError, bool $niceError, bool $logError, string $logErrorFile
//$this->emitter->errorHandler(true, true, true, "{$dir}storage/logs/error.log");

// Set current URI path
$param = $this->request->getQueryParams();
$this->routes->setDispatchPath("/" . ($param['page'] ?? ""));
//$param = $this->request->getQueryParams();
//$path = ($param['page'] ?? "");
if(is_null($path)) {
$path = $this->request->getUri()->getPath();
}

$this->routes->setDispatchPath($path);
$this->app->run();

}
Expand Down

0 comments on commit e42ea3a

Please sign in to comment.