Routing test 1

This commit is contained in:
Roman Hergenreder 2020-06-19 14:12:07 +02:00
parent 077fe68914
commit 78c5934480
10 changed files with 136 additions and 23 deletions

@ -2,8 +2,15 @@ php_flag display_errors on
Options -Indexes
RedirectMatch 404 /\.git
RedirectMatch 404 /src
RedirectMatch 404 /test
RewriteEngine On
RewriteRule ^api/(.*)?$ index.php?api=$1&$2 [L,QSA]
RewriteRule ^admin(/(.*)?)?$ index.php?site=admin&$1 [L,QSA]
RewriteRule ^((?!((js|css|img|fonts|api|docs)($|/)))(.*)?)$ index.php?site=$1&$2 [L,QSA]
RewriteRule ^api(/.*)?$ /index.php?api=$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?site=$1 [L,QSA]
</IfModule>

@ -7,8 +7,6 @@ use \Driver\SQL\Condition\Compare;
class Fetch extends Request {
private array $notifications;
public function __construct($user, $externalCall = false) {
parent::__construct($user, $externalCall, array());
$this->loginRequired = true;

@ -0,0 +1,61 @@
<?php
namespace Api\Routes;
use Api\Parameter\StringType;
use \Api\Request;
use Driver\SQL\Column\Column;
use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\Regex;
class Find extends Request {
public function __construct($user, $externalCall = false) {
parent::__construct($user, $externalCall, array(
'request' => new StringType('request', 128, true, '/')
));
$this->isPublic = false;
}
public function execute($values = array()) {
if(!parent::execute($values)) {
return false;
}
$request = $this->getParam('request');
if (!startsWith($request, '/')) {
$request = "/$request";
}
$sql = $this->user->getSQL();
$res = $sql
->select("uid", "request", "action", "target", "extra")
->from("Route")
->where(new CondBool("active"))
->where(new Regex("^$request$", new Column("request")))
->limit(1)
->execute();
$this->lastError = $sql->getLastError();
$this->success = ($res !== FALSE);
if ($this->success) {
if (!empty($res)) {
$row = $res[0];
$this->result["route"] = array(
"uid" => intval($row["uid"]),
"request" => $row["request"],
"action" => $row["action"],
"target" => $row["target"],
"extra" => $row["extra"]
);
} else {
$this->result["route"] = NULL;
}
}
return $this->success;
}
}

@ -129,7 +129,8 @@ class CreateDatabase {
->primaryKey("uid");
$queries[] = $sql->insert("Route", array("request", "action", "target"))
->addRow("/admin(/.*)?", "dynamic", "\\Core\\Documents\\AdminDashboard");
->addRow("/admin(/.*)?", "dynamic", "\\Core\\Documents\\AdminDashboard")
->addRow("/api/(.*)", "dynamic", "\\Core\\Api\\$1");
return $queries;
}

@ -0,0 +1,17 @@
<?php
namespace Driver\SQL\Condition;
class Regex extends Condition {
private $leftExpression;
private $rightExpression;
public function __construct($leftExpression, $rightExpression) {
$this->leftExpression = $leftExpression;
$this->rightExpression = $rightExpression;
}
public function getLeftExp() { return $this->leftExpression; }
public function getRightExp() { return $this->rightExpression; }
}

@ -6,6 +6,7 @@ use Driver\SQL\Column\Column;
use Driver\SQL\Condition\Compare;
use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\CondOr;
use Driver\SQL\Condition\Regex;
use Driver\SQL\Constraint\Constraint;
use \Driver\SQL\Constraint\Unique;
use \Driver\SQL\Constraint\PrimaryKey;
@ -326,11 +327,17 @@ abstract class SQL {
return $this->buildCondition($condition[0], $params);
} else {
$conditions = array();
foreach($condition as $cond) {
foreach ($condition as $cond) {
$conditions[] = $this->buildCondition($cond, $params);
}
return implode(" AND ", $conditions);
}
} else if ($condition instanceof Regex) {
$left = $condition->getLeftExp();
$right = $condition->getRightExp();
$left = ($left instanceof Column) ? $this->columnName($left->getName()) : $this->addValue($left, $params);
$right = ($right instanceof Column) ? $this->columnName($right->getName()) : $this->addValue($right, $params);
return $left . " REGEXP " . $right;
} else {
$this->lastError = "Unsupported condition type: " . get_class($condition);
return false;

@ -85,6 +85,7 @@
function getClassPath($class, $suffix=true) {
$path = str_replace('\\', '/', $class);
if (startsWith($path, "/")) $path = substr($path, 1);
$suffix = ($suffix ? ".class" : "");
return "core/$path$suffix.php";
}

@ -60,7 +60,7 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) {
}
}
} else {
$documentName = $_GET["site"];
$documentName = $_GET["site"] ?? "/";
if ($installation) {
if ($documentName !== "" && $documentName !== "index.php") {
$response = "Redirecting to <a href=\"/\">/</a>";
@ -70,24 +70,47 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) {
$response = $document->getCode();
}
} else {
if(empty($documentName) || strcasecmp($documentName, "install") === 0) {
$documentName = "home";
} else if(!preg_match("/[a-zA-Z]+(\/[a-zA-Z]+)*/", $documentName)) {
$documentName = "Document404";
}
$documentName = strtoupper($documentName[0]) . substr($documentName, 1);
$documentName = str_replace("/", "\\", $documentName);
$class = "\\Documents\\$documentName";
$file = getClassPath($class);
if(!file_exists($file) || !is_subclass_of($class, Document::class)) {
$document = new Document404($user);
$req = new \Api\Routes\Find($user);
$success = $req->execute(array("request" => $documentName));
$response = "";
if (!$success) {
$response = "Unable to find route: " . $req->getLastError();
} else {
$document = new $class($user);
$route = $req->getResult()["route"];
if (is_null($route)) {
$response = (new Document404($user))->getCode();
} else {
$target = trim(explode("\n", $route["target"])[0]);
switch ($route["action"]) {
case "redirect_temporary":
http_send_status(307);
header("Location: $target");
break;
case "redirect_permanently":
http_send_status(308);
header("Location: $target");
break;
case "static":
http_send_status(501);
$response = "Not implemented yet.";
break;
case "dynamic":
$view = $route["extra"] ?? "";
$file = getClassPath($target);
if(!file_exists($file) || !is_subclass_of($target, Document::class)) {
$document = new Document404($user);
} else {
$document = new $target($user);
}
$response = $document->getCode();
break;
}
}
}
$user->processVisit();
$response = $document->getCode();
}
}

@ -1 +0,0 @@
DENY FROM ALL;

@ -1 +0,0 @@
DENY FROM ALL