-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
123 lines (101 loc) · 2.6 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
<?php
// MySQL credentials and SQL queries.
define("DB_USER", getenv("MYSQL_USER"));
define("DB_PASS", getenv("MYSQL_PASS"));
define("DB_NAME", getenv("MYSQL_NAME"));
define("DB_HOST", getenv("MYSQL_HOST"));
// A label to use for logging errors (for greping syslog, yo).
define("LOG_LABEL", "SchoolData");
// Include required classes.
require 'classes/limonade/limonade.php';
require 'classes/db/class.db.php';
// Routes for HTTP requests.
dispatch('/', 'schoolsSummary');
dispatch('/closing', 'schoolClosingSummary');
dispatch('/:code/:data', 'schoolLookup');
// Run this sucker!
run();
/*
* Return summary school info.
*/
function schoolsSummary() {
try {
// Get callback parameter (if used).
@$callback = $_GET["callback"];
// Render JSON response.
return generateResponse('GetSchoolSummary', $callback);
}
catch (DBException $ex) {
logError($ex->getMessage());
header("HTTP/1.1 500 Internal Server Error");
}
}
/*
* Return summary for schools slated for closure.
*/
function schoolClosingSummary() {
try {
// Get callback parameter (if used).
@$callback = $_GET["callback"];
// Render JSON response.
return generateResponse('GetSchoolClosingSummary', $callback);
}
catch (DBException $ex) {
logError($ex->getMessage());
header("HTTP/1.1 500 Internal Server Error");
}
}
/*
* Lookup school information.
*/
function schoolLookup() {
try {
// Extract parameters from URL.
$code = (int) params('code');
$data = params('data');
// Get callback parameter (if used).
@$callback = $_GET["callback"];
// Render JSON response.
$data = $data ? $data : 'school_information';
return generateResponse('GetSchoolData', $callback, array($data, $code));
}
catch (DBException $ex) {
logError($ex->getMessage());
header("HTTP/1.1 500 Internal Server Error");
}
}
/*
* Generate the JSON response from the API.
*/
function generateResponse($name, $callback, Array $parameters=array()) {
$response = json_encode(getData($name, $parameters));
if($callback) {
header('Content-type: application/javascript');
return "$callback($response);";
}
else {
header('Content-type: application/json');
return $response;
}
}
/*
* Run SQL query to get school data.
*/
function getData($name, Array $parameters=array()) {
$db = new DB(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$result = $db->executeStatement($name, $parameters);
$json_array = array();
while($row = mysqli_fetch_assoc($result)) {
array_push($json_array, $row);
}
return $json_array;
}
/*
* Log an error.
*/
function logError($error) {
openlog(LOG_LABEL, LOG_PID, LOG_LOCAL0);
syslog(LOG_ERR, $error);
closelog();
}
?>