Skip to content
Snippets Groups Projects
Commit ee3b26ae authored by sanjay 001's avatar sanjay 001
Browse files

improved code architechture

parent 14a15d02
No related branches found
No related tags found
No related merge requests found
RewriteBase /
RewriteEngine On
RewriteRule ^/?api/([^/]+)?$ api/index.php?rquest=$1 [QSA,NC,L]
# Rewrite API requests
RewriteRule ^/?api/([^/]+)/([^/]+)?$ api/index.php?namespace=$1&rquest=$2 [L,QSA,NC]
RewriteRule ^/?api/([^/]+)?$ api/index.php?rquest=$1 [L,QSA,NC]
## For general files, if not above, just remove .php
# Redirect .php requests to 404 error
RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule ^ - [R=404,L]
# 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
# Resolve extensionless .php files
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)$ $1.php [L]
<?php
${basename(__FILE__, '.php')} = function()
{
};
\ No newline at end of file
<?php
${basename(__FILE__, '.php')} = function()
{
if (
$this->get_request_method() == "POST" and
isset($this->_request["user"]) and
isset($this->_request["pass"]) and
isset($this->_request["email"])
) {
try {
$userobj = new signup(
$this->_request["user"],
$this->_request["pass"],
$this->_request["email"]
);
$data = [
"message" => "signup successful",
"userid" => $userobj->getinsertid(),
];
$data = $this->json($data);
$this->response($data, 200);
} catch (Exception $e) {
$data = [
"error" => $e->getMessage(),
];
$data = $this->json($data);
$this->response($data, 409);
}
} else {
$data = [
"status" => "bad_request",
];
$data = $this->json($data);
$this->response($data, 400);
}
};
\ No newline at end of file
<?php
${basename(__FILE__, '.php')} = function(){
$power = $this->name." has superior intelligence and he is rich.";
return $power;
};
......@@ -2,7 +2,7 @@
error_reporting(E_ALL ^ E_DEPRECATED);
require_once($_SERVER['DOCUMENT_ROOT']."/api/REST.api.php");
require_once($_SERVER['DOCUMENT_ROOT']."/api/lib/database.class.php");
require_once($_SERVER['DOCUMENT_ROOT']."/api/lib/Signup.class.php");
require_once($_SERVER['DOCUMENT_ROOT']."/api/lib/signup.class.php");
// require_once($_SERVER['DOCUMENT_ROOT']."/api/lib/User.class.php");
require_once($_SERVER['DOCUMENT_ROOT']."/api/lib/Auth.class.php");
......@@ -10,11 +10,11 @@ require_once($_SERVER['DOCUMENT_ROOT']."/api/lib/Auth.class.php");
class API extends REST
{
public $data = "";
public $current_call;
public function __construct()
{
parent::__construct(); // Init parent contructor
database::getconnection(); // Initiate Database connection
}
/*
......@@ -22,77 +22,50 @@ class API extends REST
* 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) {
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 = [
"status" => "WRONG_CALL",
"msg" => "The type of call cannot be accepted by our servers.",
];
$error = $this->json($error);
$this->response($error, 406);
}
$data = [
"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;
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();
}
}
}
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);
//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 {
$data = [
"status" => "bad_request",
];
$data = $this->json($data);
$this->response($data, 400);
$this->response($this->json(['error'=>'methood_not_callable']),404);
}
}
private function test()
/*************API SPACE START*******************/
private function test() // This is for testing purpose
{
$data = $this->json(getallheaders());
$this->response($data, 200);
......@@ -101,44 +74,9 @@ class API extends REST
private function request_info()
{
$data = $this->json($_SERVER);
$this->response($data, 200);
}
function signup()
{
if (
$this->get_request_method() == "POST" and
isset($this->_request["user"]) and
isset($this->_request["pass"]) and
isset($this->_request["email"])
) {
try {
$userobj = new signup(
$this->_request["user"],
$this->_request["pass"],
$this->_request["email"]
);
$data = [
"message" => "signup successful",
"userid" => $userobj->getinsertid(),
];
$data = $this->json($data);
$this->response($data, 200);
} catch (Exception $e) {
$data = [
"error" => $e->getMessage(),
];
$data = $this->json($data);
$this->response($data, 409);
}
} else {
$data = [
"status" => "bad_request",
];
$data = $this->json($data);
$this->response($data, 400);
}
}
/*************API SPACE END*********************/
......
......@@ -32,6 +32,7 @@ class signup
throw new Exception("Failed : " . $this->db->error);
} else {
$this->userid = $this->db->insert_id;
// $this->sendVerificationMail();
}
}
......
call.php 0 → 100644
<pre><?php
class Superhero {
public $name;
public function __construct($name){
$this->name = $name;
}
public function __call($method, $args){
echo "Method Called: $method\n";
var_dump($args);
$methods = get_class_methods('Superhero');
var_dump($methods);
foreach($methods as $m){
if($m == $method){
echo("Calling the private function from __call(): ".$m."\n");
return $this->$m();
}
}
$dir = __DIR__.'/api/apis';
$methods = scandir($dir);
foreach($methods as $m){
if($m == "." or $m == ".."){
echo $m;
continue;
}
$basem = basename($m, '.php');
echo "Trying to call $basem() for $method()\n";
if($basem == $method){
include $dir."/".$m;
$func = Closure::bind(${$basem}, $this, get_class());
if(is_callable($func)){
return call_user_func_array($func, $args);
} else {
echo "Something is wrong";
}
}
}
}
private function getName(){
return $this->name;
}
}
$hero = new Superhero("Batman");
echo $hero->getName()."\n";
echo $hero->get_powers();
?>
</pre>
\ No newline at end of file
test.php 0 → 100644
<?php
if("fs")
{
echo "true";
}
else{
echo 'false';
}
\ No newline at end of file
<?php
$token = $_GET['token'];
echo "verified";
\ No newline at end of file
require_once $_SERVER['DOCUMENT_ROOT'].'/api/lib/signup.class.php';
require_once($_SERVER['DOCUMENT_ROOT']."/api/lib/database.class.php");
$token = mysqli_real_escape_string(database::getConnection(), $_GET['token']);
try{
if(signup::verifyAccount($token)){
?>
<h1 style="color: green">Verified</h1>
<?php
} else {
?>
<h1 style="color: red">Cannot Verify</h1>
<?php
}
}
catch(Exception $e){
?>
<h1 style="color: orange"><?=$e->getMessage()?></h1>
<?php
}
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