-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·168 lines (136 loc) · 4.03 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* ==============================
* Aegis Framework | MIT License
* http://www.aegisframework.com/
* ==============================
*/
/**
* Include Aegis Library
*
* This includes the custom error handlers and the autoload function
* required to load classes dynamically.
*/
include("lib/Aegis.php");
/**
* Debugging logs are shown on errors by default, to disable them,
* uncomment the following line.
*/
Aegis::$debugging = false;
/**
* Set domain name for Router
*
* This domain name is used for routing purposes and to load all resources
* correctly since it's used in the base tag for the pages. Set it to the
* path of your project.
*/
Router::$domain = Config::get("Domain");
$db = new Database(Config::get("DB_User"), Config::get("DB_Password"), Config::get("DB"));
$latest = "0.2.6";
$liteLatest = "0.1.0";
$session = new Session();
/**
* Register Routes
*
* Register all the custom routes for your site, the callback function
* will be executed when the route is accessed.
*/
Router::get("/", function(){
global $session, $latest;
return new main($latest);
});
Router::get("/logout", function(){
global $session;
$session -> set("logged", false);
$session -> end();
});
Router::post("/login", function(){
global $db, $session;
HTTP::type("json");
if($data = Request::post(["user", "password"])){
if($db -> exists("Citizen", "User", $data["user"])){
$password = $db -> select("Citizen", ["Password"], "User", $data["user"])[0]["Password"];
if(Password::compare($data["password"], $password)){
$session -> set("logged", true);
return new JSON($db -> select("Citizen", ["User", "Public", "Secret"], "User", $data["user"])[0]);
}else{
return new JSON(["error" => "Invalid Credentials"]);
}
}else{
return new JSON(["error" => "User does not exist"]);
}
}else{
return new JSON(["error" => "Invalid Request"]);
}
});
Router::post("/register", function(){
global $db;
HTTP::type("json");
if($data = Request::post(["user", "password"])){
if(!$db -> exists("Citizen", "User", $data["user"])){
$db -> insert("Citizen", [
"User" => $data["user"],
"Password" => Password::hash($data["password"])
]);
return new JSON(["status" => "Success"]);
}else{
return new JSON(["error" => "User already exists"]);
}
}else{
return new JSON(["error" => "Invalid Request"]);
}
});
Router::post("/key", function(){
global $db;
if($data = Request::post(["user", "password", "pub", "key"])){
if($db -> exists("Citizen", "User", $data["user"])){
$info = $db -> select("Citizen", ["Password", "Public"], "User", $data["user"])[0];
$pass = $info["Password"];
$public = $info["Public"];
if(is_null($public)){
if(Password::compare($data["password"], $pass)){
$db -> update("Citizen", [
"Public" => $data["pub"],
"Secret" => $data["key"]
], "User", $data["user"]);
HTTP::type("json");
return new JSON(["status" => "succcess"]);
}
}else{
return new JSON(["error" => "A key had already been set for this user"]);
}
}else{
return new JSON(["error" => "User does not exist"]);
}
}else{
return new JSON(["error" => "Invalid Request"]);
}
});
Router::get("/key/{user}", function($user){
global $db;
HTTP::type("json");
if($db -> exists("Citizen", "User", $user)){
$public = $db -> select("Citizen", ["Public"], "User", $user)[0]["Public"];
return new JSON(["key" => $public]);
}else{
return new JSON(["error" => "User does not exist"]);
}
});
Router::get("/latest", function(){
global $latest;
HTTP::type("json");
return new JSON(["version" => $latest]);
});
Router::get("/lite/latest", function(){
global $liteLatest;
HTTP::type("json");
return new JSON(["version" => $liteLatest]);
});
/**
* Make the router listen to requests.
*
* The router will now match any request to the previously registered
* routes and run the callback function of the match.
*/
Router::listen();
?>