From 517124b5c76c4ed715e3df3cce3c467f158fbbdb Mon Sep 17 00:00:00 2001
From: SivaShankar <n.sivashankar2002@gmail.com>
Date: Mon, 30 Dec 2024 10:44:12 +0000
Subject: [PATCH] Rearch completed

---
 .htaccess              |  18 +++---
 REST.api.php           | 125 ------------------------------------
 env.json               |   6 ++
 index.php              | 142 -----------------------------------------
 lib/Auth.class.php     |   1 -
 lib/Database.class.php |  22 -------
 lib/Signup.class.php   |  32 ----------
 pass.php               |  12 ----
 route.php              |  12 ----
 9 files changed, 16 insertions(+), 354 deletions(-)
 delete mode 100644 REST.api.php
 create mode 100644 env.json
 delete mode 100644 index.php
 delete mode 100644 lib/Auth.class.php
 delete mode 100644 lib/Database.class.php
 delete mode 100644 lib/Signup.class.php
 delete mode 100644 pass.php
 delete mode 100644 route.php

diff --git a/.htaccess b/.htaccess
index f640b8c..12a9a2d 100644
--- a/.htaccess
+++ b/.htaccess
@@ -1,12 +1,14 @@
 RewriteBase /
-# RewriteEngine On
+RewriteEngine On
 
-# RewriteCond %{REQUEST_FILENAME} !-d
-# RewriteCond %{REQUEST_FILENAME} !-s
-# RewriteRule ^(.*)$ index.php?rquest=$1 [QSA,NC,L]
+RewriteRule ^/?api/([^/]+)?$ api/index.php?rquest=$1 [QSA,NC,L]
 
-# RewriteCond %{REQUEST_FILENAME} -d
-# RewriteRule ^(.*)$ index.php [QSA,NC,L]
+## For general files, if not above, just remove .php
 
-# RewriteCond %{REQUEST_FILENAME} -s
-# RewriteRule ^(.*)$ index.php [QSA,NC,L]
\ No newline at end of file
+# Redirect external .php requests to 404 Error (Pretending that I am not doing PHP)
+RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
+RewriteRule ^(.+)\.php$ "http://%{HTTP_HOST}/$1" [R=404,L]
+
+# Resolve .php file for extensionless php urls
+RewriteCond %{REQUEST_FILENAME}\.php -f
+RewriteRule ^([^/.]+)$ $1.php [L]
\ No newline at end of file
diff --git a/REST.api.php b/REST.api.php
deleted file mode 100644
index 816e296..0000000
--- a/REST.api.php
+++ /dev/null
@@ -1,125 +0,0 @@
-<?php
-
-class REST
-{
-    public $_allow = array();
-    public $_content_type = "application/json";
-    public $_request = array();
-
-    private $_method = "";
-    private $_code = 200;
-
-    public function __construct()
-    {
-        $this->inputs();
-    }
-
-    public function get_referer()
-    {
-        return $_SERVER['HTTP_REFERER'];
-    }
-
-    public function response($data, $status)
-    {
-        $this->_code = ($status) ? $status : 200;
-        $this->set_headers();
-        echo $data;
-        exit;
-    }
-
-    private function get_status_message()
-    {
-        $status = array(
-                    100 => 'Continue',
-                    101 => 'Switching Protocols',
-                    200 => 'OK',
-                    201 => 'Created',
-                    202 => 'Accepted',
-                    203 => 'Non-Authoritative Information',
-                    204 => 'No Content',
-                    205 => 'Reset Content',
-                    206 => 'Partial Content',
-                    300 => 'Multiple Choices',
-                    301 => 'Moved Permanently',
-                    302 => 'Found',
-                    303 => 'See Other',
-                    304 => 'Not Modified',
-                    305 => 'Use Proxy',
-                    306 => '(Unused)',
-                    307 => 'Temporary Redirect',
-                    400 => 'Bad Request',
-                    401 => 'Unauthorized',
-                    402 => 'Payment Required',
-                    403 => 'Forbidden',
-                    404 => 'Not Found',
-                    405 => 'Method Not Allowed',
-                    406 => 'Not Acceptable',
-                    407 => 'Proxy Authentication Required',
-                    408 => 'Request Timeout',
-                    409 => 'Conflict',
-                    410 => 'Gone',
-                    411 => 'Length Required',
-                    412 => 'Precondition Failed',
-                    413 => 'Request Entity Too Large',
-                    414 => 'Request-URI Too Long',
-                    415 => 'Unsupported Media Type',
-                    416 => 'Requested Range Not Satisfiable',
-                    417 => 'Expectation Failed',
-                    500 => 'Internal Server Error',
-                    501 => 'Not Implemented',
-                    502 => 'Bad Gateway',
-                    503 => 'Service Unavailable',
-                    504 => 'Gateway Timeout',
-                    505 => 'HTTP Version Not Supported');
-        return ($status[$this->_code]) ? $status[$this->_code] : $status[500];
-    }
-
-    public function get_request_method()
-    {
-        return $_SERVER['REQUEST_METHOD'];
-    }
-
-    private function inputs()
-    {
-        switch ($this->get_request_method()) {
-            case "POST":
-                //$this->_request = $this->cleanInputs($_POST);
-                $this->_request =  $this->cleanInputs(array_merge($_GET, $_POST));
-                break;
-            case "GET":
-                $this->_request = $this->cleanInputs($_GET);
-                // no break
-            case "DELETE":
-                $this->_request = $this->cleanInputs($_GET);
-                break;
-            case "PUT":
-                parse_str(file_get_contents("php://input"), $this->_request);
-                $this->_request = $this->cleanInputs($this->_request);
-                break;
-            default:
-                $this->response('', 406);
-                break;
-        }
-    }
-
-    private function cleanInputs($data)
-    {
-        $clean_input = array();
-        if (is_array($data)) {
-            foreach ($data as $k => $v) {
-                $clean_input[$k] = $this->cleanInputs($v);
-            }
-        } else {
-            $data = trim(stripslashes($data));
-            $data = strip_tags($data);
-            $clean_input = trim($data);
-        }
-        return $clean_input;
-    }
-
-    private function set_headers()
-    {
-        header("HTTP/1.1 ".$this->_code." ".$this->get_status_message());
-        header("Content-Type:".$this->_content_type);
-    }
-}
diff --git a/env.json b/env.json
new file mode 100644
index 0000000..b8ae62e
--- /dev/null
+++ b/env.json
@@ -0,0 +1,6 @@
+{
+    "server": "mysql.selfmade.ninja",
+    "username": "Siva_shankar",
+    "password": "Assignment1@selfmadeninja",
+    "database": "Siva_shankar_apis"
+}
\ No newline at end of file
diff --git a/index.php b/index.php
deleted file mode 100644
index d6373f8..0000000
--- a/index.php
+++ /dev/null
@@ -1,142 +0,0 @@
-<?php
-
-error_reporting(E_ALL ^ E_DEPRECATED);
-require_once("REST.api.php");
-require_once("lib/Database.class.php");
-require_once("lib/Signup.class.php");
-
-class API extends REST
-{
-    public $data = "";
-
-    private $db = null;
-
-    public function __construct()
-    {
-        parent::__construct();                  // Init parent contructor
-        $this->db = Database::getConnection();  // Initiate Database connection
-    }
-
-    /*
-     * Public method for access api.
-     * This method dynmically call the method based on the query string
-     *
-     */
-    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".
-    }
-
-    /*************API SPACE START*******************/
-
-    private function about()
-    {
-
-        if ($this->get_request_method() != "POST") {
-            $error = array('status' => 'WRONG_CALL', "msg" => "The type of call cannot be accepted by our servers.");
-            $error = $this->json($error);
-            $this->response($error, 406);
-        }
-        $data = array('version' => $this->_request['version'], 'desc' => 'This API is created by Blovia Technologies Pvt. Ltd., for the public usage for accessing data about vehicles.');
-        $data = $this->json($data);
-        $this->response($data, 200);
-
-    }
-
-    private function verify()
-    {
-        if ($this->get_request_method() == "POST" and isset($this->_request['user']) and isset($this->_request['pass'])) {
-            $user = $this->_request['user'];
-            $password =  $this->_request['pass'];
-
-            $flag = 0;
-            if ($user == "admin") {
-                if ($password == "adminpass123") {
-                    $flag = 1;
-                }
-            }
-
-            if ($flag == 1) {
-                $data = [
-                    "status" => "verified"
-                ];
-                $data = $this->json($data);
-                $this->response($data, 200);
-            } else {
-                $data = [
-                    "status" => "unauthorized"
-                ];
-                $data = $this->json($data);
-                $this->response($data, 401);
-            }
-        } else {
-            $data = [
-                    "status" => "bad_request"
-                ];
-            $data = $this->json($data);
-            $this->response($data, 400);
-        }
-    }
-
-    private function test()
-    {
-        $data = $this->json(getallheaders());
-        $this->response($data, 200);
-    }
-
-    private function request_info()
-    {
-        $data = $this->json($_SERVER);
-    }
-
-    public function generate_hash()
-    {
-        $bytes = random_bytes(16);
-        return bin2hex($bytes);
-    }
-
-    private function gen_hash()
-    {
-        if (isset($this->_request['pass'])) {
-            $s = new Signup("", $this->_request['pass'], "");
-            $hash = $s->hashPassword();
-            $data = [
-                "hash" => $hash,
-                "info" => password_get_info($hash),
-                "val" => $this->_request['pass'],
-                "verify" => password_verify($this->_request['pass'], $hash),
-                "spot_verify" => password_verify($this->_request['pass'], password_hash($this->_request['pass'], PASSWORD_BCRYPT))
-            ];
-            $data = $this->json($data);
-            $this->response($data, 200);
-        }
-    }
-
-
-
-
-    /*************API SPACE END*********************/
-
-    /*
-        Encode array into JSON
-    */
-    private function json($data)
-    {
-        if (is_array($data)) {
-            return json_encode($data, JSON_PRETTY_PRINT);
-        } else {
-            return "{}";
-        }
-    }
-
-}
-
-// Initiiate Library
-
-$api = new API();
-$api->processApi();
diff --git a/lib/Auth.class.php b/lib/Auth.class.php
deleted file mode 100644
index b3d9bbc..0000000
--- a/lib/Auth.class.php
+++ /dev/null
@@ -1 +0,0 @@
-<?php
diff --git a/lib/Database.class.php b/lib/Database.class.php
deleted file mode 100644
index b99898d..0000000
--- a/lib/Database.class.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-class Database
-{
-    public static $db;
-    public static function getConnection()
-    {
-        $config_json = file_get_contents('../../env.json');
-        $config = json_decode($config_json, true);
-        if (Database::$db != null) {
-            return Database::$db;
-        } else {
-            Database::$db = mysqli_connect($config['server'], $config['username'], $config['password'], $config['database']);
-            if (!Database::$db) {
-                die("Connection failed: ".mysqli_connect_error());
-            } else {
-                return Database::$db;
-            }
-        }
-    }
-
-}
diff --git a/lib/Signup.class.php b/lib/Signup.class.php
deleted file mode 100644
index d9d67d8..0000000
--- a/lib/Signup.class.php
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-
-require_once('Database.class.php');
-
-class Signup
-{
-    private $username;
-    private $password;
-    private $email;
-
-    private $db;
-
-    public function __construct($username, $password, $email)
-    {
-        $this->db = Database::getConnection();
-        $this->username = $username;
-        $this->password = $password;
-        $this->email = $email;
-    }
-
-    public function getInsertID()
-    {
-
-    }
-
-    public function hashPassword()
-    {
-        //echo $this->password;
-        return password_hash($this->password, PASSWORD_BCRYPT);
-    }
-
-}
diff --git a/pass.php b/pass.php
deleted file mode 100644
index 482f931..0000000
--- a/pass.php
+++ /dev/null
@@ -1,12 +0,0 @@
-<?php
-
-$options = [
-    'cost' => 12,
-];
-$p = password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
-echo $p;
-
-//sleep(5);
-
-$hash = "$2y$10$pTt2yMWDZ1RSiav0WmTuGeT.JNZUWwNb6vzmIV3lRh5wZfLuSut0u";
-var_dump(password_verify("rasmuslerdorf", $p));
diff --git a/route.php b/route.php
deleted file mode 100644
index c79c64f..0000000
--- a/route.php
+++ /dev/null
@@ -1,12 +0,0 @@
-<pre><?php
-
-print_r($GLOBALS);
-print_r($_SERVER);
-print_r($_REQUEST);
-print_r($_POST);
-print_r($_GET);
-print_r($_FILES);
-print_r($_ENV);
-print_r($_COOKIE);
-print_r($_SESSION);
-?></pre>
\ No newline at end of file
-- 
GitLab