-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.php
67 lines (62 loc) · 1.48 KB
/
async.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
<?php
/*
* LG - Looking Glass
*
* @author: Johan Hedberg <[email protected]>
*
* @license: See LICENSE file here: https://github.com/crazzy/lg
*/
# Inclusions
require "config.php";
require "lib/cache.php";
require "lib/util.php";
function lg_async_error() {
header("X-LG-Async-Status: error\r\n", TRUE);
die("error");
}
/* Check valid call and valid input data */
if(!isset($_SERVER['REQUEST_METHOD']) || ($_SERVER['REQUEST_METHOD'] != 'POST')) {
lg_async_error();
}
$async_id = isset($_POST['async_id']) ? $_POST['async_id'] : '';
if(!preg_match('/^[a-z0-9]+$/', $async_id)) {
lg_async_error();
}
$nextchunk = isset($_POST['nextchunk']) ? $_POST['nextchunk'] : '';
if(!preg_match('/^[0-9]+$/', $nextchunk)) {
lg_async_error();
}
/* Check status and if we have more data */
$status = LG_cache::get("async_$async_id");
if(false === $status) die("error");
header("X-LG-Async-Status: $status\r\n");
switch($status) {
case 'init':
die("init");
break;
case 'error':
lg_async_error();
break;
case 'data':
$data = LG_cache::get("async_{$async_id}_ch_{$nextchunk}");
if((false === $data) || empty($data)) {
header("X-LG-Async-Status: wait\r\n", TRUE);
die("wait");
}
else {
die($data);
}
break;
case 'complete':
$tot_chunks = LG_cache::get("async_{$async_id}_nochunks");
if(false === $tot_chunks) {
lg_async_error();
}
for($i = $nextchunk; $i <= $tot_chunks; ++$i) {
echo LG_cache::get("async_{$async_id}_ch_{$i}");
}
break;
default:
die("error");
break;
}