Skip to content
Snippets Groups Projects
Commit 517124b5 authored by SivaShankar's avatar SivaShankar
Browse files

Rearch completed

parent 1eb9a597
No related branches found
No related tags found
No related merge requests found
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
<?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);
}
}
{
"server": "mysql.selfmade.ninja",
"username": "Siva_shankar",
"password": "Assignment1@selfmadeninja",
"database": "Siva_shankar_apis"
}
\ No newline at end of file
<?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();
<?php
<?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;
}
}
}
}
<?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);
}
}
<?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));
<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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment