This repository has been archived by the owner on Jan 1, 2020. It is now read-only.
/
Track.php
executable file
·90 lines (76 loc) · 2.44 KB
/
Track.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
<?php
namespace Controller;
use Model\Bucket;
class Track extends Base\Page
{
public $formats = array('json');
public function req_get ()
{
header('Content-Type: text/plain');
$req = $this->app->request;
$params = array_merge($req->query->all(), $req->request->all());
$format = 'json';
$dataload = $req->get('data') ? json_decode(urldecode($req->get('data')), true) : array();
$payload = $req->get('payload') ? json_decode(urldecode($req->get('payload')), true) : array();
$data = array_merge($dataload, $payload);
// Event is required
$event = $req->get('event') ?: (
isset($data['event'])
? $data['event']
: (
// Legacy, will be removed in next version
(! empty($this->uri[1]))
? $this->uri[1]
: false
)
);
if (! $event) {
echo '500 No Event Specified';
exit;
}
// Get Bucket Instance
if ($req->get('bucket')) {
$bucket_id = $req->get('bucket');
} else {
$bucket_id = array_key_exists('appkey', $params)
? $params['appkey'] : (
array_key_exists('appkey', $data)
? $data['appkey']
: false
);
}
$bucket_id = trim($bucket_id);
if (! $bucket_id) {
echo '500 No Bucket ID Specified';
exit;
}
$bucket = Bucket::findById($bucket_id);
if (! $bucket) {
echo '500 Invalid Bucket ID';
exit;
}
// Normalize Data
unset($data['event']);
unset($data['appkey']);
unset($data['bucket']);
unset($data['sig'], $data['hash']);
// Append to data
$insert = array();
$insert['t'] = new \MongoDate();
$insert['e'] = $event;
$insert['d'] = $data;
// Save Data to log
try {
$collection = $this->app->mongo->selectCollection($bucket->event_collection);
$collection->insert($insert);
echo $insert['_id'];
exit;
}
// Could not connect (this is where queue will be needed)
catch (MongoConnectionException $e) {
echo '503 Database Exception';
exit;
}
exit;
}
}