-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
60 lines (54 loc) · 1.51 KB
/
api.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
<?php
/*
* API.php
* Handles ajax requests and other API calls
*/
//Initialize Key Classes
require_once 'assets/boot.php';
$action = isset($_REQUEST['action'])? $_REQUEST['action'] : 'help';
switch ($action){
case 'getLevel':
//Sanitize Inputs
$id = isset($_REQUEST['id'])? sanitize($_REQUEST['id']) : 1;
//Formulate Response
$response = getLevel($id);
$responseType = 'application/json';
break;
case 'help':
default:
$response = "No help available for this service.";
$responseType = 'text/html';
break;
}
//Publish Response
header("Content-type: $responseType");
header('Cache-Control: no-store, no-cache, must-revalidate');
echo ($responseType == 'application/json')? json_encode($response) : $response;
//End API Logic
die;
//Support Functions
/**
* Get level by level id
* Returns the level object in $reponse->result
* @param integer $levelId
* @return stdClass $response
*/
function getLevel($id){
$id = intval($id);
$response = new stdClass();
$response->result = false;
//Fetch from db
$db = Database::obtain();
try {
$response->result = $db->levels->findOne(array('id'=>$id));
} catch (Exception $e){
$response->error = true;
$response->errorMsg = "Unable to find level $id";
global $logger;
if ($logger instanceof Logger){
$logger->error("Unable to find level $id\n$e");
}
}
return $response;
}
?>