fleetvu/vexpense/fleetvuApi/index.php
2017-06-22 16:11:47 +05:30

285 lines
8.8 KiB
PHP

<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
//header("Access-Control-Allow-Headers: X-Requested-With");
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
include 'db.php';
$system_path = 'vendor';
$application_folder = $system_path.'/vexpense';
define('ENVIRONMENT', 'development');
define('APPPATH', $application_folder.'/');
// Set the current directory correctly for CLI requests
if (defined('STDIN'))
{
chdir(dirname(__FILE__));
}
if (realpath($system_path) !== FALSE)
{
$system_path = realpath($system_path).'/';
}
// ensure there's a trailing slash
$system_path = rtrim($system_path, '/').'/';
/*
* -------------------------------------------------------------------
* Now that we know the path, set the main path constants
* -------------------------------------------------------------------
*/
// The name of THIS file
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
// The PHP file extension
// this global constant is deprecated.
define('EXT', '.php');
// Path to the system folder
define('BASEPATH', str_replace("\\", "/", $system_path));
// Path to the front controller (this file)
define('FCPATH', str_replace(SELF, '', __FILE__));
// Name of the "system folder"
define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));
require(BASEPATH.'database/Common.php');
require 'vendor/autoload.php';
use \Slim\Middleware\JwtAuthentication;
use \Slim\Middleware\JwtAuthentication\RequestPathRule;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use vexpense\Controller\Dashboard;
use vexpense\Controller\Trip;
use vexpense\Controller\Expense;
$app = new Slim\App();
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
];
$c = new \Slim\Container($configuration);
$app = new \Slim\App($c);
//$app->config('debug', true);
$app->add(new JwtAuthentication([
"secret" => 'secret',
"secure" => false,
"rules" => [
new RequestPathRule([
"path" => ["/dashboardEndTripList","/getRoutesList","/getVehicleDriverCustomerList","/addTrip","/getTrip","/getExpenseCategory","/addExpense","/addFuelExpense","/searchRouteList","/fuelTrackList"],
"passthrough" => ["/login","/public"]
])
],
"callback" => function ($request,$response,$args) use ($app) {
$app->jwt = $args["decoded"];
//echo'<pre>';print_r($args);echo'</pre>';exit;
}
]));
$app->post("/public", function ($request,$response,$args) use ($app) {
$dashboard = new Dashboard($app);
$dashboard->getDashboard();
//
exit;
$data = array("message"=>" success","data"=>"Secured Content");
echo json_encode($data);
return $response;
});
$app->get("/dashboardEndTripList", function ($request,$response,$args) use ($app) {
$dashboard = new Dashboard($app->jwt);
$response = $dashboard->dashboardEndTripList();
return $response;
});
$app->get("/getRoutesList", function ($request,$response,$args) use ($app) {
//echo'<pre>';print_r($app->jwt);echo'</pre>';exit;
$trip = new Trip($app->jwt);
$response = $trip->getRoutes();
return $response;
});
$app->get("/dashboardCountList", function ($request,$response,$args) use ($app) {
$dashboard = new Dashboard($app->jwt);
$response = $dashboard->dashboardAllCountList(true);
return $response;
});
$app->get("/getVehicleDriverCustomerList", function ($request,$response,$args) use ($app) {
$get = $request->getParams();
$getResponse = isset($get['response'])?$get['response']:'';
$trip = new Trip($app->jwt);
$response = $trip->getVehicleDriverCustomerData($getResponse);
return $response;
});
$app->get("/addTrip", function ($request,$response,$args) use ($app) {
$get = $request->getParams();
$trip = new Trip($app->jwt);
$response = $trip->tripForm($get);
return $response;
});
$app->get("/getExpenseCategory", function ($request,$response,$args) use ($app) {
$trip = new Expense($app->jwt);
$response = $trip->getExpenseCategory();
return $response;
});
$app->get("/getTrip", function ($request,$response,$args) use ($app) {
$get = $request->getParams();
$trip = new Expense($app->jwt);
$response = $trip->getTripList($get);
return $response;
});
$app->get("/addExpense", function ($request,$response,$args) use ($app) {
$get = $request->getParams();
$trip = new Expense($app->jwt);
$response = $trip->addExpenseRequest($get);
return $response;
});
$app->get("/addFuelExpense", function ($request,$response,$args) use ($app) {
$get = $request->getParams();
$trip = new Expense($app->jwt);
$response = $trip->addFuelExpenseRequest($get);
return $response;
});
$app->get("/searchRouteList", function ($request,$response,$args) use ($app) {
$get = $request->getParams();
$trip = new Trip($app->jwt);
$response = $trip->routeListData($get);
return $response;
});
$app->get("/fuelTrackList", function ($request,$response,$args) use ($app) {
$get = $request->getParams();
$trip = new Trip($app->jwt);
$response = $trip->fuelTrackData($get);
return $response;
});
$app->post("/login", function ($request,$response,$args) use ($app) {
$post = $request->getParams();
if($post['email'] =='' && $post['password'] == ''){
$data = array("message"=>" error","data"=>"Unable to process email or password missing");
return json_encode($data);
}
$sql = "SELECT * FROM tbl_admin as TU WHERE TU.admin_email=:email AND TU.admin_password=:password";
$password = sha1($post['password']);
try {
$db = getDB();
$stmt = $db->prepare($sql);
$stmt->bindParam("email", $post['email']);
$stmt->bindParam("password", $password);
$stmt->execute();
$updates = $stmt->fetch(PDO::FETCH_OBJ);
if(!$updates){
$data = array("message"=>" error","data"=>"Invalid useremail or Password");
return json_encode($data);
}
/* Here generate and return JWT to the client. */
$signer = new Sha256();
$token = (new Builder())->setIssuer('http://resico@fleetvu.in') // Configures the issuer (iss claim)
->setAudience('mobileApp') // Configures the audience (aud claim)
->setId($updates->id, true) // Configures the id (jti claim), replicating as a header item
->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
// ->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
// ->setExpiration(time() + 120) // Configures the expiration time of the token (exp claim)
->set('user_id', $updates->id) // Configures a new claim, called "uid"
->set('scope', array('read','write','delete')) // Configures a new claim, called "uid"
->sign($signer, 'secret') // creates a signature using "testing" as key
->getToken(); // Retrieves the generated token
if($updates->access == "Admin")
{
$data = array("message"=>"success","access_token"=>"".$token,"user_detail"=>$updates);
}
if($updates->access == "User")
{
$currentDate = date('Y-m-d');
//print_r($currentDate); die;
$id = $updates->user_id;
$sqlQuery = "SELECT * FROM tbl_trip WHERE trip_driver=:id AND date_format(str_to_date(trip_departure,'%m/%d/%Y'),'%Y-%m-%d')=:date";
$db = getDB();
$stmt = $db->prepare($sqlQuery);
$stmt->bindParam("id", $id);
$stmt->bindParam("date", $currentDate);
$stmt->execute();
$trips = $stmt->fetch(PDO::FETCH_OBJ);
//print_r($trips); die;
if(!$trips){
$data = array("message"=>" error","data"=>"You have no trips for today's date");
return json_encode($data);
}
$data = array("message"=>"success","access_token"=>"".$token,"user_detail"=>$updates,"user_trip_details"=>$trips);
}
return json_encode($data);
} catch(PDOException $e) {
return '{"error":{"text":'. $e->getMessage() .'}}';
}catch(Exception $e) {
return '{"error":{"text":'. $e->getMessage() .'}}';
}
});
$app->run();
?>