From b1c0848c2c0f98b264dbe28d01b1299103f7f05c Mon Sep 17 00:00:00 2001
From: Sibidharan Nandhakumar <hello@sibidharan.me>
Date: Mon, 10 May 2021 20:46:55 +0530
Subject: [PATCH] WIP: POST->GET bug

---
 .htaccess                |  2 ++
 api/apis/auth/signup.php | 34 +++++++++++++++++++++
 api/index.php            | 65 +++++++++++++++++++---------------------
 call.php                 |  3 +-
 4 files changed, 68 insertions(+), 36 deletions(-)
 create mode 100644 api/apis/auth/signup.php

diff --git a/.htaccess b/.htaccess
index 4e4725f..b4b1e6e 100644
--- a/.htaccess
+++ b/.htaccess
@@ -2,6 +2,8 @@ RewriteBase /
 RewriteEngine On
 
 RewriteRule ^/?api/([^/]+)?$ api/index.php?rquest=$1 [QSA,NC,L]
+RewriteRule ^/?api/([^/]+)/([^/]+)?$ api/index.php?rquest=$2&namespace=$1 [QSA,NC,L]
+
 
 ## For general files, if not above, just remove .php
 
diff --git a/api/apis/auth/signup.php b/api/apis/auth/signup.php
new file mode 100644
index 0000000..fbfe8f5
--- /dev/null
+++ b/api/apis/auth/signup.php
@@ -0,0 +1,34 @@
+<?php
+
+${basename(__FILE__, '.php')} = function(){
+    if($this->get_request_method() == "POST" and isset($this->_request['username']) and isset($this->_request['email']) and isset($this->_request['password'])){
+        $username = $this->_request['username'];
+        $email = $this->_request['email'];
+        $password = $this->_request['password'];
+
+        try{
+            $s = new Signup($username, $password, $email);
+            $data = [
+                "message" => "Signup success",
+                "userid" => $s->getInsertID()
+            ];
+            $this->response($this->json($data), 200);
+        } catch(Exception $e) {
+            $data = [
+                "error" => $e->getMessage()
+            ];
+            $this->response($this->json($data), 409);
+        }
+         
+    } else {
+        $data = [
+            "error" => "Bad request",
+            "method" => $this->get_request_method(),
+            "server" => $_SERVER,
+            "post" => $_POST,
+            "get" => $_GET
+        ];
+        $data = $this->json($data);
+        $this->response($data, 400);
+    }
+};
\ No newline at end of file
diff --git a/api/index.php b/api/index.php
index e56513f..815df11 100644
--- a/api/index.php
+++ b/api/index.php
@@ -9,6 +9,7 @@ class API extends REST {
     public $data = "";
     
     private $db = NULL;
+    private $current_call;
     
     public function __construct(){
         parent::__construct();                  // Init parent contructor
@@ -22,14 +23,39 @@ class API extends REST {
     */
     public function processApi(){
         $func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
-        if((int)method_exists($this,$func) > 0)
-        $this->$func();
-        else
-        $this->response('',400);                // If the method not exist with in this class, response would be "Page not found".
+        if((int)method_exists($this,$func) > 0){
+            $this->$func();
+        }
+        else {
+            if(isset($_GET['namespace'])){
+                $dir = $_SERVER['DOCUMENT_ROOT'].'/api/apis/'.$_GET['namespace'];
+                $methods = scandir($dir);
+                //var_dump($methods);
+                foreach($methods as $m){
+                    if($m == "." or $m == ".."){
+                        continue;
+                    }
+                    $basem = basename($m, '.php');
+                    //echo "Trying to call $basem() for $func()\n";
+                    if($basem == $func){
+                        include $dir."/".$m;
+                        $this->current_call = Closure::bind(${$basem}, $this, get_class());
+                        $this->$basem();
+                    }
+                }
+            } else {
+                //we can even process functions without namespace here.
+                $this->response($this->json(['error'=>'methood_not_found']),404);
+            }
+        }
     }
 
     public function __call($method, $args){
-        
+        if(is_callable($this->current_call)){
+            return call_user_func_array($this->current_call, $args);
+        } else {
+            $this->response($this->json(['error'=>'methood_not_callable']),404);
+        }
     }
     
     /*************API SPACE START*******************/
@@ -85,35 +111,6 @@ class API extends REST {
             $this->response($data,200);
         }
     }
-
-    private function signup(){
-        if($this->get_request_method() == "POST" and isset($this->_request['username']) and isset($this->_request['email']) and isset($this->_request['password'])){
-            $username = $this->_request['username'];
-            $email = $this->_request['email'];
-            $password = $this->_request['password'];
-
-            try{
-                $s = new Signup($username, $password, $email);
-                $data = [
-                    "message" => "Signup success",
-                    "userid" => $s->getInsertID()
-                ];
-                $this->response($this->json($data), 200);
-            } catch(Exception $e) {
-                $data = [
-                    "error" => $e->getMessage()
-                ];
-                $this->response($this->json($data), 409);
-            }
-             
-        } else {
-            $data = [
-                "error" => "Bad request"
-            ];
-            $data = $this->json($data);
-            $this->response($data, 400);
-        }
-    }
     
     
     
diff --git a/call.php b/call.php
index 1fa706c..4df4738 100644
--- a/call.php
+++ b/call.php
@@ -20,10 +20,9 @@ class Superhero {
         }
         $dir = __DIR__.'/api/apis';
         $methods = scandir($dir);
-
+        var_dump($methods);
         foreach($methods as $m){
             if($m == "." or $m == ".."){
-                echo $m;
                 continue;
             }
             $basem = basename($m, '.php');
-- 
GitLab