-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.php
289 lines (266 loc) · 10 KB
/
handler.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?
// Set Custom Error & Exception Handlers
set_error_handler('customErrorHandler');
set_exception_handler('customExceptionHandler');
// Initialize API
$ajax = new Ajax();
// Interpret the API request
$ajax->interpretAjax(
[ // Public Classes
'OperationsManager',
'GrBC',
'NotificationService',
'FileSystem',
'Home',
'AcademicsService',
'Applications',
'Admissions',
'AcademicProgress',
'FormsService',
'FinancesService',
'Transcript',
'Schedule',
'LibraryService',
'Viva',
'Moodle'
]
);
// Log the API request
$ajax->logRequest();
class Ajax {
function __construct(){
if(count($_GET) >= count($_POST)){
$this->method = 'GET';
$this->r = $this->getArgs("GET");
} else {
$this->method = 'POST';
$this->r = $this->getArgs("POST");
}
global $session;
if(isset($this->r->t)){
$session = new Session($this->r->t);
$this->session = $session;
} else {
$session = new Session();
$this->session = $session;
}
// ---------------------------------------------
// include_once 'exporter.php';
// export($this->session);
// ---------------------------------------------
}
public function getArgs($method = "GET"){
$this->args = null;
if($method == "GET"){
if(isset($_GET["r"])) $this->args = $_GET["r"];
} else {
if(isset($_POST["r"])) $this->args = $_POST["r"];
}
if(isset($this->args)) {
// JSON decode the request
$jsonDecodedRequest = json_decode($this->args);
// Check and report JSON decoding errors
if($jsonDecodedRequest === null) {
interpretJSONLastError(json_last_error());
}
// Return the decoded JSON
return $jsonDecodedRequest;
}
}
public function interpretAjax($classes){
if(is_object($this->r)){
if($this->r){
// $this->r->q needs to be an Array so that it can support multiple queries coming from a single Ajax request
if(!is_array($this->r->q)){
$this->r->q = [$this->r->q];
}
// $response is an Array so that it can hold multiple results of multiple queries
$response = [];
// Iterate queries
foreach($this->r->q as $q){
$methodFound = false;
// Iterate known $classes to find the one that has the method requested by the query
foreach($classes as $className) {
$class = new $className($this->session);
if (method_exists($className, $q)) {
$methodFound = true;
// Check if Access Control allows for User to call the method
if ($this->session->authenticationService->authenticateOperation($q)) {
// User has Access to this method
array_push($response, $class->{$q}(isset($this->r->p) ? $this->r->p : null));
} else {
// User does not have access to this method
throw new Exception(__METHOD__.': Access Denied: <font style="color: red">'.$q.'</font>');
}
break;
}
}
if(!$methodFound){
throw(new Exception(__METHOD__.': Unknown API Method: <font style="color: red">'.$q.'</font>'));
// array_push($response, new AjaxError(__METHOD__.': Unknown API Method: <font style="color: red">'.$q.'</font>'));
}
}
if(!empty($response)) {
if (count($response) > 1) {
$jsonEncodedResponse = json_encode($response);
} else {
$jsonEncodedResponse = json_encode($response[0]);
}
if ($jsonEncodedResponse === false) {
echo json_encode(new AjaxError(interpretJSONLastError(json_last_error())));
} else {
echo $jsonEncodedResponse;
}
}
}
}
}
public function logRequest(){
if(
isset($this->r) &&
$this->r->q[0] !== "getImage" &&
$this->r->q[0] !== "getNotificationsLength" &&
$this->r->q[0] !== "getNotifications"
){
$userId = isset($this->session->authenticationService->user->id)? $this->session->authenticationService->user->id: null;
$return = $this->session->db->sql([
'statement' => 'INSERT INTO',
'table' => 'admin_logs',
'columns' => 'user_id, post_data',
'values' => [
$userId,
$this->args
]
]);
}
}
}
class AjaxResponse {
function __construct($data = null, $success = true, $reason = null) {
$this->data = $data;
$this->success = $success;
if(!$success){
$this->errorReason = $reason;
}
}
}
class AjaxError {
function __construct($errorReason = 'Unknown Error', $errorData = null) {
global $session;
$this->errorReason = $errorReason;
if($errorData !== null) {
$this->errorData = $errorData;
}
if($_SERVER['SERVER_NAME'] === 'www.grbc.gr') {
(new NotificationService())->send([
"toEmails" => DEV_EMAIL,
"subject" => '⚠️ JoOS Error Report',
"message" => '<b>'.$errorReason.'</b><pre>'.print_r($errorData, true).'</pre><pre>'.print_r($session->authenticationService->user, true).'</pre>'
]);
}
}
}
function customExceptionHandler($e) {
global $session;
if(!isset($session)){
global $dbSettings;
include_once 'classes/dbcontroller.php';
$session = (object) [
"db" => new DBController($dbSettings)
];
}
$uri = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$return = $session->db->sql([
'statement' => 'INSERT INTO',
'table' => 'admin_errors',
'columns' => 'source, userId, message, uri, file, line',
'values' => [
'api',
isset($session->authenticationService->user->id) ?
$session->authenticationService->user->id : null,
$e->getMessage(),
$uri,
$e->getFile(),
$e->getLine()
]
]);
echo json_encode(new AjaxResponse((object) [
"source" => 'api',
"code" => $e->getCode(),
"message" => $e->getMessage(),
"uri" => $uri,
"file" => $e->getFile(),
"line" => $e->getLine()
], false, $e->getMessage()));
}
function interpretJSONLastError ($error) {
switch ($error) {
case JSON_ERROR_DEPTH:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_DEPTH</font>';
case JSON_ERROR_STATE_MISMATCH:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_STATE_MISMATCH</font>';
case JSON_ERROR_CTRL_CHAR:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_CTRL_CHAR</font>';
case JSON_ERROR_SYNTAX:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_SYNTAX</font>';
case JSON_ERROR_UTF8:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_UTF8</font>';
case JSON_ERROR_RECURSION:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_RECURSION</font>';
case JSON_ERROR_INF_OR_NAN:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_INF_OR_NAN</font>';
case JSON_ERROR_UNSUPPORTED_TYPE:
return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_UNSUPPORTED_TYPE</font>';
// Available since PHP 7.0.0
// case JSON_ERROR_INVALID_PROPERTY_NAME:
// return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_INVALID_PROPERTY_NAME</font>';
// Available since PHP 7.0.0
// case JSON_ERROR_UTF16:
// return __METHOD__.': Malformed Request: <font style="color: red">JSON_ERROR_UTF16</font>';
default:
return __METHOD__.': Malformed Request: <font style="color: red">Unknown error</font>';
}
}
function dbg($p, $die = true){
if(is_array($p) || is_object($p)){
print_r($p);
} else {
echo($p);
}
if($die){
die();
}
}
function booleanize($mySQLTinyIntValue){
if($mySQLTinyIntValue === "1"){
return true;
} elseif($mySQLTinyIntValue === "0"){
return false;
} else {
return $mySQLTinyIntValue;
}
}
function JSFDates($rows, $columnName) {
$returnObject = false;
if(!is_array($rows)) {
$returnObject = true;
$rows = [$rows];
}
foreach($rows as $i => $row) {
$rows[$i]->{$columnName} = $rows[$i]->{$columnName} === null ? null : str_replace('-', '/', $rows[$i]->{$columnName});
}
return $returnObject ? $rows[0] : $rows;
}
function fDate($date = null, $format = '%d %b. %Y', $locale = 'greek') {
if(!isset($date)){
$date = date('Y-m-d');
}
setlocale(LC_CTYPE, $locale);
setlocale(LC_TIME, $locale);
$return = iconv('Windows-1253', 'UTF-8', ltrim(strftime($format, strtotime($date)), '0'));
// This only works locally
$return = str_replace('Μαϊ', 'Μαΐ', $return);
// This only works online
$return = str_replace('Μάι', 'Μαΐ', $return);
return $return;
}