-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgetFrameData.php
More file actions
executable file
·49 lines (44 loc) · 1.7 KB
/
getFrameData.php
File metadata and controls
executable file
·49 lines (44 loc) · 1.7 KB
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
<?php
header("Content-Type: application/json", true);
//Caching: http://www.theukwebdesigncompany.com/articles/php-caching.php
$cachefile = "data/cache.json";
$cachetime = 10 * 60; // 10 minutes
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
readfile($cachefile);
exit;
}
ob_start(); // start the output buffer
include('./config.php');
$confobject = array('updatetime' => date(DATE_W3C));
//display what's in the frames table
try {
$DBH = new PDO(PDO_CONNECTION, DB_READ_USER, DB_READ_PASS);
// Debating combining votes table with frames table, but I'm inclined to keep them seperate so
// we're not writing to the frames table as much. Started discuccion with issue #57.
$STH = $DBH->query('
SELECT frames.frame, blink, link,
IFNULL(votes.voteyes, 0) AS \'yes\', IFNULL(votes.voteno, 0) AS \'no\',
(2 * voteyes > voteno && voteyes + voteno > 10) AS \'special\'
FROM frames
LEFT JOIN votes
ON votes.frame = frames.frame');
$STH->setFetchMode(PDO::FETCH_ASSOC);
echo "[".json_encode($confobject);
while($row = $STH->fetch()) {
echo ",";
//Warning: will only work, if column names and object-attribute names are consistent!
echo json_encode($row);
}
echo "]";
} catch(PDOException $e) {
$dblog = "./data/dblog.txt"; //Stores database exceptions.
file_put_contents($dblog, $eventtime . "\t" . $e->getMessage() . "\n", FILE_APPEND);
}
$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush();