Skip to content
Snippets Groups Projects
index.php 4.2 KiB
Newer Older
Sibidharan's avatar
Sibidharan committed
<?php
    error_reporting(E_ALL ^ E_DEPRECATED);
    require_once("REST.api.php");
Sibidharan's avatar
Sibidharan committed
    require_once("lib/Database.class.php");
    require_once("lib/Signup.class.php");
Sibidharan's avatar
Sibidharan committed

    class API extends REST {

        public $data = "";

        private $db = NULL;

        public function __construct(){
Sibidharan's avatar
Sibidharan committed
            parent::__construct();                  // Init parent contructor
            $this->db = Database::getConnection();  // Initiate Database connection
Sibidharan's avatar
Sibidharan committed
        }

        /*
         * 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);
            }
Sibidharan's avatar
Sibidharan committed
            $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.');
Sibidharan's avatar
Sibidharan committed
            $data = $this->json($data);
            $this->response($data,200);

        }

        private function verify(){
Sibidharan's avatar
Sibidharan committed
            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;
                    }
Sibidharan's avatar
Sibidharan committed
                }

Sibidharan's avatar
Sibidharan committed
                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);
                }
Sibidharan's avatar
Sibidharan committed
            } else {
                $data = [
Sibidharan's avatar
Sibidharan committed
                        "status" => "bad_request"
                    ];
                    $data = $this->json($data);
                    $this->response($data,400);
Sibidharan's avatar
Sibidharan committed
            }
        }

        private function test(){
                $data = $this->json(getallheaders());
                $this->response($data,200);
        }

Sibidharan's avatar
Sibidharan committed
        private function request_info(){
            $data = $this->json($_SERVER);
        }

Sibidharan's avatar
Sibidharan committed
        function generate_hash(){
            $bytes = random_bytes(16);
            return bin2hex($bytes);
        }

Sibidharan's avatar
Sibidharan committed
        private function gen_hash(){
            if(isset($this->_request['pass'])){
                $s = new Signup("", $this->_request['pass'], "");
                $hash = $s->hashPassword();
                $data = [
                    "hash" => $hash,
Sibidharan's avatar
Sibidharan committed
                    "info" => password_get_info($hash),
Sibidharan's avatar
Sibidharan committed
                    "val" => $this->_request['pass'],
Sibidharan's avatar
Sibidharan committed
                    "verify" => password_verify($this->_request['pass'], $hash),
                    "spot_verify" => password_verify($this->_request['pass'], password_hash($this->_request['pass'], PASSWORD_BCRYPT))
Sibidharan's avatar
Sibidharan committed
                ];
                $data = $this->json($data);
                $this->response($data,200);
            }
        }

Sibidharan's avatar
Sibidharan committed



        /*************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();
?>