-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
144 lines (129 loc) · 4.63 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* API MANAGEMENT Created by Ibnu Maksum
*/
use \Firebase\JWT\JWT;
header('Access-Control-Allow-Origin: *');
date_default_timezone_set("Asia/Jakarta");
error_reporting(E_ERROR);
include "vendor/autoload.php";
include "config.php";
include "function.php";
use Medoo\Medoo;
$_db = new Medoo([
// required
'database_type' => 'mysql',
'database_name' => $db_name,
'server' => $db_host,
'username' => $db_user,
'password' => $db_pass
]);
//Parsing URL
$_path = array_values(array_filter(explode("/",parse_url($_SERVER['REQUEST_URI'])['path'])));
$_jml = count($_path);
$_admin = 'admin';
if(isset($_path[0]) && $_path[0]==$_admin){
session_start();
$tpl = new League\Plates\Engine('template');
if(empty($_SESSION['EMAIL'])){
include "modules/login.php";
}
//All Methods
$tpl->addData([
'_methods' => ['GET','POST','PUT','DELETE','PATCH'],
'_types' => ['http','sql','php','plain','echo'],
'_dbs' => ['mysql', 'mssql', 'oracle', 'pgsql'],
'_env' => ['development','staging','production']
]);
//foo/bar.php
if(file_exists($modul = "modules/".$_path[1]."/".$_path[2].".php")){
$tpl->addData(['crumbs' => $crumbs=[$_path[0],$_path[1],$_path[2]]]);
unset($_path[0],$_path[1],$_path[2]);
$_path = array_values($_path);
include $modul;
//foo/foo.php
}else if(file_exists($modul = "modules/".$_path[1]."/".$_path[1].".php")){
$tpl->addData(['crumbs' => $crumbs=[$_path[0],$_path[1]]]);
unset($_path[0],$_path[1]);
$_path = array_values($_path);
include $modul;
die();
//foo.php
}else if(file_exists($modul = "modules/".$_path[1].".php")){
$tpl->addData(['crumbs' => $crumbs=[$_path[0],$_path[1]]]);
unset($_path[0],$_path[1]);
$_path = array_values($_path);
include $modul;
die();
}else{
$tpl->addData(['crumbs' => [$_path[0]]]);
include "modules/home.php";
}
}else{
$env = 'production';
if($_path[0]=='dev'){
unset($_path[0]);
$_path = array_values($_path);
$env = 'development';
}else if($_path[0]=='sta'){
unset($_path[0]);
$_path = array_values($_path);
$env = 'staging';
}
$fields = ['id', 'methods', 'route_type', 'content_type', 'db_id', 'auth_id', 'content', 'retry', 'retry_delay', 'timeout'];
if(isset($_path[0],$_path[1],$_path[2])){
$routes = $_db->select('api_routes', $fields,
['AND'=>['version'=>$_path[0], 'category'=>$_path[1], 'function'=>$_path[2],'environment'=>$env,'enabled'=>1]]
);
if(!isset($routes) || !is_array($routes) || count($routes)==0){
$routes = $_db->select('api_routes', $fields,
['AND'=>['version'=>$_path[0], 'category'=>$_path[1],'environment'=>$env,'enabled'=>1]]
);
unset($_path[0],$_path[1]);
$_path = array_values($_path);
}else{
unset($_path[0],$_path[1],$_path[2]);
$_path = array_values($_path);
}
}else if(isset($_path[0],$_path[1])){
$routes = $_db->select('api_routes', $fields,
['AND'=>['version'=>$_path[0], 'category'=>$_path[1],'environment'=>$env,'enabled'=>1]]
);
unset($_path[0],$_path[1]);
$_path = array_values($_path);
}
if(!isset($routes) || !is_array($routes) || count($routes)==0){
sendError('no Route matched ');
}
foreach($routes as $route){
//CHECK METHOD
if(strpos($route['methods'],$_SERVER["REQUEST_METHOD"])===false){
sendError("Method Not Allowed",['HTTP/1.0 405 Method Not Allowed',"Content-Type: Application/json"]);
}else{
break;
}
}
//recount
$_jml = count($_path);
//analytics
analytics($route['id']);
if($route['auth_id']>0){
$auth = $_db->get('api_auth',['jwt_secret', 'expired', 'header'],['id'=>$route['auth_id']]);
$token = getallheaders()[$auth['header']];
if(empty($token)){
sendError("Unauthorized",['HTTP/1.0 401 Unauthorized']);
}else{
try{
$decoded = JWT::decode($token, $auth['jwt_secret'], array('HS256'));
}catch(Exception $e){
sendError("Unauthorized",['HTTP/1.0 401 Unauthorized']);
}
}
}
//find processor based route type
if(file_exists('processor/'.$route['route_type'].'.php')){
include 'processor/'.$route['route_type'].'.php';
}else{
sendError('no Route matched.');
}
}