Browse Source

Add Regex to extra field

Roman Hergenreder 3 years ago
parent
commit
4f9a9e063a
3 changed files with 16 additions and 4 deletions
  1. 11 1
      core/core.php
  2. 4 2
      index.php
  3. 1 1
      js/script.js

+ 11 - 1
core/core.php

@@ -1,6 +1,6 @@
 <?php
 
-define("WEBBASE_VERSION", "1.0.1");
+define("WEBBASE_VERSION", "1.0.2");
 
 function getProtocol() {
   return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
@@ -157,3 +157,13 @@ function serveStatic(string $webRoot, string $file) {
 
   return "";
 }
+
+function parseClass($class) {
+  if (!startsWith($class, "\\")) {
+    $class = "\\$class";
+  }
+
+  $parts = explode("\\", $class);
+  $parts = array_map('ucfirst', $parts);
+  return implode("\\", $parts);
+}

+ 4 - 2
index.php

@@ -111,9 +111,10 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) {
         $response = (new Document404($user))->getCode();
       } else {
         $target = trim(explode("\n", $route["target"])[0]);
+        $extra = $route["extra"] ?? "";
 
         $pattern = str_replace("/","\\/", $route["request"]);
-        $pattern = "/$pattern/";
+        $pattern = "/$pattern/i";
         if (!startsWith($requestedUri, '/')) {
           $requestedUri = "/$requestedUri";
         }
@@ -122,6 +123,7 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) {
         if (is_array($match) && !empty($match)) {
           foreach($match as $index => $value) {
             $target = str_replace("$$index", $value, $target);
+            $extra  = str_replace("$$index", $value, $extra);
           }
         }
 
@@ -139,7 +141,7 @@ if(isset($_GET["api"]) && is_string($_GET["api"])) {
             $response = serveStatic($currentDir, $target);
             break;
           case "dynamic":
-            $view = $route["extra"] ?? "";
+            $view = parseClass($extra);
             $file = getClassPath($target);
             if(!file_exists($file) || !is_subclass_of($target, Document::class)) {
               $document = new Document404($user, $view);

+ 1 - 1
js/script.js

@@ -9,7 +9,7 @@ let Core = function () {
     params = typeof params !== 'undefined' ? params : {};
     callback = typeof callback !== 'undefined' ? callback : function (data) {};
 
-    const path = '/api/' + (func.startsWith('/') ? '' : '/') + func;
+    const path = '/api' + (func.startsWith('/') ? '' : '/') + func;
     $.post(path, params, function (data) {
       console.log(func + "(): success=" + data.success + " msg=" + data.msg);
       callback.call(this, data);